-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoneymoney_api.py
41 lines (30 loc) · 1.04 KB
/
moneymoney_api.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
#!/usr/bin/env python3
"""
Python MoneyMoney API
@see https://moneymoney-app.com/applescript/
"""
import plistlib
import subprocess
from typing import TypedDict
class Account(TypedDict):
name: str
bankCode: str
balance: list[list[float | str]]
portfolio: bool
group: bool
attributes: dict[str, str]
def __run_apple_script(script: str) -> bytes:
command = ['osascript', '-e', script]
with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as pipe:
result = pipe.communicate()
if result[1]:
raise RuntimeError(f'Could not run Apple Script "{script}": {result[1].decode().strip()}')
return result[0]
def fetch_moneymoney_accounts() -> list[Account]:
result = __run_apple_script('tell application "MoneyMoney" to export accounts')
# Parse XML property list.
try:
plist = plistlib.loads(result)
except plistlib.InvalidFileException as exception:
raise ValueError('Could not parse XML property list') from exception
return plist