forked from mjgiarlo/namaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namaste.py
122 lines (106 loc) · 3.13 KB
/
namaste.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
import logging
import os
import glob
import re
# setup logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
def main():
import optparse
parser = optparse.OptionParser("Usage: %prog [options] <cmd> [<value>]")
parser.add_option("-d", dest="directory", default=os.getcwd(),
help="directory")
options, args = parser.parse_args()
if len(args) == 0:
parser.print_help()
return
d = options.directory
cmd = args[0]
value = None
try:
value = args[1]
except IndexError:
pass
if cmd == 'type':
dirtype(d, value)
elif cmd == 'who':
who(d, value)
elif cmd == 'what':
what(d, value)
elif cmd == 'when':
when(d, value)
elif cmd == 'where':
where(d, value)
elif cmd == 'get':
get(d)
elif cmd == 'gettypes':
get_types(d)
else:
logger.debug("unknown command: %s" % cmd)
def dirtype(d, value, verbose=True):
namaste = _set_namaste(d, 0, value)
if verbose:
logger.debug("created namaste %s" % namaste)
return namaste
def who(d, value, verbose=True):
namaste = _set_namaste(d, 1, value)
if verbose:
logger.debug("created namaste %s" % namaste)
return namaste
def what(d, value, verbose=True):
namaste = _set_namaste(d, 2, value)
if verbose:
logger.debug("created namaste %s" % namaste)
return namaste
def when(d, value, verbose=True):
namaste = _set_namaste(d, 3, value)
if verbose:
logger.debug("created namaste %s" % namaste)
return namaste
def where(d, value, verbose=True):
namaste = _set_namaste(d, 4, value)
if verbose:
logger.debug("created namaste %s" % namaste)
return namaste
def get(d, verbose=True):
tags = []
for namaste in glob.iglob(os.path.join(d, "[0-4]=*")):
tags.append(namaste)
if tags and verbose:
logger.debug("namastes: %s" % ", ".join(tags))
return tags
def get_types(d, verbose=True):
type_tags = _get_namaste(d, 0)
types = {}
if type_tags:
p = re.compile(r"""0=
(?P<name>.+)_
(?P<major>\d+)\.
(?P<minor>\d+)""", re.VERBOSE)
for t in type_tags:
m = p.match(t)
if m != None:
g = m.groupdict()
if verbose:
logger.debug("namaste - directory type %(name)s - version %(major)s.%(minor)s" % (g))
types[g['name']] = g
return types
def _set_namaste(d, tag, value):
if not value:
return None
if not os.path.isdir(d):
raise Exception("directory %s does not exist" % d)
namaste = os.path.join(d, _make_namaste(tag, value))
with open(namaste, 'w') as f:
f.write(value)
f.write("\n")
return namaste
def _get_namaste(d, tag):
if not os.path.isdir(d):
raise Exception("directory %s does not exist" % d)
namaste = [x for x in os.listdir(d) if x.startswith('%s=' % tag)]
if namaste:
return namaste
return None
def _make_namaste(tag, value):
return "%s=%s" % (tag, value)