-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
55 lines (44 loc) · 1.28 KB
/
utils.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
import config
import re
# clean_output(text, context)
# This utility function accepts a text string and "context" tag indicating what the string represents, and
# applies rules to clean it up for return.
def clean_output(text, context=False):
stripped = text.strip(" ")
# an empty dates range, "( - )", return nothing
if stripped == "( - )":
return ""
# any date string leading with @
if context == "date":
return re.sub('@.+@', '', text).strip(" ")
return stripped
# return_empty(value)
def return_empty(value):
if value is None:
return ""
stripped = value.strip(" ")
return stripped
# return id from an id_xref element
def get_id(obj):
(first, s, last) = obj.xref_id.split("@")
prefix = s[0].lower()
n = int(s[1:])
id = f"{prefix}{n:05d}"
return id
# open an iXXXXX.md or fXXXXX.md file in the specified mode and return the file pointer
def open_md(id, mode):
# open a file at {opath}/{id}.md
path = f"{config.opath}{id}.md"
# if mode is 'w' or 'r' open the file and return
if mode in "wr":
f = open(path, mode)
return f
# if mode is 'a' make sure we rewind one line!
if mode == "a":
f = open(path, "r")
lines = f.readlines()
lines = lines[:-1]
f = open(path, "w")
for line in lines:
f.write(line)
return f