Пример #1
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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