Debian Codegraph @

scripts/CGI_RDFa.py

raw symbols SCIP
1 #!/usr/local/bin/python3
2 # -*- coding: utf-8 -*-
3 # Maintainer: Ivan Herman <[email protected]>
4
5 """
6 Possible CGI entry point for the RDFa 1.1 package.
7
8 This version is adapted to the particualarities of the W3C setup as well as my own machine for Python paths
9
10
11 @author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
12 @license: This software is available for use under the
13 U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">}
14 @contact: Ivan Herman, [email protected]
15 """
16
17 """
18 $Id: RDFa.py,v 1.27 2018/05/23 08:57:19 carcone Exp $
19 """
20
21 __version__ = "4.0.0"
22 import cgi
23 import cgitb
24 import sys, os
25 #import StringIO
26 #cgi.print_environ()
27
28 if sys.platform == "darwin" :
29 # this is my local machine
30 sys.path.insert(0,'/Users/ivan/Library/Python')
31 sys.path.insert(0,'/Users/ivan/Library/Python/RDFa')
32 os.environ['PyRdfaCacheDir'] = '/Users/ivan/.pyrdfa-cache'
33 cgitb.enable()
34
35 else :
36 # This will have to be updated for the Python3 installation!!!
37 # webencodings pip3 should also be done!!!
38 sys.path.insert(0,"/usr/lib/python2.7/dist-packages")
39 sys.path.insert(0,'/home/ivan/lib/python')
40 os.environ['PyRdfaCacheDir'] = '/usr/local/apache/cgi/cgi-bin-other/RDFa/data-local'
41 cgitb.enable(display=0, logdir="/home/nobody/tracebacks/")
42
43 from pyRdfa import processURI, RDFaError
44
45 # Register the RDFa JSON-LD serializer; for some reasons installing via pip did not work
46 from rdflib.plugin import register, Serializer
47 register('json', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
48
49 def err_message(msg) :
50 from cleanhtml import clean_print
51 print('Content-type: text/html; charset=utf-8')
52 print('Status: 400 Invalid Input')
53 print()
54 print("<html>")
55 print("<head>")
56 print("<title>Error in RDFa processing</title>")
57 print("</head><body>")
58 print("<h1>Error in distilling RDFa</h1>")
59 print("<p>")
60 clean_print("pyRdfa cannot process this URI: %s", uri)
61 print("</p>")
62 if len(msg) != 0 :
63 print("<p>")
64 clean_print(msg)
65 print("</p>")
66 print("</body>")
67 print("</html>")
68 sys.exit(1)
69
70
71 def brett_test(uri) :
72
73 if not sys.platform == "darwin" :
74 from checkremote import check_url_safety, UnsupportedResourceError
75 from urllib2 import HTTPError, URLError
76 try:
77 check_url_safety(uri)
78 except HTTPError as e:
79 err_message('HTTP Error with the error code: %s and the error message: "%s"' (e.code, e.reason))
80 except URLError as e:
81 err_message('URL Error with the error message: "%s"' % e.reason)
82 except UnsupportedResourceError as e:
83 msg = e.args[0] + ": " + e.args[1]
84 err_message('Unsupported Resource Error with the error message "%s"' % msg)
85 except Exception as e:
86 l = len(e.args)
87 msg = "" if l == 0 else (e.args[0] if l == 1 else e.args)
88 err_message('Exception raised: "%s"' % msg)
89
90 #
91 # to make this thing exist...
92 uri = ""
93 form = cgi.FieldStorage()
94 # First see if an upload is present in the form
95 if "uploaded" in form and form["uploaded"].file :
96 uri = "uploaded:"
97 elif "text" in form and form["text"].value != None and len(form["text"].value.strip()) != 0 :
98 uri = "text:"
99 else :
100 if not "uri" in form :
101 print('Content-type: text/html; charset=utf-8')
102 print('Status: 400 Invalid Input')
103 print()
104 print("<html>")
105 print("<head>")
106 print("<title>Error in RDFa processing</title>")
107 print("</head><body>")
108 print("<h1>Error in distilling RDFa</h1>")
109 print("<p>No URI has been specified</p>")
110 print("</body>")
111 print("</html>")
112 sys.exit(1)
113
114 try :
115 uri = form.getfirst("uri")
116 except :
117 print('Content-type: text/html; charset=utf-8')
118 print('Status: 400 Invalid Input')
119 print()
120 print("<html>")
121 print("<head>")
122 print("<title>Error in RDFa processing</title>")
123 print("</head><body>")
124 print("<h1>Error in distilling RDFa</h1>")
125 print("<p>No URI has been specified</p>")
126 print("</body>")
127 print("</html>")
128 sys.exit(1)
129
130 if "validate" in form :
131 from rdfavalidator import validateURI
132 if not (uri == 'text:' or uri == 'uploaded:') :
133 brett_test(uri)
134 print('Content-Type: text/html; charset=utf-8')
135 print()
136 print(validateURI(uri, form))
137 else :
138 try :
139 # Thanks to Sergio and Diego for the idea and code for the referer branch
140 if uri == "referer" :
141 uri = os.getenv('HTTP_REFERER')
142 if uri is None:
143 newuri = "http://www.w3.org/2012/pyRdfa/no_referer.html"
144 else:
145 brett_test(uri)
146 newuri = "http://www.w3.org/2012/pyRdfa/extract?uri=" + uri
147 print("Status: 307 Moved Temporarily")
148 print("Location: " + newuri)
149 print()
150 else :
151 # last point of check: use Brett's script to check the validity of the URI
152 if not (uri == 'text:' or uri == 'uploaded:') :
153 brett_test(uri)
154
155 if "format" in form.keys() :
156 format = form.getfirst("format")
157 else :
158 format = "turtle"
159 retval = processURI(uri, format, form)
160 print(retval)
161 except Exception as e :
162 l = len(e.args)
163 msg = "" if l == 0 else (e.args[0] if l == 1 else e.args)
164 err_message('Exception raised: "%s"' % msg)