-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfuse.py
executable file
·172 lines (144 loc) · 5.41 KB
/
fuse.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
#!/usr/bin/env python2.7
from __future__ import with_statement
import os
import sys
import errno
from fusepy.fuse import FUSE, FuseOSError, Operations
import imap
import config
import json
class Imap(Operations):
emails = None
account_config = None
root = None
_EMAIL_DIR = '.emails'
def __init__(self, root, emails, account_config):
self.root = root
self.emails = emails
self.account_config = account_config
# Helpers
# =======
def _full_path(self, partial):
if partial.startswith("/"):
partial = partial[1:]
path = os.path.join(self.root, partial)
return path
# Filesystem methods
# ==================
def getattr(self, path, fh=None):
if 'README.md' in path:
full_path = self._full_path(path)
st = os.lstat(full_path)
return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
if 'plain-text.txt' in path or 'metadata.txt' in path or 'README' in path:
return {
'st_atime': 1485735362.1132069,
'st_ctime': 1485727143.0372078,
'st_gid': 1000,
'st_mode': 33204, # normal?
#'st_mode': 41471, # symlink
'st_mtime': 1485727143.0372078,
'st_nlink': 1,
'st_size': 12000,
'st_uid': 1000
}
if '.txt' in path:
return {
'st_atime': 1485735362.1132069,
'st_ctime': 1485727143.0372078,
'st_gid': 1000,
#'st_mode': 33204, # normal?
'st_mode': 41471, # symlink
'st_mtime': 1485727143.0372078,
'st_nlink': 1,
'st_size': 12000,
'st_uid': 1000
}
#if self.account_config.email_address in path:
return { # Directory
'st_atime': 1485738550.6692064,
'st_ctime': 1485738570.1532063,
'st_gid': 1000,
'st_mode': 16893,
'st_mtime': 1485738570.1532063,
'st_nlink': 2,
'st_size': 4096,
'st_uid': 1000
}
def readdir(self, path, fh):
full_path = self._full_path(path)
dirents = ['.', '..']
if '/' == path:
dirents.append(self.account_config.email_address)
dirents.extend(os.listdir(full_path))
elif '/' + self.account_config.email_address == path:
dirents.append(self._EMAIL_DIR)
dirents.append('plain-text')
elif '/' + self.account_config.email_address + '/plain-text' == path:
#email_titles = []
for key, email in self.emails.iteritems():
dirents.append('%s::%s.%s.txt' % (
email['subject'],
email['from_email'],
key
))
#dirents.extend(email_titles)
elif '/' + self.account_config.email_address + '/' + self._EMAIL_DIR in path:
keys = self.emails.keys()
dir_no = path.split('/')[-1]
if '/' + self.account_config.email_address + '/' + self._EMAIL_DIR != path:
dirents.append('plain-text.txt')
dirents.append('metadata.txt')
else:
dirents.extend(keys)
for r in dirents:
yield r
def readlink(self, path):
file_name_parts = path.split('.')
if file_name_parts[-1] == 'txt':
return '../.emails/%s/plain-text.txt' % (
file_name_parts[-2]
)
pathname = os.readlink(self._full_path(path))
if pathname.startswith("/"):
# Path name is absolute, sanitize it.
return os.path.relpath(pathname, self.root)
else:
return pathname
def statfs(self, path):
full_path = self._full_path(path)
stv = os.statvfs(full_path)
d = dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
'f_frsize', 'f_namemax'))
return d
def link(self, target, name):
return os.link(self._full_path(target), self._full_path(name))
# File methods
# ============
def open(self, path, flags):
if 'README.md' in path:
full_path = self._full_path(path)
return os.open(full_path, flags)
return 1
def read(self, path, length, offset, fh):
if 'plain-text.txt' in path:
dir_no = path.split('/')[3].split('/')[-1]
return self.emails[dir_no]['plain_text']
if 'metadata.txt' in path:
dir_no = path.split('/')[3].split('/')[-1]
return 'from : ' + self.emails[dir_no]['from'] + '\n' + \
'subject : ' + self.emails[dir_no]['subject'] + '\n' + \
'sent : ' + self.emails[dir_no]['date'].strftime(
'%Y-%m-%d/%Y %H:%M:%S'
)
os.lseek(fh, offset, os.SEEK_SET)
return os.read(fh, length)
def main(mount_point):
root = 'in/'
account_config = config.load()
emails = imap.get_inbox_listing(account_config)
FUSE(Imap(root, emails, account_config), mount_point, nothreads=True, foreground=True)
if __name__ == '__main__':
main(sys.argv[1])