3030
3131
3232class JavaMain :
33-
33+ max_output_length = 32 # lines beyond which we flag this
3434 maindef = re .compile ("public\s+static\s+void\s+main" )
3535 ellipses = ["[...]" .center (maxlinewidth , '_' )]
3636
@@ -40,7 +40,7 @@ class JFile:
4040 def with_main (javaFilePath ):
4141 with javaFilePath .open () as doc :
4242 code = doc .read ()
43- if JavaMain .maindef .search (code ):
43+ if JavaMain .maindef .search (code ) or "{Exec:" in code :
4444 return JavaMain .JFile (javaFilePath , code )
4545 return None
4646 def __init__ (self , javaFilePath , code ):
@@ -62,6 +62,8 @@ def create(javaFilePath):
6262 return None
6363 if "/* Output: (None) */" in j_file .code :
6464 return None
65+ if "/* Output: (Execute to see)" in j_file .code :
66+ return None
6567 outfile = javaFilePath .with_name (javaFilePath .stem + "-output.txt" )
6668 errfile = javaFilePath .with_name (javaFilePath .stem + "-erroroutput.txt" )
6769 if outfile .exists () or errfile .exists ():
@@ -75,6 +77,7 @@ def __init__(self, javaFilePath, j_file, outfile, errfile):
7577 self .errfile = errfile
7678 self .first_and_last = None
7779 self .first_lines = None
80+ self .long_output = False
7881
7982 ol = self .j_file .output_line
8083 if ol :
@@ -103,6 +106,9 @@ def __init__(self, javaFilePath, j_file, outfile, errfile):
103106 result += err
104107 self .result = JavaMain .wrapOutput (result ) + "\n "
105108
109+ if len (self .result .splitlines ()) > JavaMain .max_output_length :
110+ self .long_output = True
111+
106112 for line in self .j_file .lines :
107113 if line .startswith ("} ///:~" ):
108114 self .j_file .newcode += "} /* Output:\n "
@@ -127,6 +133,9 @@ def wrapOutput(output):
127133 result += textwrap .wrap (line .rstrip (), width = maxlinewidth )
128134 return "\n " .join (result )
129135
136+ def write_modified_file (self ):
137+ with self .j_file .javaFilePath .open ('w' ) as modified :
138+ modified .write (self .j_file .newcode )
130139
131140
132141@CmdLine ('o' )
@@ -169,8 +178,6 @@ def extractResults():
169178 if outline :
170179 results .write (outline + "\n " )
171180 results .write (j_main .result )
172- # else:
173- # results.write("[ No output for {} ]\n".format(jfp))
174181 os .system ("subl AttachedResults.txt" )
175182
176183@CmdLine ('n' )
@@ -201,73 +208,43 @@ def noOutputFixup():
201208 # test.write(ruler(jfp))
202209 # test.write(newcode)
203210
211+ @CmdLine ('v' )
212+ def viewAttachedFiles ():
213+ """View all files containing output in this directory and below"""
214+ for java in Path ("." ).rglob ("*.java" ):
215+ with java .open () as codefile :
216+ code = codefile .read ()
217+ if "/* Output:" in code :
218+ if "/* Output: (None)" in code :
219+ continue
220+ if "/* Output: (Execute to see)" in code :
221+ continue
222+ for n , line in enumerate (code .splitlines ()):
223+ if "/* Output:" in line :
224+ os .system ("subl {}:{}" .format (java , n ))
225+ continue
226+
204227
205228@CmdLine ('a' )
206229def attachFiles ():
207230 """Attach standard and error output to all files"""
208231 os .chdir (str (examplePath ))
209- test = open ("test.txt" , 'w' )
232+ test = open ("AllFilesWithOutput.txt" , 'w' )
233+ longOutput = open ("LongOutput.txt" , 'w' )
210234 for jfp in Path ("." ).rglob ("*.java" ):
211235 if "gui" in jfp .parts or "swt" in jfp .parts :
212236 continue
213237 j_main = JavaMain .create (jfp )
214238 if j_main is None :
215239 continue
240+ j_main .write_modified_file ()
216241 test .write (ruler ())
217242 test .write (j_main .new_code ())
218- os .system ("subl test.txt" )
243+ if j_main .long_output :
244+ longOutput .write (ruler ())
245+ longOutput .write (j_main .new_code ())
246+ os .system ("subl AllFilesWithOutput.txt" )
247+ os .system ("subl LongOutput.txt" )
219248
220249if __name__ == '__main__' : CmdLine .run ()
221250
222-
223-
224- # def newOutput(javaFilePath):
225- # outfile = javaFilePath.with_name(javaFilePath.stem + "-output.txt")
226- # errfile = javaFilePath.with_name(javaFilePath.stem + "-erroroutput.txt")
227- # result =""
228- # if outfile.exists():
229- # with outfile.open() as f:
230- # out = f.read().strip()
231- # if out:
232- # result += out + "\n"
233- # if errfile.exists():
234- # with errfile.open() as f:
235- # err = f.read().strip()
236- # if err:
237- # result += "___[ Error Output ]___\n"
238- # result += err
239- # result = wrapOutput(result)
240- # if result:
241- # return result + "\n"
242- # return None
243-
244-
245-
246-
247-
248-
249-
250- # def appendOutputFiles(javaFilePath):
251- # jfile = JFile.with_main(javaFilePath)
252- # if jfile is None:
253- # return
254- # output = newOutput(javaFilePath)
255- # if not output:
256- # return
257- # if not self.output_tags.has_output: # no /* Output: at all
258- # with self.javaFilePath.open() as jf:
259- # code = jf.read()
260- # lines = code.splitlines()
261- # while lines[-1].strip() is "":
262- # lines.pop()
263- # assert lines[-1].rstrip() == "} ///:~"
264- # lines[-1] = "} /* Output:"
265- # lines.append(self.new_output)
266- # lines.append("*///:~")
267- # result = "\n".join(lines) + "\n"
268- # with self.javaFilePath.open("w") as jf:
269- # jf.write(result)
270- # return result
271- # else:
272- # print("{} already has Output!".format(self.javaFilePath))
273- # sys.exit()
0 commit comments