2 # -*- coding: utf-8 -*-
4 # "THE BEER-WARE LICENSE" (Revision 42):
5 # Enko <enko@thewired.hacked.jp> wrote this file. As long as you
6 # retain this notice you can do whatever you want with this stuff. If
7 # we meet some day, and you think this stuff is worth it, you can buy
13 fuse
.fuse_python_api
= (0, 2)
16 from datetime
import date
23 class QuasselFileStat(fuse
.Stat
):
36 class QuasselFS(Fuse
):
40 def __init__(self
, *args
, **kw
):
41 Fuse
.__init
__(self
, *args
, **kw
)
43 self
.groupings
= ("daily", "weekly", "monthly")
44 self
.log
= quasseltool
.Logutil()
46 def _getbuf(self
, path
):
47 if QuasselFS
._bufs
.has_key(path
):
48 QuasselFS
._bufs
[path
].update()
50 QuasselFS
._bufs
[path
] = QuasselFS
.Buf(path
)
51 return QuasselFS
._bufs
[path
]
53 def getattr(self
, path
):
54 print ("getattr",path
)
55 st
= QuasselFileStat()
58 st
.st_ctime
= time
.time()
59 st
.st_atime
= time
.time()
60 st
.st_mtime
= time
.time()
62 params
= path
.split("/")
63 paramcount
= len(params
)
65 st
.st_mode
= stat
.S_IFDIR |
0755
67 elif (paramcount
== 2) and (len(params
[1]) > 0) and self
.log
.is_user(params
[1]):
69 st
.st_mode
= stat
.S_IFDIR |
0755
71 elif (paramcount
== 3) and self
.log
.is_network(params
[1], params
[2]):
73 st
.st_mode
= stat
.S_IFDIR |
0755
75 elif (paramcount
>= 4) and self
.log
.is_buffer(params
[1], params
[2], params
[3]):
76 #/user/network/channel/
78 st
.st_mode
= stat
.S_IFDIR |
0755
81 if params
[4] == params
[3]+".log":
82 #/user/network/channel/channel.log
83 st
.st_mode
= stat
.S_IFREG |
0444
86 #log = getlog(params[1],params[2],time.strptime(params[3].split(".")[0],"%Y-%m-%d"))
87 st
.st_size
= len(self
._getbuf
(path
))
90 #/user/network/channel/weekly/
91 st
.st_mode
= stat
.S_IFDIR |
0755
94 #/user/network/channel/daily/2008-11-23.log
95 st
.st_mode
= stat
.S_IFREG |
0444
97 #TODO: proper size calculations, maybe with time-based Invalidation of Buf objects
98 #log = getlog(params[1],params[2],time.strptime(params[3].split(".")[0],"%Y-%m-%d"))
99 st
.st_size
= len(self
._getbuf
(path
))
105 def readdir(self
, path
, offset
):
106 #print("readdir",path, offset)
109 params
= path
.split("/")
110 paramcount
= len(params
)
112 #print ("param",params,paramcount)
114 ret
.extend(self
.log
.get_users())
115 elif (paramcount
== 2) and (len(params
[1]) > 0):
117 ret
.extend(self
.log
.get_networks(params
[1]))
118 elif (paramcount
== 3):
120 ret
.extend(self
.log
.get_buffers(params
[1], params
[2]))
122 elif (paramcount
== 4):
123 #/user/network/channel
124 ret
.append(params
[3]+".log")
125 ret
.extend(self
.groupings
)
128 yield fuse
.Direntry(r
)
131 def __init__(self
, path
):
133 self
.params
= path
.split("/")
138 return len(self
.string
)
141 if time
.time() > self
.expires
:
142 log
= quasseltool
.Logutil()
143 if len(self
.params
) > 4:
144 if log
.is_buffer(self
.params
[1], self
.params
[2], self
.params
[3]) and self
.params
[4] == self
.params
[3]+".log":
147 #expires every 10 minutes
148 self
.expires
= time
.time()+600
150 def write(self
, string
):
151 #print ("string", string)
152 self
.string
+= string
153 #self.pos += len(string)
155 def read(self
, size
, offset
):
156 print ("read", self
.params
)
157 slen
= len(self
.string
)
159 if offset
+ size
> slen
:
161 buf
= self
.string
[offset
:offset
+size
]
166 #TODO: if buf is unicode then this will fail
170 return buf
.decode("iso8859_15", "replace")
172 def _fill(self
, log
):
173 oldtime
= time
.time()
174 print "start: "+str(oldtime
)
175 log
.getlog(self
.params
[1], self
.params
[2], self
.params
[3], self
)
176 newtime
= time
.time()
177 print "end: "+str(newtime
)+" Duration: "+str(newtime
- oldtime
)
179 def read(self
, path
, size
, offset
):
180 print ("def read", path
, size
, offset
)
181 buf
= self
._getbuf
(path
)
182 return buf
.read(size
, offset
)
188 Userspace hello example
191 server
= QuasselFS(version
="%prog " + fuse
.__version
__,
193 dash_s_do
='setsingle')
195 server
.parse(values
=server
, errex
=1)
198 if __name__
=='__main__':