[kernel] r9836 - in people/waldi/dkt/lib/dkt/systemconfig: . test

Bastian Blank waldi at alioth.debian.org
Thu Nov 29 12:31:36 UTC 2007


Author: waldi
Date: Thu Nov 29 12:31:36 2007
New Revision: 9836

Log:
lib/dkt/systemconfig: Add.


Added:
   people/waldi/dkt/lib/dkt/systemconfig/
   people/waldi/dkt/lib/dkt/systemconfig/__init__.py
   people/waldi/dkt/lib/dkt/systemconfig/file.py
   people/waldi/dkt/lib/dkt/systemconfig/test/
   people/waldi/dkt/lib/dkt/systemconfig/test/__init__.py
   people/waldi/dkt/lib/dkt/systemconfig/test/test_file.py

Added: people/waldi/dkt/lib/dkt/systemconfig/__init__.py
==============================================================================

Added: people/waldi/dkt/lib/dkt/systemconfig/file.py
==============================================================================
--- (empty file)
+++ people/waldi/dkt/lib/dkt/systemconfig/file.py	Thu Nov 29 12:31:36 2007
@@ -0,0 +1,46 @@
+from dkt.config import file
+
+_marker = object()
+
+class SystemConfig(object):
+    __slots__ = "_config", "_lists"
+
+    def __init__(self, fp):
+        self._lists = {}
+
+        self._config = c = file.Config(fp)
+
+        for s in c._sections.itervalues():
+            name = s['__type__']
+            s1 = self._lists.get(name, [])
+            s1.append(s)
+            self._lists[name] = s1
+
+    def __getattr__(self, name):
+        return getattr(self._config, name)
+
+    def get_sectionlist(self, name, default = _marker):
+        l = self._lists.get(name, _marker)
+        if l is _marker:
+            if default is _marker:
+                raise KeyError(name)
+            return default
+
+        ret = []
+        for item in l:
+            newitem = {}
+            ret.append(newitem)
+            for key, value in item.iteritems():
+                if key == '__value__':
+                    newitem['name'] = item['__value__']
+                elif not (key.startswith('__') and key.endswith('__')):
+                    newitem[key] = value
+
+        return ret
+
+    def itersectionslists(self):
+        return self._lists.iterkeys()
+
+    def sectionlists(self):
+        return self._lists.keys()
+

Added: people/waldi/dkt/lib/dkt/systemconfig/test/__init__.py
==============================================================================

Added: people/waldi/dkt/lib/dkt/systemconfig/test/test_file.py
==============================================================================
--- (empty file)
+++ people/waldi/dkt/lib/dkt/systemconfig/test/test_file.py	Thu Nov 29 12:31:36 2007
@@ -0,0 +1,62 @@
+import py
+from dkt.systemconfig.file import *
+from cStringIO import StringIO
+
+config1 = r"""
+[a]
+a: b
+a-b: c
+
+[b:1]
+a1:b
+a2: b
+a3 :b
+a4 : b
+
+[b: 2]
+a:
+ b
+ c
+
+[b :3]
+
+[b : 4]
+"""
+config1_a = {'a': 'b', 'a-b': 'c'}
+config1_b_1 = {'a1': 'b', 'a2': 'b', 'a3': 'b', 'a4': 'b'}
+config1_b_2 = {'a': '\nb\nc'}
+
+c = SystemConfig(StringIO(config1))
+
+def test_a():
+    assert c.get_section('a') == config1_a
+    assert c.get('a', '__type__') == 'a'
+    assert c.get('a', '__name__') == 'a'
+
+def test_b():
+    b_1 = config1_b_1.copy()
+    b_1['name'] = '1'
+    b_2 = config1_b_2.copy()
+    b_2['name'] = '2'
+    assert c.get_sectionlist('b') == [b_1, b_2, {'name': '3'}, {'name': '4'}]
+
+def test_b_1():
+    assert c.get_section('b:1') == config1_b_1
+    assert c.get('b:1', '__type__') == 'b'
+    assert c.get('b:1', '__name__') == 'b:1'
+
+def test_b_2():
+    assert c.get_section('b:2') == config1_b_2
+
+def test_b_3():
+    assert c.get_section('b:3') == {}
+
+def test_b_4():
+    assert c.get_section('b:4') == {}
+
+def test_sections():
+    assert c.sections() == ['a', 'b:1', 'b:2', 'b:3', 'b:4']
+
+def test_sectionslists():
+    assert c.sectionlists() == ['a', 'b']
+



More information about the Kernel-svn-changes mailing list