-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathh_django_common.py
578 lines (369 loc) · 13.3 KB
/
h_django_common.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# -*- encoding: utf-8 -*-
"""
Copyright (c) App-Generator.dev | AppSeed.us
"""
from .common import *
from .h_files import *
from .h_util import *
def cfg_load( FILE_PATH=FILE_DJ_SETTINGS_s ):
retcode = COMMON.OK
content = []
try:
raw_content = file_load( FILE_PATH, True ) # as list
if not raw_content:
print ('Err loading ['+FILE_PATH+'] file')
return COMMON.NOT_FOUND, None
# print('Content: ' + raw_content)
content = raw_content
except Exception as e:
print('Err loading ['+FILE_PATH+']: ' + str(e) )
retcode = COMMON.ERR
return retcode, content
def cfg_save( FILE_PATH, aContent ):
retcode = COMMON.OK
try:
file_content = ''
if type( aContent ) is list:
for line in aContent:
file_content += line + '\n'
# expect a string here
else:
file_content = aContent
file_write( FILE_PATH, file_content)
except Exception as e:
retcode = COMMON.ERR
return retcode
def cfg_format( FILE_PATH ):
retcode = COMMON.OK
try:
if not ( file_exists( FILE_PATH ) ):
print( 'Err locate ['+FILE_PATH+']' )
return COMMON.ERR
# Apply 'Black' formatter over the file
result = exec_process( 'black ' + FILE_PATH )
#if COMMON.OK != result:
# print('Err formating settings')
# exit(1)
except Exception as e:
print('Err formating exeption: ' + str(e) )
retcode = COMMON.ERR
return retcode
def file_format( FILE_PATH ):
return cfg_format( FILE_PATH )
def file_process( FILE_PATH, MARKER, aContent ):
retcode, content = cfg_load( FILE_PATH )
file_c = []
MARKER_BEGIN = '#' + MARKER
MARKER_END = '#END' + MARKER
processing = False
for line in content:
if MARKER_BEGIN in line:
processing = True
content_new = ''
content_new += MARKER_BEGIN + '\n'
content_new += aContent + '\n'
content_new += MARKER_END + '\n'
file_c.append( content_new )
elif processing:
if MARKER_END in line:
processing = False
else:
file_c.append( line )
return cfg_save( FILE_PATH, file_c )
def h_var_typology( content ):
if not content:
return COMMON.CFG_VAR_NA
if '=' in content and '[' in content:
return COMMON.CFG_VAR_LIST
if '=' in content and '{' in content:
return COMMON.CFG_VAR_DICT
if '=' in content and not '[' in content and not '}' in content:
return COMMON.CFG_VAR_SIMPLE
# Default is unknown
return COMMON.CFG_VAR_NA
def h_extract_sections( content ):
file_imports = []
file_sections = []
for line in content:
# import here
if 'from ' in line or 'import ' in line:
file_imports.append ( h_del_lsep( line ) ) # strip line separators
# main sections here
if '=' in line:
file_sections.append( line.split('=')[0].strip() )
print("Imports : " + str( file_imports ) )
print("Sections : " + str( file_sections ) )
return file_sections
def cfg_imports( FILE_PATH ):
retcode = COMMON.OK
imports = []
retcode, content = cfg_load( FILE_PATH )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] file')
return retcode, None
for line in content:
# import here
if 'from ' in line or 'import ' in line:
imports.append ( h_del_lsep( line ) ) # strip line separators
return retcode, imports
def cfg_sections( FILE_PATH ):
retcode = COMMON.OK
sections = []
retcode, content = cfg_load( FILE_PATH )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] file')
return retcode, None
for line in content:
# import here
if '=' in line and '#' not in line:
sections.append( line.split('=')[0].strip() )
return retcode, sections
def cfg_var_upd( FILE_PATH, var_name, var_value, SkipQuotes=False):
retcode = COMMON.NOT_FOUND
retcode, content = cfg_load( FILE_PATH )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] file')
return retcode
new_content = []
found = False
for line in content:
if var_name not in line:
new_content.append( line )
continue
found = True
# variable found
retcode = COMMON.OK
var_typology = h_var_typology( line )
if COMMON.CFG_VAR_SIMPLE == var_typology:
aValue_c = var_value
if aValue_c.lower() == 'random':
var_value = h_random()
if SkipQuotes:
line = var_name + ' = ' + var_value
else:
line = var_name + ' = ' + '"' + var_value + '"'
new_content.append( line )
if not found:
line = var_name + ' = ' + '"' + var_value + '"'
new_content.append( line + '\n' )
# Variable was found and successfully processed
if COMMON.OK == retcode:
cfg_save( FILE_PATH, new_content )
return retcode
def cfg_var_comment( FILE_PATH, var_name):
retcode = COMMON.NOT_FOUND
retcode, content = cfg_load( FILE_PATH )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] file')
return retcode
new_content = []
found = False
for line in content:
if var_name not in line:
new_content.append( line )
continue
found = True
# variable found
retcode = COMMON.OK
var_typology = h_var_typology( line )
if COMMON.CFG_VAR_SIMPLE == var_typology:
line = '#' + line
new_content.append( line + '\n' )
if not found:
line = var_name + ' = ' + '"' + var_value + '"'
new_content.append( line + '\n' )
# Variable was found and successfully processed
if COMMON.OK == retcode:
cfg_save( new_content )
return retcode
def cfg_var_print( FILE_PATH, var_name ):
retcode = COMMON.NOT_FOUND
retcode, content = cfg_load( FILE_PATH )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] file')
return retcode
new_content = []
for line in content:
if var_name not in line:
new_content.append( line )
continue
else:
# variable found
retcode = COMMON.OK
var_typology = h_var_typology( line )
print(' > Var found : ' + h_del_lsep( line ) )
print(' > Var typology : ' + commonTxt( var_typology ) )
if COMMON.OK != retcode:
print(' > Var not found ')
return retcode
def cfg_section_get( FILE_PATH, section ):
retcode = COMMON.NOT_FOUND
retcode, content = cfg_load( FILE_PATH )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] file')
return retcode
section_content = []
section_begin = False
section_end = False
retcode = COMMON.NOT_FOUND
# This is used to count [, {
section_control_begin = ''
section_control_end = ''
section_control_idx = 0
for line in content:
line = h_del_lsep( line )
# We have an end here
if section_end:
break
# Computing is over
if section_begin and section_control_idx == 0:
section_end = True
retcode = COMMON.OK
continue
# Computing is active
if section_begin:
section_content.append( line )
if section_control_begin in line:
section_control_idx += 1
if section_control_end in line:
section_control_idx -= 1
# Other things
if not section_begin and section not in line:
continue
# Here is a match
if '=' in line:
section_begin = True
# Detect topology
var_typology = h_var_typology( line )
# Simple, one-time
if COMMON.CFG_VAR_SIMPLE == var_typology:
section_content.append( line )
section_end = True
retcode = COMMON.OK
# Lists (we can have mixed types)
if COMMON.CFG_VAR_LIST == var_typology:
# save the first line
section_content.append( line )
section_begin = True
retcode = COMMON.OK
section_control_begin = '['
section_control_end = ']'
section_control_idx = 1
# Dicts (we can have mixed types)
if COMMON.CFG_VAR_DICT == var_typology:
# save the first line
section_content.append( line )
section_begin = True
retcode = COMMON.OK
section_control_begin = '{'
section_control_end = '}'
section_control_idx = 1
# Exit
if COMMON.OK == retcode:
#print( 'BEGIN >>>' )
#line_nb = 0
#for item in section_content:
# line_nb += 1
# print ( str(line_nb) + '|'+ item )
#print( '<<< END' )
pass
else:
print( ' > Section ['+section+'] not found ' )
# Exit point
return retcode, section_content
def cfg_section_update( FILE_PATH, aSectionName, aSectionContent ):
retcode = COMMON.OK
retcode, content = cfg_load( FILE_PATH ) # as list
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] file')
return retcode
content_p = []
section_start = False
section_end = False
for line in content:
if line.startswith( aSectionName ):
section_start = True
content_p.append(aSectionContent)
continue
if not section_start:
content_p.append( line )
if section_start and not section_end and (']' == line):
section_end = True
continue
if section_end:
content_p.append( line )
# Exit
if COMMON.OK == retcode:
# Save the content
cfg_save(FILE_PATH, content_p )
cfg_format( FILE_PATH )
# Exit point
return retcode
def cfg_section_list( FILE_PATH, SECTION_NAME ):
retcode = COMMON.OK
# load INSTALLED_APPS
retcode, content = cfg_section_get(FILE_PATH, SECTION_NAME)
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] -> ['+SECTION_NAME+'] section')
return retcode, None
return retcode, content[1:(len(content)-1)] # slice, cut first & last element
def cfg_section_add_item( FILE_PATH, SECTION_NAME, aItem, SkipQuotes=False ):
# Check for duplicates
retcode, all_items = cfg_section_list( FILE_PATH, SECTION_NAME)
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] -> ['+SECTION_NAME+']')
return retcode, None
retcode, section_content = cfg_section_get( FILE_PATH, SECTION_NAME )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] -> ['+SECTION_NAME+']')
return retcode, None
# Processing (LIST) type
section_content_str = ''
for line in section_content:
# insert new app at the end
if ']' in line:
if SkipQuotes:
section_content_str += ' ' + aItem + ',\n'
else:
section_content_str += ' "' + aItem + '",\n'
section_content_str += line + '\n'
retcode = cfg_section_update( FILE_PATH, SECTION_NAME, section_content_str)
# Exit
if COMMON.OK == retcode:
print( 'Section ['+SECTION_NAME+'] updated successfully' )
else:
print( 'Error updating ['+SECTION_NAME+'] section' )
# Exit point
return retcode, section_content
def cfg_section_add_item_first( FILE_PATH, SECTION_NAME, aItem ):
# Check for duplicates
retcode, all_items = cfg_section_list( FILE_PATH, SECTION_NAME)
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] -> ['+SECTION_NAME+']')
return retcode, None
retcode, section_content = cfg_section_get( FILE_PATH, SECTION_NAME )
if COMMON.OK != retcode:
print('Err loading ['+FILE_PATH+'] -> ['+SECTION_NAME+']')
return retcode, None
# Processing (LIST) type
section_content_str = ''
is_first_line = False
for line in section_content:
# insert new app at the end
if '[' in line:
is_first_line = True
section_content_str += line + '\n'
continue
if is_first_line:
is_first_line = False
section_content_str += ' "' + aItem + '",\n'
# All other lines
section_content_str += line + '\n'
retcode = cfg_section_update( FILE_PATH, SECTION_NAME, section_content_str)
# Exit
if COMMON.OK == retcode:
print( 'Section ['+SECTION_NAME+'] updated successfully' )
else:
print( 'Error updating ['+SECTION_NAME+'] section' )
# Exit point
return retcode, section_content