Skip to content

robot.utils.robotpath

normpath

normpath(path, case_normalize=False)

Replacement for os.path.normpath with some enhancements.

  1. Convert non-Unicode paths to Unicode using the file system encoding.
  2. NFC normalize Unicode paths (affects mainly OSX).
  3. Optionally lower-case paths on case-insensitive file systems. That includes Windows and also OSX in default configuration.
  4. Turn c: into c:\ on Windows instead of keeping it as c:.
Source code in src/robot/utils/robotpath.py
def normpath(path, case_normalize=False):
    """Replacement for os.path.normpath with some enhancements.

    1. Convert non-Unicode paths to Unicode using the file system encoding.
    2. NFC normalize Unicode paths (affects mainly OSX).
    3. Optionally lower-case paths on case-insensitive file systems.
       That includes Windows and also OSX in default configuration.
    4. Turn ``c:`` into ``c:\\`` on Windows instead of keeping it as ``c:``.
    """
    # FIXME: Support pathlib.Path
    if not is_string(path):
        path = system_decode(path)
    path = safe_str(path)  # Handles NFC normalization on OSX
    path = os.path.normpath(path)
    if case_normalize and CASE_INSENSITIVE_FILESYSTEM:
        path = path.lower()
    if WINDOWS and len(path) == 2 and path[1] == ':':
        return path + '\\'
    return path

abspath

abspath(path, case_normalize=False)

Replacement for os.path.abspath with some enhancements and bug fixes.

  1. Non-Unicode paths are converted to Unicode using file system encoding.
  2. Optionally lower-case paths on case-insensitive file systems. That includes Windows and also OSX in default configuration.
  3. Turn c: into c:\ on Windows instead of c:\current\path.
Source code in src/robot/utils/robotpath.py
def abspath(path, case_normalize=False):
    """Replacement for os.path.abspath with some enhancements and bug fixes.

    1. Non-Unicode paths are converted to Unicode using file system encoding.
    2. Optionally lower-case paths on case-insensitive file systems.
       That includes Windows and also OSX in default configuration.
    3. Turn ``c:`` into ``c:\\`` on Windows instead of ``c:\\current\\path``.
    """
    path = normpath(path, case_normalize)
    return normpath(os.path.abspath(path), case_normalize)
get_link_path(target, base)

Returns a relative path to target from base.

If base is an existing file, then its parent directory is considered to be the base. Otherwise base is assumed to be a directory.

The returned path is URL encoded. On Windows returns an absolute path with file: prefix if the target is on a different drive.

Source code in src/robot/utils/robotpath.py
def get_link_path(target, base):
    """Returns a relative path to ``target`` from ``base``.

    If ``base`` is an existing file, then its parent directory is considered to
    be the base. Otherwise ``base`` is assumed to be a directory.

    The returned path is URL encoded. On Windows returns an absolute path with
    ``file:`` prefix if the target is on a different drive.
    """
    path = _get_link_path(target, base)
    url = path_to_url(path)
    if os.path.isabs(path):
        url = 'file:' + url
    return url