-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_utils.py
41 lines (35 loc) · 1.21 KB
/
setup_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
import os
import re
from io import open
RE_VARIABLE = r"{} = ['\"]([^'\"]*)['\"]"
RE_README_SECTION_HEADING = r"#+ {}"
RE_README_SECTION = r"{}[^#]*"
RE_NEWLINES = r"{}\n+"
README_NAME = "README.md"
def read_markdown(path, title, sentences=(0,)):
content = read(path)
heading_regex = RE_README_SECTION_HEADING.format(title)
section_regex = RE_README_SECTION.format(heading_regex)
newline_regex = RE_NEWLINES.format(heading_regex)
match = re.search(section_regex, content)
try:
desc = match.group(0)
desc = desc.strip()
desc = re.sub(newline_regex, "", desc)
parts = desc.split(".")
desc = ". ".join([x for x in parts if parts.index(x) in sentences])
desc = desc.strip()
except AttributeError:
desc = ""
return desc
def read(parts, variable=None):
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), *parts)
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
if variable is None:
return content
regex = RE_VARIABLE.format(variable)
match = re.search(regex, content, re.M)
if match:
return match.group(1)
raise RuntimeError("Failed to read {} in {}".format(variable, path))