comparison tincan.py @ 58:e08e24707da1 draft

Recognize index.pspx (and index.html, and index.htm if static).
author David Barts <n5jrn@me.com>
date Fri, 31 May 2019 20:42:57 -0700
parents 935f8013f540
children 60907204a265
comparison
equal deleted inserted replaced
57:935f8013f540 58:e08e24707da1
382 _PEXTEN = ".py" 382 _PEXTEN = ".py"
383 _TEXTEN = ".pspx" 383 _TEXTEN = ".pspx"
384 _FLOOP = "tincan.forwards" 384 _FLOOP = "tincan.forwards"
385 _FORIG = "tincan.origin" 385 _FORIG = "tincan.origin"
386 _FTYPE = "tincan.iserror" 386 _FTYPE = "tincan.iserror"
387 _INDEX = "/index" + _TEXTEN
388 _LINDEX = len(_INDEX)
389 _INDICES = [ _INDEX, "/index.html", "/index.htm" ]
387 390
388 class _TinCanBaseRoute(object): 391 class _TinCanBaseRoute(object):
389 """ 392 """
390 The base class for all NON ERROR routes. Error routes are just a little 393 The base class for all NON ERROR routes. Error routes are just a little
391 bit different. 394 bit different.
438 self._encoding = None 441 self._encoding = None
439 442
440 def launch(self): 443 def launch(self):
441 self.logger.info("adding static route: %s", self._urlpath) 444 self.logger.info("adding static route: %s", self._urlpath)
442 self._app.route(self._urlpath, 'GET', self) 445 self._app.route(self._urlpath, 'GET', self)
446 for i in _INDICES:
447 if self._urlpath.endswith(i):
448 li = len(i)
449 for j in [ self._urlpath[:1-li], self._urlpath[:-li] ]:
450 if j:
451 self.logger.info("adding static route: %s", j)
452 self._app.route(j, 'GET', self)
443 453
444 def _parse_date(self, ims): 454 def _parse_date(self, ims):
445 """ 455 """
446 Parse rfc1123, rfc850 and asctime timestamps and return UTC epoch. 456 Parse rfc1123, rfc850 and asctime timestamps and return UTC epoch.
447 """ 457 """
588 tfile = TemplateFile(tpath) 598 tfile = TemplateFile(tpath)
589 except OSError as e: 599 except OSError as e:
590 raise TinCanError("{0}: invalid #template: {1!s}".format(self._urlpath, e)) from e 600 raise TinCanError("{0}: invalid #template: {1!s}".format(self._urlpath, e)) from e
591 except IndexError as e: 601 except IndexError as e:
592 raise TinCanError("{0}: invalid #template".format(self._urlpath)) from e 602 raise TinCanError("{0}: invalid #template".format(self._urlpath)) from e
593 self._body = self._mktemplate(tfile.body, self.urljoin(rtpath)) 603 self._body = self._mktemplate(tfile.body, self.urljoin(*rtpath))
594 else: 604 else:
595 self._body = self._mktemplate(self._template.body, self._urlpath) 605 self._body = self._mktemplate(self._template.body, self._urlpath)
596 self._body.prepare() 606 self._body.prepare()
597 # Process loads 607 # Process loads
598 self._loads = {} 608 self._loads = {}
620 else: 630 else:
621 methods = [ i.upper() for i in self._header.methods.split() ] 631 methods = [ i.upper() for i in self._header.methods.split() ]
622 if not methods: 632 if not methods:
623 raise TinCanError("{0}: no #methods specified".format(self._urlpath)) 633 raise TinCanError("{0}: no #methods specified".format(self._urlpath))
624 # Register this thing with Bottle 634 # Register this thing with Bottle
625 self.logger.info("adding route: %s (%s)", self._origin, ','.join(methods)) 635 mtxt = ','.join(methods)
636 self.logger.info("adding route: %s (%s)", self._origin, mtxt)
626 self._app.route(self._origin, methods, self) 637 self._app.route(self._origin, methods, self)
638 if self._origin.endswith(_INDEX):
639 for i in [ self._origin[:1-_LINDEX], self._origin[:-_LINDEX] ]:
640 if i:
641 self.logger.info("adding route: %s (%s)", i, mtxt)
642 self._app.route(i, methods, self)
627 643
628 def _mktemplate(self, source, name): 644 def _mktemplate(self, source, name):
629 try: 645 try:
630 return ChameleonTemplate(source=source, encoding=self._encoding) 646 return ChameleonTemplate(source=source, encoding=self._encoding)
631 except TemplateError as e: 647 except TemplateError as e: