Esempio n. 1
0
    def create_from_name_and_dictionary(self, name, datas):
        """Return a populated object Parameter from dictionary datas
        """
        parameter = ObjectParameter()
        self.set_common_datas(parameter, name, datas)

        if "optional" in datas:
            parameter.optional = to_bool(datas["optional"])
        if "type" in datas:
            parameter.type = str(datas["type"])
        if "generic" in datas:
            parameter.generic = to_bool(datas["generic"])

        return parameter
Esempio n. 2
0
    def create_from_name_and_dictionary(self, name, datas):
        """Return a populated object Parameter from dictionary datas
        """
        parameter = ObjectParameter()
        self.set_common_datas(parameter, name, datas)

        if "optional" in datas:
            parameter.optional = to_bool(datas["optional"])
        if "type" in datas:
            parameter.type = str(datas["type"])
        if "generic" in datas:
            parameter.generic = to_bool(datas["generic"])

        return parameter
Esempio n. 3
0
    def create_from_dictionary(self, datas):
        """Return a populated object ResponseCode from dictionary datas
        """
        if "code" not in datas:
            raise ValueError("A response code must contain a code in \"%s\"." % repr(datas))

        code = ObjectResponseCode()
        self.set_common_datas(code, str(datas["code"]), datas)

        code.code = int(datas["code"])
        if "message" in datas:
            code.message = str(datas["message"])
        elif code.code in self.default_messages.keys():
            code.message = self.default_messages[code.code]
        if "generic" in datas:
            code.generic = to_bool(datas["generic"])

        return code
Esempio n. 4
0
    def set_common_datas(self, element, name, datas):
        """Populated common data for an element from dictionnary datas
        """
        element.name = str(name)
        if "description" in datas:
            element.description = str(datas["description"]).strip()

        if isinstance(element, Sampleable) and element.sample is None and "sample" in datas:
            element.sample = str(datas["sample"]).strip()

        if isinstance(element, Displayable):
            if "display" in datas:
                element.display = to_bool(datas["display"])

            if "label" in datas:
                element.label = datas["label"]
            else:
                element.label = element.name
Esempio n. 5
0
    def create_from_dictionary(self, datas):
        """Return a populated object ResponseCode from dictionary datas
        """
        if "code" not in datas:
            raise ValueError("A response code must contain a code in \"%s\"." %
                             repr(datas))

        code = ObjectResponseCode()
        self.set_common_datas(code, str(datas["code"]), datas)

        code.code = int(datas["code"])
        if "message" in datas:
            code.message = str(datas["message"])
        elif code.code in self.default_messages.keys():
            code.message = self.default_messages[code.code]
        if "generic" in datas:
            code.generic = to_bool(datas["generic"])

        return code
Esempio n. 6
0
 def merge_extends(self,
                   target,
                   extends,
                   inherit_key="inherit",
                   inherit=False):
     """Merge extended dicts
     """
     if isinstance(target, dict):
         if inherit and inherit_key in target and not to_bool(
                 target[inherit_key]):
             return
         if not isinstance(extends, dict):
             raise ValueError("Unable to merge: Dictionnary expected")
         for key in extends:
             if key not in target:
                 target[str(key)] = extends[key]
             else:
                 self.merge_extends(target[key], extends[key], inherit_key,
                                    True)
     elif isinstance(target, list):
         if not isinstance(extends, list):
             raise ValueError("Unable to merge: List expected")
         target += extends
Esempio n. 7
0
    def delete_removed(self, datas):
        """Delete sub properties flagged as removed
        """
        if isinstance(datas, dict):
            if self.removed_key in datas and to_bool(datas[self.removed_key]):
                return None

            new_datas = {}
            for key in datas:
                cleaned = self.delete_removed(datas[key])
                if cleaned is not None:
                    new_datas[key] = cleaned

            return new_datas
        elif isinstance(datas, list):
            new_datas = []
            for item in datas:
                cleaned = self.delete_removed(item)
                if cleaned is not None:
                    new_datas.append(cleaned)
            return new_datas
        else:
            return datas
Esempio n. 8
0
    def delete_removed(self, datas):
        """Delete sub properties flagged as removed
        """
        if isinstance(datas, dict):
            if self.removed_key in datas and to_bool(datas[self.removed_key]):
                return None

            new_datas = {}
            for key in datas:
                cleaned = self.delete_removed(datas[key])
                if cleaned is not None:
                    new_datas[key] = cleaned

            return new_datas
        elif isinstance(datas, list):
            new_datas = []
            for item in datas:
                cleaned = self.delete_removed(item)
                if cleaned is not None:
                    new_datas.append(cleaned)
            return new_datas
        else:
            return datas
Esempio n. 9
0
    def create_from_name_and_dictionary(self, name, datas):
        """Return a populated object Object from dictionary datas
        """
        if "type" not in datas:
            raise ValueError("Missing type in object \"%s\"  \"%s\"." % (name, repr(datas)))

        str_type = str(datas["type"]).lower()
        if not str_type in ObjectRaw.Types:
            type = ObjectRaw.Types("type")
        else:
            type = ObjectRaw.Types(str_type)

        if type is ObjectRaw.Types.object:
            object = ObjectObject()
            object.properties = self.create_dictionary_of_element_from_dictionary("properties", datas)
        elif type is ObjectRaw.Types.array:
            object = ObjectArray()
            if "items" in datas:
                object.items = self.create_from_name_and_dictionary("items", datas["items"])
            if "sample_count" in datas:
                object.sample_count = int(datas["sample_count"])
        elif type is ObjectRaw.Types.number:
            object = ObjectNumber()
        elif type is ObjectRaw.Types.string:
            object = ObjectString()
        elif type is ObjectRaw.Types.bool:
            object = ObjectBool()
            if "sample" in datas:
                object.sample = to_bool(datas["sample"])
        elif type is ObjectRaw.Types.reference:
            object = ObjectReference()
            if "reference" in datas:
                object.reference_name = str(datas["reference"])
        elif type is ObjectRaw.Types.type:
            object = ObjectType()
            object.type_name = str(datas["type"])
        elif type is ObjectRaw.Types.none:
            object = ObjectNone()
        elif type is ObjectRaw.Types.dynamic:
            object = ObjectDynamic()
            if "items" in datas:
                object.items = self.create_from_name_and_dictionary("items", datas["items"])
            if "sample" in datas:
                if isinstance(datas["sample"], dict):
                    object.sample = {}
                    for k, v in datas["sample"].items():
                        object.sample[str(k)] = str(v)
                else:
                    raise ValueError("A dictionnary is expected for dynamic\s object in \"%s\"" % name)
        elif type is ObjectRaw.Types.const:
            object = ObjectConst()
            if "const_type" in datas:
                const_type = str(datas["const_type"])
                if not const_type in ObjectConst.Types:
                    raise ValueError("Const type \"%s\" unknwon" % const_type)
            else:
                const_type = ObjectConst.Types.string
            object.const_type = const_type
            if not "value" in datas:
                raise ValueError("Missing const value")
            object.value = datas["value"]
        elif type is ObjectRaw.Types.enum:
            object = ObjectEnum()
            if not "values" in datas or not isinstance(datas['values'], list):
                raise ValueError("Missing enum values")
            object.values = [str(value) for value in datas["values"]]
            if "descriptions" in datas and isinstance(datas['descriptions'], dict):
                for (value_name, value_description) in datas["descriptions"].items():
                    value = EnumValue()
                    value.name = value_name
                    value.description = value_description
                    object.descriptions.append(value)

            descriptions = [description.name for description in object.descriptions]
            for value_name in [value for value in object.values if value not in descriptions]:
                value = EnumValue()
                value.name = value_name
                object.descriptions.append(value)
        else:
            object = ObjectRaw()

        self.set_common_datas(object, name, datas)
        object.type = type

        if "optional" in datas:
            object.optional = to_bool(datas["optional"])

        return object
Esempio n. 10
0
    def create_from_name_and_dictionary(self, name, datas):
        """Return a populated object Object from dictionary datas
        """
        if "type" not in datas:
            raise ValueError("Missing type in object \"%s\"  \"%s\"." %
                             (name, repr(datas)))

        str_type = str(datas["type"]).lower()
        if not str_type in ObjectRaw.Types:
            type = ObjectRaw.Types("type")
        else:
            type = ObjectRaw.Types(str_type)

        if type is ObjectRaw.Types.object:
            object = ObjectObject()
            object.properties = self.create_dictionary_of_element_from_dictionary(
                "properties", datas)
        elif type is ObjectRaw.Types.array:
            object = ObjectArray()
            if "items" in datas:
                object.items = self.create_from_name_and_dictionary(
                    "items", datas["items"])
            if "sample_count" in datas:
                object.sample_count = int(datas["sample_count"])
        elif type is ObjectRaw.Types.number:
            object = ObjectNumber()
        elif type is ObjectRaw.Types.string:
            object = ObjectString()
        elif type is ObjectRaw.Types.bool:
            object = ObjectBool()
            if "sample" in datas:
                object.sample = to_bool(datas["sample"])
        elif type is ObjectRaw.Types.reference:
            object = ObjectReference()
            if "reference" in datas:
                object.reference_name = str(datas["reference"])
        elif type is ObjectRaw.Types.type:
            object = ObjectType()
            object.type_name = str(datas["type"])
        elif type is ObjectRaw.Types.none:
            object = ObjectNone()
        elif type is ObjectRaw.Types.dynamic:
            object = ObjectDynamic()
            if "items" in datas:
                object.items = self.create_from_name_and_dictionary(
                    "items", datas["items"])
            if "sample" in datas:
                if isinstance(datas["sample"], dict):
                    object.sample = {}
                    for k, v in datas["sample"].items():
                        object.sample[str(k)] = str(v)
                else:
                    raise ValueError(
                        "A dictionnary is expected for dynamic\s object in \"%s\""
                        % name)
        elif type is ObjectRaw.Types.const:
            object = ObjectConst()
            if "const_type" in datas:
                const_type = str(datas["const_type"])
                if not const_type in ObjectConst.Types:
                    raise ValueError("Const type \"%s\" unknwon" % const_type)
            else:
                const_type = ObjectConst.Types.string
            object.const_type = const_type
            if not "value" in datas:
                raise ValueError("Missing const value")
            object.value = datas["value"]
        elif type is ObjectRaw.Types.enum:
            object = ObjectEnum()
            if not "values" in datas or not isinstance(datas['values'], list):
                raise ValueError("Missing enum values")
            object.values = [str(value) for value in datas["values"]]
            if "descriptions" in datas and isinstance(datas['descriptions'],
                                                      dict):
                for (value_name,
                     value_description) in datas["descriptions"].items():
                    value = EnumValue()
                    value.name = value_name
                    value.description = value_description
                    object.descriptions.append(value)

            descriptions = [
                description.name for description in object.descriptions
            ]
            for value_name in [
                    value for value in object.values
                    if value not in descriptions
            ]:
                value = EnumValue()
                value.name = value_name
                object.descriptions.append(value)
        else:
            object = ObjectRaw()

        self.set_common_datas(object, name, datas)
        object.type = type

        if "optional" in datas:
            object.optional = to_bool(datas["optional"])

        return object