Skip to content

Commit 64e0ac7

Browse files
authored
Merge pull request #11 from remyrd/fix/None-entries
Handle nonexistent entries
2 parents 628352c + 3a0794a commit 64e0ac7

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

jimpass/parser.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ def fetch_param_from_dict(self, item: dict, param_name: str) -> str:
3434
if p not in sub:
3535
return "?"
3636
sub = sub[p]
37-
return str(sub)
37+
return str(sub) if sub else "?"
3838

3939
def dumps(self, item_dict: dict) -> str:
4040

4141
flat_mapping = dict(
42-
[(k, self.fetch_param_from_dict(item_dict, k)) for k, v in self.mapping.items()])
42+
[(k, self.fetch_param_from_dict(item_dict, k))
43+
for k, v in self.mapping.items()])
4344
return self.template_str.format(**flat_mapping)
4445

4546
def _mapping_string_to_dict(self, dict_mapping: list, value: str, sub: dict) -> dict:
@@ -64,8 +65,9 @@ def loads(self, item_str: str) -> dict:
6465
if key not in self.mapping:
6566
raise Exception("Unkown variable in template string")
6667
res = {}
67-
for param_name in params.keys():
68-
self._mapping_string_to_dict(self.mapping[param_name].split('.'), params[param_name], res)
68+
for param_name, val in params.items():
69+
if val not in '?':
70+
self._mapping_string_to_dict(self.mapping[param_name].split('.'), params[param_name], res)
6971
return res
7072

7173
def str_matches_mapping(self, item_str: str) -> bool:

tests/test_parser.py

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
}
2020
dummy_str = "account <b>Bob</b> with user sponge.bob"
2121

22+
dummy_incomplete_item = {
23+
"name": "Bob"
24+
}
25+
dummy_incomplete_str = "account <b>Bob</b> with user ?" # intentionally missing
26+
2227

2328
def test_dump():
2429
assert(parser.dumps(dummy_item) == dummy_str)
@@ -29,3 +34,5 @@ def test_load():
2934
assert('name' in loaded_item and 'login' in loaded_item)
3035
assert(loaded_item['name'] == dummy_item['name'])
3136
assert(loaded_item['login']['username'] == dummy_item['login']['username'])
37+
loaded_incomplete_item = parser.loads(dummy_incomplete_str)
38+
assert(loaded_incomplete_item['name'] == dummy_incomplete_item['name'])

0 commit comments

Comments
 (0)