import logging
import sys
+from . import exceptions
+from . import constants
from pgwui_common import plugin
# Constants
'literal_column_headings',
])
-# Required length of HMAC value
-HMAC_LEN = 40
-
# Logging
log = logging.getLogger(__name__)
-# Exceptions
-class Error(Exception):
- '''Base error class for module
- '''
- pass
-
-
-class AutoconfigureConflict(Error):
- def __init__(self):
- super().__init__(
- 'Autoconfigure is True and there is a pyramid.include setting')
-
-
-class BadSettingsAbort(Error):
- def __init__(self):
- super().__init__('Aborting due to bad setting(s)')
-
-
-class UnknownSettingKeyError(Error):
- def __init__(self, key):
- super().__init__('Unknown PGWUI setting: {}'.format(key))
-
-
-class MissingSettingError(Error):
- def __init__(self, key):
- super().__init__('Missing PGWUI setting: {}'.format(key))
-
-
-class NotBooleanSettingError(Error):
- def __init__(self, key, value):
- super().__init__(
- 'The "{}" PGWUI setting must be "True" or "False"'
- .format(key))
-
-
-class BadLiteralColumnHeadingsError(Error):
- def __init__(self, value):
- super().__init__(
- 'The "pgwui.literal_column_headings" PGWUI setting must be'
- '"on", "off", "ask", or not present')
-
-
-class BadHMACError(Error):
- pass
-
-
-class NoHMACError(BadHMACError):
- def __init__(self):
- super().__init__('Missing session.secret configuration')
-
-
-class HMACLengthError(BadHMACError):
- def __init__(self):
- super().__init__(
- 'The session.secret value is not {} characters in length'
- .format(HMAC_LEN))
-
-
# Functions
def abort_on_bad_setting(errors, key, component_keys):
if key[:6] == 'pgwui.':
if (key[6:] not in SETTINGS
and key not in component_keys):
- errors.append(UnknownSettingKeyError(key))
+ errors.append(exceptions.UnknownSettingKeyError(key))
def require_setting(errors, setting, settings):
if setting not in settings:
- errors.append(MissingSettingError(setting))
+ errors.append(exceptions.MissingSettingError(setting))
def boolean_setting(errors, setting, settings):
val = literal_eval(settings[setting])
if (val is not True
and val is not False):
- errors.append(NotBooleanSettingError(setting, settings[setting]))
+ errors.append(exceptions.NotBooleanSettingError(
+ setting, settings[setting]))
def validate_setting_values(errors, settings):
return
if 'session.secret' not in settings:
- errors.append(NoHMACError())
+ errors.append(exceptions.NoHMACError())
return
- if len(settings['session.secret']) != HMAC_LEN:
- errors.append(HMACLengthError())
+ if len(settings['session.secret']) != constants.HMAC_LEN:
+ errors.append(exceptions.HMACLengthError())
return
if value is None:
return
if value not in ('on', 'off', 'ask'):
- errors.append(BadLiteralColumnHeadingsError(value))
+ errors.append(exceptions.BadLiteralColumnHeadingsError(value))
def parse_assignments(lines):
'''Report errors and exit
'''
tagged = [(logging.ERROR, error) for error in errors]
- tagged.append((logging.CRITICAL, BadSettingsAbort()))
+ tagged.append((logging.CRITICAL, exceptions.BadSettingsAbort()))
for (level, error) in tagged:
log.log(level, error)
return []
if 'pyramid.include' in settings:
- log.info(AutoconfigureConflict())
+ log.info(exceptions.AutoconfigureConflict())
return components
--- /dev/null
+# Copyright (C) 2020 The Meme Factory, Inc. http://www.karlpinc.com/
+
+# This file is part of PGWUI_Server.
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public License
+# as published by the Free Software Foundation, either version 3 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program. If not, see
+# <http://www.gnu.org/licenses/>.
+#
+
+# Karl O. Pinc <kop@karlpinc.com>
+
+'''Constants shared by multiple modules
+'''
+
+
+# Required length of HMAC value
+HMAC_LEN = 40
--- /dev/null
+# Copyright (C) 2018, 2019 The Meme Factory, Inc. http://www.karlpinc.com/
+
+# This file is part of PGWUI_Server.
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public License
+# as published by the Free Software Foundation, either version 3 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program. If not, see
+# <http://www.gnu.org/licenses/>.
+#
+
+# Karl O. Pinc <kop@karlpinc.com>
+
+'''Exceptions for PGWUI_Server
+'''
+
+from . import constants
+
+
+class Error(Exception):
+ '''Base error class for module
+ '''
+ pass
+
+
+class AutoconfigureConflict(Error):
+ def __init__(self):
+ super().__init__(
+ 'Autoconfigure is True and there is a pyramid.include setting')
+
+
+class BadSettingsAbort(Error):
+ def __init__(self):
+ super().__init__('Aborting due to bad setting(s)')
+
+
+class UnknownSettingKeyError(Error):
+ def __init__(self, key):
+ super().__init__('Unknown PGWUI setting: {}'.format(key))
+
+
+class MissingSettingError(Error):
+ def __init__(self, key):
+ super().__init__('Missing PGWUI setting: {}'.format(key))
+
+
+class NotBooleanSettingError(Error):
+ def __init__(self, key, value):
+ super().__init__(
+ 'The "{}" PGWUI setting must be "True" or "False"'
+ .format(key))
+
+
+class BadLiteralColumnHeadingsError(Error):
+ def __init__(self, value):
+ super().__init__(
+ 'The "pgwui.literal_column_headings" PGWUI setting must be'
+ '"on", "off", "ask", or not present')
+
+
+class BadHMACError(Error):
+ pass
+
+
+class NoHMACError(BadHMACError):
+ def __init__(self):
+ super().__init__('Missing session.secret configuration')
+
+
+class HMACLengthError(BadHMACError):
+ def __init__(self):
+ super().__init__(
+ 'The session.secret value is not {} characters in length'
+ .format(constants.HMAC_LEN))
# Use as a regular module, not a plugin, so lint checks work
from pgwui_testing import testing
+import pgwui_server.constants as constants
+import pgwui_server.exceptions as ex
import pgwui_server.__init__ as pgwui_server_init
pgwui_server_init.abort_on_bad_setting(errors, 'pgwui.foo', [])
assert errors
- assert isinstance(errors[0], pgwui_server_init.UnknownSettingKeyError)
+ assert isinstance(errors[0], ex.UnknownSettingKeyError)
def test_abort_on_bad_setting_good():
pgwui_server_init.require_setting(errors, 'key', {})
assert errors
- assert isinstance(errors[0], pgwui_server_init.MissingSettingError)
+ assert isinstance(errors[0], ex.MissingSettingError)
def test_require_setting_present():
pgwui_server_init.boolean_setting(errors, 'key', {'key': '0'})
assert errors
- assert isinstance(errors[0], pgwui_server_init.NotBooleanSettingError)
+ assert isinstance(errors[0], ex.NotBooleanSettingError)
mock_boolean_setting = testing.make_mock_fixture(
mock_do_validate_hmac.return_value = True
errors = []
pgwui_server_init.validate_hmac(
- errors, {'session.secret': 'x' * pgwui_server_init.HMAC_LEN})
+ errors, {'session.secret': 'x' * constants.HMAC_LEN})
assert errors == []
pgwui_server_init.validate_hmac(errors, {})
assert errors
- assert isinstance(errors[0], pgwui_server_init.NoHMACError)
+ assert isinstance(errors[0], ex.NoHMACError)
def test_validate_hmac_length(mock_do_validate_hmac):
pgwui_server_init.validate_hmac(errors, {'session.secret': ''})
assert errors
- assert isinstance(errors[0], pgwui_server_init.HMACLengthError)
+ assert isinstance(errors[0], ex.HMACLengthError)
mock_validate_hmac = testing.make_mock_fixture(
assert errors
assert isinstance(
- errors[0], pgwui_server_init.BadLiteralColumnHeadingsError)
+ errors[0], ex.BadLiteralColumnHeadingsError)
mock_validate_literal_column_headings = testing.make_mock_fixture(
def test_unknownsettingkeyerror():
'''Takes an argument'''
- assert isinstance(pgwui_server_init.UnknownSettingKeyError('key'),
- pgwui_server_init.Error)
+ assert isinstance(ex.UnknownSettingKeyError('key'),
+ ex.Error)
def test_missingsettingerror():
'''Takes an argument'''
- assert isinstance(pgwui_server_init.MissingSettingError('key'),
- pgwui_server_init.Error)
+ assert isinstance(ex.MissingSettingError('key'),
+ ex.Error)
def test_notbooleansettingerror():
'''Takes two arguments'''
- assert isinstance(pgwui_server_init.NotBooleanSettingError('key', 'val'),
- pgwui_server_init.Error)
+ assert isinstance(ex.NotBooleanSettingError('key', 'val'),
+ ex.Error)