From 261f6be7e108a809d76e949f67f399b7226781ce Mon Sep 17 00:00:00 2001 From: Ruud Date: Wed, 23 Feb 2011 14:47:21 +0100 Subject: [PATCH] Setting global --- couchpotato/__init__.py | 6 ++++-- couchpotato/api/__init__.py | 3 +-- couchpotato/cli.py | 13 ++++++++----- couchpotato/core/auth.py | 4 ++-- couchpotato/environment.py | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/couchpotato/__init__.py b/couchpotato/__init__.py index cb8677b..512b71c 100644 --- a/couchpotato/__init__.py +++ b/couchpotato/__init__.py @@ -16,12 +16,14 @@ app = Flask(__name__) log = CPLog(__name__) web = Module(__name__, 'web') + def get_session(engine): engine = engine if engine else get_engine() - return scoped_session(sessionmaker(autoflush = True, transactional = True, bind = engine)) + return scoped_session(sessionmaker(transactional = True, bind = engine)) def get_engine(): - return create_engine('sqlite:///' + Env.get('db_path'), echo = Env.get('debug')) + return create_engine('sqlite:///' + Env.get('db_path'), echo = False) + @web.route('/') @requires_auth diff --git a/couchpotato/api/__init__.py b/couchpotato/api/__init__.py index 1f90ec4..2b0639a 100644 --- a/couchpotato/api/__init__.py +++ b/couchpotato/api/__init__.py @@ -28,8 +28,7 @@ def setting_save_view(): option = a.get('name') value = a.get('value') - Env.get('settings').set(section, option, value) - Env.get('settings').save() + Env.setting(option, section, value).save() return jsonify({ 'success': True, diff --git a/couchpotato/cli.py b/couchpotato/cli.py index 7e10814..80dc17d 100644 --- a/couchpotato/cli.py +++ b/couchpotato/cli.py @@ -48,9 +48,12 @@ def cmd_couchpotato(base_path, args): Env.set('app_dir', base_path) Env.set('data_dir', options.data_dir) Env.set('db_path', os.path.join(options.data_dir, 'couchpotato.db')) + Env.set('quiet', options.quiet) + Env.set('daemonize', options.daemonize) + Env.set('args', args) # Determine debug - debug = options.debug or Env.get('settings').get('debug', default = False) + debug = options.debug or Env.setting('debug', default = False) Env.set('debug', debug) @@ -96,13 +99,13 @@ def cmd_couchpotato(base_path, args): # Create app from couchpotato import app - api_key = Env.get('settings').get('api_key') - url_base = '/' + Env.get('settings').get('url_base') if Env.get('settings').get('url_base') else '' + api_key = Env.setting('api_key') + url_base = '/' + Env.setting('url_base') if Env.setting('url_base') else '' reloader = debug and not options.daemonize # Basic config - app.host = Env.get('settings').get('host', default = '0.0.0.0') - app.port = Env.get('settings').get('port', default = 5000) + app.host = Env.setting('host', default = '0.0.0.0') + app.port = Env.setting('port', default = 5000) app.debug = debug app.secret_key = api_key app.static_path = url_base + '/static' diff --git a/couchpotato/core/auth.py b/couchpotato/core/auth.py index d6b2f8a..de0979a 100644 --- a/couchpotato/core/auth.py +++ b/couchpotato/core/auth.py @@ -3,7 +3,7 @@ from flask import request, Response from functools import wraps def check_auth(username, password): - return username == Env.get('settings').get('username') and password == Env.get('settings').get('password') + return username == Env.setting('username') and password == Env.setting('password') def authenticate(): return Response( @@ -16,7 +16,7 @@ def requires_auth(f): @wraps(f) def decorated(*args, **kwargs): auth = request.authorization - if Env.get('settings').get('username') and (not auth or not check_auth(auth.username, auth.password)): + if Env.setting('username') and (not auth or not check_auth(auth.username, auth.password)): return authenticate() return f(*args, **kwargs) diff --git a/couchpotato/environment.py b/couchpotato/environment.py index 3c45973..1d1138f 100644 --- a/couchpotato/environment.py +++ b/couchpotato/environment.py @@ -1,12 +1,16 @@ from couchpotato.core.settings import Settings class Env: + + ''' Environment variables ''' _debug = False _settings = Settings() _options = None _args = None _quiet = False + _deamonize = False + ''' Data paths and directories ''' _app_dir = "" _data_dir = "" _db_path = "" @@ -22,3 +26,15 @@ class Env: @staticmethod def set(attr, value): return setattr(Env, '_' + attr, value) + + @staticmethod + def setting(attr, section = 'global', value = None, default = ''): + + # Return setting + if value == None: + return Env.get('settings').get(attr, default = default, section = section) + + # Set setting + s = Env.get('settings') + s.set(section, attr, value) + return s