Dodałem "django-nose" - runner testów dla nosetest.
[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(document.name, document.text, author, comment, parent)
19
20     def delete(self, name, author, comment):
21         self.vstorage.delete_page(name, author, comment)
22
23     def all(self):
24         return list(self.vstorage.all_pages())
25
26     def _info(self, name):
27         return self.vstorage.page_meta(name)
28
29
30 class Document(object):
31     META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
32
33     def __init__(self, storage, **kwargs):
34         self.storage = storage
35         for attr, value in kwargs.iteritems():
36             setattr(self, attr, value)
37
38     def revision(self):
39         try:
40             return self.storage._info(self.name)[0]
41         except DocumentNotFound:
42             return - 1
43
44     def plain_text(self):
45         return re.sub(self.META_REGEX, '', self.text, 1)
46
47     def meta(self):
48         result = {}
49
50         m = re.match(self.META_REGEX, self.text)
51         if m:
52             for line in m.group(1).split('\n'):
53                 try:
54                     k, v = line.split(':', 1)
55                     result[k.strip()] = v.strip()
56                 except ValueError:
57                     continue
58
59         return result
60
61 # Every time somebody says "let's have a global variable", God kills a kitten.
62 storage = DocumentStorage(settings.REPOSITORY_PATH)