<commit_before>#
# growler_indexer/middleware.py
#

from os import (path)

class Indexer():
  
  def __init__(self, dir, prefix = "/"):
    print("[Indexer::__init__]")
    self.path = dir
    self.abs = path.abspath(dir)
    print("  hosting", self.abs)

  def __call__(self, req, res):
    res.send_html("<h1>%s</h1>" % (self.abs))<commit_msg>Returns 404 on directories which do not exist.<commit_after>#
# growler_indexer/middleware.py
#

from os import (path, listdir)
from string import (Template)


class Indexer():
  
  html_tmpl = Template("""<!DOCTYPE html><html><head>$head</head><body>$body</body></html>""")
  head_tmpl = Template("""<title>$title</title>""")
  
  def __init__(self, dir, prefix = "/"):
    print("[Indexer::__init__]")
    self.path = dir
    print("dir:",dir)
    print (path.expanduser(dir))
    self.abs = path.abspath(path.expanduser(dir))
    print("  hosting", self.abs)

  def __call__(self, req, res):
    tpath = path.join(self.abs, *req.path[1:].split('/'))
    try:
      ul = "<ul><li>%s</li></ul>" % ("</li><li>".join(listdir(tpath)))
    except FileNotFoundError:
      head = self.head_tmpl.substitute(title="404 Not Found")
      body = "<h1>404 - Not Found</h1><p>The requested URL %s was not found on this server." % (self.abs)
      res.send_html(self.html_tmpl.substitute(head=head, body=body))
      return

    title = "Index of %s" % (self.path)
    body = "<h1>Index of %s</h1>%s" % (self.abs, ul)
    head = self.head_tmpl.substitute(title=title)
    res.send_html(self.html_tmpl.substitute(head=head, body=body))
{'fraction_non_alphanumeric': 0.1881377551020408, 'fraction_numerical': 0.01020408163265306, 'mean_word_length': 3.8425925925925926, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2948793', 'n_tokens_mistral': 593, 'n_tokens_neox': 580, 'n_words': 114}