-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathris_conv.py
69 lines (50 loc) · 2.43 KB
/
ris_conv.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
import re
from csv import DictWriter
from config import *
from string_builder import StringBuilder
class RISConv:
def __init__(self, file_path: str) -> None:
self._entries = []
self._tags_in_file = {}
self._file_path = file_path
def ris_to_dict(self) -> list:
if not self._entries:
with open(self._file_path, encoding=ENCONDING) as ris_file:
new_entry_flag = True
for line in ris_file:
line = line.strip()
if not line:
continue
if new_entry_flag:
self._entries.append({})
new_entry_flag = False
search = re.search('(\w{2}) - ?(.*)', line)
if search is None:
current_entry[name_tag].add(line)
continue
tag_start_pos = search.start()
if tag_start_pos != 0:
current_entry[name_tag].add(line[:tag_start_pos])
current_tag, tag_value = search.groups()
name_tag = TAG_KEY_MAPPING[current_tag]
if current_tag == END_REFERENCE_TAG:
new_entry_flag = True
continue
current_entry = self._entries[-1]
if current_tag == TYPE_TAG:
current_entry[name_tag] = TYPE_REFERENCE_MAPPING[tag_value]
elif current_tag == NOTE_TAG and tag_value.startswith(CITED_BY_TEXT):
name_tag = TAG_KEY_MAPPING[CITED_BY_TAG]
current_entry[name_tag] = re.search(':(\\d+)', tag_value).group(1)
elif name_tag not in current_entry:
current_entry[name_tag] = StringBuilder(tag_value)
else:
current_entry[name_tag].add(f'{SUB_DELIMITER}{tag_value}')
self._tags_in_file[name_tag] = True
return self._entries
def ris_to_csv(self, file_path: str) -> None:
entries = self.ris_to_dict()
with open(file_path, 'w', encoding=ENCONDING, newline='') as file:
writer = DictWriter(file, self._tags_in_file, delimiter=CSV_DELIMITER)
writer.writeheader()
writer.writerows(entries)