|
20 | 20 | your eyes from eye strain. |
21 | 21 | """ |
22 | 22 |
|
23 | | -import argparse |
24 | | -import logging |
25 | 23 | import signal |
26 | 24 | import sys |
27 | 25 |
|
28 | | -import psutil |
29 | | -from safeeyes import utility, translations |
30 | | -from safeeyes.translations import translate as _ |
| 26 | +from safeeyes import translations |
31 | 27 | from safeeyes.model import Config |
32 | 28 | from safeeyes.safeeyes import SafeEyes |
33 | | -from safeeyes.safeeyes import SAFE_EYES_VERSION |
34 | | -from safeeyes.rpc import RPCClient |
35 | | - |
36 | | - |
37 | | -def __running(): |
38 | | - """Check if SafeEyes is already running.""" |
39 | | - process_count = 0 |
40 | | - current_user = psutil.Process().username() |
41 | | - for proc in psutil.process_iter(): |
42 | | - if not proc.cmdline: |
43 | | - continue |
44 | | - try: |
45 | | - # Check if safeeyes is in process arguments |
46 | | - if callable(proc.cmdline): |
47 | | - # Latest psutil has cmdline function |
48 | | - cmd_line = proc.cmdline() |
49 | | - else: |
50 | | - # In older versions cmdline was a list object |
51 | | - cmd_line = proc.cmdline |
52 | | - if ("python3" in cmd_line[0] or "python" in cmd_line[0]) and ( |
53 | | - "safeeyes" in cmd_line[1] or "safeeyes" in cmd_line |
54 | | - ): |
55 | | - if proc.username() == current_user: |
56 | | - process_count += 1 |
57 | | - if process_count > 1: |
58 | | - return True |
59 | | - |
60 | | - # Ignore if process does not exist or does not have command line args |
61 | | - except (IndexError, psutil.NoSuchProcess): |
62 | | - pass |
63 | | - return False |
64 | 29 |
|
65 | 30 |
|
66 | 31 | def main(): |
67 | 32 | """Start the Safe Eyes.""" |
68 | 33 | system_locale = translations.setup() |
69 | 34 |
|
70 | | - parser = argparse.ArgumentParser(prog="safeeyes") |
71 | | - group = parser.add_mutually_exclusive_group() |
72 | | - group.add_argument( |
73 | | - "-a", "--about", help=_("show the about dialog"), action="store_true" |
74 | | - ) |
75 | | - group.add_argument( |
76 | | - "-d", |
77 | | - "--disable", |
78 | | - help=_("disable the currently running safeeyes instance"), |
79 | | - action="store_true", |
80 | | - ) |
81 | | - group.add_argument( |
82 | | - "-e", |
83 | | - "--enable", |
84 | | - help=_("enable the currently running safeeyes instance"), |
85 | | - action="store_true", |
86 | | - ) |
87 | | - group.add_argument( |
88 | | - "-q", |
89 | | - "--quit", |
90 | | - help=_("quit the running safeeyes instance and exit"), |
91 | | - action="store_true", |
92 | | - ) |
93 | | - group.add_argument( |
94 | | - "-s", "--settings", help=_("show the settings dialog"), action="store_true" |
95 | | - ) |
96 | | - group.add_argument( |
97 | | - "-t", "--take-break", help=_("Take a break now").lower(), action="store_true" |
98 | | - ) |
99 | | - parser.add_argument( |
100 | | - "--debug", help=_("start safeeyes in debug mode"), action="store_true" |
101 | | - ) |
102 | | - parser.add_argument( |
103 | | - "--status", |
104 | | - help=_("print the status of running safeeyes instance and exit"), |
105 | | - action="store_true", |
106 | | - ) |
107 | | - parser.add_argument( |
108 | | - "--version", action="version", version="%(prog)s " + SAFE_EYES_VERSION |
109 | | - ) |
110 | | - args = parser.parse_args() |
111 | | - |
112 | | - # Initialize the logging |
113 | | - utility.initialize_logging(args.debug) |
114 | | - utility.initialize_platform() |
115 | 35 | config = Config.load() |
116 | | - utility.cleanup_old_user_stylesheet() |
117 | 36 |
|
118 | | - if __running(): |
119 | | - logging.info("Safe Eyes is already running") |
120 | | - if not config.get("use_rpc_server", True): |
121 | | - # RPC sever is disabled |
122 | | - print( |
123 | | - _( |
124 | | - "Safe Eyes is running without an RPC server. Turn it on to use" |
125 | | - " command-line arguments." |
126 | | - ) |
127 | | - ) |
128 | | - sys.exit(0) |
129 | | - return |
130 | | - rpc_client = RPCClient(config.get("rpc_port")) |
131 | | - if args.about: |
132 | | - rpc_client.show_about() |
133 | | - elif args.disable: |
134 | | - rpc_client.disable_safeeyes() |
135 | | - elif args.enable: |
136 | | - rpc_client.enable_safeeyes() |
137 | | - elif args.settings: |
138 | | - rpc_client.show_settings() |
139 | | - elif args.take_break: |
140 | | - rpc_client.take_break() |
141 | | - elif args.status: |
142 | | - print(rpc_client.status()) |
143 | | - elif args.quit: |
144 | | - rpc_client.quit() |
145 | | - else: |
146 | | - # Default behavior is opening settings |
147 | | - rpc_client.show_settings() |
148 | | - sys.exit(0) |
149 | | - else: |
150 | | - if args.status: |
151 | | - print(_("Safe Eyes is not running")) |
152 | | - sys.exit(0) |
153 | | - elif not args.quit: |
154 | | - logging.info("Starting Safe Eyes") |
155 | | - safe_eyes = SafeEyes(system_locale, config, args) |
156 | | - safe_eyes.start() |
| 37 | + safe_eyes = SafeEyes(system_locale, config) |
| 38 | + safe_eyes.run(sys.argv) |
157 | 39 |
|
158 | 40 |
|
159 | 41 | if __name__ == "__main__": |
|
0 commit comments