[kernel] r9395 - people/waldi/dkt/lib/dkt/config

Bastian Blank waldi at alioth.debian.org
Tue Aug 28 17:51:30 UTC 2007


Author: waldi
Date: Tue Aug 28 17:51:30 2007
New Revision: 9395

Log:
lib/dkt/config/file.py: Add.


Added:
   people/waldi/dkt/lib/dkt/config/file.py

Added: people/waldi/dkt/lib/dkt/config/file.py
==============================================================================
--- (empty file)
+++ people/waldi/dkt/lib/dkt/config/file.py	Tue Aug 28 17:51:30 2007
@@ -0,0 +1,82 @@
+import re
+from . import base
+
+class Config(base.Config):
+    def __init__(self, fp):
+        from dkt.support.ordered_dict import OrderedDict
+        self._sections = OrderedDict()
+        self._read(fp)
+
+    _rules = r"""
+^
+(
+    \[
+    (?P<section>[-\w\d]+)
+    (
+        \s*
+        :
+        \s*
+        (?P<section_value>[-\w\d]+)
+    )?
+    \]
+    |
+    (?P<option>[-\w\d]+)
+    \s*
+    :
+    \s*
+    (?P<option_value>.*)
+    |
+    \s+
+    (?P<continuation>.*)
+    |
+    #.*
+    |
+)
+$
+"""
+    _re = re.compile(_rules, re.UNICODE | re.VERBOSE)
+
+    def _read(self, fp):
+        cursect = None
+        optname = None
+
+        while True:
+            line = fp.readline()
+            if not line:
+                break
+            line = line.rstrip()
+
+            match = self._re.match(line)
+            if match is None:
+                raise RuntimeError("Parse error")
+
+            if match.group('section') is not None:
+                sectname = secttype = match.group('section')
+                if match.group('section_value') is not None:
+                    sectname += ':' + match.group('section_value')
+                if sectname in self._sections:
+                    cursect = self._sections[sectname]
+                else:
+                    cursect = {'__name__': sectname, '__type__': secttype}
+                    self._sections[sectname] = cursect
+                optname = None
+
+            elif match.group('option') is not None:
+                if cursect is None:
+                    raise RuntimeError("Parse Error")
+
+                optname = match.group('option')
+                cursect[optname] = match.group('option_value')
+
+            elif match.group('continuation') is not None:
+                if optname is None:
+                    raise RuntimeError("Parse Error")
+
+                value = match.group('continuation')
+                if value:
+                    cursect[optname] = "%s\n%s" % (cursect[optname], value)
+
+class MutableConfig(Config, base.MutableConfig):
+    def __init__(self, fp):
+        super(MutableConfig, self).__init__(fp)
+



More information about the Kernel-svn-changes mailing list