Skip to content

Commit 5460f9c

Browse files
Merge pull request #67 from jessevz/master
Added windows support
2 parents a86fcea + 052eca9 commit 5460f9c

4 files changed

Lines changed: 133 additions & 115 deletions

File tree

bin/demeuk.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,14 @@
148148
from glob import glob
149149
from html import unescape
150150
from inspect import cleandoc
151-
from locale import LC_ALL, setlocale
151+
from locale import LC_ALL, setlocale, getlocale
152152
from math import ceil
153-
from multiprocessing import cpu_count, Pool
153+
from multiprocessing import cpu_count
154+
from os import name as os_name
155+
if os_name == 'nt':
156+
from multiprocessing.pool import ThreadPool as Pool
157+
else:
158+
from multiprocessing import Pool
154159
from os import linesep, access, path, R_OK, F_OK, W_OK
155160
from re import compile as re_compile
156161
from re import search
@@ -647,9 +652,7 @@ def check_empty_line(line):
647652
Returns:
648653
true of line is empty or only contains whitespace chars
649654
"""
650-
if line == '':
651-
return True
652-
elif line.isspace():
655+
if line == '' or line.isspace():
653656
return True
654657
return False
655658

@@ -1375,7 +1378,7 @@ def chunkify(filename, size=CHUNK_SIZE):
13751378
fh.readline()
13761379

13771380
while True:
1378-
lines = [line.rstrip(b'\n') for line in fh.readlines(size)]
1381+
lines = [line.rstrip(linesep.encode()) for line in fh.readlines(size)]
13791382
yield lines
13801383
if len(lines) == 0:
13811384
break
@@ -1388,6 +1391,16 @@ def stderr_print(*args, **kwargs):
13881391
print(*args, **kwargs)
13891392

13901393

1394+
def init_worker(config_data):
1395+
global config
1396+
config = config_data
1397+
1398+
try:
1399+
signal(SIGINT, SIG_IGN)
1400+
except ValueError:
1401+
pass # signal() only works in the main thread; ThreadPool workers are threads
1402+
1403+
13911404
def main():
13921405
#
13931406
# Config parser
@@ -1507,10 +1520,9 @@ def main():
15071520
if arguments.get('--input-encoding'):
15081521
config['input_encoding'] = arguments.get('--input-encoding').split(',')
15091522

1523+
setlocale(LC_ALL, 'en_US.UTF-8')
15101524
if arguments.get('--output-encoding'):
15111525
setlocale(LC_ALL, arguments.get('--output-encoding'))
1512-
else:
1513-
setlocale(LC_ALL, 'en_US.UTF-8')
15141526

15151527
if arguments.get('--punctuation'):
15161528
config['punctuation'] = arguments.get('--punctuation')
@@ -1741,13 +1753,14 @@ def main():
17411753
stderr_print('Main: done chunking file.')
17421754
stderr_print('Main: processing started.')
17431755

1756+
encoding = getlocale()[1]
17441757
if output_file:
1745-
p_output_file = open(output_file, 'w')
1758+
p_output_file = open(output_file, 'w', encoding=encoding, newline='')
17461759
else:
17471760
p_output_file = stdout
17481761

17491762
if log_file:
1750-
p_log_file = open(log_file, 'a')
1763+
p_log_file = open(log_file, 'a', encoding=encoding, newline='')
17511764
else:
17521765
p_log_file = stderr
17531766

@@ -1764,9 +1777,6 @@ def write_results_and_log(async_result):
17641777
write_results(async_result['results'])
17651778
write_log(async_result['log'])
17661779

1767-
def init_worker():
1768-
signal(SIGINT, SIG_IGN)
1769-
17701780
def process_jobs(chunk_start):
17711781
# Cut file in to chunks and process each trunk multi-threaded
17721782
while True:
@@ -1791,7 +1801,7 @@ def process_jobs(chunk_start):
17911801
sleep(1)
17921802

17931803
write_log(f'Running demeuk - {version}{linesep}')
1794-
with Pool(a_threads, init_worker) as pool:
1804+
with Pool(a_threads, init_worker, initargs=(config,)) as pool:
17951805
jobs = []
17961806
# chunk_start will be the started value of the combined output lines
17971807
chunk_start = 0

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
docopt
2-
chardet
2+
chardet==5.2.0
33
nltk
44
ftfy
55
unidecode

0 commit comments

Comments
 (0)