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"
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')
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
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
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"
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"
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"
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
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
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 "
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"
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
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"
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
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
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
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
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
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"
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"
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
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
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"
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"
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
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"
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"
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"
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"
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"
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
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
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"
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") == ""
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"
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") == ""
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"
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')
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
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"
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
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
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'
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'
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
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')
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=" "> 1 2 3 4 5 </list> </constants> </config> """), LOCAL_NAMESPACE) assert len(conf.get("nl-list")) == 5 assert conf.get("nl-list")[2] == "3"
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"
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"
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=" "> 1 2 3 4 5 </list> </constants> </config> """), LOCAL_NAMESPACE) assert len(conf.get("nl-list")) == 5 assert conf.get("nl-list")[2] == "3"
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"
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
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"
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
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
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"
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"
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