Python os.path.relpath() Examples

The following are 30 code examples of os.path.relpath(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os.path , or try the search function .
Example #1
Source File: ingest_database.py    From ibeis with Apache License 2.0 6 votes vote down vote up
def get_name_texts_from_parent_folder(gpath_list, img_dir, fmtkey=None):
    """
    Input: gpath_list
    Output: names based on the parent folder of each image
    """
    #from os.path import commonprefix
    relgpath_list = [relpath(gpath, img_dir) for gpath in gpath_list]
    #_prefix = commonprefix(gpath_list)
    #relgpath_list = [relpath(gpath, _prefix) for gpath in gpath_list]
    _name_list  = [dirname(relgpath) for relgpath in relgpath_list]

    if fmtkey is not None:
        #fmtkey = 'African Wild Dog: {name}'
        import parse
        parse_results = [parse.parse(fmtkey, name) for name in _name_list]
        _name_list = [res['name'] if res is not None else name
                      for name, res in zip(_name_list, parse_results)]

    name_list = list(map(normalize_name, _name_list))
    return name_list 
Example #2
Source File: plot_directive.py    From Computable with MIT License 6 votes vote down vote up
def relpath(path, start=os.path.curdir):
            """Return a relative version of a path"""
            from os.path import sep, curdir, join, abspath, commonprefix, \
                 pardir

            if not path:
                raise ValueError("no path specified")

            start_list = abspath(start).split(sep)
            path_list = abspath(path).split(sep)

            # Work out how much of the filepath is shared by start and path.
            i = len(commonprefix([start_list, path_list]))

            rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
            if not rel_list:
                return curdir
            return join(*rel_list) 
Example #3
Source File: register.py    From kubeflow-and-mlops with Apache License 2.0 6 votes vote down vote up
def run(model_path, model_name, tenant_id, service_principal_id,
        service_principal_password, subscription_id, resource_group, workspace, tags):
    auth_args = {
        'tenant_id': tenant_id,
        'service_principal_id': service_principal_id,
        'service_principal_password': service_principal_password
    }

    ws_args = {
        'auth': ServicePrincipalAuthentication(**auth_args),
        'subscription_id': subscription_id,
        'resource_group': resource_group
    }

    ws = Workspace.get(workspace, **ws_args)

    print(ws.get_details())

    print('\nSaving model {} to {}'.format(model_path, model_name))

    # Model Path needs to be relative
    model_path = relpath(model_path, '.')

    model = Model.register(ws, model_name=model_name, model_path=model_path, tags=tags)
    print('Done!') 
Example #4
Source File: generate_dingbats_html.py    From nototools with Apache License 2.0 6 votes vote down vote up
def _generate_fontkey(fonts, targets, data_dir):
    lines = ['<p style="margin-bottom:5px"><b>Targets</b>']
    lines.append('<div style="margin-left:20px"><table class="key">')
    for tid, target in enumerate(targets):
        lines.append('<tr><th><a href="#target_%s">%s</a>' % (tid, target.name))
    lines.append("</table></div>")

    lines.append('<p style="margin-bottom:5px"><b>Fonts</b>')
    lines.append('<div style="margin-left:20px"><table class="key">')
    for key, keyinfos in fonts:
        for font, name, _ in keyinfos:
            rel_font = path.relpath(font, data_dir) if font else "(no font)"
            lines.append("<tr><th>%s<td>%s<td>%s" % (key, name, rel_font))
    lines.append("</table></div>")

    return "\n".join(lines) 
Example #5
Source File: plot_directive.py    From matplotlib-4-abaqus with MIT License 6 votes vote down vote up
def relpath(path, start=os.path.curdir):
            """Return a relative version of a path"""
            from os.path import sep, curdir, join, abspath, commonprefix, \
                 pardir

            if not path:
                raise ValueError("no path specified")

            start_list = abspath(start).split(sep)
            path_list = abspath(path).split(sep)

            # Work out how much of the filepath is shared by start and path.
            i = len(commonprefix([start_list, path_list]))

            rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
            if not rel_list:
                return curdir
            return join(*rel_list) 
Example #6
Source File: evidence_corpus.py    From XQA with MIT License 6 votes vote down vote up
def _gather_files(input_root, output_dir, skip_dirs, wiki_only):
    if not exists(output_dir):
        mkdir(output_dir)

    all_files = []
    for root, dirs, filenames in walk(input_root):
        if skip_dirs:
            output = join(output_dir, relpath(root, input_root))
            if exists(output):
                continue
        path = relpath(root, input_root)
        normalized_path = normalize_wiki_filename(path)
        if not exists(join(output_dir, normalized_path)):
            mkdir(join(output_dir, normalized_path))
        all_files += [join(path, x) for x in filenames]
    if wiki_only:
        all_files = [x for x in all_files if "wikipedia/" in x]
    return all_files 
Example #7
Source File: widgets.py    From vy with MIT License 6 votes vote down vote up
def  __call__(self, options=[], display=True):
        """
        Fill a LinePicker with a list of options. 
        
        When display=False it just fills the Line 
        Picker for later showing the options with LinePicker.display method.
        """
        # Make sure it is a list otherwise it may receive
        # an iterator and display no results even when there are
        # errors.
        options = list(options)
        ranges = zip(('%s - %s:%s' % (msg, relpath(filename), line)
        for filename, line, msg in options), options)

        ranges = list(ranges)
        super(LinePicker, self).__call__(ranges, display) 
Example #8
Source File: __init__.py    From pulsar with Apache License 2.0 6 votes vote down vote up
def __directory_contents(self, directory, recursive=True):
        contents = []
        if recursive:
            for path, _, files in walk(directory):
                relative_path = relpath(path, directory)
                for name in files:
                    # Return file1.txt, dataset_1_files/image.png, etc... don't
                    # include . in path.
                    if relative_path != curdir:
                        contents.append(join(relative_path, name))
                    else:
                        contents.append(name)
        else:
            if os.path.isdir(directory):
                for name in os.listdir(directory):
                    contents.append(name)
        return contents

    # Following abstractions store metadata related to jobs. 
Example #9
Source File: cmds_events_doc.py    From moler with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _walk_moler_python_files(path, *args):
    """
    Walk thru directory with commands and search for python source code (except __init__.py)
    Yield relative filepath to parameter path

    :param path: relative path do directory with commands
    :type path:
    :rtype: str
    """
    repo_path = abspath(join(path, '..', '..'))

    observer = "event" if "events" in split(path) else "command"
    print("Processing {}s test from path: '{}'".format(observer, path))

    for (dirpath, _, filenames) in walk(path):
        for filename in filenames:
            if filename.endswith('__init__.py'):
                continue
            if filename.endswith('.py'):
                abs_path = join(dirpath, filename)
                in_moler_path = relpath(abs_path, repo_path)
                yield in_moler_path 
Example #10
Source File: test_data.py    From psyplot with GNU General Public License v2.0 5 votes vote down vote up
def test_array_info(self):
        variables, coords = self._from_dataset_test_variables
        variables['v4'] = variables['v3'].copy()
        ds = xr.Dataset(variables, coords)
        fname = osp.relpath(bt.get_file('test-t2m-u-v.nc'), '.')
        ds2 = xr.open_dataset(fname)
        l = ds.psy.create_list(
            name=[['v1', ['v3', 'v4']], ['v1', 'v2']], prefer_list=True)
        l.extend(ds2.psy.create_list(name=['t2m'], x=0, t=1),
                 new_name=True)
        self.assertEqual(l.array_info(engine='netCDF4'), OrderedDict([
            # first list contating an array with two variables
            ('arr0', OrderedDict([
                ('arr0', {'dims': {'t': slice(None), 'x': slice(None)},
                          'attrs': OrderedDict(), 'store': (None, None),
                          'name': 'v1', 'fname': None}),
                ('arr1', {'dims': {'y': slice(None)},
                          'attrs': OrderedDict(), 'store': (None, None),
                          'name': [['v3', 'v4']], 'fname': None}),
                ('attrs', OrderedDict())])),
            # second list with two arrays containing each one variable
            ('arr1', OrderedDict([
                ('arr0', {'dims': {'t': slice(None), 'x': slice(None)},
                          'attrs': OrderedDict(), 'store': (None, None),
                          'name': 'v1', 'fname': None}),
                ('arr1', {'dims': {'y': slice(None), 'x': slice(None)},
                          'attrs': OrderedDict(), 'store': (None, None),
                          'name': 'v2', 'fname': None}),
                ('attrs', OrderedDict())])),
            # last array from real dataset
            ('arr2', {'dims': {'z': slice(None), 'y': slice(None),
                               't': 1, 'x': 0},
                      'attrs': ds2.t2m.attrs,
                      'store': ('xarray.backends.netCDF4_',
                                'NetCDF4DataStore'),
                      'name': 't2m', 'fname': fname}),
            ('attrs', OrderedDict())]))
        return l 
Example #11
Source File: exampleinclude.py    From airflow with Apache License 2.0 5 votes vote down vote up
def doctree_read(app, doctree):
    """
    Reads documentation tree for the application and register sources in the generated documentation.

    :param app: application
    :param doctree: documentation tree

    :return None

    """
    env = app.builder.env
    if not hasattr(env, "_viewcode_modules"):
        env._viewcode_modules = {}  # type: ignore

    if app.builder.name == "singlehtml":
        return

    for objnode in doctree.traverse(ExampleHeader):
        filepath = objnode.get("filename")
        relative_path = path.relpath(
            filepath, path.commonprefix([app.config.exampleinclude_sourceroot, filepath])
        )
        modname = relative_path.replace("/", ".")[:-3]
        show_button = register_source(app, env, modname)
        onlynode = create_node(env, relative_path, show_button)

        objnode.replace_self(onlynode)
# pylint: enable=protected-access 
Example #12
Source File: generate_dingbats_html.py    From nototools with Apache License 2.0 5 votes vote down vote up
def _generate_styles(fonts, relpath):
    face_pat = """@font-face {
      font-family: "%s"; src:url("%s")
    }"""

    facelines = []
    classlines = []
    for key, keyinfos in fonts:
        index = 0
        for font, _, _ in keyinfos:
            if len(keyinfos) > 1:
                kname = "%s_%d" % (replace_nonalpha(key), index)
            else:
                kname = replace_nonalpha(key)
            index += 1
            if not font:
                classlines.append(".%s { font-size: 12pt }" % kname)
            else:
                if relpath is None:
                    font = "file://" + font
                else:
                    font = path.join(relpath, path.basename(font))
                facelines.append(face_pat % (kname, font))
                classlines.append(
                    '.%s { font-family: "%s", "noto_0" }' % (kname, kname)
                )

    lines = []
    lines.extend(facelines)
    lines.append("")
    lines.extend(classlines)
    return "\n    ".join(lines) 
Example #13
Source File: setup.py    From klustakwik2 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _package_tree(pkgroot):
    path = op.dirname(__file__)
    subdirs = [op.relpath(i[0], path).replace(op.sep, '.')
               for i in os.walk(op.join(path, pkgroot))
               if '__init__.py' in i[2]]
    return subdirs 
Example #14
Source File: test_8_final_touches.py    From diy-lang with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def prepare_env():
    global env
    env = Environment()
    path = join(dirname(relpath(__file__)), '..', 'stdlib.diy')
    interpret_file(path, env) 
Example #15
Source File: resources.py    From fbs with GNU General Public License v3.0 5 votes vote down vote up
def _get_files_to_copy(src_dir_or_file, dest_dir, exclude):
    excludes = _paths(map(path, exclude))
    if isfile(src_dir_or_file) and src_dir_or_file not in excludes:
        yield src_dir_or_file, join(dest_dir, basename(src_dir_or_file))
    else:
        for (subdir, _, files) in os.walk(src_dir_or_file):
            dest_subdir = join(dest_dir, relpath(subdir, src_dir_or_file))
            for file_ in files:
                file_path = join(subdir, file_)
                dest_path = join(dest_subdir, file_)
                if file_path not in excludes:
                    yield file_path, dest_path 
Example #16
Source File: generate_dingbats_html.py    From nototools with Apache License 2.0 5 votes vote down vote up
def generate_text(outfile, title, fonts, targets, flag_sets, metrics, data_dir):
    outfile.write(title + "\n")
    outfile.write("\n")
    outfile.write("Fonts:\n")
    max_keylen = max(len(key) for key, _ in fonts)
    fmt = "  %%%ds: %%s (%%s)" % max_keylen
    for key, keyinfos in fonts:
        for font, name, _ in keyinfos:
            rel_font = path.relpath(font, data_dir) if font else "(no font)"
            outfile.write(fmt % (key, name, rel_font) + "\n")
    outfile.write("\n")

    for target in targets:
        outfile.write("\n")
        outfile.write(target.generate_text(flag_sets, metrics) + "\n") 
Example #17
Source File: archive.py    From appstore with GNU Affero General Public License v3.0 5 votes vote down vote up
def build_files(args: Dict[str, str]) -> Dict[str, str]:
    platform = int(args['platform'])  # prevent path traversal
    vars = {
        'id': args['name'].lower(),
        'summary': args['summary'],
        'description': args['description'],
        'name': ' '.join(re.findall(r'[A-Z][^A-Z]*', args['name'])),
        'namespace': args['name'],
        'author_name': args['author_name'],
        'author_mail': args['author_email'],
        'author_homepage': args['author_homepage'],
        'issue_tracker': args['issue_tracker'],
        'categories': args['categories'],
        'nextcloud_version': platform
    }
    vars.update(settings.APP_SCAFFOLDING_PROFILES.get(platform, {}))
    relative_base = 'app-templates/%i/app/' % platform
    base = resolve_file_relative_path(__file__, relative_base)

    context = Context({'app': vars})
    result = {}
    if isdir(base):
        for root, dirs, files in walk(base):
            for file in files:
                file_path = join(root, file)
                rel_file_path = '%s/%s' % (
                    vars['id'], relpath(file_path, base)
                )
                with open(file_path) as f:
                    t = Template(f.read())
                    result[rel_file_path] = t.render(context)

    return result 
Example #18
Source File: load.py    From GraphDash with Apache License 2.0 5 votes vote down vote up
def load_data_raw(data_dir):
    """Loading data, directly looking for any graph."""
    data, nb_graphs = Tree(factory=default_family_data), 0

    if not op.isdir(data_dir):
        print('(!) {0} is not a directory'.format(data_dir))
        return data

    extensions = '.png .jpg .jpeg .bmp .eps .ps .svg .gif'.split()
    group_size = 10

    for filepath in iter_all_files(data_dir, extensions):
        # Number rounded to the closest 10/20/30/...
        c = int(nb_graphs / group_size) * group_size
        family_tuple = ("Graphs {0:6d}-{1:6d}".format(c + 1, c + group_size),)

        def rel(f):
            """Adjusting graph path to relative root from data_dir"""
            return op.relpath(op.join(op.dirname(filepath), f), data_dir)

        graph_data = default_graph_data()
        graph_data.update({
            'name' : rel(filepath),
            'title': op.basename(filepath),
        })

        node = data.create_from_path(family_tuple)
        node.data['graphs'].append(graph_data)
        nb_graphs += 1

    print('( ) {0} graphs loaded from {1}'.format(nb_graphs, data_dir))
    return data 
Example #19
Source File: conf.py    From txacme with MIT License 5 votes vote down vote up
def linkcode_resolve(domain, info):
    """
    Determine the URL corresponding to Python object
    """
    if domain != 'py':
        return None
    modname = info['module']
    fullname = info['fullname']
    submod = sys.modules.get(modname)
    if submod is None:
        return None
    obj = submod
    for part in fullname.split('.'):
        try:
            obj = getattr(obj, part)
        except:
            return None
    try:
        fn = inspect.getsourcefile(obj)
    except:
        fn = None
    if not fn:
        return None
    try:
        source, lineno = inspect.findsource(obj)
    except:
        lineno = None
    if lineno:
        linespec = "#L%d" % (lineno + 1)
    else:
        linespec = ""
    fn = relpath(fn, start='..')
    return "https://github.com/mithrandi/txacme/blob/%s/%s%s" % (
        txacme_version_info['full-revisionid'], fn, linespec) 
Example #20
Source File: export_hsdb.py    From ibeis with Apache License 2.0 5 votes vote down vote up
def get_hsdb_image_gpaths(ibs, gid_list):
    r"""
    Args:
        ibs (IBEISController):  ibeis controller object
        gid_list (list):

    Returns:
        list: gpath_list

    CommandLine:
        python -m ibeis.dbio.export_hsdb --test-get_hsdb_image_gpaths

    Example:
        >>> # ENABLE_DOCTEST
        >>> from ibeis.dbio.export_hsdb import *  # NOQA
        >>> import ibeis
        >>> # build test data
        >>> ibs = ibeis.opendb('testdb1')
        >>> gid_list = ibs.get_valid_gids()[0:2]
        >>> # execute function
        >>> gpath_list = get_hsdb_image_gpaths(ibs, gid_list)
        >>> # verify results
        >>> result = ut.repr2(gpath_list, nl=1)
        >>> print(result)
        [
            '../_ibsdb/images/66ec193a-1619-b3b6-216d-1784b4833b61.jpg',
            '../_ibsdb/images/d8903434-942f-e0f5-d6c2-0dcbe3137bf7.jpg',
        ]
    """
    imgdir = join(ibs.get_dbdir(), 'images')
    gpath_list_ = ibs.get_image_paths(gid_list)
    gpath_list  = [ut.ensure_unixslash(relpath(gpath, imgdir))
                   for gpath in gpath_list_]
    return gpath_list 
Example #21
Source File: setup.py    From fbs with GNU General Public License v3.0 5 votes vote down vote up
def _get_package_data(pkg_dir, data_subdir):
    result = []
    for dirpath, _, filenames in os.walk(join(pkg_dir, data_subdir)):
        for filename in filenames:
            filepath = join(dirpath, filename)
            result.append(relpath(filepath, pkg_dir))
    return result 
Example #22
Source File: imdb.py    From mxnet-yolo with MIT License 5 votes vote down vote up
def save_imglist(self, fname=None, root=None, shuffle=False):
        """
        save imglist to disk

        Parameters:
        ----------
        fname : str
            saved filename
        """
        str_list = []
        for index in range(self.num_images):
            label = self.label_from_index(index)
            path = self.image_path_from_index(index)
            if root:
                path = osp.relpath(path, root)
            str_list.append('\t'.join([str(index), str(2), str(label.shape[1])] \
              + ["{0:.4f}".format(x) for x in label.ravel()] + [path,]) + '\n')
        if str_list:
            if shuffle:
                import random
                random.shuffle(str_list)
            if not fname:
                fname = self.name + '.lst'
            with open(fname, 'w') as f:
                for line in str_list:
                    f.write(line)
        else:
            raise RuntimeError("No image in imdb") 
Example #23
Source File: _aws.py    From fbs with GNU General Public License v3.0 5 votes vote down vote up
def upload_folder_contents(dir_path, dest_path, bucket, key, secret):
    result = []
    for file_path in _iter_files_recursive(dir_path):
        file_relpath = relpath(file_path, dir_path)
        # Replace backslashes on Windows by forward slashes:
        file_relpath = file_relpath.replace(os.sep, '/')
        file_dest = dest_path + '/' + file_relpath
        upload_file(file_path, file_dest, bucket, key, secret)
        result.append(file_dest)
    return result 
Example #24
Source File: deadcode.py    From vy with MIT License 5 votes vote down vote up
def check_module(self, event=None):
        path  = get_project_root(self.area.filename)
        child = Popen([self.path,  path],
        stdout=PIPE, stderr=STDOUT, encoding=self.area.charset)
        output = child.communicate()[0]

        regex  = '(%s):([0-9]+):?[0-9]*:(.+)' % relpath(self.area.filename)
        ranges = findall(regex, output)

        sys.stdout.write('%s errors:\n%s\n' % (self.area.filename, output))
        self.area.chmode('NORMAL')

        root.status.set_msg('Vulture errors: %s' % len(ranges))
        if ranges:
            self.options(ranges) 
Example #25
Source File: snakerr.py    From vy with MIT License 5 votes vote down vote up
def check_module(self, event=None):
        path  = get_project_root(self.area.filename)
        child = Popen([self.path,  path],
        stdout=PIPE, stderr=STDOUT, encoding=self.area.charset)
        output = child.communicate()[0]

        regex  = '(%s):([0-9]+):?[0-9]*:(.+)' % relpath(self.area.filename)
        ranges = findall(regex, output)
        sys.stdout.write('Errors:\n%s\n' % output)
        self.area.chmode('NORMAL')
        root.status.set_msg('Pyflakes errors: %s' % len(ranges))

        if ranges:
            self.options(ranges) 
Example #26
Source File: mypy.py    From vy with MIT License 5 votes vote down vote up
def check_module(self, event=None):
        path  = get_project_root(self.area.filename)
        child = Popen([self.path,  path],
        stdout=PIPE, stderr=STDOUT, encoding=self.area.charset)
        output = child.communicate()[0]

        regex  = '(%s):([0-9]+):(.+)' % relpath(self.area.filename)
        ranges = findall(regex, output)
        sys.stdout.write('Mypy errors: \n%s\n' % output)
        self.area.chmode('NORMAL')

        root.status.set_msg('Mypy errors: %s' % len(ranges))
        if ranges:
            self.options(ranges) 
Example #27
Source File: plot_directive.py    From matplotlib-4-abaqus with MIT License 5 votes vote down vote up
def relpath(path, start=os.path.curdir):
            """Return a relative version of a path"""
            from os.path import sep, curdir, join, abspath, commonprefix, \
                 pardir, splitunc

            if not path:
                raise ValueError("no path specified")
            start_list = abspath(start).split(sep)
            path_list = abspath(path).split(sep)
            if start_list[0].lower() != path_list[0].lower():
                unc_path, rest = splitunc(path)
                unc_start, rest = splitunc(start)
                if bool(unc_path) ^ bool(unc_start):
                    raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
                                                                        % (path, start))
                else:
                    raise ValueError("path is on drive %s, start on drive %s"
                                                        % (path_list[0], start_list[0]))
            # Work out how much of the filepath is shared by start and path.
            for i in range(min(len(start_list), len(path_list))):
                if start_list[i].lower() != path_list[i].lower():
                    break
            else:
                i += 1

            rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
            if not rel_list:
                return curdir
            return join(*rel_list) 
Example #28
Source File: test_filesystems.py    From ibis with Apache License 2.0 5 votes vote down vote up
def _get_all_files(path):
    paths = {}
    for dirpath, _, filenames in os.walk(path):
        rel_dir = osp.relpath(dirpath, path)
        if rel_dir == '.':
            rel_dir = ''
        for name in filenames:
            abspath = osp.join(dirpath, name)
            relpath = osp.join(rel_dir, name)
            paths[relpath] = abspath

    return paths 
Example #29
Source File: test_filesystems.py    From ibis with Apache License 2.0 5 votes vote down vote up
def _check_directories_equal(left, right):
    left_files = _get_all_files(left)
    right_files = _get_all_files(right)

    assert set(left_files.keys()) == set(right_files.keys())

    for relpath, labspath in left_files.items():
        rabspath = right_files[relpath]
        assert _contents_equal(rabspath, labspath) 
Example #30
Source File: setup.py    From bhmm with GNU Lesser General Public License v3.0 5 votes vote down vote up
def find_package_data(data_root, package_root):
    files = []
    for root, dirnames, filenames in os.walk(data_root):
        for fn in filenames:
            files.append(relpath(join(root, fn), package_root))
    return files


################################################################################
# SETUP
################################################################################