Skip to content

Instantly share code, notes, and snippets.

@jgillmanjr
Created October 20, 2018 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgillmanjr/0fa93c6d97c9fdba4c1a3c9fcd1744c8 to your computer and use it in GitHub Desktop.
Save jgillmanjr/0fa93c6d97c9fdba4c1a3c9fcd1744c8 to your computer and use it in GitHub Desktop.
Perl CIFP Parser main parser def conversion
# Parse the various parser pl files - because fuck manually working that shit if avoidable
# This is also making some horridly bad one-off assumptions. Not reusable *at all*
raw_parsers_pl = requests.get('https://raw.githubusercontent.com/jgillmanjr/parseCifp/master/parsers.pl').text.splitlines()
# Regexy shit
ntr_key_check_re = re.compile('\'([A-Z]*)\'')
sec_key_xit_re = re.compile('\}')
xit_key_check_re = re.compile('.*,')
has_sq_re = re.compile('\'')
parser_data_re = re.compile('.*([a-zA-Z]+:[0-9]+)')
# Do some initial cleanup and condensing
iter_one = []
for line in raw_parsers_pl:
pre_iter = line.replace(' ', '').replace('=>', ':') # Just collapse this shit
if not (len(pre_iter) == 0 or pre_iter[0] in ['#', ' ']):
iter_one.append(pre_iter)
# Really do shit
#iter_two = []
preparse_dict = {}
sec_key = None
subsec_key = None
for i in iter_one:
# Set booleans and data, yo
if ntr_key_check_re.match(i) is not None:
has_ntr_key = True
current_key = ntr_key_check_re.match(i).group(1)
key_grp_all = ntr_key_check_re.match(i).group()
else:
has_ntr_key = False
key_grp_all = ''
has_sec_key_xit = sec_key_xit_re.match(i) is not None
has_xit_key = xit_key_check_re.match(i) is not None
if parser_data_re.match(i) is not None:
has_parser_data = True
raw_parser_data = parser_data_re.match(i).group().strip(key_grp_all).strip(':\'')
field = raw_parser_data.split(':')[0]
fieldlen = int(raw_parser_data.split(':')[1])
parser_data = (field, fieldlen)
else:
has_parser_data = False
# End booleans
# print('ntr:{ntr}, scx:{scx}, xit:{xit}, has_prs_d:{prsr}\n{i}'.format(
# ntr=has_ntr_key, scx=has_sec_key_xit, xit=has_xit_key, prsr=has_parser_data, i=i))
if has_ntr_key:
if sec_key is None:
sec_key = current_key
preparse_dict[sec_key] = {}
else:
subsec_key = current_key
preparse_dict[sec_key][subsec_key] = []
if has_parser_data:
preparse_dict[sec_key][subsec_key].append(parser_data)
if has_xit_key:
if has_sec_key_xit:
subsec_key = None
sec_key = None
continue
if subsec_key is not None:
subsec_key = None
continue
# Now a dict of the actual parsers for sections
parse_dict = {}
for tk, tv in preparse_dict.items():
parse_dict[tk] = {}
for sk, sv in tv.items():
parse_dict[tk][sk] = FixedWidth(p_builder(sv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment