コード例 #1
0
ファイル: supplemental.py プロジェクト: siq/scheme
        if isinstance(value, basestring):
            try:
                return import_object(value)
            except ImportError:
                error = ValidationError(identity=ancestry, field=self, value=value).construct(
                    'import', value=value)
                raise error.capture()
        else:
            return value

class Url(Text):
    """A resource field for urls."""

    def __init__(self, **params):
        errors = [
            FieldError('pattern', 'invalid value', '%(field)s must be a valid URL')
        ]
        pattern = re.compile('(?i)'
            r'^(?:([^:]+)://)?'
            r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
            r'localhost|'
            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
            r'(?::\d+)?'
            r'(?:/?|[/?]\S+)$'
        )
        params['escape_html_entities'] = False
        super(Url, self).__init__(errors=errors, strip=False, pattern=pattern, **params)


__all__ = construct_all_list(locals(), Field)
コード例 #2
0
ファイル: formats.py プロジェクト: esho/scheme
        skipinitialspace = False

    @classmethod
    def serialize(cls, value, columns=None, path=None):
        if path:
            value = traverse_to_key(value, path)
        if not isinstance(value, (list, tuple)):
            raise ValueError(value)
        if not value:
            return ''

        if isinstance(columns, basestring):
            columns = columns.split(',')
        if not columns:
            columns = sorted(value[0].keys())

        content = StringIO()
        writer = DictWriter(content, columns, extrasaction='ignore', dialect=cls.Dialect)

        writer.writerow(dict((name, name) for name in columns))
        writer.writerows(value)
        return content.getvalue()

def serialize(mimetype, value, **params):
    return Format.formats[mimetype].serialize(value, **params)

def unserialize(mimetype, value, **params):
    return Format.formats[mimetype].unserialize(value, **params)

__all__ = ['Format'] + construct_all_list(locals(), Format)
コード例 #3
0
ファイル: formats.py プロジェクト: abbasyousafzai/scheme
        data = {}
        for name, value in parse_qsl(content):
            if value[0] in ("{", "["):
                value = cls._unserialize_structured_value(value)
            else:
                value = cls._unserialize_simple_value(value)
            data[name] = value
        return data


class Yaml(Format):
    extensions = [".yaml", ".yml"]
    mimetype = "application/x-yaml"
    name = "yaml"

    @classmethod
    def serialize(cls, value):
        import yaml

        return yaml.dump(value)

    @classmethod
    def unserialize(cls, value):
        import yaml

        return yaml.load(value)


__all__ = ["Format"] + construct_all_list(locals(), Format)
コード例 #4
0
ファイル: fields.py プロジェクト: abbasyousafzai/scheme
        self.fields = tuple(list(self.fields[:idx]) + [field] + list(self.fields[idx + 1:]))

    @classmethod
    def _visit_field(cls, specification, callback):
        return {'fields': tuple([callback(field) for field in specification['fields']])}

class Undefined(object):
    """A field which can be defined at a later time."""

    def __init__(self, field=None):
        self.callbacks = []
        self.field = field

    def define(self, field):
        self.field = field
        for callback, args in self.callbacks:
            callback(field, *args)

    def register(self, callback, *args):
        self.callbacks.append((callback, args))

Errors = Tuple((
    Sequence(
        Map(Text(nonnull=True), description='A mapping describing an error with this request.'),
        description='A sequence of global errors for this request.'),
    Field(description='A structure containing structural errors for this request.')),
    description='A two-tuple containing the errors for this request.'
)

__all__ = ['INCOMING', 'OUTGOING', 'Field', 'Errors', 'Undefined'] + construct_all_list(locals(), Field)
コード例 #5
0
    def _serialize_value(self, value):
        return identify_object(value)

    def _unserialize_value(self, value, ancestry):
        if isinstance(value, basestring):
            try:
                return import_object(value)
            except ImportError:
                error = ValidationError(identity=ancestry, field=self, value=value).construct(
                    'import', value=value)
                raise error.capture()
        else:
            return value

class Url(Text):
    """A resource field for urls."""

    errors = [
        Error('pattern', 'invalid value', '%(field)s must be a valid URL')
    ]
    pattern = re.compile('(?i)'
        r'^(?:([^:]+)://)?'
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
        r'localhost|'
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
        r'(?::\d+)?'
        r'(?:/?|[/?]\S+)$'
    )

__all__ = construct_all_list(locals(), Field)
コード例 #6
0
        if not isinstance(value, (list, tuple)):
            raise ValueError(value)
        if not value:
            return ''

        if isinstance(columns, basestring):
            columns = columns.split(',')
        if not columns:
            columns = sorted(value[0].keys())

        content = StringIO()
        writer = DictWriter(content,
                            columns,
                            extrasaction='ignore',
                            dialect=cls.Dialect)

        writer.writerow(dict((name, name) for name in columns))
        writer.writerows(value)
        return content.getvalue()


def serialize(mimetype, value, **params):
    return Format.formats[mimetype].serialize(value, **params)


def unserialize(mimetype, value, **params):
    return Format.formats[mimetype].unserialize(value, **params)


__all__ = ['Format'] + construct_all_list(locals(), Format)