Example #1
0
class App(BottleCL):
    def __init__(self, log_filename):
        super(App, self).__init__()
        logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG, filename=log_filename)

        self._factory = FiasFactory()

    def get_app(self):
        return self._app

    def init_routes(self):
        self.add_route(r'/expand/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__expand)
        self.add_route(r'/normalize/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__normalize)
        self.add_route(r'/gettext/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__gettext)
        self.add_route(r'/find/<text>', self.__find)
        self.add_route(r'/find/<text>/<strong>', self.__find)
        self.add_error(404, self.basic_error_handler)
        self.add_error(500, self.basic_error_handler)

    def __expand(self, aoid):
        response.content_type = 'application/json'
        response.set_header('Access-Control-Allow-Origin', '*')

        return json.dumps(self._factory.expand(aoid))

    def __normalize(self, aoid):
        response.content_type = 'application/json'
        response.set_header('Access-Control-Allow-Origin', '*')

        return json.dumps(self._factory.normalize(aoid))

    def __find(self, text, strong=False):
        strong = (strong == "strong")
        response.content_type = 'application/json'
        response.set_header('Access-Control-Allow-Origin', '*')

        return json.dumps(self._factory.find(text, strong))

    def __gettext(self, aoid):
        response.content_type = 'application/json'
        response.set_header('Access-Control-Allow-Origin', '*')

        return json.dumps(self._factory.gettext(aoid))

    @staticmethod
    def basic_error_handler(error):
        response.content_type = 'application/json'
        response.set_header('Access-Control-Allow-Origin', '*')

        return json.dumps(dict(error=error.status))
Example #2
0
    def __init__(self, log_filename):
        super(App, self).__init__()
        logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG, filename=log_filename)

        self._factory = FiasFactory()