Skip to content

Commit 32b7b9c

Browse files
committed
Workaround for Japanese Windows environment
- Avoiding problems with incorrect encoding `UnicodeDecodeError: 'cp932' codec can't decode byte 0x85 in position 2897: illegal multibyte sequence` - Added option to output directly to file PowerShell converts encoding and newline characters when passing stdout
1 parent 0b92413 commit 32b7b9c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Diff for: expand.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
Options:
2727
-a --all import all modules
2828
-h --help print help
29+
-o file
30+
--output file output file
2931
'''
3032
output_header = '//https://github.com/rust-lang-ja/ac-library-rs\n'
31-
opt_list = ['help', 'all']
33+
opt_list = ['help', 'all', 'output=']
3234
output_list_all = ('convolution', 'dsu', 'fenwicktree', 'lazysegtree', 'math',
3335
'maxflow', 'mincostflow', 'modint', 'scc', 'segtree',
3436
'string', 'twosat',
@@ -44,13 +46,14 @@
4446
'segtree': ('internal_bit', 'internal_type_traits',),
4547
'twosat': ('internal_scc',), }
4648
src_path = 'src/'
49+
output_path = None
4750

4851

4952
def output_file(filename):
5053
global src_path
5154

5255
res = []
53-
with open(src_path+filename+'.rs', 'r') as f:
56+
with open(src_path+filename+'.rs', 'r', encoding='utf-8', newline='') as f:
5457
res.append('pub mod {} {{'.format(filename))
5558

5659
for line in f:
@@ -61,7 +64,7 @@ def output_file(filename):
6164

6265

6366
try:
64-
opts, args = getopt.getopt(sys.argv[1:], 'ah', opt_list)
67+
opts, args = getopt.getopt(sys.argv[1:], 'aho:', opt_list)
6568
except getopt.GetoptError as e:
6669
print(e)
6770
print(usage)
@@ -77,6 +80,8 @@ def output_file(filename):
7780
sys.exit(0)
7881
elif o == '--all' or o == '-a':
7982
args = list(output_list_all)
83+
elif o == '--output' or o == '-o':
84+
output_path = v
8085

8186
output_list = set()
8287

@@ -108,11 +113,14 @@ def output_file(filename):
108113
# rustfmt
109114
with tempfile.TemporaryDirectory() as temp_dir:
110115
temp_file = temp_dir + '/output.rs'
111-
with open(temp_file, 'w') as f:
116+
with open(temp_file, 'w', encoding='utf-8', newline='') as f:
112117
print(output_header, file=f)
113118
for i in output_data:
114119
print(i, file=f)
115120
output_data = subprocess.run(["rustfmt", temp_file], check=True)
116-
with open(temp_file, 'r') as f:
121+
with open(temp_file, 'r', encoding='utf-8', newline='') as f:
122+
wf = open(output_path, 'w', encoding='utf-8', newline='') if output_path is not None else sys.stdout
117123
for line in f:
118-
print(line, end="")
124+
print(line, end='', file=wf)
125+
if output_path is not None:
126+
wf.close()

0 commit comments

Comments
 (0)