-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
108 lines (84 loc) · 3.08 KB
/
util.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
import os
from string import Formatter
from PySide2.QtCore import QLocale
run_time_root = os.path.dirname(__file__)
# contract_template_path = os.path.join(run_time_root, "templates/contract_templates/cht/中文契約範本.template")
# QMessageBox.information(self, "Info", contract_template_path)
# with open(contract_template_path, 'rb') as f:
# txt = f.read().decode('UTF-8')
# QMessageBox.information(self, "Info", txt)
# party_a_template_path = os.path.join(run_time_root, "templates/party_a_template/cht/cht-甲方.template")
# QMessageBox.information(self, "Info", party_a_template_path)
# with open(party_a_template_path, 'rb') as f:
# txt = f.read().decode('UTF-8')
# QMessageBox.information(self, "Info", txt)
"""
IBM 3 character code
"""
def lang2code(name):
return {
u"\u7e41\u9ad4\u4e2d\u6587": "cht",
u"English": "enu",
u"\u65e5\u672c\u8a9e": "jpn",
u"\ud55c\uad6d\uc5b4": "kor"
}.get(name, "cht")
# path utils
def get_contract_template_dir(lang):
return os.path.join(run_time_root, "templates/contract_templates/", lang2code(lang))
def get_party_a_template_dir(lang):
return os.path.join(run_time_root,"templates/party_a_template/", lang2code(lang))
def get_payment_method_template_dir(lang):
return os.path.join(run_time_root, "templates/payment_method_templates/", lang2code(lang))
# control utils
def enable_controls(control_list, enable):
for control in control_list:
control.setEnabled(enable)
# text file utils
def read_utf8(path):
if os.path.exists(path):
with open(path, 'rb') as f:
return f.read().decode('UTF-8')
def parse_fields(txt):
if txt:
return [fname for _, fname, _, _ in Formatter().parse(txt) if fname]
def read_fields(path):
txt = read_utf8(path)
return parse_fields(txt)
# date
def zh_tw_date_format():
return "yyyy 年 MM 月 dd 日"
def en_us_date_format():
return "MMMM dd, yyyy"
def ja_jp_date_format():
return "yyyy 年 MM 月 dd 日"
def ko_kr_date_format():
return "yyyy 년 MM 월 dd 일"
def select_date_format(x):
return {
"繁體中文": zh_tw_date_format(),
"English": en_us_date_format(),
"日本語": ja_jp_date_format(),
"한국어": ko_kr_date_format()
}.get(x, en_us_date_format())
# locale
def zh_tw_locale():
return QLocale(QLocale.Chinese, QLocale.TraditionalChineseScript, QLocale.Taiwan)
def en_us_locale():
return QLocale(QLocale.English, QLocale.UnitedStates)
def ja_jp_locale():
return QLocale(QLocale.Japanese, QLocale.Japan)
def ko_kr_locale():
return QLocale(QLocale.Korean, QLocale.SouthKorea)
def select_locale(x):
return {
"繁體中文": zh_tw_locale(),
"English": en_us_locale(),
"日本語": ja_jp_locale(),
"한국어": ko_kr_locale()
}.get(x, en_us_locale())
# locale and date util
def select_locale_and_date_format(x):
return (select_locale(x), select_date_format(x))
def to_date_str(lang, qdate):
curr_locale, date_fmt = select_locale_and_date_format(lang)
return curr_locale.toString(qdate, date_fmt)