def write_to_shelve_file(my_shelve_data_dict = None): global shelve_data_structure test_dict = my_shelve_data_dict if not test_dict: test_dict = shelve_data_structure general = util.load_general_section() #ie ../istudy_large_files/SHELVE/shelve.data filename = '{}/{}'.format(general['shelve'], 'shelve.data') print('Writing to ', filename) with open(filename, mode='w') as foo: keys = list(test_dict.keys()) keys.sort() #For each table name for e in keys: print('[[{}]]'.format(e), file=foo ) #For each data filename for filename in test_dict[e]: print('[{}]'.format(filename), file=foo ) #For each key in filename section for key in test_dict[e][filename]: print('{} {}'.format(key, test_dict[e][filename][key]), file=foo ) print(file=foo) print(file=foo)
def read_from_shelve_file(): import re import os global shelve_data_structure temp = shelve_data_structure general = util.load_general_section() filename = '{}/{}'.format(general['shelve'], 'shelve.data') #Create shelve file with empty data structure if not exist if not os.path.exists(filename): write_to_shelve_file() #Even matches empty between double-bracket pairs table_section_pattern = re.compile(r'^\[\[(.*?)\]\]$') #At least 2 chars between brackets ??? fixed #filename_section_pattern = re.compile(r'^\[([^\[].*?[^\]])?\]$') filename_section_pattern = re.compile(r'^\[(?![\[])(.*?)(?<![\]])\]$') #Key is tab-separated from value data_section_pattern = re.compile(r'^(?P<key>[^\t]+)\t+(?P<value>[^\t]+)$') lines = [] last_table = None last_filename = None with open(filename) as foo: last_position = foo.tell() #For each line for line in foo: line = line.strip() #Skip blank line if not line: continue #Skip comment line if line[0] == '#': continue #Store table name line #lines.append(line) mo = table_section_pattern.search(line) if mo: #print(mo.group(1)) last_table = mo.group(1) continue #Store filename line mo = filename_section_pattern.search(line) if mo: #print(last_table, mo.group(1)) last_filename = mo.group(1) continue #Store data lines mo = data_section_pattern.search(line) if mo: #print(last_table, last_filename, mo.group('key'), mo.group('value')) if last_filename not in temp[last_table]: temp[last_table][last_filename] = {} try: temp[last_table][last_filename][mo.group('key').strip()] = \ eval(mo.group('value').strip()) except SyntaxError as e: #print('[read_from_shelve_file] eval("{}") failed' # .format(e.text) ) ... except: raise #print(temp) #write_to_shelve_file(temp) return temp