Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions gurpsspace/output/latexout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
processing towards a nicely formatted PDF file.
"""

import subprocess
import os

# from ..starsystem import StarSystem
from ..tables import AtmCompAbbr

Expand All @@ -13,6 +16,36 @@ def __init__(self, starsystem, filename='starsystem.tex'):
self.starsystem = starsystem
self.filename = filename

def make_pdf(self) -> str:
self.write()
base_name = os.path.splitext(self.filename)[0]
pdf_name = base_name + ".pdf"

cmd = ['pdflatex', '-interaction', 'nonstopmode', self.filename]
proc = subprocess.Popen(cmd)
proc.communicate()

retcode = proc.returncode
if not retcode == 0:
os.unlink(pdf_name)
raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd)))
# PDFLaTeX has to be run twice to properly generate the ToC
cmd = ['pdflatex', '-interaction', 'nonstopmode', self.filename]
proc = subprocess.Popen(cmd)
proc.communicate()

retcode = proc.returncode
if not retcode == 0:
os.unlink(pdf_name)
raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd)))

os.unlink(base_name + '.log')
os.unlink(base_name + '.aux')
os.unlink(base_name + '.toc')
os.unlink(base_name + '.out')

return os.path.abspath(pdf_name)

def write(self):
# Open the file
file = open(self.filename, 'w')
Expand Down
9 changes: 9 additions & 0 deletions gurpsspace/starsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@ def write_latex(self, filename='starsystem.tex') -> None:
writer = LW(self, filename)
writer.write()

def make_pdf(self, filename='starsystem.tex'):
"""
Create a PDF document with all the information about the starsystem.
:return: Path to the PDF.
"""

writer = LW(self, filename)
return writer.make_pdf()

def get_age(self) -> int:
"""Return star system age in billion years"""
return self.age
Expand Down
57 changes: 42 additions & 15 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from gurpsspace import starsystem as starsys
from namegenerator import namegenerator
from cherrypy.lib.static import serve_file

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('webgui/templates'))
Expand Down Expand Up @@ -68,6 +69,22 @@ def starsystem(self, must_have_garden="False", open_cluster=None, num_stars=0, a
else:
mysys = starsys.StarSystem(**arguments)

for star in mysys.stars:
for key, v in star.planetsystem.get_orbitcontents().items():
if namegen is not None:
simple_name = v.get_name().replace("-", "")
if cherrypy.session.get('name_of_' + simple_name) is None:
name = namegen.get_random_name()
star.planetsystem.get_orbitcontents()[key].set_name(name)
# For some reason, using simple_name here leads to storing stuff improperly and
# generating new names every time. No idea why.
cherrypy.session['name_of_' + v.get_name().replace("-", "")] = name
else:
name = cherrypy.session.get('name_of_' + simple_name)
star.planetsystem.get_orbitcontents()[key].set_name(name)

cherrypy.session.save()

tmpl = env.get_template('overview.html')
cherrypy.session['starsystem'] = mysys
cherrypy.response.cookie['names'] = {}
Expand All @@ -83,34 +100,20 @@ def planetsystem(self, star_id=""):
else:
star_id = int(star_id)

namegen = cherrypy.session.get('namegen')

tmpl = env.get_template('planetsystem.html')
env.globals['translate_row'] = self.translate_row

t_count = 0
a_count = 0
g_count = 0
for key, v in starsystem.stars[star_id].planetsystem.get_orbitcontents().items():
for key, _ in starsystem.stars[star_id].planetsystem.get_orbitcontents().items():
if starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].type() == 'Terrestrial':
t_count += 1
if starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].type() == 'Ast. Belt':
a_count += 1
if starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].type() == 'Gas Giant':
g_count += 1
if namegen is not None:
simple_name = v.get_name().replace("-", "")
if cherrypy.session.get('name_of_' + simple_name) is None:
name = namegen.get_random_name()
starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].set_name(name)
# For some reason, using simple_name here leads to storing stuff improperly and
# generating new names every time. No idea why.
cherrypy.session['name_of_' + v.get_name().replace("-", "")] = name
else:
name = cherrypy.session.get('name_of_' + simple_name)
starsystem.stars[star_id].planetsystem.get_orbitcontents()[key].set_name(name)

cherrypy.session.save()
cherrypy.session['planetsystem'] = starsystem.stars[star_id].planetsystem
return tmpl.render(planetsystem=starsystem.stars[star_id].planetsystem, terrestrial_count=t_count, asteroid_count=a_count, gas_giant_count=g_count)

Expand Down Expand Up @@ -140,6 +143,30 @@ def satellites(self, planet_id=""):
tmpl = env.get_template('moons.html')
return tmpl.render(moons=moons, planet_name=planet.get_name())

@cherrypy.expose
def printable(self):
try:
starsystem = cherrypy.session['starsystem']
except KeyError:
raise cherrypy.HTTPError(404)

t_count = 0
a_count = 0
g_count = 0
for star in starsystem.stars:
for key, _ in star.planetsystem.get_orbitcontents().items():
if star.planetsystem.get_orbitcontents()[key].type() == 'Terrestrial':
t_count += 1
if star.planetsystem.get_orbitcontents()[key].type() == 'Ast. Belt':
a_count += 1
if star.planetsystem.get_orbitcontents()[key].type() == 'Gas Giant':
g_count += 1

tmpl = env.get_template('printable.html')
env.globals['translate_row'] = self.translate_row

return tmpl.render(starsystem=starsystem, seed=self.random_seed, terrestrial_count=t_count, asteroid_count=a_count, gas_giant_count=g_count)

def translate_row(self, planet, row):
"""
It is difficult in HTML and Jinja to make a table where each column is a single item, rather than each row.
Expand Down
1 change: 1 addition & 0 deletions webgui/templates/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h1>Star System Generator for GURPS 4<sup>th</sup> Edition</h1>
<p><a href="">Click here if you want to change the settings.</a></p>
</aside>
<p>Seed: {{seed}}</p>
<p>Printable view <a href="printable">here</a>.</p>
<p class="success">You have successfully generated the following star system:</p>
<h2>Star system properties</h2>

Expand Down
Loading