##// END OF EJS Templates
Merge pull request #5390 from minrk/finish-5333...
Brian E. Granger -
r15906:7b9b9b23 merge
parent child Browse files
Show More
@@ -1,125 +1,137 b''
1 1 """Notebook related utilities
2 2
3 3 Authors:
4 4
5 5 * Brian Granger
6 6 """
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Copyright (C) 2011 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 from __future__ import print_function
16 16
17 17 import os
18 18 import stat
19 19
20 20 try:
21 21 from urllib.parse import quote, unquote
22 22 except ImportError:
23 23 from urllib import quote, unquote
24 24
25 25 from IPython.utils import py3compat
26 26
27 27 # UF_HIDDEN is a stat flag not defined in the stat module.
28 28 # It is used by BSD to indicate hidden files.
29 29 UF_HIDDEN = getattr(stat, 'UF_HIDDEN', 32768)
30 30
31 31 #-----------------------------------------------------------------------------
32 32 # Imports
33 33 #-----------------------------------------------------------------------------
34 34
35 35 def url_path_join(*pieces):
36 36 """Join components of url into a relative url
37 37
38 38 Use to prevent double slash when joining subpath. This will leave the
39 39 initial and final / in place
40 40 """
41 41 initial = pieces[0].startswith('/')
42 42 final = pieces[-1].endswith('/')
43 43 stripped = [s.strip('/') for s in pieces]
44 44 result = '/'.join(s for s in stripped if s)
45 45 if initial: result = '/' + result
46 46 if final: result = result + '/'
47 47 if result == '//': result = '/'
48 48 return result
49 49
50 50 def path2url(path):
51 51 """Convert a local file path to a URL"""
52 52 pieces = [ quote(p) for p in path.split(os.sep) ]
53 53 # preserve trailing /
54 54 if pieces[-1] == '':
55 55 pieces[-1] = '/'
56 56 url = url_path_join(*pieces)
57 57 return url
58 58
59 59 def url2path(url):
60 60 """Convert a URL to a local file path"""
61 61 pieces = [ unquote(p) for p in url.split('/') ]
62 62 path = os.path.join(*pieces)
63 63 return path
64 64
65 65 def url_escape(path):
66 66 """Escape special characters in a URL path
67 67
68 68 Turns '/foo bar/' into '/foo%20bar/'
69 69 """
70 70 parts = py3compat.unicode_to_str(path).split('/')
71 71 return u'/'.join([quote(p) for p in parts])
72 72
73 73 def url_unescape(path):
74 74 """Unescape special characters in a URL path
75 75
76 76 Turns '/foo%20bar/' into '/foo bar/'
77 77 """
78 78 return u'/'.join([
79 79 py3compat.str_to_unicode(unquote(p))
80 80 for p in py3compat.unicode_to_str(path).split('/')
81 81 ])
82 82
83 83 def is_hidden(abs_path, abs_root=''):
84 84 """Is a file hidden or contained in a hidden directory?
85 85
86 86 This will start with the rightmost path element and work backwards to the
87 87 given root to see if a path is hidden or in a hidden directory. Hidden is
88 88 determined by either name starting with '.' or the UF_HIDDEN flag as
89 89 reported by stat.
90 90
91 91 Parameters
92 92 ----------
93 93 abs_path : unicode
94 94 The absolute path to check for hidden directories.
95 95 abs_root : unicode
96 96 The absolute path of the root directory in which hidden directories
97 97 should be checked for.
98 98 """
99 99 if not abs_root:
100 100 abs_root = abs_path.split(os.sep, 1)[0] + os.sep
101 101 inside_root = abs_path[len(abs_root):]
102 102 if any(part.startswith('.') for part in inside_root.split(os.sep)):
103 103 return True
104 104
105 # check that dirs can be listed
106 # may fail on Windows junctions or non-user-readable dirs
107 if os.path.isdir(abs_path):
108 try:
109 os.listdir(abs_path)
110 except OSError:
111 return True
112
105 113 # check UF_HIDDEN on any location up to root
106 114 path = abs_path
107 115 while path and path.startswith(abs_root) and path != abs_root:
116 try:
117 # may fail on Windows junctions
108 118 st = os.stat(path)
119 except OSError:
120 return True
109 121 if getattr(st, 'st_flags', 0) & UF_HIDDEN:
110 122 return True
111 123 path = os.path.dirname(path)
112 124
113 125 return False
114 126
115 127 def to_os_path(path, root=''):
116 128 """Convert an API path to a filesystem path
117 129
118 130 If given, root will be prepended to the path.
119 131 root must be a filesystem path already.
120 132 """
121 133 parts = path.strip('/').split('/')
122 134 parts = [p for p in parts if p != ''] # remove duplicate splits
123 135 path = os.path.join(root, *parts)
124 136 return path
125 137
General Comments 0
You need to be logged in to leave comments. Login now