def _build_py_value(data_value, data_def, impl=None): """" Converts a data value to python native value impl input is required to create Struct class instances :type data_value: :class:`vmware.vapi.data.value.DataValue` :param data_value: Input data value :type data_def: :class:`vmware.vapi.data.definition.DataDefinition` :param data_def: Data definition :type impl: :class:`vmware.vapi.bindings.stub.VapiInterface` or :class:`vmware.vapi.bindings.skeleton.VapiInterface` or ``None`` :param impl: Python generated class to resolve structure classes :rtype: :class:`object` :return: Native python value """ if data_def.type == Type.OPTIONAL: if data_value.is_set(): element_def = data_def.element_type element_value = data_value.value py_value = _build_py_value(element_value, element_def, impl) else: py_value = None elif data_def.type == Type.LIST: py_value = [] element_def = data_def.element_type for element_value in data_value: py_value.append(_build_py_value(element_value, element_def, impl)) elif data_def.type in (Type.STRUCTURE, Type.ERROR): struct_name = data_def.name class_name = Converter.underscore_to_capwords(struct_name) py_value = {} for field in data_def.get_field_names(): field_def = data_def.get_field(field) field_val = data_value.get_field(field) py_value[field] = _build_py_value(field_val, field_def, impl) if impl and hasattr(impl, class_name): kwargs = _pepify_args(py_value) py_value = getattr(impl, class_name)(**kwargs) elif data_def.type == Type.VOID: py_value = None # For opaque value, no need of any conversion elif data_def.type == Type.OPAQUE: py_value = data_value # Primitive type Integer/Double/String/Boolean/Secret else: py_type = _py_type_map.get(data_def.type) py_value = py_type(data_value.value) return py_value
def create_stub(self, service_name): """ Create a stub corresponding to the specified service name :type service_name: :class:`str` :param service_name: Name of the service :rtype: :class:`VapiInterface` :return: The stub correspoding to the specified service name """ path_split = service_name.split('.') module_name = '%s_client' % '.'.join(path_split[:-1]) class_name = Converter.underscore_to_capwords(path_split[-1]) module = __import__(module_name, globals(), locals(), class_name) cls = getattr(module, class_name) return cls(self._config)
def __getattr__(cls, name): """ Return a synthesized factory method if the given name matches the name of one of the standard error types (after converting from lower-case-with-underscores to cap-words). """ if name.startswith(cls.METHOD_PREFIX) and name is not 'new_vapi_error': canonical_error_type_name = name[cls.METHOD_PREFIX_LENGTH:] capwords_error_type_name = Converter.underscore_to_capwords( canonical_error_type_name) error_type = getattr(errors_provider, capwords_error_type_name) if (isinstance(error_type, type) and issubclass(error_type, VapiError)): def factory_method(**kwargs): """ Synthesized factory function for a standard error type. """ return error_type(**kwargs) return factory_method msg = "type object '%s' has no attribute '%s'" % (cls.__name__, name) raise AttributeError(msg)
def test_underscore_to_capwords(self): self.assertEqual(Converter.underscore_to_capwords('cap_words'), 'CapWords') self.assertEqual(Converter.underscore_to_capwords('CapWords'), 'CapWords')