configglue team mailing list archive
-
configglue team
-
Mailing list archive
-
Message #00098
[Merge] lp:~ricardokirkner/configglue/refactor-optons-section into lp:configglue
Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/refactor-optons-section into lp:configglue with lp:~ricardokirkner/configglue/refactor-options-option as a prerequisite.
Requested reviews:
Configglue developers (configglue)
For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/refactor-optons-section/+merge/64443
--
https://code.launchpad.net/~ricardokirkner/configglue/refactor-optons-section/+merge/64443
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/refactor-optons-section into lp:configglue.
=== modified file 'configglue/inischema/glue.py'
--- configglue/inischema/glue.py 2011-06-13 18:51:30 +0000
+++ configglue/inischema/glue.py 2011-06-13 18:51:30 +0000
@@ -28,7 +28,7 @@
from configglue.pyschema.parser import SchemaConfigParser
from configglue.pyschema.schema import (
BoolOption,
- ConfigSection,
+ Section,
IntOption,
ListOption,
Schema,
@@ -70,7 +70,7 @@
if section_name == '__main__':
section = MySchema
else:
- section = ConfigSection(name=section_name)
+ section = Section(name=section_name)
setattr(MySchema, section_name, section)
for option_name in p.options(section_name):
option = p.get(section_name, option_name)
=== modified file 'configglue/pyschema/parser.py'
--- configglue/pyschema/parser.py 2011-06-13 18:51:30 +0000
+++ configglue/pyschema/parser.py 2011-06-13 18:51:30 +0000
@@ -204,7 +204,7 @@
If section is specified, return all options from that section only.
Section is to be specified *by name*, not by
- passing in real ConfigSection objects.
+ passing in real Section objects.
"""
values = collections.defaultdict(dict)
=== modified file 'configglue/pyschema/schema.py'
--- configglue/pyschema/schema.py 2011-06-13 18:51:30 +0000
+++ configglue/pyschema/schema.py 2011-06-13 18:51:30 +0000
@@ -26,6 +26,7 @@
'ConfigOption',
'Option',
'ConfigSection',
+ 'Section',
'DictConfigOption',
'DictOption',
'IntConfigOption',
@@ -47,9 +48,9 @@
def get_config_objects(obj):
objects = []
for name, obj in getmembers(obj):
- if isinstance(obj, (ConfigSection, Option)):
+ if isinstance(obj, (Section, Option)):
objects.append((name, obj))
- elif type(obj) == type and issubclass(obj, ConfigSection):
+ elif type(obj) == type and issubclass(obj, Section):
instance = obj()
for key, value in get_config_objects(obj):
setattr(instance, key, value)
@@ -62,12 +63,12 @@
To define your own configuration schema you should:
1- Inherit from Schema
- 2- Add Option and ConfigSections as class attributes.
+ 2- Add Option and Sections as class attributes.
With that your whole configuration schema is defined, and you can now
load configuration files.
- Options that don't go in a ConfigSection will belong in the
+ Options that don't go in a Section will belong in the
'__main__' section of the configuration files.
One Option comes already defined in Schema, 'includes' in the
@@ -86,7 +87,7 @@
def _add_item(self, name, item):
"""Add a top-level item to the schema."""
item.name = name
- if isinstance(item, ConfigSection):
+ if isinstance(item, Section):
self._add_section(name, item)
elif isinstance(item, Option):
self._add_option(name, item)
@@ -104,7 +105,7 @@
def _add_option(self, name, option):
"""Add a top-level option to the schema."""
section = self._sections.setdefault('__main__',
- ConfigSection(name='__main__'))
+ Section(name='__main__'))
option.section = section
setattr(section, name, option)
@@ -118,23 +119,23 @@
def is_valid(self):
"""Return whether the schema has a valid structure."""
explicit_default_section = isinstance(getattr(self, '__main__', None),
- ConfigSection)
+ Section)
is_valid = not explicit_default_section
return is_valid
def has_section(self, name):
"""Return whether the schema as a given section."""
- """Return True if a ConfigSection with the given name is available"""
+ """Return True if a Section with the given name is available"""
return name in self._sections.keys()
def section(self, name):
- """Return a ConfigSection by name"""
+ """Return a Section by name"""
section = self._sections.get(name)
- assert section is not None, "Invalid ConfigSection name '%s'" % name
+ assert section is not None, "Invalid Section name '%s'" % name
return section
def sections(self):
- """Returns the list of available ConfigSections"""
+ """Returns the list of available Sections"""
return self._sections.values()
def options(self, section=None):
@@ -160,12 +161,12 @@
return options
-class ConfigSection(object):
+class Section(object):
"""A group of options.
This class is just a bag you can dump Options in.
- After instantiating the Schema, each ConfigSection will know its own
+ After instantiating the Schema, each Section will know its own
name.
"""
@@ -184,7 +185,7 @@
name = " %s" % self.name
else:
name = ''
- value = "<ConfigSection%s>" % name
+ value = "<{0}{1}>".format(self.__class__.__name__, name)
return value
def has_option(self, name):
@@ -588,7 +589,8 @@
# deprecated
#
-class DeprecatedOption(type):
+
+class Deprecated(type):
def __init__(cls, name, bases, attrs):
warn('{0} is deprecated; use {1} instead.'.format(
name, bases[0].__name__), DeprecationWarning)
@@ -596,28 +598,32 @@
class StringConfigOption(StringOption):
- __metaclass__ = DeprecatedOption
+ __metaclass__ = Deprecated
class IntConfigOption(IntOption):
- __metaclass__ = DeprecatedOption
+ __metaclass__ = Deprecated
class BoolConfigOption(BoolOption):
- __metaclass__ = DeprecatedOption
+ __metaclass__ = Deprecated
class DictConfigOption(DictOption):
- __metaclass__ = DeprecatedOption
+ __metaclass__ = Deprecated
class LinesConfigOption(ListOption):
- __metaclass__ = DeprecatedOption
+ __metaclass__ = Deprecated
class TupleConfigOption(TupleOption):
- __metaclass__ = DeprecatedOption
+ __metaclass__ = Deprecated
class ConfigOption(Option):
- __metaclass__ = DeprecatedOption
+ __metaclass__ = Deprecated
+
+
+class ConfigSection(Section):
+ __metaclass__ = Deprecated
=== modified file 'doc/ref/schemas/options.rst'
--- doc/ref/schemas/options.rst 2011-06-13 18:51:30 +0000
+++ doc/ref/schemas/options.rst 2011-06-13 18:51:30 +0000
@@ -83,7 +83,7 @@
.. attribute:: Option.section
-The :class:`~configglue.pyschema.ConfigSection` object where this option was
+The :class:`~configglue.pyschema.Section` object where this option was
defined.
Default is ``None``.
=== modified file 'doc/topics/config-file.rst'
--- doc/topics/config-file.rst 2011-06-13 18:51:30 +0000
+++ doc/topics/config-file.rst 2011-06-13 18:51:30 +0000
@@ -6,7 +6,7 @@
the configuration, in the same format supported by ConfigParser. Within a
configuration file, there a few special syntax constructs you should be aware of.
-A ConfigSection is matched by a ConfigParser section, which is defined like::
+A Section is matched by a ConfigParser section, which is defined like::
[my_section]
@@ -23,7 +23,7 @@
This configuration file would match with a schema like the following::
class MySchema(pyschema.Schema):
- class my_section(pyschema.ConfigSection):
+ class my_section(pyschema.Section):
my_option = pyschema.StringOption()
======================
=== modified file 'doc/topics/schemas.rst'
--- doc/topics/schemas.rst 2011-06-13 18:51:30 +0000
+++ doc/topics/schemas.rst 2011-06-13 18:51:30 +0000
@@ -14,7 +14,7 @@
:class:`~configglue.pyschema.schema.Schema`.
* Each attribute of the schema represents either a configuration section
- (see :class:`~configglue.pyschema.schema.ConfigSection`) or
+ (see :class:`~configglue.pyschema.schema.Section`) or
option (see :class:`~configglue.pyschema.schema.Option`).
Quick example
@@ -134,8 +134,8 @@
its attributes from the base classes.
This poses a slight problem for attributes of type
-:class:`~configglue.pyschema.schema.ConfigSection`. Usually, you'll want to
-extend a :class:`~configglue.pyschema.schema.ConfigSection` instead of
+:class:`~configglue.pyschema.schema.Section`. Usually, you'll want to
+extend a :class:`~configglue.pyschema.schema.Section` instead of
overriding it. In order to achieve this, in your schema subclass, copy the
parent's attribute explicitely, to avoid modifying the parent schema class.
Option attributes (derived from
@@ -151,7 +151,7 @@
class BaseSchema(pyschema.Schema):
option1 = pyschema.IntOption()
- class section1(pyschema.ConfigSection):
+ class section1(pyschema.Section):
option1 = pyschema.BoolOption()
@@ -172,7 +172,7 @@
class ChildSchema(pyschema.Schema):
option1 = pyschema.IntOption()
option2 = pyschema.IntOption()
- class section1(pyschema.ConfigSection):
+ class section1(pyschema.Section):
option1 = pyschema.BoolOption()
option2 = IntOption()
=== modified file 'tests/pyschema/test_parser.py'
--- tests/pyschema/test_parser.py 2011-06-13 18:51:30 +0000
+++ tests/pyschema/test_parser.py 2011-06-13 18:51:30 +0000
@@ -43,7 +43,7 @@
)
from configglue.pyschema.schema import (
BoolOption,
- ConfigSection,
+ Section,
DictOption,
IntOption,
ListOption,
@@ -227,10 +227,10 @@
def test_interpolate_across_sections(self):
class MySchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
- class baz(ConfigSection):
+ class baz(Section):
wham = IntOption()
config = StringIO("[foo]\nbar=%(wham)s\n[baz]\nwham=42")
@@ -241,10 +241,10 @@
def test_interpolate_invalid_key(self):
class MySchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
- class baz(ConfigSection):
+ class baz(Section):
wham = IntOption()
config = StringIO("[foo]\nbar=%(wham)s\n[baz]\nwham=42")
@@ -423,7 +423,7 @@
def test_init_invalid_schema(self):
class MyInvalidSchema(Schema):
- class __main__(ConfigSection):
+ class __main__(Section):
pass
self.assertRaises(SchemaValidationError, SchemaConfigParser,
@@ -453,7 +453,7 @@
class MySchema(Schema):
foo = StringOption()
- class baz(ConfigSection):
+ class baz(Section):
bar = StringOption()
parser = SchemaConfigParser(MySchema())
@@ -484,10 +484,10 @@
def test_values_many_sections_same_option(self):
class MySchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
- class baz(ConfigSection):
+ class baz(Section):
bar = IntOption()
config = StringIO("[foo]\nbar=3\n[baz]\nbar=4")
@@ -501,10 +501,10 @@
def test_values_many_sections_different_options(self):
class MySchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
- class bar(ConfigSection):
+ class bar(Section):
baz = IntOption()
config = StringIO("[foo]\nbar=3\n[bar]\nbaz=4")
@@ -518,7 +518,7 @@
def test_parse_option(self):
class MyOtherSchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = StringOption()
expected_value = 'baz'
@@ -536,7 +536,7 @@
class MySchema(Schema):
foo = BoolOption(default=True)
- class bar(ConfigSection):
+ class bar(Section):
baz = IntOption()
bla = StringOption(default='hello')
@@ -616,7 +616,7 @@
def test_get_default_from_section(self):
class MySchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
config = StringIO("[__main__]\n")
expected = 0
@@ -766,7 +766,7 @@
class MySchema(Schema):
foo = StringOption()
- class DEFAULTSECT(ConfigSection):
+ class DEFAULTSECT(Section):
pass
parser = SchemaConfigParser(MySchema())
=== modified file 'tests/pyschema/test_schema.py'
--- tests/pyschema/test_schema.py 2011-06-13 18:51:30 +0000
+++ tests/pyschema/test_schema.py 2011-06-13 18:51:30 +0000
@@ -26,6 +26,7 @@
ConfigOption,
Option,
ConfigSection,
+ Section,
DictConfigOption,
DictOption,
IntConfigOption,
@@ -47,16 +48,16 @@
foo = BoolOption()
class MyOtherSchema(Schema):
- class web(ConfigSection):
+ class web(Section):
bar = IntOption()
- class froo(ConfigSection):
+ class froo(Section):
twaddle = ListOption(item=BoolOption())
class MyThirdSchema(Schema):
bar = IntOption()
- class froo(ConfigSection):
+ class froo(Section):
twaddle = ListOption(item=BoolOption())
schema = MySchema()
@@ -73,11 +74,11 @@
def test_schema_validation(self):
class BorkenSchema(Schema):
- class __main__(ConfigSection):
+ class __main__(Section):
foo = BoolOption()
class SomeSchema(Schema):
- class mysection(ConfigSection):
+ class mysection(Section):
pass
schema = BorkenSchema()
@@ -90,7 +91,7 @@
class MySchema(Schema):
foo = BoolOption()
- class bar(ConfigSection):
+ class bar(Section):
baz = IntOption()
schema = MySchema()
@@ -104,7 +105,7 @@
class MySchema(Schema):
foo = BoolOption()
- class bar(ConfigSection):
+ class bar(Section):
baz = IntOption()
schema = MySchema()
@@ -135,10 +136,10 @@
class MySchema(Schema):
foo = IntOption()
- class one(ConfigSection):
+ class one(Section):
bar = IntOption()
- two = ConfigSection()
+ two = Section()
two.bam = IntOption()
expected = {
@@ -166,8 +167,8 @@
self.assertNotEqual(opt1, opt3)
def test_equal_when_in_section(self):
- sect1 = ConfigSection(name='sect1')
- sect2 = ConfigSection(name='sect2')
+ sect1 = Section(name='sect1')
+ sect2 = Section(name='sect2')
opt1 = IntOption()
opt2 = IntOption()
@@ -197,15 +198,15 @@
class TestSchemaInheritance(unittest.TestCase):
def setUp(self):
class SchemaA(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
class SchemaB(SchemaA):
- class baz(ConfigSection):
+ class baz(Section):
wham = IntOption()
class SchemaC(SchemaA):
- class bar(ConfigSection):
+ class bar(Section):
woof = IntOption()
self.schema = SchemaB()
@@ -215,7 +216,7 @@
names = [('foo', ['bar']), ('baz', ['wham'])]
for section, options in names:
section_obj = getattr(self.schema, section)
- self.assertTrue(isinstance(section_obj, ConfigSection))
+ self.assertTrue(isinstance(section_obj, Section))
for option in options:
option_obj = getattr(section_obj, option)
self.assertTrue(isinstance(option_obj, IntOption))
@@ -237,7 +238,7 @@
def test_merge_inherited(self):
class SchemaA(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
bar = IntOption()
@@ -845,8 +846,8 @@
self.assertEqual(self.parser.values(), expected_values)
-class TestConfigSection(unittest.TestCase):
- cls = ConfigSection
+class TestSection(unittest.TestCase):
+ cls = Section
def test_default_name(self):
section = self.cls()
@@ -872,8 +873,9 @@
section1 = self.cls()
section2 = self.cls(name='foo')
- self.assertEqual(repr(section1), '<ConfigSection>')
- self.assertEqual(repr(section2), '<ConfigSection foo>')
+ self.assertEqual(repr(section1), '<{0}>'.format(self.cls.__name__))
+ self.assertEqual(repr(section2),
+ '<{0} foo>'.format(self.cls.__name__))
def test_has_option(self):
section = self.cls()
@@ -896,3 +898,7 @@
section.bar = 4
self.assertEqual(section.options(), [section.foo])
+
+
+class TestConfigSection(TestSection):
+ cls = ConfigSection
=== modified file 'tests/pyschema/test_schemaconfig.py'
--- tests/pyschema/test_schemaconfig.py 2011-06-13 18:51:30 +0000
+++ tests/pyschema/test_schemaconfig.py 2011-06-13 18:51:30 +0000
@@ -29,10 +29,11 @@
from configglue.pyschema.parser import SchemaConfigParser
from configglue.pyschema.schema import (
ConfigOption,
- Option,
ConfigSection,
IntOption,
+ Option,
Schema,
+ Section,
StringOption,
)
@@ -49,7 +50,7 @@
expected = "<{0} name>".format(self.cls.__name__)
self.assertEqual(repr(opt), expected)
- sect = ConfigSection(name='sect')
+ sect = Section(name='sect')
opt = self.cls(name='name', section=sect)
expected = "<{0} sect.name>".format(self.cls.__name__)
self.assertEqual(repr(opt), expected)
@@ -85,26 +86,28 @@
cls = ConfigOption
-class TestConfigSection(unittest.TestCase):
+class TestSection(unittest.TestCase):
+ cls = Section
+
def test_repr_name(self):
- sect = ConfigSection()
- expected = "<ConfigSection>"
+ sect = self.cls()
+ expected = "<{0}>".format(self.cls.__name__)
self.assertEqual(repr(sect), expected)
- sect = ConfigSection(name='sect')
- expected = "<ConfigSection sect>"
+ sect = self.cls(name='sect')
+ expected = "<{0} sect>".format(self.cls.__name__)
self.assertEqual(repr(sect), expected)
def test_equal(self):
- sec1 = ConfigSection()
- sec2 = ConfigSection(name='sec2')
+ sec1 = self.cls()
+ sec2 = self.cls(name='sec2')
- self.assertEqual(sec1, ConfigSection())
- self.assertEqual(sec2, ConfigSection(name='sec2'))
+ self.assertEqual(sec1, self.cls())
+ self.assertEqual(sec2, self.cls(name='sec2'))
self.assertNotEqual(sec1, sec2)
def test_has_option(self):
- class sec1(ConfigSection):
+ class sec1(self.cls):
foo = IntOption()
sec1 = sec1()
@@ -112,10 +115,14 @@
self.assertFalse(sec1.has_option('bar'))
+class TestConfigSection(TestSection):
+ cls = ConfigSection
+
+
class TestSchemaConfigGlue(unittest.TestCase):
def setUp(self):
class MySchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
bar = IntOption()
baz = IntOption(help='The baz option')
@@ -160,10 +167,10 @@
def test_ambiguous_option(self):
class MySchema(Schema):
- class foo(ConfigSection):
+ class foo(Section):
baz = IntOption()
- class bar(ConfigSection):
+ class bar(Section):
baz = IntOption()
config = StringIO("[foo]\nbaz=1")
Follow ups