* Poprawiony freeze() dla głownego okna.
[redakcja.git] / lib / wlrepo / __init__.py
1 # -*- encoding: utf-8 -*-
2 __author__="Łukasz Rekucki"
3 __date__ ="$2009-09-18 10:49:24$"
4 __doc__ = """Main module for the Repository Abstraction Layer"""
5
6 class Library(object):
7
8     def __init__(self, create=False):
9         """Open an existing library, or create a new one. By default, fails if
10         the library doesn't exist."""
11         self.create = create      
12
13     def documents(self):
14         """List all documents in the library."""
15         pass
16
17     def document_for_rev(self, rev):
18         """Retrieve a document in the specified revision."""
19         pass
20
21     def document(self, docid, user=None, rev='latest'):
22         """Retrieve a document from a library."""
23         pass
24
25     def get_revision(self, revid):
26         """Retrieve a handle to a specified revision."""
27         return None
28
29     def document_create(self, docid):
30         """Create a new document. The document will have it's own branch."""
31         
32
33 class Document(object):
34     """A class representing a document package boundled with a revision."""
35
36     def __init__(self, library, revision):
37         """_library_ should be an instance of a Library."""
38         self._library = library
39         if isinstance(revision, Revision):
40             self._revision = revision
41         else:
42             self._revision = library.get_revision(revision)
43
44
45     def take(self, user):
46         """Make a user copy of the document. This is persistant."""
47         pass
48
49     def giveback(self):
50         """Informs the library, that the user no longer needs this document.
51         Should be called on the user version of document. If not, it doesn nothing."""
52        
53     def data(self, entry):
54         """Returns the specified entry as a unicode data."""
55         pass
56
57     @property
58     def library(self):
59         return self._library
60
61     @property
62     def revision(self):
63         return self._revision
64
65     @property
66     def id(self):
67         return self._revision.document_name
68
69     @property
70     def owner(self):
71         return self._revision.user_name
72     
73     def parentof(self, other):
74         return self._revision.parentof(other._revision)
75
76     def ancestorof(self, other):
77         return self._revision.ancestorof(other._revision)
78
79
80 class Revision(object):
81
82     def __init__(self, lib):
83         self._library = lib
84
85     def parentof(self, other):
86         return False
87
88     def ancestorof(self, other):
89         return False
90
91     @property
92     def document_name(self):
93         raise ValueError()
94
95     @property
96     def user_name(self):
97         raise ValueError()
98
99 #
100 # Exception classes
101 #
102
103 class LibraryException(Exception):    
104     def __init__(self, msg, cause=None):
105         Exception.__init__(self, msg)
106         self.cause = cause
107
108 class RevisionNotFound(LibraryException):
109     def __init__(self, rev):
110         LibraryException.__init__(self, "Revision %r not found." % rev)
111
112 class RevisionMismatch(LibraryException):
113     def __init__(self, fdi, rev):
114         LibraryException.__init__(self, "No revision %r for document %r." % (rev, fdi))
115     
116 class EntryNotFound(LibraryException):
117     def __init__(self, rev, entry, guesses=[]):
118         LibraryException.__init__(self, \
119             u"Entry '%s' at revision %r not found. %s" % (entry, rev, \
120             (u"Posible values:\n" + u',\n'.join(guesses)) if len(guesses) else u'') )
121
122 class DocumentAlreadyExists(LibraryException):
123     pass
124
125 # import backends to local namespace
126
127 def open_library(path, proto, *args, **kwargs):
128     if proto == 'hg':
129         import wlrepo.mercurial_backend
130         return wlrepo.mercurial_backend.MercurialLibrary(path, *args, **kwargs)
131
132     raise NotImplemented()