Example #1
0
    def __init__(self, value):

        super(NumericSetWinnowValue, self).__init__(value)

        if isinstance(value, list) or isinstance(value, set):
            self.as_list = value
        else:
            as_list = [to_decimal(d) for d in value[u"value"]]
            if not (isinstance(as_list, list) or isinstance(as_list, set)):
                raise OptionsExceptionFailedValidation(
                    "NumericNumberSieveValue must be a Decimal")
            self.as_list = as_list

        for v in self.as_list:
            if not isinstance(v, Decimal):
                raise OptionsExceptionFailedValidation(
                    "NumericSetSieveValue all values must be Decimals")

        if len(self.as_list) == 0:
            raise OptionsExceptionFailedValidation(
                "NumericSetSieveValue: empty set")

        if len(self.as_list) == 1:
            raise OptionsExceptionFailedValidation(
                "NumericSetSieveValue: set with one value should have been cast into NumericNumberSieveValue"
            )
Example #2
0
 def set_list(self, l):
     if len(l) != 2:
         raise OptionsExceptionFailedValidation(
             "Bool list can only have two members")
     if not isinstance(l[0], bool):
         raise OptionsExceptionFailedValidation(
             "Bool list must have only true and false")
     if not isinstance(l[1], bool):
         raise OptionsExceptionFailedValidation(
             "Bool list must have only true and false")
     if l[0] == l[1]:
         raise OptionsExceptionFailedValidation(
             "Bool list must have only true and false")
     self.true = None
     self.undetermined = True
Example #3
0
    def __new__(cls, value):

        if isinstance(value, list) or isinstance(value, set):
            as_list = value
            if len(as_list) > MAX_VALUE_SET_SIZE:
                raise OptionsExceptionNotAllowed(
                    "maximum value set size exceeded")
            elif len(as_list) == 0:
                return None
            elif len(as_list) == 1:
                return NumericNumberWinnowValue(list(as_list)[0])
            else:
                return NumericWinnowValue.__new__(cls)
        else:
            as_list = value[u"value"]
            if not (isinstance(as_list, list) or isinstance(as_list, set)):
                raise OptionsExceptionFailedValidation(
                    "NumericNumberSieveValue must be a Decimal")
            if len(as_list) > MAX_VALUE_SET_SIZE:
                raise OptionsExceptionNotAllowed(
                    "maximum value set size exceeded")
            elif len(as_list) == 0:
                return None
            elif len(as_list) == 1:
                v = deepcopy(value)
                v[u"value"] = list(as_list)[0]
                return NumericNumberWinnowValue(v)
            else:
                return NumericWinnowValue.__new__(cls)
Example #4
0
    def intersection(self, other):

        self.check_class(other)
        intersects = False

        if type(other) in (NumericNumberWinnowValue, NumericSetWinnowValue):
            if self.number in other.possible_values():
                intersects = True
        elif type(other) == NumericStepWinnowValue:
            if self.number >= other.min and self.number <= other.max:
                if (self.number - other.start) % other.step == 0:
                    intersects = True
        elif type(other) == NumericRangeWinnowValue:
            if self.number >= other.min and self.number <= other.max:
                intersects = True
        else:
            raise OptionsExceptionFailedValidation(
                "intersection of NumericNumberSieveValue failed")

        if intersects:
            info = self.get_merged_info(other)
            info[u"value"] = self.number
            return NumericNumberWinnowValue(info)
        else:
            return None
Example #5
0
    def publish(cls, db, fileset_json):

        winnow.validate(fileset_json)
        path_elements = fileset_json["path"].split("/")

        if len(path_elements) != 6:
            raise OptionsExceptionFailedValidation(
                "Fileset path should have 5 elements %s" % path_elements)

        product_path = "/".join(path_elements[:-1])
        product = WinnowProduct.get_from_path(db, product_path)
        product_doc = product.get_doc()

        if product is None:
            raise OptionsExceptionReferenceError(
                "Reference Error: couldn't find %s" % product_path)

        version = product_doc["version"]
        kwargs = {
            "product_version":
            "%s@%s.%s.%s" %
            (product_doc["path"], version[0], version[1], version[2])
        }
        fileset = WinnowFileset.add_doc(db, fileset_json, kwargs=kwargs)

        # TODO check how this works now

        # if not product.allows(fileset):
        #     raise OptionsExceptionNotAllowed("fileset not allowed: %s" % utils.json_dumps(fileset_json))

        db.index_fileset(product_path, fileset.kwargs[u"uuid"])

        return fileset
Example #6
0
    def __init__(self, value):

        super(NumericStepWinnowValue, self).__init__(value)
        self.start = to_decimal(value[u"start"])
        self.step = to_decimal(value[u"step"])

        if self.first_value() is None:
            raise OptionsExceptionFailedValidation(
                "NumericStepSieveValue: empty set")
Example #7
0
    def __init__(self, value):

        super(NumericRangeWinnowValue, self).__init__(value)
        self.max = to_decimal(value[u"max"])
        self.min = to_decimal(value[u"min"])
        if self.max < self.min:
            raise OptionsExceptionFailedValidation(
                "NumericRangeSieveValue: max %s less than min %s" %
                (self.max, self.min))
Example #8
0
    def from_value(cls, value):
        if isinstance(value, list):
            try:
                decimal_list = [to_decimal(v) for v in value]
            except:
                raise OptionsExceptionFailedValidation(
                    "NumericSieveValue unrecognised value type")
            numeric = NumericSetWinnowValue(decimal_list)
            if numeric is None:
                raise OptionsExceptionFailedValidation(
                    "NumericSieveValue: empty set")
            return numeric

        elif isinstance(value, dict):

            numeric_type = value[u"type"]
            if numeric_type == VALUE_TYPE_NUMERIC_NUMBER:
                return NumericNumberWinnowValue(value)
            elif numeric_type == VALUE_TYPE_NUMERIC_SET:
                numeric = NumericSetWinnowValue(value)
                if numeric is None:
                    raise OptionsExceptionFailedValidation(
                        "NumericSieveValue: empty set")
                return numeric
            elif numeric_type == VALUE_TYPE_NUMERIC_RANGE:
                return NumericRangeWinnowValue(value)
            elif numeric_type == VALUE_TYPE_NUMERIC_STEP:
                step = NumericStepWinnowValue(value)
                first = step.first_value()
                if first is None:
                    raise OptionsExceptionFailedValidation(
                        "NumericSieveValue: empty set")
                elif first + step.step > step.max:
                    return NumericNumberWinnowValue(first)
                else:
                    return step
        else:
            try:
                d = to_decimal(value)
            except:
                raise OptionsExceptionFailedValidation(
                    "NumericSieveValue unrecognised value type")
            return NumericNumberWinnowValue(d)
Example #9
0
    def from_value(cls, value):

        if isinstance(value, list):
            try:
                string_list = [unicode(v) for v in value]
            except:
                raise OptionsExceptionFailedValidation(
                    "OptionStringSieveValue unrecognised value type")
            # numeric = NumericSetSieveValue.make(decimal_list)

            option = OptionStringWinnowValue(string_list)
            if option is None:
                raise OptionsExceptionFailedValidation(
                    "OptionSieveValue: empty set")
            return option

        elif isinstance(value, dict):
            option_type = value[u"type"]
            if option_type == VALUE_TYPE_SET_STRING:
                return OptionStringWinnowValue(value)
            elif option_type == VALUE_TYPE_SET_COLOUR:
                return OptionStringWinnowValue(value)
            elif option_type == VALUE_TYPE_SET_SIZE:
                return OptionStringWinnowValue(value)
            elif option_type == VALUE_TYPE_SET_RESOURCE:
                return OptionResourceWinnowValue(value)
            elif option_type == VALUE_TYPE_SET_NULL:
                return OptionNullWinnowValue(value)
            elif option_type == VALUE_TYPE_VALUE_STRING:
                return OptionStringWinnowValue(value)
            else:
                raise OptionsExceptionFailedValidation(
                    "OptionSieveValue unrecognised value type")
        elif value is None:
            return OptionNullWinnowValue(value)
        else:
            try:
                s = unicode(value)
            except:
                raise OptionsExceptionFailedValidation(
                    "OptionSieveValue unrecognised value type")

            return OptionStringWinnowValue(s)
Example #10
0
    def __init__(self, value):

        super(NumericNumberWinnowValue, self).__init__(value)

        if isinstance(value, Decimal):
            self.number = value
        else:
            number = to_decimal(value[u"value"])
            if not isinstance(number, Decimal):
                raise OptionsExceptionFailedValidation(
                    "NumericNumberSieveValue must be a Decimal")
            self.number = number
Example #11
0
    def __init__(self, value):

        super(BooleanWinnowValue, self).__init__(value)

        if isinstance(value, bool):
            self.true = value
            self.undetermined = False
        elif isinstance(value, list):
            self.set_list(value)
        elif isinstance(value, dict):
            v = value[u"value"]
            if isinstance(v, bool):
                self.true = v
                self.undetermined = False
            elif isinstance(v, list):
                self.set_list(v)
        else:
            raise OptionsExceptionFailedValidation(
                "BooleanWinnowValue bad type")
Example #12
0
def validate(doc):
    try:
        validation.validate(doc)
    except jsonschema.ValidationError, e:
        raise OptionsExceptionFailedValidation(e)
Example #13
0
def value_factory(input_value, key=None):
    #
    # print "***************"
    # print "key", key
    # print "input_value", utils.json_dumps(input_value)

    value = input_value if key is None else value_with_key(input_value, key)

    cls = None
    if isinstance(value, dict):
        if value.has_key(u"path"):
            cls = OptionResourceWinnowValue
            value = {u"type": VALUE_TYPE_SET_RESOURCE, u"values": [value]}
        else:
            cls = VALUE_TYPES[value["type"]]

    elif isinstance(value, bool):
        cls = BooleanWinnowValue

    elif isinstance(value, int) or isinstance(value, Decimal) or isinstance(
            value, float):
        cls = NumericWinnowValue

    elif isinstance(value, unicode):
        if value.startswith("$ref:"):
            raise OptionsExceptionFailedValidation(
                "this ref has not been derefferenced before merging %s" %
                value)
        cls = OptionWinnowValue

    elif isinstance(value, str):
        raise OptionsExceptionFailedValidation(
            "the value %s is a string rather than unicode", value)

    elif value is None:
        cls = OptionNullWinnowValue

    elif isinstance(value, list):
        v = value[0]
        if isinstance(v, int) or isinstance(v, Decimal) or isinstance(
                v, float):
            cls = NumericWinnowValue
        if isinstance(v, unicode):
            cls = OptionWinnowValue
        if isinstance(v, bool):
            cls = BooleanWinnowValue
        if isinstance(v, dict):

            if v.has_key(u"path"):
                cls = OptionResourceWinnowValue
                value = {u"type": VALUE_TYPE_SET_RESOURCE, u"values": value}
            else:
                if v["type"] == VALUE_TYPE_VALUE_STRING:
                    cls = OptionWinnowValue
                else:
                    cls = VALUE_TYPES[v["type"]]
    else:
        pass

    if cls:
        result = cls.from_value(value)

        # print "result class", result.__class__

        return result

    return None