From d89b01ddd7a47d5133331ea1909b79d20011bdba Mon Sep 17 00:00:00 2001 From: "M. Folkers" Date: Mon, 6 May 2019 01:00:06 +0200 Subject: [PATCH] Making use of CLI arguments --- README.md | 32 ++++++++++++-- lib/copic.py | 2 +- lib/dulux.py | 2 +- lib/pantone.py | 9 ++++ lib/prismacolor.py | 2 +- lib/ral.py | 12 ++++- main.py | 108 ++++++++++++++++++++++++++++++--------------- 7 files changed, 124 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 1d1047a..bba581c 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,35 @@ This library provides an easy way to generate [*color palettes*](https://www.ety .. featuring [PANTONE®](https://www.pantone.com), [RAL®](https://www.ral-farben.de), [Dulux®](https://www.dulux.com.au) as well as [Copic®](https://www.copicmarker.com) and [Prismacolor®](https://www.prismacolor.com) (proprietary color spaces). For now, `we-love-colors` creates master palettes for use in [Scribus](https://www.scribus.net), an open source desktop publishing program, as well as [GIMP](https://www.gimp.org) and [Inkscape](https://inkscape.org). ## Getting started -.. was never easier. Make sure Python3 is intalled on your system, then simply do: +Depending on your setup you might prefer a .. + +### Local installation via `virtualenv` +Running `setup.sh` will install all dependencies inside a virtual environment, ready for action. + +### Global installation via `requirements.txt` +It's as easy as `pip install -r requirements.txt`, but you might want to make sure that Python v3 is installed on your system. + +## Usage +Fetching color sets and processing them is really straightforward - for everything else, there's `--help`: ```bash -python3 fetch.py # or any other `fetch*` script -python3 process.py +$ python main.py +Usage: main.py [OPTIONS] COMMAND [ARGS]... + +Options: + -v, --version Show the version and exit. + -h, --help Show this message and exit. + +Commands: + fetch ARGS: pantone | ral | dulux | copic | prismacolor + process ARGS: pantone | ral | dulux | copic | prismacolor + + +# Example 1 - Gotta fetch 'em `--all`: +$ python main.py fetch --all && python main.py process --all + +# Example 2 - Fetching two sets & processing them: +$ python main.py fetch copic dulux && python main.py process copic dulux # or simply `--all` ``` ## Color samples @@ -48,7 +72,7 @@ We assume neither ownership nor intellectual property of any kind - color codes - [x] Filtering neons, pastels & metallics - [x] Adding copyright notice for RAL®/Dulux® (XML + GPL) + fallback option - [x] Adding examples for all color palettes -- [ ] Making use of CLI arguments +- [x] Making use of CLI arguments - [x] Automatizing example generation - [x] Combining all `fetch` scripts - [x] Cleaning up examples (merge CSS, remove RGB2hex & PHP error settings) diff --git a/lib/copic.py b/lib/copic.py index 97873d4..397f249 100644 --- a/lib/copic.py +++ b/lib/copic.py @@ -44,7 +44,7 @@ def __init__(self): # Valid `set_name` parameter: # - 'copic', currently 289 colors ## - def fetch(self, set_name): + def fetch(self, set_name='copic'): # One baseURL to rule them all base_url = 'https://www.copicmarker.com/collections/collect' diff --git a/lib/dulux.py b/lib/dulux.py index 17ec2ed..193f14c 100644 --- a/lib/dulux.py +++ b/lib/dulux.py @@ -43,7 +43,7 @@ def __init__(self): # Valid `set_name` parameter: # - 'dulux', currently 1768 colors ## - def fetch(self, set_name): + def fetch(self, set_name='dulux'): # One baseURL to rule them all base_url = 'https://colour.dulux.ca/all-colors' diff --git a/lib/pantone.py b/lib/pantone.py index 0aa0e71..38ebc04 100755 --- a/lib/pantone.py +++ b/lib/pantone.py @@ -87,6 +87,15 @@ def fetch(self, set_name, firstPage, lastPage): print('Loading ' + color['code'] + ' in set "' + set_name + '" .. done') + ## + # Fetches all PANTONE® colors at once + ## + def fetch_all(self): + self.fetch('graphic-design', 1, 32) + self.fetch('fashion-design', 1, 14) + self.fetch('product-design', 1, 10) + + ## # Creates JSON files for Dulux® color sets ## diff --git a/lib/prismacolor.py b/lib/prismacolor.py index 6bd160a..8386a90 100644 --- a/lib/prismacolor.py +++ b/lib/prismacolor.py @@ -44,7 +44,7 @@ def __init__(self): # Valid `set_name` parameter: # - 'premier', currently 150 colors ## - def fetch(self, set_name): + def fetch(self, set_name='premier'): # One baseURL to rule them all base_url = 'https://kredki.eu/pl/p/Prismacolor-Colored-Pencils-Kredki-Art-150-Kol/75' diff --git a/lib/ral.py b/lib/ral.py index a6fcd74..d1999ed 100755 --- a/lib/ral.py +++ b/lib/ral.py @@ -16,7 +16,7 @@ from palette import Palette -class Ral(Palette): +class RAL(Palette): # Dictionary holding fetched colors sets = { 'classic': [], @@ -114,3 +114,13 @@ def fetch(self, set_name): self.sets[identifier].append(color) print('Loading ' + color['code'] + ' in set "' + set_name + '" .. done') + + + ## + # Fetches all RAL® colors at once + ## + def fetch_all(self): + self.fetch('classic') + self.fetch('design') + self.fetch('effect') + self.fetch('plastics') diff --git a/main.py b/main.py index 2dfdbd6..b5cbc24 100644 --- a/main.py +++ b/main.py @@ -4,44 +4,82 @@ # IMPORTS # For more information, see https://www.python.org/dev/peps/pep-0008/#imports ## +import click from lib.pantone import Pantone -from lib.ral import Ral +from lib.ral import RAL from lib.dulux import Dulux from lib.copic import Copic from lib.prismacolor import Prismacolor -pantone = Pantone() -pantone.fetch('graphic-design', 1, 32) -pantone.fetch('fashion-design', 1, 14) -pantone.fetch('product-design', 1, 10) -pantone.save() -pantone.create_json() -pantone.make_palettes() - -ral = Ral() -ral.fetch('classic') -ral.fetch('design') -ral.fetch('effect') -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() + +CONTEXT_SETTINGS = dict( + help_option_names=['-h', '--help'], +) + + + +class_map = { + 'dulux': Dulux, + 'copic': Copic, + 'prismacolor': Prismacolor, +} + +@click.group(context_settings=CONTEXT_SETTINGS) +@click.version_option(None, '-v', '--version') +def cli(): + pass + +@cli.command() +@click.argument('sets', nargs=-1) +@click.option('--all', 'fetch_all', flag_value=True, help='Fetch all available color sets & save as JSON.') +def fetch(sets, fetch_all): + """ + ARGS: + pantone | ral | dulux | copic | prismacolor + """ + valid_sets = list(class_map.keys()) + + if fetch_all == True: + sets = valid_sets + + for set in sets: + if set in valid_sets: + object = class_map[set]() + + try: + object.fetch_all() + except AttributeError: + object.fetch() + + object.save() + object.create_json() + else: + print('"' + set + '" isn\'t available. Please provide a valid color space,\nsuch as "pantone", "ral", "dulux", "copic" & "prismacolor".') + continue + + +@cli.command() +@click.argument('sets', nargs=-1) +@click.option('--all', 'process_all', flag_value=True, help='Process all available color sets & generate color palettes.') +def process(sets, process_all): + """ + ARGS: + pantone | ral | dulux | copic | prismacolor + """ + valid_sets = list(class_map.keys()) + + if process_all == True: + sets = valid_sets + + for set in sets: + if set in valid_sets: + object = class_map[set]() + object.make_palettes() + else: + print('"' + set + '" isn\'t available. Please provide a valid color space,\nsuch as "pantone", "ral", "dulux", "copic" & "prismacolor".') + continue + + +if __name__ == '__main__': + cli()