11#!/usr/bin/env python
2- # encoding: utf-8
3-
42import codecs
53import json
64import os
97DEFAULT_SECRET_PHRASES = ("key" , "password" , "secret" )
108
119
12- class Worker ( object ) :
10+ class Worker :
1311 READ_TIMEOUT = 3 # seconds
1412
1513 def __init__ (self , job_directory , secret_phrases ):
@@ -25,8 +23,9 @@ def __init__(self, job_directory, secret_phrases):
2523 self .secret_phrases = secret_phrases
2624 # Load input
2725 self ._input = {}
28- if os .path .isfile ("%s/input/input.json" % self .job_directory ):
29- with open ("%s/input/input.json" % self .job_directory ) as f_input :
26+ input_path = f"{ self .job_directory } /input/input.json"
27+ if os .path .isfile (input_path ):
28+ with open (input_path ) as f_input :
3029 self ._input = json .load (f_input )
3130 else :
3231 # If input file doesn't exist,
@@ -72,15 +71,9 @@ def __set_proxies(self):
7271 def __set_encoding ():
7372 try :
7473 if sys .stdout .encoding != "UTF-8" :
75- if sys .version_info [0 ] == 3 :
76- sys .stdout = codecs .getwriter ("utf-8" )(sys .stdout .buffer , "strict" )
77- else :
78- sys .stdout = codecs .getwriter ("utf-8" )(sys .stdout , "strict" )
74+ sys .stdout = codecs .getwriter ("utf-8" )(sys .stdout .buffer , "strict" )
7975 if sys .stderr .encoding != "UTF-8" :
80- if sys .version_info [0 ] == 3 :
81- sys .stderr = codecs .getwriter ("utf-8" )(sys .stderr .buffer , "strict" )
82- else :
83- sys .stderr = codecs .getwriter ("utf-8" )(sys .stderr , "strict" )
76+ sys .stderr = codecs .getwriter ("utf-8" )(sys .stderr .buffer , "strict" )
8477 except Exception :
8578 pass # nosec B110
8679
@@ -123,13 +116,9 @@ def __write_output(self, data, ensure_ascii=False):
123116 if self .job_directory is None :
124117 json .dump (data , sys .stdout , ensure_ascii = ensure_ascii )
125118 else :
126- try :
127- os .makedirs ("%s/output" % self .job_directory )
128- except Exception :
129- pass # nosec B110
130- with open (
131- "%s/output/output.json" % self .job_directory , mode = "w"
132- ) as f_output :
119+ output_path = f"{ self .job_directory } /output"
120+ os .makedirs (output_path , exist_ok = True )
121+ with open (f"{ output_path } /output.json" , mode = "w" ) as f_output :
133122 json .dump (data , f_output , ensure_ascii = ensure_ascii )
134123
135124 def get_data (self ):
0 commit comments