11from .utils import *
22import subprocess
3+ import tempfile
4+ import os
35
46
57class IO (object ):
@@ -23,29 +25,37 @@ def __init__(self, *args, **kwargs):
2325 """
2426 if len (args ) == 0 :
2527 if not "file_prefix" in kwargs :
26- raise Exception ("You must specify either two file names or file_prefix." )
27-
28- if "data_id" in kwargs :
29- filename_prefix = "%s%d" % (kwargs ["file_prefix" ], kwargs ["data_id" ])
28+ self .file_flag = 0
29+ (fd , self .input_filename ) = tempfile .mkstemp ()
30+ os .close (fd )
31+ (fd , self .output_filename ) = tempfile .mkstemp ()
32+ os .close (fd )
3033 else :
31- filename_prefix = kwargs ["file_prefix" ]
34+ self .file_flag = 2
35+ if "data_id" in kwargs :
36+ filename_prefix = "%s%d" % (kwargs ["file_prefix" ], kwargs ["data_id" ])
37+ else :
38+ filename_prefix = kwargs ["file_prefix" ]
3239
33- input_suffix = kwargs .get ("input_suffix" , ".in" )
34- output_suffix = kwargs .get ("output_suffix" , ".out" )
35- disable_output = kwargs .get ("disable_output" , False )
36- self .input_filename = filename_prefix + input_suffix
37- self .output_filename = filename_prefix + output_suffix if not disable_output else None
40+ input_suffix = kwargs .get ("input_suffix" , ".in" )
41+ output_suffix = kwargs .get ("output_suffix" , ".out" )
42+ disable_output = kwargs .get ("disable_output" , False )
43+ self .input_filename = filename_prefix + input_suffix
44+ self .output_filename = filename_prefix + output_suffix if not disable_output else None
3845 elif len (args ) == 1 :
46+ self .file_flag = 1
3947 self .input_filename = args [0 ]
40- self .output_filename = None
48+ (fd , self .output_filename ) = tempfile .mkstemp ()
49+ os .close (fd )
4150 elif len (args ) == 2 :
51+ self .file_flag = 2
4252 self .input_filename = args [0 ]
4353 self .output_filename = args [1 ]
4454 else :
4555 raise Exception ("Invalid argument count" )
4656
47- self .input_file = open (self .input_filename , 'w' )
48- self .output_file = open (self .output_filename , 'w' ) if self .output_filename else None
57+ self .input_file = open (self .input_filename , 'w+ ' )
58+ self .output_file = open (self .output_filename , 'w+ ' ) if self .output_filename else None
4959 self .is_first_char = dict ()
5060 print ("Processing %s" % self .input_filename )
5161
@@ -56,6 +66,10 @@ def __del__(self):
5666 try :
5767 self .input_file .close ()
5868 self .output_file .close ()
69+ if self .file_flag <= 1 :
70+ os .remove (self .output_filename )
71+ if self .file_flag == 0 :
72+ os .remove (self .input_filename )
5973 except Exception :
6074 pass
6175
@@ -69,6 +83,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
6983 try :
7084 self .input_file .close ()
7185 self .output_file .close ()
86+ if self .file_flag <= 1 :
87+ os .remove (self .output_filename )
88+ if self .file_flag == 0 :
89+ os .remove (self .input_filename )
7290 except Exception :
7391 pass
7492
@@ -118,7 +136,7 @@ def output_gen(self, shell_cmd):
118136 with open (self .input_filename , 'r' ) as f :
119137 self .output_file .write (subprocess .check_output (shell_cmd , shell = True , stdin = f ).decode ('ascii' ))
120138
121- self .input_file = open (self .input_filename , 'a' )
139+ self .input_file = open (self .input_filename , 'a+ ' )
122140 print (self .output_filename , " done" )
123141
124142 def output_write (self , * args , ** kwargs ):
@@ -138,3 +156,6 @@ def output_writeln(self, *args, **kwargs):
138156 args = list (args )
139157 args .append ("\n " )
140158 self .output_write (* args , ** kwargs )
159+
160+ def flush_buffer (self ):
161+ self .input_file .flush ()
0 commit comments