1
0
Fork 0
mirror of https://github.com/Oreolek/gamebookformat.git synced 2024-05-14 23:18:19 +03:00
gamebookformat/templates.py
Pelle Nilsson 1bf91169c5 Codewords and items.
Need to think more about design and code is not pretty.
2013-06-07 00:14:06 +02:00

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)