33# AUTOGENERATED! DO NOT EDIT! File to edit: ../00_core.ipynb.
44
55# %% auto 0
6- __all__ = ['GH_HOST' , 'EMPTY_TREE_SHA' , 'print_summary' , 'GhApi' , 'date2gh' , 'gh2date' ]
6+ __all__ = ['GH_HOST' , 'img_md_pat' , ' EMPTY_TREE_SHA' , 'print_summary' , 'GhApi' , 'date2gh' , 'gh2date' , 'replace_md_imgs ' ]
77
88# %% ../00_core.ipynb 2
99from fastcore .all import *
1717from datetime import datetime ,timedelta
1818from pprint import pprint
1919from time import sleep
20- import os
20+ import os , shutil , tempfile , git
2121
2222# %% ../00_core.ipynb 4
2323GH_HOST = os .getenv ('GH_HOST' , "https://api.github.com" )
@@ -163,19 +163,37 @@ def gh2date(dtstr:str)->datetime:
163163 return datetime .fromisoformat (dtstr .replace ('Z' , '' ))
164164
165165# %% ../00_core.ipynb 69
166+ img_md_pat = re .compile (r'!\[(?P<alt>.*?)\]\((?P<url>[^\s]+)\)' )
167+ def replace_md_imgs (content , func ): return img_md_pat .sub (func , content )
168+
166169@patch
167- def create_gist (self :GhApi , description , content , filename = 'gist.txt' , public = False ):
168- "Create a gist containing a single file"
169- return self .gists .create (description , public = public , files = {filename : {"content" : content }})
170+ def create_gist (self :GhApi , description , content , filename = 'gist.txt' , public = False , img_paths = None ):
171+ 'Create a gist, optionally with images where each md img url will be placed with img upload urls.'
172+ gist = self .gists .create (description , public = public , files = {filename : {"content" : content }})
173+ if not img_paths : return gist
174+ with tempfile .TemporaryDirectory () as clone_dir :
175+ gist_id , gist_url = gist .id , gist .html_url
176+ token = self .headers ['Authorization' ].split ('token ' )[1 ]
177+ repo = git .Repo .clone_from (f'https://{ token } @gist.github.com/{ gist_id } .git' , clone_dir )
178+ clone_dir , img_paths = Path (clone_dir ), L (img_paths ).map (Path )
179+ for o in img_paths :
180+ shutil .copy2 (o , clone_dir / o .name )
181+ repo .index .add ([o .name ])
182+ repo .index .commit ("Add images" )
183+ repo .remotes .origin .push ()
184+ updated_gist = self .gists .get (gist_id )
185+ img_urls = {o .name : updated_gist .files [o .name ].raw_url for o in img_paths }
186+ content = replace_md_imgs (content , lambda m : f"![{ m ['alt' ]} ]({ img_urls .get (m ['url' ], m ['url' ])} )" )
187+ return self .gists .update (gist_id , files = {filename :{'content' :content }})
170188
171- # %% ../00_core.ipynb 74
189+ # %% ../00_core.ipynb 76
172190@patch
173191def delete_release (self :GhApi , release ):
174192 "Delete a release and its associated tag"
175193 self .repos .delete_release (release .id )
176194 self .git .delete_ref (f'tags/{ release .tag_name } ' )
177195
178- # %% ../00_core.ipynb 76
196+ # %% ../00_core.ipynb 78
179197@patch
180198def upload_file (self :GhApi , rel , fn ):
181199 "Upload `fn` to endpoint for release `rel`"
@@ -184,7 +202,7 @@ def upload_file(self:GhApi, rel, fn):
184202 mime = mimetypes .guess_type (fn , False )[0 ] or 'application/octet-stream'
185203 return self (url , 'POST' , headers = {'Content-Type' :mime }, query = {'name' :fn .name }, data = fn .read_bytes ())
186204
187- # %% ../00_core.ipynb 77
205+ # %% ../00_core.ipynb 79
188206@patch
189207def create_release (self :GhApi , tag_name , branch = 'master' , name = None , body = '' ,
190208 draft = False , prerelease = False , files = None ):
@@ -195,62 +213,62 @@ def create_release(self:GhApi, tag_name, branch='master', name=None, body='',
195213 for file in listify (files ): self .upload_file (rel , file )
196214 return rel
197215
198- # %% ../00_core.ipynb 85
216+ # %% ../00_core.ipynb 87
199217@patch
200218def list_tags (self :GhApi , prefix :str = '' ):
201219 "List all tags, optionally filtered to those starting with `prefix`"
202220 return self .git .list_matching_refs (f'tags/{ prefix } ' )
203221
204- # %% ../00_core.ipynb 90
222+ # %% ../00_core.ipynb 92
205223@patch
206224def list_branches (self :GhApi , prefix :str = '' ):
207225 "List all branches, optionally filtered to those starting with `prefix`"
208226 return self .git .list_matching_refs (f'heads/{ prefix } ' )
209227
210- # %% ../00_core.ipynb 95
228+ # %% ../00_core.ipynb 97
211229# See https://stackoverflow.com/questions/9765453
212230EMPTY_TREE_SHA = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
213231
214- # %% ../00_core.ipynb 97
232+ # %% ../00_core.ipynb 99
215233@patch
216234def create_branch_empty (self :GhApi , branch ):
217235 t = self .git .create_tree (base_tree = EMPTY_TREE_SHA , tree = [dict (
218236 path = '.dummy' , content = 'ignore me' , mode = '100644' , type = 'blob' )])
219237 c = self .git .create_commit (f'create { branch } ' , t .sha )
220238 return self .git .create_ref (f'refs/heads/{ branch } ' , c .sha )
221239
222- # %% ../00_core.ipynb 99
240+ # %% ../00_core.ipynb 101
223241@patch
224242def delete_tag (self :GhApi , tag :str ):
225243 "Delete a tag"
226244 return self .git .delete_ref (f'tags/{ tag } ' )
227245
228- # %% ../00_core.ipynb 100
246+ # %% ../00_core.ipynb 102
229247@patch
230248def delete_branch (self :GhApi , branch :str ):
231249 "Delete a branch"
232250 return self .git .delete_ref (f'heads/{ branch } ' )
233251
234- # %% ../00_core.ipynb 102
252+ # %% ../00_core.ipynb 104
235253@patch
236254def get_branch (self :GhApi , branch = None ):
237255 branch = branch or self .repos .get ().default_branch
238256 return self .list_branches (branch )[0 ]
239257
240- # %% ../00_core.ipynb 104
258+ # %% ../00_core.ipynb 106
241259@patch
242260def list_files (self :GhApi , branch = None ):
243261 ref = self .get_branch (branch )
244262 res = self .git .get_tree (ref .object .sha ).tree
245263 return {o .path :o for o in res }
246264
247- # %% ../00_core.ipynb 106
265+ # %% ../00_core.ipynb 108
248266@patch
249267def get_content (self :GhApi , path ):
250268 res = self .repos .get_content (path )
251269 return base64 .b64decode (res .content )
252270
253- # %% ../00_core.ipynb 108
271+ # %% ../00_core.ipynb 110
254272@patch
255273def create_or_update_file (self :GhApi , path , message , committer , author , content = None , sha = None , branch = '' ):
256274 if not branch : branch = api .repos .get ()['default_branch' ]
@@ -260,28 +278,28 @@ def create_or_update_file(self:GhApi, path, message, committer, author, content=
260278 return self .repos .create_or_update_file_contents (path , message , content = content ,
261279 branch = branch , committer = committer or {}, author = author or {}, ** kwargs )
262280
263- # %% ../00_core.ipynb 109
281+ # %% ../00_core.ipynb 111
264282@patch
265283def create_file (self :GhApi , path , message , committer , author , content = None , branch = None ):
266284 if not branch : branch = api .repos .get ()['default_branch' ]
267285 return self .create_or_update_file (path , message , branch = branch , committer = committer , content = content , author = author )
268286
269- # %% ../00_core.ipynb 111
287+ # %% ../00_core.ipynb 113
270288@patch
271289def delete_file (self :GhApi , path , message , committer , author , sha = None , branch = None ):
272290 if not branch : branch = api .repos .get ()['default_branch' ]
273291 if sha is None : sha = self .list_files ()[path ].sha
274292 return self .repos .delete_file (path , message = message , sha = sha ,
275293 branch = branch , committer = committer , author = author )
276294
277- # %% ../00_core.ipynb 113
295+ # %% ../00_core.ipynb 115
278296@patch
279297def update_contents (self :GhApi , path , message , committer , author , content , sha = None , branch = None ):
280298 if not branch : branch = api .repos .get ()['default_branch' ]
281299 if sha is None : sha = self .list_files ()[path ].sha
282300 return self .create_or_update_file (path , message , committer = committer , author = author , content = content , sha = sha , branch = branch )
283301
284- # %% ../00_core.ipynb 117
302+ # %% ../00_core.ipynb 119
285303@patch
286304def enable_pages (self :GhApi , branch = None , path = "/" ):
287305 "Enable or update pages for a repo to point to a `branch` and `path`."
0 commit comments