示例#1
0
    def parse_json_file(self):
        """ Parse json file for this campaign.

        If file is corrupted,
        it will raise Campaign.CorruptedFile exception.
        """
        # campaign data
        if self.json_path:
            try:
                content = S3Data().fetch(self.json_path)
                content_json = parse_json_string(content)
                Campaign.validate(content_json, self.uuid)
                self._content_json = content_json
                attributes = self.get_attributes()
                for key, value in content_json.items():
                    if key in attributes:
                        setattr(self, key, value)
            except json.decoder.JSONDecodeError:
                raise JsonModel.CorruptedFile
        self.types = Campaign.parse_types_string(json.dumps(self.types))

        # geometry data
        if self.geojson_path:
            try:
                content = S3Data().fetch(self.geojson_path)
                geometry = parse_json_string(content)
                self.geometry = geometry
                self._content_json['geometry'] = geometry
            except json.decoder.JSONDecodeError:
                raise JsonModel.CorruptedFile
示例#2
0
 def parse_types_string(types_string):
     types = parse_json_string(types_string)
     for type, value in types.items():
         json_tags = {}
         tags = value['tags']
         if (isinstance(tags, list)):
             for tag in value['tags']:
                 tag = tag.replace(']', '')
                 tag_splitted = tag.split('[')
                 tag_key = tag_splitted[0].strip()
                 if len(tag_splitted) == 2:
                     json_tags[tag_key] = tag_splitted[1].split(',')
                 else:
                     json_tags[tag_key] = []
             value['tags'] = json_tags
     return types