@@ -40,7 +40,9 @@ def main():
40
40
"needs to be changed. Otherwise returns 1." ,
41
41
)
42
42
parser .add_argument (
43
- "files" , nargs = "*" , help = "One or more python files to sort"
43
+ "files" ,
44
+ nargs = "*" ,
45
+ help = "One or more python files to sort, or '-' for stdin." ,
44
46
)
45
47
46
48
args = parser .parse_args ()
@@ -56,20 +58,29 @@ def main():
56
58
for path in find_python_files (args .files ):
57
59
errors = False
58
60
59
- try :
60
- original_bytes = path .read_bytes ()
61
- except FileNotFoundError :
62
- sys .stderr .write (f"ERROR: { escape_path (path )} does not exist\n " )
63
- unsortable += 1
64
- continue
65
- except IsADirectoryError :
66
- sys .stderr .write (f"ERROR: { escape_path (path )} is a directory\n " )
67
- unsortable += 1
68
- continue
69
- except PermissionError :
70
- sys .stderr .write (f"ERROR: { escape_path (path )} is not readable\n " )
71
- unsortable += 1
72
- continue
61
+ if str (path ) == "-" :
62
+ original_bytes = sys .stdin .buffer .read ()
63
+ else :
64
+ try :
65
+ original_bytes = path .read_bytes ()
66
+ except FileNotFoundError :
67
+ sys .stderr .write (
68
+ f"ERROR: { escape_path (path )} does not exist\n "
69
+ )
70
+ unsortable += 1
71
+ continue
72
+ except IsADirectoryError :
73
+ sys .stderr .write (
74
+ f"ERROR: { escape_path (path )} is a directory\n "
75
+ )
76
+ unsortable += 1
77
+ continue
78
+ except PermissionError :
79
+ sys .stderr .write (
80
+ f"ERROR: { escape_path (path )} is not readable\n "
81
+ )
82
+ unsortable += 1
83
+ continue
73
84
74
85
# The logic for converting from bytes to text is duplicated in `ssort`
75
86
# and here because we need access to the text to be able to compute a
@@ -80,6 +91,8 @@ def main():
80
91
sys .stderr .write (
81
92
f"ERROR: unknown encoding, { exc .encoding !r} , in { escape_path (path )} \n "
82
93
)
94
+ if str (path ) == "-" :
95
+ sys .stdout .buffer .write (original_bytes )
83
96
unsortable += 1
84
97
continue
85
98
@@ -89,6 +102,8 @@ def main():
89
102
sys .stderr .write (
90
103
f"ERROR: encoding error in { escape_path (path )} : { exc } \n "
91
104
)
105
+ if str (path ) == "-" :
106
+ sys .stdout .buffer .write (original_bytes )
92
107
unsortable += 1
93
108
continue
94
109
@@ -129,10 +144,14 @@ def _on_wildcard_import(**kwargs):
129
144
)
130
145
131
146
if errors :
147
+ if str (path ) == "-" :
148
+ sys .stdout .buffer .write (original_bytes )
132
149
unsortable += 1
133
150
continue
134
151
135
152
except Exception as e :
153
+ if str (path ) == "-" :
154
+ sys .stdout .buffer .write (original_bytes )
136
155
raise Exception (f"ERROR while sorting { path } \n " ) from e
137
156
138
157
if original != updated :
@@ -154,8 +173,13 @@ def _on_wildcard_import(**kwargs):
154
173
updated_bytes = re .sub ("\n " , newline , updated_bytes )
155
174
updated_bytes = updated_bytes .encode (encoding )
156
175
157
- path .write_bytes (updated_bytes )
176
+ if str (path ) == "-" :
177
+ sys .stdout .buffer .write (updated_bytes )
178
+ else :
179
+ path .write_bytes (updated_bytes )
158
180
else :
181
+ if str (path ) == "-" and not args .check :
182
+ sys .stdout .buffer .write (original_bytes )
159
183
unchanged += 1
160
184
161
185
if args .show_diff :
0 commit comments