| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | """ |
|---|
| 4 | Create a New Plugin |
|---|
| 5 | ~~~~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | This script asks a few questions to create a plugin skeleton |
|---|
| 8 | |
|---|
| 9 | :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details. |
|---|
| 10 | :license: BSD, see LICENSE for more details. |
|---|
| 11 | """ |
|---|
| 12 | import re |
|---|
| 13 | import sys |
|---|
| 14 | import os |
|---|
| 15 | from datetime import date |
|---|
| 16 | from jinja2 import Template |
|---|
| 17 | from optparse import OptionParser |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | _ident_re = re.compile(r'^[a-z][a-z0-9_]*$') |
|---|
| 21 | init_template = Template('''\ |
|---|
| 22 | # -*- coding: utf-8 -*- |
|---|
| 23 | """ |
|---|
| 24 | zine.plugins.{{ package }} |
|---|
| 25 | ~~~~~~~~~~~~~{{ '~' * package|length }} |
|---|
| 26 | |
|---|
| 27 | Plugin implementation description goes here. |
|---|
| 28 | |
|---|
| 29 | :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details. |
|---|
| 30 | :license: BSD, see LICENSE for more details. |
|---|
| 31 | """ |
|---|
| 32 | |
|---|
| 33 | def setup(app, plugin): |
|---|
| 34 | pass |
|---|
| 35 | |
|---|
| 36 | ''') |
|---|
| 37 | |
|---|
| 38 | doc_template = Template('''\ |
|---|
| 39 | {{ name }} |
|---|
| 40 | {{ '~' * name|length }} |
|---|
| 41 | |
|---|
| 42 | Plugin documentation goes here... |
|---|
| 43 | |
|---|
| 44 | ''') |
|---|
| 45 | |
|---|
| 46 | sys.path.append(os.path.dirname(__file__)) |
|---|
| 47 | import _init_zine |
|---|
| 48 | |
|---|
| 49 | from zine.utils.text import gen_ascii_slug |
|---|
| 50 | from zine.utils.mail import split_email |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | def main(): |
|---|
| 54 | global parser |
|---|
| 55 | parser = OptionParser(usage='%prog [path]') |
|---|
| 56 | options, args = parser.parse_args() |
|---|
| 57 | if not args: |
|---|
| 58 | path = '.' |
|---|
| 59 | elif len(args) == 1: |
|---|
| 60 | path = args[0] |
|---|
| 61 | else: |
|---|
| 62 | parser.error('incorrect number of arguments') |
|---|
| 63 | |
|---|
| 64 | print 'Welcome to the Plugin Wizard' |
|---|
| 65 | print 'This wizard will ask you a few questions to create a new' |
|---|
| 66 | print 'plugin skelleton.' |
|---|
| 67 | print |
|---|
| 68 | |
|---|
| 69 | while 1: |
|---|
| 70 | name = raw_input('English Name of the Plugin: ').strip() |
|---|
| 71 | if name: |
|---|
| 72 | break |
|---|
| 73 | print 'Error: you have to provide a name' |
|---|
| 74 | |
|---|
| 75 | package = gen_ascii_slug(name, delim='_') |
|---|
| 76 | while 1: |
|---|
| 77 | tmp = raw_input('Name of the package [%s]: ' % package) |
|---|
| 78 | if not tmp: |
|---|
| 79 | break |
|---|
| 80 | if _ident_re.search(tmp): |
|---|
| 81 | package = tmp |
|---|
| 82 | break |
|---|
| 83 | print 'Error: invalid package name' |
|---|
| 84 | |
|---|
| 85 | while 1: |
|---|
| 86 | author = raw_input('Author: ').strip() |
|---|
| 87 | if author: |
|---|
| 88 | break |
|---|
| 89 | print 'Error: You have to provide an author' |
|---|
| 90 | |
|---|
| 91 | author_url = raw_input('Author URL: ') |
|---|
| 92 | plugin_url = raw_input('Plugin URL: ') |
|---|
| 93 | |
|---|
| 94 | folder = os.path.join(path, package) |
|---|
| 95 | os.mkdir(folder) |
|---|
| 96 | |
|---|
| 97 | f = file(os.path.join(folder, 'metadata.txt'), 'w') |
|---|
| 98 | try: |
|---|
| 99 | for key, value in ('Name', name), ('Author', author), \ |
|---|
| 100 | ('Author URL', author_url), \ |
|---|
| 101 | ('Plugin URL', plugin_url): |
|---|
| 102 | if value: |
|---|
| 103 | f.write('%s: %s\n' % (key, value.encode('utf-8'))) |
|---|
| 104 | f.write('\nVersion: 0.1\nLicense: BSD\nDescription:\n') |
|---|
| 105 | finally: |
|---|
| 106 | f.close() |
|---|
| 107 | |
|---|
| 108 | author_name, author_email = split_email(author) |
|---|
| 109 | init_template.stream(dict( |
|---|
| 110 | package=package, |
|---|
| 111 | author=author_name or author_email, |
|---|
| 112 | year=date.today().year |
|---|
| 113 | )).dump(os.path.join(folder, '__init__.py'), 'utf-8') |
|---|
| 114 | |
|---|
| 115 | os.mkdir(os.path.join(folder, 'i18n')) |
|---|
| 116 | os.mkdir(os.path.join(folder, 'docs')) |
|---|
| 117 | os.mkdir(os.path.join(folder, 'docs', 'en')) |
|---|
| 118 | doc_template.stream(dict(name=name)) \ |
|---|
| 119 | .dump(os.path.join(folder, 'docs', 'en', 'index.rst')) |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | if __name__ == '__main__': |
|---|
| 123 | main() |
|---|