From 9b11dd0d870ecb30baa7a7bbe33f648c8a73dd0d Mon Sep 17 00:00:00 2001 From: Simeon Warner Date: Wed, 27 Feb 2013 21:21:19 -0500 Subject: [PATCH] Give control over whether path attribute is set from build --- resync/resource_list_builder.py | 18 ++++++++++++++---- resync/test/test_resource_list_builder.py | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/resync/resource_list_builder.py b/resync/resource_list_builder.py index 0ab3e0c..096f5b0 100644 --- a/resync/resource_list_builder.py +++ b/resync/resource_list_builder.py @@ -56,7 +56,7 @@ class ResourceListBuilder(): return(True) return(False) - def from_disk(self,resource_list=None): + def from_disk(self, resource_list=None, set_path=False): """Create or extend resource_list with resources from disk scan Assumes very simple disk path to URL mapping: chop path and @@ -66,6 +66,9 @@ class ResourceListBuilder(): If a resource_list is specified then items are added to that rather than creating a new one. + If set_path is True then the path attribue will be set with the + local path for each Resource. + mapper=Mapper('http://example.org/path','/path/to/files') mb = ResourceListBuilder(mapper=mapper) m = resource_list_from_disk() @@ -79,10 +82,15 @@ class ResourceListBuilder(): # Run for each map in the mappings for map in self.mapper.mappings: self.logger.info("Scanning disk for %s" % (str(map))) - self.from_disk_add_map(resource_list=resource_list, map=map) + self.from_disk_add_map(resource_list=resource_list, map=map, set_path=set_path) return(resource_list) - def from_disk_add_map(self, resource_list=None, map=None): + def from_disk_add_map(self, resource_list=None, map=None, set_path=False): + """Add to resource_list with resources from disk scan based one map + + If set_path is True then the path attribue will be set with the + local path for each Resource. + """ # sanity if (resource_list is None or map is None): raise ValueError("Must specify resource_list and map") @@ -111,7 +119,9 @@ class ResourceListBuilder(): sys.stderr.write("Ignoring file %s (error: %s)" % (file,str(e))) continue timestamp = file_stat.st_mtime #UTC - r = Resource(uri=uri,timestamp=timestamp,path=file) + r = Resource(uri=uri,timestamp=timestamp) + if (set_path): + r.path=file if (self.do_md5): # add md5 r.md5=compute_md5_for_file(file) diff --git a/resync/test/test_resource_list_builder.py b/resync/test/test_resource_list_builder.py index effe26f..2da6e72 100644 --- a/resync/test/test_resource_list_builder.py +++ b/resync/test/test_resource_list_builder.py @@ -40,7 +40,7 @@ class TestResourceListBuilder(unittest.TestCase): def test4_data(self): rlb = ResourceListBuilder(do_md5=True) rlb.mapper = Mapper(['http://example.org/t','resync/test/testdata/dir1']) - rl = rlb.from_disk() + rl = rlb.from_disk(set_path=True) self.assertEqual( len(rl), 2) r1 = rl.resources.get('http://example.org/t/file_a') self.assertTrue( r1 is not None )