File: file_path.py

package info (click to toggle)
python-apptools 4.5.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,668 kB
  • sloc: python: 16,754; makefile: 79
file content (85 lines) | stat: -rw-r--r-- 2,677 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Simple class to support file path objects that work well in the
context of persistent storage with the state_pickler.

"""
# Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
# Copyright (c) 2005, Enthought, Inc.
# License: BSD Style.

# Standard library imports.
import os
from os.path import abspath, normpath, dirname, commonprefix, join


class FilePath(object):
    """This class stores two paths to the file.  A relative path and
    an absolute one.  The absolute path is used by the end user.  When
    this object is pickled the state_pickler sets the relative path
    relative to the file that is being generated.  When unpickled, the
    stored relative path is used to set the absolute path correctly
    based on the path of the saved file.
    """
    def __init__(self, value=''):
        self.set(value)

    def __str__(self):
        return self.abs_pth

    def __repr__(self):
        return self.abs_pth.__repr__()

    def get(self):
        """Get the path.
        """
        return self.abs_pth

    def set(self, value):
        """Sets the value of the path.
        """
        self.rel_pth = value
        if value:
            self.abs_pth = normpath(abspath(value))
        else:
            self.abs_pth = ''

    def set_relative(self, base_f_name):
        """Sets the path relative to `base_f_name`.  Note that
        `base_f_name` and self.rel_pth should be valid file names
        correct on the current os.  The set name is a file name that
        has a POSIX path.
        """

        # Get normalized paths.
        _src = abspath(base_f_name)
        _dst = self.abs_pth

        # Now strip out any common prefix between the two paths.
        for part in _src.split(os.sep):
            if _dst.startswith(part+os.sep):
                length = len(part) + 1
                _src = _src[length:]
                _dst = _dst[length:]
            else:
                break

        # For each directory in the source, we need to add a reference to
        # the parent directory to the destination.
        ret = (_src.count(os.sep) * ('..' + os.sep)) + _dst

        # Make it posix style.
        if os.sep != '/':
            ret.replace(os.sep, '/')

        # Store it.
        self.rel_pth = ret

    def set_absolute(self, base_f_name):
        """Sets the absolute file name for the current relative file
        name with respect to the given `base_f_name`.
        """
        base_f_name = normpath(abspath(base_f_name))
        rel_file_name = normpath(self.rel_pth)
        file_name = join(dirname(base_f_name), rel_file_name)
        file_name = os.path.normpath(file_name)
        self.abs_pth = file_name