Skip to content

Commit cff36b6

Browse files
committed
add missing update example snippet
1 parent 5774b55 commit cff36b6

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@
5858
src_examples = os.path.join(os.path.dirname(os.path.realpath(__file__)), "update_example_snippets.py")
5959
if not os.path.exists(dst_examples):
6060
shutil.copy(src_examples, dst_examples)
61-
6261
except IOError:
63-
log_error("Unable to create %s or unable to add example updater" % snippetPath)
62+
log_error("Unable to create %s or unable to add example updater, please report this bug" % snippetPath)
6463

6564

6665
def includeWalk(dir, includeExt):

update_example_snippets.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Update or download example snippets
2+
#
3+
# Automatically download and update a collection of snippets to your local snippet folder
4+
5+
from zipfile import ZipFile
6+
from tempfile import TemporaryFile
7+
import os
8+
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+
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
14+
tab2space = False
15+
width = 4
16+
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)
24+
25+
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]
39+
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)
55+
56+
update_snippets()

0 commit comments

Comments
 (0)