Mercurial > danboorufs
annotate danboorufs.py @ 2:85cbd44f98b1 draft
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 21 Aug 2012 20:17:49 +0200 |
parents | 63ccd8b0d615 |
children | 880904f1071f |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python2 |
2 # -*- encoding: utf-8 -*- | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
3 # |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
4 # |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
5 # Copyright © 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
6 # |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
7 # Permission is hereby granted, free of charge, to any person obtaining a copy |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
8 # of this software and associated documentation files (the "Software"), to deal |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
9 # in the Software without restriction, including without limitation the rights |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
11 # copies of the Software, and to permit persons to whom the Software is |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
12 # furnished to do so, subject to the following conditions: |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
13 # |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
14 # The above copyright notice and this permission notice shall be included in |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
15 # all copies or substantial portions of the Software. |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
16 # |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
23 # SOFTWARE. |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
24 |
0 | 25 |
26 from __future__ import with_statement | |
27 | |
28 from errno import ENOENT, ENOTDIR | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
29 from sys import argv |
0 | 30 from threading import Lock |
31 from time import time | |
32 | |
33 import os | |
34 | |
35 from fuse import FUSE, FuseOSError, Operations, LoggingMixIn | |
36 | |
37 | |
38 class Danbooru(LoggingMixIn, Operations): | |
39 ''' | |
40 Represent a list of images as a filesystem tree, with nice tag filtering. | |
41 ''' | |
42 | |
43 def __init__(self, tagfiles, root): | |
44 ''' | |
45 Takes a list of files containing the tags. They have to be named as the | |
46 image, with ".tags" at the end. | |
47 ''' | |
48 self.paths = {} | |
49 self.files = {} | |
50 self.tags = {} | |
51 self.cache = {} | |
52 | |
53 start = time() | |
54 | |
55 for name in tagfiles: | |
56 filename = name.replace('.tags', '') | |
57 basename = os.path.basename(filename) | |
58 self.paths[basename] = filename | |
59 tags = [] | |
60 self.files[basename] = tags | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
61 with open(name, 'r') as tagfile: |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
62 for line in tagfile: |
0 | 63 for tag in line.split(): |
64 tag = tag.decode('UTF-8') | |
1
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
65 tag = tag.replace('/', u'�') #XXX |
0 | 66 tags.append(tag) |
67 self.tags.setdefault(tag, []).append(basename) | |
68 | |
69 print('[%d] Index done.' % (time() - start)) | |
70 | |
71 self.root = root | |
72 self.rwlock = Lock() | |
73 | |
74 def _split_path(self, path): | |
75 if path == '/': | |
76 return (None, None) | |
77 | |
1
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
78 real_path = path[1:].split('/') |
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
79 |
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
80 # Remove the leading - of tag exclusion. |
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
81 path = [tag[1:] if tag[0] == '-' else tag for tag in real_path] |
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
82 |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
83 for tag in path[:-1]: |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
84 if tag not in self.tags: |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
85 raise FuseOSError(ENOENT) |
0 | 86 |
87 if path[-1] in self.tags: | |
1
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
88 return (real_path, None) |
0 | 89 |
90 if path[-1] not in self.paths: | |
91 raise FuseOSError(ENOENT) | |
92 | |
1
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
93 return (real_path[:-1], self.paths[real_path[-1]]) |
0 | 94 |
95 def access(self, path, mode): | |
96 self._split_path(path) | |
97 | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
98 def getattr(self, path, file_handle=None): |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
99 _, filename = self._split_path(path) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
100 path = filename if filename else self.root |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
101 stat = os.lstat(path) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
102 return dict((key, getattr(stat, key)) for key in ('st_atime', |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
103 'st_ctime', 'st_gid', 'st_mode', 'st_mtime', |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
104 'st_nlink', 'st_size', 'st_uid')) |
0 | 105 |
106 getxattr = None | |
107 listxattr = None | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
108 |
0 | 109 def open(self, path, flags): |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
110 _, filename = self._split_path(path) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
111 return os.open(filename, flags) |
0 | 112 |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
113 def read(self, path, size, offset, file_handle): |
0 | 114 with self.rwlock: |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
115 os.lseek(file_handle, offset, 0) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
116 return os.read(file_handle, size) |
0 | 117 |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
118 def readdir(self, path, file_handle): |
0 | 119 if path == '/': |
120 return ['.', '..'] + self.tags.keys() + self.files.keys() | |
121 | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
122 tags, filename = self._split_path(path) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
123 if filename: |
0 | 124 return FuseOSError(ENOTDIR) |
125 | |
126 tags = set(tags) | |
127 | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
128 key = ' '.join(tags) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
129 if key in self.cache: |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
130 return ['.', '..'] + self.cache[key] |
0 | 131 |
1
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
132 inclusion_tags = set(tag for tag in tags if tag[0] != '-') |
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
133 exclusion_tags = set(tag[1:] for tag in tags if tag[0] == '-') |
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
134 |
0 | 135 # Get the list of the files corresponding to those tags. |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
136 files = reduce((lambda s, t: s.intersection(self.tags[t])), |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
137 inclusion_tags, set(self.files)) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
138 files -= set([f for f in files |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
139 if exclusion_tags.intersection(self.files[f])]) |
1
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
140 |
63ccd8b0d615
Add tag exclusion support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
141 # Those next two steps are for useless tags removal. |
0 | 142 |
143 # Get the tags of those files. | |
144 taglist = reduce((lambda s, f: s.union(self.files[f])), files, set()) | |
145 taglist -= tags | |
146 | |
147 # Remove the tags that can’t precise the file list anymore. | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
148 remove = reduce((lambda s, f: s.intersection(self.files[f])), files, |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
149 taglist) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
150 taglist -= remove |
0 | 151 |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
152 self.cache[key] = list(taglist) + list(files) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
153 return ['.', '..'] + self.cache[key] |
0 | 154 |
155 readlink = os.readlink | |
156 | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
157 def release(self, path, file_handle): |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
158 return os.close(file_handle) |
0 | 159 |
160 def statfs(self, path): | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
161 _, filename = self._split_path(path) |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
162 path = filename if filename else self.root |
0 | 163 stv = os.statvfs(path) |
164 return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree', | |
165 'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag', | |
166 'f_frsize', 'f_namemax')) | |
167 | |
168 utimens = os.utime | |
169 | |
170 | |
171 if __name__ == '__main__': | |
172 if len(argv) < 3: | |
173 print('usage: %s <tag file> [<tag file>...] <mountpoint>' % argv[0]) | |
174 exit(1) | |
175 | |
176 mountpoint = argv.pop() | |
177 | |
2
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
178 fuse = FUSE(Danbooru(argv[1:], os.path.dirname(mountpoint)), mountpoint, |
85cbd44f98b1
Add license, (try to) respect the PEP 8, and don’t override the file builtin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
179 foreground=True) |