/usr/lib/python3/dist-packages/systemimage/bag.py is in system-image-common 2.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2013-2014 Canonical Ltd.
# Author: Barry Warsaw <barry@ubuntu.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""The Bag class."""
__all__ = [
'Bag',
]
import keyword
from collections import defaultdict
COMMASPACE = ', '
def default():
def identity(value):
return value
return identity
def make_converter(original):
converters = defaultdict(default)
if original is not None:
converters.update(original)
return converters
class Bag:
def __init__(self, *, converters=None, **kws):
self._converters = make_converter(converters)
self.__original__ = {}
self.__untranslated__ = {}
self._load_items(kws)
def update(self, *, converters=None, **kws):
if converters is not None:
self._converters.update(converters)
self._load_items(kws)
def _load_items(self, kws):
for key, value in kws.items():
self.__original__[key] = value
safe_key, converted_value = self._normalize_key_value(key, value)
self.__untranslated__[key] = converted_value
# BAW 2013-04-30: attribute values *must* be immutable, but for
# now we don't enforce this. If you set or delete attributes, you
# will probably break things.
self.__dict__[safe_key] = converted_value
def _normalize_key_value(self, key, value):
value = self._converters[key](value)
key = key.replace('-', '_')
if keyword.iskeyword(key):
key += '_'
return key, value
def __repr__(self):
return '<Bag: {}>'.format(COMMASPACE.join(sorted(
key for key in self.__dict__ if not key.startswith('_'))))
def __setitem__(self, key, value):
if key in self.__original__:
raise ValueError('Attributes are immutable: {}'.format(key))
safe_key, converted_value = self._normalize_key_value(key, value)
self.__dict__[safe_key] = converted_value
self.__untranslated__[key] = converted_value
def __getitem__(self, key):
return self.__untranslated__[key]
# Pickle protocol.
def __getstate__(self):
# We don't need to pickle the converters, because for all practical
# purposes, those are only used when the Bag is instantiated.
return (self.__original__,
{key: value for key, value in self.__dict__.items()
if not key.startswith('_')})
def __setstate__(self, state):
original, values = state
self.__original__ = original
self._converters = None
for key, value in values.items():
self.__dict__[key] = value
|