-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-receipt
More file actions
executable file
·100 lines (86 loc) · 2.55 KB
/
new-receipt
File metadata and controls
executable file
·100 lines (86 loc) · 2.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env -S uv --quiet run --no-project --script --
# https://peps.python.org/pep-0723/
# https://github.com/astral-sh/uv
# /// script
# requires-python = ">=3.14,<4"
# dependencies = [
# "python-dateutil >=2.7.3"
# ]
# ///
import asyncio, pathlib, subprocess, os
import dateutil.parser
async def main(*, args=None, prog=None, loop=None):
receipt_files = [pathlib.Path(p) for p in args]
if not receipt_files:
file_dir = pathlib.Path.home() / "Downloads"
file_base_name = "receipt"
counter = 0
file_path = file_dir / f"{file_base_name}.txt"
while file_path.exists():
counter += 1
file_path = file_dir / f"{file_base_name}-{counter}.txt"
print(file_path)
fields = [
"Transaction Date",
"Location",
"Payee",
"Account",
"Amount",
]
if file_path.exists():
raise RuntimeError(f"already exists: {file_path}")
with open(file_path, "w") as fo:
fo.write("\n".join(f"{field}: ?" for field in fields) + "\n")
editor_cmd = await get_editor_cmd(loop)
p = await asyncio.create_subprocess_shell(f"{editor_cmd} {file_path}")
await p.communicate()
receipt_files.append(file_path)
for file_path in receipt_files:
with open(file_path, "r") as fo:
for line in fo:
name, sep, value = line.partition(":")
if sep != ":":
continue
if name == "Transaction Date":
date = dateutil.parser.parse(value)
elif name == "Payee":
payee = value.strip()
elif name == "Account":
account = value.strip()
elif name == "Amount":
amount = value.strip()
new_file_name = "{date:%Y-%m-%d} {account} {amount} {payee}.txt".format(
date=date,
account=account,
amount=amount,
payee=payee
)
new_file_path = file_path.parent / new_file_name
if new_file_path.exists():
raise RuntimeError(f"already exists: {new_file_path}")
print(f"{file_path} -> {new_file_path}")
os.rename(file_path, new_file_path)
async def get_editor_cmd(loop):
p = await asyncio.create_subprocess_exec(
"git", "var", "GIT_EDITOR",
stdout=subprocess.PIPE,
loop=loop
)
stdout_data, stderr_data = await p.communicate()
if stderr_data:
raise RuntimeError(stderr_data)
return stdout_data.decode().strip()
def _smain(*, args=None, prog=None):
if sys.platform == "win32":
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
loop = asyncio.get_event_loop()
return loop.run_until_complete(main(args=args, prog=prog, loop=loop))
def _ssmain():
try:
sys.exit(_smain(args=sys.argv[1:], prog=sys.argv[0]))
except KeyboardInterrupt:
sys.stderr.write("\n")
if __name__ == "__main__":
_ssmain()