|
36 | 36 | "from datetime import datetime, timedelta, timezone\n", |
37 | 37 | "from pprint import pprint\n", |
38 | 38 | "from time import sleep\n", |
39 | | - "import os, shutil, tempfile, git" |
| 39 | + "import os, shutil, tempfile, subprocess" |
40 | 40 | ] |
41 | 41 | }, |
42 | 42 | { |
|
1056 | 1056 | "source": [ |
1057 | 1057 | "#|export\n", |
1058 | 1058 | "img_md_pat = re.compile(r'!\\[(?P<alt>.*?)\\]\\((?P<url>[^\\s]+)\\)')\n", |
1059 | | - "def replace_md_imgs(content, func): return img_md_pat.sub(func, content)\n", |
| 1059 | + "\n", |
| 1060 | + "def _run_subp(cmd): \n", |
| 1061 | + " r = subprocess.run(cmd, check=False, capture_output=True, text=True)\n", |
| 1062 | + " if r.returncode != 0: raise RuntimeError(r.stderr)\n", |
1060 | 1063 | "\n", |
1061 | 1064 | "@patch\n", |
1062 | 1065 | "def create_gist(self:GhApi, description, content, filename='gist.txt', public=False, img_paths=None):\n", |
1063 | 1066 | " 'Create a gist, optionally with images where each md img url will be placed with img upload urls.'\n", |
1064 | 1067 | " gist = self.gists.create(description, public=public, files={filename: {\"content\": content}})\n", |
1065 | 1068 | " if not img_paths: return gist\n", |
1066 | 1069 | " with tempfile.TemporaryDirectory() as clone_dir:\n", |
1067 | | - " gist_id, gist_url = gist.id, gist.html_url\n", |
1068 | 1070 | " token = self.headers['Authorization'].split('token ')[1]\n", |
1069 | | - " repo = git.Repo.clone_from(f'https://{token}@gist.github.com/{gist_id}.git', clone_dir)\n", |
| 1071 | + " _run_subp(['git', 'clone', f'https://{token}@gist.github.com/{gist.id}.git', clone_dir])\n", |
1070 | 1072 | " clone_dir, img_paths = Path(clone_dir), L(img_paths).map(Path)\n", |
1071 | | - " for o in img_paths: \n", |
1072 | | - " shutil.copy2(o, clone_dir/o.name)\n", |
1073 | | - " repo.index.add([o.name])\n", |
1074 | | - " repo.index.commit(\"Add images\")\n", |
1075 | | - " repo.remotes.origin.push() \n", |
1076 | | - " updated_gist = self.gists.get(gist_id)\n", |
| 1073 | + " for o in img_paths: shutil.copy2(o, clone_dir/o.name)\n", |
| 1074 | + " _run_subp(['git', '-C', clone_dir, 'add', '.'])\n", |
| 1075 | + " _run_subp(['git', '-C', clone_dir, 'commit', '-m', 'Add images'])\n", |
| 1076 | + " _run_subp(['git', '-C', clone_dir, 'push']) \n", |
| 1077 | + " updated_gist = self.gists.get(gist.id)\n", |
1077 | 1078 | " img_urls = {o.name: updated_gist.files[o.name].raw_url for o in img_paths}\n", |
1078 | | - " content = replace_md_imgs(content, lambda m: f\"![{m['alt']}]({img_urls.get(m['url'], m['url'])})\")\n", |
1079 | | - " return self.gists.update(gist_id, files={filename:{'content':content}})" |
| 1079 | + " content = img_md_pat.sub(lambda m: f\"![{m['alt']}]({img_urls.get(m['url'], m['url'])})\", content)\n", |
| 1080 | + " return self.gists.update(gist.id, files={filename:{'content':content}})" |
1080 | 1081 | ] |
1081 | 1082 | }, |
1082 | 1083 | { |
|
1088 | 1089 | "name": "stdout", |
1089 | 1090 | "output_type": "stream", |
1090 | 1091 | "text": [ |
1091 | | - "https://gist.github.com/jph00/634c4553fac5fdd3b2d810dcdae2d841\n" |
| 1092 | + "https://gist.github.com/KeremTurgutlu/c2150e4efd1177439ff35a4633584144\n" |
1092 | 1093 | ] |
1093 | 1094 | } |
1094 | 1095 | ], |
|
1126 | 1127 | "name": "stdout", |
1127 | 1128 | "output_type": "stream", |
1128 | 1129 | "text": [ |
1129 | | - "https://gist.github.com/jph00/dbd28d7ef14117b4aa4b0d8fc9a9aa81\n" |
| 1130 | + "https://gist.github.com/KeremTurgutlu/4c60f8b06baf7b57dc225dd86b65028f\n" |
1130 | 1131 | ] |
1131 | 1132 | } |
1132 | 1133 | ], |
|
1143 | 1144 | { |
1144 | 1145 | "data": { |
1145 | 1146 | "text/plain": [ |
1146 | | - "'some image\\n\\n'" |
| 1147 | + "'some image\\n\\n'" |
1147 | 1148 | ] |
1148 | 1149 | }, |
1149 | 1150 | "execution_count": null, |
|
1806 | 1807 | ], |
1807 | 1808 | "metadata": { |
1808 | 1809 | "kernelspec": { |
1809 | | - "display_name": "python3", |
| 1810 | + "display_name": "python", |
1810 | 1811 | "language": "python", |
1811 | | - "name": "python3" |
| 1812 | + "name": "python" |
1812 | 1813 | } |
1813 | 1814 | }, |
1814 | 1815 | "nbformat": 4, |
|
0 commit comments