|
1 |
| -# Update or download example snippets |
| 1 | +# update snippets |
2 | 2 | #
|
3 |
| -# Automatically download and update a collection of snippets to your local snippet folder |
| 3 | +# Automatically download and update this collection of snippets to your local snippet folder |
4 | 4 |
|
5 | 5 | from zipfile import ZipFile
|
6 | 6 | from tempfile import TemporaryFile
|
|
9 | 9 | #TODO: Merge remote with local description or hotkey changes (AKA: if filename matches, skip the first two lines, truncate, re-write the rest)
|
10 | 10 |
|
11 | 11 | domain = b'https://gist.github.com'
|
12 |
| -path = b'/psifertex/6fbc7532f536775194edd26290892ef7' # Feel free to adapt to your own setup |
13 |
| -subfolder = 'examples' # Change to save the snippets to a different sub-folder |
| 12 | +path = b'/psifertex/6fbc7532f536775194edd26290892ef7' # Feel free to adapt to your own setup |
| 13 | +subfolder = 'default' # Change to save the snippets to a different sub-folder |
14 | 14 | tab2space = False
|
15 | 15 | width = 4
|
16 | 16 |
|
17 | 17 | def download(url):
|
18 |
| - provider = next(iter(DownloadProvider)).create_instance() |
19 |
| - code, data = provider.get_response(url) |
20 |
| - if code == 0: |
21 |
| - return data |
22 |
| - else: |
23 |
| - raise ConnectionError("Unsuccessful download of %s" % url) |
| 18 | + # Can also use 'CoreDownloadProvider' or 'PythonDownloadProvider' as keys here |
| 19 | + provider = DownloadProvider[Settings().get_string('network.downloadProviderName')].create_instance() |
| 20 | + code, data = provider.get_response(url) |
| 21 | + if code == 0: |
| 22 | + return data |
| 23 | + else: |
| 24 | + raise ConnectionError("Unsuccessful download of %s" % url) |
24 | 25 |
|
25 | 26 | def update_snippets():
|
26 |
| - if not interaction.show_message_box('Warning', "Use at your own risk. Do you want to automatically overwrite local snippets from gist?", buttons=MessageBoxButtonSet.YesNoButtonSet): |
27 |
| - return |
28 |
| - snippetPath = os.path.realpath(os.path.join(user_plugin_path(), '..', 'snippets', subfolder)) |
29 |
| - if not os.path.isdir(snippetPath): |
30 |
| - os.makedirs(snippetPath) |
31 |
| - url = domain + path |
32 |
| - log_info("Downloading from: %s" % url) |
33 |
| - source = download(url) |
34 |
| - zipPath = [s for s in source.split(b'\"') if s.endswith(b'.zip')] |
35 |
| - if len(zipPath) != 1: |
36 |
| - log_error("Update failed.") |
37 |
| - return |
38 |
| - url = domain + zipPath[0] |
| 27 | + if not interaction.show_message_box('Warning', "Use at your own risk. Do you want to automatically overwrite local snippets from gist?", buttons=MessageBoxButtonSet.YesNoButtonSet): |
| 28 | + return |
| 29 | + snippetPath = os.path.realpath(os.path.join(user_plugin_path(), '..', 'snippets', subfolder)) |
| 30 | + if not os.path.isdir(snippetPath): |
| 31 | + os.makedirs(snippetPath) |
| 32 | + url = domain + path |
| 33 | + log_info("Downloading from: %s" % url) |
| 34 | + source = download(url) |
| 35 | + zipPath = [s for s in source.split(b'\"') if s.endswith(b'.zip')] |
| 36 | + if len(zipPath) != 1: |
| 37 | + log_error("Update failed.") |
| 38 | + return |
| 39 | + url = domain + zipPath[0] |
39 | 40 |
|
40 |
| - log_info("Downloading from: %s" % url) |
41 |
| - zip = download(url) |
42 |
| - with TemporaryFile() as f: |
43 |
| - f.write(zip) |
44 |
| - with ZipFile(f, 'r') as zip: |
45 |
| - for item in zip.infolist(): |
46 |
| - if item.filename[-1] == '/': |
47 |
| - continue |
48 |
| - basename = os.path.basename(item.filename) |
49 |
| - with open(os.path.join(snippetPath, basename), 'wb') as f: |
50 |
| - if tab2space: |
51 |
| - f.write(zip.read(item).replace(b'\t', b' ' * width)) |
52 |
| - else: |
53 |
| - f.write(zip.read(item)) |
54 |
| - log_info("Extracting %s" % item.filename) |
| 41 | + log_info("Downloading from: %s" % url) |
| 42 | + zip = download(url) |
| 43 | + with TemporaryFile() as f: |
| 44 | + f.write(zip) |
| 45 | + with ZipFile(f, 'r') as zip: |
| 46 | + for item in zip.infolist(): |
| 47 | + if item.filename[-1] == '/': |
| 48 | + continue |
| 49 | + basename = os.path.basename(item.filename) |
| 50 | + with open(os.path.join(snippetPath, basename), 'wb') as f: |
| 51 | + if tab2space: |
| 52 | + f.write(zip.read(item).replace(b'\t', b' ' * width)) |
| 53 | + else: |
| 54 | + f.write(zip.read(item)) |
| 55 | + log_info("Extracting %s" % item.filename) |
55 | 56 |
|
56 | 57 | update_snippets()
|
0 commit comments