slhaplot

changeset 134
323754f1d261
parent 133
5e27f4121fd1
child 136
1218f20d4cf5
     1.1 --- a/slhaplot	Sun Mar 06 23:44:39 2011 +0000
     1.2 +++ b/slhaplot	Mon Mar 07 10:48:37 2011 +0000
     1.3 @@ -17,8 +17,6 @@
     1.4    * Merge labels if shifting fails (cf. "poi" test spectrum file).
     1.5    * Avoid overlap/too-tight clustering of y-axis ticks. Looks fine for *most*
     1.6      spectra I've seen with 100 GeV intervals, but it should be handled properly.
     1.7 -  * Use object wrapper for format strings, to centralise "needs pdflatex?"-type
     1.8 -    questions and provide UI case-insensitivity
     1.9    * Allow user to provide a file which defines the particle line x-positions, labels, etc.
    1.10    * Allow use of --outname to specify a list of output base names for multiple inputs.
    1.11    * Use proper distinction between physical, plot-logical, and plot output coords.
    1.12 @@ -82,10 +80,80 @@
    1.13          self.color = color
    1.14  
    1.15  
    1.16 +# ## Python version workaround
    1.17 +# if not "any" in dir():
    1.18 +#     def any(*args):
    1.19 +#         for i in args:
    1.20 +#             if i: return True
    1.21 +#         return False
    1.22 +
    1.23 +
    1.24  class OutputFormatSpec(object):
    1.25      """Object to abstract translation of semi-arbitrary format strings into
    1.26      something more semantically queryable."""
    1.27 -    pass
    1.28 +
    1.29 +    def __init__(self, fmtstr):
    1.30 +        self.format_string = fmtstr.lower()
    1.31 +        if "tikz" not in self.format_string:
    1.32 +            self.format_string = "tikz" + self.format_string
    1.33 +        if self.format_string == "tikz":
    1.34 +            self.format_string = "tikztex"
    1.35 +        elif self.format_string == "tikzfrag":
    1.36 +            self.format_string = "tikztexfrag"
    1.37 +        if "frag" in self.format_string and any(f in self.format_string for f in ["pdf", "eps", "png"]):
    1.38 +            logging.error("Oops! You can't currently use LaTeX fragment output together with graphics "
    1.39 +                          "formats, since the graphics can't be built from the incomplete LaTeX "
    1.40 +                          "file. We'll fix this, but for now you will have to run slhaplot twice: "
    1.41 +                          "once for the LaTeX fragment, and another time for the graphical output "
    1.42 +                          "formats. Exiting...")
    1.43 +            sys.exit(1)
    1.44 +
    1.45 +    def make_tex(self):
    1.46 +        return ("tex" in self.format_string and not "frag" in self.format_string)
    1.47 +
    1.48 +    def make_texfrag(self):
    1.49 +        return ("texfrag" in self.format_string)
    1.50 +
    1.51 +    def make_pdf(self):
    1.52 +        return ("pdf" in self.format_string)
    1.53 +
    1.54 +    def make_eps(self):
    1.55 +        return ("eps" in self.format_string)
    1.56 +
    1.57 +    def make_png(self):
    1.58 +        return ("png" in self.format_string)
    1.59 +
    1.60 +
    1.61 +    def need_tikz(self):
    1.62 +        for f in ["pdf", "eps", "png"]:
    1.63 +            if f in self.format_string:
    1.64 +                return True
    1.65 +        return False
    1.66 +
    1.67 +    def need_epslatex(self):
    1.68 +        for f in ["eps"]:
    1.69 +            if f in self.format_string:
    1.70 +                return True
    1.71 +        return False
    1.72 +
    1.73 +    def need_pdflatex(self):
    1.74 +        for f in ["pdf", "png"]:
    1.75 +            if f in self.format_string:
    1.76 +                return True
    1.77 +        return False
    1.78 +
    1.79 +    def need_convert(self):
    1.80 +        for f in ["png"]:
    1.81 +            if f in self.format_string:
    1.82 +                return True
    1.83 +        return False
    1.84 +
    1.85 +    def need_compilation(self):
    1.86 +        return self.need_epslatex() or self.need_pdflatex()
    1.87 +
    1.88 +    def file_extensions(self):
    1.89 +        return [f for f in ["tex", "pdf", "eps", "png"] if f in self.format_string]
    1.90 +
    1.91  
    1.92  
    1.93  import pyslha
    1.94 @@ -154,20 +222,8 @@
    1.95  else:
    1.96      opts.DECAYS_MINBR = float(opts.DECAYS_MINBR)
    1.97  
    1.98 -## Output format wrangling
    1.99 -if "tikz" not in opts.FORMAT:
   1.100 -    opts.FORMAT = "tikz" + opts.FORMAT
   1.101 -if opts.FORMAT == "tikz":
   1.102 -    opts.FORMAT = "tikztex"
   1.103 -elif opts.FORMAT == "tikzfrag":
   1.104 -    opts.FORMAT = "tikztexfrag"
   1.105 -if "frag" in opts.FORMAT and any(f in opts.FORMAT for f in ["pdf", "eps", "png"]):
   1.106 -    logging.error("Oops! You can't currently use LaTeX fragment output together with graphics "
   1.107 -                  "formats, since the graphics can't be built from the incomplete LaTeX "
   1.108 -                  "file. We'll fix this, but for now you will have to run slhaplot twice: "
   1.109 -                  "once for the LaTeX fragment, and another time for the graphical output "
   1.110 -                  "formats. Exiting...")
   1.111 -    sys.exit(1)
   1.112 +## Output format handling: convert string arg to a more semantically queryable type
   1.113 +opts.FORMAT = OutputFormatSpec(opts.FORMAT)
   1.114  
   1.115  
   1.116  ## Check non-optional arguments
   1.117 @@ -184,7 +240,7 @@
   1.118  import subprocess
   1.119  
   1.120  ## Test for tikz package if rendering the LaTeX source
   1.121 -if not any(f in opts.FORMAT for f in ["pdf", "eps", "png"]):
   1.122 +if opts.FORMAT.need_tikz():
   1.123      p = subprocess.Popen(["which", "kpsewhich"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   1.124      rtn = p.wait()
   1.125      if rtn != 0:
   1.126 @@ -197,7 +253,7 @@
   1.127              sys.exit(3)
   1.128  
   1.129  ## Test for pdflatex if we need to make a PDF
   1.130 -if "pdf" in opts.FORMAT or "png" in opts.FORMAT:
   1.131 +if opts.FORMAT.need_pdflatex():
   1.132      p = subprocess.Popen(["which", "pdflatex"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   1.133      rtn = p.wait()
   1.134      if rtn != 0:
   1.135 @@ -205,7 +261,7 @@
   1.136          sys.exit(3)
   1.137  
   1.138      ## Test for convert if we need to make a bitmap format
   1.139 -    if "png" in opts.FORMAT:
   1.140 +    if opts.FORMAT.need_convert():
   1.141          p = subprocess.Popen(["which", "convert"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   1.142          rtn = p.wait()
   1.143          if rtn != 0:
   1.144 @@ -213,7 +269,7 @@
   1.145              sys.exit(3)
   1.146  
   1.147  ## Test for latex, dvips and ps2eps if making an EPS
   1.148 -if "eps" in opts.FORMAT:
   1.149 +if opts.FORMAT.need_epslatex():
   1.150      p = subprocess.Popen(["which", "latex"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   1.151      rtn = p.wait()
   1.152      if rtn != 0:
   1.153 @@ -246,8 +302,7 @@
   1.154          outname = o
   1.155  
   1.156      ## Info for the user
   1.157 -    # TODO: Print all output filenames explicitly?
   1.158 -    extlist = [f for f in ["tex", "pdf", "eps", "png"] if f in opts.FORMAT]
   1.159 +    extlist = opts.FORMAT.file_extensions()
   1.160      extstr = ",".join(extlist)
   1.161      if len(extlist) > 1:
   1.162          extstr = "{" + extstr + "}"
   1.163 @@ -437,11 +492,12 @@
   1.164      out = ""
   1.165  
   1.166      ## TIKZ FORMAT
   1.167 -    if "tikz" in opts.FORMAT:
   1.168 +    # TODO: Remove this test?
   1.169 +    if "tikz" in opts.FORMAT.format_string:
   1.170  
   1.171          ## Comment out the preamble etc. if only the TikZ fragment is wanted
   1.172          c = ""
   1.173 -        if opts.FORMAT == "tikztexfrag":
   1.174 +        if opts.FORMAT.make_texfrag():
   1.175              c = "%"
   1.176  
   1.177          ## Write LaTeX header
   1.178 @@ -563,10 +619,10 @@
   1.179  
   1.180  
   1.181          ## Write output
   1.182 -        if "tex" in opts.FORMAT:
   1.183 +        if opts.FORMAT.make_tex():
   1.184              writeout(out, outname+".tex")
   1.185  
   1.186 -        if any(f in opts.FORMAT for f in ["pdf", "eps", "png"]):
   1.187 +        if opts.FORMAT.need_compilation():
   1.188              ## Run LaTeX
   1.189              import tempfile, shutil, subprocess
   1.190              tmpdir = tempfile.mkdtemp()
   1.191 @@ -578,11 +634,11 @@
   1.192                                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   1.193  
   1.194              ## Processing via PDF
   1.195 -            if any(f in opts.FORMAT for f in ["pdf", "png"]):
   1.196 +            if opts.FORMAT.need_pdflatex():
   1.197                  try:
   1.198                      p = mktmpprocess(["pdflatex", r"\scrollmode\input", "mytmp.tex"])
   1.199                      p.wait()
   1.200 -                    if "pdf" in opts.FORMAT:
   1.201 +                    if opts.FORMAT.make_pdf():
   1.202                          shutil.copyfile(os.path.join(tmpdir, "mytmp.pdf"), outname+".pdf")
   1.203                  except Exception, e:
   1.204                      logging.error("pdflatex could not be run: PDF and/or PNG format mode cannot work")
   1.205 @@ -591,7 +647,7 @@
   1.206                      # TODO: Can we use a finally block to make sure that the tmpdir is cleared up, despite the 'sys.exit's?
   1.207  
   1.208                  ## Turning the PDF into a PNG if required
   1.209 -                if "png" in opts.FORMAT:
   1.210 +                if opts.FORMAT.make_png():
   1.211                      try:
   1.212                          p = mktmpprocess(["convert", "-density", "150", "-flatten", "mytmp.pdf", "mytmp.png"])
   1.213                          p.wait()
   1.214 @@ -602,7 +658,7 @@
   1.215                          sys.exit(3)
   1.216  
   1.217              ## Making a PS or EPS
   1.218 -            if any(f in opts.FORMAT for f in ["eps"]):
   1.219 +            if opts.FORMAT.make_eps():
   1.220                  try:
   1.221                      p = mktmpprocess(["latex", r"\scrollmode\input", "mytmp.tex"])
   1.222                      p.wait()

mercurial