SafeEyes/safeeyes/__main__.py

136 lines
5.1 KiB
Python
Raw Normal View History

2017-03-23 01:07:51 +01:00
#!/usr/bin/env python3
2016-10-15 06:11:27 +02:00
# Safe Eyes is a utility to remind you to take break frequently
# to protect your eyes from eye strain.
# Copyright (C) 2016 Gobinath
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2017-10-07 15:10:31 +02:00
"""
Safe Eyes is a utility to remind you to take break frequently to protect your eyes from eye strain.
"""
import argparse
import gettext
import locale
import logging
2017-10-17 00:40:02 +02:00
import signal
2017-10-07 15:10:31 +02:00
import sys
2017-04-03 20:18:23 +02:00
2017-10-07 15:10:31 +02:00
import psutil
2020-03-18 13:33:11 +01:00
from safeeyes import utility
2017-10-07 15:10:31 +02:00
from safeeyes.model import Config
2020-03-18 13:33:11 +01:00
from safeeyes.safeeyes import SafeEyes
from safeeyes.safeeyes import SAFE_EYES_VERSION
2017-10-07 15:10:31 +02:00
from safeeyes.rpc import RPCClient
2020-03-18 13:33:11 +01:00
gettext.install('safeeyes', utility.LOCALE_PATH)
2017-10-07 15:10:31 +02:00
def __running():
"""
Check if SafeEyes is already running.
"""
process_count = 0
for proc in psutil.process_iter():
if not proc.cmdline:
continue
try:
# Check if safeeyes is in process arguments
if callable(proc.cmdline):
# Latest psutil has cmdline function
cmd_line = proc.cmdline()
else:
# In older versions cmdline was a list object
cmd_line = proc.cmdline
if ('python3' in cmd_line[0] or 'python' in cmd_line[0]) and ('safeeyes' in cmd_line[1] or 'safeeyes' in cmd_line):
process_count += 1
if process_count > 1:
return True
# Ignore if process does not exist or does not have command line args
except (IndexError, psutil.NoSuchProcess):
pass
return False
2016-10-15 06:11:27 +02:00
def main():
2017-10-07 15:10:31 +02:00
"""
Start the Safe Eyes.
"""
2020-03-18 13:33:11 +01:00
system_locale = gettext.translation('safeeyes', localedir=utility.LOCALE_PATH, languages=[utility.system_locale(), 'en_US'], fallback=True)
2017-10-07 15:10:31 +02:00
system_locale.install()
try:
# locale.bindtextdomain is required for Glade files
locale.bindtextdomain('safeeyes', utility.LOCALE_PATH)
except AttributeError:
logging.warning('installed python\'s gettext module does not support locale.bindtextdomain. locale.bindtextdomain is required for Glade files')
2017-10-07 15:10:31 +02:00
parser = argparse.ArgumentParser(prog='safeeyes', description=_('description'))
group = parser.add_mutually_exclusive_group()
group.add_argument('-a', '--about', help=_('show the about dialog'), action='store_true')
group.add_argument('-d', '--disable', help=_('disable the currently running safeeyes instance'), action='store_true')
group.add_argument('-e', '--enable', help=_('enable the currently running safeeyes instance'), action='store_true')
group.add_argument('-q', '--quit', help=_('quit the running safeeyes instance and exit'), action='store_true')
group.add_argument('-s', '--settings', help=_('show the settings dialog'), action='store_true')
group.add_argument('-t', '--take-break', help=_('Take a break now').lower(), action='store_true')
parser.add_argument('--debug', help=_('start safeeyes in debug mode'), action='store_true')
2018-01-29 02:16:02 +01:00
parser.add_argument('--status', help=_('print the status of running safeeyes instance and exit'), action='store_true')
2017-10-07 15:10:31 +02:00
parser.add_argument('--version', action='version', version='%(prog)s ' + SAFE_EYES_VERSION)
args = parser.parse_args()
# Initialize the logging
utility.initialize_logging(args.debug)
2020-12-26 01:28:38 +01:00
utility.initialize_platform()
2017-10-07 15:10:31 +02:00
config = Config()
if __running():
logging.info("Safe Eyes is already running")
2019-11-25 02:39:28 +01:00
if not config.get("use_rpc_server", True):
# RPC sever is disabled
2019-12-17 02:39:06 +01:00
print(_('Safe Eyes is running without an RPC server. Turn it on to use command-line arguments.'))
2019-11-25 02:39:28 +01:00
sys.exit(0)
return
2017-10-07 15:10:31 +02:00
rpc_client = RPCClient(config.get('rpc_port'))
if args.about:
rpc_client.show_about()
elif args.disable:
rpc_client.disable_safeeyes()
elif args.enable:
rpc_client.enable_safeeyes()
elif args.settings:
rpc_client.show_settings()
elif args.take_break:
rpc_client.take_break()
2018-01-29 02:16:02 +01:00
elif args.status:
print(rpc_client.status())
2017-10-07 15:10:31 +02:00
elif args.quit:
rpc_client.quit()
else:
# Default behavior is opening settings
rpc_client.show_settings()
sys.exit(0)
2018-01-29 02:16:02 +01:00
else:
if args.status:
print(_('Safe Eyes is not running'))
sys.exit(0)
elif not args.quit:
logging.info("Starting Safe Eyes")
safe_eyes = SafeEyes(system_locale, config, args)
2020-03-18 13:33:11 +01:00
safe_eyes.start()
2016-10-15 06:11:27 +02:00
if __name__ == '__main__':
2017-10-17 00:40:02 +02:00
signal.signal(signal.SIGINT, signal.SIG_DFL) # Handle Ctrl + C
2017-10-07 15:10:31 +02:00
main()