Given a path and options for a format (ext, suffix, prefix), return the corresponding base path
8629
def base_path(main_path, fmt):
    """"""
    if not fmt:
        return os.path.splitext(main_path)[0]

    fmt = long_form_one_format(fmt)
    fmt_ext = fmt['extension']
    suffix = fmt.get('suffix')
    prefix = fmt.get('prefix')

    base, ext = os.path.splitext(main_path)
    if ext != fmt_ext:
        raise InconsistentPath(u"Notebook path '{}' was expected to have extension '{}'".format(main_path, fmt_ext))

    if suffix:
        if not base.endswith(suffix):
            raise InconsistentPath(u"Notebook name '{}' was expected to end with suffix '{}'".format(base, suffix))
        base = base[:-len(suffix)]

    if not prefix:
        return base

    prefix_dir, prefix_file_name = os.path.split(prefix)
    notebook_dir, notebook_file_name = os.path.split(base)
    sep = base[len(notebook_dir):-len(notebook_file_name)]

    if prefix_file_name:
        if not notebook_file_name.startswith(prefix_file_name):
            raise InconsistentPath(u"Notebook name '{}' was expected to start with prefix '{}'"
                                   .format(notebook_file_name, prefix_file_name))
        notebook_file_name = notebook_file_name[len(prefix_file_name):]

    if prefix_dir:
        if not notebook_dir.endswith(prefix_dir):
            raise InconsistentPath(u"Notebook directory '{}' was expected to end with directory prefix '{}'"
                                   .format(notebook_dir, prefix_dir))
        notebook_dir = notebook_dir[:-len(prefix_dir)]

    if not notebook_dir:
        return notebook_file_name

    # Does notebook_dir ends with a path separator?
    if notebook_dir[-1:] == sep:
        return notebook_dir + notebook_file_name

    return notebook_dir + sep + notebook_file_name
399733
function (path, ext) {
            var result = path.match(splitPathRe) || [],
                basename;
            basename = result[3] || '';
            if (ext && basename && basename.slice(-1 * ext.length) == ext) {
                basename = basename.slice(0, -1 * ext.length);
            }
            return basename;
        }
1298270
def FindIxes(self, paths, prefix, suffix):
        

        suffix = self.subst('$'+suffix)
        prefix = self.subst('$'+prefix)

        for path in paths:
            dir,name = os.path.split(str(path))
            if name[:len(prefix)] == prefix and name[-len(suffix):] == suffix:
                return path
265455
def basename(self, suffix=''):
		''' '''
		return os.path.basename(self._file, suffix) if self._file else None
392060
def get_filebase(path, pattern):
    """"""
    # A pattern can include directories
    tail_len = len(pattern.split(os.path.sep))
    return os.path.join(*str(path).split(os.path.sep)[-tail_len:])
466135
def flattened_params_url(self, path_prefix, *paths, **kparams):
        

        base = self.base_url + path_prefix
        for p in paths:
            base += "/"
            base += p
        if kparams:
            base += "?"
            # use %r in val?
            base += '&'.join("%s=%s" % (key,val) for (key,val) in kparams.items())
        return base
1084194
def _get_relative_ext(of, sf):
    
    def half_finished_trim(orig, prefix):
        return (os.path.basename(prefix).count(".") > 0 and
                os.path.basename(orig).count(".") == os.path.basename(prefix).count("."))
    # Handle remote files
    if of.find(":") > 0:
        of = os.path.basename(of.split(":")[-1])
    if sf.find(":") > 0:
        sf = os.path.basename(sf.split(":")[-1])
    prefix = os.path.commonprefix([sf, of])
    while prefix.endswith(".") or (half_finished_trim(sf, prefix) and half_finished_trim(of, prefix)):
        prefix = prefix[:-1]
    exts_to_remove = of.replace(prefix, "")
    ext_to_add = sf.replace(prefix, "")
    # Return extensions relative to original
    if not exts_to_remove or exts_to_remove.startswith("."):
        return str("^" * exts_to_remove.count(".") + ext_to_add)
    else:
        raise ValueError("No cross platform way to reference complex extension: %s %s" % (sf, of))
475661
def join_path(base, *parts: str):
    """"""
    _parts = "/".join((_part.strip("/") for _part in parts))
    # _parts = '/'.join(parts)
    if base.endswith("/"):
        url = base + _parts
    else:
        url = base + "/" + _parts
    return url
354629
def find_base(path):
	""""""
	result = _pattern.match(path)

	if result:
		base = result.group(0)
	else:
		base = "./"

	if base.endswith('/') or base.endswith('\\'):
		return os.path.abspath(base)
	else:
		return os.path.dirname(os.path.abspath(base))
529468
def find_best_match(path, prefixes):
    """"""
    path_parts = path.split('.')
    for p in prefixes:
        if len(p) <= len(path_parts) and p == path_parts[:len(p)]:
            return '.'.join(p), '.'.join(path_parts[len(p):])
    return '', path
47212
def extname(path, **kwargs):
    """"""
    import os.path
    name, ext = os.path.splitext(path, **kwargs)
    return ext
126851
def proper_path(path):
    
    if path.startswith("./"):
        pass
    elif path.startswith("/"):
        path = ".%s" % path
    elif path.startswith("."):
        while path.startswith("."):
            path = path[1:]
        if path.startswith("/"):
            path = ".%s" % path
    else:
        path = "./%s" % path

    if not path.endswith("/"):
        path += "/"

    return path
600825
def get_file_suffix(self, path, prefix):
        
        parent = self._ensure_folder_path(path)

        file_list = self.drive.ListFile({
            'q': "'{}' in parents and trashed=false and title contains '{}'".format(
                parent['id'], prefix)}).GetList()
        try:
            number_of_files = len(file_list)
        except:
            number_of_files = 0
        return '{0:04}'.format(number_of_files)
1250582
def path(self):
        ""
        prefix = ()
        if self.parent:
            prefix = self.parent.path

        if self.id_or_name:
            return prefix + (self.kind, self.id_or_name)

        return prefix + (self.kind,)
537256
def norm_join(prefix, suffix):
    
    if (prefix is None) and (suffix is None):
        return "."
    if prefix is None:
        return os.path.normpath(suffix)
    if suffix is None:
        return os.path.normpath(prefix)
    return os.path.normpath(os.path.join(prefix, suffix))
63434
def fast_relpath_optional(path, start):
  
  if len(start) == 0:
    # Empty prefix.
    return path

  # Determine where the matchable prefix ends.
  pref_end = len(start) - 1 if start[-1] == '/' else len(start)
  if pref_end > len(path):
    # The prefix is too long to match.
    return None
  elif path[:pref_end] == start[:pref_end] and (len(path) == pref_end or path[pref_end] == '/'):
    # The prefix matches, and the entries are either identical, or the suffix indicates that
    # the prefix is a directory.
    return path[pref_end+1:]
1165532
def match_path_extname(path, extensions)
      basename = File.basename(path)

      i = basename.index('.'.freeze)
      while i && i < basename.length - 1
        extname = basename[i..-1]
        if value = extensions[extname]
          return extname, value
        end

        i = basename.index('.'.freeze, i+1)
      end

      nil
    end