custom config override file

This commit is contained in:
Simone Margaritelli 2019-10-03 14:45:39 +02:00
parent 5968030ed3
commit f6605640b3
4 changed files with 70 additions and 40 deletions

View File

@ -95,7 +95,7 @@ ui:
# IMPORTANT: The lifespan of an eINK display depends on the cumulative amount of refreshes. If you want to # IMPORTANT: The lifespan of an eINK display depends on the cumulative amount of refreshes. If you want to
# preserve your display over time, you should set this value to 0.0 so that the display will be refreshed only # preserve your display over time, you should set this value to 0.0 so that the display will be refreshed only
# if any of the important data fields changed (the uptime and blinking cursor won't trigger a refresh). # if any of the important data fields changed (the uptime and blinking cursor won't trigger a refresh).
fps: 0.3 fps: 0.0
display: display:
enabled: true enabled: true
rotation: 180 rotation: 180

View File

@ -1,11 +1,16 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os
import argparse import argparse
import yaml
import time import time
import traceback import traceback
import yaml
import core import core
import pwnagotchi, pwnagotchi.plugins as plugins import pwnagotchi
import pwnagotchi.utils as utils
import pwnagotchi.version as version
import pwnagotchi.plugins as plugins
from pwnagotchi.log import SessionParser from pwnagotchi.log import SessionParser
from pwnagotchi.voice import Voice from pwnagotchi.voice import Voice
@ -14,18 +19,20 @@ from pwnagotchi.ui.display import Display
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-C', '--config', action='store', dest='config', default='/root/pwnagotchi/config.yml') parser.add_argument('-C', '--config', action='store', dest='config', default='/root/pwnagotchi/config.yml',
help='Main configuration file.')
parser.add_argument('-U', '--user-config', action='store', dest='user_config', default='/root/pwnagotchi.yml',
help='If this file exists, configuration will be merged and this will override default values.')
parser.add_argument('--manual', dest="do_manual", action="store_true", default=False, help="Manual mode.") parser.add_argument('--manual', dest="do_manual", action="store_true", default=False, help="Manual mode.")
parser.add_argument('--clear', dest="do_clear", action="store_true", default=False, parser.add_argument('--clear', dest="do_clear", action="store_true", default=False,
help="Clear the ePaper display and exit.") help="Clear the ePaper display and exit.")
args = parser.parse_args() args = parser.parse_args()
config = utils.load_config(args)
if args.do_clear: if args.do_clear:
print("clearing the display ...") print("clearing the display ...")
with open(args.config, 'rt') as fp:
config = yaml.safe_load(fp)
cleardisplay = config['ui']['display']['type'] cleardisplay = config['ui']['display']['type']
if cleardisplay in ('inkyphat', 'inky'): if cleardisplay in ('inkyphat', 'inky'):
print("inky display") print("inky display")
@ -59,9 +66,6 @@ if args.do_clear:
print("unknown display type %s" % cleardisplay) print("unknown display type %s" % cleardisplay)
quit() quit()
with open(args.config, 'rt') as fp:
config = yaml.safe_load(fp)
plugins.load_from_path(plugins.default_path) plugins.load_from_path(plugins.default_path)
if 'plugins' in config['main'] and config['main']['plugins'] is not None: if 'plugins' in config['main'] and config['main']['plugins'] is not None:
plugins.load_from_path(config['main']['plugins']) plugins.load_from_path(config['main']['plugins'])
@ -71,7 +75,7 @@ plugins.on('loaded')
display = Display(config=config, state={'name': '%s>' % pwnagotchi.name()}) display = Display(config=config, state={'name': '%s>' % pwnagotchi.name()})
agent = Agent(view=display, config=config) agent = Agent(view=display, config=config)
core.log("%s@%s (v%s)" % (pwnagotchi.name(), agent._identity, pwnagotchi.version)) core.log("%s@%s (v%s)" % (pwnagotchi.name(), agent._identity, version.version))
# for key, value in config['personality'].items(): # for key, value in config['personality'].items():
# core.log(" %s: %s" % (key, value)) # core.log(" %s: %s" % (key, value))

View File

@ -1,7 +1,9 @@
import _thread import _thread
import core import core
import pwnagotchi, pwnagotchi.plugins as plugins import pwnagotchi
import pwnagotchi.version as version
import pwnagotchi.plugins as plugins
from pwnagotchi.mesh import get_identity from pwnagotchi.mesh import get_identity
@ -22,7 +24,7 @@ class AsyncAdvertiser(object):
self._advertiser = Advertiser( self._advertiser = Advertiser(
self._config['main']['iface'], self._config['main']['iface'],
pwnagotchi.name(), pwnagotchi.name(),
pwnagotchi.version, version.version,
self._identity, self._identity,
period=0.3, period=0.3,
data=self._config['personality']) data=self._config['personality'])

View File

@ -0,0 +1,24 @@
import yaml
import os
# https://stackoverflow.com/questions/823196/yaml-merge-in-python
def merge_config(user, default):
if isinstance(user, dict) and isinstance(default, dict):
for k, v in default.items():
if k not in user:
user[k] = v
else:
user[k] = merge_config(user[k], v)
return user
def load_config(args):
with open(args.config, 'rt') as fp:
config = yaml.safe_load(fp)
if os.path.exists(args.user_config):
with open(args.user_config, 'rt') as fp:
user_config = yaml.safe_load(fp)
config = merge_config(user_config, config)
return config