/usr/lib/python3/dist-packages/guidata/userconfigio.py is in python3-guidata 1.7.6-1.
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | # -*- coding: utf-8 -*-
#
# Copyright © 2012 CEA
# Pierre Raybaut
# Licensed under the terms of the CECILL License
# (see guidata/__init__.py for details)
"""
Reader and Writer for the serialization of DataSets into .ini files,
using the open-source `userconfig` Python package
UserConfig reader/writer objects
(see guidata.hdf5io for another example of reader/writer)
"""
import collections
import datetime
from guidata.py3compat import is_unicode, PY3
class GroupContext(object):
"""Group context object"""
def __init__(self, handler, group_name):
self.handler = handler
self.group_name = group_name
def __enter__(self):
self.handler.begin(self.group_name)
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
self.handler.end(self.group_name)
return False
class BaseIOHandler(object):
"""Base I/O Handler with group context manager
(see guidata.hdf5io for another example of this handler usage)"""
def __init__(self):
self.option = []
def group(self, group_name):
"""Enter a group. This returns a context manager, to be used with
the `with` statement"""
return GroupContext(self, group_name)
def begin(self, section):
self.option.append(section)
def end(self, section):
sect = self.option.pop(-1)
assert sect == section, "Error: %s != %s" % (sect, section)
class UserConfigIOHandler(BaseIOHandler):
def __init__(self, conf, section, option):
self.conf = conf
self.section = section
self.option = [option]
def begin(self, section):
self.option.append(section)
def end(self, section):
sect = self.option.pop(-1)
assert sect == section
def group(self, option):
"""Enter a HDF5 group. This returns a context manager, to be used with
the `with` statement"""
return GroupContext(self, option)
class WriterMixin(object):
def write(self, val, group_name=None):
"""Write value using the appropriate routine depending on value type
group_name: if None, writing the value in current group"""
import numpy as np
if group_name:
self.begin(group_name)
if isinstance(val, bool):
self.write_bool(val)
elif isinstance(val, int):
self.write_int(val)
elif isinstance(val, float):
self.write_float(val)
elif is_unicode(val):
self.write_unicode(val)
elif isinstance(val, str):
self.write_any(val)
elif isinstance(val, np.ndarray):
self.write_array(val)
elif np.isscalar(val):
self.write_any(val)
elif val is None:
self.write_none()
elif isinstance(val, (list, tuple)):
self.write_sequence(val)
elif isinstance(val, datetime.datetime):
self.write_float(val.timestamp())
elif isinstance(val, datetime.date):
self.write_int(val.toordinal())
elif hasattr(val, 'serialize') and isinstance(val.serialize,
collections.Callable):
# The object has a DataSet-like `serialize` method
val.serialize(self)
else:
raise NotImplementedError("cannot serialize %r of type %r" %
(val, type(val)))
if group_name:
self.end(group_name)
class UserConfigWriter(UserConfigIOHandler, WriterMixin):
def write_any(self, val):
option = "/".join(self.option)
self.conf.set(self.section, option, val)
write_bool = write_int = write_float = write_any
write_array = write_sequence = write_str = write_any
def write_unicode(self, val):
self.write_any(val.encode("utf-8"))
if PY3:
write_unicode = write_str
def write_none(self):
self.write_any(None)
class UserConfigReader(UserConfigIOHandler):
def read_any(self):
option = "/".join(self.option)
val = self.conf.get(self.section, option)
return val
read_bool = read_int = read_float = read_any
read_array = read_sequence = read_none = read_str = read_any
def read_unicode(self):
val = self.read_any()
if is_unicode(val) or val is None:
return val
else:
return self.read_str()
if PY3:
read_unicode = read_str
|