79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
import configparser
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
import infi.systray
|
|
|
|
import pkg_resources # needed for pyinstaller?
|
|
import valconomy
|
|
|
|
DATA_DIR = os.path.join(os.environ['LOCALAPPDATA'], 'Valconomy')
|
|
LOG_FILE = os.path.join(DATA_DIR, 'app.log')
|
|
CONFIG_FILE = os.path.join(DATA_DIR, 'config.ini')
|
|
|
|
log = logging.getLogger('valconomy-app')
|
|
|
|
def parse_log_level(level: str) -> int:
|
|
match level.lower():
|
|
case 'debug':
|
|
return logging.DEBUG
|
|
case 'info':
|
|
return logging.INFO
|
|
case 'warning':
|
|
return logging.WARNING
|
|
case 'error':
|
|
return logging.ERROR
|
|
case _:
|
|
return logging.INFO
|
|
|
|
def data_file(fname: str) -> str:
|
|
return os.path.abspath(os.path.join(os.path.dirname(__file__), fname))
|
|
|
|
def do_open_datadir(tray: infi.systray.SysTrayIcon):
|
|
os.startfile(DATA_DIR)
|
|
|
|
def main():
|
|
os.makedirs(DATA_DIR, exist_ok=True)
|
|
|
|
if sys.stderr is None:
|
|
# running in console-less mode, redirect to log file
|
|
sys.stderr = open(LOG_FILE, 'a')
|
|
|
|
conf = configparser.ConfigParser()
|
|
conf.read_dict({
|
|
'general': {'log_level': 'info'},
|
|
'valorant': {'player_uuid': ''},
|
|
})
|
|
conf.read(CONFIG_FILE)
|
|
with open(CONFIG_FILE, 'w') as f:
|
|
conf.write(f)
|
|
|
|
log_level = parse_log_level(conf['general']['log_level'])
|
|
logging.basicConfig(
|
|
format='%(asctime)s %(name)s %(levelname)s %(message)s', level=log_level,
|
|
stream=sys.stderr)
|
|
log.info('Starting up')
|
|
|
|
if not conf['valorant']['player_uuid']:
|
|
log.error(f'No player UUID set, exiting...')
|
|
sys.exit(1)
|
|
|
|
val_handler = valconomy.ValconomyHandler()
|
|
val_sm = valconomy.ValconomyStateMachine(conf['valorant']['player_uuid'], val_handler)
|
|
|
|
val_client = valconomy.ValorantLocalClient(val_sm.handle_presence)
|
|
def do_quit(tray: infi.systray.SysTrayIcon):
|
|
log.info('Shutting down')
|
|
val_client.stop()
|
|
|
|
with infi.systray.SysTrayIcon(
|
|
data_file('icon.ico'), 'Valconomy', on_quit=do_quit,
|
|
menu_options=(
|
|
('Open data directory', None, do_open_datadir),
|
|
)) as tray:
|
|
val_client.run()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|