|
| 1 | +import os |
| 2 | +import json |
| 3 | +import jinja2 |
| 4 | +import cookiecutter.find as find |
| 5 | +import cookiecutter.generate as gen |
| 6 | +from cookiecutter.config import DEFAULT_CONFIG as config |
| 7 | +from cookiecutter.environment import StrictEnvironment |
| 8 | +from cookiecutter.exceptions import NonTemplatedInputDirException |
| 9 | +from cookiecutter.main import cookiecutter as c |
| 10 | + |
| 11 | + |
| 12 | +def recut(): |
| 13 | + """ |
| 14 | + Recreate setup.py so that we can edit keywords |
| 15 | + """ |
| 16 | + # template location |
| 17 | + try: |
| 18 | + # cutting cookie from directory with template |
| 19 | + temp_dir = find.find_template('..') |
| 20 | + except NonTemplatedInputDirException as e: |
| 21 | + # template coming from Github |
| 22 | + # Hooks are passed through jinja2. raw will |
| 23 | + # Make sure `cookiecutter.project` isn't replaced |
| 24 | + {% raw %} |
| 25 | + temp_dir = os.path.join(config['cookiecutters_dir'], |
| 26 | + 'cookiecutter-ckan-extension', |
| 27 | + '{{cookiecutter.project}}') |
| 28 | + {% endraw %} |
| 29 | + |
| 30 | + # Location for resulting file |
| 31 | + destination = os.getcwd() |
| 32 | + # name of template |
| 33 | + setup_template = 'setup.py' |
| 34 | + |
| 35 | + # get context |
| 36 | + context = {{ cookiecutter | jsonify }} |
| 37 | + |
| 38 | + # Process keywords |
| 39 | + keywords = context['keywords'].strip().split() |
| 40 | + keywords = [keyword for keyword in keywords |
| 41 | + if keyword not in ('ckan', 'CKAN', 'A','space', |
| 42 | + 'seperated','list','of','keywords')] |
| 43 | + keywords.insert(0, 'CKAN') |
| 44 | + keywords = u' '.join(keywords) |
| 45 | + context['keywords'] = keywords |
| 46 | + |
| 47 | + # Double check 'project_shortname' and 'plugin_class_name' |
| 48 | + short_name = context['project'][8:].replace('-','_') |
| 49 | + if context['project_shortname'] != short_name: |
| 50 | + context['project_shortname'] = short_name |
| 51 | + |
| 52 | + plugin_class_name = '{}Plugin'.format(context['project_shortname']\ |
| 53 | + .title().replace('_','')) |
| 54 | + if context['plugin_class_name'] != plugin_class_name: |
| 55 | + context['plugin_class_name'] = plugin_class_name |
| 56 | + |
| 57 | + # Recut cookie |
| 58 | + env = StrictEnvironment() |
| 59 | + env.loader = jinja2.FileSystemLoader(temp_dir) |
| 60 | + gen.generate_file(project_dir=destination, |
| 61 | + infile=setup_template, |
| 62 | + context={'cookiecutter': context}, |
| 63 | + env=env) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + context = {{ cookiecutter | jsonify }} |
| 68 | + if context['_source'] == 'local': |
| 69 | + recut() |
0 commit comments