forked from alyf-de/erpnext_druckformate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
110 lines (86 loc) · 2.89 KB
/
update.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
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
101
102
103
104
105
106
107
108
109
110
from configparser import ConfigParser
from getpass import getpass
from pathlib import Path
from subprocess import run
import typer
from frappeclient import FrappeClient
def abspath(relpath: str) -> Path:
return Path(__file__).parent / relpath
def get_css(input_path: Path) -> str:
return run(
["sass", "--style=compressed", input_path], check=True, capture_output=True
).stdout.decode()
def get_credentials(
config: ConfigParser,
base_url: str | None,
username: str | None,
password: str | None,
) -> tuple[str, str, str]:
base_url = base_url or config.get("DEFAULT", "BaseURL")
username = username or config.get("DEFAULT", "User")
password = password or config.get("DEFAULT", "Password")
while not base_url:
base_url = input("Base URL: ")
while not username:
username = input("Username: ")
while not password:
password = getpass()
return base_url, username, password
def print_format_dict(
print_format_name: str,
doctype: str,
module: str | None,
html: str,
css: str,
is_standard: bool,
) -> dict:
data = {
"doctype": "Print Format",
"name": print_format_name,
"doc_type": doctype,
"custom_format": 1,
"html": html,
"css": css,
"print_format_type": "Jinja",
"standard": "Yes" if is_standard else "No",
}
if module:
data["module"] = module
return data
def sync(client: FrappeClient, print_format: dict) -> None:
if client.get_list(
"Print Format", fields=["name"], filters={"name": print_format["name"]}
):
client.update(print_format)
else:
client.insert(print_format)
def main(
base_url: str = None,
username: str = None,
password: str = None,
config_file: str = None,
only_template: str = None,
):
config = ConfigParser()
config.read(abspath(config_file or "config.ini"))
base_url, username, password = get_credentials(config, username, password, base_url)
client = FrappeClient(url=base_url, username=username, password=password)
css = get_css(abspath(config.get("DEFAULT", "ScssFile")))
for print_format_name in config.sections():
file_path = abspath(config.get(print_format_name, "TemplateFile"))
if only_template and Path(only_template).name != file_path.name:
continue
print(f"Syncing {file_path.name} -> Print Format '{print_format_name}'")
sync(
client,
print_format_dict(
print_format_name,
doctype=config.get(print_format_name, "DocType"),
module=config.get(print_format_name, "Module", fallback=None),
html=file_path.read_text(),
css=css,
is_standard=config.getboolean(print_format_name, "IsStandard"),
),
)
if __name__ == "__main__":
typer.run(main)