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
e16a1357
Commit
e16a1357
authored
Jun 28, 2013
by
willmcgugan@gmail.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added 'relativefrom' method to path.py
parent
961085b8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
2 deletions
+45
-2
fs/multifs.py
+2
-2
fs/path.py
+28
-0
fs/tests/test_path.py
+15
-0
No files found.
fs/multifs.py
View file @
e16a1357
...
...
@@ -127,7 +127,7 @@ class MultiFS(FS):
def
_priority_sort
(
self
):
"""Sort filesystems by priority order"""
priority_order
=
sorted
(
self
.
fs_lookup
.
keys
(),
key
=
lambda
n
:
self
.
fs_priorities
[
n
],
reverse
=
True
)
priority_order
=
sorted
(
self
.
fs_lookup
.
keys
(),
key
=
lambda
n
:
self
.
fs_priorities
[
n
],
reverse
=
True
)
self
.
fs_sequence
=
[
self
.
fs_lookup
[
name
]
for
name
in
priority_order
]
@synchronize
...
...
@@ -181,7 +181,7 @@ class MultiFS(FS):
"""
if
name
not
in
self
.
fs_lookup
:
raise
ValueError
(
"No filesystem called '
%
s'"
%
name
)
raise
ValueError
(
"No filesystem called '
%
s'"
%
name
)
fs
=
self
.
fs_lookup
[
name
]
self
.
fs_sequence
.
remove
(
fs
)
del
self
.
fs_lookup
[
name
]
...
...
fs/path.py
View file @
e16a1357
...
...
@@ -396,6 +396,29 @@ def frombase(path1, path2):
return
path2
[
len
(
path1
):]
def
relativefrom
(
base
,
path
):
"""Return a path relative from a given base path,
i.e. insert backrefs as appropriate to reach the path from the base.
:param base_path: Path to a directory
:param path: Path you wish to make relative
>>> relativefrom("foo/bar", "baz/index.html")
'../baz/index.html'
"""
base
=
list
(
iteratepath
(
base
))
path
=
list
(
iteratepath
(
path
))
while
base
and
path
and
base
[
0
]
==
path
[
0
]:
base
.
pop
(
0
)
path
.
pop
(
0
)
# If you multiply a list by a negative number, you get an empty list!
return
u'/'
.
join
([
u'..'
]
*
len
(
base
)
+
path
)
class
PathMap
(
object
):
"""Dict-like object with paths for keys.
...
...
@@ -626,3 +649,8 @@ def iswildcard(path):
if
__name__
==
"__main__"
:
print
recursepath
(
'a/b/c'
)
print
relativefrom
(
'/'
,
'/foo'
)
print
relativefrom
(
'/foo/bar'
,
'/foo/baz'
)
print
relativefrom
(
'/foo/bar/baz'
,
'/foo/egg'
)
print
relativefrom
(
'/foo/bar/baz/egg'
,
'/foo/egg'
)
fs/tests/test_path.py
View file @
e16a1357
...
...
@@ -150,6 +150,21 @@ class TestPathFunctions(unittest.TestCase):
self
.
assertFalse
(
iswildcard
(
'img.jpg'
))
self
.
assertFalse
(
iswildcard
(
'foo/bar'
))
def
test_realtivefrom
(
self
):
tests
=
[(
'/'
,
'/foo.html'
,
'foo.html'
),
(
'/foo'
,
'/foo/bar.html'
,
'bar.html'
),
(
'/foo/bar/'
,
'/egg.html'
,
'../../egg.html'
),
(
'/a/b/c/d'
,
'e'
,
'../../../../e'
),
(
'/a/b/c/d'
,
'a/d'
,
'../../../d'
),
(
'/docs/'
,
'tags/index.html'
,
'../tags/index.html'
),
(
'foo/bar'
,
'baz/index.html'
,
'../../baz/index.html'
),
(
''
,
'a'
,
'a'
),
(
'a'
,
'b/c'
,
'../b/c'
)
]
for
base
,
path
,
result
in
tests
:
self
.
assertEqual
(
relativefrom
(
base
,
path
),
result
)
class
Test_PathMap
(
unittest
.
TestCase
):
...
...
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