-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_import_to.py
More file actions
107 lines (94 loc) · 3.33 KB
/
_import_to.py
File metadata and controls
107 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!python3
'''Designed to be used as a share extension.
Import files into Pythonista app. Can import from file, selected text, and
url of single file(such as https://github...) on the open web.'''
import appex
import console
import os
import re
import requests
import shutil
def numbered_filename(filename, i):
return ('(' + str(i) + ')').join(os.path.splitext(filename))
def import_from_file(fname, fpath, basepath):
i = 2
write_path = orig_path = os.path.join(basepath, fname)
while os.path.exists(write_path):
write_path = numbered_filename(orig_path, i)
fname = os.path.basename(write_path)
i += 1
shutil.copy(fpath, write_path)
console.hud_alert('Imported as: ' + fname)
def import_from_text(fname, fpath, basepath):
i = 2
write_path = orig_path = os.path.join(basepath, fname)
while os.path.exists(write_path):
write_path = numbered_filename(orig_path, i)
fname = os.path.basename(write_path)
i += 1
try:
with open(fpath) as r:
content = r.read()
except:
content = fpath
if isinstance(content, bytes):
content = content.decode('utf8')
with open(write_path, 'wb') as w:
w.write(content.encode('utf8'))
console.hud_alert('Imported as: ' + fname)
def import_from_url(fname, fpath, basepath):
if fpath.startswith('https://github'):
fpath = 'https://raw.' + fpath[8:]
fpath = fpath.replace('/blob', '')
elif fpath.startswith('https://gist.'):
r = requests.get('http://api.github.com/gists/' + os.path.basename(fpath)).json()
fname = list(r['files'].keys())[0]
fpath += '/raw'
i = 2
fname_ori = fname.split('.')[0]
fname_ext = re.findall('(\.[a-zA-Z]+)', fname)[-1]
fname = fname_ori + fname_ext
write_path = orig_path = os.path.join(basepath, fname)
while os.path.exists(write_path):
write_path = numbered_filename(orig_path, i)
fname = os.path.basename(write_path)
i += 1
try:
r = requests.get(fpath)
content = r.text
except:
content = fpath
with open(write_path, 'w') as w:
w.write(content)
console.hud_alert('Imported as: ' + fname)
def main():
console.hud_alert('processing..', 'success')
if not appex.is_running_extension():
print('This script is intended to be run from the sharing extension.')
return
basepath = os.path.expanduser('~/Documents/')
fpath = appex.get_file_path()
if fpath:
fname = os.path.split(fpath)[1]
try:
import_from_file(fname, fpath, basepath)
except:
fpath = appex.get_text()
if os.path.isfile(fpath):
fname = os.path.split(fpath)[1]
import_from_text(fname, fpath, basepath)
elif appex.get_text():
fpath = appex.get_text()
ask = 'Choose File Extension', 'py', 'txt', 'pyui'
resp = console.alert('Import file as..', *ask,
hide_cancel_button=False)
fname = 'imported.' + ask[resp]
import_from_text(fname, fpath, basepath)
elif appex.get_url():
fpath = appex.get_url()
fname = os.path.split(fpath)[1]
import_from_url(fname, fpath, basepath)
else:
console.hud_alert('Not a file!', icon='error')
if __name__ == '__main__':
main()