-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.py
More file actions
513 lines (392 loc) · 16.6 KB
/
shell.py
File metadata and controls
513 lines (392 loc) · 16.6 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# -*- coding: utf-8 -*-
# import os
# os.chdir('D:/Denis/python/freemind-tools')
import sys
# sys.path.append('D:/Denis/python/freemind-tools')
import os.path
import re
from datetime import date, datetime
from pprint import pprint
import freemind
BTN_OK = 'button_ok'
BTN_STOP = 'stop-sign'
BTN_CANCEL = 'button_cancel'
# def print_as_tree(node, level):
# for key in node.keys():
# if key == '@TEXT':
# print(level * ' ' + node[key])
def display_nodes(nodes:list, level=0):
if type(nodes) is freemind.FreeMindNode:
print(level * ' ' + nodes.get_title())
if nodes.has_content():
content = nodes.get_content()
l = level + 1
print(l * ' ' + '---')
delim = l * ' '
print(delim + ('\n' + delim).join(content.split("\n")))
print(l * ' ' + '---')
if len(nodes) > 0:
for node in nodes:
display_nodes(node, level + 1)
def display_command(path=None):
result = query_nodes(path)
if not result:
print("%s not found" % select)
else:
# then process only first node cause we don't expect find more
display_nodes(result)
def check_path(path=None):
if path is None or not os.path.isfile(path):
print("Please define valid path")
exit()
def dict_set(props, name, value):
if name in props:
props[name].append(value)
else:
props.setdefault(name, [value])
def node_is_expired(node):
return node.has_attr('Due') and node.get_attr('Due') < datetime.now()
def format_icon(icon):
if icon == BTN_OK:
return 'DONE'
elif icon == BTN_STOP:
return 'STOP'
elif icon == BTN_CANCEL:
return 'CANCEL'
else:
return icon
def format_icons(icons):
if icons is None:
return []
return [format_icon(i) for i in icons]
def full_node_path(fullNodePath, node):
return (full_node_path(fullNodePath, node.get_parent()) if
node.get_parent() is not None and node.get_parent().get_title() != 'root' else fullNodePath) + '/' + node.get_title()
def format_node(node: freemind.FreeMindNode, format="flag parent / title {attrs} icon") -> str:
"""
Format: parent, flag, title, icon, attrs
:param node: some node
:param format: use format
:return: formatted string
"""
attrs = []
for i in ["Due", "Start"]:
if node.has_attr(i):
attrs.append("%s: %s" % (i, date.strftime(node.get_attr(i), "%d.%m.%Y")))
if node.has_attr("Assigned"):
attrs.append(node.get_attr("Assigned"))
flag = ''
if node_is_expired(node):
flag += '[EXPIRED] '
parent = ''
grandparent = ''
fullnodepath = full_node_path('', node)
if node.get_parent() is not None:
parent = node.get_parent().get_title()
if node.get_parent().get_parent() is not None:
grandparent = node.get_parent().get_parent().get_title()
icon = ", ".join(format_icons(node.get_attr('@ICON')))
if node.has_content():
content = "--\n%s\n--\n" % node.get_content()
else:
content = ''
# return "%s%s / %s {%s} %s" % (flag, parent, node.get_title(), ", ".join(attrs), node.get_attr('@ICON'))
# print(format.replace('icon', node.get_attr('@ICON')))
attr_pattrn = re.compile("{@([\w\-]+)}")
return attr_pattrn.sub(lambda m: node.get_attr(m.group(1)), format) \
.replace('{parent}', parent) \
.replace('{grandparent}', grandparent) \
.replace('{fullnodepath}', fullnodepath) \
.replace('{flag}', flag) \
.replace('{title}', node.get_title()) \
.replace('{attrs}', ", ".join(attrs)) \
.replace('{icon}', icon)\
.replace('{content}', content)
def match_condition(node, filter_rules):
# if len(filter_rules) == 1 and filter_rules[0] == '':
# return True
conditions = []
for i in filter_rules:
if i == '': # if empty condition passed
conditions.append(False)
elif i == '*':
conditions.append(True)
elif i[:5] == 'title' and node.get_title() == i[6:]:
conditions.append(True)
elif i == 'root' and node.get_parent() is None:
conditions.append(True)
elif i[:3] == 'id:' and node.has_attr("@ID") and i[3:] == node.get_attr("@ID"):
conditions.append(True)
elif i[:4] == 'icon' and node.has_attr("@ICON") and i[5:] in node.get_attr("@ICON"):
conditions.append(True)
elif i[:9] == 'has-attr:' and node.has_attr(i[9:]):
conditions.append(True)
elif i[:11] == 'hasnt-attr:' and not node.has_attr(i[11:]):
conditions.append(True)
elif i[:5] == '!icon' and node.has_attr("@ICON") and i[6:] not in node.get_attr("@ICON"):
conditions.append(True)
elif i == 'expired' and node_is_expired(node):
conditions.append(True)
elif i == 'not-expired' and not node_is_expired(node):
conditions.append(True)
elif i.lower() == 'not-assigned' and not node.has_attr("Assigned"):
conditions.append(True)
elif i.lower()[:8] == 'assigned' and node.has_attr("Assigned"):
if len(i) == 8:
conditions.append(True) # just assigned
elif node.get_attr("Assigned") == i[9:]:
conditions.append(True) # assigned and has passed assignee
else:
conditions.append(False)
# TODO : simplify it later
# print(node)
# print(filter_rules, conditions)
return len([i for i in conditions if i]) > 0
def nodes_select(doc, rules: str):
if rules == '':
return doc
conditions = rules.split(',')
return [i for i in freemind.traverse(doc, lambda n: match_condition(n, conditions))]
def nodes_filter(nodes: list, rules: str):
if rules == '':
return nodes
conditions = rules.split(',')
return [i for i in nodes if not match_condition(i, conditions)]
def query_nodes(path=None, select='', filter=''):
check_path(path)
doc = freemind.freemind_load(path)
result = nodes_select(doc, select)
result = nodes_filter(result, filter)
return result
def todo_command(path=None, select='', filter='', group='', format='flag title {attrs} icon'):
result = query_nodes(path, select=select, filter=filter)
# for i in result:
# print(i)
# exit()
if group == '':
for i in result:
print(format_node(i, format=format))
else:
groups = {}
for i in result:
if i.has_attr(group):
dict_set(groups, i.get_attr(group), i)
else:
dict_set(groups, 'None', i)
for i in sorted(groups.keys()):
# sys.stdout.buffer.write(i)
# print(i)
print(i)
print("\n".join(["\t%s" % format_node(i, format=format) for i in sorted(groups[i], key=lambda x: x.get_title())]))
def process_goals(nodes:list, filter:list, level=0, format='flag title {attrs} icon'):
if type(nodes) is freemind.FreeMindNode:
print(level * ' ' + format_node(nodes, format=format))
# if nodes.has_content():
# content = nodes.get_content()
# l = level + 1
# if description == 'yes':
# print(l * ' ' + '---')
# delim = l * ' '
# print(delim + ('\n' + delim).join(content.split("\n")))
# print(l * ' ' + '---')
if len(nodes) > 0:
for node in nodes:
if not match_condition(node, filter):
process_goals(node, filter, level + 1, format=format)
def goals_command(path=None, select='', filter='', format='flag title {attrs} icon'):
result = query_nodes(path, select, filter)
if not result:
print("%s not found" % select)
else:
# then process only first node cause we don't expect find more
process_goals(result, filter.split(','), format=format)
def stat_command(path=None):
check_path(path)
print("Goals count: %s" % len(query_nodes(path, '*', '')))
print("Goals done: %s" % len(query_nodes(path, 'icon-%s' % BTN_OK, '')))
print("Goals canceled: %s" % len(query_nodes(path, 'icon-%s' % BTN_CANCEL, '')))
print("Goals stoped: %s" % len(query_nodes(path, 'icon-%s' % BTN_STOP, '')))
node = freemind.freemind_load(path)
print("Goals in progress: %s" % len(freemind.select_bottom(node)))
def traverse_command(path=None, select='', filter='', format='title'):
result = query_nodes(path, select, filter)
if not result:
print("%s not found" % select)
else:
# then process only first node cause we don't expect find more
process_goals(result, filter=filter.split(','), format=format)
def questions_command(path=None):
check_path(path)
def fn_list(n):
if not hasattr(fn_list, 'counter'):
fn_list.counter = 1
if n.has_attr('@ICON') and 'help' in n.get_attr('@ICON'):
print("%s. %s\n %s\n" % (fn_list.counter, n.get_title(), n.get_content()))
fn_list.counter += 1
result = freemind.freemind_load(path)
freemind.traverse(result, fn_list)
def estimate_command(path=None, field='estimate'):
check_path(path)
print("Estimate field: %s\n" % field)
format = ' - {grandparent}/{parent}/{title}, {@%s}h' % field
def fn_list(n):
if not hasattr(fn_list, 'result'):
fn_list.result = []
fn_list.total = 0
if n.has_attr(field):
if n.has_attr('@ICON') and BTN_STOP in n.get_attr('@ICON'):
return
if n.get_attr(field).strip() == '':
return
fn_list.result.append(format_node(n, format))
fn_list.total += float(n.get_attr(field))
result = freemind.freemind_load(path)
freemind.traverse(result, fn_list)
[print(i) for i in sorted(fn_list.result)]
print("\nTotal: %sh" % fn_list.total)
def competences_command(path=None):
check_path(path)
result = query_nodes(path, select='title:Activities')
if len(result) != 1:
print('Activities node missed')
return
import json
output = []
for competence in result[0]:
tech = False
if competence.has_attr('tech') and competence.get_attr('tech').lower() == 'yes':
tech = True
project = False
if competence.has_attr('project') and competence.get_attr('project').lower() == 'yes':
project = True
roles = '-'
if competence.has_attr('roles'):
roles = competence.get_attr('roles')
confirm = ''
if competence.has_attr('confirm'):
confirm = competence.get_attr('confirm')
employee = False
if competence.has_attr('employee') and competence.get_attr('employee').lower() == 'yes':
employee = True
# print("%s [tech: %s, project: %s, roles: %s]" % (competence.get_title(), tech, project, roles))
competences = []
for competence_level in competence:
if competence_level.get_title() == 'attribute_layout':
continue
level = 0
if competence_level.has_attr('@ICON'):
if 'full-1' in competence_level.get_attr('@ICON'):
level = '1'
elif 'full-2' in competence_level.get_attr('@ICON'):
level = '2'
elif 'full-3' in competence_level.get_attr('@ICON'):
level = '3'
elif 'full-4' in competence_level.get_attr('@ICON'):
level = '4'
# print(" %s. %s" % (level, competence_level.get_title()))
competences.append({'level': level, 'desc': competence_level.get_title()})
result = {
'title': competence.get_title(),
'tech': tech,
'project': project,
'roles': roles.split(','),
'confirm': confirm.split(','),
'competences': competences,
'employee': employee
}
if competence.has_attr('description'):
result.setdefault('description', competence.get_attr('description'))
output.append(result)
print(json.dumps(output))
def __revert_spaces(s):
return s.replace('§', ' ')
def spec_command(path=None, parts=None, out=None):
check_path(path)
doc = freemind.freemind_load(path)
if out:
output = open(out, "w", encoding="utf-8")
def process_node(n, level):
if n.has_attr('@ICON') and 'button_cancel' in n.get_attr('@ICON'):
return
result = level * '#' + ' ' + n.get_title() + "\n\n"
if n.has_content():
result += n.get_content() + "\n\n"
outStr = __revert_spaces(result)
if out:
output.write(outStr)
else:
print(outStr)
def fn_list(nodes, level):
if not nodes:
return
for n in nodes:
process_node(n, level)
fn_list(n, level+1)
if parts is None:
pass
else:
for part in parts.split(';'):
nodes = nodes_select(doc, "title:%s" % part)
fn_list(nodes, 1)
# print(nodes)
if out:
output.close()
def tex_command(path):
check_path(path)
doc = open(path).read()
begin_marker = '\\begin{document}'
end_marker = '\\end{document}'
begin_pos = doc.find(begin_marker)
end_pos = doc.find(end_marker)
if begin_pos < 0 or end_pos < 0:
print('Markers not found')
return
print(doc[begin_pos+len(begin_marker):end_pos])
if __name__ == '__main__':
# shell.py search --path=Goals.mm --icon=stop-sign // nodes on hold
# shell.py search --path=Goals.mm --icon=yes // important nodes
# shell.py search --path=Goals.mm // all nodes
# shell.py group --path=Goals.mm --by=resource // actual tasks by resource
# shell.py group --path=Goals.mm --by=expired // expired tasks
# mm todo --path=Goals.mm --filter=not-assigned --group=Assigned
# mm todo --path=Goals.mm --filter=expired
# mm traverse --path=tests\Test.mm --select="title:New Mindmap" --format="title @Assigned"
# node = freemind.FreeMindNode(None)
# node.set_title('test')
# node.add_attr('@ICON', 'stop-sign')
# print(should_filter(node, ['icon-stop-sign']))
# goals_command('Goals.mm', select='title:Ilya Levoshko (QA)', filter='icon-button_ok,icon-stop-sign', description='no')
# todo_command('Goals.mm', select='expired', filter='icon-button_ok')
# todo_command('Goals.mm', select='assigned:@Sheremetov', filter='icon-button_ok')
# todo_command('D:\Denis\python\onixteam\Goals.mm', select='assigned', filter='icon-button_ok')
# exit()
# todo_command('D:\Denis\python\onixteam\Goals.mm', select='id:363b6bf92c5df3e2dc30043f1212d103')
# stat_command(PATH)
# nodes = query_nodes(PATH, 'icon-button_ok', '')
# print(len(nodes))
# nodes = freemind.freemind_load('Goals.mm')
# todo_command('tests/Test.mm')
# result = freemind.freemind_load('D:/temp/presale/impesa/test.mm')
# freemind.traverse_with_level(result, lambda n, l: print((l-1) * ' ' + format_node(n, '{title}')))
# traverse_command('tests/Test.mm', select='title:New Mindmap', filter='', format='title {@Assigned}')
# traverse_command("D:/Dropbox/onix/clients/UpMost/UpMostLanding.mm", select='title:Screens', filter='icon-stop-sign', format='title,{@estimate},{@estimate-res}')
# print([i.get_title() for i in query_nodes("D:/Dropbox/onix/clients/UpMost/UpMostLanding.mm")])
# freemind.traverse(result, lambda n: print(format_node(n, 'title')))
# estimate_command("D:/Dropbox/onix/clients/UpMost/UpMostLanding.mm")
# estimate_command("tests/Test.mm")
# spec_command('D:/temp/presale/impesa/Impesa.mm', 'Web application functionality')
# display_command('D:/temp/presale/impesa/impesa.mm')
# tex_command('D:/temp/presale/impesa/impesa.tex')
# exit()
# PATH = '/home/denis/Dropbox/Onix/skills-matrix-v2.mm'
# competences_command(PATH)
# exit()
# python3 shell.py competences --path=/home/denis/Dropbox/Onix/skills-matrix-v2.mm > /var/www/hrm/web/code/webroot/competences.json
# result = query_nodes('tests/TestFP.mm', select='id:ID_1232863674')
# pprint(result[0][0])
# for i in result[0]:
# pprint(i)
# print('--')
# exit()
from commandliner import commandliner
commandliner(locals())