-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmenu.py
executable file
·146 lines (92 loc) · 3.55 KB
/
dmenu.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
from subprocess import Popen, PIPE, check_output
from shlex import split
from os.path import join as pjoin, basename
from glob import iglob
from argparse import ArgumentParser
opt="-fn -adobe-helvetica-bold-r-normal-*-25-180-100-100-p-138-iso8859-1"
home = "/home/istvan"
progs = pjoin(home, "progs")
temu = "lxterminal"
gamma_doc = pjoin(home, "Dokumentumok", "gamma_doc")
class dmenu(object):
dmenu_cmd = split("dmenu %s" % opt)
def __init__(self, *args, **kwargs):
self.cmd, self.interm = kwargs.get("cmd"), kwargs.get("interm", False)
self.options = {arg : arg for arg in args}
_dict = kwargs.get("dict")
if _dict is not None:
self.options.update(_dict)
self.choices = b"\n".join(choice.encode("ascii")
for choice in self.options.keys())
@classmethod
def file_list(cls, *pattern, **kwargs):
paths = {basename(path): path for path in iglob(pjoin(*pattern))}
return cls(dict=paths, **kwargs)
def select(self):
p = Popen(dmenu.dmenu_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate(self.choices)
if p.returncode != 0:
raise RuntimeError("Non zero returncode from dmenu!")
return self.options[out.decode().strip()]
def __call__(self):
mode = self.select()
if self.cmd is None:
raise RuntimeError("cmd not defined!")
if self.interm:
cmd = "%s -e '%s %s'" % (temu, self.cmd, mode)
else:
cmd = "%s %s" % (self.cmd, mode)
check_output(split(cmd))
def paths(**kwargs):
path = kwargs.pop("path")
return {
key: pjoin(path, key) if value is None else value
for key, value in kwargs.items()
}
def modules(**kwargs):
module = dmenu(*kwargs.keys()).select()
kwargs[module]()
repos = paths(path=progs,
gamma=None,
insar_meteo=None,
utils=None,
texfile=pjoin(home, "Dokumentumok", "texfiles")
)
def pull_repo(path):
cmd = "git --git-dir=%s --work-tree=%s pull origin master" \
% (pjoin(path, ".git"), path)
try:
with open(pjoin(home, "dmenu.py.log"), "ab") as f:
f.write(check_output(split(cmd)))
except:
pass
def pull_all():
for repo in repos.values():
pull_repo(repo)
def eofs():
path = pjoin(home, "progs", "eofs", "game")
jar = pjoin(path, "Phoenix.jar")
cmd = pjoin(path, "jre", "bin", "java")
cmd += " -jar -Xss32m %s --enableai -d" % jar
print(cmd)
# check_output(split(cmd))
def main():
ap = ArgumentParser()
ap.add_argument("mode", help="Mode", type=str,
choices=["modules", "programs", "pull_all"])
args = ap.parse_args()
if args.mode == "modules":
modules(mc=dmenu.file_list(home, "progs", "*", cmd="mc", interm=True),
playlist=dmenu.file_list(home, "playlists", "*", cmd="parole"),
gamma_doc=dmenu.file_list(home, gamma_doc, "*.html",
cmd="chromium-browser"),
pull_all=pull_all,
eofs=eofs)
elif args.mode == "pull_all":
pull_all()
else:
check_output(split("dmenu_run %s" % opt))
return 0
if __name__ == "__main__":
main()