diff --git a/lib/copic.py b/lib/copic.py index 397f249..6e9fd83 100644 --- a/lib/copic.py +++ b/lib/copic.py @@ -24,9 +24,6 @@ class Copic(Palette): # Identifier identifier = 'copic' - # 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 ', diff --git a/lib/dulux.py b/lib/dulux.py index 193f14c..23ac671 100644 --- a/lib/dulux.py +++ b/lib/dulux.py @@ -24,9 +24,6 @@ class Dulux(Palette): # Identifier identifier = 'dulux' - # 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 ', diff --git a/lib/pantone.py b/lib/pantone.py index 0d91ae4..bf3689a 100755 --- a/lib/pantone.py +++ b/lib/pantone.py @@ -27,9 +27,6 @@ class Pantone(Palette): # Identifier identifier = 'pantone' - # 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 ', diff --git a/lib/prismacolor.py b/lib/prismacolor.py index 8386a90..629e5f3 100644 --- a/lib/prismacolor.py +++ b/lib/prismacolor.py @@ -24,9 +24,6 @@ class Prismacolor(Palette): # Identifier identifier = 'prismacolor' - # 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 ', diff --git a/lib/ral.py b/lib/ral.py index d1999ed..e35e640 100755 --- a/lib/ral.py +++ b/lib/ral.py @@ -34,9 +34,6 @@ class RAL(Palette): # Identifier identifier = 'ral' - # 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 ', diff --git a/main.py b/main.py index 4afd99c..011f588 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ # IMPORTS # For more information, see https://www.python.org/dev/peps/pep-0008/#imports ## + import click from lib.pantone import Pantone @@ -63,7 +64,8 @@ def fetch(sets, fetch_all): @cli.command() @click.argument('sets', nargs=-1) -def process(sets): +@click.option('-f', '--format', type=click.Choice(['xml', 'gpl', 'acb', 'soc']), help='Only given format will be generated.') +def process(sets, format=''): """ ARGS: pantone | ral | dulux | copic | prismacolor @@ -76,7 +78,12 @@ def process(sets): for set in sets: if set in valid_sets: object = class_map[set]() - object.make_palettes() + + if format != '': + make_palette = getattr(object, 'make_' + format, None) + make_palette() + else: + object.make_palettes() else: print('"' + set + '" isn\'t available. Please provide a valid color space,\nsuch as "pantone", "ral", "dulux", "copic" & "prismacolor".') continue diff --git a/palette.py b/palette.py index 0e1d5eb..d78b576 100644 --- a/palette.py +++ b/palette.py @@ -15,8 +15,19 @@ class Palette: def __init__(self): + # Copyright notices + self.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', + } + + # Creating global JSON path + self.json_path = './palettes/' + self.identifier + '/json' os.makedirs(self.json_path, exist_ok=True) + # Globbing all JSON source files + self.json_files = glob.glob(self.json_path + '/*/*.json', recursive=True) + ## # Dumps fetched colors as JSON @@ -64,28 +75,26 @@ def create_json(self, input_filename=''): # 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', - } + self.make_xml() + self.make_gpl() + self.make_acb() + self.make_soc() - # Globbing all JSON source files - paths = glob.glob('./palettes/' + self.identifier + '/json/*/*.json', recursive=True) - for path in paths: + # Builds XML color palette (Scribus) + def make_xml(self): + for path in self.json_files: + output_path = os.path.dirname(path).replace('/json', '/xml') file_name = os.path.basename(path).replace('.json', '') + xml_file = output_path + '/' + file_name + '.xml' + + root = etree.Element('SCRIBUSCOLORS') + comment = etree.Comment(self.copyright.get('xml', self.default_copyright['xml'])) + root.insert(0, comment) 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'] @@ -98,33 +107,35 @@ def make_palettes(self): 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') + # Builds GPL color palette (GIMP + Inkscape) + def make_gpl(self): + for path in self.json_files: + output_path = os.path.dirname(path).replace('/json', '/gpl') + file_name = os.path.basename(path).replace('.json', '') + gpl_file = output_path + '/' + file_name + '.gpl' + + with open(path, 'r') as file: + data = json.load(file) # 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: + title = file_name.title() if self.identifier != 'pantone' else file_name.replace('colors', 'Colors') + file.write('GIMP Palette\n') file.write('Name: ' + title + '\n') - file.write(self.copyright.get('xml', default_copyright['xml'])) + file.write(self.copyright.get('xml', self.default_copyright['xml'])) file.write('\n') for color in data: @@ -140,11 +151,20 @@ def make_palettes(self): print('Generating %s .. done' % gpl_file) - ## - # Building ACB color palettes for AutoCAD - ## + # Builds ACB color palette (AutoCAD) + def make_acb(self): + for path in self.json_files: + output_path = os.path.dirname(path).replace('/json', '/acb') + file_name = os.path.basename(path).replace('.json', '') + acb_file = output_path + '/' + file_name + '.acb' + root = etree.Element('colorbook') + title = etree.SubElement(root, 'bookName') + title.text = file_name.title() if self.identifier != 'pantone' else file_name.replace('colors', 'Colors') + comment = etree.Comment(self.copyright.get('xml', self.default_copyright['xml'])) + root.insert(0, comment) + color_page = etree.SubElement(root, 'colorPage') page_color = etree.SubElement(color_page, 'pageColor') rgb8 = etree.SubElement(page_color, 'RGB8') @@ -153,13 +173,14 @@ def make_palettes(self): 'green': '100', 'blue': '100', } + for color, value in page_background.items(): entry = etree.SubElement(rgb8, color) entry.text = value - title.text = file_name.title() if self.identifier != 'pantone' else file_name.replace('colors', 'Colors') - comment = etree.Comment(self.copyright.get('xml', default_copyright['xml'])) - root.insert(0, comment) + with open(path, 'r') as file: + data = json.load(file) + for color in data: rgb = color['rgb'][4:-1].split(',') name = color['name'].title() if color['name'] != '' else color['code'] @@ -177,20 +198,22 @@ def make_palettes(self): entry.text = value # Creating directories for XML color palettes (if it doesn't exist already) - output_path = os.path.dirname(path).replace('/json', '/acb') os.makedirs(output_path, exist_ok=True) # Writing XML color palettes to disk (mirroring JSON source structure) - acb_file = output_path + '/' + file_name + '.acb' tree = etree.ElementTree(root) tree.write(acb_file, xml_declaration=True, encoding='UTF-8', pretty_print=True) print('Generating %s .. done' % acb_file) - ## - # Building SOC color palettes for Open/LibreOffice - ## + # Builds SOC color palette (OpenOffice + LibreOffice) + def make_soc(self): + for path in self.json_files: + output_path = os.path.dirname(path).replace('/json', '/soc') + file_name = os.path.basename(path).replace('.json', '') + soc_file = output_path + '/' + file_name + '.soc' + namespaces = { 'office': 'http://openoffice.org/2000/office', 'style': 'http://openoffice.org/2000/style', @@ -211,8 +234,12 @@ def make_palettes(self): 'config': 'http://openoffice.org/2001/config', } root = etree.Element('color-table', nsmap=namespaces) - comment = etree.Comment(self.copyright.get('xml', default_copyright['xml'])) + comment = etree.Comment(self.copyright.get('xml', self.default_copyright['xml'])) root.insert(0, comment) + + with open(path, 'r') as file: + data = json.load(file) + for color in data: name = color['name'].title() if color['name'] != '' else color['code'] entry = etree.SubElement(root, 'color') @@ -221,11 +248,9 @@ def make_palettes(self): entry.set('color', color['hex']) # Creating directories for XML color palettes (if it doesn't exist already) - output_path = os.path.dirname(path).replace('/json', '/soc') os.makedirs(output_path, exist_ok=True) # Writing XML color palettes to disk (mirroring JSON source structure) - soc_file = output_path + '/' + file_name + '.soc' tree = etree.ElementTree(root) tree.write(soc_file, xml_declaration=True, encoding='UTF-8', pretty_print=True)