Logging for staging, removed unused requirements.
[redakcja.git] / apps / wiki / models.py
1 import re
2 import vstorage
3 from vstorage import DocumentNotFound
4 from wiki import settings
5
6 class DocumentStorage(object):
7     def __init__(self, path):
8         self.vstorage = vstorage.VersionedStorage(path)
9
10     def get(self, name, revision = None):
11         if revision is None:
12             text = self.vstorage.page_text(name)
13         else:
14             text = self.vstorage.revision_text(name, revision)
15         return Document(self, name = name, text = text)
16
17     def put(self, document, author, comment, parent):
18         self.vstorage.save_text(
19                 title = document.name,
20                 text = document.text, 
21                 author = author, 
22                 comment = comment, 
23                 parent = parent)
24
25     def delete(self, name, author, comment):
26         self.vstorage.delete_page(name, author, comment)
27
28     def all(self):
29         return list(self.vstorage.all_pages())
30     
31     def history(self, title):
32         return list(self.vstorage.page_history(title))
33
34     def _info(self, name):
35         return self.vstorage.page_meta(name)
36
37
38 class Document(object):
39     META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
40
41     def __init__(self, storage, **kwargs):
42         self.storage = storage
43         for attr, value in kwargs.iteritems():
44             setattr(self, attr, value)
45
46     def revision(self):
47         try:
48             return self.storage._info(self.name)[0]
49         except DocumentNotFound:
50             return - 1
51
52     @property
53     def plain_text(self):
54         return re.sub(self.META_REGEX, '', self.text, 1)
55
56     def meta(self):
57         result = {}
58
59         m = re.match(self.META_REGEX, self.text)
60         if m:
61             for line in m.group(1).split('\n'):
62                 try:
63                     k, v = line.split(':', 1)
64                     result[k.strip()] = v.strip()
65                 except ValueError:
66                     continue
67
68         return result
69
70 # Every time somebody says "let's have a global variable", God kills a kitten.
71 storage = DocumentStorage(settings.REPOSITORY_PATH)