Using a config YAML file

This commit is contained in:
Fabio Manganiello 2017-11-03 01:56:27 +01:00
parent a509adb018
commit 091aa3223c
2 changed files with 38 additions and 8 deletions

4
config.yaml Normal file
View file

@ -0,0 +1,4 @@
pushbullet_token: o.EHMMnZneJdpNQv9FSFbyY2busin7floe
# device_id: <your_device_id> (default: hostname)
# debug: True (default: False)

View file

@ -9,18 +9,29 @@ import subprocess
import sys import sys
import time import time
import websocket import websocket
import yaml
from threading import Thread from threading import Thread
from getopt import getopt 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 <info@fabiomanganiello.com>' __author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
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): def on_open(ws):
@ -93,20 +104,35 @@ def on_push(ws, data):
def main(): def main():
global DEBUG DEBUG = False
config_file = curdir + os.sep + 'config.yaml'
optlist, args = getopt(sys.argv[1:], 'vh') optlist, args = getopt(sys.argv[1:], 'vh')
for opt, arg in optlist: for opt, arg in optlist:
if opt == '-c':
config_file = arg
if opt == '-v': if opt == '-v':
DEBUG = True DEBUG = True
elif opt == '-h': elif opt == '-h':
print(''' print('''
Usage: {} [-v] [-h] Usage: {} [-v] [-h] [-c <config_file>]
-v Enable debug mode -v Enable debug mode
-h Show this help -h Show this help
-c Path to the configuration file (default: ./config.yaml)
'''.format(sys.argv[0])) '''.format(sys.argv[0]))
return 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: if DEBUG:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
websocket.enableTrace(True) websocket.enableTrace(True)