Add config file and argparse

- '~/.config/mach5/config.ini' will be default config file, can be
  changed in command line

- '~/.config/mach5/menu.yml' is the default menu file. Can be changed
  in command line parameters or in config file. Comman line parameters
  take preference.
master
Sergio Alvariño 3 years ago
parent fbdf2b6ec0
commit 7478bc2878

@ -5,4 +5,12 @@ A chords app for Linux
## TODO
- More checks on command contructor
- More checks on Command constructor
- Add checks on Menu constructor
- Add config
- Add logger
## Referencias
- [Autokey](https://github.com/autokey/)

49
mach5

@ -2,7 +2,50 @@
from machlib.menu import Menu, get_data_from
# import subprocess
# from getkey import getkey, keys
import argparse
from configparser import ConfigParser, ExtendedInterpolation
import os
import os.path
import errno
Menu(get_data_from('~/.config/mach5/menu.yml')).get_command().execute()
# default configuration and menu files
_default_configuration_file = '~/.config/mach5/config.ini'
# -------------------- MAIN --------------------
# Parse command line options
arg_parser = argparse.ArgumentParser(
description='Our simple chords application')
arg_parser.add_argument('-c', '--cfg', help='Config ini file')
arg_parser.add_argument('-m', '--menu', help='Menu yaml file')
args = arg_parser.parse_args()
# load config file
cfg_file = args.cfg if args.cfg else _default_configuration_file
cfg_file = os.path.expanduser(cfg_file)
if not os.path.exists(cfg_file):
raise FileNotFoundError(errno.ENOENT,
os.strerror(errno.ENOENT), cfg_file)
_configuration = ConfigParser(interpolation=ExtendedInterpolation())
found = _configuration.read(cfg_file)
# [TODO] next lines should raise an error
if (not found):
print('[ERROR] Config file not found')
raise FileNotFoundError
menu_file = args.menu if args.menu\
else _configuration['General']['default_menu_file']
menu_file = os.path.expanduser(menu_file)
if not os.path.exists(menu_file):
raise FileNotFoundError(errno.ENOENT,
os.strerror(errno.ENOENT), menu_file)
Menu(get_data_from(menu_file)).get_command().execute()

Loading…
Cancel
Save