-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashformat.py
49 lines (40 loc) · 1.75 KB
/
hashformat.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
# -*- coding: utf-8 -*-
__author__ = 'essepuntato'
import re
def process_hashformat(file_path):
result = []
with open(file_path, "rU") as f:
first_field_name = None
cur_object = None
cur_field_name = None
cur_field_content = None
for line in f.readlines():
cur_matching = re.search("^#([^\s]+)\s(.+)$", line, re.DOTALL)
if cur_matching is not None:
cur_field_name = cur_matching.group(1)
cur_field_content = cur_matching.group(2)
# If both the name and the content are defined, continue to process
if cur_field_name and cur_field_content:
# Identify the separator key
if first_field_name is None:
first_field_name = cur_field_name
# If the current field is equal to the separator key,
# then create a new object
if cur_field_name == first_field_name:
# If there is an already defined object, add it to the
# final result
if cur_object is not None:
result += [cur_object]
cur_object = {}
# Add the new key to the object
cur_object[cur_field_name] = cur_field_content
elif cur_object is not None and len(cur_object) > 0:
cur_object[cur_field_name] += line
# Insert the last object in the result
if cur_object is not None and len(cur_object) > 0:
result += [cur_object]
# Clean the final \n
for item in result:
for key in item:
item[key] = item[key].rstrip()
return result