-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdata_reader.py
More file actions
57 lines (41 loc) · 1.55 KB
/
Copy pathdata_reader.py
File metadata and controls
57 lines (41 loc) · 1.55 KB
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
"""Read data from files"""
from importlib import resources
from telegram.helpers import escape_markdown
from .config import Config
def get_abs_path(*root_file_path: str) -> str:
"""Get the abs path from the root directory of the project to the requested path
Args:
root_file_path: path from the root project directory
Returns:
corresponding abs path
"""
base_path = resources.files("spotted")
result_path = base_path
for part in root_file_path:
result_path = result_path / part
return str(result_path)
def read_file(*root_file_path: str) -> str:
"""Read the contents of the file
Args:
root_file_path: path of the file to read from the root project directory
Returns:
contents of the file
"""
with open(get_abs_path(*root_file_path), "r", encoding="utf-8") as in_file:
text = in_file.read().strip()
return text
def read_md(file_name: str) -> str:
"""Read the contents of a markdown file.
The path is data/markdown.
It also will replace the following parts of the text:
- {channel_tag} -> Config.settings['post']['channel_tag']
- {bot_tag} -> Config.settings['bot_tag']
Args:
file_name: name of the file
Returns:
contents of the file
"""
text = read_file("config", "markdown", f"{file_name}.md")
text = text.replace("{channel_tag}", escape_markdown(Config.post_get("channel_tag"), version=2))
text = text.replace("{bot_tag}", escape_markdown(Config.settings_get("bot_tag"), version=2))
return text