/
aptrow.py
689 lines (580 loc) · 29.6 KB
/
aptrow.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
""" Copyright 2009 Philip Dorrell http://www.1729.com/ (email: http://www.1729.com/email.html)
This file is part of Aptrow ("Advance Programming Technology Read-Only Webification": http://www.1729.com/aptrow/)
Aptrow is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Aptrow 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with Aptrow (as license-gplv3.txt).
If not, see <http://www.gnu.org/licenses/>."""
# Platform: Python 3.1 (currently being developed on MS Windows)
# Base module to be imported by all modules presenting specific resource types, and also by aptrow_server.py
import urllib
import os
import traceback
import cgi
import io
import htmltags as tag
def h(value):
""" HTML escape a string value """
return cgi.escape(value)
def hr(value):
""" HTML escape %r version of object description """
return h(value.__repr__())
def spacedList(theList):
"""Take a list and return a list with spaces inserted between the strings"""
newList = [" "] * (len(theList)*2-1)
for i, item in enumerate (theList):
newList[i*2] = item
return newList
"""A mapping from URL prefix to ResourceModule"""
resourceModules = {}
def addResourceModule(prefix, resourceModule):
"""Add a ResourceModule to resourceModules, also record the ResourceModule's urlPrefix value
(so we can go from URL to Resource and back again)"""
resourceModule.urlPrefix = prefix
resourceModules[prefix] = resourceModule
def addModule(prefix, moduleName, moduleAttribute = "aptrowModule"):
"""Import the named module and add it's enclosed ResourceModule
(by default defined as <module>.aptrowModule)"""
module =__import__(moduleName, locals(), globals(), [], -1)
addResourceModule(prefix, getattr(module, moduleAttribute))
class ResourceModule:
"""A ResourceModule represents information about Resource classes defined within one
Python module.
Lookup of resources occurs in several steps:
1. Lookup ResourceModule in resourceModules by first portion of path in URL
2. Lookup Resource class from ResourceModule by second portion of path in URL
3. Intepret query parameters to create base Resource from Resource class
4. (Optional, one or more times) interpret 'attribute' query parameters to determine attribute of base resource
"""
def __init__(self):
self.classes = {}
def getResourceClass(self, name):
"""Lookup named Resource class"""
resourceClass = self.classes.get(name)
if resourceClass == None:
raise ResourceTypeNotFoundForPathException("%s%s" % (self.urlPrefix, name))
return resourceClass
aptrowModule = ResourceModule()
def resourceTypeNameInModule(name, module):
""" Class decorator to define the resource type (i.e. 2nd part of URL)
for a resource class, i.e. a class derived from Resource, relative to a ResourceModule """
def registerResourceClass(resourceClass):
print("Registering resource class %s" % (resourceClass.__name__))
module.classes[name] = resourceClass
resourceClass.resourcePath = name
resourceClass.module = module
return resourceClass
return registerResourceClass
class MessageException(Exception):
"""An exception class whose default string representation is the 'message' attribute."""
def __str__(self):
return self.message
class ResourceTypeNotFoundForPathException(MessageException):
"""Thrown when looking up a resource type not registered in resourceClasses."""
def __init__(self, path):
self.path = path
self.message = "No resource type defined for path \"%s\"" % path
def getResource(url):
"""Given a URL, find or create the corresponding resource that the URL represents.
(The intention is to support remote resources from other Aptrow servers, but currently
only URL's relative to the local server are supported.)
Even though WSGI functions parse URL's and query parameters for you, this method (which
duplicates some of that parsing) is needed
to process URL's included as parameter values in other URL's. """
if url.startswith("/"):
localUrl = url[1:]
queryStart = localUrl.find("?")
if queryStart == -1:
path = localUrl
query = None
else:
path = localUrl[:queryStart]
query = localUrl[queryStart+1:]
resource, view = getResourceAndViewFromPathAndQuery(path, query)
return resource
else:
raise Error("Non-local resource URL's not yet implemented (doesn't start with '/'): %s" % localUrl)
class UnknownAttributeException(MessageException):
"""Thrown when an attribute name is not valid."""
def __init__(self, attribute, resource):
self.attribute = attribute
self.resource = resource
self.message = "No attribute \"%s\" defined for resource [%s]" % (attribute, resource.heading())
def getResourceAndViewFromPathAndQuery(path, query):
"""Look up resource object from URL path and query. This includes processing of parameters passed to the
base resource type, and optionally the processing of attribute parameters, for when one resource is
defined as an attribute of another. (For example, a FileContents resource can be specified as the
"contents" attribute of a File resource, with additional optional parameter "contentType".)
A numbering scheme allows attribute lookups to be chained (see AptrowQueryParams for details).
"""
secondSlashPos = path.find("/")
if secondSlashPos != -1:
urlPrefix = path[:secondSlashPos]
resourceModule = resourceModules.get(urlPrefix)
else:
resourceModule = None
if resourceModule == None:
raise ResourceTypeNotFoundForPathException(path)
else:
resourceClass = resourceModule.getResourceClass(path[secondSlashPos+1:])
if resourceClass == None:
raise ResourceTypeNotFoundForPathException(path)
queryParams = urllib.parse.parse_qs(query)
aptrowQueryParams = AptrowQueryParams(queryParams)
resourceParamValues = getResourceParams(aptrowQueryParams, resourceClass.resourceParams)
object = resourceClass(*resourceParamValues)
object.resourceParamValues = resourceParamValues # record parameters passed in
for attribute,params in aptrowQueryParams.attributesAndParams():
attributeValue = object.resolveAttribute(attribute, params)
if attributeValue == None:
raise UnknownAttributeException(attribute, object)
object = attributeValue
view = aptrowQueryParams.getView()
if view == None and object != None:
view = object.defaultView()
return object, view
class AptrowApp:
"""The main WSGI Aptrow web application. It accepts the URL representing a resource, finds
the resource, and then asks the resource to display itself."""
def __init__(self, environ, start_response):
"""WSGI initialiser: save environment and response object"""
self.environ = environ
self.start = start_response
#print ("environ = %r" % environ)
def not_found(self, message):
"""General handler for something not found: currently a message in a plain-text page."""
self.start('404 Not Found', [('Content-type', 'text/plain')])
return message
def __iter__(self):
"""Main WSGI method to yield content of requested web page. Looks up resource from URL,
and then calls resouce "page" method to render the web page."""
pathInfo = self.environ['PATH_INFO']
# print ("pathInfo = %r" % pathInfo)
if pathInfo.startswith("/"):
pathInfo = pathInfo[1:]
queryString = self.environ['QUERY_STRING']
print ("queryString = %r" % queryString)
try:
object, view = getResourceAndViewFromPathAndQuery(pathInfo, queryString)
object.checkExists()
self.message = ""
for text in object.page(self, view): yield text
except MissingParameterException as exc:
yield self.not_found("For resource type \"%s\" %s" % (pathInfo, exc.message))
except UnknownAttributeException as exc:
yield self.not_found(exc.message)
except ResourceTypeNotFoundForPathException as exc:
yield self.not_found("No resource type defined for path \"%s\"" % pathInfo)
except (NoSuchObjectException, ParameterException) as exception:
yield self.not_found(exception.message)
def runAptrowServer(host, port):
from wsgiref.simple_server import make_server
httpd = make_server(host, port, AptrowApp)
print("Serving HTTP on http://%s:%s/ ..." % (host, port))
# Respond to requests until process is killed
httpd.serve_forever()
class ParameterException(MessageException):
"""Thrown when a URL parameter is invalid or missing"""
def __init__(self, message):
self.message = message
class MissingParameterException(MessageException):
"""Thrown when a required URL parameter is missing"""
def __init__(self, name = None):
self.message = "Missing parameter %s" % name
class UnknownViewTypeException(MessageException):
"""Thrown when a view type is invalid"""
def __init__(self, type):
self.message = "Unknown view type \"%s\"" % type
class Interpretation:
"""A possible interpretation of this resource as another resource."""
def __init__(self, resource, description, likely):
self.resource = resource
self.description = description
self.likely = likely
def link(self):
return tag.A(h(self.description), href = self.resource.url())
class ResourceInterface:
"""A holder for methods that can 'interpret' a give type of resource, e.g. a 'file-like' resource.
Useage: add resource interfaces to the 'resourceInterfaces' class variable of the target resource classes, and
define @interpretationOf-decorated (static) methods in the resource classes providing the interpretations.
"""
def __init__(self):
self.interpretationMethods = []
def addInterpretation(self, method):
self.interpretationMethods.append(method)
def getInterpretationsOf(self, resource):
return [method(resource) for method in self.interpretationMethods]
"""all resources are "aptrow" resources (do not include this in 'resourceInterfaces' because
it is always implicitly included.)"""
aptrowResource = ResourceInterface()
""" resource interface for resources which are "like" a File
So if your resource class is 'file-like', then add this resource interface to
the class variable 'resourceInterfaces' of your class.
Current requirements for 'file-like' are:
* openBinaryFile(self) method which returns an opened binary file
(todo: some other way to return binary data from an object which can not be accessed this way) """
fileLikeResource = ResourceInterface()
def interpretationOf(resourceInterface):
"""Use this decorator to decorate a function (or static method) which returns an Interpretation
of a supplied source. The resourceInterface argument describes what 'kind' of resource it applies to."""
def decorator(interpretationMethod):
resourceInterface.addInterpretation(interpretationMethod)
return interpretationMethod
return decorator
class View:
"""A 'View' is information about how a resource is to be presented, and
is defined by URL parameters prefixed with 'view'.
To present a Resource in different ways (at least as HTML), add necessary
parameters to the associated View object.
A integer 'depth' parameter is included as standard.
"""
def __init__(self, type, params = {}):
self.type = type
self.params = params
self.depth = self.params.get("depth")
if self.depth != None:
self.depth = int(self.depth)
def depthLessOne(self):
"""One less than defined depth (if depth is defined)"""
if self.depth == None:
return None
else:
return self.depth-1
def __eq__(self, other):
"""Is this view the same as another view.
(Used to implement the 'make-the-link-to-yourself-inactive' functionality.)"""
return other != None and self.type == other.type and self.params == other.params
def __repr__(self):
return "View[%s, %r]" % (self.type, self.params)
def htmlParamsDict(self):
"""Return HTML parameter dictionary (used to recreate view params in URL)"""
dict = {}
if self.type != None:
dict["view"] = self.type
for key, value in self.params.items():
dict["view.%s" % key] = value
return dict
class MethodsByViewType:
"""A dictionary of methods retrieved by view type.
Throws UnknownViewTypeException if view type is unknown.
Effectively wraps a dict so that it throws an UnknownViewTypeException
if the view type is not defined.
"""
def __init__(self):
self.methods = {}
def __setitem__(self, key, value):
self.methods[key] = value
def __getitem__(self, key):
value = self.methods.get(key)
if value == None:
raise UnknownViewTypeException(key)
else:
return value
def byViewMethod(func):
"""Use this decorator to replace a method with a lookup table
which looks up a method by view type."""
return MethodsByViewType()
def byView(viewType, methodsByViewType):
"""Use this decorator to mark the methods that will be found
when a method defined by the 'byViewMethod' has a lookup done on it."""
def decorator(func):
methodsByViewType[viewType] = func
return func
return decorator
class AptrowQueryParams:
"""An object wrapping URL parameters, and presenting them as follows:
Parameters consist of:
* Base parameters passed directly to the resource type in order to retrieve (or create) the base resource.
* Attribute parameters.
Attribute parameters (and any other special parameters, but there aren't any so far) start with '_'.
All base parameters should start with an alphabetic character.
Attribute parameters represent a possible chain of 1 more attribute lookups, each with an attribute
name and a set of named parameters for each lookup. The lookups are applied first to the base resource,
and then in turn to the result of each previous lookup.
The chain of N attribute names is represented by parameters with the key '_<n>' for n = 1 to N.
Individual attribute parameters are numbered from 1 up, with the format '_<n>.<name>', where <n> is the
number, and <name> is the parameter name (which should also start with an alphabetic character).
The attribute calls are in effect method calls, because they can take parameters. However they are called
'attributes' to emphasise their read-only nature. """
def __init__(self, htmlParams):
self.htmlParams = htmlParams
def getString(self, name):
"""Retrieve an optional base resource parameter by name."""
valuesArray = self.htmlParams.get(name)
return None if valuesArray == None else valuesArray[0]
def getRequiredString(self, name):
"""Retrieve a required base resource parameter by name."""
value = self.getString(name)
if value == None:
raise ParameterException("Missing parameter %r" % name)
return value
def attributesAndParams(self):
"""Extract attribute parameters as a list of pairs of names and parameter dicts."""
count = 1
finished = False
while not finished:
attributeKey = "_%s" % count
attribute = self.getString(attributeKey)
if attribute == None:
finished = True
else:
yield (attribute, self.attributeParams(count))
count += 1
def attributeParams(self, count):
"""For a given attribute lookup (identified by number from 1 up), retrieve parameters
for that lookup into a dict."""
params = {}
for key, value in self.htmlParams.items():
prefix = "_%s." % count
if key.startswith(prefix):
params[key[len(prefix):]] = value[0]
return params
def getView(self, defaultType = None):
"""Get the View object defined by the 'view' and 'view.<param>' URL parameters."""
type = self.getString("view")
if type == None:
return None
else:
params = {}
for key, value in self.htmlParams.items():
if key.startswith("view."):
params[key[len("view."):]] = value[0]
return View(type, params)
class NoSuchObjectException(MessageException):
"""Thrown when a Resource has been created and is then later found not to represent a valid resource.
Generally thrown by the 'checkExists()' method."""
def __init__(self, message):
self.message = message
class Param:
"""Parameter definition for an expected base resource parameter.
(This is a base class: the 'getStringValue' method must be defined
by an actual concrete Parameter definition class.)
"""
def __init__(self, name, optional = False):
self.name = name
self.optional = optional
def getValue(self, stringValue):
"""Get value of parameter from a string value.
(Depends on definition of getStringValue method, if a value is supplied.)"""
if stringValue == None:
if self.optional:
return None
else:
raise MissingParameterException(self.name)
else:
return self.getValueFromString(stringValue)
def description(self):
return "%s[%s%s]" % (self.label(), self.name, " (optional)" if self.optional else "")
class StringParam(Param):
"""Parameter definition for an expected base resource parameter, expecting it to be a string value."""
def getValueFromString(self, stringValue):
"""Value for a string parameter is just the string"""
return stringValue
def getStringFromValue(self, value):
"""Get the string which represents the value (i.e. inverse of getValueFromString)"""
return value
def label(self):
return "String"
def reflectionHtml(self, value):
return "%s = \"%s\"" % (h(self.name), h(value))
class ResourceParam(Param):
"""Parameter definition for an expected base resource parameter, expecting it to be a URL representing
another resource (to be used as input when creating the resource being created)."""
def getValueFromString(self, stringValue):
"""Convert to a value by looking up resource from URL"""
return getResource(stringValue)
def getStringFromValue(self, value):
"""Get the string which represents the value (i.e. inverse of getValueFromString)"""
return value.url()
def label(self):
return "Resource"
def reflectionHtml(self, value):
return [h(self.name), " =", tag.BR(), value.reflectionHtml()]
def getResourceParams(queryParams, paramDefinitions):
"""Get parameters for creating a resource from an AptrowQueryParams and an array of parameter definitions"""
return [paramDefinition.getValue(queryParams.getString(paramDefinition.name))
for paramDefinition in paramDefinitions]
def attribute(*params):
"""Decorator for attribute methods"""
def decorator(func):
func.aptrowAttributeParams = params
return func
return decorator
class Resource:
"""Base class for all resources handled and retrieved by the application."""
def __init__(self, *args):
self.module = None
self.args = args
self.init(*args) # have to define init method for each Resource Class
def urlParams(self):
"""Parameters required to construct the URL for this resource.
Reconstructed from the args used to construct this resource object."""
paramsMap = {}
for resourceParam, arg in zip(self.__class__.resourceParams, self.args):
if arg != None:
paramsMap[resourceParam.name] = [resourceParam.getStringFromValue(arg)]
return paramsMap
def checkExists(self):
"""Default existence check: always passes.
Resource objects are always created from URL definitions. Often they represent information
from an external source (such as a file with a specified name), but their initial creation
does not depend on such information actually existing or being available (i.e. the file might not exist).
Override the 'checkExists' method to throw NoSuchObjectException's if a resource is found
to not represent a valid external resource.
"""
pass
def getInterpretations(self):
interpretations = aptrowResource.getInterpretationsOf(self)
if hasattr(self.__class__, "resourceInterfaces"):
for interface in self.__class__.resourceInterfaces:
interpretations += interface.getInterpretationsOf(self)
return interpretations
def interpretationLinksHtml(self):
interpretations = self.getInterpretations()
likelyLinks = [interpretation.link() for interpretation in interpretations if interpretation.likely]
unlikelyLinks = [interpretation.link() for interpretation in interpretations if not interpretation.likely]
if len(likelyLinks) + len(unlikelyLinks) == 0:
return ""
else:
if len(unlikelyLinks) > 0:
unlikelyLinks = ["("] + unlikelyLinks + [")"]
return tag.P(tag.B("Interpret as:"), " ", spacedList(likelyLinks+unlikelyLinks))
def defaultView(self):
return None
def htmlLink(self):
"""Default HTML link for a resource. No styling or any other extras,
and uses 'heading()' for display value."""
return tag.A(h(self.heading()), href=self.url())
def attributeUrlParams(self, attribute, count, params):
"""Return URL parameters for a single (numbered) attribute lookup as a map."""
attributeParams = {"_%s" % count: attribute}
for key, value in params.items():
attributeParams["_%s.%s" % (count, key)] = value
return attributeParams
def page(self, app, view):
for element in self.htmlPage(app, view): yield tag.toString(element)
def htmlPage(self, app, view):
"""Return the web page for the resource. Default is to return an HTML page
by calling the resource's 'html()' method. (Override this method entirely
if something else is required. Note that currently this application does
not take any notice of requested content types.)"""
heading = self.heading()
response_headers = [('Content-Type','text/html')]
app.start('200 OK', response_headers)
yield "<html><head><title>%s</title></head><body>" % h(heading)
yield app.message
yield tag.H2(h(heading))
yield self.interpretationLinksHtml()
try:
for text in self.html(view): yield text
except BaseException as error:
traceback.print_exc()
yield "<div class =\"aptrowError\">Error: %s</div>" % (h(str(error)),)
yield "</body></html>"
def resolveAttribute(self, attribute, params):
"""Resolve an attribute, by looking for a method with same name decorated by @attribute decorator."""
attributeMethod = self.__class__.__dict__.get(attribute)
if hasattr(attributeMethod, "aptrowAttributeParams"):
attributeParams = attributeMethod.aptrowAttributeParams
return attributeMethod(self, *[param.getValue(params.get(param.name)) for param in attributeParams])
else:
return None
def viewLink(self, view, description, currentView):
"""HTML for the link to another view (inactive if the 'other' view is same as this view) """
if view == currentView:
return h(description)
else:
return tag.A(h(description), href = self.url(view = view))
def viewLinksHtml(self, viewsAndDescriptions, currentView):
return spacedList([self.viewLink(view, description, currentView)
for view, description in viewsAndDescriptions])
def listAndTreeViewLinks(self, view):
"""Common list of view types: list or tree with optional depths"""
maxDepths = 4
if view.type == "tree" and view.depth != None:
maxDepths = view.depth+2
return [self.viewLink(View("list"), "list", view),
" ",
self.viewLink(View("tree"), "tree", view),
"(depth: ",
spacedList([self.viewLink(View("tree", {"depth": str(depth)}), str(depth), view)
for depth in range(1, maxDepths+1)]),
")"]
def modulePrefix(self):
if hasattr(self.__class__, "module"):
return "/" + self.__class__.module.urlPrefix
else:
return ""
def url(self, attributesAndParams = [], view = None):
""" Construct URL for this resource, from registered resource type and parameter
values from urlParams(). Any supplied attribute lookups are added to the end of the URL."""
urlString = "%s/%s?%s" % (self.modulePrefix(), self.__class__.resourcePath,
urllib.parse.urlencode(self.urlParams(), True))
count = 1
for attribute,params in attributesAndParams:
urlString += "&%s" % urllib.parse.urlencode(self.attributeUrlParams(attribute, count, params))
count += 1
if view != None:
urlString += "&%s" % urllib.parse.urlencode(view.htmlParamsDict())
return urlString
def formActionParamsAndCount(self, attributesAndParams = []):
"""Return form action, params (for hidden inputs) and count (for additional attribute params)"""
action = "%s/%s" % (self.modulePrefix(), self.__class__.resourcePath)
params = []
for key, values in self.urlParams().items():
for value in values:
params.append((key, value))
count = 0
for attribute,params in attributesAndParams:
count += 1
for key, value in self.attributeUrlParams(attribute, count, params).items():
params.append((key, value))
return action, params, count
def getAttributeHtml(self, attribute, params):
paramHtmls = ["%s = \"%s\"" % (h(param), h(value)) for param,value in params.items()]
return [tag.B("->"), " ", tag.B(h(attribute)), "(", ", ".join(paramHtmls), ")"]
def reflectionHtml(self, attributesAndParams = []):
"""Output HTML showing the parameters that define this resource"""
resourceParamsAndValues = zip(self.__class__.resourceParams, self.resourceParamValues)
paramValuesHtmls = [param.reflectionHtml(value) for (param, value) in resourceParamsAndValues]
paramValuesListItems = [tag.LI(html) for html in paramValuesHtmls]
attributesAndParamsHtmls = [self.getAttributeHtml(attribute, params)
for attribute, params in attributesAndParams]
attributeAndParamItems = [ [tag.BR(), html] for html in attributesAndParamsHtmls]
return [tag.B(h(self.__class__.__name__), ":"), tag.UL(*paramValuesListItems),
attributeAndParamItems]
@resourceTypeNameInModule("contents", aptrowModule)
class FileContents(Resource):
"""A resource representing the contents of a file, to be returned directly
to the web browser (with an optionally specified content type). The 'file'
can be any resource which has a suitable 'openBinaryFile()' method."""
resourceParams = [ResourceParam("file"), StringParam("contentType", optional = True)]
def init(self, file, contentType = None):
self.file = file
self.contentType = contentType
def checkExists(self):
"""This resource exists if the file resource exists."""
self.file.checkExists()
def page(self, app, view):
"""Override default page() method to send contents directly with content type (if specified)."""
response_headers = []
if self.contentType != None:
response_headers.append(('Content-Type', self.contentType))
app.start('200 OK', response_headers)
with self.file.openBinaryFile() as f:
yield f.read()
import tempfile
def getFileResourcePath(fileResource):
"""Get path of file from 'file-like' resource, either
from .path attribute, or from getFileName() & openBinaryFile()"""
if hasattr(fileResource, "path"):
return fileResource.path
else:
tempDir = tempfile.mkdtemp(prefix = "aptrow_")
tempFileName = os.path.join(tempDir, fileResource.getFileName())
with fileResource.openBinaryFile() as inFile:
fileContents = inFile.read()
with open(tempFileName, "wb") as outFile:
outFile.write(fileContents)
return tempFileName