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
e8b34d83
Commit
e8b34d83
authored
Apr 06, 2010
by
rfkelly0
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add iternames() method to PathMap
parent
65057312
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
1 deletions
+31
-1
fs/path.py
+29
-1
fs/tests/test_path.py
+2
-0
No files found.
fs/path.py
View file @
e8b34d83
...
@@ -288,6 +288,7 @@ class PathMap(object):
...
@@ -288,6 +288,7 @@ class PathMap(object):
self
.
_map
=
{}
self
.
_map
=
{}
def
__getitem__
(
self
,
path
):
def
__getitem__
(
self
,
path
):
"""Get the value stored under the given path."""
m
=
self
.
_map
m
=
self
.
_map
for
name
in
iteratepath
(
path
):
for
name
in
iteratepath
(
path
):
try
:
try
:
...
@@ -300,6 +301,7 @@ class PathMap(object):
...
@@ -300,6 +301,7 @@ class PathMap(object):
raise
KeyError
(
path
)
raise
KeyError
(
path
)
def
__contains__
(
self
,
path
):
def
__contains__
(
self
,
path
):
"""Check whether the given path has a value stored in the map."""
try
:
try
:
self
[
path
]
self
[
path
]
except
KeyError
:
except
KeyError
:
...
@@ -308,6 +310,7 @@ class PathMap(object):
...
@@ -308,6 +310,7 @@ class PathMap(object):
return
True
return
True
def
__setitem__
(
self
,
path
,
value
):
def
__setitem__
(
self
,
path
,
value
):
"""Set the value stored under the given path."""
m
=
self
.
_map
m
=
self
.
_map
for
name
in
iteratepath
(
path
):
for
name
in
iteratepath
(
path
):
try
:
try
:
...
@@ -317,6 +320,7 @@ class PathMap(object):
...
@@ -317,6 +320,7 @@ class PathMap(object):
m
[
""
]
=
value
m
[
""
]
=
value
def
__delitem__
(
self
,
path
):
def
__delitem__
(
self
,
path
):
"""Delete the value stored under the given path."""
ms
=
[[
self
.
_map
,
None
]]
ms
=
[[
self
.
_map
,
None
]]
for
name
in
iteratepath
(
path
):
for
name
in
iteratepath
(
path
):
try
:
try
:
...
@@ -335,12 +339,14 @@ class PathMap(object):
...
@@ -335,12 +339,14 @@ class PathMap(object):
del
ms
[
-
1
][
0
][
ms
[
-
1
][
1
]]
del
ms
[
-
1
][
0
][
ms
[
-
1
][
1
]]
def
get
(
self
,
path
,
default
=
None
):
def
get
(
self
,
path
,
default
=
None
):
"""Get the value stored under the given path, or the given default."""
try
:
try
:
return
self
[
path
]
return
self
[
path
]
except
KeyError
:
except
KeyError
:
return
default
return
default
def
pop
(
self
,
path
,
default
=
None
):
def
pop
(
self
,
path
,
default
=
None
):
"""Pop the value stored under the given path, or the given default."""
ms
=
[[
self
.
_map
,
None
]]
ms
=
[[
self
.
_map
,
None
]]
for
name
in
iteratepath
(
path
):
for
name
in
iteratepath
(
path
):
try
:
try
:
...
@@ -368,7 +374,8 @@ class PathMap(object):
...
@@ -368,7 +374,8 @@ class PathMap(object):
m
=
m
.
setdefault
(
name
,{})
m
=
m
.
setdefault
(
name
,{})
return
m
.
setdefault
(
""
,
value
)
return
m
.
setdefault
(
""
,
value
)
def
clear
(
self
,
root
=
None
):
def
clear
(
self
,
root
=
"/"
):
"""Clear all entries beginning with the given root path."""
m
=
self
.
_map
m
=
self
.
_map
for
name
in
iteratepath
(
root
):
for
name
in
iteratepath
(
root
):
try
:
try
:
...
@@ -378,6 +385,7 @@ class PathMap(object):
...
@@ -378,6 +385,7 @@ class PathMap(object):
m
.
clear
()
m
.
clear
()
def
iterkeys
(
self
,
root
=
"/"
,
m
=
None
):
def
iterkeys
(
self
,
root
=
"/"
,
m
=
None
):
"""Iterate over all keys beginning with the given root path."""
if
m
is
None
:
if
m
is
None
:
m
=
self
.
_map
m
=
self
.
_map
for
name
in
iteratepath
(
root
):
for
name
in
iteratepath
(
root
):
...
@@ -397,6 +405,7 @@ class PathMap(object):
...
@@ -397,6 +405,7 @@ class PathMap(object):
return
list
(
self
.
iterkeys
(
root
))
return
list
(
self
.
iterkeys
(
root
))
def
itervalues
(
self
,
root
=
"/"
,
m
=
None
):
def
itervalues
(
self
,
root
=
"/"
,
m
=
None
):
"""Iterate over all values whose keys begin with the given root path."""
if
m
is
None
:
if
m
is
None
:
m
=
self
.
_map
m
=
self
.
_map
for
name
in
iteratepath
(
root
):
for
name
in
iteratepath
(
root
):
...
@@ -416,6 +425,7 @@ class PathMap(object):
...
@@ -416,6 +425,7 @@ class PathMap(object):
return
list
(
self
.
itervalues
(
root
))
return
list
(
self
.
itervalues
(
root
))
def
iteritems
(
self
,
root
=
"/"
,
m
=
None
):
def
iteritems
(
self
,
root
=
"/"
,
m
=
None
):
"""Iterate over all (key,value) pairs beginning with the given root."""
if
m
is
None
:
if
m
is
None
:
m
=
self
.
_map
m
=
self
.
_map
for
name
in
iteratepath
(
root
):
for
name
in
iteratepath
(
root
):
...
@@ -434,4 +444,22 @@ class PathMap(object):
...
@@ -434,4 +444,22 @@ class PathMap(object):
def
items
(
self
,
root
=
"/"
):
def
items
(
self
,
root
=
"/"
):
return
list
(
self
.
iteritems
(
root
))
return
list
(
self
.
iteritems
(
root
))
def
iternames
(
self
,
root
=
"/"
):
"""Iterate over all names beneath the given root path.
This is basically the equivalent of listdir() for a PathMap - it yields
the next level of name components beneath the given path.
"""
m
=
self
.
_map
for
name
in
iteratepath
(
root
):
try
:
m
=
m
[
name
]
except
KeyError
:
return
for
nm
in
m
:
if
nm
:
yield
nm
def
names
(
self
,
root
=
"/"
):
return
list
(
self
.
iternames
(
root
))
fs/tests/test_path.py
View file @
e8b34d83
...
@@ -117,5 +117,7 @@ class Test_PathMap(unittest.TestCase):
...
@@ -117,5 +117,7 @@ class Test_PathMap(unittest.TestCase):
self
.
assertEquals
(
sorted
(
map
.
items
(
"/hello/world/"
)),[(
"/hello/world"
,
1
),(
"/hello/world/howareya"
,
2
),(
"/hello/world/iamfine"
,
3
)])
self
.
assertEquals
(
sorted
(
map
.
items
(
"/hello/world/"
)),[(
"/hello/world"
,
1
),(
"/hello/world/howareya"
,
2
),(
"/hello/world/iamfine"
,
3
)])
self
.
assertEquals
(
zip
(
map
.
keys
(),
map
.
values
()),
map
.
items
())
self
.
assertEquals
(
zip
(
map
.
keys
(),
map
.
values
()),
map
.
items
())
self
.
assertEquals
(
zip
(
map
.
keys
(
"batman"
),
map
.
values
(
"batman"
)),
map
.
items
(
"batman"
))
self
.
assertEquals
(
zip
(
map
.
keys
(
"batman"
),
map
.
values
(
"batman"
)),
map
.
items
(
"batman"
))
self
.
assertEquals
(
set
(
map
.
iternames
(
"hello"
)),
set
((
"world"
,
"kitty"
)))
self
.
assertEquals
(
set
(
map
.
iternames
(
"/hello/kitty"
)),
set
((
"islame"
,)))
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