summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/minimaws/lib/wsgiserve.py
blob: 96ea43f35c57e4182aad183cafb8b85d4148f19e (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
##
## license:BSD-3-Clause
## copyright-holders:Vas Crabb

from . import dbaccess
from . import htmltmpl

import cgi
import sys
import wsgiref.simple_server
import wsgiref.util

if sys.version_info > (3, ):
    import urllib.parse as urlparse
else:
    import urlparse


class MachineHandler(object):
    def __init__(self, app, application_uri, environ, start_response, **kwargs):
        super(MachineHandler, self).__init__(**kwargs)
        self.application_uri = application_uri
        self.environ = environ
        self.start_response = start_response
        self.dbcurs = app.dbconn.cursor()
        self.shortname = wsgiref.util.shift_path_info(environ)

    def __iter__(self):
        if not self.shortname:
            # could probably list machines here or something
            self.start_response('403 Forbidden', [('Content-type', 'text/plain')])
            yield '403 Forbidden'.encode('utf-8')
        elif self.environ['PATH_INFO']:
            # subdirectory of a machine
            self.start_response('404 Not Found', [('Content-type', 'text/plain')])
            yield '404 Not Found'.encode('utf-8')
        else:
            machine_info = self.dbcurs.get_machine_info(self.shortname).fetchone()
            if not machine_info:
                self.start_response('404 Not Found', [('Content-type', 'text/plain')])
                yield '404 Not Found'.encode('utf-8')
            else:
                self.start_response('200 OK', [('Content-type', 'text/html; chearset=utf-8')])
                description = machine_info['description']
                yield htmltmpl.MACHINE_PROLOGUE.substitute(
                        description=cgi.escape(description),
                        shortname=cgi.escape(self.shortname),
                        isdevice=cgi.escape('Yes' if machine_info['isdevice'] else 'No'),
                        runnable=cgi.escape('Yes' if machine_info['runnable'] else 'No'),
                        sourcefile=cgi.escape(machine_info['sourcefile'])).encode('utf-8')
                if machine_info['year'] is not None:
                    yield (
                            '    <tr><th style="text-align: right">Year:</th><td>%s</td></tr>\n' \
                            '    <tr><th style="text-align: right">Manufacturer:</th><td>%s</td></tr>\n' %
                            (cgi.escape(machine_info['year']), cgi.escape(machine_info['Manufacturer']))).encode('utf-8')
                if machine_info['cloneof'] is not None:
                    parent = self.dbcurs.listfull(machine_info['cloneof']).fetchone()
                    if parent:
                        yield (
                                '    <tr><th style="text-align: right">Parent Machine:</th><td><a href="%s">%s (%s)</a></td></tr>\n' %
                                (cgi.escape('%s/machine/%s' % (self.application_uri, machine_info['cloneof']), True), cgi.escape(parent[1]), cgi.escape(machine_info['cloneof']))).encode('utf-8')
                    else:
                        yield (
                                '    <tr><th style="text-align: right">Parent Machine:</th><td><a href="%s">%s</a></td></tr>\n' %
                                (cgi.escape('%s/machine/%s' % (self.application_uri, machine_info['cloneof']), True), cgi.escape(machine_info['cloneof']))).encode('utf-8')
                if (machine_info['romof'] is not None) and (machine_info['romof'] != machine_info['cloneof']):
                    parent = self.dbcurs.listfull(machine_info['romof']).fetchone()
                    if parent:
                        yield (
                                '    <tr><th style="text-align: right">Parent ROM set:</th><td><a href="%s">%s (%s)</a></td></tr>\n' %
                                (cgi.escape('%s/machine/%s' % (self.application_uri, machine_info['romof']), True), cgi.escape(parent[1]), cgi.escape(machine_info['romof']))).encode('utf-8')
                    else:
                        yield (
                                '    <tr><th style="text-align: right">Parent Machine:</th><td><a href="%s">%s</a></td></tr>\n' %
                                (cgi.escape('%s/machine/%s' % (self.application_uri, machine_info['romof']), True), cgi.escape(machine_info['romof']))).encode('utf-8')
                yield '</table>\n'.encode('utf-8')

                devices = self.dbcurs.get_devices_referenced(machine_info['id'])
                first = True
                for name, desc in devices:
                    if first:
                        yield '<h2>Devices Referenced</h2>\n<ul>\n'.encode('utf-8')
                        first = False
                    if desc is not None:
                        yield (
                                '    <li><a href="%s">%s (%s)</a></li>\n' %
                                (self.machine_href(name), cgi.escape(desc), cgi.escape(name))).encode('utf-8')
                    else:
                        yield (
                                '    <li><a href="%s">%s</a></li>\n' %
                                (self.machine_href(name), cgi.escape(name))).encode('utf-8')
                if not first:
                    yield '</ul>\n'.encode('utf-8')

                devices = self.dbcurs.get_device_references(self.shortname)
                first = True
                for name, desc in devices:
                    if first:
                        yield '<h2>Referenced By</h2>\n<ul>\n'.encode('utf-8')
                        first = False
                    yield (
                            '    <li><a href="%s">%s (%s)</a></li>\n' %
                            (self.machine_href(name), cgi.escape(desc), cgi.escape(name))).encode('utf-8')
                if not first:
                    yield '</ul>\n'.encode('utf-8')

                yield '</html>\n'.encode('utf-8')

    def machine_href(self, shortname):
        return cgi.escape(urlparse.urljoin(self.application_uri, 'machine/%s' % (shortname, )), True)


class MiniMawsApp(object):
    def __init__(self, dbfile, **kwargs):
        super(MiniMawsApp, self).__init__(**kwargs)
        self.dbconn = dbaccess.QueryConnection(dbfile)

    def __call__(self, environ, start_response):
        application_uri = wsgiref.util.application_uri(environ)
        module = wsgiref.util.shift_path_info(environ)
        if module == 'machine':
            return MachineHandler(self, application_uri, environ, start_response)
        else:
            start_response('200 OK', [('Content-type', 'text/plain')])
            return ('Module is %s\n' % (module, ), )


def run_server(options):
    application = MiniMawsApp(options.database)
    server = wsgiref.simple_server.make_server(options.host, options.port, application)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass