Debian Codegraph @

python/example/menu.py

raw
1 #!/usr/bin/python3
2
3 # -*- coding: utf-8 -*-
4 """
5 hierarchical prompt usage example
6 """
7 from __future__ import print_function, unicode_literals
8 from PyInquirer import style_from_dict, Token, prompt, Separator
9
10 import aftl
11
12 def get_storage_prompt(session, s):
13 info = session.get_storage_info(s)
14 name = info.StorageDescription
15 if not name:
16 name = repr(s)
17 return { 'name': name, 'value': s }
18
19 def get_storage(session):
20 storage_prompt = {
21 'type': 'list',
22 'name': 'storage',
23 'message': 'Select storage',
24 'choices': list(map(lambda s: get_storage_prompt(session, s), session.get_storage_ids())) + [{'name': 'Quit', 'value': None}]
25 }
26 answers = prompt(storage_prompt)
27 return answers.get('storage')
28
29 def get_object_prompt(session, object):
30 info = session.get_object_info(object)
31 name = info.Filename
32 if info.ObjectFormat == aftl.ObjectFormat.Association:
33 name += '/'
34 return {'name': name, 'value': (object, info.ObjectFormat) }
35
36 def get_object(session, storage, parent):
37 objects = session.get_object_handles(storage, aftl.ObjectFormat.Any, parent)
38 object_prompt = {
39 'type': 'list',
40 'name': 'object',
41 'message': 'Browse objects',
42 'choices': [{'name': '..', 'value': (None, None) }] + \
43 list(map(lambda o: get_object_prompt(session, o), objects))
44 }
45 answers = prompt(object_prompt)
46 return answers.get('object')
47
48 def get_object_action(session):
49 action_prompt = {
50 'type': 'expand',
51 'name': 'action',
52 'message': 'Object Action (press h for help)',
53 'choices': [
54 { 'key': 'b', 'name': 'Back', 'value': 'back' },
55 Separator(),
56 { 'key': 'd', 'name': 'Download', 'value': 'download' },
57 ]
58 }
59 return prompt(action_prompt).get('action')
60
61 def main():
62 session = aftl.Device.find_first().open_session()
63 while True:
64 storage = get_storage(session)
65 if storage is None:
66 break
67
68 path = [aftl.Session.Root]
69 while path:
70 object, type = get_object(session, storage, path[-1])
71 if type == aftl.ObjectFormat.Association:
72 path.append(object)
73 continue
74
75 if object is None:
76 path.pop()
77 continue
78
79 while True:
80 action = get_object_action(session)
81 if action == 'back':
82 break
83
84 if action == 'download':
85 name = session.get_object_string_property(object, aftl.ObjectProperty.ObjectFilename)
86 with open(name, "wb") as f:
87 session.get_object(object, f)
88
89
90 if __name__ == '__main__':
91 main()