forked from BenderV/file2db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pashua.py
67 lines (54 loc) · 2.35 KB
/
Pashua.py
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
"""
Pashua.py - Interface to Pashua
Pashua is an application that can be used to provide some type of dialog GUI
for Python and shell applications on Mac OS X. Pashua.py is the glue between
your script and Pashua. To learn more about Pashua, take a look at the
application's Readme file. Pashua's homepage is www.bluem.net/jump/pashua
Please note in order for the example to work, the Pashua application
must be in the current path, in /Applications/ or in ~/Applications/
If none of these paths apply, you will have to specify it manually:
Pashua.PATH = '/path/to/appfolder';
... before you call Pashua.run(). Alternatively, you may specify the
path (the directory that contains Pashua.app, without trailing slash)
as 3rd argument to run().
"""
import os.path
import sys
import subprocess
BUNDLE_PATH = "Pashua.app/Contents/MacOS/Pashua"
PASHUA_PLACES = [
os.path.join(os.path.dirname(sys.argv[0]), "Pashua"),
os.path.join(os.path.dirname(sys.argv[0]), BUNDLE_PATH),
os.path.join(".", BUNDLE_PATH),
os.path.join("/Applications", BUNDLE_PATH),
os.path.join(os.path.expanduser("~/Applications"), BUNDLE_PATH),
os.path.join("/usr/local/bin", BUNDLE_PATH)
]
# Find Pashua by looking in each of the search locations, returning the first
# matching path. Will raise an exception, if Pashua.app cannot be found.
def locate_pashua(pashua_path=None):
if pashua_path:
# Custom path given
PASHUA_PLACES.insert(0, pashua_path + '/' + BUNDLE_PATH)
for bundle_path in PASHUA_PLACES:
if os.path.exists(bundle_path):
return bundle_path
raise IOError("Unable to locate the Pashua application.")
# Calls the pashua executable, parses its result string and generates
# a dictionary that's returned.
def run(config_data, pashua_path=None):
# Get path to the executable inside Pashua.app
app_path = locate_pashua(pashua_path)
# Send string to pashua standard input, receive result.
s = subprocess.Popen([app_path, "-"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result, _ = s.communicate(input=config_data)
# Parse result
d = {}
for line in result.decode('utf8').splitlines():
if '=' in line: # avoid empty lines.
k, _, v = line.partition('=')
d[k] = v.rstrip()
return d