Skip to content

Commit cfd5aa4

Browse files
committed
Merge pull request #68 from FodT/json
fixes for json format changes
2 parents 95f2440 + ec1b065 commit cfd5aa4

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

run_acs.py

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#! /usr/bin/env python
2+
13
from __future__ import print_function
24
from terminalone import T1, filters
35
from terminalone.utils import credentials

terminalone/jsonparser.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def __init__(self, body):
5050
if type(data) == list:
5151
self.entities = map(self.process_entity, data)
5252

53-
elif parsed_data.get('include') is not None or \
54-
parsed_data.get('exclude') is not None or \
55-
parsed_data.get('enabled') is not None:
56-
self._parse_target_dimensions(parsed_data)
53+
elif data.get('include') is not None or \
54+
data.get('exclude') is not None or \
55+
data.get('enabled') is not None:
56+
self._parse_target_dimensions(data)
5757

5858
elif data.get('permissions') is not None:
5959
self.entities = self._parse_permissions(data['permissions'])
@@ -109,16 +109,14 @@ def _parse_permissions(self, permissions):
109109

110110
def _parse_target_dimensions(self, data):
111111
"""Iterate over target dimensions and parse into dicts"""
112-
exclude = data.get('exclude', {})
113-
include = data.get('include', {})
114-
exclude_entities = exclude.get('entities')
115-
include_entities = include.get('entities')
112+
exclude_entities = data.get('exclude')
113+
include_entities = data.get('include')
116114
if include_entities is not None:
117-
include_list = map(self.process_entity, include_entities[0].get('entity', []))
115+
include_list = map(self.process_entity, include_entities)
118116
else:
119117
include_list = []
120118
if exclude_entities is not None:
121-
exclude_list = map(self.process_entity, exclude_entities[0].get('entity', []))
119+
exclude_list = map(self.process_entity, exclude_entities)
122120
else:
123121
exclude_list = []
124122
self.entity_count = 1
@@ -149,20 +147,28 @@ def process_entity(cls, entity):
149147
output['_type'] = output['entity_type']
150148

151149
for key, val in six.iteritems(output):
152-
# FIXME this isn't the correct thing to do;
153-
# however it's linked to an inconsistency in t1 json api - so fix when that's changed.
154-
if type(val) == list: # Get parent entities recursively
150+
if type(val) is dict and 'entity_type' in val: # Get parent entities recursively
151+
cls.process_related_entity(relations, val)
152+
elif type(val) is list and 'entity_type' in next(iter(val), {}):
155153
for child in val:
156-
ent = cls.process_entity(child)
157-
if child['rel'] == ent['_type']:
158-
relations[child['rel']] = ent
159-
else:
160-
relations.setdefault(child['rel'], []).append(ent)
154+
cls.process_related_entity(relations, child)
155+
# this is new as we are potentially returning multiple
156+
# currency types, but for now let's grab the first value
157+
elif type(val) is list and 'currency_code' in next(iter(val), {}):
158+
output[key] = val[0]['value']
161159

162160
if relations:
163161
output['relations'] = relations
164162
return output
165163

164+
@classmethod
165+
def process_related_entity(cls, relations, val):
166+
ent = cls.process_entity(val)
167+
if val['rel'] == ent['_type']:
168+
relations[val['rel']] = ent
169+
else:
170+
relations.setdefault(val['rel'], []).append(ent)
171+
166172
@staticmethod
167173
def process_permission(permission, type):
168174
if not permission:

0 commit comments

Comments
 (0)