Make configurable: server URL, user, password, debugging
This commit is contained in:
parent
e26563b946
commit
135fc1044c
|
@ -8,6 +8,7 @@ from __future__ import print_function
|
|||
import freitagslib.network as net
|
||||
from freitagslib.commands import BuyCommand, DepositCommand
|
||||
from freitagslib.encoding import asciify
|
||||
from freitagslib.settings import settings, Settings
|
||||
|
||||
import colorama
|
||||
from colorama import Fore, Style
|
||||
|
@ -519,6 +520,10 @@ def read_line(f, timeout, timeout_func):
|
|||
|
||||
def main():
|
||||
colorama.init()
|
||||
|
||||
if not settings.load("freitagskasse.conf"):
|
||||
sys.exit(1)
|
||||
|
||||
status = Status()
|
||||
global scroll_line1,scroll_line2
|
||||
global myDisplay
|
||||
|
|
|
@ -18,39 +18,43 @@ if __name__ == '__main__':
|
|||
sys.path.append('.')
|
||||
from freitagslib.item import Item
|
||||
from freitagslib.tools import require_type
|
||||
from freitagslib.settings import settings, Settings
|
||||
|
||||
|
||||
DEPOSIT_CASH = 1
|
||||
DEPOSIT_BANK = 2
|
||||
|
||||
|
||||
_BASE_URL = 'https://k4ever.freitagsrunde.org:443/api2/'
|
||||
|
||||
_auth = base64.encodestring('%s:%s' % (os.environ['BARCODE_PLUGIN_USER'], os.environ['BARCODE_PLUGIN_PASS'])).rstrip()
|
||||
_headers = {'Authorization':'Basic %s' % _auth}
|
||||
|
||||
ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT = range(3)
|
||||
|
||||
|
||||
if bool(os.environ.get('BARCODE_PLUGIN_DEBUG', False)):
|
||||
_h = urllib2.HTTPHandler(debuglevel=1)
|
||||
_opener = urllib2.build_opener(_h)
|
||||
_urlopen = _opener.open
|
||||
else:
|
||||
_urlopen = urllib2.urlopen
|
||||
def _urlopen(*args):
|
||||
if settings.CLIENT_DEBUG:
|
||||
_h = urllib2.HTTPHandler(debuglevel=1)
|
||||
_opener = urllib2.build_opener(_h)
|
||||
_func = _opener.open
|
||||
else:
|
||||
_func = urllib2.urlopen
|
||||
return _func(*args)
|
||||
|
||||
|
||||
def _get_default_headers():
|
||||
_auth = base64.encodestring('%s:%s' % (settings.SERVER_USER, settings.SERVER_PASSWORD)).rstrip()
|
||||
_headers = {'Authorization':'Basic %s' % _auth}
|
||||
return _headers
|
||||
|
||||
|
||||
def _request(api_rel_url, data, method, headers=None):
|
||||
url = _BASE_URL + api_rel_url
|
||||
url = settings.SERVER_URL + api_rel_url
|
||||
|
||||
if isinstance(data, dict):
|
||||
data = urllib.urlencode(data)
|
||||
|
||||
# Add default headers
|
||||
if headers is None:
|
||||
final_headers = _headers
|
||||
final_headers = _get_default_headers()
|
||||
else:
|
||||
final_headers = copy.copy(_headers)
|
||||
final_headers = copy.copy(_get_default_headers())
|
||||
final_headers.update(headers)
|
||||
|
||||
if method == 'GET':
|
||||
|
@ -118,7 +122,10 @@ def deposit(amount, transaction_type, user_name):
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
user_name = get_user_name_from_auth_blob(os.environ['BARCODE_PLUGIN_TEST_AUTH_BLOB'])
|
||||
if not settings.load('freitagskasse.conf', (('TEST_AUTH_BLOB', str, None), )):
|
||||
sys.exit(1)
|
||||
|
||||
user_name = get_user_name_from_auth_blob(settings.TEST_AUTH_BLOB)
|
||||
print('User name: ' + user_name)
|
||||
|
||||
balance = get_balance(user_name)
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
|
||||
# Licensed under GPL v3 or later
|
||||
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import ConfigParser
|
||||
|
||||
|
||||
_KEYS = (
|
||||
# Key, type, default
|
||||
('CLIENT_DEBUG', bool, False), # Should go first to enable debugging as early as possible
|
||||
('SERVER_URL', str, None),
|
||||
('SERVER_USER', str, None),
|
||||
('SERVER_PASSWORD', str, None),
|
||||
)
|
||||
|
||||
_DEFAULTS = dict([(key, str(default))
|
||||
for (key, _type, default)
|
||||
in _KEYS
|
||||
if default is not None])
|
||||
|
||||
|
||||
class Settings:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def load(self, filename, _additional_keys=None):
|
||||
additional_keys = () if _additional_keys is None else _additional_keys
|
||||
|
||||
cp = ConfigParser.ConfigParser(_DEFAULTS)
|
||||
if not cp.read(filename):
|
||||
print('FATAL: Config file %s could not be parsed.' % (filename), file=sys.stderr)
|
||||
return False
|
||||
|
||||
valid = True
|
||||
for key, _type, dummy in _KEYS + additional_keys:
|
||||
try:
|
||||
if _type is bool:
|
||||
value = cp.getboolean('client-barcode', key)
|
||||
else:
|
||||
value = cp.get('client-barcode', key)
|
||||
except ValueError:
|
||||
print('ERROR: Key %s in config file %s must be of type %s.' \
|
||||
% (key, filename, _type.__name__), file=sys.stderr)
|
||||
valid = False
|
||||
except ConfigParser.NoOptionError:
|
||||
print('ERROR: Config file %s misses required key %s.' \
|
||||
% (filename, key), file=sys.stderr)
|
||||
valid = False
|
||||
else:
|
||||
setattr(self, key, value)
|
||||
if self.CLIENT_DEBUG:
|
||||
print('DEBUG: CONFIG: %s=%s("%s")' % (key, type(value).__name__, value))
|
||||
|
||||
return valid
|
||||
|
||||
|
||||
settings = Settings()
|
Loading…
Reference in New Issue