Repository navigation
Expand file tree
/
Copy pathbcli.py
More file actions
413 lines (354 loc) · 11.8 KB
/
Copy pathbcli.py
File metadata and controls
413 lines (354 loc) · 11.8 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
from prompt_toolkit import prompt
from prompt_toolkit.shortcuts import message_dialog, yes_no_dialog, input_dialog, button_dialog, radiolist_dialog
import os
import json
import re
import unicodedata
from constants import *
def cli_configured(config_filename):
"""
Check if a config file exists. This file should
point to BetaLibrary's directory
"""
if not config_filename in os.listdir():
return False
return True
def slugify(value, allow_unicode=False):
"""
Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
Remove characters that aren't alphanumerics, underscores, or hyphens.
Convert to lowercase. Also strip leading and trailing whitespace.
Source: https://github.com/django/django/blob/master/django/utils/text.py
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode(
'ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return re.sub(r'[-\s]+', '_', value)
def load_configuration():
"""
Load or load and configure BetaLibrary's project directory
"""
if not cli_configured(CONFIG_FILE):
configuration = yes_no_dialog(
title='Configuration',
text="BetaLibrary's path is not configured. Configure now?")
text = input_dialog(
title='Configuration',
text="Please enter BetaLibrary's path: ")
if text:
with open("config.txt", 'w', encoding='utf-8') as f:
f.write(text)
return text
else:
with open("config.txt", 'r', encoding='utf-8') as f:
return f.read()
def load_zones(path):
"""
Load all bouldering zones
"""
return [d for d in next(os.walk(path+DATA+ZONES))[1]]
def load_sectors(zone, path):
"""
Load the sectors of a bouldering zone
"""
return [d for d in next(os.walk(path+DATA+ZONES+SEPARATOR+zone+SECTORS))[2]]
def load_prefix(key):
"""
Define the prefix that should be shown
in the dialog for the current key
"""
if key not in NUMERICS:
return key
return 'Number of ' + key
def try_parse(value_to_parse):
"""
Try to parse the value to a number. If this
fails, keep the original type
"""
try:
return float(value_to_parse)
except:
return value_to_parse
def autocompute_fields(data):
"""
Automatic computation of data fields from the
rest of input data
"""
for sector in data['sectors']:
sector['sector_data'] = '/sectors/' + \
slugify(sector['name'], False).lower() + '.txt'
def choose_action():
"""
Select which action to perform
"""
return button_dialog(
title='Options',
text='What do you want to do?',
buttons=[
('Create', 0),
('Modify', 1),
('Delete', 2),
('Exit', 3)
],
)
def execute_action(action, path):
"""
Execute the selected action
"""
if action == EXIT:
return
if action == CREATE:
create(path)
if action == MODIFY:
modify(path)
if action == DELETE:
delete(path)
def create(path):
"""
Create action
"""
create_type = button_dialog(
title='Create',
text='What do you want to create?',
buttons=[
('Zone', 0),
('Sector', 1),
('Exit', 3)
],
)
if create_type == ZONE:
create_zone(path)
if create_type == SECTOR:
create_sector(path)
if create_type == EXIT:
return
def create_zone(path):
"""
Create a new zone
"""
creation_type = button_dialog(
title='Create',
text='How do you want to create the zone?',
buttons=[
('Empty template (only structure)', 0),
('Structure and data', 1),
('Exit', 3)
],
)
data = {}
with open('zone_template.txt', encoding='utf-8') as json_file:
data = json.load(json_file)
if creation_type == EXIT:
return
# Create zone with all the data
elif creation_type == 1:
for data_key in data.keys():
if data_key in AUTOCOMPUTED:
continue
field_value = input_dialog(
title='Zone creation',
text=load_prefix(data_key)+": ")
if field_value is None:
return None
if data_key in NUMERICS:
content_array = []
for i in range(int(field_value)):
content = {}
for sub_data_key in data[data_key][0].keys():
if sub_data_key in AUTOCOMPUTED:
continue
value = input_dialog(
title='Zone creation',
text=load_prefix(sub_data_key)+" "+str(i)+": ")
content[sub_data_key] = try_parse(value)
content_array += [content]
data[data_key] = content_array
else:
data[data_key] = try_parse(field_value)
autocompute_fields(data)
# Create only zone template
elif creation_type == 0:
for data_key in data.keys():
if data_key not in TEMPLATED_ZONE_CREATION:
continue
field_value = input_dialog(
title='Zone creation',
text=load_prefix(data_key)+": ")
if data_key in NUMERICS:
content_array = []
for i in range(int(field_value)):
content = {}
for sub_data_key in data[data_key][0].keys():
content[sub_data_key] = ""
content_array += [content]
data[data_key] = content_array
else:
data[data_key] = try_parse(field_value)
print(data)
# Create zone
zone_path = path+DATA+ZONES+SEPARATOR + \
slugify(data['name'], False).lower()
os.mkdir(zone_path)
os.mkdir(zone_path + SECTORS)
# Load sector template
sector_data = {}
with open('sector_template.txt', encoding='utf-8') as json_file:
sector_data = json.load(json_file)
# Add sectors
for num, sector in enumerate(data['sectors']):
# if a sector data file name has been provided, create the file
if sector['sector_data']:
with open(zone_path + slugify(sector['sector_data'], False), 'w', encoding='utf-8') as f:
f.write(json.dumps(sector_data, indent=4, sort_keys=True))
elif sector['name']:
sector['sector_data'] = SECTORS + SEPARATOR + \
slugify(sector['name'], False)+'.txt'
with open(zone_path + slugify(sector['sector_data'], False), 'w', encoding='utf-8') as f:
f.write(json.dumps(sector_data, indent=4, sort_keys=True))
else:
sector['sector_data'] = SECTORS + \
SEPARATOR + 'sector_' + str(num) + '.txt'
with open(zone_path + sector['sector_data'], 'w', encoding='utf-8') as f:
f.write(json.dumps(sector_data, indent=4, sort_keys=True))
# Add zone data
with open(zone_path + SEPARATOR + slugify(data['name'], False).lower()+'.txt', 'w', encoding='utf-8') as f:
f.write(json.dumps(data, indent=4, sort_keys=True))
def create_sector(path):
"""
Create a new sector
"""
zones = load_zones(path)
selected_zone = radiolist_dialog(
values=[
(zone, zone) for zone in zones
],
title='Select Zone',
text='Please select a zone (use enter to select and tab to move to confirmation buttons):')
zone_path = path+DATA+ZONES+SEPARATOR+selected_zone
creation_type = button_dialog(
title='Create Sector',
text='How do you want to create the sector?',
buttons=[
('Empty template (only structure)', 0),
('Structure and data', 1),
('Exit', 3)
],
)
# Load sector template
sector_data = {}
with open('sector_template.txt', encoding='utf-8') as json_file:
sector_data = json.load(json_file)
# only template
if creation_type == 0:
# ask for sector name
sector_name = input_dialog(
title='Sector creation',
text="Sector Name: ")
if sector_name is None:
return None
else:
# create sector txt
with open(
zone_path + SEPARATOR + "sectors" + SEPARATOR + slugify(sector_name, False) + '.txt',
'w', encoding='utf-8'
) as f:
f.write(json.dumps(sector_data, indent=4, sort_keys=True))
# add sector in zone txt
# its fields are:
# {
# "name":"Sector El Anfibio",
# "sector_data":"/sectors/el_anfibio.txt",
# "link":"https://www.youtube.com/playlist?list=PLlwn5IhJiUnPbYvXtpKcRXimqFxnfB9y-"
# },
# inside the sectors key
zone_data = {}
with open(zone_path + SEPARATOR + selected_zone + '.txt', encoding='utf-8') as json_file:
zone_data = json.load(json_file)
zone_data['sectors'].append(
{
'name': sector_name,
'sector_data':'/sectors/'+slugify(sector_name, False)+'.txt',
'link': ''
}
)
with open(zone_path + SEPARATOR + selected_zone + '.txt', 'w', encoding='utf-8') as f:
f.write(json.dumps(zone_data, indent=4, sort_keys=True))
def modify(path):
"""
Modify action
"""
pass
def delete(path):
"""
Delete action
"""
delete_type = button_dialog(
title='Delete',
text='What do you want to delete?',
buttons=[
('Zone', 0),
('Sector', 1),
('Exit', 3)
],
)
if delete_type == ZONE:
delete_zone(path)
if delete_type == SECTOR:
delete_sector(path)
if delete_type == EXIT:
return
def delete_zone(path):
"""
Deletes a zone
"""
zones = load_zones(path)
selected_zone = radiolist_dialog(
values=[
(zone, zone) for zone in zones
],
title='Select Zone',
text='Please select a zone to delete (use tab to move to confirmation buttons):')
def delete_sector(path):
"""
Delectes a sector from a zone
"""
zones = load_zones(path)
selected_zone = radiolist_dialog(
values=[
(zone, zone) for zone in zones
],
title='Select Zone',
text='Please select a zone (use tab to move to confirmation buttons):')
sectors = load_sectors(selected_zone, path)
selected_sector = radiolist_dialog(
# Remove extension by slicing
values=[
(sector, sector[:-4]) for sector in sectors
],
title='Select Sector',
text='Please select a sector to delete (use tab to move to confirmation buttons):')
# TODO: Replace by an easier selector dialog
sure = radiolist_dialog(
values=[
(True, "Yes"),
(False, "No")
],
title='Select Sector',
text='Are you sure you want to delete '+selected_sector[:-4]+' from '+selected_zone+'?')
if sure:
os.remove(path+DATA+ZONES+SEPARATOR+selected_zone+SECTORS+SEPARATOR+selected_sector)
def main():
"""
Main entry point of the cli. Loads project path
and asks user for an action
"""
path = load_configuration()
if path is None:
return
execute_action(choose_action(), path)
if __name__ == "__main__":
main()