Exemple #1
0
    def produce_contexts(self, msg):
        """Produce contexts based on incoming message.

        :param msg: Parsed request in this format: `[IN_REQUEST, body, header]`
        """

        if not isinstance(msg, list):
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(msg, "Request must be a list")

        if not len(msg) >= 2:
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(len(msg), "Request must have at least two "
                                                          "elements. It has %r")

        if not isinstance(msg[0], int):
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(msg[0], "Request version must be an integer. "
                                                                    "It was %r")

        processor = self._version_map.get(msg[0], None)
        if processor is None:
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(msg[0], "Unknown request type %r")

        initial_ctx = processor(self, msg)
        contexts = self.generate_contexts(initial_ctx)

        p_ctx, others = contexts[0], contexts[1:]

        return p_ctx, others
Exemple #2
0
    def _from_dict_value(self, key, cls, inst, validator):
        if validator is self.SOFT_VALIDATION:
            self.validate(key, cls, inst)

        if issubclass(cls, AnyDict):
            return inst

        # get native type
        if issubclass(cls, File) and isinstance(inst, self.complex_as):
            retval = self._doc_to_object(cls.Attributes.type, inst, validator)

        elif issubclass(cls, ComplexModelBase):
            retval = self._doc_to_object(cls, inst, validator)

        else:
            if cls.Attributes.empty_is_none and inst in (u'', b''):
                inst = None

            if (validator is self.SOFT_VALIDATION
                                and isinstance(inst, six.string_types)
                                and not cls.validate_string(cls, inst)):
                raise ValidationError((key, inst))

            if issubclass(cls, (ByteArray, File)):
                retval = self.from_string(cls, inst, self.binary_encoding)
            else:
                retval = self.from_string(cls, inst)

        # validate native type
        if validator is self.SOFT_VALIDATION and \
                                           not cls.validate_native(cls, retval):
            raise ValidationError((key, retval))

        return retval
Exemple #3
0
    def from_element(self, ctx, cls, element):
        if bool(element.get('{%s}nil' % _ns_xsi)):
            if self.validator is self.SOFT_VALIDATION and not \
                                                        cls.Attributes.nillable:
                raise ValidationError('')
            return cls.Attributes.default

        # if present, use the xsi:type="ns0:ObjectName"
        # attribute to instantiate subclass objects
        if self.parse_xsi_type:
            xsi_type = element.get('{%s}type' % _ns_xsi, None)
            if xsi_type is not None:
                if ":" in xsi_type:
                    prefix, objtype = xsi_type.split(':', 1)
                else:
                    prefix, objtype = None, xsi_type

                ns = element.nsmap.get(prefix)
                if ns is not None:
                    classkey = "{%s}%s" % (ns, objtype)
                else:
                    raise ValidationError(xsi_type)

                newclass = ctx.app.interface.classes.get(classkey, None)
                if newclass is None:
                    raise ValidationError(xsi_type)

                cls = newclass
                logger.debug("xsi:type overrides %r to %r", cls, newclass)

        handler = self.deserialization_handlers[cls]
        return handler(ctx, cls, element)
Exemple #4
0
    def _from_dict_value(self, key, class_, value, validator):
        if validator is self.SOFT_VALIDATION:
            self.validate(key, class_, value)

        if issubclass(class_, AnyDict):
            return value

        # get native type
        if issubclass(class_, File) and isinstance(value, self.complex_as):
            retval = self._doc_to_object(File.Value, value, validator)

        elif issubclass(class_, ComplexModelBase):
            retval = self._doc_to_object(class_, value, validator)

        else:
            if value == '' and class_.Attributes.empty_is_none:
                value = None

            if (validator is self.SOFT_VALIDATION
                    and isinstance(value, six.string_types)
                    and not class_.validate_string(class_, value)):
                raise ValidationError((key, value))

            if issubclass(class_, (ByteArray, File)):
                retval = self.from_string(class_, value, self.binary_encoding)
            else:
                retval = self.from_string(class_, value)

        # validate native type
        if validator is self.SOFT_VALIDATION and \
                                     not class_.validate_native(class_, retval):
            raise ValidationError((key, retval))

        return retval
Exemple #5
0
    def _from_dict_value(cls, class_, value, validator):
        # validate raw input
        if validator is cls.SOFT_VALIDATION:
            if issubclass(class_, Unicode) and not isinstance(value, basestring):
                raise ValidationError(value)
            if issubclass(class_, Unicode) and not isinstance(value, unicode):
                # Note that String is a subclass of Unicode
                if not (issubclass(class_, String) and isinstance(value, str)):
                    value = ProtocolBase.from_string(class_, value)

            elif issubclass(class_, Decimal) and not isinstance(value,
                                                            (int, long, float)):
                raise ValidationError(value)

            elif issubclass(class_, DateTime) and not (
                                isinstance(value, unicode) and
                                         class_.validate_string(class_, value)):
                raise ValidationError(value)

        # get native type
        if issubclass(class_, ComplexModelBase):
            retval = cls._doc_to_object(class_, value, validator)

        elif issubclass(class_, DateTime):
            retval = ProtocolBase.from_string(class_, value)

        else:
            retval = value

        # validate native type
        if validator is cls.SOFT_VALIDATION and \
                                     not class_.validate_native(class_, retval):
            raise ValidationError(retval)

        return retval
Exemple #6
0
        def process(value):
            if value is not None:
                if value.data is not None:
                    value.path = uuid1().get_hex()
                    fp = join(self.store, value.path)
                    if not abspath(fp).startswith(self.store):
                        raise ValidationError(
                            value.path, "Path %r contains "
                            "relative path operators (e.g. '..')")

                    with open(fp, 'wb') as file:
                        for d in value.data:
                            file.write(d)

                elif value.handle is not None:
                    value.path = uuid1().hex
                    fp = join(self.store, value.path)
                    if not abspath(fp).startswith(self.store):
                        raise ValidationError(
                            value.path, "Path %r contains "
                            "relative path operators (e.g. '..')")

                    data = mmap(value.handle.fileno(), 0)  # 0 = whole file
                    with open(fp, 'wb') as out_file:
                        out_file.write(data)
                        data.close()

                elif value.path is not None:
                    in_file_path = value.path

                    if not isfile(in_file_path):
                        logger.error("File path in %r not found" % value)

                    if dirname(abspath(in_file_path)) != self.store:
                        dest = join(self.store, uuid1().get_hex())

                        if value.move:
                            shutil.move(in_file_path, dest)
                            print("move", in_file_path, dest)
                        else:
                            shutil.copy(in_file_path, dest)

                        value.path = basename(dest)
                        value.abspath = dest

                else:
                    raise ValueError("Invalid file object passed in. All of "
                                     ".data, .handle and .path are None.")

                value.store = self.store
                value.abspath = join(self.store, value.path)

                retval = self.get_object_as_json(
                    value,
                    self.cls,
                    ignore_wrappers=self.ignore_wrappers,
                    complex_as=self.complex_as,
                )

                return retval
Exemple #7
0
    def decompose_incoming_envelope(self, ctx, message=JsonDocument.REQUEST):
        indoc = ctx.in_document
        if not isinstance(indoc, dict):
            raise ValidationError(indoc, "Invalid Request")

        ver = indoc.get(self.VERSION)
        if ver is None:
            raise ValidationError(ver, "Unknown Version")

        body = indoc.get(self.BODY)
        err = indoc.get(self.FAULT)
        if body is None and err is None:
            raise ValidationError((body, err), "Request data not found")

        ctx.protocol.error = False
        if err is not None:
            ctx.in_body_doc = err
            ctx.protocol.error = True
        else:
            if not isinstance(body, dict):
                raise ValidationError(body, "Request body not found")
            if not len(body) == 1:
                raise ValidationError(body, "Need len(body) == 1")

            ctx.in_header_doc = indoc.get(self.HEAD)
            if not isinstance(ctx.in_header_doc, list):
                ctx.in_header_doc = [ctx.in_header_doc]

            (ctx.method_request_string,ctx.in_body_doc), = body.items()
Exemple #8
0
    def _from_dict_value(self, class_, value, validator):
        if validator is self.SOFT_VALIDATION:
            self.validate(class_, value)

        if issubclass(class_, AnyDict):
            return value

        # get native type
        if issubclass(class_, ComplexModelBase):
            retval = self._doc_to_object(class_, value, validator)

        else:
            if (validator is self.SOFT_VALIDATION
                                and isinstance(value, basestring)
                                and not class_.validate_string(class_, value)):
                raise ValidationError(value)

            if issubclass(class_, (ByteArray, file)):
                retval = self.from_string(class_, value,
                                                   self.default_binary_encoding)

            else:
                retval = self.from_string(class_, value)

        # validate native type
        if validator is self.SOFT_VALIDATION and \
                                     not class_.validate_native(class_, retval):
            raise ValidationError(retval)

        return retval
Exemple #9
0
    def produce_contexts(self, msg):
        """msg = [IN_REQUEST, body, header]"""

        if not isinstance(msg, list):
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(msg, "Request must be a list")

        if not len(msg) >= 2:
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(msg,
                                  "Request must have at least two elements.")

        if not isinstance(msg[0], int):
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(msg[0],
                                  "Request version must be an integer.")

        processor = self._version_map.get(msg[0], None)
        if processor is None:
            logger.debug("Incoming request: %r", msg)
            raise ValidationError(msg[0], "Unknown request type")

        initial_ctx = processor(self, msg)
        contexts = self.generate_contexts(initial_ctx)

        return contexts[0], contexts[1:]
Exemple #10
0
    def from_string(cls, string):
        if cls.Attributes.max_str_len is not None and len(
                string) > cls.Attributes.max_str_len:
            raise ValidationError(string, 'string too long.')

        try:
            return decimal.Decimal(string)
        except decimal.InvalidOperation, e:
            raise ValidationError(string)
Exemple #11
0
    def _to_native_values(self, cls, member, orig_k, k, v, req_enc, validator):
        value = []

        for v2 in v:
            # some wsgi implementations pass unicode strings, some pass str
            # strings. we get unicode here when we can and should.
            if v2 is not None and req_enc is not None \
                                    and not issubclass(member.type, String) \
                                    and issubclass(member.type, Unicode) \
                                    and not isinstance(v2, six.text_type):
                try:
                    v2 = v2.decode(req_enc)
                except UnicodeDecodeError as e:
                    raise ValidationError(v2, "%r while decoding %%r" % e)

            # validate raw data (before deserialization)
            try:
                if (validator is self.SOFT_VALIDATION
                        and not member.type.validate_string(member.type, v2)):
                    raise ValidationError([orig_k, v2])

            except TypeError:
                raise ValidationError([orig_k, v2])

            cls_attrs = self.get_cls_attrs(member.type)
            v2 = self._parse(cls_attrs, v2)

            # deserialize to native type
            if issubclass(member.type, File):
                if isinstance(v2, File.Value):
                    native_v2 = v2
                else:
                    native_v2 = self.from_unicode(member.type, v2,
                                                  self.binary_encoding)

            elif issubclass(member.type, ByteArray):
                native_v2 = self.from_unicode(member.type, v2,
                                              self.binary_encoding)
            else:
                try:
                    native_v2 = self.from_unicode(member.type, v2)
                except ValidationError as e:
                    ns = "%s.%s" % (cls.get_namespace(), cls.get_type_name())
                    raise ValidationError(
                        e.faultstring,
                        "Validation failed for %s.%s: %%s" % (ns, k))

            # validate native data (after deserialization)
            native_v2 = self._sanitize(cls_attrs, native_v2)
            if validator is self.SOFT_VALIDATION:
                if not member.type.validate_native(member.type, native_v2):
                    raise ValidationError([orig_k, v2])

            value.append(native_v2)

        return value
Exemple #12
0
def decimal_from_string(cls, string):
    if cls.Attributes.max_str_len is not None and len(string) > \
                                                     cls.Attributes.max_str_len:
        raise ValidationError(string, "Decimal %%r longer than %d characters"
                                                   % cls.Attributes.max_str_len)

    try:
        return decimal.Decimal(string)
    except decimal.InvalidOperation, e:
        raise ValidationError(string, "%%r: %r" % e)
Exemple #13
0
def integer_from_string(cls, string):
    if cls.Attributes.max_str_len is not None and len(string) > \
                                                     cls.Attributes.max_str_len:
        raise ValidationError(string, "Integer %%r longer than %d characters"
                                                   % cls.Attributes.max_str_len)

    try:
        return int(string)
    except ValueError:
        raise ValidationError(string, "Could not cast %r to integer")
Exemple #14
0
def cache_package(spec, own_url):
    try:
        spec = Requirement.parse(spec)

    except ValueError:
        raise ArgumentError(
            "Not a URL, existing file, or requirement spec: %r" % (spec, ))

    try:
        # download and unpack source package
        path = tempfile.mkdtemp('.spynepi')
        logger.info("Downloading %r" % spec)
        dist = PackageIndex().fetch_distribution(spec,
                                                 path,
                                                 force_scan=True,
                                                 source=True)
        archive_path = dist.location
        logger.info("Unpacking %r" % archive_path)
        unpack_archive(dist.location, path)

        # generate pypirc if possible
        if os.environ.has_key('HOME'):
            _generate_pypirc(own_url)
        else:  # FIXME: ??? No idea. Hopefully setuptools knows better.
            pass  # raise NotImplementedError("$HOME not defined, .pypirc not found.")

        # find setup.py in package. plagiarized from setuptools.
        setups = glob(os.path.join(path, '*', 'setup.py'))
        if not setups:
            raise ValidationError(
                "Couldn't find a setup script in %r editable distribution: %r"
                % (spec, os.path.join(path, '*', 'setup.py')))

        if len(setups) > 1:
            raise ValidationError(
                "Multiple setup scripts found in %r editable distribution: %r"
                % (spec, setups))

        # self-register the package.
        lib_dir = os.path.dirname(setups[0])
        command = ["python", "setup.py", "register", "-r", REPO_NAME]
        logger.info('calling %r', command)
        subprocess.call(command, cwd=lib_dir, stdout=sys.stdout)

        # self-upload the package
        command = ["python", "-m", "spynepi.util.pypi.upload", archive_path]
        logger.info('calling %r', command)
        subprocess.call(command,
                        cwd=lib_dir,
                        stdin=sys.stdin,
                        stdout=sys.stdout)

    finally:
        shutil.rmtree(path)
Exemple #15
0
    def base_from_element(prot, cls, element):
        if prot.validator is prot.SOFT_VALIDATION and not (
                                            cls.validate_string(cls, element.text)):
            raise ValidationError(element.text)

        retval = callable(cls, element.text)

        if prot.validator is prot.SOFT_VALIDATION and not (
                                            cls.validate_native(cls, retval)):
            raise ValidationError(element.text)
        return retval
Exemple #16
0
    def decimal_from_unicode(self, cls, string):
        cls_attrs = self.get_cls_attrs(cls)
        if cls_attrs.max_str_len is not None and len(string) > \
                                                     cls_attrs.max_str_len:
            raise ValidationError(string, "Decimal %%r longer than %d "
                                          "characters" % cls_attrs.max_str_len)

        try:
            return D(string)
        except InvalidOperation as e:
            raise ValidationError(string, "%%r: %r" % e)
Exemple #17
0
def byte_array_from_element(prot, cls, element):
    if prot.validator is prot.SOFT_VALIDATION and not (cls.validate_string(
            cls, element.text)):
        raise ValidationError(element.text)

    retval = prot.from_string(cls, element.text, prot.default_binary_encoding)

    if prot.validator is prot.SOFT_VALIDATION and not (cls.validate_native(
            cls, retval)):
        raise ValidationError(retval)

    return retval
Exemple #18
0
    def byte_array_from_element(self, ctx, cls, element):
        if self.validator is self.SOFT_VALIDATION and not (
                                        cls.validate_string(cls, element.text)):
            raise ValidationError(element.text)

        retval = self.from_unicode(cls, element.text, self.binary_encoding)

        if self.validator is self.SOFT_VALIDATION and not (
                                            cls.validate_native(cls, retval)):
            raise ValidationError(retval)

        return retval
Exemple #19
0
    def integer_from_string(self, cls, string):
        if isinstance(string, six.string_types) and \
                                    cls.Attributes.max_str_len is not None and \
                                    len(string) > cls.Attributes.max_str_len:
            raise ValidationError(
                string, "Integer %%r longer than %d characters" %
                cls.Attributes.max_str_len)

        try:
            return int(string)
        except ValueError:
            raise ValidationError(string, "Could not cast %r to integer")
Exemple #20
0
    def base_from_element(self, ctx, cls, element):
        if self.validator is self.SOFT_VALIDATION and not (cls.validate_string(
                cls, element.text)):
            raise ValidationError(element.text)

        retval = self.from_string(cls, element.text)

        if self.validator is self.SOFT_VALIDATION and not (cls.validate_native(
                cls, retval)):
            raise ValidationError(retval)

        return retval
Exemple #21
0
    def from_urlsafe_base64(cls, value):
        #FIXME: Find out why we need to do this.
        if isinstance(value, six.text_type):
            value = value.encode('utf8')
        try:
            return (urlsafe_b64decode(_bytes_join(value)), )

        except TypeError as e:
            logger.exception(e)

            if len(value) > 100:
                raise ValidationError(value)
            else:
                raise ValidationError(value[:100] + "(...)")
Exemple #22
0
    def integer_from_string(self, cls, string):
        cls_attrs = self.get_cls_attrs(cls)

        if isinstance(string, (six.text_type, six.binary_type)) and \
                                    cls_attrs.max_str_len is not None and \
                                    len(string) > cls_attrs.max_str_len:
            raise ValidationError(string,
                                         "Integer %%r longer than %d characters"
                                                        % cls_attrs.max_str_len)

        try:
            return int(string)
        except ValueError:
            raise ValidationError(string, "Could not cast %r to integer")
Exemple #23
0
def check_freq_dict(cls, d, fti=None):
    if fti is None:
        fti = cls.get_flat_type_info(cls)

    for k, v in fti.items():
        val = d[k]

        min_o, max_o = v.Attributes.min_occurs, v.Attributes.max_occurs
        if val < min_o:
            raise ValidationError(
                k, '%%r member must occur at least %d times.' % min_o)
        elif val > max_o:
            raise ValidationError(
                k, '%%r member must occur at most %d times.' % max_o)
Exemple #24
0
def unicode_from_element(prot, cls, element):
    if prot.validator is prot.SOFT_VALIDATION and not (
                                        cls.validate_string(cls, element.text)):
        raise ValidationError(element.text)

    s = element.text
    if s is None:
        s = ''

    retval = cls.from_string(s)

    if prot.validator is prot.SOFT_VALIDATION and not (
                                        cls.validate_native(cls, retval)):
        raise ValidationError(element.text)
    return retval
Exemple #25
0
    def from_element(self, ctx, cls, element):
        cls_attrs = self.get_cls_attrs(cls)

        if bool(element.get(XSI('nil'))):
            if self.validator is self.SOFT_VALIDATION and not \
                                                             cls_attrs.nillable:
                raise ValidationError(None)

            if self.replace_null_with_default:
                return cls_attrs.default

            return None

        # if present, use the xsi:type="ns0:ObjectName"
        # attribute to instantiate subclass objects
        if self.parse_xsi_type:
            xsi_type = element.get(XSI_TYPE, None)
            if xsi_type is not None:
                if ":" in xsi_type:
                    prefix, objtype = xsi_type.split(':', 1)
                else:
                    prefix, objtype = None, xsi_type

                ns = element.nsmap.get(prefix)
                if ns is not None:
                    classkey = "{%s}%s" % (ns, objtype)

                else:
                    logger.error(
                        "xsi:type namespace prefix "
                        "'%s' in '%s' not found in %r", ns, xsi_type,
                        element.nsmap)

                    raise ValidationError(xsi_type)

                newclass = ctx.app.interface.classes.get(classkey, None)
                if newclass is None:
                    logger.error(
                        "xsi:type '%s' interpreted as class key '%s' "
                        "is not recognized", xsi_type, classkey)
                    raise ValidationError(xsi_type)

                cls = newclass
                logger.debug("xsi:type '%s' overrides %r to %r", xsi_type, cls,
                             newclass)

        handler = self.deserialization_handlers[cls]
        return handler(ctx, cls, element)
Exemple #26
0
    def decompose_incoming_envelope(self, ctx, message):
        """Sets ``ctx.in_body_doc``, ``ctx.in_header_doc`` and
        ``ctx.method_request_string`` using ``ctx.in_document``.
        """

        assert message in (ProtocolBase.REQUEST, ProtocolBase.RESPONSE)

        # set ctx.in_header
        ctx.transport.in_header_doc = None  # use an rpc protocol if you want headers.

        doc = ctx.in_document

        ctx.in_header_doc = None
        ctx.in_body_doc = doc

        if len(doc) == 0:
            raise Fault("Client", "Empty request")

        logger.debug('\theader : %r' % (ctx.in_header_doc))
        logger.debug('\tbody   : %r' % (ctx.in_body_doc))

        if not isinstance(doc, dict) or len(doc) != 1:
            raise ValidationError("Need a dictionary with exactly one key "
                                  "as method name.")

        mrs, = doc.keys()
        ctx.method_request_string = '{%s}%s' % (self.app.interface.get_tns(),
                                                mrs)
Exemple #27
0
    def unicode_from_element(self, ctx, cls, element):
        if self.validator is self.SOFT_VALIDATION and not (
                                        cls.validate_string(cls, element.text)):
            raise ValidationError(element.text)

        s = element.text
        if s is None:
            s = ''

        retval = self.from_unicode(cls, s)

        if self.validator is self.SOFT_VALIDATION and not (
                                              cls.validate_native(cls, retval)):
            raise ValidationError(retval)

        return retval
Exemple #28
0
    def from_string(cls, string):
        duration = _duration_re.match(string).groupdict(0)
        if duration is None:
            ValidationError(string)

        days = int(duration['days'])
        days += int(duration['months']) * 30
        days += int(duration['years']) * 365
        hours = int(duration['hours'])
        minutes = int(duration['minutes'])
        seconds = float(duration['seconds'])
        f, i = math.modf(seconds)
        seconds = i
        microseconds = int(1e6 * f)

        delta = datetime.timedelta(days=days,
                                   hours=hours,
                                   minutes=minutes,
                                   seconds=seconds,
                                   microseconds=microseconds)

        if duration['sign'] == "-":
            delta *= -1

        return delta
Exemple #29
0
    def from_string(cls, string):
        try:
            d = datetime.datetime.strptime(string, cls.Attributes.format)
            return datetime.date(d.year, d.month, d.day)

        except ValueError:
            raise ValidationError(string)
Exemple #30
0
    def datetime_from_string_iso(self, cls, string):
        astz = cls.Attributes.as_timezone

        match = cls._utc_re.match(string)
        if match:
            tz = pytz.utc
            retval = _parse_datetime_iso_match(match, tz=tz)
            if astz is not None:
                retval = retval.astimezone(astz)
            return retval

        if match is None:
            match = cls._offset_re.match(string)
            if match:
                tz_hr, tz_min = [int(match.group(x)) for x in ("tz_hr", "tz_min")]
                tz = FixedOffset(tz_hr * 60 + tz_min, {})
                retval = _parse_datetime_iso_match(match, tz=tz)
                if astz is not None:
                    retval = retval.astimezone(astz)
                return retval

        if match is None:
            match = cls._local_re.match(string)
            if match:
                retval = _parse_datetime_iso_match(match)
                if astz:
                    retval = retval.replace(tzinfo=astz)
                return retval

        raise ValidationError(string)