File: schemaless.py

package info (click to toggle)
zconfig 2.7.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 896 kB
  • ctags: 769
  • sloc: python: 5,196; xml: 329; perl: 37; makefile: 32; sh: 1
file content (116 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (2)
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
##############################################################################
#
# Copyright (c) 2007 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""\
Support for working with ZConfig data without a schema.

"""
__docformat__ = "reStructuredText"

import ZConfig.cfgparser


def loadConfigFile(file, url=None):
    c = Context()
    Parser(Resource(file, url), c).parse(c.top)
    return c.top


class Resource:

    def __init__(self, file, url=''):
        self.file, self.url = file, url


class Section(dict):

    imports = ()

    def __init__(self, type='', name='', data=None, sections=None):
        dict.__init__(self)
        if data:
            self.update(data)
        self.sections = sections or []
        self.type, self.name = type, name

    def addValue(self, key, value, *args):
        if key in self:
            self[key].append(value)
        else:
            self[key] = [value]

    def __str__(self, pre=''):
        result = []

        if self.imports:
            for pkgname in self.imports:
                result.append('%import ' + pkgname)
            result.append('')

        if self.type:
            if self.name:
                start = '%s<%s %s>' % (pre, self.type, self.name)
            else:
                start = '%s<%s>' % (pre, self.type)
            result.append(start)
            pre += '  '

        lst = list(self.items())
        lst.sort()
        for name, values in lst:
            for value in values:
                result.append('%s%s %s' % (pre, name, value))

        if self.sections and self:
            result.append('')

        for section in self.sections:
            result.append(section.__str__(pre))
        
        if self.type:
            pre = pre[:-2]
            result.append('%s</%s>' % (pre, self.type))
            result.append('')

        result = '\n'.join(result).rstrip()
        if not pre:
            result += '\n'
        return result


class Context:

    def __init__(self):
        self.top = Section()
        self.sections = []

    def startSection(self, container, type, name):
        newsec = Section(type, name)
        container.sections.append(newsec)
        return newsec

    def endSection(self, container, type, name, newsect):
        pass

    def importSchemaComponent(self, pkgname):
        if pkgname not in self.top.imports:
            self.top.imports += (pkgname, )

    def includeConfiguration(self, section, newurl, defines):
        raise NotImplementedError('includes are not supported')


class Parser(ZConfig.cfgparser.ZConfigParser):

    def handle_define(self, section, rest):
        raise NotImplementedError('defines are not supported')