-
Notifications
You must be signed in to change notification settings - Fork 2
/
dokuwikifuse.py
259 lines (190 loc) · 6.73 KB
/
dokuwikifuse.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from easyfuse import Operations as BaseOperations
from llfuse import ROOT_INODE
from easyfuse import Directory, File, mount
from dokuwiki import DokuWiki
import logging
import argparse
from pprint import pprint # noqa
try:
from config import Config
except:
from default_config import DefaultConfig as Config
parser = argparse.ArgumentParser(description='A CLI utility to mount dokuwiki'
' as a filesystem')
parser.add_argument('--url', help='url of the host running dokuwiki')
parser.add_argument('--user', '-u', help='user used to log in')
parser.add_argument('--password', '-p', help='password for the user')
parser.add_argument('--mountpoint', help='mountpoint for the filesystem')
parser.add_argument('--chroot', help='directory to chroot into')
parser.add_argument('--log', default='INFO', help='loglevel')
args = vars(parser.parse_args())
loglevel = getattr(logging, args['log'].upper(), None)
if not isinstance(loglevel, int):
raise ValueError('Invalid log level: %s' % args['log'])
logging.basicConfig(level=loglevel, format='[%(levelname)s] %(message)s')
del args['log']
for key, val in args.items():
if val is not None:
setattr(Config, key, val)
if not Config.chroot.endswith('/'):
Config.chroot += '/'
dw = DokuWiki(Config.url, Config.user, Config.password)
class WikiEntry:
@property
def full_depth(self):
return self.depth + len(Config.chroot.split('/')) - 2
@property
def full_path(self):
"""The full path of this entry including chroot."""
return Config.chroot + self.path.rstrip('/')
@property
def parents_old(self):
if self.inode == ROOT_INODE or self.parent.inode == ROOT_INODE:
# Ignore the last empty string when splitting
return Config.chroot.split('/')[:-1]
return self.parent.parents_old + [self.parent.name]
class WikiFile(WikiEntry, File):
_text = None
_prints = File._prints + ('doku_path',)
@classmethod
def from_wiki_data(cls, wiki_data, *args, **kwargs):
self = cls(wiki_data['id'] + '.doku', *args, **kwargs)
self.content = None
self.modified = wiki_data['mtime']
self.st_size = wiki_data['size']
return self
def refresh_content(self):
self.content = dw.pages.get(self.doku_path).encode('utf8')
@property
def text(self):
return self.content.decode('utf8')
@text.setter
def text(self, value):
self.content = value.encode('utf8')
@property
def doku_path(self):
return ':'.join(self.parents_old + [self.name.rstrip('.doku')])
def save(self):
super().save()
dw.pages.set(self.doku_path, self.text)
def delete(self):
super().delete()
if len(self.text):
# Don't delete files that are already empty, since a removed page
# is just an empty page in dokuwiki
dw.pages.delete(self.doku_path)
class WikiAttachment(WikiEntry, File):
_content = b''
@classmethod
def from_wiki_data(cls, wiki_data, *args, **kwargs):
self = cls(wiki_data['file'], *args, **kwargs)
# To make sure they are refreshed when read the first time
self.content = None
self.modified = wiki_data['mtime']
self.st_size = wiki_data['size']
return self
def refresh_content(self):
self._content = dw.medias.get(self.doku_path)
@property
def doku_path(self):
return ':'.join(self.parents_old + [self.name])
def save(self):
super().save()
dw.medias.set(self.doku_path, self.content, overwrite=True)
def delete(self):
super().delete()
dw.medias.delete(self.doku_path)
class WikiDir(WikiEntry, Directory):
def refresh_children(self):
pages = dw.pages.list(self.full_path, depth=self.full_depth + 2)
attachments = dw.medias.list(self.full_path,
depth=self.full_depth + 2)
super().refresh_children()
for p in pages:
path = p['id'].split(':')[self.full_depth:]
if len(path) > 1:
dir_name = path[0]
if dir_name in self.children:
continue
WikiDir(dir_name, self)
else:
p['id'] = path[-1]
WikiFile.from_wiki_data(p, self)
for a in attachments:
path = a['id'].split(':')[self.full_depth:]
if len(path) > 1:
dir_name = path[0]
if dir_name in self.children:
continue
WikiDir(dir_name, self)
else:
WikiAttachment.from_wiki_data(a, self)
class Operations(BaseOperations):
def __init__(self, *args, **kwargs):
super().__init__(dir_class=WikiDir, *args, **kwargs)
def illegal_filename(self, name):
# Files that start with a dot, without an extension and other
# temporary files
return name.startswith('.') or '.' not in name or name.endswith('~')
def get_file_class(self, name):
if name.endswith('.doku'):
return WikiFile
return WikiAttachment
'''
def release(self, inode):
logging.debug('release')
pass
def releasedir(self, inode):
logging.debug('releasedir')
pass
def forget(self, *args, **kwargs):
logging.debug('forget')
pass
def rename(self, *args, **kwargs):
logging.debug('rename')
pass
def rename(self, *args, **kwargs):
logging.debug('rename')
pass
def rename(self, *args, **kwargs):
logging.debug('rename')
pass
def destroy(self, *args, **kwargs):
logging.debug('destroy')
pass
def link(self, *args, **kwargs):
logging.debug('link')
pass
def mknod(self, *args, **kwargs):
logging.debug('mknod')
pass
def readlink(self, *args, **kwargs):
logging.debug('readlink')
pass
def removexattr(self, *args, **kwargs):
logging.debug('removexattr')
pass
def getexttr(self, *args, **kwargs):
logging.debug('getexattr')
pass
def fsync(self, *args, **kwargs):
logging.debug('fsync')
pass
def fsyncdir(self, *args, **kwargs):
logging.debug('fsyncdir')
pass
def listxattr(self, *args, **kwargs):
logging.debug('listxattr')
pass
def setxattr(self, *args, **kwargs):
logging.debug('setxattr')
pass
def statfs(self, *args, **kwargs):
logging.debug('statfs')
pass
def symlink(self, *args, **kwargs):
logging.debug('symlink')
pass
'''
if __name__ == '__main__':
mount(Operations(), Config.mountpoint, {'fsname=dokuwikifs'})