예제 #1
0
def testGetDefault():
    "XMLConfig::get should return the default if the key is not found"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    conf = getConfig()
    conf.parse(getConfigFile1(), LOCAL_NAMESPACE)
    assert conf.get("key1") == "string"
    assert conf.get("key2", "default") == "default"
예제 #2
0
def testFloatBase64Encoded():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    from decimal import Decimal
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <float key="long_pi" encoding="base64">
                My4xNDE1OTI2NTM1ODk3OTMyMzg0NjI2NDMzODMyNzk1MDI4ODQxOTcxNjkzOTkz
                NzUxMDU4MjA5NzQ5NDQ1OTIzMDc4MTY0MDYyODYyMDg5OTg2MjgwMzQ4MjUzNDIx
                MTcwNjc5ODIxNDgwODY1MTMyODIzMDY2NDcwOTM4NDQ2MDk1NTA1ODIyMzE3MjUz
                NTk0MDgxMjg0ODExMTc0NTAyODQxMDI3MDE5Mzg1MjExMDU1NTk2NDQ2MjI5NDg5
                NTQ5MzAzODE5NjQ0Mjg4MTA5NzU2NjU5MzM0NDYxMjg0NzU2NDgyMzM3ODY3ODMx
                NjUyNzEyMDE5MDkxNDU2NDg1NjY5MjM0NjAzNDg2MTA0NTQzMjY2NDgyMTMzOTM2
                MDcyNjAyNDkxNDEyNzM3MjQ1ODcwMDY2MDYzMTU1ODgxNzQ4ODE1MjA5MjA5NjI4
                MjkyNTQwOTE3MTUzNjQzNjc4OTI1OTAzNjAwMTEzMzA1MzA1NDg4MjA0NjY1MjEz
                ODQxNDY5NTE5NDE1MTE2MDkK
            </float>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("long_pi") == Decimal('3.14159265358979323846264338327950288'
        '41971693993751058209749445923078164062862089986280348253421170679821480'
        '86513282306647093844609550582231725359408128481117450284102701938521105'
        '55964462294895493038196442881097566593344612847564823378678316527120190'
        '91456485669234603486104543266482133936072602491412737245870066063155881'
        '74881520920962829254091715364367892590360011330530548820466521384146951'
        '941511609')
예제 #3
0
def testTypeChoose():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="default">
                <choose>
                    <default>default</default>
                    <when test="1==0">when</when>
                </choose>
            </string>
            <string key="chosen">
                <choose>
                    <default>default</default>
                    <when test="1==1">when</when>
                </choose>
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("default") == "default"
    assert type(conf.get("default")) is str
    assert conf.get("chosen") == "when"
    assert type(conf.get("chosen")) is str
예제 #4
0
def testTypeChoose():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="default">
                <choose>
                    <default>default</default>
                    <when test="1==0">when</when>
                </choose>
            </string>
            <string key="chosen">
                <choose>
                    <default>default</default>
                    <when test="1==1">when</when>
                </choose>
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("default") == "default"
    assert type(conf.get("default")) is str
    assert conf.get("chosen") == "when"
    assert type(conf.get("chosen")) is str
예제 #5
0
def testCircularImport():
    "Property detect circluar importing"
    from xmlconfig import getConfig
    Urls.clear()
    Urls["config2.xml"] = \
    """<?xml version="1.0"?>
    <config>
        <constants namespace="circular" src="file:config.xml"/>        
        <constants>
            <string key="key22">This was imported from config2.xml</string>        
            <string key="foreign">
                Namespace changed in %(circular:key4.import)
            </string>
        </constants>
    </config>
    """
    Urls["config.xml"] = \
    """<?xml version="1.0"?>
    <config>
        <constants namespace="import" src="file:config2.xml"/>
        <constants>
            <section key="key4">
                <string key="key5">value2</string>
                <string key="import">%(import:key22)</string>
            </section>
        </constants>
    </config>
    """
    conf=getConfig()
    conf.load("config.xml")
    assert conf.get("import:foreign") == \
        "Namespace changed in This was imported from config2.xml"
예제 #6
0
def testGetDefault():
    "XMLConfig::get should return the default if the key is not found"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    conf=getConfig()
    conf.parse(getConfigFile1(), LOCAL_NAMESPACE)
    assert conf.get("key1") == "string"
    assert conf.get("key2","default") == "default"
예제 #7
0
def testCircularImport():
    "Property detect circluar importing"
    from xmlconfig import getConfig
    Urls.clear()
    Urls["config2.xml"] = \
    """<?xml version="1.0"?>
    <config>
        <constants namespace="circular" src="file:config.xml"/>        
        <constants>
            <string key="key22">This was imported from config2.xml</string>        
            <string key="foreign">
                Namespace changed in %(circular:key4.import)
            </string>
        </constants>
    </config>
    """
    Urls["config.xml"] = \
    """<?xml version="1.0"?>
    <config>
        <constants namespace="import" src="file:config2.xml"/>
        <constants>
            <section key="key4">
                <string key="key5">value2</string>
                <string key="import">%(import:key22)</string>
            </section>
        </constants>
    </config>
    """
    conf = getConfig()
    conf.load("config.xml")
    assert conf.get("import:foreign") == \
        "Namespace changed in This was imported from config2.xml"
예제 #8
0
 def validate(self, url):
     config = getConfig()
     # Use the on_load event, because, if the document imports other
     # documents, we want to validate them all. (XXX Do we really?)
     config.on_load += self.validate_file
     try:
         config.load(url)
     except:
         # We should catch the errors in one or more of the validation
         # checks. (Fingers crossed.)
         pass
예제 #9
0
 def validate(self, url):
     config = getConfig()
     # Use the on_load event, because, if the document imports other
     # documents, we want to validate them all. (XXX Do we really?)
     config.on_load += self.validate_file
     try:
         config.load(url)
     except:
         # We should catch the errors in one or more of the validation
         # checks. (Fingers crossed.)
         pass
예제 #10
0
def testTypeString():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="str">string</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("str") == "string"
    assert type(conf.get("str")) is str
예제 #11
0
def testWhitespace1():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="preserved" options="preserve-whitespace">
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("preserved") == "\n            "
예제 #12
0
def testReferenceChars():
    "Incomplete references should not break"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
           <string key="not-a-ref">%(int</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("not-a-ref") == "%(int"
예제 #13
0
def testTypeString():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="str">string</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("str") == "string"
    assert type(conf.get("str")) is str
예제 #14
0
def testReferenceChars():
    "Incomplete references should not break"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
           <string key="not-a-ref">%(int</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("not-a-ref") == "%(int"
예제 #15
0
def testTypeInt():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <int key="int">299792458</int>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("int") == 299792458
    assert type(conf.get("int")) is int
예제 #16
0
def testTypeInt():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <int key="int">299792458</int>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("int") == 299792458
    assert type(conf.get("int")) is int
예제 #17
0
def testTypeList():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <list key="list">1,2,3,4,5</list>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    # Default type for list items is str
    assert conf.get("list") == ["1", "2", "3", "4", "5"]
    assert type(conf.get("list")) is list
예제 #18
0
def testSimpleDelimiter():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <list key="star-delimiter" delimiter="*">
            1*2*3*4
            </list>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert len(conf.get("star-delimiter")) == 4
예제 #19
0
def testMultiCharDelimiter():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <list key="crazy-delimiter" delimiter="-==-">
            1-==-2-==-3-==-4
            </list>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert len(conf.get("crazy-delimiter")) == 4
예제 #20
0
def testTypeEncrypted():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="password" options="salt:pYzSxGhzoc4H;encoding:base64">
                UhRy2pLYh6g=
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("password") == "password"
예제 #21
0
def testBasicReference():
    "References match %(name)"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="ref">reference value</string>
            <string key="tramp">%(ref)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("tramp") == "reference value"
예제 #22
0
def testReferenceInvalid():
    "Invalid references should default to None"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <int key="int-ref">42</int>
            <string key="tramp3">%(int ref)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("tramp3") is None
예제 #23
0
def testTypeList():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <list key="list">1,2,3,4,5</list>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    # Default type for list items is str
    assert conf.get("list") == ["1","2","3","4","5"]
    assert type(conf.get("list")) is list
예제 #24
0
def testTypeEncrypted():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="password" options="salt:pYzSxGhzoc4H;encoding:base64">
                UhRy2pLYh6g=
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("password") == "password"
예제 #25
0
def testBasicReference():
    "References match %(name)"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="ref">reference value</string>
            <string key="tramp">%(ref)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("tramp") == "reference value"
예제 #26
0
def testReferenceInvalid():
    "Invalid references should default to None"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <int key="int-ref">42</int>
            <string key="tramp3">%(int ref)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("tramp3") is None
예제 #27
0
def testEnvironmentReference():
    "The magic 'env' namespace should refer to environment variables"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    import os
    os.environ['test_var'] = 'xmlconfig is sweet'
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
           <string key="env">%(env:test_var)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("test_var") == "xmlconfig is sweet"
예제 #28
0
def testEnvironmentReference():
    "The magic 'env' namespace should refer to environment variables"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    import os
    os.environ['test_var'] = 'xmlconfig is sweet'
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
           <string key="env">%(env:test_var)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("test_var") == "xmlconfig is sweet"
예제 #29
0
def testImportContent():
    "Cannot import content from a file"
    from xmlconfig import getConfig
    Urls.clear()
    Urls["file:file.txt"] = "Content embedded in a file"
    Urls["config.xml"] = \
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="import" src="file:file.txt"/>
        </constants>
    </config>
    """
    conf = getConfig()
    conf.load("config.xml")
    assert conf.get("import") == "Content embedded in a file"
예제 #30
0
def testImportContent():
    "Cannot import content from a file"
    from xmlconfig import getConfig
    Urls.clear()
    Urls["file:file.txt"] = "Content embedded in a file"
    Urls["config.xml"] = \
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="import" src="file:file.txt"/>
        </constants>
    </config>
    """
    conf=getConfig()
    conf.load("config.xml")
    assert conf.get("import") == "Content embedded in a file"
예제 #31
0
def testForwardReference():
    "Reference a constant that has not yet been defined"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="ref2">This is a forward %(key7)</string>
    
            <!-- This is a comment -->
            <string key="key7">reference</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("ref2") == "This is a forward reference"
예제 #32
0
def testTypeFloat():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <float key="float">3.14159265359</float>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)

    # Float element returns Decimal-s for precision
    from decimal import Decimal
    assert conf.get("float") == Decimal('3.14159265359')
    assert type(conf.get("float")) is Decimal
예제 #33
0
def testTypeFloat():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <float key="float">3.14159265359</float>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)

    # Float element returns Decimal-s for precision
    from decimal import Decimal
    assert conf.get("float") == Decimal('3.14159265359')
    assert type(conf.get("float")) is Decimal
예제 #34
0
def testForwardReference():
    "Reference a constant that has not yet been defined"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="ref2">This is a forward %(key7)</string>
    
            <!-- This is a comment -->
            <string key="key7">reference</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("ref2") == "This is a forward reference"
예제 #35
0
def testChooseImportOs():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="please_break">
                <choose>
                    <default/>
                    <when test="__import__('os').getuid() != 0">broken</when>
                </choose>
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("please_break") == ""
예제 #36
0
def testForwardForeignNamespace():
    "Reference a constant in a namespace that is defined later"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="key8">This is a reference to a %(other:foreign) namespace</string>
        </constants>
        <constants namespace="other">
            <string key="foreign">foreign</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("key8") == "This is a reference to a foreign namespace"
    assert conf.get("other:foreign") == "foreign"
예제 #37
0
def testChooseImportOs():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="please_break">
                <choose>
                    <default/>
                    <when test="__import__('os').getuid() != 0">broken</when>
                </choose>
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("please_break") == ""
예제 #38
0
def testForwardForeignNamespace():
    "Reference a constant in a namespace that is defined later"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="key8">This is a reference to a %(other:foreign) namespace</string>
        </constants>
        <constants namespace="other">
            <string key="foreign">foreign</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("key8") == "This is a reference to a foreign namespace"
    assert conf.get("other:foreign") == "foreign"
예제 #39
0
def testTypeFloatExponent():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <float key="avogadro">6.023E+23</float>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    from decimal import Decimal
    print conf.get("avogadro")
    assert conf.get("avogadro") == Decimal('6.023E+23')

    # Float element returns Decimal-s for precision
    from decimal import Decimal
    assert conf.get("float") == Decimal('3.14159265359')
예제 #40
0
def testSectionReference():
    "Reference an entire section"
    from xmlconfig import getConfig, LOCAL_NAMESPACE, SectionConstant
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <section key="key4">
                <string key="key5">value2</string>
            </section>
            <!-- Reference an entire section -->
            <section key="section_import">%(key4)</section>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    # XXX This really should be returned as a dict type
    assert type(conf.get("section_import")) is SectionConstant
예제 #41
0
def testListPreserveWhitespace():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <list key="ws-delimiter" delimiter="," preserve-whitespace="true">
            1,
            2,
            3,
            4
            </list>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert len(conf.get("ws-delimiter")) == 4
    assert conf.get("ws-delimiter")[1] == "            2"
예제 #42
0
def testReferenceChangeType():
    """
    Type should be determined by the constant enclosing the reference, not
    the referencee
    """
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <int key="int-ref">42</int>
            <string key="tramp2">%(int-ref)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("tramp2") == "42"
    assert type(conf.get("tramp2")) is str
예제 #43
0
def testSectionReference():
    "Reference an entire section"
    from xmlconfig import getConfig, LOCAL_NAMESPACE, SectionConstant
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <section key="key4">
                <string key="key5">value2</string>
            </section>
            <!-- Reference an entire section -->
            <section key="section_import">%(key4)</section>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    # XXX This really should be returned as a dict type
    assert type(conf.get("section_import")) is SectionConstant
예제 #44
0
def testClearEncryptedReference():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="password" options="salt:pYzSxGhzoc4H;encoding:base64">
                UhRy2pLYh6g=
            </string>
            
            <string key="dsn">
                DSN=ODBC;UID=schema;PWD=%(password)
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("dsn") == 'DSN=ODBC;UID=schema;PWD=password'
예제 #45
0
def testClearEncryptedReference():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="password" options="salt:pYzSxGhzoc4H;encoding:base64">
                UhRy2pLYh6g=
            </string>
            
            <string key="dsn">
                DSN=ODBC;UID=schema;PWD=%(password)
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("dsn") == 'DSN=ODBC;UID=schema;PWD=password'
예제 #46
0
def testReferenceChangeType():
    """
    Type should be determined by the constant enclosing the reference, not
    the referencee
    """
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <int key="int-ref">42</int>
            <string key="tramp2">%(int-ref)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("tramp2") == "42"
    assert type(conf.get("tramp2")) is str
예제 #47
0
def testTypeFloatExponent():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <float key="avogadro">6.023E+23</float>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    from decimal import Decimal
    print conf.get("avogadro")
    assert conf.get("avogadro") == Decimal('6.023E+23')

    # Float element returns Decimal-s for precision
    from decimal import Decimal
    assert conf.get("float") == Decimal('3.14159265359')
예제 #48
0
def testTypeListNewLineSeparated():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <list key="nl-list" delimiter="&#10;">
            1
            2
            3
            4
            5
            </list>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert len(conf.get("nl-list")) == 5
    assert conf.get("nl-list")[2] == "3"
예제 #49
0
def testNestedDictReference():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <!-- Test for a nested dictionary -->
            <section key="dict_in_dict">
                <section key="internal_dict">
                    <string key="inside">Inside a dict inside a dict</string>
                </section>
            </section>
            
            <string key="nested_dict_ref">%(dict_in_dict.internal_dict.inside)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("nested_dict_ref") == "Inside a dict inside a dict"
예제 #50
0
def testNestedDictReference():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <!-- Test for a nested dictionary -->
            <section key="dict_in_dict">
                <section key="internal_dict">
                    <string key="inside">Inside a dict inside a dict</string>
                </section>
            </section>
            
            <string key="nested_dict_ref">%(dict_in_dict.internal_dict.inside)</string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("nested_dict_ref") == "Inside a dict inside a dict"
예제 #51
0
def testTypeListNewLineSeparated():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <list key="nl-list" delimiter="&#10;">
            1
            2
            3
            4
            5
            </list>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert len(conf.get("nl-list")) == 5
    assert conf.get("nl-list")[2] == "3"
예제 #52
0
def testEncryptedReference():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="password" options="salt:pYzSxGhzoc4H;encoding:base64">
                UhRy2pLYh6g=
            </string>
            
            <!-- This cryptic constant has an embedded reference to %(password),
                 so it tests the order of decoding, decryption, and reference
                 resolution -->
            <string key="encrypted-with-ref" salt="t3J6jETFKlsN" encoding="base64">
                dv5XkQCiUXW0EgL3uc0fUmsOA6+CK9G9S2pj+X+HgphMd/cPPNAtMw==
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("encrypted-with-ref") == "Encrypted reference to password"
예제 #53
0
def testTypeDict():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <section key="dict">
                <string key="item1">item1</string>
                <int key="item2">1000</int>
            </section>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert 'item1' in conf.get("dict")
    assert 'item2' in conf.get("dict")
    # XXX This interface isn't really nice -- lose the .value
    assert conf.get("dict")["item1"].value == "item1"
    assert conf["dict"]["item2"].value == 1000
    assert type(conf.get("dict").get("item1").value) is str
    assert type(conf["dict"].get("item2").value) is int
예제 #54
0
def testEncryptedReference():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <string key="password" options="salt:pYzSxGhzoc4H;encoding:base64">
                UhRy2pLYh6g=
            </string>
            
            <!-- This cryptic constant has an embedded reference to %(password),
                 so it tests the order of decoding, decryption, and reference
                 resolution -->
            <string key="encrypted-with-ref" salt="t3J6jETFKlsN" encoding="base64">
                dv5XkQCiUXW0EgL3uc0fUmsOA6+CK9G9S2pj+X+HgphMd/cPPNAtMw==
            </string>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("encrypted-with-ref") == "Encrypted reference to password"
예제 #55
0
def testTypeDict():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf=getConfig()
    conf.parse(stringIOWrapper(
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <section key="dict">
                <string key="item1">item1</string>
                <int key="item2">1000</int>
            </section>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert 'item1' in conf.get("dict")
    assert 'item2' in conf.get("dict")
    # XXX This interface isn't really nice -- lose the .value
    assert conf.get("dict")["item1"].value == "item1"
    assert conf["dict"]["item2"].value == 1000
    assert type(conf.get("dict").get("item1").value) is str
    assert type(conf["dict"].get("item2").value) is int
예제 #56
0
def testTypeBool():
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    from core import stringIOWrapper
    conf = getConfig()
    conf.parse(
        stringIOWrapper(u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants>
            <bool key="bool1">False</bool>
            <bool key="bool2">True</bool>
            <bool key="bool3">1</bool>
            <bool key="bool4">0</bool>
            <bool key="bool5"></bool>
            <bool key="bool6">Non-empty string</bool>
        </constants>
    </config>
    """), LOCAL_NAMESPACE)
    assert conf.get("bool1") is False
    assert conf.get("bool2") is True
    assert conf.get("bool3") is True
    assert conf.get("bool4") is False
    assert conf.get("bool5") is False
    assert conf.get("bool6") is True
예제 #57
0
def testRelativeImport():
    """Transfer leading absolute or relative path to the location of 
    documents imported"""
    from xmlconfig import getConfig
    Urls["../config/config2.xml"] = \
    """<?xml version="1.0"?>
    <config>
        <constants>
            <string key="key22">This was imported from config2.xml</string>
        </constants>
    </config>
    """
    Urls["../config/config.xml"] = \
    """<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants namespace="import" src="file:config2.xml"/>
        <constants>
            <string key="imported">%(import:key22)</string>
        </constants>
    </config>
    """
    conf = getConfig()
    conf.load("../config/config.xml")
    assert conf.get("imported") == "This was imported from config2.xml"
예제 #58
0
def testImportConfig():
    "Cannot import another config file"
    from xmlconfig import getConfig
    Urls.clear()
    Urls["config2.xml"] = \
    """<?xml version="1.0"?>
    <config>
        <constants>
            <string key="key22">This was imported from config2.xml</string>
        </constants>
    </config>
    """
    Urls["config.xml"] = \
    u"""<?xml version="1.0" encoding="utf-8"?>
    <config>
        <constants namespace="import" src="file:config2.xml"/>
        <constants>
            <string key="imported">%(import:key22)</string>
        </constants>
    </config>
    """
    conf = getConfig()
    conf.load("config.xml")
    assert conf.get("imported") == "This was imported from config2.xml"
예제 #59
0
def testLoad():
    "Auto prepend file: to the open url"
    from xmlconfig import getConfig, LOCAL_NAMESPACE
    conf = getConfig("")
    conf.parse(getConfigFile1(), LOCAL_NAMESPACE)
    assert conf.get("key1") is not None