|
1 | | -import datetime, pytz, os |
| 1 | +import datetime, pytz, os, subprocess |
2 | 2 | from jinja2 import Environment, FileSystemLoader, select_autoescape |
3 | 3 |
|
4 | 4 | from starlette.responses import RedirectResponse |
5 | 5 |
|
6 | 6 | env = Environment( |
7 | | - loader= FileSystemLoader('./posterTemplates'), |
8 | | - autoescape=select_autoescape(['svg']) |
| 7 | + loader= FileSystemLoader('./posterTemplates'), |
| 8 | + autoescape=select_autoescape(['svg']) |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | class PosterGenerator: |
12 | 12 | def __init__(self,data): |
| 13 | + self.supported_formats = ['svg', 'pdf', 'png'] |
| 14 | + |
| 15 | + self.data = data |
13 | 16 | for key in data: |
14 | 17 | setattr(self,key,data[key]) |
| 18 | + |
15 | 19 | if self.current_event: |
16 | 20 | self.start = datetime.datetime.fromtimestamp(data['current_event']['starts_at']).astimezone(pytz.timezone(self.timezone)) |
17 | 21 | self.month = self.start.strftime('%B') |
18 | 22 | self.short_month = self.start.strftime('%b') |
19 | 23 | self.day = self.start.day |
20 | 24 | self.year = self.start.strftime('%Y') |
21 | | - self.data = data |
| 25 | + self.url = self.webname |
| 26 | + |
| 27 | + def get_cache(self, file_format, template_name='', full=True): |
| 28 | + basedir = os.path.dirname(os.path.realpath(__file__)) if full else '' |
| 29 | + return '{}/generated/{}/{}/{}'.format(basedir, self.id, file_format, template_name.replace('svg', file_format)) |
22 | 30 |
|
23 | | - def make_poster(self, template, file_format): |
24 | | - supported_files = ['svg','pdf'] |
| 31 | + def make_poster(self, template_name, file_format): |
25 | 32 | file_format = file_format.lower() |
26 | | - if file_format not in supported_files: |
27 | | - return('ERROR: File provided not supported format') |
28 | | - if template not in env.list_templates(): |
29 | | - return({ |
30 | | - "requested_template": template, |
31 | | - "template_list": env.list_templates(), |
32 | | - "ERROR": 'Template provided not loaded in environment' |
33 | | - }) |
| 33 | + if file_format not in self.supported_formats: |
| 34 | + return('ERROR: File provided not supported format') |
| 35 | + |
| 36 | + if template_name not in env.list_templates(): |
| 37 | + return({ |
| 38 | + "requested_template": template_name, |
| 39 | + "template_list": env.list_templates(), |
| 40 | + "ERROR": 'Template provided not loaded in environment' |
| 41 | + }) |
| 42 | + |
| 43 | + if not hasattr(self, 'make_poster_{}'.format(file_format)): |
| 44 | + return('ERROR: no make_poster method for format') |
| 45 | + |
34 | 46 | if not self.current_event: |
35 | | - return('ERROR: Event not live in Clear') |
36 | | - file = template |
37 | | - template = env.get_template(file) |
38 | | - id = self.id |
39 | | - if not os.path.exists('generated/'): |
40 | | - os.mkdir('generated/') |
41 | | - if not os.path.exists('generated/{}/'.format(id)): |
42 | | - os.mkdir('generated/{}/'.format(id)) |
43 | | - if not os.path.exists('generated/{}/svg/'.format(id)): |
44 | | - os.mkdir('generated/{}/svg/'.format(id)) |
45 | | - if not os.path.exists('generated/{}/pdf/'.format(id)): |
46 | | - os.mkdir('generated/{}/pdf/'.format(id)) |
47 | | - with open("generated/{}/svg/{}".format(id, file), "w+") as f: |
48 | | - f.write(template.render(**vars(self))) |
49 | | - # if file_format == 'pdf': |
50 | | - # make_pdf('generated/{}/svg/{}'.format(id, file), |
51 | | - # 'generated/{}/pdf/{}'.format(id, file.replace('.svg', '.pdf'))) |
52 | | - # return 'generated/{}/pdf/{}'.format(id, file.replace('.svg', '.pdf')) |
53 | | - return RedirectResponse(url='/generated/{}/svg/{}'.format(id, file)) |
| 47 | + return('ERROR: Event not live in Clear') |
| 48 | + |
| 49 | + if not os.path.isfile(self.get_cache(file_format, template_name)): |
| 50 | + os.makedirs(self.get_cache(file_format), exist_ok=True) |
| 51 | + getattr(self, 'make_poster_{}'.format(file_format))(template_name) |
| 52 | + |
| 53 | + return RedirectResponse(url=self.get_cache(file_format, template_name, full=False)) |
| 54 | + |
| 55 | + def require_format(self, template_name, file_format): |
| 56 | + if not os.path.isfile(self.get_cache(file_format, template_name)): |
| 57 | + self.make_poster(template_name, file_format) |
| 58 | + |
| 59 | + def make_poster_svg(self, template_name): |
| 60 | + template = env.get_template(template_name) |
| 61 | + |
| 62 | + with open(self.get_cache('svg', template_name), "w+") as f: |
| 63 | + f.write(template.render(**vars(self))) |
| 64 | + |
| 65 | + def make_poster_pdf(self, template_name): |
| 66 | + self.require_format(template_name, 'svg') |
| 67 | + f_in = self.get_cache('svg', template_name) |
| 68 | + f_out = self.get_cache('pdf', template_name) |
| 69 | + |
| 70 | + with open(os.devnull, 'wb') as devnull: |
| 71 | + subprocess.check_call(['inkscape', '-z', '-f', f_in, '-A', f_out], stdout=devnull, stderr=subprocess.STDOUT) |
| 72 | + |
| 73 | + def make_poster_png(self, template_name): |
| 74 | + self.require_format(template_name, 'svg') |
| 75 | + f_in = self.get_cache('svg', template_name) |
| 76 | + f_out = self.get_cache('png', template_name) |
| 77 | + |
| 78 | + with open(os.devnull, 'wb') as devnull: |
| 79 | + subprocess.check_call(['inkscape', '-z', '-w', '600', '-f', f_in, '-e', f_out], stdout=devnull, stderr=subprocess.STDOUT) |
54 | 80 |
|
55 | 81 | def make_posters(self,templates=env.list_templates()): |
56 | | - for template in templates: |
57 | | - self.make_poster(template, 'svg') # pdf so it makes them both |
58 | | - |
59 | | - # def make_pdf(input,output): |
60 | | - # if os.path.isfile(input): |
61 | | - # with open(os.devnull, 'wb') as devnull: |
62 | | - # subprocess.check_call(['inkscape {} --export-pdf={}'.format(input, output)], stdout=devnull, stderr=subprocess.STDOUT) |
63 | | - # else: |
64 | | - # print('Input file does not exist') |
65 | | - # return |
| 82 | + for template_name in templates: |
| 83 | + for file_format in self.supported_formats: |
| 84 | + self.make_poster(template_name, file_format) |
0 commit comments