From 091aa3223cd2aa3868b523c84e06de23625d0c1c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 3 Nov 2017 01:56:27 +0100 Subject: [PATCH] Using a config YAML file --- config.yaml | 4 ++++ notifier.py | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 config.yaml diff --git a/config.yaml b/config.yaml new file mode 100644 index 00000000..581db4de --- /dev/null +++ b/config.yaml @@ -0,0 +1,4 @@ +pushbullet_token: o.EHMMnZneJdpNQv9FSFbyY2busin7floe +# device_id: (default: hostname) +# debug: True (default: False) + diff --git a/notifier.py b/notifier.py index 8d9223c3..77c388bd 100644 --- a/notifier.py +++ b/notifier.py @@ -9,18 +9,29 @@ import subprocess import sys import time import websocket +import yaml from threading import Thread from getopt import getopt -lib_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep + 'lib' -sys.path.insert(0, lib_dir) - __author__ = 'Fabio Manganiello ' -API_KEY = 'o.EHMMnZneJdpNQv9FSFbyY2busin7floe' -DEVICE_ID = socket.gethostname() -DEBUG = False +#-----------# + +curdir = os.path.dirname(os.path.realpath(__file__)) +lib_dir = curdir + os.sep + 'lib' +sys.path.insert(0, lib_dir) + +config_file = curdir + os.sep + 'config.yaml' +config = {} +with open(config_file,'r') as f: + config = yaml.load(f) + +API_KEY = config['pushbullet_token'] +DEVICE_ID = config['device_id'] \ + if 'device_id' in config else socket.gethostname() + +DEBUG = config['debug'] if 'debug' in config else False def on_open(ws): @@ -93,20 +104,35 @@ def on_push(ws, data): def main(): - global DEBUG + DEBUG = False + config_file = curdir + os.sep + 'config.yaml' optlist, args = getopt(sys.argv[1:], 'vh') for opt, arg in optlist: + if opt == '-c': + config_file = arg if opt == '-v': DEBUG = True elif opt == '-h': print(''' -Usage: {} [-v] [-h] +Usage: {} [-v] [-h] [-c ] -v Enable debug mode -h Show this help + -c Path to the configuration file (default: ./config.yaml) '''.format(sys.argv[0])) return + config = {} + with open(config_file,'r') as f: + config = yaml.load(f) + + API_KEY = config['pushbullet_token'] + DEVICE_ID = config['device_id'] \ + if 'device_id' in config else socket.gethostname() + + if 'debug' in config: + DEBUG = config['debug'] + if DEBUG: logging.basicConfig(level=logging.DEBUG) websocket.enableTrace(True)