FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mip/python-stdlib/pathlib/pathlib.py at PyDevices · PyDevices/mip · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
PyDevices
/
mip
Public
forked from
micropython/micropython-lib
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
mip
/
python-stdlib
/
pathlib
/
pathlib.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
210 lines (166 loc) · 5.71 KB
Breadcrumbs
mip
/
python-stdlib
/
pathlib
/
pathlib.py
Copy path
File metadata and controls
210 lines (166 loc) · 5.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import
errno
import
os
from
micropython
import
const
_SEP
=
const
(
"/"
)
def
_mode_if_exists
(
path
):
try
:
return
os
.
stat
(
path
)[
0
]
except
OSError
as
e
:
if
e
.
errno
==
errno
.
ENOENT
:
return
0
raise
e
def
_clean_segment
(
segment
):
segment
=
str
(
segment
)
if
not
segment
:
return
"."
segment
=
segment
.
rstrip
(
_SEP
)
if
not
segment
:
return
_SEP
while
True
:
no_double
=
segment
.
replace
(
_SEP
+
_SEP
,
_SEP
)
if
no_double
==
segment
:
break
segment
=
no_double
return
segment
class
Path
:
def
__init__
(
self
,
*
segments
):
segments_cleaned
=
[]
for
segment
in
segments
:
segment
=
_clean_segment
(
segment
)
if
segment
[
0
]
==
_SEP
:
segments_cleaned
=
[
segment
]
elif
segment
==
"."
:
continue
else
:
segments_cleaned
.
append
(
segment
)
self
.
_path
=
_clean_segment
(
_SEP
.
join
(
segments_cleaned
))
def
__truediv__
(
self
,
other
):
return
Path
(
self
.
_path
,
str
(
other
))
def
__rtruediv__
(
self
,
other
):
return
Path
(
other
,
self
.
_path
)
def
__repr__
(
self
):
return
f'
{
type
(
self
).
__name__
}
("
{
self
.
_path
}
")'
def
__str__
(
self
):
return
self
.
_path
def
__eq__
(
self
,
other
):
return
self
.
absolute
()
==
Path
(
other
).
absolute
()
def
absolute
(
self
):
path
=
self
.
_path
cwd
=
os
.
getcwd
()
if
not
path
or
path
==
"."
:
return
cwd
if
path
[
0
]
==
_SEP
:
return
path
return
_SEP
+
path
if
cwd
==
_SEP
else
cwd
+
_SEP
+
path
def
resolve
(
self
):
return
self
.
absolute
()
def
open
(
self
,
mode
=
"r"
,
encoding
=
None
):
return
open
(
self
.
_path
,
mode
,
encoding
=
encoding
)
def
exists
(
self
):
return
bool
(
_mode_if_exists
(
self
.
_path
))
def
mkdir
(
self
,
parents
=
False
,
exist_ok
=
False
):
try
:
os
.
mkdir
(
self
.
_path
)
return
except
OSError
as
e
:
if
e
.
errno
==
errno
.
EEXIST
and
exist_ok
:
return
elif
e
.
errno
==
errno
.
ENOENT
and
parents
:
pass
# handled below
else
:
raise
e
segments
=
self
.
_path
.
split
(
_SEP
)
progressive_path
=
""
if
segments
[
0
]
==
""
:
segments
=
segments
[
1
:]
progressive_path
=
_SEP
for
segment
in
segments
:
progressive_path
+=
_SEP
+
segment
try
:
os
.
mkdir
(
progressive_path
)
except
OSError
as
e
:
if
e
.
errno
!=
errno
.
EEXIST
:
raise
e
def
is_dir
(
self
):
return
bool
(
_mode_if_exists
(
self
.
_path
)
&
0x4000
)
def
is_file
(
self
):
return
bool
(
_mode_if_exists
(
self
.
_path
)
&
0x8000
)
def
_glob
(
self
,
path
,
pattern
,
recursive
):
# Currently only supports a single "*" pattern.
n_wildcards
=
pattern
.
count
(
"*"
)
n_single_wildcards
=
pattern
.
count
(
"?"
)
if
n_single_wildcards
:
raise
NotImplementedError
(
"? single wildcards not implemented."
)
if
n_wildcards
==
0
:
raise
ValueError
elif
n_wildcards
>
1
:
raise
NotImplementedError
(
"Multiple * wildcards not implemented."
)
prefix
,
suffix
=
pattern
.
split
(
"*"
)
for
name
,
mode
,
*
_
in
os
.
ilistdir
(
path
):
full_path
=
path
+
_SEP
+
name
if
name
.
startswith
(
prefix
)
and
name
.
endswith
(
suffix
):
yield
full_path
if
recursive
and
mode
&
0x4000
:
# is_dir
yield
from
self
.
_glob
(
full_path
,
pattern
,
recursive
=
recursive
)
def
glob
(
self
,
pattern
):
"""Iterate over this subtree and yield all existing files (of any
kind, including directories) matching the given relative pattern.
Currently only supports a single "*" pattern.
"""
return
self
.
_glob
(
self
.
_path
,
pattern
,
recursive
=
False
)
def
rglob
(
self
,
pattern
):
return
self
.
_glob
(
self
.
_path
,
pattern
,
recursive
=
True
)
def
stat
(
self
):
return
os
.
stat
(
self
.
_path
)
def
read_bytes
(
self
):
with
open
(
self
.
_path
,
"rb"
)
as
f
:
return
f
.
read
()
def
read_text
(
self
,
encoding
=
None
):
with
open
(
self
.
_path
,
"r"
,
encoding
=
encoding
)
as
f
:
return
f
.
read
()
def
rename
(
self
,
target
):
os
.
rename
(
self
.
_path
,
target
)
def
rmdir
(
self
):
os
.
rmdir
(
self
.
_path
)
def
touch
(
self
,
exist_ok
=
True
):
if
self
.
exists
():
if
exist_ok
:
return
# TODO: should update timestamp
else
:
# In lieue of FileExistsError
raise
OSError
(
errno
.
EEXIST
)
with
open
(
self
.
_path
,
"w"
):
pass
def
unlink
(
self
,
missing_ok
=
False
):
try
:
os
.
unlink
(
self
.
_path
)
except
OSError
as
e
:
if
not
(
missing_ok
and
e
.
errno
==
errno
.
ENOENT
):
raise
e
def
write_bytes
(
self
,
data
):
with
open
(
self
.
_path
,
"wb"
)
as
f
:
f
.
write
(
data
)
def
write_text
(
self
,
data
,
encoding
=
None
):
with
open
(
self
.
_path
,
"w"
,
encoding
=
encoding
)
as
f
:
f
.
write
(
data
)
def
with_suffix
(
self
,
suffix
):
index
=
-
len
(
self
.
suffix
)
or
None
return
Path
(
self
.
_path
[:
index
]
+
suffix
)
@
property
def
stem
(
self
):
return
self
.
name
.
rsplit
(
"."
,
1
)[
0
]
@
property
def
parent
(
self
):
tokens
=
self
.
_path
.
rsplit
(
_SEP
,
1
)
if
len
(
tokens
)
==
2
:
if
not
tokens
[
0
]:
tokens
[
0
]
=
_SEP
return
Path
(
tokens
[
0
])
return
Path
(
"."
)
@
property
def
name
(
self
):
return
self
.
_path
.
rsplit
(
_SEP
,
1
)[
-
1
]
@
property
def
suffix
(
self
):
elems
=
self
.
_path
.
rsplit
(
"."
,
1
)
return
""
if
len
(
elems
)
==
1
else
"."
+
elems
[
1
]
Back
|
FazBrowse Home
|
New Git URL