-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
79 lines (57 loc) · 2.12 KB
/
parser.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
"""
Simple parser to produce a markdown output for a text file with predeterminate format.
"""
from pathlib import Path
input_file = "data.txt"
predeterminate_format = "# {}\n\nAuthor: {}\n\n"
def get_file_content(filename):
with open(filename, 'r') as reader:
all_lines = reader.read().splitlines()
return all_lines
def get_file_pure_content(filename):
with open(filename, 'r') as reader:
all_lines = reader.readlines()
return all_lines
def store_file(filename, data):
with open(filename, 'w') as writer:
for line in data:
writer.write(line)
def scan_directory(directory):
files = Path(directory)
all_files = [filename.name for filename in files.iterdir()]
return all_files
def parse_file(filename, predeterminate_format):
input_data = get_file_content(filename)
input_prefix = Path(filename).stem
input_suffix = Path(filename).suffix
data_to_store = []
for line in input_data:
parsed_line = line.split(',')
parsed_line = [entry.strip(' ') for entry in parsed_line]
data_to_store.append(predeterminate_format.format(
parsed_line[1], parsed_line[0]))
store_file("{}_{}_output.md".format(
input_prefix, input_suffix), data_to_store)
def get_complete_path_filename(base_path, filename):
return "{}/{}".format(base_path, filename)
def get_all_formats(data_dir, format_dir):
all_formats = []
all_formats_files = scan_directory(
get_complete_path_filename(data_dir, format_dir))
for format_file in all_formats_files:
content = get_file_pure_content(get_complete_path_filename(
get_complete_path_filename(data_dir, format_dir), format_file))
all_formats.append(content)
return all_formats
def main(data_dir, input_dir, format_dir, opening_braquet, closing_braquet):
try:
pass
except IOError:
print("File Error")
if __name__ == "__main__":
data_dir = "data"
input_dir = "input"
format_dir = "format"
opening_braquet = "{"
closing_braquet = "}"
main(data_dir, input_dir, format_dir, opening_braquet, closing_braquet)