From db08447b89ddccd3861d7255f1194d56291c1365 Mon Sep 17 00:00:00 2001 From: "M. Folkers" Date: Sun, 5 May 2019 01:15:00 +0200 Subject: [PATCH] Merging `process.py` workflow with existing `palette.py` parent class --- lib/copic.py | 6 +++ lib/dulux.py | 6 +++ lib/pantone.py | 8 +++- lib/prismacolor.py | 6 +++ lib/ral.py | 6 +++ main.py | 5 ++ palette.py | 84 +++++++++++++++++++++++++++++++++- process.py | 111 --------------------------------------------- 8 files changed, 119 insertions(+), 113 deletions(-) delete mode 100644 process.py diff --git a/lib/copic.py b/lib/copic.py index 7871a45..97873d4 100644 --- a/lib/copic.py +++ b/lib/copic.py @@ -27,6 +27,12 @@ class Copic(Palette): # Global JSON path json_path = './palettes/' + identifier + '/json' + # Copyright notices + copyright = { + 'xml': '\n Copic® and related trademarks are the property of\n Too Marker Corporation (https://www.toomarker.co.jp/en)\n ', + 'gpl': '##\n# Copic® and related trademarks are the property of\n# Too Marker Corporation (https://www.toomarker.co.jp/en)\n##\n', + } + def __init__(self): super().__init__() diff --git a/lib/dulux.py b/lib/dulux.py index 9ea2fac..17ec2ed 100644 --- a/lib/dulux.py +++ b/lib/dulux.py @@ -27,6 +27,12 @@ class Dulux(Palette): # Global JSON path json_path = './palettes/' + identifier + '/json' + # Copyright notices + copyright = { + 'xml': '\n Dulux® and related trademarks are the property of\n AkzoNobel N.V. (https://www.akzonobel.com) (joint-stock company) (worldwide) or\n DuluxGroup (https://www.dulux.com.au) (Australia & New Zealand) \n ', + 'gpl': '##\n# Dulux® and related trademarks are the property of\n# AkzoNobel N.V. (https://www.akzonobel.com) (joint-stock company) (worldwide) or\n# DuluxGroup (https://www.dulux.com.au) (Australia & New Zealand) \n##\n', + } + def __init__(self): super().__init__() diff --git a/lib/pantone.py b/lib/pantone.py index 12fc8b0..0aa0e71 100755 --- a/lib/pantone.py +++ b/lib/pantone.py @@ -30,6 +30,12 @@ class Pantone(Palette): # Global JSON path json_path = './palettes/' + identifier + '/json' + # Copyright notices + copyright = { + 'xml': '\n PANTONE® and related trademarks are the property of\n Pantone LLC (https://www.pantone.com), a division of X-Rite, a Danaher company\n ', + 'gpl': '##\n# PANTONE® and related trademarks are the property of\n# Pantone LLC (https://www.pantone.com), a division of X-Rite, a Danaher company\n##\n' + } + def __init__(self): super().__init__() @@ -325,4 +331,4 @@ def create_json(self, input_filename=''): with open(json_path, 'w') as file: file.write(json.dumps(colors, indent=4)) - print('%s has been created.' % json_path) + print('Generating %s .. done' % json_path) diff --git a/lib/prismacolor.py b/lib/prismacolor.py index f2d7871..6bd160a 100644 --- a/lib/prismacolor.py +++ b/lib/prismacolor.py @@ -27,6 +27,12 @@ class Prismacolor(Palette): # Global JSON path json_path = './palettes/' + identifier + '/json' + # Copyright notices + copyright = { + 'xml': '\n Prismacolor® and related trademarks are the property of\n Berol Corporation (http://www.berol.co.uk), owned by Sanford L.P. (http://www.sanfordb2b.com),\n a Newell Brands (https://www.newellbrands.com) company\n ', + 'gpl': '##\n# Prismacolor® and related trademarks are the property of\n# Berol Corporation (http://www.berol.co.uk), owned by Sanford L.P. (http://www.sanfordb2b.com),\n# a Newell Brands (https://www.newellbrands.com) company\n##\n', + } + def __init__(self): super().__init__() diff --git a/lib/ral.py b/lib/ral.py index ae23d66..a6fcd74 100755 --- a/lib/ral.py +++ b/lib/ral.py @@ -37,6 +37,12 @@ class Ral(Palette): # Global JSON path json_path = './palettes/' + identifier + '/json' + # Copyright notices + copyright = { + 'xml': '\n RAL® and related trademarks are the property of\n RAL gGmbH (https://www.ral-farben.de) (non-profit LLC) or\n RAL Deutsches Institut für Gütesicherung und Kennzeichnung e. V. (https://www.ral.de)\n ', + 'gpl': '##\n# RAL® and related trademarks are the property of\n# RAL gGmbH (https://www.ral-farben.de) (non-profit LLC) or\n# RAL Deutsches Institut für Gütesicherung und Kennzeichnung e. V. (https://www.ral.de)\n##\n', + } + def __init__(self): super().__init__() diff --git a/main.py b/main.py index 36cd1fc..2dfdbd6 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,7 @@ pantone.fetch('product-design', 1, 10) pantone.save() pantone.create_json() +pantone.make_palettes() ral = Ral() ral.fetch('classic') @@ -25,18 +26,22 @@ ral.fetch('plastics') ral.save() ral.create_json() +ral.make_palettes() dulux = Dulux() dulux.fetch('dulux') dulux.save() dulux.create_json() +dulux.make_palettes() copic = Copic() copic.fetch('copic') copic.save() copic.create_json() +copic.make_palettes() prismacolor = Prismacolor() prismacolor.fetch('premier') prismacolor.save() prismacolor.create_json() +prismacolor.make_palettes() diff --git a/palette.py b/palette.py index cbea2c5..4d12ffa 100644 --- a/palette.py +++ b/palette.py @@ -7,8 +7,11 @@ ## import os +import glob import json +from lxml import etree + class Palette: def __init__(self): @@ -54,4 +57,83 @@ def create_json(self, input_filename=''): with open(json_path, 'w') as file: file.write(json.dumps(colors, indent=4)) - print('%s has been created.' % json_path) + print('Generating %s .. done' % json_path) + + + ## + # Makes color palettes in various formats + ## + def make_palettes(self): + # Copyright notices + default_copyright = { + 'xml': '\n For copyright and other legal information,\n please refer to "README.md" in the root of this project\n ', + 'gpl': '##\n# For copyright and other legal information, please refer to "README.md" in the root of this project\n##\n', + } + + # Globbing all JSON source files + paths = glob.glob('./palettes/' + self.identifier + '/json/*/*.json', recursive=True) + + for path in paths: + file_name = os.path.basename(path).replace('.json', '') + + with open(path, 'r') as file: + data = json.load(file) + + + ## + # Building XML color palettes for Scribus + ## + root = etree.Element('SCRIBUSCOLORS') + comment = etree.Comment(self.copyright.get('xml', default_copyright['xml'])) + root.insert(0, comment) + for color in data: + rgb = color['rgb'][4:-1].split(',') + name = color['name'].title() if color['name'] != '' else color['code'] + entry = etree.SubElement(root, 'COLOR') + entry.set('NAME', name) + entry.set('SPACE', 'RGB') + entry.set('R', rgb[0]) + entry.set('G', rgb[1]) + entry.set('B', rgb[2]) + + # Creating directories for XML color palettes (if it doesn't exist already) + output_path = os.path.dirname(path).replace('/json', '/xml') + os.makedirs(output_path, exist_ok=True) + + # Writing XML color palettes to disk (mirroring JSON source structure) + xml_file = output_path + '/' + file_name + '.xml' + tree = etree.ElementTree(root) + tree.write(xml_file, xml_declaration=True, encoding='UTF-8', pretty_print=True) + + print('Generating %s .. done' % xml_file) + + + ## + # Building GPL color palettes for GIMP/Inkscape + ## + title = file_name.title() if self.identifier != 'pantone' else file_name.replace('colors', 'Colors') + + # Creating directories for GPL color palettes (if it doesn't exist already) + output_path = os.path.dirname(path).replace('/json', '/gpl') + os.makedirs(output_path, exist_ok=True) + + # Writing GPL color palettes to disk (mirroring JSON source structure) + gpl_file = output_path + '/' + file_name + '.gpl' + + with open(gpl_file, 'w') as file: + file.write('GIMP Palette\n') + file.write('Name: ' + title + '\n') + file.write(self.copyright.get('xml', default_copyright['xml'])) + file.write('\n') + + for color in data: + name = color['name'].title() if color['name'] != '' else color['code'] + line = color['rgb'][4:-1].split(',') + + for i in range(len(line)): + line[i] = '{:0>3}'.format(line[i]) + + line.append(name) + file.write(' '.join(line) + '\n') + + print('Generating %s .. done' % gpl_file) diff --git a/process.py b/process.py deleted file mode 100644 index 58d1d10..0000000 --- a/process.py +++ /dev/null @@ -1,111 +0,0 @@ -#!/usr/bin/env python3 - -from lxml import etree -import json, glob, os - - -## -# Globbing all JSON source files -## -paths = glob.glob('./*/json/**/*.json', recursive=True) - - -## -# Copyright notices -## -copyright = { - # Default notice - 'default_xml': '\n For copyright and other legal information,\n please refer to "README.md" in the root of this project\n ', - 'default_gpl': '##\n# For copyright and other legal information, please refer to "README.md" in the root of this project\n##\n', - - # Owner's notice - 'pantone': { - 'xml': '\n PANTONE® and related trademarks are the property of\n Pantone LLC (https://www.pantone.com), a division of X-Rite, a Danaher company\n ', - 'gpl': '##\n# PANTONE® and related trademarks are the property of\n# Pantone LLC (https://www.pantone.com), a division of X-Rite, a Danaher company\n##\n' - }, - 'ral': { - 'xml': '\n RAL® and related trademarks are the property of\n RAL gGmbH (https://www.ral-farben.de) (non-profit LLC) or\n RAL Deutsches Institut für Gütesicherung und Kennzeichnung e. V. (https://www.ral.de)\n ', - 'gpl': '##\n# RAL® and related trademarks are the property of\n# RAL gGmbH (https://www.ral-farben.de) (non-profit LLC) or\n# RAL Deutsches Institut für Gütesicherung und Kennzeichnung e. V. (https://www.ral.de)\n##\n', - }, - 'dulux': { - 'xml': '\n Dulux® and related trademarks are the property of\n AkzoNobel N.V. (https://www.akzonobel.com) (joint-stock company) (worldwide) or\n DuluxGroup (https://www.dulux.com.au) (Australia & New Zealand) \n ', - 'gpl': '##\n# Dulux® and related trademarks are the property of\n# AkzoNobel N.V. (https://www.akzonobel.com) (joint-stock company) (worldwide) or\n# DuluxGroup (https://www.dulux.com.au) (Australia & New Zealand) \n##\n', - }, - 'copic': { - 'xml': '\n Copic® and related trademarks are the property of\n Too Marker Corporation (https://www.toomarker.co.jp/en)\n ', - 'gpl': '##\n# Copic® and related trademarks are the property of\n# Too Marker Corporation (https://www.toomarker.co.jp/en)\n##\n', - }, - 'prismacolor': { - 'xml': '\n Prismacolor® and related trademarks are the property of\n Berol Corporation (http://www.berol.co.uk), owned by Sanford L.P. (http://www.sanfordb2b.com),\n a Newell Brands (https://www.newellbrands.com) company\n ', - 'gpl': '##\n# Prismacolor® and related trademarks are the property of\n# Berol Corporation (http://www.berol.co.uk), owned by Sanford L.P. (http://www.sanfordb2b.com),\n# a Newell Brands (https://www.newellbrands.com) company\n##\n', - }, -} - - -## -# Processing source files -## -for path in paths: - # ./path/to/file.json >> path - identifier = path.split('/')[1] - - # ./path/to/file.json >> file - file_name = os.path.basename(path).replace('.json', '') - - # Loading PANTONE® colors from each JSON file as object - with open(path, 'r') as file: - data = json.load(file) - - - ## - # Building XML color palettes for Scribus - ## - root = etree.Element('SCRIBUSCOLORS') - comment = etree.Comment(copyright[identifier].get('xml', copyright['default_xml'])) - root.insert(0, comment) - for color in data: - rgb = color['rgb'][4:-1].split(',') - name = color['name'].title() if color['name'] != '' else color['code'] - entry = etree.SubElement(root, 'COLOR') - entry.set('NAME', name) - entry.set('SPACE', 'RGB') - entry.set('R', rgb[0]) - entry.set('G', rgb[1]) - entry.set('B', rgb[2]) - - # Creating directories for XML color palettes (if it doesn't exist already) - output_path = os.path.dirname(path).replace('/json', '/xml') - os.makedirs(output_path, exist_ok=True) - - # Writing XML color palettes to disk (mirroring JSON source structure) - tree = etree.ElementTree(root) - tree.write(output_path + '/' + file_name + '.xml', xml_declaration=True, encoding='UTF-8', pretty_print=True) - print('%s.xml has been created.' % file_name) - - - ## - # Building GPL color palettes for GIMP/Inkscape - ## - title = file_name.title() if identifier != 'pantone' else file_name.replace('colors', 'Colors') - - # Creating directories for GPL color palettes (if it doesn't exist already) - output_path = os.path.dirname(path).replace('/json', '/gpl') - os.makedirs(output_path, exist_ok=True) - - # Writing GPL color palettes to disk (mirroring JSON source structure) - with open(output_path + '/' + file_name + '.gpl','w') as file: - file.write('GIMP Palette\n') - file.write('Name: ' + title + '\n') - file.write(copyright[identifier].get('gpl', copyright['default_gpl'])) - file.write('\n') - - for color in data: - name = color['name'].title() if color['name'] != '' else color['code'] - line = color['rgb'][4:-1].split(',') - - for i in range(len(line)): - line[i] = '{:0>3}'.format(line[i]) - - line.append(name) - file.write(' '.join(line) + '\n') - print('%s.gpl has been created.' % file_name)