~ubuntu-branches/ubuntu/utopic/lazygal/utopic

« back to all changes in this revision

Viewing changes to lazygal/pathutils.py

  • Committer: Package Import Robot
  • Author(s): Michal Čihař
  • Date: 2012-05-14 10:26:46 UTC
  • mfrom: (1.2.10)
  • Revision ID: package-import@ubuntu.com-20120514102646-a8aqsadph5u2s2sf
Tags: 0.7.2-1
* New upstream release.
  - Fix video thumbnailing handling (Closes: #662118).
  - Fixes French translation (Closes: #664167).
* Clean generated files in clean phase (Closes: #671317).
* Bump standards to 3.9.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
 
18
18
import os, sys
 
19
import posixpath
 
20
 
 
21
 
 
22
def is_root_posix(path):
 
23
    return path == '/'
 
24
 
 
25
 
 
26
def is_root_win32(path):
 
27
    return path[1:] == ':\\' # strip drive letter in comparison
 
28
 
 
29
 
 
30
if sys.platform == 'win32':
 
31
    is_root = is_root_win32
 
32
else:
 
33
    is_root = is_root_posix
19
34
 
20
35
 
21
36
def path2unicode(path):
30
45
    Returns whether path is a subdirectory of dir_path.
31
46
    '''
32
47
    test_path = path
33
 
    while test_path != dir_path and test_path != '/':
 
48
    while test_path != dir_path and not is_root(test_path):
34
49
        test_path, tail = os.path.split(test_path)
35
50
 
36
51
    if test_path == dir_path:
39
54
        return False
40
55
 
41
56
 
42
 
def relative_path(from_dir, to_path):
43
 
    '''
44
 
    Returns the relative path of to_path as if currently working in from_dir.
45
 
    '''
46
 
    if not os.path.isdir(to_path):
47
 
        to_path, fn = os.path.split(to_path)
48
 
    else:
49
 
        fn = None
50
 
 
51
 
    rel_path = ''
52
 
    common_path = from_dir
53
 
    while not is_subdir_of(common_path, to_path):
54
 
        common_path, tail = os.path.split(common_path)
55
 
        rel_path = os.path.join('..', rel_path)
56
 
 
57
 
    if is_subdir_of(common_path, to_path):
58
 
        rel_path = os.path.join(rel_path, to_path[len(common_path)+1:])
59
 
 
60
 
    if fn:
61
 
        rel_path = os.path.join(rel_path, fn)
62
 
 
63
 
    return rel_path
 
57
def url_path(physical_path, input_pathmodule=os.path):
 
58
    '''
 
59
    Convert a physical path to a path suitable for use in a URL link,
 
60
    i.e. using forward slashes. This can only be used for relative paths
 
61
    because while converting, the root (either '/' or 'C:\\') is irrelevant.
 
62
    '''
 
63
    if input_pathmodule == posixpath: return physical_path
 
64
 
 
65
    head = physical_path
 
66
    path_list = []
 
67
    while head != '' and not is_root_posix(head) and not is_root_win32(head):
 
68
        head, tail = input_pathmodule.split(head)
 
69
        path_list.append(tail)
 
70
 
 
71
    path_list.reverse()
 
72
    return posixpath.join(*path_list)
64
73
 
65
74
 
66
75
# vim: ts=4 sw=4 expandtab