This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 634
Expand file tree
/
Copy pathpath.py
More file actions
86 lines (68 loc) · 2.35 KB
/
path.py
File metadata and controls
86 lines (68 loc) · 2.35 KB
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
86
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""""Utilities for manipulating and inspecting OS paths."""
from '__go__/os' import Stat
from '__go__/path/filepath' import Abs, Base, Clean, Dir as dirname, IsAbs as isabs, Join, Split # pylint: disable=g-multiple-import,unused-import
def abspath(path):
result, err = Abs(path)
if err:
raise OSError(err.Error())
if isinstance(path, unicode):
# Grumpy compiler encoded the string into utf-8, so the result can be
# decoded using utf-8.
return unicode(result, 'utf-8')
return result
def basename(path):
return '' if path.endswith('/') else Base(path)
def exists(path):
_, err = Stat(path)
return err is None
def isdir(path):
info, err = Stat(path)
if info and err is None:
return info.Mode().IsDir()
return False
def isfile(path):
info, err = Stat(path)
if info and err is None:
return info.Mode().IsRegular()
return False
# NOTE(compatibility): This method uses Go's filepath.Join() method which
# implicitly normalizes the resulting path (pruning extra /, .., etc.) The usual
# CPython behavior is to leave all the cruft. This deviation is reasonable
# because a) result paths will point to the same files and b) one cannot assume
# much about the results of join anyway since it's platform dependent.
def join(*paths):
if not paths:
raise TypeError('join() takes at least 1 argument (0 given)')
parts = []
for p in paths:
if isabs(p):
parts = [p]
else:
parts.append(p)
result = Join(*parts)
if result and not paths[-1]:
result += '/'
return result
def normpath(path):
result = Clean(path)
if isinstance(path, unicode):
return unicode(result, 'utf-8')
return result
def split(path):
head, tail = Split(path)
if len(head) > 1 and head[-1] == '/':
head = head[:-1]
return (head, tail)