def as_array(obj): if hasattr(obj, "to_array"): return obj.to_array() elif isinstance(obj, (list, tuple)): return [as_array(x) for x in obj] elif isinstance(obj, dict): array = {} for key in obj.keys(): array[key] = as_array(obj[key]) # end for return array else: _json_dumps(obj) # raises error if is wrong json return obj
def as_array(obj): """ Creates an json-like representation of a variable, supporting types with a `.to_array()` function. :rtype: dict|list|str|int|float|bool|None """ if hasattr(obj, "to_array"): return obj.to_array() elif isinstance(obj, (list, tuple)): return [as_array(x) for x in obj] elif isinstance(obj, dict): return {key: as_array(obj[key]) for key in obj.keys()} else: _json_dumps(obj) # raises error if is wrong json return obj
def dump(filename, worflow_tests_config, file_format=FileFormats.YAML): """ Write the configuration of a workflow test suite to a YAML or JSON file. :type filename: str :param filename: the absolute path of the YAML or JSON configuration file :type worflow_tests_config: dict or list :param worflow_tests_config: a dictionary which maps a workflow test name to the corresponding configuration (:class:`WorkflowTestCase`) or a list of :class:`WorkflowTestCase` instances :type file_format: str :param file_format: ``YAML`` or ``JSON`` """ workflows = {} config = worflow_tests_config.copy() if isinstance( worflow_tests_config, dict) else {} config["workflows"] = workflows if isinstance(worflow_tests_config, dict): worflow_tests_config = worflow_tests_config["workflows"].values() elif not isinstance(worflow_tests_config, list): raise ValueError( "'workflow_tests_config' must be a configuration dict " "or a list of 'WorkflowTestCase' instances") for worlflow in worflow_tests_config: workflows[worlflow.name] = worlflow.to_dict() with open(filename, "w") as f: if FileFormats.is_yaml(file_format): _yaml_dump(config, f) else: f.write(_json_dumps(config, indent=2)) return config
def _serialize(self, data, content_type): if data is None: return None, {} if content_type is None: data = _json_dumps(data) content_type = 'application/json' return data, {'Content-Type': content_type}
def _init_policy(self): """ Initialize IAM policy. This policy allow instance to: - Load FPGA bitstream. - Access to S3 buckets objects for read and write. Returns: str: 'policy' """ # Create a policy with _exception_handler(filter_error_codes='EntityAlreadyExists'): self._iam_client.create_policy(PolicyName=self._policy, PolicyDocument=_json_dumps( self.POLICY_DOCUMENT)) _get_logger().debug( _utl.gen_msg('created_named', 'policy', self._policy)) with _exception_handler(): response = self._iam_client.list_policies(Scope='Local', OnlyAttached=False, MaxItems=100) for policy_item in response['Policies']: if policy_item['PolicyName'] == self._policy: self._policy_arn = policy_item['Arn'] # 'policy' returns str is used set future object ID return 'policy' raise _exc.HostConfigurationException(gen_msg=('created_failed_named', 'IAM policy', self._policy))
def _init_role(self): """ Initialize IAM role. This role allow to perform actions defined by policy. """ with _exception_handler(filter_error_codes='EntityAlreadyExists'): role = self._iam_resource.create_role( RoleName=self._role, AssumeRolePolicyDocument=_json_dumps( self.ASSUME_ROLE_POLICY_DOCUMENT), Description=_utl.gen_msg('accelize_generated')) _get_logger().debug(_utl.gen_msg('created_named', 'IAM role', role))
def json_dumps(obj): return _json_dumps(obj).encode('utf-8')
def json_dumps(obj): return _json_dumps(obj).encode("utf-8")