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
3af4d595
Commit
3af4d595
authored
Jun 16, 2010
by
willmcgugan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added an ignore_errors keyword parameter to listdir
parent
29cbc8af
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
9 deletions
+36
-9
fs/base.py
+36
-9
No files found.
fs/base.py
View file @
3af4d595
...
@@ -496,7 +496,12 @@ class FS(object):
...
@@ -496,7 +496,12 @@ class FS(object):
return
sub_fs
return
sub_fs
def
walk
(
self
,
path
=
"/"
,
wildcard
=
None
,
dir_wildcard
=
None
,
search
=
"breadth"
):
def
walk
(
self
,
path
=
"/"
,
wildcard
=
None
,
dir_wildcard
=
None
,
search
=
"breadth"
,
ignore_errors
=
False
):
"""Walks a directory tree and yields the root path and contents.
"""Walks a directory tree and yields the root path and contents.
Yields a tuple of the path of each directory and a list of its file
Yields a tuple of the path of each directory and a list of its file
contents.
contents.
...
@@ -507,15 +512,26 @@ class FS(object):
...
@@ -507,15 +512,26 @@ class FS(object):
:param search: -- A string dentifying the method used to walk the directories. There are two such methods:
:param search: -- A string dentifying the method used to walk the directories. There are two such methods:
* 'breadth' Yields paths in the top directories first
* 'breadth' Yields paths in the top directories first
* 'depth' Yields the deepest paths first
* 'depth' Yields the deepest paths first
:param ignore_errors: Ignore any errors reading the directory
"""
"""
def
listdir
(
path
,
*
args
,
**
kwargs
):
if
ignore_errors
:
try
:
return
self
.
listdir
(
path
,
*
args
,
**
kwargs
)
except
:
return
[]
else
:
return
self
.
listdir
(
path
,
*
args
,
**
kwargs
)
if
search
==
"breadth"
:
if
search
==
"breadth"
:
dirs
=
[
path
]
dirs
=
[
path
]
while
dirs
:
while
dirs
:
current_path
=
dirs
.
pop
()
current_path
=
dirs
.
pop
()
paths
=
[]
paths
=
[]
for
filename
in
self
.
listdir
(
current_path
):
for
filename
in
listdir
(
current_path
):
path
=
pathjoin
(
current_path
,
filename
)
path
=
pathjoin
(
current_path
,
filename
)
if
self
.
isdir
(
path
):
if
self
.
isdir
(
path
):
...
@@ -535,7 +551,7 @@ class FS(object):
...
@@ -535,7 +551,7 @@ class FS(object):
elif
search
==
"depth"
:
elif
search
==
"depth"
:
def
recurse
(
recurse_path
):
def
recurse
(
recurse_path
):
for
path
in
self
.
listdir
(
recurse_path
,
wildcard
=
dir_wildcard
,
full
=
True
,
dirs_only
=
True
):
for
path
in
listdir
(
recurse_path
,
wildcard
=
dir_wildcard
,
full
=
True
,
dirs_only
=
True
):
for
p
in
recurse
(
path
):
for
p
in
recurse
(
path
):
yield
p
yield
p
yield
(
recurse_path
,
self
.
listdir
(
recurse_path
,
wildcard
=
wildcard
,
files_only
=
True
))
yield
(
recurse_path
,
self
.
listdir
(
recurse_path
,
wildcard
=
wildcard
,
files_only
=
True
))
...
@@ -546,27 +562,38 @@ class FS(object):
...
@@ -546,27 +562,38 @@ class FS(object):
raise
ValueError
(
"Search should be 'breadth' or 'depth'"
)
raise
ValueError
(
"Search should be 'breadth' or 'depth'"
)
def
walkfiles
(
self
,
path
=
"/"
,
wildcard
=
None
,
dir_wildcard
=
None
,
search
=
"breadth"
):
def
walkfiles
(
self
,
path
=
"/"
,
wildcard
=
None
,
dir_wildcard
=
None
,
search
=
"breadth"
,
ignore_errors
=
False
):
"""Like the 'walk' method, but just yields files.
"""Like the 'walk' method, but just yields files.
:param path: Root path to start walking
:param path: Root path to start walking
:param wildcard: If given, only return files that match this wildcard
:param wildcard: If given, only return files that match this wildcard
:param dir_wildcard: If given, only walk directories that match the wildcard
:param dir_wildcard: If given, only walk directories that match the wildcard
:param search: Same as walk method
:param search: Same as walk method
:param ignore_errors: Ignore any errors reading the directory
"""
"""
for
path
,
files
in
self
.
walk
(
path
,
wildcard
=
wildcard
,
dir_wildcard
=
dir_wildcard
,
search
=
search
):
for
path
,
files
in
self
.
walk
(
path
,
wildcard
=
wildcard
,
dir_wildcard
=
dir_wildcard
,
search
=
search
,
ignore_errors
=
ignore_errors
):
for
f
in
files
:
for
f
in
files
:
yield
pathjoin
(
path
,
f
)
yield
pathjoin
(
path
,
f
)
def
walkdirs
(
self
,
path
=
"/"
,
wildcard
=
None
,
search
=
"breadth"
):
def
walkdirs
(
self
,
path
=
"/"
,
wildcard
=
None
,
search
=
"breadth"
,
ignore_errors
=
False
):
""" Like the 'walk' method but yields directories.
""" Like the 'walk' method but yields directories.
:param path: -- Root path to start walking
:param path: -- Root path to start walking
:param wildcard: -- If given, only return dictories that match this wildcard
:param wildcard: -- If given, only return dictories that match this wildcard
:param search: -- Same as the walk method
:param search: -- Same as the walk method
:param ignore_errors: Ignore any errors reading the directory
"""
"""
for
p
,
files
in
self
.
walk
(
path
,
wildcard
=
wildcard
,
search
=
search
):
for
p
,
files
in
self
.
walk
(
path
,
wildcard
=
wildcard
,
search
=
search
,
ignore_errors
=
ignore_errors
):
yield
p
yield
p
...
@@ -825,9 +852,9 @@ class SubFS(FS):
...
@@ -825,9 +852,9 @@ class SubFS(FS):
def
desc
(
self
,
path
):
def
desc
(
self
,
path
):
if
self
.
isdir
(
path
):
if
self
.
isdir
(
path
):
return
"Sub dir of
%
s"
%
str
(
self
.
parent
)
return
"Sub dir of
%
s"
%
str
(
self
.
parent
)
else
:
else
:
return
"File in sub dir of
%
s"
%
str
(
self
.
parent
)
return
"File in sub dir of
%
s"
%
str
(
self
.
parent
)
def
_delegate
(
self
,
path
):
def
_delegate
(
self
,
path
):
return
pathjoin
(
self
.
sub_dir
,
relpath
(
normpath
(
path
)))
return
pathjoin
(
self
.
sub_dir
,
relpath
(
normpath
(
path
)))
...
...
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