def test_boolean_transform(): """Transform boolean values""" xml_string = strip_xml(""" <data> <value>True</value> </data> """) value = {'value': 'It is true'} def _after_parse(_, x): if x: return 'It is true' else: return 'It is false' def _before_serialize(_, x): if x == 'It is true': return True else: return False hooks = xml.Hooks(after_parse=_after_parse, before_serialize=_before_serialize) processor = xml.dictionary('data', [xml.boolean('value', hooks=hooks)]) _transform_test_case_run(processor, value, xml_string)
def test_primitive_values_serialize(): """Serializes primitive values""" value = { 'boolean': True, 'float': 3.14, 'int': 1, 'string': 'Hello, World' } processor = xml.dictionary('root', [ xml.boolean('boolean'), xml.floating_point('float'), xml.integer('int'), xml.string('string'), ]) expected = strip_xml(""" <root> <boolean>True</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_primitive_default_present(): """Parses primitive values with defaults specified""" xml_string = """ <root> <boolean>false</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, default=True), xml.floating_point('float', required=False, default=0.0), xml.integer('int', required=False, default=0), xml.string('string', required=False, default=''), ]) expected = { 'boolean': False, 'float': 3.14, 'int': 1, 'string': 'Hello, World', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_primitive_aliased(): """Parses primitive values with aliases""" xml_string = """ <root> <boolean>true</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', alias='b'), xml.floating_point('float', alias='f'), xml.integer('int', alias='i'), xml.string('string', alias='s'), ]) expected = { 'b': True, 'f': 3.14, 'i': 1, 's': 'Hello, World', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_attribute_serialize_multiple(): """Serializing multiple attributes to the same element""" value = { 'attribute_a': 'Hello, World', 'attribute_b': True, 'data': 1, 'message': 'Hello, World' } processor = xml.dictionary('root', [ xml.integer('data'), xml.string('data', attribute='attribute_a'), xml.boolean('data', attribute='attribute_b'), xml.string('message') ]) expected = strip_xml(""" <root> <data attribute_a="Hello, World" attribute_b="True">1</data> <message>Hello, World</message> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_serialize_to_file(tmpdir): """Serialize XML data to a file""" value = { 'boolean': True, 'float': 3.14, 'int': 1, 'string': 'Hello, World' } processor = xml.dictionary('root', [ xml.boolean('boolean'), xml.floating_point('float'), xml.integer('int'), xml.string('string'), ]) expected = strip_xml(""" <root> <boolean>True</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """) xml_file_name = 'data.xml' xml_file_path = os.path.join(tmpdir.strpath, xml_file_name) xml.serialize_to_file(processor, value, xml_file_path) # Ensure the file contents match what is expected. xml_file = tmpdir.join(xml_file_name) actual = xml_file.read() assert expected == actual
def test_parse_boolean_invalid(): """Parse an invalid boolean value""" xml_string = """ <root> <value>hello</value> </root> """ processor = xml.dictionary('root', [ xml.boolean('value'), ]) with pytest.raises(xml.InvalidPrimitiveValue): xml.parse_from_string(processor, xml_string)
def test_primitive_values_serialize_falsey_omitted(): """Serialize false primitive values""" value = {'boolean': False, 'float': 0.0, 'int': 0, 'string': ''} processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, omit_empty=True), xml.floating_point('float', required=False, omit_empty=True), xml.integer('int', required=False, omit_empty=True), xml.string('string', required=False, omit_empty=True), ]) expected = strip_xml(""" <root /> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_primitive_values_serialize_falsey(): """Serialize false primitive values""" value = {'boolean': False, 'float': 0.0, 'int': 0, 'string': ''} processor = xml.dictionary('root', [ xml.boolean('boolean', required=False), xml.floating_point('float', required=False), xml.integer('int', required=False), xml.string('string', required=False), ]) expected = strip_xml(""" <root> <boolean>False</boolean> <float>0.0</float> <int>0</int> <string /> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def test_primitive_default(): """Parses primitive values with defaults specified""" xml_string = """ <root> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, default=False), xml.floating_point('float', required=False, default=0.0), xml.integer('int', required=False, default=0), xml.string('string', required=False, default=''), ]) expected = { 'boolean': False, 'float': 0.0, 'int': 0, 'string': '', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_primitive_values_serialize_none_default(): """Serialize primitive values where the default for the value is None""" value = {} processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, default=None), xml.floating_point('float', required=False, default=None), xml.integer('int', required=False, default=None), xml.string('string', required=False, default=None), ], required=False) expected = strip_xml(""" <root> <boolean /> <float /> <int /> <string /> </root> """) actual = xml.serialize_to_string(processor, value) assert expected == actual
def _games_list_to_games(self, data): game_processor = xml.dictionary("items", [ xml.array( xml.dictionary("item", [ xml.integer(".", attribute="id"), xml.string(".", attribute="type"), xml.string("name[@type='primary']", attribute="value", alias="name"), xml.string("description"), xml.string("image", required=False, alias="thumbnail"), xml.array( xml.string("link[@type='boardgamecategory']", attribute="value", required=False), alias="categories", ), xml.array( xml.string("link[@type='boardgamemechanic']", attribute="value", required=False), alias="mechanics", ), xml.array( xml.dictionary("link[@type='boardgameexpansion']", [ xml.integer(".", attribute="id"), xml.boolean( ".", attribute="inbound", required=False), ], required=False), alias="expansions", ), xml.array(xml.dictionary( "poll[@name='suggested_numplayers']/results", [ xml.string(".", attribute="numplayers"), xml.array( xml.dictionary("result", [ xml.string(".", attribute="value"), xml.integer(".", attribute="numvotes"), ], required=False), ) ]), alias="suggested_numplayers"), xml.string("statistics/ratings/averageweight", attribute="value", alias="weight"), xml.string( "statistics/ratings/ranks/rank[@friendlyname='Board Game Rank']", attribute="value", alias="rank"), xml.string("statistics/ratings/bayesaverage", attribute="value", alias="rating"), xml.string("playingtime", attribute="value", alias="playing_time"), ], required=False, alias="items")) ]) games = xml.parse_from_string(game_processor, data) games = games["items"] return games
def _games_list_to_games(self, data): def numplayers_to_result(_, results): result = { result["value"].lower().replace(" ", "_"): int(result["numvotes"]) for result in results } if not result: result = {'best': 0, 'recommended': 0, 'not_recommended': 0} is_recommended = result['best'] + result['recommended'] > result[ 'not_recommended'] if not is_recommended: return "not_recommended" is_best = result['best'] > 10 and result['best'] > result[ 'recommended'] if is_best: return "best" return "recommended" def suggested_numplayers(_, numplayers): # Remove not_recommended player counts numplayers = [ players for players in numplayers if players["result"] != "not_recommended" ] # If there's only one player count, that's the best one if len(numplayers) == 1: numplayers[0]["result"] = "best" # Just return the numbers return [(players["numplayers"], players["result"]) for players in numplayers] def log_item(_, item): logger.debug("Successfully parsed: {} (id: {}).".format( item["name"], item["id"])) return item game_processor = xml.dictionary("items", [ xml.array( xml.dictionary( "item", [ xml.integer(".", attribute="id"), xml.string(".", attribute="type"), xml.string("name[@type='primary']", attribute="value", alias="name"), xml.string("description"), xml.array( xml.string("link[@type='boardgamecategory']", attribute="value", required=False), alias="categories", ), xml.array( xml.string("link[@type='boardgamemechanic']", attribute="value", required=False), alias="mechanics", ), xml.array( xml.dictionary( "link[@type='boardgameexpansion']", [ xml.integer(".", attribute="id"), xml.boolean(".", attribute="inbound", required=False), ], required=False), alias="expansions", ), xml.array( xml.dictionary( "poll[@name='suggested_numplayers']/results", [ xml.string(".", attribute="numplayers"), xml.array(xml.dictionary("result", [ xml.string(".", attribute="value"), xml.integer(".", attribute="numvotes"), ], required=False), hooks=xml.Hooks( after_parse= numplayers_to_result)) ]), alias="suggested_numplayers", hooks=xml.Hooks(after_parse=suggested_numplayers), ), xml.string("statistics/ratings/averageweight", attribute="value", alias="weight"), xml.string( "statistics/ratings/ranks/rank[@friendlyname='Board Game Rank']", attribute="value", required=False, alias="rank"), xml.string("statistics/ratings/usersrated", attribute="value", alias="usersrated"), xml.string("statistics/ratings/owned", attribute="value", alias="numowned"), xml.string("statistics/ratings/bayesaverage", attribute="value", alias="rating"), xml.string("playingtime", attribute="value", alias="playing_time"), ], required=False, alias="items", hooks=xml.Hooks(after_parse=log_item), )) ]) games = xml.parse_from_string(game_processor, data) games = games["items"] return games
# <PhoneticWord>Bar</PhoneticWord> # </Transform> # </PhoneticTransforms> # <ProcessorAffinity>4095</ProcessorAffinity> # <ProfilerRefreshInterval>30</ProfilerRefreshInterval> # <PurgeArchivedLogs>False</PurgeArchivedLogs> # <ReferenceToSelf>You</ReferenceToSelf> # <RepositoryLastViewed>1/15/2018 10:50:08 PM -06:00</RepositoryLastViewed> # <ShareServiceUri>https://eq.gimasoft.com/GINAServices/Package.svc</ShareServiceUri> # <ShareWhiteList /> # <StopSearchingAfterFirstMatch>False</StopSearchingAfterFirstMatch> # </Settings> settings_processor = xml.dictionary('Settings', [ xml.string('AcceptShareLevel'), xml.boolean('AllowGamTextTriggerShares'), xml.boolean('AllowSharedPackages'), xml.boolean('ArchiveLogs'), xml.integer('ArchivePurgeDaysToKeep'), xml.string('AutoMergeShareLevel'), xml.boolean('AutoUpdate'), xml.boolean('CheckLibraryAtStartup'), xml.boolean('CompressArchivedLogs'), xml.string('CTagClipboardReplacement'), xml.boolean('DisplayMatches'), xml.boolean('EnableDebugLog'), xml.boolean('EnableSound'), xml.boolean('EnableText'), xml.boolean('EnableTimers'), xml.string('EverquestFolder'), xml.string('ImportedMediaFileFolder'),