Repository navigation
Expand file tree
/
Copy pathwrite.py
More file actions
74 lines (68 loc) · 2.18 KB
/
Copy pathwrite.py
File metadata and controls
74 lines (68 loc) · 2.18 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import datetime
import os
import pathlib
import subprocess
import tempfile
import config
import errors
import utils
BASE_FILE_PATH: str = f"{pathlib.Path().home()}/.entry/entries/"
MONTHS: dict[int, str] = {
1: "january",
2: "february",
3: "march",
4: "april",
5: "may",
6: "june",
7: "july",
8: "august",
9: "september",
10: "october",
11: "november",
12: "december"
}
def entry_from_editor() -> utils.Entry:
editor = os.environ.get("EDITOR", "nano")
# BUG: using helix does not work
# fallback to nano for now
if editor == "helix":
editor = "nano"
initial_msg = "\n\n========== NEW ENRTY =========="
initial_msg += "\nAuthor: "
initial_msg += config.get_active_config_name()
initial_msg += "\nTimestamp: "
initial_msg += utils.get_date_string()
initial_msg = initial_msg.encode()
with tempfile.NamedTemporaryFile() as tmp:
_ = tmp.write(initial_msg)
tmp.flush()
_ = subprocess.call([editor, tmp.name])
_ = tmp.seek(0)
content: str = tmp.read().decode("utf-8")
cutoff = content.find("=")
content = content[:cutoff].rstrip()
new_entry = utils.Entry(
content=content,
author_name=config.get_active_config_name(),
author_id=config.get_active_config_id(),
timestamp=utils.get_date_string()
)
return new_entry
def entry_from_message(msg: str) -> utils.Entry:
return utils.Entry(
content=msg,
author_name=config.get_active_config_name(),
author_id=config.get_active_config_id(),
timestamp=utils.get_date_string()
)
def save_entry(entry: utils.Entry) -> None:
if entry.content == "":
return
current_month = datetime.datetime.now().month
current_year = datetime.datetime.now().year
mkdir = subprocess.run(["mkdir", "-p", f"{BASE_FILE_PATH}/y{current_year}"])
if mkdir.returncode != 0:
raise errors.MkdirError()
with open(f"{BASE_FILE_PATH}/y{current_year}/{MONTHS[current_month]}.toml", "a") as f:
_ = f.write(entry.to_toml())
print("Entry saved")