mirror of
https://github.com/Oreolek/gamebookformat.git
synced 2024-05-14 23:18:19 +03:00
1bf91169c5
Need to think more about design and code is not pretty.
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import os
|
|
import os.path
|
|
import sys
|
|
|
|
class Templates (object):
|
|
def __init__(self, templatedirs, extension):
|
|
self.extension = extension
|
|
self.cached_templates = {}
|
|
self.templatedirs = templatedirs
|
|
|
|
def get(self, name):
|
|
if name in self.cached_templates:
|
|
return self.cached_templates[name]
|
|
for templatedir in self.templatedirs:
|
|
if self.has_template_in(templatedir, name):
|
|
return self.get_in(templatedir, name)
|
|
return ""
|
|
|
|
def has_template_in(self, templatedir, name):
|
|
# FIXME better test
|
|
return os.path.exists(self.get_template_filename(templatedir, name))
|
|
|
|
def get_in(self, templatedir, name):
|
|
filename = self.get_template_filename(templatedir, name)
|
|
f = open(filename, "r")
|
|
template = f.read()
|
|
f.close()
|
|
self.cached_templates[name] = template
|
|
return template
|
|
|
|
def get_template_filename(self, templatedir, name):
|
|
return os.path.join(templatedir,
|
|
self.extension,
|
|
name + "." + self.extension)
|