publik-base-theme/src/render-imgs-dashboard.py

117 lines
3.7 KiB
Python

#! /usr/bin/env python3
# publik-base-theme
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import subprocess
import os
import re
import xml.etree.ElementTree as ET
import sys
import tempfile
inkscape = os.path.abspath(os.path.join(os.path.dirname(__file__), 'inkscape_wrapper.py'))
names = {
'12': 'profile',
'13': 'documents',
'14': 'family',
'15': 'requests',
'16': 'links',
'17': 'billings',
}
filenames = [
'EO_CONNECTVILLE_PUBLIK_PICTO_12.svg',
'EO_CONNECTVILLE_PUBLIK_PICTO_13.svg',
'EO_CONNECTVILLE_PUBLIK_PICTO_14.svg',
'EO_CONNECTVILLE_PUBLIK_PICTO_15.svg',
'EO_CONNECTVILLE_PUBLIK_PICTO_16.svg',
'EO_CONNECTVILLE_PUBLIK_PICTO_17.svg',
]
parser = argparse.ArgumentParser()
parser.add_argument('path', help='out path')
parser.add_argument('--normal', default='4d4d4d')
parser.add_argument('--normal-height')
parser.add_argument('--normal-width')
parser.add_argument('--selected')
parser.add_argument('--selected-height')
parser.add_argument('--selected-width')
parser.add_argument('--title')
parser.add_argument('--title-height')
parser.add_argument('--title-width')
args = parser.parse_args()
path_out = args.path
variants = {}
for variant in ('normal', 'selected', 'title'):
if not getattr(args, variant):
continue
variant_code = variant
if variant_code == 'normal':
variant_code = ''
variants[variant_code] = {
'colour': getattr(args, variant),
'height': getattr(args, variant + '_height'),
'width': getattr(args, variant + '_width'),
}
if not os.path.exists(path_out):
os.makedirs(path_out)
for filename in filenames:
icon_name = names.get(re.search(r'\d+', filename).group())
for variant_name, variant_data in variants.items():
variant_colour = variant_data.get('colour')
variant_height = variant_data.get('height')
variant_width = variant_data.get('width')
tree = ET.parse(open(os.path.join('pictos', filename)))
if variant_name:
out_filepath = os.path.join(path_out, '%s-%s.png' % (icon_name, variant_name))
else:
out_filepath = os.path.join(path_out, '%s.png' % icon_name)
for node in tree.iter():
tag_name = node.tag.split('}')[-1]
if tag_name == 'path':
node.attrib['fill'] = '#%s' % variant_colour
node.attrib['stroke'] = 'none'
if tag_name in ('circle', 'rect'):
node.attrib['fill'] = 'none'
node.attrib['stroke'] = '#%s' % variant_colour
with tempfile.NamedTemporaryFile(suffix='.svg') as tmpfile:
tree.write(tmpfile.name)
tmpfile.flush()
cmd = [
inkscape,
'--without-gui',
'--file',
tmpfile.name,
'--export-area-drawing',
'--export-png',
out_filepath,
]
if variant_width:
cmd.extend(['--export-width', variant_width])
else:
cmd.extend(['--export-height', variant_height or '60'])
subprocess.call(cmd)