-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPackUI.py
More file actions
117 lines (93 loc) · 4.32 KB
/
PackUI.py
File metadata and controls
117 lines (93 loc) · 4.32 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
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
################################################################################
# PackUI by dgelessus
# https://github.com/dgelessus/pythonista-scripts/blob/master/UI/PackUI.py
################################################################################
# Package a UI script and its corresponding .pyui file into a single,
# self-extracting file. When run, the generated script will unpack both files,
# allowing both running and further editing without limitations.
#
# The PackUI script itself can be given a py/pyui pair to package either using
# the (very basic) UI by running it directly, via sys.argv, or by importing it
# and using the PackUI.pack() function. All paths are considered relative to
# the current working directory.
################################################################################
import os.path
def script_text(name, pyfile, pyuifile):
return '''# -*- coding: utf-8 -*-
###############################################################################
# This is a self-extracting UI application package for {name}.
# Run this script once to extract the packaged application.
# The files will be extracted to {name}.py and {name}.pyui.
# Make sure that these files do not exist yet.
# To update from an older version, move or delete the old files first.
# After extracting, the application can be found at {name}.py.
# This bundle can be deleted after extraction.
###############################################################################
# Packaged using PackUI by dgelessus
# https://github.com/dgelessus/pythonista-scripts/blob/master/UI/PackUI.py
###############################################################################
import console, os.path
NAME = "{name}"
PYFILE = """{pyfile}"""
PYUIFILE = """{pyuifile}"""
def fix_quotes_out(s):
return s.replace("\\\\\\"\\\\\\"\\\\\\"", "\\"\\"\\"").replace("\\\\\\\\", "\\\\")
def main():
if os.path.exists(NAME + ".py"):
console.alert("Failed to Extract", NAME + ".py already exists.")
return
if os.path.exists(NAME + ".pyui"):
console.alert("Failed to Extract", NAME + ".pyui already exists.")
return
with open(NAME + ".py", "w") as f:
f.write(fix_quotes_out(PYFILE))
with open(NAME + ".pyui", "w") as f:
f.write(fix_quotes_out(PYUIFILE))
msg = NAME + ".py and " + NAME + ".pyui were successfully extracted!"
console.alert("Extraction Successful", msg, "OK", hide_cancel_button=True)
if __name__ == "__main__":
main()'''.format(name=name, pyfile=pyfile, pyuifile=pyuifile)
def fix_quotes_in(s):
return s.replace("\\", "\\\\").replace("\"\"\"", "\\\"\\\"\\\"")
def pack(path):
if not os.path.exists(path + ".py"):
raise IOError(path + ".py does not exist")
elif not os.path.isfile(path + ".py"):
raise IOError(path + ".py is not a file")
elif not os.path.exists(path + ".pyui"):
raise IOError(path + ".pyui does not exist")
elif not os.path.isfile(path + ".pyui"):
raise IOError(path + ".pyui is not a file")
elif os.path.exists(path + ".uipack.py"):
raise IOError(path + ".uipack.py already exists")
name = os.path.split(path)[1]
with open(path + ".py") as f:
pyfile = fix_quotes_in(f.read())
with open(path + ".pyui") as f:
pyuifile = fix_quotes_in(f.read())
out = script_text(name, pyfile, pyuifile)
with open(path + ".uipack.py", "w") as f:
f.write(out)
return out
def main():
import sys
if len(sys.argv) > 1: # pack files specified via argv
arg = ' '.join(sys.argv[1:])
try:
pack(arg)
except IOError as err:
print("Failed to pack program: " + err.message)
else: # display prompt for file
import console
msg = "Enter path (relative to current directory) of the application to be packaged, without .py or .pyui suffixes."
arg = console.input_alert("Package UI Application", msg)
try:
pack(arg)
except IOError as err:
console.alert("Failed to pack", err.message)
return
msg = "Application was successfully packaged into {}.uipack.py!".format(arg)
console.alert("Packaging Successful", msg, "OK", hide_cancel_button=True)
if __name__ == "__main__":
main()