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 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 bundle(dir, file): """ Create a Research Object Bundle """ manifestfilepath = manifest_file(dir) if not directory_and_manifest_exist(dir): print("Could not find manifest file: {}".format(manifestfilepath)) return 1 manifest = Manifest(filename=manifestfilepath) bundle = Bundle.create_from_manifest(file, manifest) bundle.close()
def annotate(dir, file_uri_or_pattern, attributename=None, attributevalue=None, annotation_file_or_uri=None, regexp=False): """ Annotate a specified research object component ro annotate file attribute-name [ attribute-value ] ro annotation file -f file_or_uri """ manifestfilepath = manifest_file(dir) if not directory_and_manifest_exist(dir): print("Could not find manifest file: {}".format(manifestfilepath)) return 1 manifest = Manifest(filename=manifestfilepath) files = [] if regexp: try: pattern = re.compile(file_uri_or_pattern) except re.error as e: #print('''%(rocmd)s remove -w "%(rofile)s" <...> : %(err)s''' % ro_options) return 1 files = [ aggregate for aggregate in manifest.aggregates if pattern.search(str(aggregate.uri)) ] else: files = [file_uri_or_pattern] if annotation_file_or_uri: #we are adding an annotation about file_uri_or_pattern using annotation_file_or_uri as the contents of the annotation for file in files: if os.path.isfile(annotation_file_or_uri): if not annotation_file_or_uri.startswith(".ro/annotations/"): annotation_file_or_uri = sanitize_filename_for_identifier(annotation_file_or_uri, dir) manifest.add_annotation(about=file, contents=annotation_file_or_uri) with open(manifestfilepath, 'w') as manifest_filehandle: manifest_filehandle.write(manifest.to_json()) return 0
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_add_existing_aggregate(self): manifest = Manifest() manifest.add_aggregate("/test", createdBy="Alice W.Land", createdOn="2013-03-05T17:29:03Z", mediatype="text/plain") manifest.add_aggregate("/test", createdBy="Deckard", createdOn="2013-03-05T17:29:03Z", mediatype="text/plain") contains = Aggregate("/test") in manifest.aggregates self.assertTrue(contains) a = manifest.get_aggregate("/test") self.assertEquals(a.createdBy.name, "Deckard")
def init(name, dir, id=None, creator=None, verbose=False, force=False): """ Create a new Research Object. ro init name [-f] [ -d dir ] [ -i id ] """ id = id or sanitize_name_for_identifier(name) timestamp = datetime.datetime.now().replace(microsecond=0) if verbose: print("ro create \{}\" -d \"{}\" -i \"{}\"".format(name,dir,id)) manifestdir = manifest_directory(dir) log.debug("manifestdir: " + manifestdir) manifestfilepath = manifest_file(dir) log.debug("manifestfilepath: " + manifestfilepath) try: os.makedirs(manifestdir) except OSError: if os.path.isdir(manifestdir): # Someone else created it... if force: pass else: print(".ro folder already exists, use --force to init an existing ro.") return 1 else: # There was an error on creation, so make sure we know about it raise #Create the manifest manifest = Manifest(id=id) manifest.createdBy = creator manifest.createdOn = timestamp manifest.title = name manifest.description = name log.debug("manifest: " + manifest.to_json()) with open(manifestfilepath, 'w') as manifest_filehandle: manifest_filehandle.write(manifest.to_json()) return 0
def status(dir, verbose=False): """ Display status of a designated research object ro status [ -d dir ] """ manifestfilepath = manifest_file(dir) if not directory_and_manifest_exist(dir): print("Could not find manifest file: {}".format(manifestfilepath)) return 1 manifest = Manifest(filename=manifestfilepath) print("Research Object status") print(" Identifier: {}".format(manifest.id)) print(" Title: {}".format(manifest.title)) print(" Creator:") if manifest.createdBy.name: print(" Name: {}".format(manifest.createdBy.name)) if manifest.createdBy.orcid: print(" Orcid: {}".format(manifest.createdBy.orcid)) if manifest.createdBy.uri: print(" URI: {}".format(manifest.createdBy.uri)) print(" Created: {}".format(manifest.createdOn)) print(" Aggregates:") aggregates = [aggregate.uri for aggregate in manifest.aggregates] for aggregate in aggregates: print(" {}".format(aggregate)) #Establish the Unaggregated files files = [] for dirpath, dirnames, filenames in os.walk(dir): #make the path relative to the top dir of the ro dirpath = os.path.relpath(dirpath, dir) files += [sanitize_filename_for_identifier(os.path.join(dirpath, filename), dir) for filename in filenames] for aggregate in aggregates: if aggregate in files: files.remove(aggregate) print("") print("Unaggregated files:") for file in files: print(file) print("") return 0
def list(dir): """ List contents of a designated research object -a displays files present in directory as well as aggregated resources -h includes hidden files in display ro list [ -d dir ] ro ls [ -d dir ] """ manifestfilepath = manifest_file(dir) if not directory_and_manifest_exist(dir): print("Could not find manifest file: {}".format(manifestfilepath)) return 1 manifest = Manifest(filename=manifestfilepath) print("{} aggregates:".format(manifest.id)) aggregates = [aggregate.uri for aggregate in manifest.aggregates] for aggregate in aggregates: print(" {}".format(aggregate)) return 0
def annotations(dir, file=None): """ Display annotations ro annotations [ file | -d dir ] """ manifestfilepath = manifest_file(dir) if not directory_and_manifest_exist(dir): print("Could not find manifest file: {}".format(manifestfilepath)) return 1 manifest = Manifest(filename=manifestfilepath) print("Annotations:") for annotation in manifest.annotations: if file and annotation.about and annotation.about != file: continue print("---") print("id: {}".format(annotation.uri)) if annotation.about: print("about: {}".format(annotation.about)) if annotation.contents: print("contents: {}".format(annotation.contents)) return 0
def remove(dir, file_uri_or_pattern, verbose=False, regexp=False): """ Remove a specified research object component or components remove [ -d <dir> ] <file-or-uri> remove -d <dir> -w <pattern> """ #Get the manifest file for this ro manifest_file_path = manifest_file(dir) manifest = Manifest(filename=manifest_file_path) if regexp: try: pattern = re.compile(file_uri_or_pattern) except re.error as e: #print('''%(rocmd)s remove -w "%(rofile)s" <...> : %(err)s''' % ro_options) return 1 for aggregate in [ aggregate for aggregate in manifest.aggregates if pattern.search(str(aggregate.uri)) ]: manifest.remove_aggregate(aggregate) for annotation in [ annotation for annotation in manifest.annotations if pattern.search(str(annotation.uri)) ]: manifest.remove_annotation(annotation) else: aggregate = manifest.get_aggregate(file_uri_or_pattern) if aggregate: manifest.remove_aggregate(aggregate) annotation = manifest.get_annotation(file_uri_or_pattern) if annotation: manifest.remove_annotation(annotation) with open(manifest_file_path, 'w') as manifest_filehandle: manifest_filehandle.write(manifest.to_json()) return 0
def test_manifest_creation(self): m = Manifest() self.assertEquals(m.id, "/") pass
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)