Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/online_check/stdnum.wsgi
blob: f65c959c8c9a75c8a16ba3df17f45da8b17392ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# stdnum.wsgi - simple WSGI application to check numbers
#
# Copyright (C) 2017-2023 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Simple WSGI application to check numbers."""

import datetime
import html
import inspect
import json
import os
import re
import sys
import urllib.parse


sys.stdout = sys.stderr
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'python-stdnum'))

from stdnum.util import (  # noqa: E402,I001 (import after changes to sys.path)
    get_module_description, get_module_name, get_number_modules, to_unicode)


_template = None


def get_conversions(module, number):
    """Return the possible conversions for the number."""
    for name, func in inspect.getmembers(module, inspect.isfunction):
        if name.startswith('to_') or name.startswith('get_'):
            args, varargs, varkw, defaults = inspect.getargspec(func)
            if defaults:
                args = args[:-len(defaults)]
            if args == ['number'] and not name.endswith('binary'):
                try:
                    prop = name.split('_', 1)[1].replace('_', ' ')
                    conversion = func(number)
                    if isinstance(conversion, datetime.date):
                        yield (prop, conversion.strftime('%Y-%m-%d'))
                    elif conversion != number:
                        yield (prop, to_unicode(conversion))
                except Exception:  # noqa: B902 (catch anything that goes wrong)
                    pass


def info(module, number):
    """Return information about the number."""
    compactfn = getattr(module, 'compact', lambda x: x)
    formatfn = getattr(module, 'format', compactfn)
    return dict(
        number=formatfn(number),
        compact=compactfn(number),
        valid=module.is_valid(number),
        module=module.__name__.split('.', 1)[1],
        name=to_unicode(get_module_name(module)),
        description=to_unicode(get_module_description(module)),
        conversions=dict(get_conversions(module, number)))


def format(data):
    """Return an HTML snippet describing the number."""
    description = html.escape(data['description']).replace('\n\n', '<br/>\n')
    description = re.sub(
        r'^[*] (.*)$', r'<ul><li>\1</li></ul>',
        description, flags=re.MULTILINE)
    description = re.sub(
        r'\b((https?|ftp)://[^\s<]*[-\w+&@#/%=~_|])',
        r'<a href="\1">\1</a>',
        description, flags=re.IGNORECASE + re.UNICODE)
    for name, conversion in data.get('conversions', {}).items():
        description += '\n<br/><b><i>%s</i></b>: %s' % (
            html.escape(name), html.escape(conversion))
    return '<li>%s: <b>%s</b><p>%s</p></li>' % (
        html.escape(data['number']),
        html.escape(data['name']),
        description)


def application(environ, start_response):
    """WSGI application."""
    # read template if needed
    global _template
    if not _template:
        basedir = os.path.join(
            environ['DOCUMENT_ROOT'],
            os.path.dirname(environ['SCRIPT_NAME']).strip('/'))
        _template = to_unicode(open(os.path.join(basedir, 'template.html'), 'rt').read())
    is_ajax = environ.get(
        'HTTP_X_REQUESTED_WITH', '').lower() == 'xmlhttprequest'
    parameters = urllib.parse.parse_qs(environ.get('QUERY_STRING', ''))
    results = []
    number = ''
    if 'number' in parameters:
        number = to_unicode(parameters['number'][0])
        results = [
            info(module, number)
            for module in get_number_modules()
            if module.is_valid(number)]
    if is_ajax:
        start_response('200 OK', [
            ('Content-Type', 'application/json'),
            ('Vary', 'X-Requested-With')])
        return [json.dumps(results, indent=2, sort_keys=True).encode('utf-8')]
    start_response('200 OK', [
        ('Content-Type', 'text/html; charset=utf-8'),
        ('Vary', 'X-Requested-With')])
    return [(_template % dict(
        value=html.escape(number, True),
        results=u'\n'.join(format(data) for data in results))).encode('utf-8')]