Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Groszer committed Aug 30, 2012
1 parent fa48574 commit cf305f8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
40 changes: 22 additions & 18 deletions src/cipher/configstore/configstore.py
Expand Up @@ -34,11 +34,13 @@

novalue = object()


def stringify(s):
if s is None:
return ''
return unicode(s)


def parse_time(s):
return dateutil.parser.parse(s).time()

Expand Down Expand Up @@ -67,8 +69,8 @@ def load_type_Time(self, unicode, field):
def load_type_Timedelta(self, unicode, field):
if not unicode:
return
h, m , s = [int(p) for p in unicode.strip().split(':')]
return datetime.timedelta(seconds=h*3600+m*60+s)
h, m, s = [int(p) for p in unicode.strip().split(':')]
return datetime.timedelta(seconds=h * 3600 + m * 60 + s)

def load_type_Text(self, value, field):
return value.replace('\n<BLANKLINE>\n', '\n\n')
Expand Down Expand Up @@ -97,10 +99,10 @@ def _load_field(self, name, field, config, context=None):
# skip read-only fields, especially __name__
return False
field_type = field.__class__
if hasattr(self, 'load_'+name):
converter = getattr(self, 'load_'+name, None)
elif hasattr(self, 'load_type_'+field_type.__name__):
converter = getattr(self, 'load_type_'+field_type.__name__, None)
if hasattr(self, 'load_' + name):
converter = getattr(self, 'load_' + name, None)
elif hasattr(self, 'load_type_' + field_type.__name__):
converter = getattr(self, 'load_type_' + field_type.__name__, None)
converter = lambda v, converter=converter: converter(v, field)
elif zope.schema.interfaces.IFromUnicode.providedBy(field):
converter = field.bind(context).fromUnicode
Expand Down Expand Up @@ -179,10 +181,10 @@ def _dump_field(self, name, field, config, value=novalue):
# skip read-only fields, we won't be loading them
return
field_type = field.__class__
if hasattr(self, 'dump_'+name):
converter = getattr(self, 'dump_'+name)
elif hasattr(self, 'dump_type_'+field_type.__name__):
converter = getattr(self, 'dump_type_'+field_type.__name__)
if hasattr(self, 'dump_' + name):
converter = getattr(self, 'dump_' + name)
elif hasattr(self, 'dump_type_' + field_type.__name__):
converter = getattr(self, 'dump_type_' + field_type.__name__)
converter = lambda v, converter=converter: converter(v, field)
elif zope.schema.interfaces.IFromUnicode.providedBy(field):
converter = stringify
Expand All @@ -205,7 +207,7 @@ def _dump(self, config):
def dump(self, config=None):
if config is None:
config = ConfigParser.RawConfigParser(dict_type=odict.odict)
config.optionxform = str # don't lowercase
config.optionxform = str # don't lowercase
# Write object's configuration.
self._dump(config)
# Write any sub-object configuration.
Expand All @@ -216,6 +218,7 @@ def dump(self, config=None):
store.dump(config)
return config


class CollectionConfigurationStore(ConfigurationStore):
section_prefix = None
item_factory = None
Expand Down Expand Up @@ -263,15 +266,15 @@ def load(self, config):
# Note sends a ContainerModifiedEvent which would causes a config
# dump, overwriting the original file with partial information.
# That's why we have this _v_load_in_progress "lock".
self._add(name, item)
self._add(name, item)
self._applyPostAddConfig(item, config, section)
zope.event.notify(
interfaces.ObjectConfigurationLoadedEvent(self.context))

def dump(self, config=None):
if config is None:
config = ConfigParser.RawConfigParser(dict_type=odict.odict)
config.optionxform = str # don't lowercase
config.optionxform = str # don't lowercase
for name, item in self._getItems():
if getattr(item, '__parent__', self) is None:
# Sad story: you remove an item from a BTreeContainer
Expand All @@ -288,9 +291,10 @@ def dump(self, config=None):
item_store.dump(config)
return config


def createConfigurationStore(schema, section=None):
return type(
schema.getName()[1:]+'Store', (ConfigurationStore,),
schema.getName()[1:] + 'Store', (ConfigurationStore,),
{'section': section, 'schema': schema})


Expand All @@ -303,7 +307,7 @@ def get_site(self):
return None

def get_filename(self):
return self.context.__name__+'.ini'
return self.context.__name__ + '.ini'

def _load_from_external(self, config):
# Load general attributes.
Expand All @@ -328,7 +332,7 @@ def load(self, config):
self.get_config_dir(), config.get(self.section, 'config-path'))
__traceback_info__ = fn
ext_config = ConfigParser.RawConfigParser(dict_type=odict.odict)
ext_config.optionxform = str # don't lowercase
ext_config.optionxform = str # don't lowercase
ext_config.read(fn)
self._load_from_external(ext_config)

Expand All @@ -339,7 +343,7 @@ def _dump_to_external(self):
self.get_config_dir(), cs.__name__, self.get_filename())
# Create a new configuration for the table-specific API.
config = ConfigParser.RawConfigParser(dict_type=odict.odict)
config.optionxform = str # don't lowercase
config.optionxform = str # don't lowercase
# Write object's configuration.
orig_section = self.section
self.section = 'general'
Expand All @@ -360,7 +364,7 @@ def _dump_to_external(self):
def dump(self, config=None):
if config is None:
config = ConfigParser.RawConfigParser(dict_type=odict.odict)
config.optionxform = str # don't lowercase
config.optionxform = str # don't lowercase
# Write any sub-object configuration into a separate configuration file.
fn = self._dump_to_external()
# Just a small stub in the main config.
Expand Down
5 changes: 3 additions & 2 deletions src/cipher/configstore/interfaces.py
Expand Up @@ -32,8 +32,8 @@ class IConfigurationStore(zope.interface.Interface):
schema = zope.interface.Attribute('The schema to be serialized.')

section = zope.schema.ASCIILine(
title = u'Section Name',
description = u'The name of the section in the configuration.',
title=u'Section Name',
description=u'The name of the section in the configuration.',
required=True)

def load(config):
Expand All @@ -45,6 +45,7 @@ def dump(config=None):
If the `config` paramter is `None`, a configuration object is created.
"""


class ICipherObject(zope.interface.Interface):
"mark all Cipher objects"

Expand Down

0 comments on commit cf305f8

Please sign in to comment.