#!/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 (
'
Year: | %s |
\n' \
' Manufacturer: | %s |
\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 (
' Parent Machine: | %s (%s) |
\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 (
' Parent Machine: | %s |
\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 (
' Parent ROM set: | %s (%s) |
\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 (
' Parent Machine: | %s |
\n' %
(cgi.escape('%s/machine/%s' % (self.application_uri, machine_info['romof']), True), cgi.escape(machine_info['romof']))).encode('utf-8')
yield '\n'.encode('utf-8')
devices = self.dbcurs.get_devices_referenced(machine_info['id'])
first = True
for name, desc in devices:
if first:
yield 'Devices Referenced
\n\n'.encode('utf-8')
first = False
if desc is not None:
yield (
' - %s (%s)
\n' %
(self.machine_href(name), cgi.escape(desc), cgi.escape(name))).encode('utf-8')
else:
yield (
' - %s
\n' %
(self.machine_href(name), cgi.escape(name))).encode('utf-8')
if not first:
yield '
\n'.encode('utf-8')
devices = self.dbcurs.get_device_references(self.shortname)
first = True
for name, desc in devices:
if first:
yield 'Referenced By
\n\n'.encode('utf-8')
first = False
yield (
' - %s (%s)
\n' %
(self.machine_href(name), cgi.escape(desc), cgi.escape(name))).encode('utf-8')
if not first:
yield '
\n'.encode('utf-8')
yield '