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
ff191617
Commit
ff191617
authored
Jul 26, 2008
by
willmcgugan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a getinfo method. Expanded browsewin utility to show filesize and created date
parent
aece1874
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
105 additions
and
122 deletions
+105
-122
fs/browsewin.py
+35
-10
fs/fs.py
+52
-103
fs/memoryfs.py
+18
-9
No files found.
fs/browsewin.py
View file @
ff191617
#!/usr/bin/env python
import
wx
import
wx.gizmos
import
fs
...
...
@@ -15,8 +16,13 @@ class BrowseFrame(wx.Frame):
self
.
fs
=
fs
self
.
SetTitle
(
"FS Browser - "
+
str
(
fs
))
self
.
tree
=
wx
.
TreeCtrl
(
self
,
-
1
)
self
.
root_id
=
self
.
tree
.
AddRoot
(
str
(
fs
),
data
=
wx
.
TreeItemData
(
{
'path'
:
"/"
,
'expanded'
:
False
}
))
self
.
tree
=
wx
.
gizmos
.
TreeListCtrl
(
self
,
-
1
,
style
=
wx
.
TR_DEFAULT_STYLE
|
wx
.
TR_HIDE_ROOT
)
self
.
tree
.
AddColumn
(
"FS"
,
300
)
self
.
tree
.
AddColumn
(
"Size"
,
150
)
self
.
tree
.
AddColumn
(
"Created"
,
250
)
self
.
root_id
=
self
.
tree
.
AddRoot
(
'root'
,
data
=
wx
.
TreeItemData
(
{
'path'
:
"/"
,
'expanded'
:
False
}
))
rid
=
self
.
tree
.
GetItemData
(
self
.
root_id
)
isz
=
(
16
,
16
)
il
=
wx
.
ImageList
(
isz
[
0
],
isz
[
1
])
...
...
@@ -32,6 +38,10 @@ class BrowseFrame(wx.Frame):
self
.
Bind
(
wx
.
EVT_TREE_ITEM_EXPANDING
,
self
.
OnItemExpanding
)
wx
.
CallAfter
(
self
.
OnInit
)
def
OnInit
(
self
):
self
.
expand
(
self
.
root_id
)
...
...
@@ -60,19 +70,34 @@ class BrowseFrame(wx.Frame):
name
=
fs
.
pathsplit
(
new_path
)[
-
1
]
if
not
is_dir
and
name
.
endswith
(
'.txt'
):
txt
=
self
.
fs
.
open
(
new_path
)
.
readline
()[:
50
]
.
rstrip
()
name
+=
" - "
+
txt
new_item
=
self
.
tree
.
AppendItem
(
item_id
,
name
,
data
=
wx
.
TreeItemData
({
'path'
:
new_path
,
'expanded'
:
False
}))
info
=
self
.
fs
.
getinfo
(
new_path
)
if
is_dir
:
self
.
tree
.
SetItemHasChildren
(
new_item
)
self
.
tree
.
SetItemImage
(
new_item
,
self
.
fldridx
,
wx
.
TreeItemIcon_Normal
)
self
.
tree
.
SetItemImage
(
new_item
,
self
.
fldropenidx
,
wx
.
TreeItemIcon_Expanded
)
self
.
tree
.
SetItemImage
(
new_item
,
self
.
fldridx
,
0
,
wx
.
TreeItemIcon_Normal
)
self
.
tree
.
SetItemImage
(
new_item
,
self
.
fldropenidx
,
0
,
wx
.
TreeItemIcon_Expanded
)
self
.
tree
.
SetItemText
(
new_item
,
""
,
1
)
ct
=
info
.
get
(
'created_time'
,
None
)
if
ct
is
not
None
:
self
.
tree
.
SetItemText
(
new_item
,
ct
.
ctime
(),
2
)
else
:
self
.
tree
.
SetItemText
(
new_item
,
'unknown'
,
2
)
else
:
self
.
tree
.
SetItemImage
(
new_item
,
self
.
fileidx
,
0
,
wx
.
TreeItemIcon_Normal
)
self
.
tree
.
SetItemText
(
new_item
,
str
(
info
.
get
(
'size'
,
'?'
))
+
" bytes"
,
1
)
ct
=
info
.
get
(
'created_time'
,
None
)
if
ct
is
not
None
:
self
.
tree
.
SetItemText
(
new_item
,
ct
.
ctime
(),
2
)
else
:
self
.
tree
.
SetItemImage
(
new_item
,
self
.
fileidx
,
wx
.
TreeItemIcon_Normal
)
self
.
tree
.
SetItemText
(
new_item
,
'unknown'
,
2
)
item_data
[
'expanded'
]
=
True
self
.
tree
.
Expand
(
item_id
)
...
...
fs/fs.py
View file @
ff191617
...
...
@@ -2,6 +2,7 @@ import os
import
os.path
import
fnmatch
from
itertools
import
chain
import
datetime
error_msgs
=
{
...
...
@@ -11,6 +12,7 @@ error_msgs = {
"NO_DIR"
:
"Directory does not exist:
%(path)
s"
,
"NO_FILE"
:
"No such file:
%(path)
s"
,
"LISTDIR_FAILED"
:
"Unable to get directory listing:
%(path)
s"
,
"DELETE_FAILED"
:
"Unable to delete file:
%(path)
s"
,
"NO_SYS_PATH"
:
"No mapping to OS filesytem:
%(path)
s,"
,
"DIR_EXISTS"
:
"Directory exists (try allow_recreate=True):
%(path)
s"
,
"OPEN_FAILED"
:
"Unable to open file:
%(path)
s"
,
...
...
@@ -201,6 +203,10 @@ class FS(object):
sub_fs
=
SubFS
(
self
,
dirname
)
return
sub_fs
def
remove
(
self
,
filepath
):
pass
def
_listdir_helper
(
self
,
path
,
paths
,
wildcard
,
full
,
absolute
,
hidden
,
dirs_only
,
files_only
):
...
...
@@ -252,6 +258,10 @@ class FS(object):
yield
path
def
getsize
(
self
,
path
):
return
self
.
info
(
path
)[
'size'
]
class
SubFS
(
FS
):
...
...
@@ -289,7 +299,7 @@ class OSFS(FS):
def
__init__
(
self
,
root_path
):
expanded_path
=
normpath
(
os
.
path
.
expanduser
(
root_path
))
expanded_path
=
normpath
(
os
.
path
.
expanduser
(
os
.
path
.
expandvars
(
root_path
)
))
if
not
os
.
path
.
exists
(
expanded_path
):
raise
FSError
(
"NO_DIR"
,
expanded_path
,
msg
=
"Root directory does not exist:
%(path)
s"
)
...
...
@@ -361,132 +371,70 @@ class OSFS(FS):
def
remove
(
self
,
path
):
sys_path
=
self
.
getsyspath
(
path
)
try
:
os
.
remove
(
sys_path
)
except
OSError
,
e
:
raise
FSError
(
"FILE_DELETE_FAILED"
,
path
,
details
=
e
)
def
remove_dir
(
self
,
path
,
recursive
=
False
):
sys_path
=
self
.
getsyspath
(
path
)
if
recursive
:
class
MountFS
(
FS
):
TYPE_FILE
,
TYPE_DIR
,
TYPE_FS
=
range
(
3
)
class
DirEntry
(
object
):
def
__init__
(
self
,
type
,
name
,
contents
):
assert
filename
or
dirname
or
fs
,
"Must specifiy a filename, a dirname or a fs!"
self
.
type
=
type
self
.
name
=
name
self
.
contents
=
contents
def
isdir
(
self
):
return
self
.
type
==
TYPE_DIR
def
isfile
(
self
):
return
self
.
type
==
TYPE_FILE
def
isfs
(
self
):
return
self
.
type
==
TYPE_FS
def
__init__
(
self
):
self
.
parent
=
None
self
.
root
=
{}
def
mkdir
(
self
,
dirpath
,
recursive
=
True
):
if
recursive
and
'/'
in
dirpath
:
raise
PathError
(
"NO_DIR"
,
dirpath
,
msg
=
"Use recursive=True to create this path:
%(path)
s"
)
def
do_mkdir
(
dirname
):
if
dirname
not
in
current_dir
:
current_dir
[
"dirname"
]
=
{}
return
True
return
False
current_dir
=
self
.
root
for
path_component
in
split_path
(
current_dir
):
if
path_component
in
current_dir
:
if
not
current_dir
[
path_component
]
.
isdir
():
raise
PathError
(
"NO_DIR"
,
dirpath
,
msg
=
"Path references a file, not a dir:
%(path)
s"
)
current_dir
[
path_component
]
=
DirEntry
(
TYPE_DIR
,
path_component
,
{})
current_dir
=
current_dir
[
path_component
]
.
contents
return
self
def
mountdir
(
self
,
dirname
,
dirfs
,
params
=
None
,
create_path
=
True
):
if
dirname
in
self
.
dir_mounts
:
raise
FSError
(
"MOUNT_NOT_FREE"
,
dirname
,
msg
=
"A directory of this name is already mounted"
)
success
,
code
=
dirfs
.
_onmount
(
self
)
if
success
:
self
.
dir_mounts
[
dirname
]
=
dirfs
return
code
def
unmountdir
(
self
,
dirname
):
if
dirname
not
in
self
.
dir_mounts
:
raise
FSError
(
"NOT_MOUNTED"
,
"Directory not mounted"
,
dirname
)
dirfs
=
self
.
dir_mounts
[
dirname
]
success
=
dirfs
.
_onunmount
(
self
)
if
success
:
del
dirfs
[
dirname
]
return
code
try
:
os
.
rmdir
(
sys_path
)
except
OSError
,
e
:
raise
FSError
(
"DIR_DELETE_FAILED"
,
path
,
details
=
e
)
else
:
try
:
os
.
removedirs
(
sys_path
)
except
OSError
,
e
:
raise
FSError
(
"DIR_DELETE_FAILED"
,
path
,
details
=
e
)
def
mountfile
(
self
,
filename
,
callable
,
params
=
None
,
create_path
=
True
):
pass
def
unmountfile
(
self
,
filename
):
pass
def
getinfo
(
self
,
path
):
sys_path
=
self
.
getsyspath
(
path
)
try
:
stats
=
os
.
stat
(
sys_path
)
except
OSError
,
e
:
raise
FSError
(
"UNKNOWN_ERROR"
,
path
,
details
=
e
)
def
_onmount
(
self
,
parent
):
pass
info
=
dict
((
k
,
getattr
(
stats
,
k
))
for
k
in
dir
(
stats
)
if
not
k
.
startswith
(
'__'
)
)
def
_onunmount
(
self
,
parent
):
pass
info
[
'size'
]
=
info
[
'st_size'
]
ct
=
info
.
get
(
'st_ctime'
,
None
)
if
ct
is
not
None
:
info
[
'created_time'
]
=
datetime
.
datetime
.
fromtimestamp
(
ct
)
def
open
(
self
,
pathname
,
params
=
None
):
pass
at
=
info
.
get
(
'st_atime'
,
None
)
if
at
is
not
None
:
info
[
'accessed_time'
]
=
datetime
.
datetime
.
fromtimestamp
(
at
)
def
opendir
(
self
,
dirname
,
params
=
None
):
mt
=
info
.
get
(
'st_mtime'
,
None
)
if
mt
is
not
None
:
info
[
'modified_time'
]
=
datetime
.
datetime
.
fromtimestamp
(
at
)
pass
return
info
def
isdir
(
self
,
dirname
):
pass
def
isfile
(
self
,
filename
):
pass
def
getsize
(
self
,
path
):
def
listdir
(
self
,
pathname
,
absolute
=
False
):
pass
sys_path
=
self
.
getsyspath
(
path
)
def
mkdir
(
self
,
pathname
):
pass
try
:
stats
=
os
.
stat
(
sys_path
)
except
OSError
,
e
:
raise
FSError
(
"UNKNOWN_ERROR"
,
path
,
details
=
e
)
def
exists
(
self
,
filename
):
pass
return
stats
.
st_size
def
getsyspath
(
self
,
filename
):
return
None
...
...
@@ -497,6 +445,7 @@ if __name__ == "__main__":
for
filename
in
osfs
.
walk_files
(
"/prettycharts"
,
"*.pov"
):
print
filename
print
osfs
.
info
(
filename
)
import
browsewin
browsewin
.
browse
(
osfs
)
...
...
fs/memoryfs.py
View file @
ff191617
#!/usr/bin/env python
import
os
import
datetime
from
fs
import
FS
,
pathsplit
,
_iteratepath
,
FSError
,
print_fs
try
:
...
...
@@ -119,6 +120,8 @@ class MemoryFS(FS):
self
.
locks
=
0
self
.
created_time
=
datetime
.
datetime
.
now
()
def
lock
(
self
):
self
.
locks
+=
1
...
...
@@ -144,14 +147,6 @@ class MemoryFS(FS):
def
__str__
(
self
):
return
"
%
s:
%
s"
%
(
self
.
name
,
self
.
desc_contents
())
class
FileEntry
(
object
):
def
__init__
(
self
):
self
.
memory_file
=
None
self
.
value
=
""
def
_make_dir_entry
(
self
,
*
args
,
**
kwargs
):
return
self
.
dir_entry_factory
(
*
args
,
**
kwargs
)
...
...
@@ -161,6 +156,9 @@ class MemoryFS(FS):
self
.
dir_entry_factory
=
MemoryFS
.
DirEntry
self
.
root
=
self
.
_make_dir_entry
(
'dir'
,
'root'
)
def
__str__
(
self
):
return
"<MemoryFS>"
def
_get_dir_entry
(
self
,
dirpath
):
current_dir
=
self
.
root
...
...
@@ -310,7 +308,6 @@ class MemoryFS(FS):
raise
FSError
(
"NO_FILE"
,
path
)
def
_on_close_memory_file
(
self
,
path
,
value
):
filepath
,
filename
=
pathsplit
(
path
)
...
...
@@ -327,6 +324,18 @@ class MemoryFS(FS):
return
self
.
_listdir_helper
(
path
,
paths
,
wildcard
,
full
,
absolute
,
hidden
,
dirs_only
,
files_only
)
def
getinfo
(
self
,
path
):
dir_entry
=
self
.
_get_dir_entry
(
path
)
info
=
{}
info
[
'created_time'
]
=
dir_entry
.
created_time
if
dir_entry
.
isfile
():
info
[
'size'
]
=
len
(
dir_entry
.
data
)
return
info
def
ishidden
(
self
,
pathname
):
return
False
...
...
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