Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webcache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webcache
Commits
ee28c5c8
Commit
ee28c5c8
authored
Feb 07, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added basis set of functions for file collections to Cache object.
parent
db024017
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
2 deletions
+134
-2
src/cache.py
src/cache.py
+70
-1
tests/test_cache.py
tests/test_cache.py
+64
-1
No files found.
src/cache.py
View file @
ee28c5c8
from
os.path
import
getmtime
,
exists
def
to_dir
(
path
):
"""
Transform a path to a directory, that can be used as prefix before a
filename.
"""
if
not
path
:
return
''
if
path
[
-
1
]
!=
'/'
:
return
path
+
'/'
return
path
class
Cache
:
pass
"""
A Cache instance represents a single file in the cache directory, which is
a concatenation of the file list.
"""
def
__init__
(
self
,
root
=
''
,
files
=
[],
cached
=
'cached'
):
"""
The constructor takes the following arguments (all are optional):
1. The directory in which the cached files are located (empty by
default).
2. An initial list of number of files to include in the cahce file.
3. The cached files directory (defaults to 'cached').
"""
self
.
root
=
to_dir
(
root
)
self
.
cached
=
to_dir
(
cached
)
self
.
files
=
map
(
lambda
f
:
self
.
root
+
f
,
files
)
self
.
assert_files_exist
()
def
assert_files_exist
(
self
):
for
path
in
self
.
files
:
if
not
exists
(
path
):
raise
IOError
(
'File "%s" does not exist.'
%
path
)
def
add
(
self
,
path
,
absolute
=
False
):
"""
Add a file to the cache object. Requires the full path to the file,
relative to the root directory. If the second argument is True, the
root directory will not be prepended to the path.
"""
if
not
absolute
:
path
=
self
.
root
+
path
self
.
files
.
append
(
path
)
def
remove
(
self
,
path
,
absolute
=
False
):
"""
Remove a file from the cache object. Requires the full path to the
file, relative to the root directory. If the second argument is True,
the root directory will not be prepended to the path.
"""
if
not
absolute
:
path
=
self
.
root
+
path
self
.
files
.
remove
(
path
)
def
get_modification_dates
(
self
):
"""
Get the latest modification dates fo each file in the cache object.
"""
self
.
modified
=
{}
for
path
in
self
.
files
:
self
.
modified
[
path
]
=
getmtime
(
path
)
tests/test_cache.py
View file @
ee28c5c8
import
unittest
import
os
from
shutil
import
rmtree
from
src.cache
import
to_dir
,
Cache
class
TestCache
(
unittest
.
TestCase
):
pass
def
setUp
(
self
):
os
.
mkdir
(
'baz'
)
for
name
in
(
'foo'
,
'bar'
):
f
=
open
(
'baz/'
+
name
,
'w'
)
f
.
write
(
name
)
f
.
close
()
self
.
c
=
Cache
(
files
=
[
'foo'
,
'bar'
],
root
=
'baz'
)
def
tearDown
(
self
):
rmtree
(
'baz'
)
def
test_to_dir
(
self
):
self
.
assertEqual
(
to_dir
(
''
),
''
)
self
.
assertEqual
(
to_dir
(
'foo'
),
'foo/'
)
self
.
assertEqual
(
to_dir
(
'foo/'
),
'foo/'
)
def
test___init__
(
self
):
self
.
assertTrue
(
hasattr
(
self
.
c
,
'root'
))
self
.
assertEqual
(
self
.
c
.
root
,
'baz/'
)
self
.
assertTrue
(
hasattr
(
self
.
c
,
'files'
))
self
.
assertEqual
(
self
.
c
.
files
,
[
'baz/foo'
,
'baz/bar'
])
self
.
assertTrue
(
hasattr
(
self
.
c
,
'cached'
))
self
.
assertEqual
(
self
.
c
.
cached
,
'cached/'
)
def
test___init___custom_root
(
self
):
c
=
Cache
(
root
=
'foo'
)
self
.
assertTrue
(
hasattr
(
c
,
'root'
))
self
.
assertEqual
(
c
.
root
,
'foo/'
)
def
test___init___custom_cached_dir
(
self
):
c
=
Cache
(
cached
=
'foo'
)
self
.
assertTrue
(
hasattr
(
c
,
'cached'
))
self
.
assertEqual
(
c
.
cached
,
'foo/'
)
def
test___init___non_existant_file
(
self
):
self
.
assertRaises
(
IOError
,
Cache
,
files
=
[
'foo'
])
def
test_add
(
self
):
self
.
c
.
add
(
'baz'
)
self
.
assertIn
(
'baz/baz'
,
self
.
c
.
files
)
def
test_add_non_relative
(
self
):
self
.
c
.
add
(
'baz'
,
absolute
=
True
)
self
.
assertIn
(
'baz'
,
self
.
c
.
files
)
def
test_remove
(
self
):
self
.
c
.
remove
(
'foo'
)
self
.
assertNotIn
(
'baz/foo'
,
self
.
c
.
files
)
def
test_remove_non_relative
(
self
):
self
.
c
.
remove
(
'baz/foo'
,
absolute
=
True
)
self
.
assertNotIn
(
'baz/foo'
,
self
.
c
.
files
)
def
test_get_modification_dates
(
self
):
self
.
c
.
get_modification_dates
()
self
.
assertIn
(
'baz/foo'
,
self
.
c
.
modified
)
self
.
assertEqual
(
self
.
c
.
modified
[
'baz/foo'
],
os
.
path
.
getmtime
(
'baz/foo'
))
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