-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.py
48 lines (37 loc) · 1.1 KB
/
templates.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
"""
Templates!
"""
import json
import logging
import config
class TemplateDB(object):
"""
Stores templates, not even a DB
"""
def __init__(self):
try:
self.templates = json.loads(open(config.templatedb).read())
except:
logging.debug("No templates file exists")
self.templates = dict()
def get_template(self, template):
logging.debug("Looking up %s" % (template))
try:
return self.templates[template]
except:
raise TemplateException("Template not found")
def add_template(self, template, text):
if len(text) > 256:
return False
self.templates[template] = text
self._sync_template()
return True
def del_template(self, template):
del self.templates[template]
self._sync_template()
def list_templates(self, search=""):
return [(k, v) for k, v in self.templates.items()]
def _sync_template(self):
open(config.templatedb, 'w').write(json.dumps(self.templates))
class TemplateException(ValueError):
pass