-
Notifications
You must be signed in to change notification settings - Fork 1
/
lfy.in
executable file
·117 lines (88 loc) · 2.93 KB
/
lfy.in
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
111
112
113
114
115
116
117
#!@PYTHON@
# lt
import argparse
import gettext
import locale
import os
import subprocess
import sys
import gi
gi.require_version("Adw", "1")
gi.require_version('Gio', '2.0')
gi.require_version('Gtk', '4.0')
gi.require_version('Gdk', '4.0')
APP_ID = '@APP_ID@'
VERSION = '@VERSION@'
SCHEMAS_DIR = '@SCHEMAS_DIR@'
PYTHON_DIR = '@PYTHON_DIR@'
LOCALE_DIR = '@LOCALE_DIR@'
PKGDATA_DIR = '@PKGDATA_DIR@'
if not os.path.exists(PKGDATA_DIR):
# 说明是pip这一类的
print(f"路径修正:{SCHEMAS_DIR}")
THIS_DIR, THIS_FILENAME = os.path.split(__file__)
PYTHON_DIR = os.path.abspath(f"{THIS_DIR}/../lib/")
SCHEMAS_DIR = os.path.abspath(f"{THIS_DIR}/../share/")
PKGDATA_DIR = os.path.abspath(f"{SCHEMAS_DIR}/{APP_ID}/")
LOCALE_DIR = os.path.abspath(f"{SCHEMAS_DIR}/locale/")
subprocess.run(
["glib-compile-schemas", f"{SCHEMAS_DIR}/glib-2.0/schemas"], check=True)
os.environ["XDG_DATA_DIRS"] = f'{SCHEMAS_DIR}:' + \
os.environ.get("XDG_DATA_DIRS", "")
print(SCHEMAS_DIR)
# python lib
sys.path.append(PYTHON_DIR)
def set_internationalization():
"""Sets application internationalization."""
try:
locale.bindtextdomain(APP_ID, LOCALE_DIR)
locale.textdomain(APP_ID)
except AttributeError as e:
from lfy.api.utils.debug import get_logger
get_logger().error(e)
print(f"Some gettext translations will not work. Error:\n{e}")
gettext.bindtextdomain(APP_ID, LOCALE_DIR)
gettext.textdomain(APP_ID)
def set_resources():
"""Sets application ressource file."""
from gi.repository import Gio # pylint: disable=c0415
# pylint: disable=w0212
Gio.Resource._register(Gio.resource_load(
os.path.join(PKGDATA_DIR, f'{APP_ID}.gresource')))
def run_application():
"""运行
Returns:
_type_: _description_
"""
from lfy.main import LfyApplication # pylint: disable=c0415
return LfyApplication(APP_ID, VERSION).run(sys.argv)
def main():
"""主入口
Returns:
_type_: _description_
"""
set_internationalization()
parser = argparse.ArgumentParser(description="translate")
parser.add_argument('-l', type=str, help='待翻译文字或待OCR文件')
parser.add_argument('-s', type=str, help='所用服务', default="")
parser.add_argument('-t', type=int, help='翻译后面的文字')
parser.add_argument('-o', type=str, help='识别图片')
args = parser.parse_args()
if args.t:
if len(args.s) > 0:
from lfy.code import req_text
print(req_text(args.l, args.s, args.t))
return
print(get_help_server(False))
return
if args.o:
if len(args.s) > 0:
from lfy.code import ocr_img
print(req_ocr(args.l, args.s, args.o))
return
print(get_help_server(True))
return
set_resources()
return sys.exit(run_application())
if __name__ == '__main__':
main()