2023-06-12 09:11:18 +02:00
#!/usr/bin/env python3
# You can test it with:
2023-06-12 21:45:53 +02:00
# curl --request POST --data string=foobar\&what=language http://localhost:4000
2023-06-12 09:11:18 +02:00
import wsgiref . simple_server as server
import urllib . parse
2023-06-12 21:45:53 +02:00
# PostgreSQL interface
2023-06-12 09:11:18 +02:00
import psycopg2
2023-06-12 21:45:53 +02:00
# HTML templates http://www.yattag.org/
from yattag import Doc
2023-06-12 09:11:18 +02:00
PORT = 4000
2023-08-01 20:58:15 +02:00
#REDIRECTION_URL = "file:///home/stephane/src/Language-tags/GaBuZoMeu/registry-html" # Firefox does not accept a redirect to a file
REDIRECTION_URL = " https://www.langtag.net/registries/registry-html "
2023-06-12 09:11:18 +02:00
2023-08-01 20:58:15 +02:00
def pack ( start_response , status , data , headers = [ ] ) :
2023-06-12 09:11:18 +02:00
datae = data . encode ( ) # Always encode to UTF-8
2023-08-01 20:58:15 +02:00
response_headers = [ ( " Content-type " , " text/plain; charset=UTF-8 " ) , # TODO allows the type HTML
2023-06-12 09:11:18 +02:00
( " Content-Length " , str ( len ( datae ) ) ) ]
2023-08-01 20:58:15 +02:00
if headers != [ ] :
for header in headers :
response_headers . append ( header )
2023-06-12 09:11:18 +02:00
start_response ( status , response_headers )
return [ datae ]
def application ( environ , start_response ) :
2023-06-12 18:34:27 +02:00
if environ [ " REQUEST_METHOD " ] != " POST " :
status = " 405 Method unavailable "
output = " Unsupported HTTP method \" %s \" \r \n " % environ [ " REQUEST_METHOD " ]
return pack ( start_response , status , output )
2023-06-12 09:11:18 +02:00
try :
body_size = int ( environ . get ( " CONTENT_LENGTH " , 0 ) )
except ( ValueError ) :
body_size = 0
body = environ [ " wsgi.input " ] . read ( body_size )
query = urllib . parse . parse_qsl ( body . decode ( ) )
2023-06-12 18:34:27 +02:00
form = { }
2023-08-01 20:58:15 +02:00
headers = [ ]
2023-06-12 18:34:27 +02:00
for q in query :
form [ q [ 0 ] ] = q [ 1 ]
for mandatory in [ " string " , " what " ] :
if mandatory not in form :
status = " 400 Invalid request "
output = " Missing \" %s \" in request \r \n " % mandatory
return pack ( start_response , status , output )
if form [ " what " ] == " language " :
sql = " SELECT * FROM Languages WHERE code = %s "
2023-06-12 21:45:53 +02:00
term = form [ " string " ] . lower ( )
2023-06-12 18:34:27 +02:00
elif form [ " what " ] == " script " :
2023-06-12 21:45:53 +02:00
sql = " SELECT * FROM Scripts WHERE code = %s "
term = form [ " string " ] . capitalize ( )
2023-06-12 18:34:27 +02:00
else :
status = " 400 Invalid request "
output = " Unknown search category \" %s \" \r \n " % form [ " what " ]
return pack ( start_response , status , output )
2023-06-12 21:45:53 +02:00
cursor . execute ( sql , ( term , ) )
2023-06-12 18:34:27 +02:00
mytuple = cursor . fetchone ( )
status = " 200 OK "
2023-08-01 20:58:15 +02:00
if mytuple is not None :
status = " 308 Redirect "
url = " %s / %s / %s .html " % ( REDIRECTION_URL , form [ " what " ] , mytuple [ 1 ] )
headers . append ( ( " Location " , url ) )
output = " Redirect to %s \r \n " % ( url )
2023-06-12 21:45:53 +02:00
else :
if form [ " what " ] == " language " :
sql = " SELECT * FROM Languages,Descriptions_Languages,Descriptions WHERE Descriptions_Languages.description = Descriptions.id AND Descriptions_Languages.lang = Languages.code AND position( %s in lower(Descriptions.description)) > 0; "
elif form [ " what " ] == " script " :
raise " TODO not implemented "
cursor . execute ( sql , ( term , ) )
found = False
output = " "
for tuple in cursor . fetchall ( ) :
2023-08-01 20:58:15 +02:00
output + = " TODO %s \r \n " % str ( tuple ) # TODO make it a HTML list
2023-06-12 21:45:53 +02:00
found = True
if not found :
output = " \" %s \" not found \r \n " % term
2023-08-01 20:58:15 +02:00
return pack ( start_response , status , output , headers )
2023-06-12 09:11:18 +02:00
if __name__ == " __main__ " :
conn = psycopg2 . connect ( " dbname=lsr " )
cursor = conn . cursor ( )
httpd = server . make_server ( " " , PORT , application )
print ( " Listening on port %s " % PORT )
httpd . serve_forever ( )