Commits

Panagiotis Mavrogiorgos committed 4a13923

Added argparse. Functionality checked with -a flag

  • Participants
  • Parent commits 8830302

Comments (0)

Files changed (1)

File external_tikz.py

 Notes
 -----
 1. No command line arguments. To change the input, you must edit the
-   corresponding variables. 
+   corresponding variables.
 2. The script must be placed on the same folder as the main tex file
    whose name is specified by the MAIN_FILE variable.
 3. The pickled dictionary is stored in a file whose name is specified
 import pickle
 import subprocess
 import shutil
+import argparse
 
 PICKLE_FILE = "external_tikz.pickle"
 MAIN_FILE = "my_thesis.tex"
             old = pickle.load(f)
     return old
 
-def create_tex_file(cwd, main_file, tikz_source_path, preview, name):
+def create_tex_file(main_file, preview, cwd, tikz_source_path, name):
     """
     Create the source file and write to disk (at the current working folder)
     """
-    source_file = ""
+
     source_file = read_preamble(cwd, main_file, preview)
 
     with open(tikz_source_path,"r") as f:
     with open(name + ".tex", "w") as f:
         f.write(source_file)
 
-def create_pdf(tikz_source_path, ext, compiler, name):
+def create_pdf(ext, compiler, cwd, tikz_source_path, name):
     """
     Run XeLaTeX to the source file and copy it to the same folder as the tikz
     source code
     """
-    subprocess.call('%s %s.tex' % (compiler, name), shell=True)
-
     filename = name + ".pdf"
     try: # old file removal
         os.remove(os.path.join(os.path.dirname(tikz_source_path),filename))
     except:
         pass
 
-    # file moving
-    shutil.move(filename, os.path.dirname(tikz_source_path))
+    subprocess.call('%s %s.tex' % (compiler, name), shell=True)
+
+    # If the TSF is not in the same folder as the main file, then move it
+    if cwd != os.path.dirname(tikz_source_path):
+        shutil.move(filename, os.path.dirname(tikz_source_path))
 
 def main():
-    #-------------------------------------------------------------------------------
     cwd = os.path.relpath(os.curdir)
-    old = read_stored_data(cwd, PICKLE_FILE)
-    new = read_source_files(cwd, EXT)
-    #-------------------------------------------------------------------------------
-    # Compare the new and the old dicts
+
+    parser = argparse.ArgumentParser(description='Auto-compiles tikz source files.')
+
+    parser.add_argument('-f', '--files', dest='files', metavar = None,
+                       help='Input a tikz file list to be compiled.')
+
+    parser.add_argument('-a', '--recompile-all', dest='recompile_all',
+                       help='Recompiles all the tikz files.', action = 'store_true')
+
+    args = parser.parse_args()
+
+    # if the -a flag has been passed, erase the pickle file.
+    if args.recompile_all is True:
+        try:
+            os.remove(os.path.join(cwd, PICKLE_FILE))
+        except:
+            pass
+
+    # Read and compare the new and the old dicts
     # if the entries have been modified since last time or if there is a new entry
     # create a temporary file and compile it with xelatex
     # then move the newly created pdf to the same folder as the tikz source file.
+    old = read_stored_data(cwd, PICKLE_FILE)
+    new = read_source_files(cwd, EXT)
     for (tikz_source_path,mod_time) in new.iteritems():
         if tikz_source_path not in old or mod_time != old[tikz_source_path]:
             name = tikz_source_path.split(os.path.sep)[-1].replace("."+EXT, "")
-            create_tex_file(cwd, MAIN_FILE, tikz_source_path, PREVIEW, name)
-            create_pdf(tikz_source_path, EXT, COMPILER, name)
+            create_tex_file(MAIN_FILE, PREVIEW, cwd, tikz_source_path, name)
+            create_pdf(EXT, COMPILER, cwd, tikz_source_path, name)
             #----------------------------------------------------------------------
             # clean up code
-            # The created extensions depend on the packages uses in the preamble
+            # The created extensions depend on the packages used in the preamble
             # If leftover, files are found, then just add more entires in the
             # following tuple.
             for f in (name + ".aux", name + ".log", name + ".tex",
 
 if __name__ == "__main__":
     main()
+
+
+
+