Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import glob
  5. import os
  6. import re
  7. import string
  8. import sys
  9.  
  10.  
  11. # Pattern for parsing variables in filenames
  12. STR_SPLIT_PERIOD = re.compile(r'''((?:[^."']|"[^"]*"|'[^']*')+)''')
  13. STR_SPLIT_COMMA = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
  14.  
  15. # Static lists for gnuplot parsing
  16. GPI_OUTPUT_STR = ["o", "ou", "out", "outp", "outpu", "output"]
  17. GPI_PLOT_STR = ["p", "pl", "plo", "plot"]
  18.  
  19. rel_dir = sys.argv[1]
  20.  
  21. # Substitute variables in [possible] filename strings
  22. def substitute (var_dict, s):
  23. parts = STR_SPLIT_PERIOD.split(s)[1::2]
  24. result = ""
  25.  
  26. for p in parts:
  27. if p[0] == '"' or p[0] == "'":
  28. result += p.strip("\"'")
  29. else:
  30. try:
  31. result += var_dict[p]
  32. except KeyError:
  33. return ""
  34.  
  35. return result
  36.  
  37. # Parse gnuplot files for input and ouput files and print Tup rules
  38. def gnuplot_rules (fnames):
  39. plt_num = 0
  40. for fname in fnames:
  41. f = open(fname)
  42.  
  43. output_files = []
  44. input_files = []
  45. variables = {}
  46.  
  47. while True:
  48. l = f.readline()
  49. if len(l) == 0:
  50. # EOF
  51. break
  52.  
  53. l = l.strip()
  54. if len(l) == 0:
  55. # Blank line
  56. continue
  57.  
  58. if l[0] == "#":
  59. # Skip any commented lines
  60. continue
  61.  
  62. cmd = l.split()
  63.  
  64. if len(cmd) > 1 and cmd[0] == "set" and cmd[1] in GPI_OUTPUT_STR:
  65. # This line contains an output filename
  66. # Need to save it to the output list
  67. outp = l.split()
  68. filename = substitute(variables, outp[2])
  69. if filename not in output_files:
  70. output_files.append(filename)
  71. continue
  72.  
  73. if len(cmd) > 0 and cmd[0] in GPI_PLOT_STR:
  74.  
  75. # Remove the "plot" keyword
  76. try:
  77. pline = l.split(None, 1)[1]
  78. except IndexError:
  79. # This was a blank plot command so we should just skip it
  80. continue
  81.  
  82. # Concantenate all of the lines of the plot command
  83. pline_full = ""
  84. while True:
  85. pline = pline.strip(" \n")
  86. if len(pline) == 0:
  87. break
  88. elif pline[-1] == "\\":
  89. pline_full += pline[0:-1]
  90. pline = f.readline()
  91. else:
  92. pline_full += pline
  93. break
  94.  
  95. # Separate all of the possible plot commands by commas
  96. plots = STR_SPLIT_COMMA.split(pline_full)[1::2]
  97.  
  98. # Iterate over all of the lines in the plot
  99. for p in plots:
  100. # Get all the arguments to this particular line
  101. pargs = p.split()
  102.  
  103. if len(pargs) == 0:
  104. # Empty line in the plot command. Skip
  105. continue
  106.  
  107. # Take the first one (which is the filename), substitute any
  108. # variables, and then append it to our list of input files
  109. filename = substitute(variables, pargs[0])
  110. # Make sure the filename is actually a filename
  111. # Could be trying to plot a function.
  112. # Use the following heuristic:
  113. # - all filenames have at least one .
  114. # - all filenames have a letter following the last .
  115. if '.' in filename:
  116. chunks = filename.split('.')
  117. if chunks[-1][0].lower() in string.ascii_lowercase:
  118. # Make sure we haven't already added it
  119. if filename not in input_files:
  120. input_files.append(filename)
  121.  
  122. continue
  123.  
  124. # Check for a variable declaration
  125. # This line isn't completely correct, as an '=' inside a string
  126. # will trigger this, but (I think) leaving it this way for now won't
  127. # cause any problems.
  128. if "=" in l:
  129. var = l.split("=")
  130. variables[var[0].strip()] = var[1].strip("\" \n")
  131. continue
  132.  
  133. inputs = " ".join(input_files)
  134. outputs = " ".join(output_files)
  135.  
  136. print(": foreach {fname} | {inputs} |> gnuplot %f |> {outputs} {rel_dir}/<figspsgroup> {{epss{plt_num}}}".format(fname=fname,
  137. inputs=inputs,
  138. outputs=outputs,
  139. plt_num=plt_num,
  140. rel_dir=rel_dir))
  141. print(": foreach {{epss{plt_num}}} |> ^ CAPCONVERT %f ^ a2ping $(A2PING_OPTS) %f -o %o |> %B.pdf {rel_dir}/<figsgroup>".format(plt_num=plt_num,
  142. rel_dir=rel_dir))
  143.  
  144. plt_num += 1
  145.  
  146.  
  147.  
  148. # Get all gnuplot files
  149. gpi = glob.glob("*.plt") + glob.glob("*.gpi") + glob.glob("*.gnuplot")
  150. gnuplot_rules(gpi)
  151.  
  152. # Could add other figure generators here...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement