def to_model_property(d: JsonDict) -> ModelProperty: d["modelTitle"] = d.pop("conceptTitle") d["dataType"] = dt.deserialize(d.pop("dataType")) return ModelProperty( **{ decamelize(k): v for k, v in d.items() if decamelize(k) in ModelProperty.PUBLIC })
def put_mesh() -> RespT: """Add or update collision mesh. --- put: tags: - Collisions description: Add or update collision mesh. parameters: - name: meshId in: query schema: type: string - name: meshFileId in: query schema: type: string - name: meshScaleX in: query schema: type: number format: float default: 1.0 - name: meshScaleY in: query schema: type: number format: float default: 1.0 - name: meshScaleZ in: query schema: type: number format: float default: 1.0 requestBody: content: application/json: schema: $ref: Pose responses: 200: description: Ok content: application/json: schema: type: string """ if not isinstance(request.json, dict): raise FlaskException("Body should be a JSON dict containing Pose.", error_code=400) args = humps.decamelize(request.args.to_dict()) mesh = object_type.Mesh(args["mesh_id"], args["mesh_file_id"]) collision_objects[mesh.id] = CollisionObject( mesh, common.Pose.from_dict(humps.decamelize(request.json))) return jsonify("ok"), 200
def transform_json(this_json, stream_name, data_key): if data_key in this_json: converted_json = humps.decamelize(this_json[data_key]) else: converted_json = humps.decamelize(this_json) fixed_records = fix_records(converted_json) return converted_json
def _insert_param_values(con, job_id, topic_values): for pos, t in enumerate(topic_values): position_id = con.execute( "INSERT INTO job_topic_position(job_id, steps_id, position) VALUES (?, ?, ?)", [job_id, t["topicId"], pos]).lastrowid jtkvt = [(position_id, k, _to_untyped_value(v["value"], humps.decamelize(v["type"])), humps.decamelize(v["type"])) for k, v in t["values"].items()] con.executemany( "INSERT INTO job_config(position_id, key, value, type) VALUES(?, ?, ?, ?)", jtkvt)
def to_snake(self): # intput: # abCdEf || AbCdEf # output: # ab_cd_ef # AB_CD_EF tmp = self.str_to_convert return [ humps.decamelize(tmp), humps.decamelize(tmp).upper(), ]
def handle_response(response, success, status_code=status.HTTP_200_OK): if not success: return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=jsonable_encoder({ "msg": "Failed to restart video processor", "type": "unknown error on the config file", "body": humps.decamelize(response) }) ) content = humps.decamelize(response) if response else None return JSONResponse(status_code=status_code, content=content)
def get_file_df(file_path): data = [] if file_path.endswith(".gz"): opener = gzip.open else: opener = open with opener(file_path, "r") as f: for line in f: event_json = json.loads(line) snake_cased_event_json = humps.decamelize(event_json) data.append(snake_cased_event_json) logger.debug( f"first 5 event json objects = {json.dumps(data[0:5], indent=4, default=str)}" ) flattened_data = [] for d in data: flattened_data.append(json_util.flatten_json(d)) logger.debug( f"first 5 flattened event json objects = {json.dumps(flattened_data[0:5], indent=4, default=str)}" ) df = pd.DataFrame(flattened_data) return df
def put_object_type() -> RespT: """Add or update object type. --- put: tags: - ObjectType description: Add or update object type. requestBody: content: application/json: schema: $ref: ObjectType responses: 200: description: Ok content: application/json: schema: type: string format: date-time """ obj_type = object_type.ObjectType.from_dict(humps.decamelize(request.json)) obj_type.modified = datetime.now(tz=timezone.utc) if obj_type.id not in OBJECT_TYPES: obj_type.created = obj_type.modified OBJECT_TYPES[obj_type.id] = obj_type return jsonify(obj_type.modified.isoformat()), 200
def test_decamelize(input_str, expected_output): """ :param input_str: String that will be transformed. :param expected_output: The expected transformation. """ output = humps.decamelize(input_str) assert output == expected_output, "%s != %s" % (output, expected_output)
def put_project() -> RespT: """Add or update project. --- put: tags: - Project description: Add or update project. requestBody: content: application/json: schema: $ref: Project responses: 200: description: Ok content: application/json: schema: type: string """ project = common.Project.from_dict(humps.decamelize(request.json)) project.modified = datetime.now(tz=timezone.utc) project.int_modified = None if project.id not in PROJECTS: project.created = project.modified PROJECTS[project.id] = project return jsonify(project.modified.isoformat())
def put_scene() -> RespT: """Add or update scene. --- put: tags: - Scene description: Add or update scene. requestBody: content: application/json: schema: $ref: Scene responses: 200: description: Ok content: application/json: schema: type: string format: date-time """ scene = common.Scene.from_dict(humps.decamelize(request.json)) scene.modified = datetime.now(tz=timezone.utc) scene.int_modified = None if scene.id not in SCENES: scene.created = scene.modified SCENES[scene.id] = scene return jsonify(scene.modified.isoformat())
def update_from_response_json(self, json_dict): json_decamelized = humps.decamelize(json_dict) _ = json_decamelized.pop("type") _ = json_decamelized.pop("href") _ = json_decamelized.pop("committime") self.__init__(**json_decamelized) return self
def query_capture_service(method, path, valid_if, params=None, json=None, data=None): # Make the request try: response = requests.request( method, f"{settings.BACKEND_API}{path}", params=params, json=humps.camelize(json) if json else None, data=humps.camelize(data) if data else None, timeout=10, allow_redirects=False) except requests.exceptions.RequestException as e: raise CaptureServiceException( f"Communication with the capture service failed: {e}") from e # Validate the response try: data = humps.decamelize(safe_get_response_json(response)) assert valid_if(response.status_code, data) except AssertionError: raise CaptureServiceException(f"{response.status_code}: {data}") return response, data
def to_json( self, camel_case: bool = True, pretty_print: bool = True, drop_nulls: bool = False, ) -> str: """ Convert an implementing instance to JSON. Parameters ---------- camel_case : bool (default True) If True, the keys of the returned dict will be camel-cased. pretty_print : bool (default True) If True, JSON will be formatted prior to being returned. drop_nulls : bool (default False) If True, keys with values of `None` will be dropped from the returned JSON output. """ d = (self.schema().dump(self) if camel_case else { humps.decamelize(k): v for k, v in self.schema().dump(self).items() }) if drop_nulls: d = _drop_nulls(d) s = json.dumps(d, indent=4 if pretty_print else None) return s
def get(self, id: int) -> User: return DataclassWrapper.wrap( User, humps.decamelize( self.execute("get", "/".join(["scim", "Users", str(id)]))), )
async def get_devices(self, refresh: bool = False) -> list[Device]: """ List devices """ if self.devices and not refresh: return self.devices response = await self.__get("setup/devices") if self.is_local: new_response = [] for device in response: device.update({ 'deviceurl': device['deviceURL'], 'uiClass': device['definition']['uiClass'] }) new_response.append(device) response = new_response devices = [Device(**d) for d in humps.decamelize(response)] self.devices = devices return devices
def get_default_params(cls): """ Returns: dict: the default parameters to be used in music, user, and challenge """ return decamelize(cls._params)
def sync(self, client, startdate=None): result = [] for group in Groups().get_all_groups(client): group_id = group.get('id') for conversation in Conversations().get_conversations_for_group( client, group_id=group_id): conversation_id = conversation.get('id') for thread in ConversationThreads().get_threads_for_group( client, group_id=group_id, conversation_id=conversation_id): thread_id = thread.get('id') posts = client.get_all_resources( self.version, self.endpoint.format(group_id=group_id, conversation_id=conversation_id, thread_id=thread_id)) for post in posts: post['thread_id'] = thread_id post['conversation_id'] = conversation_id post['group_id'] = group_id result.extend(posts) yield humps.decamelize(result)
def sync(self, client, startdate=None): results = [] for group in Groups().get_all_groups(client): group_id = group.get('id') for channel in Channels().get_all_channels_for_group( client, group_id=group_id): channel_id = channel.get('id') for message in ChannelMessages( client).get_messages_for_group_channel( client, group_id=group_id, channel_id=channel_id, startdate=startdate): message_id = message.get('id') replies = client.get_all_resources( self.version, self.endpoint.format(group_id=group_id, channel_id=channel_id, message_id=message_id)) results.extend(replies) yield humps.decamelize(results)
def sync(self, client, startdate=None): resources = client.get_all_resources(self.version, self.endpoint, top=self.top, orderby=self.orderby) yield humps.decamelize(resources)
def serialize_candidate_result(raw_result): if len(raw_result["hits"]["hits"]) == 0: return {} record = raw_result["hits"]["hits"][0]["_source"] return humps.decamelize(record["candidate"])
def restructure_data(xml_str): data_dict = humps.decamelize(xmltodict.parse(xml_str.strip())) publication_src = data_dict['amsterdam_travel_times'][ 'payload_publication'] measurements = [] if 'site_measurements' in publication_src: if type(publication_src['site_measurements']) is list: measurements = [ measurement_src_to_dict(d) for d in publication_src['site_measurements'] ] else: measurements = [ measurement_src_to_dict(publication_src['site_measurements']) ] return { 'publication_type': publication_src['@type'], 'publication_reference_id': publication_src['publication_reference']['@id'], 'publication_reference_version': publication_src['publication_reference']['@version'], 'publication_time': publication_src['publication_time'], 'measurement_start_time': publication_src['measurement_period']['measurement_start_time'], 'measurement_end_time': publication_src['measurement_period']['measurement_end_time'], 'measurements': measurements, }
def _unpack_schedule(schedule): type = humps.decamelize(schedule["type"]) time = schedule["time"] if type != "interval" else None date = schedule["date"] if type == "on_date" else None time_interval = schedule["timeInterval"] if type == "interval" else None weekdays = schedule["weekdays"] if type == "weekly" else None return type, time, date, weekdays, time_interval
def from_response_json(cls, json_dict): json_decamelized = humps.decamelize(json_dict) if json_decamelized["count"] >= 1: return [ cls(**commit_dto) for commit_dto in json_decamelized["items"] ] return cls(**json_decamelized)
def replace(self, id: int, params: ReplaceUserInputParams) -> User: return DataclassWrapper.wrap( User, humps.decamelize( self.execute("put", "/".join(["scim", "Users", id]), params.to_dict())), )
def put_sphere() -> RespT: """Add or update collision sphere. --- put: tags: - Collisions description: Add or update collision sphere. parameters: - name: sphereId in: query schema: type: string - name: radius in: query schema: type: number format: float requestBody: content: application/json: schema: $ref: Pose responses: 200: description: Ok content: application/json: schema: type: string """ args = humps.decamelize(request.args.to_dict()) sphere = object_type.Sphere(args["sphere_id"], float(args["radius"])) collision_objects[sphere.id] = sphere return jsonify("ok"), 200
def test_camelized_acronyms(input_str, expected_output): """ Validate decamelizing acronyms works as expected. :type input_str: str :type expected_output: str """ assert humps.decamelize(input_str) == expected_output
def convert_json_to_go(obj: dict, keys_to_keep: dict): """ Convert dict representation to follow Terraform Go SDK rules. Does following changes: * keys should be converted from camelCase to underscore_case * dictionaries should be wrapped in lists (so {...} -> [{...}]), unless dict is empty, in which case that list should have no elements * dictionary values should be converted recursively """ if hasattr(obj, 'as_dict'): obj = obj.as_dict() result = {} for key, value in obj.items(): key = decamelize(key).replace('__', '_') if key not in keys_to_keep: continue if hasattr(value, 'as_dict'): value = value.as_dict() nested = isinstance(keys_to_keep[key], dict) if nested: if isinstance(value, dict): value = [convert_json_to_go(value, keys_to_keep[key]) ] if value else [] elif isinstance(value, list) and len(value) > 0: assert isinstance(keys_to_keep[key], dict) value = [ convert_json_to_go(x, keys_to_keep[key]) for x in value ] result[key] = value return result
def from_response_json(cls, json_dict): json_decamelized = humps.decamelize(json_dict) feature_group_json = json_decamelized["left_feature_group"] feature_group_obj = ( feature_group.OnDemandFeatureGroup.from_response_json(feature_group_json) if "storage_connector" in feature_group_json else feature_group.FeatureGroup.from_response_json(feature_group_json) ) return cls( left_feature_group=feature_group_obj, left_features=json_decamelized["left_features"], feature_store_name=json_decamelized.get("feature_store_name", None), feature_store_id=json_decamelized.get("feature_store_id", None), left_feature_group_start_time=json_decamelized.get( "left_feature_group_start_time", None ), left_feature_group_end_time=json_decamelized.get( "left_feature_group_end_time", None ), joins=[ join.Join.from_response_json(_join) for _join in json_decamelized.get("joins", []) ], filter=json_decamelized.get("filter", None), )
def from_response_json(cls, json_dict): json_decamelized = humps.decamelize(json_dict) # Currently getting multiple commits at the same time is not allowed if json_decamelized["count"] == 0: return None elif len(json_decamelized["items"]) == 1: return cls(**json_decamelized["items"][0])