def test_manifest_add_aggregate(self): manifest = Manifest() manifest.add_aggregate("/test", createdBy="Alice W.Land", createdOn="2013-03-05T17:29:03Z", mediatype="text/plain") contains = Aggregate("/test") in manifest.aggregates self.assertTrue(contains) pass
def test_manifest_read_from_filename(self): m = Manifest(filename=TESTFN) a = m.get_aggregate("/README.txt") self.assertIsNotNone(a) self.assertEquals(a, Aggregate("/README.txt")) self.assertEquals(m.createdBy.name, "Alice W. Land") self.assertEquals(m.createdBy.orcid, "http://orcid.org/0000-0002-1825-0097") self.assertEquals(m.createdBy.uri, "http://example.com/foaf#alice") m.add_aggregate(Aggregate("www.example.org/test")) m.add_aggregate(Aggregate("www.example.org/test", created_by="test"))
def test_manifest_update_existing_aggregate(self): manifest = Manifest() manifest.add_aggregate("/test", createdBy="Alice W.Land", createdOn="2013-03-05T17:29:03Z", mediatype="text/plain") contains = Aggregate("/test") in manifest.aggregates self.assertTrue(contains) a = manifest.get_aggregate("/test") a.createdBy.name = "Deckard" a = manifest.get_aggregate("/test") self.assertEquals(a.createdBy.name, "Deckard")
def add(dir, file_or_directory, createdBy=None, createdOn=None, mediatype=None, recursive=False, verbose=False, force=False): """ Add files to a research object manifest ro add [ -d dir ] file ro add [ -d dir ] [-r] [directory] Use -r/--recursive to add subdirectories recursively If no file or directory specified, defaults to current directory. """ #TODO check for file_or_directory = '.' #TODO work out what root directory of ro is and make all files relative to that #os.path.relpath if not in_directory(file_or_directory, dir): print("Error: Can't add files outside the ro directory") return 1 files = [] if os.path.isdir(file_or_directory): if recursive: for dirpath, dirnames, filenames in os.walk(file_or_directory): #make the path relative to the top dir of the ro dirpath = os.path.relpath(dirpath, dir) files += [os.path.join(dirpath, filename) for filename in filenames] else: files = [os.path.join(file_or_directory, filename) for filename in os.listdir(file_or_directory) if os.path.isfile(os.path.join(file_or_directory, filename))] else: if os.path.isfile(file_or_directory): files = [file_or_directory] else: print("Error - File does not exist: {}".format(file_or_directory)) # Read and update manifest if verbose: print("ro add -d ") #TODO fix print manifest_file_path = manifest_file(dir) manifest = Manifest(filename=manifest_file_path) for file in files: file = sanitize_filename_for_identifier(file, dir) manifest.add_aggregate(file, createdBy=createdBy, createdOn=createdOn, mediatype=mediatype) with open(manifest_file_path, 'w') as manifest_filehandle: manifest_filehandle.write(manifest.to_json()) return 0
def test_manifest_remove_aggregate(self): manifest = Manifest() manifest.add_aggregate("/test1", createdBy="Alice W.Land", createdOn="2013-03-05T17:29:03Z", mediatype="text/plain") manifest.add_aggregate("/test2", createdBy="Deckard", createdOn="2013-04-03T14:12:55Z", mediatype="text/plain") self.assertIn(Aggregate("/test1"), manifest.aggregates) manifest.remove_aggregate("/test1") self.assertNotIn(Aggregate("/test1"), manifest.aggregates) self.assertIn(Aggregate("/test2"), manifest.aggregates) aggregate = manifest.get_aggregate("/test2") manifest.remove_aggregate(aggregate) self.assertNotIn(aggregate, manifest.aggregates)