This repository has been archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
orgfuse.py
201 lines (154 loc) · 5.75 KB
/
orgfuse.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
#!/usr/bin/env python
import logging
import os
import re
from collections import OrderedDict
from errno import EIO, ENOENT, EROFS
from stat import S_IFDIR, S_IFREG
from sys import argv, exit
from time import time
from fuse import FUSE, FuseOSError, LoggingMixIn, Operations
class OrgFileParser():
HEADLINE_TOKEN = 0
SECTION_TOKEN = 1
HEADLINE_RE = re.compile(r"(\*+) (.+)")
def __init__(self, _file):
self._lines = _file.readlines()
def _tokenize(self, lines):
cur_section_strs = []
raw_tokens = [(self.HEADLINE_TOKEN, 0, "root")]
depth = 0
for line in lines:
match = self.HEADLINE_RE.match(line)
if match is None:
cur_section_strs.append(line)
else:
if cur_section_strs:
section_token = (self.SECTION_TOKEN, depth, cur_section_strs)
raw_tokens.append(section_token)
cur_section_strs = []
depth = len(match.group(1))
headline_token = (self.HEADLINE_TOKEN, depth, match.group(2))
raw_tokens.append(headline_token)
else:
if cur_section_strs:
section_token = (self.SECTION_TOKEN, depth, cur_section_strs)
raw_tokens.append(section_token)
tokens = []
for i, token in enumerate(raw_tokens):
t_type, t_depth, t_section = raw_tokens[i]
if t_type != self.HEADLINE_TOKEN:
continue
nt_type, nt_depth, nt_section = raw_tokens[i+1]
headline = t_section
depth = t_depth
section = nt_section if nt_type == self.SECTION_TOKEN else None
tokens.append((headline, depth, section))
return tokens
def _parse_tokens(self, tokens):
res = []
while tokens:
headline, depth, section = tokens[0]
tokens = tokens[1:]
child_tokens = []
for i, token in enumerate(tokens):
_, child_depth, _ = token
if child_depth <= depth:
child_tokens = tokens[:i]
tokens = tokens[i:]
break
else:
child_tokens = tokens
tokens = []
children = self._parse_tokens(child_tokens)
res.append((headline, depth, section, children))
return res
def build_tree(self):
tokens = self._tokenize(self._lines)
return self._parse_tokens(tokens)[0]
NOW = time()
class FSTree():
DIR_ATTRS = dict(st_mode=(S_IFDIR | 0o555), st_ctime=NOW,
st_mtime=NOW, st_atime=NOW, st_nlink=2,
st_uid=os.getuid(), st_gid=os.getgid())
FILE_ATTRS = dict(st_mode=(S_IFREG | 0o444), st_ctime=NOW,
st_mtime=NOW, st_atime=NOW, st_nlink=1,
st_uid=os.getuid(), st_gid=os.getgid())
@staticmethod
def from_parse_tree(root):
headline, _, section, children = root
tree = FSTree(FSTree.DIR_ATTRS, headline)
if section:
section_attrs = FSTree.FILE_ATTRS.copy()
section_content = "".join(section)
section_attrs["st_size"] = len(section_content)
section_node = FSTree(section_attrs, "section", section_content)
tree.add_child(section_node)
for child in children:
tree.add_child(FSTree.from_parse_tree(child))
return tree
def __init__(self, attrs, name, content=None):
self.attrs = attrs
self.name = name
self.content = content
self.children = OrderedDict()
def add_child(self, child):
self.children[child.name] = child
def find_path(self, path):
return self._find_path(self._convert_path(path))
def _find_path(self, path):
if len(path) == 0:
return self
left_path = path[0]
if left_path == "":
return self
if left_path in self.children:
return self.children[left_path]._find_path(path[1:])
return None
def _convert_path(self, path):
return path.lstrip("/").split("/")
def get_attrs(self):
return self.attrs
class FuseOperations(LoggingMixIn, Operations):
def __init__(self, tree):
self.tree = tree
self.fd = 0
def open(self, path, flags):
self.fd += 1
return self.fd
def read(self, path, size, offset, fh):
node = self.tree.find_path(path)
if node is None:
raise FuseOSError(EIO)
return node.content[offset:offset + size]
def readdir(self, path, fh):
node = self.tree.find_path(path)
if node is None:
raise FuseOSError(EROFS)
return ['.', '..'] + [child for child in node.children]
def getattr(self, path, fh=None):
node = self.tree.find_path(path)
if node is None:
raise FuseOSError(ENOENT)
return node.get_attrs()
HELP_MSG = """Usage: %s <orgfile> <mountpoint>
Mount an org-mode file as a directory.
Arguments:
orgfile - an org-mode file to mount
mountpoint - a directory to mount the file to"""
if __name__ == '__main__':
if len(argv) != 3:
print(HELP_MSG % argv[0])
exit(1)
org_file_path = argv[1]
if not os.path.exists(org_file_path) or not os.path.isfile(org_file_path):
print(HELP_MSG % argv[0])
exit(1)
mount_path = argv[2]
if not os.path.exists(mount_path)or not os.path.isdir(mount_path):
print(HELP_MSG % argv[0])
exit(1)
logging.basicConfig(level=logging.DEBUG)
parse_tree = OrgFileParser(open(org_file_path, "r")).build_tree()
fs_tree = FSTree.from_parse_tree(parse_tree)
FUSE(FuseOperations(fs_tree), mount_path, foreground=True)