Created
June 22, 2020 13:06
-
-
Save kidpixo/3907f0aeb260cce68db8b453971decf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter_not_zero = lambda x : list(filter(None,x)) | |
def extract_history(opusdata, histkey=None): | |
if histkey is None: | |
histkey='History' | |
history = filter_not_zero([s for s in opusdata['History'].split('COMMAND_LINE')]) | |
return [extract_single_history(h) for h in history] | |
def extract_single_history(stri): | |
stri_dict = {} | |
def tryEval(s): | |
try: | |
return eval(s, {}, {}) | |
except: | |
return s | |
if ';' in stri : | |
stri_spl = stri.split(';') | |
stri_dict['comment'] = stri_spl[1].replace('\x00','') | |
# if Operation in comment | |
if 'Operation' in stri_dict['comment']: | |
tmp_stri_dict_comment = [] | |
tmp_stri_dict_comment.append( stri_dict['comment'].split('Operation:')[0].replace('\t','') ) | |
operation_dict = {} | |
operation_list = iter(filter_not_zero(stri_dict['comment'].split('Operation:')[1].split('\t'))) | |
operation_dict['Operation'] = next(operation_list) | |
for el in operation_list: | |
key, val = el.split(':') | |
operation_dict[key.strip()] = val[1:-1] if "'" in val else tryEval(val) | |
tmp_stri_dict_comment.append(operation_dict) | |
stri_dict['comment'] = tmp_stri_dict_comment | |
else: | |
stri_dict['comment'] = stri_dict['comment'].replace('\t','') | |
di = {} | |
di['Preamble'] = stri_spl[0].split('(')[1][:-1].split('{')[0] | |
import re | |
# extract text in [] and split to list | |
di['Preamble'] = re.search(r'\[(.*?)\]',di['Preamble']).group(1).split(',') | |
s = stri_spl[0].split('(')[1][:-1].split('{')[1][:-1] | |
if len(s) != 0 : | |
for el in s.split(','): | |
key, val = el.split('=') | |
di[key.strip()] = val[1:-1] if "'" in val else tryEval(val) | |
# dict title = COMMAND_LINE argument | |
stri_dict[stri_spl[0].split('(')[0].strip()] = di | |
else: | |
stri_dict[stri_spl[0].split('(')[0].strip()] = {} | |
return stri_dict | |
else : | |
return filter_not_zero(initial_history.split('\t')) | |
opus_data_history = extract_history(opus_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment