Files
@ 3c3d26f172cf
Branch filter:
Location: rattail-project/rattail/rattail/config.py
3c3d26f172cf
3.6 KiB
text/x-python
Add `files.locking_copy_old()` function.
Something *did* appear to change regarding the behavior of the newer
`locking_copy()` function. Making the old one available again at least
temporarily, until the issue is settled.
Something *did* appear to change regarding the behavior of the newer
`locking_copy()` function. Making the old one available again at least
temporarily, until the issue is settled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | # -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2014 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail 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.
#
# Rattail 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 Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Configuration Utilities
"""
from __future__ import unicode_literals
import sys
import shlex
from edbob.configuration import AppConfigParser
def parse_list(value):
"""
Parse a configuration value, splitting by whitespace and/or commas and
taking quoting into account, etc., yielding a list of strings.
"""
if value is None:
return []
# Per the shlex docs (https://docs.python.org/2/library/shlex.html):
# "Prior to Python 2.7.3, this module did not support Unicode input."
if sys.version_info < (2, 7, 3) and isinstance(value, unicode): # pragma: no cover
value = value.encode(u'utf-8')
parser = shlex.shlex(value)
parser.whitespace += u','
parser.whitespace_split = True
values = list(parser)
for i, value in enumerate(values):
if value.startswith(u'"') and value.endswith(u'"'):
values[i] = value[1:-1]
return values
class RattailConfig(object):
"""
Rattail configuration object. Represents the sum total of configuration in
effect for a running app. This is essentially a thin wrapper over the
standard library's ``ConfigParser`` class(es); in fact any attribute not
found in this class will fall back to the parser.
.. warning::
This API should hardly be considered stable.
"""
def __init__(self, parser=None):
"""
Create a config object. You may optionally specify the config parser
instance; if none is provided then an instance of
``edbob.configuration.AppConfigParser`` will be created.
"""
if parser is None:
parser = AppConfigParser('rattail')
self.parser = parser
def __getattr__(self, name):
return getattr(self.parser, name)
def getint(self, section, option, default=None):
"""
Overriddes base class method to allow for a default.
"""
try:
val = self.parser.getint(section, option)
except TypeError:
return default
return val
def setdefault(self, section, option, value):
"""
Sets a default value for given section and option, if no value exists
there yet. The final value (whether pre-existing or newly-set) is
returned.
"""
exists = True # start by assuming value exists
if not self.parser.has_section(section):
self.parser.add_section(section)
exists = False
elif not self.parser.has_option(section, option):
exists = False
if not exists:
self.parser.set(section, option, value)
return self.parser.get(section, option)
|