File tree Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Original file line number Diff line number Diff line change @@ -76,10 +76,16 @@ def __setitem__(
76
76
value : typing .Union [str , libioc .Types .AbsolutePath ]
77
77
) -> None :
78
78
"""Set an item of the FstabLine."""
79
- if (key == "source" ) or (key == "destination" ):
79
+ _type = None
80
+ if key == "source" :
81
+ _type = libioc .Types .Path
82
+ elif key == "destination" :
83
+ _type = libioc .Types .AbsolutePath
84
+
85
+ if _type is not None : # source or destination
80
86
if isinstance (value , str ) is True :
81
- absolute_path = libioc . Types . AbsolutePath (value )
82
- elif isinstance (value , libioc . Types . AbsolutePath ) is True :
87
+ absolute_path = _type (value )
88
+ elif isinstance (value , _type ) is True :
83
89
absolute_path = value
84
90
else :
85
91
raise ValueError ("String or AbsolutePath expected" )
@@ -287,7 +293,7 @@ def parse_lines(
287
293
])
288
294
289
295
new_line = FstabLine ({
290
- "source" : libioc .Types .AbsolutePath (source ),
296
+ "source" : libioc .Types .Path (source ),
291
297
"destination" : libioc .Types .AbsolutePath (destination ),
292
298
"type" : fragments [2 ],
293
299
"options" : fragments [3 ],
Original file line number Diff line number Diff line change 27
27
import re
28
28
29
29
30
- class AbsolutePath (str ):
30
+ class Path (str ):
31
31
"""Wrapper Type for ensuring a `str` matches a Unix Path."""
32
32
33
- unix_path = re .compile (r"/([^/\0]+/*)+" )
33
+ blacklist = re .compile (
34
+ r"(\/\/)|(\/\.\.)|(\.\.\/)|(\n)|(\r)|(^\.+$)" ,
35
+ re .MULTILINE
36
+ )
34
37
35
38
def __init__ (
36
- self ,
37
- sequence : str
39
+ self ,
40
+ sequence : str
38
41
) -> None :
39
- if self .unix_path .fullmatch (sequence ) is None :
40
- raise TypeError (f"Invalid value for AbsolutePath: { sequence } " )
42
+ if isinstance (sequence , str ) is False :
43
+ raise TypeError ("Path must be a string" )
44
+
45
+ if len (self .blacklist .findall (sequence )) > 0 :
46
+ raise TypeError (f"Illegal path: { sequence } " )
47
+
41
48
self = sequence # type: ignore
42
49
43
50
51
+ class AbsolutePath (Path ):
52
+ """Wrapper Type for ensuring a `str` matches an absolute Unix Path."""
53
+
54
+ def __init__ (
55
+ self ,
56
+ sequence : str
57
+ ) -> None :
58
+ if isinstance (sequence , str ) is False :
59
+ raise TypeError ("AbsolutePath must be a string or Path" )
60
+
61
+ if str (sequence ).startswith ("/" ) is False :
62
+ raise TypeError (
63
+ f"Expected AbsolutePath to begin with /, but got: { sequence } "
64
+ )
65
+
66
+ super ().__init__ (sequence )
67
+
68
+
44
69
class UserInput :
45
70
"""Any kind of user input data."""
46
71
You can’t perform that action at this time.
0 commit comments