Skip to content

Fix KeyError: 'value' for parsing TMX Map Format #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2022
Merged

Fix KeyError: 'value' for parsing TMX Map Format #60

merged 1 commit into from
Aug 10, 2022

Conversation

laqieer
Copy link
Contributor

@laqieer laqieer commented Aug 9, 2022

Problem

KeyError: 'value' when parsing .tmx files.

Tiled version: 1.9.0
pytiled_parser version: 2.1.0

Traceback (most recent call last):
  File "./make_map.py", line 11, in <module>
    map = pytiled_parser.parse_map(map_file)
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parser.py", line 25, in parse_map
    return tmx_map_parse(file)  # type: ignore
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parsers/tmx/tiled_map.py", line 65, in parse
    layers.append(parse_layer(element, parent_dir))
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parsers/tmx/layer.py", line 365, in parse
    return _parse_object_layer(raw_layer, parent_dir)
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parsers/tmx/layer.py", line 270, in _parse_object_layer
    objects.append(parse_object(object_, parent_dir))
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parsers/tmx/tiled_object.py", line 294, in parse
    return _parse_tile(raw_object, new_tileset, new_tileset_path)
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parsers/tmx/tiled_object.py", line 156, in _parse_tile
    **_parse_common(raw_object).__dict__
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parsers/tmx/tiled_object.py", line 60, in _parse_common
    common.properties = parse_properties(properties_element)
  File "/Users/laqieer/Library/Python/3.8/lib/python/site-packages/pytiled_parser/parsers/tmx/properties.py", line 18, in parse
    value_ = raw_property.attrib["value"]
KeyError: 'value'

Reason

According to Tiled's doc:

Class properties will have their member values stored in a nested <properties> element.

In this case, raw_property.attrib doesn't have key value:

{'name': 'unit', 'type': 'class', 'propertytype': 'Unit'}

Solution

This PR use get() to get the value of key value instead. It avoids the crash when opening the TMX file with parse_map, but it cannot get the actual value of Class property. Class property's support for TMX is introduced in #56 and reverted in 9e8e3b9. In contrast, this PR is to fix the possible KeyError here rather than support Class properties.

@laqieer laqieer changed the title Fix KeyError: 'value' for TMX map format Fix KeyError: 'value' for parsing TMX Map Format Aug 9, 2022
@Cleptomania
Copy link
Member

Hey thanks for this, I'll merge it in and push out a release for it today.

I do intend to add proper support for classes at some point, but it needs more thought given to it and how exactly we want it to work.

@Cleptomania Cleptomania merged commit c7c9e3e into pythonarcade:development Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants