| 1 |
|
| 2 |
|
| 3 |
import xml.etree.ElementTree as etree |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
import re |
| 7 |
from subprocess import call |
| 8 |
|
| 9 |
FG_ROOT="../" |
| 10 |
|
| 11 |
def tex_escape(text): |
| 12 |
""" |
| 13 |
:param text: a plain text message |
| 14 |
:return: the message escaped to appear correctly in LaTeX |
| 15 |
""" |
| 16 |
conv = { |
| 17 |
'&': r'\&', |
| 18 |
'%': r'\%', |
| 19 |
'$': r'\$', |
| 20 |
'#': r'\#', |
| 21 |
'_': r'\_', |
| 22 |
'{': r'\{', |
| 23 |
'}': r'\}', |
| 24 |
'~': r'\textasciitilde{}', |
| 25 |
'^': r'\^{}', |
| 26 |
'\\': r'\textbackslash{}', |
| 27 |
'<': r'\textless', |
| 28 |
'>': r'\textgreater', |
| 29 |
} |
| 30 |
regex = re.compile('|'.join(re.escape(unicode(key)) for key in sorted(conv.keys(), key = lambda item: - len(item)))) |
| 31 |
return regex.sub(lambda match: conv[match.group()], text) |
| 32 |
|
| 33 |
def generate_airplane_latex(source): |
| 34 |
""" |
| 35 |
Generates the LaTeX files and directory structure for the procedures |
| 36 |
""" |
| 37 |
root = os.path.join("procedures", source['name']) |
| 38 |
checklists_path = os.path.join(root, "checklists") |
| 39 |
if(not os.path.isdir(root)): |
| 40 |
os.mkdir(root) |
| 41 |
if(not os.path.isdir(checklists_path)): |
| 42 |
os.mkdir(checklists_path) |
| 43 |
|
| 44 |
|
| 45 |
preamble = open(os.path.join(root, "preamble.tex"), "w") |
| 46 |
|
| 47 |
preamble.write("\\section{Preamble}") |
| 48 |
preamble.write("This procedure list should be considered incomplete and it is not intended for real world use. It is automatically generated documentation intended for FlightGear flight simulator.") |
| 49 |
|
| 50 |
documentation_tex = open(os.path.join(root, source['dir'] + "_documentation.tex"), "w") |
| 51 |
documentation_tex.write("\\documentclass{article}\n") |
| 52 |
documentation_tex.write("\\usepackage{hyperref}\n\usepackage{graphicx}\n" |
| 53 |
"\\usepackage{fancyhdr}\\pagestyle{fancy}\n\\lfoot{Intended for FlightGear}\\rfoot{Not for real world use!}") |
| 54 |
documentation_tex.write("\\title{"+source['name']+" documentation}\n") |
| 55 |
documentation_tex.write("\\author{FlightGear team}\n") |
| 56 |
documentation_tex.write("\\renewcommand{\\familydefault}{\\sfdefault}") |
| 57 |
documentation_tex.write("\\begin{document}\n") |
| 58 |
documentation_tex.write("\\maketitle\n") |
| 59 |
|
| 60 |
checklist_tex = open(os.path.join(root, source['dir'] + "_checklist.tex"), "w") |
| 61 |
checklist_tex.write("\\documentclass{article}\n") |
| 62 |
checklist_tex.write("\\usepackage{fancyhdr}\\pagestyle{fancy}\n\\lfoot{Intended for FlightGear}\\rfoot{Not for real world use!}") |
| 63 |
checklist_tex.write("\\begin{document}\n") |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
if(source['thumbnail']): |
| 68 |
shutil.copyfile(FG_ROOT + "Aircraft/" + source['dir'] + "/thumbnail.jpg", |
| 69 |
os.path.join(root, "thumbnail.jpg")) |
| 70 |
documentation_tex.write("\\begin{figure}[h!]\\centering") |
| 71 |
documentation_tex.write("\\includegraphics[width=5cm,height=5cm,keepaspectratio]{thumbnail.jpg}") |
| 72 |
documentation_tex.write("\\end{figure}") |
| 73 |
documentation_tex.write("\\tableofcontents\n") |
| 74 |
documentation_tex.write("\\input{preamble.tex}\n") |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if source['extra_documentation']: |
| 79 |
extra_docs_dir = os.path.join(FG_ROOT + "Aircraft/" + source['dir'], "Docs") |
| 80 |
extra_docs_dest_dir = os.path.join("procedures/" + source['name'], "Docs") |
| 81 |
if os.path.isdir(extra_docs_dest_dir): |
| 82 |
shutil.rmtree(extra_docs_dest_dir) |
| 83 |
shutil.copytree(extra_docs_dir, extra_docs_dest_dir) |
| 84 |
documentation_tex.write("\\input{Docs/" + source['dir'] + "_documentation}\n") |
| 85 |
|
| 86 |
|
| 87 |
if(source['sources'] > 0): |
| 88 |
documentation_tex.write("\\input{checklists.tex}\n") |
| 89 |
checklists_tex = open(os.path.join(root, "checklists.tex"), "w") |
| 90 |
checklists_tex.write("\\section{Checklists}\n") |
| 91 |
|
| 92 |
|
| 93 |
for xmlfile in source['sources']: |
| 94 |
tree = etree.parse(xmlfile) |
| 95 |
chkl_root = tree.getroot() |
| 96 |
for checklist in chkl_root: |
| 97 |
if checklist.tag != "checklist": |
| 98 |
print "Unrecognised tag in", xmlfile |
| 99 |
continue |
| 100 |
title = checklist.find("title") |
| 101 |
if title is None: |
| 102 |
title = "Untitled" |
| 103 |
else: |
| 104 |
title = tex_escape(title.text) |
| 105 |
items = [] |
| 106 |
|
| 107 |
for item in checklist.findall("item"): |
| 108 |
name = item.find("name") |
| 109 |
value = item.find("value") |
| 110 |
if name is None or value is None: |
| 111 |
continue |
| 112 |
if name.text is None or value.text is None: |
| 113 |
continue |
| 114 |
items.append({ |
| 115 |
'name': tex_escape(name.text), |
| 116 |
'value': tex_escape(value.text) |
| 117 |
}) |
| 118 |
filename = title + ".tex" |
| 119 |
filename = filename.replace("/", "_") |
| 120 |
filename = filename.replace(" ", "_") |
| 121 |
checklists_tex.write("\\subsection{" + title + "}\n") |
| 122 |
checklist_tex.write("\\section*{" + title + "}\n") |
| 123 |
checklists_tex.write("\\input{checklists/"+filename+"}\n") |
| 124 |
checklist_tex.write("\\input{checklists/"+filename+"}\n") |
| 125 |
f = open(os.path.join(checklists_path,filename), "w") |
| 126 |
if len(items) > 0: |
| 127 |
f.write("\\begin{description}\n") |
| 128 |
for item in items: |
| 129 |
f.write("\\item["+item['name']+"] \dotfill " + item['value']+"\n") |
| 130 |
f.write("\\end{description}\n") |
| 131 |
checklist_tex.write("\\clearpage\n") |
| 132 |
|
| 133 |
documentation_tex.write("\\end{document}\n") |
| 134 |
checklist_tex.write("\\end{document}\n") |
| 135 |
|
| 136 |
def gather_aircraft_metadata(directory = FG_ROOT + "Aircraft"): |
| 137 |
aircrafts = [] |
| 138 |
for aircraft_directory in os.listdir(directory): |
| 139 |
for rootfile in os.listdir(os.path.join(directory, aircraft_directory)): |
| 140 |
rootfile = os.path.join(os.path.join(directory, aircraft_directory), rootfile) |
| 141 |
if not (os.path.isfile(rootfile) and rootfile.endswith("-set.xml")): |
| 142 |
continue |
| 143 |
|
| 144 |
tree = etree.parse(rootfile) |
| 145 |
name = aircraft_directory |
| 146 |
|
| 147 |
|
| 148 |
for descr in tree.iter('description'): |
| 149 |
name = descr.text |
| 150 |
break |
| 151 |
aircraft = { |
| 152 |
'sources' : [], |
| 153 |
'name' : name, |
| 154 |
'dir' : aircraft_directory, |
| 155 |
'thumbnail': False, |
| 156 |
'extra_documentation': False |
| 157 |
} |
| 158 |
|
| 159 |
aircraft['extra_documentation'] = os.path.isdir(os.path.join(directory, |
| 160 |
aircraft_directory |
| 161 |
+ "/Docs/")) & os.path.isfile(os.path.join(directory, aircraft_directory |
| 162 |
+ "/Docs/" |
| 163 |
+ aircraft_directory |
| 164 |
+ "_documentation.tex")) |
| 165 |
|
| 166 |
if os.path.isfile(os.path.join(directory, aircraft_directory + "/thumbnail.jpg")): |
| 167 |
aircraft['thumbnail'] = True |
| 168 |
|
| 169 |
for checklist in tree.iter('checklists'): |
| 170 |
aircraft['sources'].append(os.path.join(directory, aircraft_directory, checklist.attrib['include'])) |
| 171 |
if 'include' in tree.getroot().attrib: |
| 172 |
try: |
| 173 |
tree = etree.parse(os.path.join(directory, aircraft_directory) + "/" + tree.getroot().attrib['include']) |
| 174 |
for checklist in tree.iter('checklists'): |
| 175 |
if 'include' in checklist.attrib: |
| 176 |
aircraft['sources'].append(os.path.join(directory, aircraft_directory, checklist.attrib['include'])) |
| 177 |
except: |
| 178 |
pass |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if(len(aircraft['sources']) > 0 or aircraft['extra_documentation']): |
| 183 |
aircrafts.append(aircraft) |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
return aircrafts |
| 192 |
|
| 193 |
|
| 194 |
def compile_airplane_latex(source): |
| 195 |
""" |
| 196 |
Compile the procedures using pdflatex |
| 197 |
""" |
| 198 |
wd = os.getcwd() |
| 199 |
root = os.path.join("procedures", source['name']) |
| 200 |
os.chdir(root) |
| 201 |
with open(os.devnull, "w") as fnull: |
| 202 |
mainfile = source['dir'] + "_documentation.tex" |
| 203 |
call(["pdflatex", "-interaction", "nonstopmode", mainfile], stdout=fnull,stderr=fnull) |
| 204 |
call(["pdflatex", "-interaction", "nonstopmode", mainfile], stdout=fnull,stderr=fnull) |
| 205 |
mainfile = source['dir'] + "_checklist.tex" |
| 206 |
call(["pdflatex", "-interaction", "nonstopmode", mainfile], stdout=fnull,stderr=fnull) |
| 207 |
call(["pdflatex", "-interaction", "nonstopmode", mainfile], stdout=fnull,stderr=fnull) |
| 208 |
os.chdir(wd) |
| 209 |
|
| 210 |
|
| 211 |
def generate_airplane_documentation(): |
| 212 |
|
| 213 |
|
| 214 |
aircrafts = gather_aircraft_metadata("../Aircraft") |
| 215 |
if(len(aircrafts) == 0 ): |
| 216 |
print "No aircraft found; wrong directory?" |
| 217 |
if(not os.path.isdir("procedures")): |
| 218 |
os.mkdir("procedures") |
| 219 |
|
| 220 |
|
| 221 |
for aircraft in aircrafts: |
| 222 |
generate_airplane_latex(aircraft) |
| 223 |
compile_airplane_latex(aircraft) |
| 224 |
|
| 225 |
|
| 226 |
generate_airplane_documentation() |