Skip to content

Commit

Permalink
Splitting process commands
Browse files Browse the repository at this point in the history
This fixes #5. Also, this commit moves `json_path` into Palette()
  • Loading branch information
S1SYPHOS committed May 6, 2019
1 parent cc4237c commit 9c79cd4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 56 deletions.
3 changes: 0 additions & 3 deletions lib/copic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ',
Expand Down
3 changes: 0 additions & 3 deletions lib/dulux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ',
Expand Down
3 changes: 0 additions & 3 deletions lib/pantone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ',
Expand Down
3 changes: 0 additions & 3 deletions lib/prismacolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ',
Expand Down
3 changes: 0 additions & 3 deletions lib/ral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ',
Expand Down
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
103 changes: 64 additions & 39 deletions palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand All @@ -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:
Expand All @@ -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')
Expand All @@ -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']
Expand All @@ -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',
Expand All @@ -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')
Expand All @@ -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)

Expand Down

0 comments on commit 9c79cd4

Please sign in to comment.