コード例 #1
0
ファイル: date_handler.py プロジェクト: jdost/restler
        any of them match the provided string value.  Keeps track of which
        format was matched so that it may later be used for conversion.
        """
        for dateset in cls.types:
            if not isstr(value):
                continue
            if dateset.matcher.match(value):
                cls.current = dateset.parser
                return True

        cls.current = None
        return False

    @classmethod
    def handler(cls, response, value):
        """ Converts the raw value into a rich ``datetime`` object using the
        most recently detected format as the parsing definition.
        """
        if not cls.current:
            return value

        new_date = datetime.strptime(value, cls.current)
        cls.current = None

        return new_date

DateHandler.register("[0-3][0-9]/[0-3][0-9]/[0-9]{2}", "%m/%d/%y")

from restler import Response
Response.add_datatype(DateHandler.detection, DateHandler.handler)
コード例 #2
0
ファイル: url_handler.py プロジェクト: jdost/restler
from restler.utils import isstr


class URLHandler(object):
    """ Datatype handler for URL strings.  Attempts to detect whether the
    string respresents a valid URL within the namespace of the base URL
    definition and converts the value into an equivalent :class:`Route <Route>`
    """

    @classmethod
    def detection(cls, response, value):
        """ Tests if the value matches a format that signifies it is either an
        absolute or relative path.
        """
        if not isstr(value):
            return False

        return value.startswith('/') or \
            value.startswith(str(response.__parent__))

    @classmethod
    def handler(cls, response, value):
        """ Generates a representative :class:`Route <Route>`
        """
        return response.__parent__[value]

from restler import Response
Response.add_datatype(URLHandler.detection, URLHandler.handler)
コード例 #3
0
from restler.utils import isstr


class URLHandler(object):
    """ Datatype handler for URL strings.  Attempts to detect whether the
    string respresents a valid URL within the namespace of the base URL
    definition and converts the value into an equivalent :class:`Route <Route>`
    """
    @classmethod
    def detection(cls, response, value):
        """ Tests if the value matches a format that signifies it is either an
        absolute or relative path.
        """
        if not isstr(value):
            return False

        return value.startswith('/') or \
            value.startswith(str(response.__parent__))

    @classmethod
    def handler(cls, response, value):
        """ Generates a representative :class:`Route <Route>`
        """
        return response.__parent__[value]


from restler import Response
Response.add_datatype(URLHandler.detection, URLHandler.handler)
コード例 #4
0
        format was matched so that it may later be used for conversion.
        """
        for dateset in cls.types:
            if not isstr(value):
                continue
            if dateset.matcher.match(value):
                cls.current = dateset.parser
                return True

        cls.current = None
        return False

    @classmethod
    def handler(cls, response, value):
        """ Converts the raw value into a rich ``datetime`` object using the
        most recently detected format as the parsing definition.
        """
        if not cls.current:
            return value

        new_date = datetime.strptime(value, cls.current)
        cls.current = None

        return new_date


DateHandler.register("[0-3][0-9]/[0-3][0-9]/[0-9]{2}", "%m/%d/%y")

from restler import Response
Response.add_datatype(DateHandler.detection, DateHandler.handler)