Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pyfs
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pyfs
Commits
99fad404
Commit
99fad404
authored
Oct 20, 2010
by
rfkelly0
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding DebugFS wrapper; thanks to Marek Palatinus.
This closes issue #26.
parent
04a99756
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
139 additions
and
0 deletions
+139
-0
fs/wrapfs/debugfs.py
+139
-0
No files found.
fs/wrapfs/debugfs.py
0 → 100644
View file @
99fad404
'''
@author: Marek Palatinus <marek@palatinus.cz>
@license: Public domain
DebugFS is a wrapper around filesystems to help developers
debug their work. I wrote this class mainly for debugging
TahoeFS and for fine tuning TahoeFS over Dokan with higher-level
aplications like Total Comander, Winamp etc. Did you know
that Total Commander need to open file before it delete them? :-)
I hope DebugFS can be helpful also for other filesystem developers,
especially for those who are trying to implement their first one (like me).
DebugFS prints to stdout (by default) all attempts to
filesystem interface, prints parameters and results.
Basic usage:
fs = DebugFS(OSFS('~'), identifier='OSFS@home',
\
skip=('_lock', 'listdir', 'listdirinfo'))
print fs.listdir('.')
print fs.unsupportedfunction()
Error levels:
DEBUG: Print everything (asking for methods, calls, response, exception)
INFO: Print calls, responses, exception
ERROR: Print only exceptions
CRITICAL: Print only exceptions not derived from fs.errors.FSError
How to change error level:
import logging
logger = logging.getLogger('fs.debugfs')
logger.setLevel(logging.CRITICAL)
fs = DebugFS(OSFS('~')
print fs.listdir('.')
'''
import
logging
from
logging
import
DEBUG
,
INFO
,
ERROR
,
CRITICAL
import
sys
from
fs.errors
import
FSError
logger
=
logging
.
getLogger
(
'fs.debugfs'
)
logger
.
setLevel
(
logging
.
DEBUG
)
logger
.
addHandler
(
logging
.
StreamHandler
())
class
DebugFS
(
object
):
def
__init__
(
self
,
fs
,
identifier
=
None
,
skip
=
()):
'''
fs - Reference to object to debug
identifier - Custom string-like object will be added
to each log line as identifier.
skip - list of method names which DebugFS should not log
'''
self
.
__wrapped_fs
=
fs
self
.
__identifier
=
identifier
self
.
__skip
=
skip
super
(
DebugFS
,
self
)
.
__init__
()
def
__log
(
self
,
level
,
message
):
if
self
.
__identifier
:
logger
.
log
(
level
,
'(
%
s)
%
s'
%
(
self
.
__identifier
,
message
))
else
:
logger
.
log
(
level
,
message
)
def
__parse_param
(
self
,
value
):
if
isinstance
(
value
,
basestring
):
if
len
(
value
)
>
20
:
value
=
"
%
s (length
%
d)"
%
(
repr
(
value
[:
20
]),
len
(
value
))
else
:
value
=
repr
(
value
)
elif
isinstance
(
value
,
list
):
value
=
"
%
s (
%
d items)"
%
(
repr
(
value
[:
3
]),
len
(
value
))
elif
isinstance
(
value
,
dict
):
items
=
{}
for
k
,
v
in
value
.
items
()[:
3
]:
items
[
k
]
=
v
value
=
"
%
s (
%
d items)"
%
(
repr
(
items
),
len
(
value
))
else
:
value
=
repr
(
value
)
return
value
def
__parse_args
(
self
,
*
arguments
,
**
kwargs
):
args
=
[
self
.
__parse_param
(
a
)
for
a
in
arguments
]
for
k
,
v
in
kwargs
.
items
():
args
.
append
(
"
%
s=
%
s"
%
(
k
,
self
.
__parse_param
(
v
)))
args
=
','
.
join
(
args
)
if
args
:
args
=
"(
%
s)"
%
args
return
args
def
__report
(
self
,
msg
,
key
,
value
,
*
arguments
,
**
kwargs
):
if
key
in
self
.
__skip
:
return
args
=
self
.
__parse_args
(
*
arguments
,
**
kwargs
)
value
=
self
.
__parse_param
(
value
)
self
.
__log
(
INFO
,
"
%
s
%
s
%
s ->
%
s"
%
(
msg
,
str
(
key
),
args
,
value
))
def
__getattr__
(
self
,
key
):
if
key
.
startswith
(
'__'
):
# Internal calls, nothing interesting
return
object
.
__getattribute__
(
self
,
key
)
try
:
attr
=
getattr
(
self
.
__wrapped_fs
,
key
)
except
AttributeError
,
e
:
self
.
__log
(
DEBUG
,
"Asking for not implemented method
%
s"
%
key
)
raise
e
except
Exception
,
e
:
self
.
__log
(
CRITICAL
,
"Exception
%
s:
%
s"
%
\
(
e
.
__class__
.
__name__
,
str
(
e
)))
raise
e
if
not
callable
(
attr
):
if
key
not
in
self
.
__skip
:
self
.
__report
(
"Get attribute"
,
key
,
attr
)
return
attr
def
_method
(
*
args
,
**
kwargs
):
try
:
value
=
attr
(
*
args
,
**
kwargs
)
self
.
__report
(
"Call method"
,
key
,
value
,
*
args
,
**
kwargs
)
except
FSError
,
e
:
self
.
__log
(
ERROR
,
"Call method
%
s
%
s -> Exception
%
s:
%
s"
%
\
(
key
,
self
.
__parse_args
(
*
args
,
**
kwargs
),
\
e
.
__class__
.
__name__
,
str
(
e
)))
(
exc_type
,
exc_inst
,
tb
)
=
sys
.
exc_info
()
raise
e
,
None
,
tb
except
Exception
,
e
:
self
.
__log
(
CRITICAL
,
"Call method
%
s
%
s -> Non-FS exception
%
s:
%
s"
%
\
(
key
,
self
.
__parse_args
(
*
args
,
**
kwargs
),
\
e
.
__class__
.
__name__
,
str
(
e
)))
(
exc_type
,
exc_inst
,
tb
)
=
sys
.
exc_info
()
raise
e
,
None
,
tb
return
value
if
key
not
in
self
.
__skip
:
self
.
__log
(
DEBUG
,
"Asking for method
%
s"
%
key
)
return
_method
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment