Skip to content
Browse files

script now generates pep8-valid code and is pep8-valid itself

  • Loading branch information...
1 parent b75aafe commit bf47c6ac06caf6a5ebbe0bef98f888a0b6a339b0 @steingrd committed Jan 4, 2009
View
177 scripts/django-project-template.py
@@ -10,7 +10,7 @@
Usage:
- create-django-project.py [options] projectname
+ create-django-project.py [options] projectname
Options:
@@ -25,9 +25,9 @@
import sys
-def create_project_template(projectname, **options):
- if os.path.exists(projectname):
- print 'skipping %s, directory exists' % projectname
+def create_project_template(project, **options):
+ if os.path.exists(project):
+ print 'skipping %s, directory exists' % project
return
if 'appname' in options:
@@ -40,41 +40,45 @@ def create_project_template(projectname, **options):
else:
quiet = False
- create_directory(projectname)
- create_directory(projectname, 'media')
- create_directory(projectname, 'python')
- create_directory(projectname, 'python', appname)
- create_directory(projectname, 'python', projectname)
- create_directory(projectname, 'scripts')
- create_directory(projectname, 'templates')
- create_directory(projectname, 'templates', appname)
+ create_directory(project)
+ create_directory(project, 'media')
+ create_directory(project, 'python')
+ create_directory(project, 'python', appname)
+ create_directory(project, 'python', project)
+ create_directory(project, 'scripts')
+ create_directory(project, 'templates')
+ create_directory(project, 'templates', appname)
secretkey = generate_secret_key()
- context = { 'projectname' : projectname, 'appname' : appname, 'secretkey' : secretkey }
-
- render_template('', {}, projectname, 'python', appname, '__init__.py')
- render_template(APP_URLS_PY_TEMPLATE, context, projectname, 'python', appname, 'urls.py')
- render_template(VIEWS_PY_TEMPLATE, context, projectname, 'python', appname, 'views.py')
- render_template(MODELS_PY_TEMPLATE, context, projectname, 'python', appname, 'models.py')
- render_template(FORMS_PY_TEMPLATE, context, projectname, 'python', appname, 'forms.py')
- render_template('', context, projectname, 'python', projectname, '__init__.py')
- render_template(PROJECT_URLS_PY_TEMPLATE, context, projectname, 'python', projectname, 'urls.py')
- render_template(SETTINGS_PY_TEMPLATE, context, projectname, 'python', projectname, 'settings.py')
- render_template(SETTINGS_PROD_PY_TEMPLATE, context, projectname, 'python', projectname, 'settings_prod.py')
- render_template(BASE_HTML_TEMPLATE, context, projectname, 'templates', 'base.html')
- render_template(TEMPLATE_404_TEMPLATE, {}, projectname, 'templates', '404.html')
- render_template(TEMPLATE_500_TEMPLATE, {}, projectname, 'templates', '500.html')
- render_template(INDEX_HTML_TEMPLATE, context, projectname, 'templates', appname, 'index.html')
- render_template(DEFAULT_CSS_TEMPLATE, context, projectname, 'media', 'default.css')
- render_template(MANAGE_PY_TEMPLATE, context, projectname, 'manage.py')
- render_template(ENVIRONMENT_SH_TEMPLATE, context, projectname, 'environment.sh')
- render_template(GENERATE_FCGI_SH_TEMPLATE, context, projectname, 'scripts', 'generate_fcgi.sh')
+ ctx = {'projectname': project,
+ 'appname': appname,
+ 'secretkey': secretkey}
+
+ render('', {}, project, 'python', appname, '__init__.py')
+ render(APP_URLS_PY, ctx, project, 'python', appname, 'urls.py')
+ render(VIEWS_PY, ctx, project, 'python', appname, 'views.py')
+ render(MODELS_PY, ctx, project, 'python', appname, 'models.py')
+ render('', ctx, project, 'python', project, '__init__.py')
+ render(PROJECT_URLS_PY, ctx, project, 'python', project, 'urls.py')
+ render(SETTINGS_PY, ctx, project, 'python', project, 'settings.py')
+ render(SETTINGS_PROD_PY, ctx, project, 'python', project,
+ 'settings_prod.py')
+ render(BASE_HTML, ctx, project, 'templates', 'base.html')
+ render(TEMPLATE_404, {}, project, 'templates', '404.html')
+ render(TEMPLATE_500, {}, project, 'templates', '500.html')
+ render(INDEX_HTML, ctx, project, 'templates', appname, 'index.html')
+ render(DEFAULT_CSS, ctx, project, 'media', 'default.css')
+ render(MANAGE_PY, ctx, project, 'manage.py')
+ render(ENVIRONMENT_SH, ctx, project, 'environment.sh')
+ render(GENERATE_FCGI_SH, ctx, project, 'scripts', 'generate_fcgi.sh')
# set executable flag for manage.py
- os.chmod(os.path.join(projectname, 'manage.py'), stat.S_IRWXU | stat.S_IRGRP | stat.S_IROTH)
+ os.chmod(os.path.join(project, 'manage.py'),
+ stat.S_IRWXU | stat.S_IRGRP | stat.S_IROTH)
if not quiet:
- print 'Project %s created with application %s' % (projectname, appname)
+ print 'Project %s created with application %s' % (project, appname)
+
def create_directory(dirname, *args):
"""
@@ -83,26 +87,26 @@ def create_directory(dirname, *args):
Example: create_directory('foo', 'bar', 'zot') creates the
directories foo/bar/zot/ in the current directory.
-
+
"""
if not os.path.exists(dirname):
os.mkdir(dirname)
-
+
if args:
root = dirname
for subdir in args:
newdir = os.path.join(root, subdir)
if not os.path.exists(newdir):
os.mkdir(newdir)
- root = newdir
+ root = newdir
-def render_template(template_string, context, *filepath):
+def render(template_string, context, *filepath):
"""
Renders the template string ``template_string`` to the file path
given as a list in ``filepath`` with ``context``.
- Example: render_template(string, ('foo', 'bar', 'zot.html'),
+ Example: render(string, ('foo', 'bar', 'zot.html'),
context) renders the file zot.html in the directory foo/bar/.
"""
@@ -117,13 +121,15 @@ def generate_secret_key():
"""
Generates a SECRET_KEY for Django settings module.
"""
- return ''.join([random.choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
- for i in range(50)])
+ alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
+ return ''.join([random.choice(alphabet) for i in range(50)])
def main():
try:
- opts, args = getopt.getopt(sys.argv[1:], "ha:q", ["help", "appname=", "quiet"])
+ short = "ha:q"
+ long = ["help", "appname=", "quiet"]
+ opts, args = getopt.getopt(sys.argv[1:], short, long)
except getopt.error, msg:
print msg
print "for help use --help"
@@ -140,15 +146,15 @@ def main():
print __doc__
sys.exit(0)
if o in ("-a", "--appname"):
- options['appname' ] = a
+ options['appname'] = a
if o in ("-q", "--quiet"):
- options['quiet' ] = True
+ options['quiet'] = True
for a in args:
create_project_template(a, **options)
-ENVIRONMENT_SH_TEMPLATE = """#!/bin/bash -x
+ENVIRONMENT_SH = """#!/bin/bash -x
export PYTHONPATH="$PWD/python:$PYTHONPATH"
export PATH="$PWD/scripts:$PATH"
@@ -157,10 +163,10 @@ def main():
export DJANGO_MEDIA_ROOT="$PWD/media"
"""
-DEFAULT_CSS_TEMPLATE = """/* default stylesheet, add styles here */
+DEFAULT_CSS = """/* default stylesheet, add styles here */
"""
-MANAGE_PY_TEMPLATE = """#!/usr/bin/env python
+MANAGE_PY = """#!/usr/bin/env python
from django.core.management import execute_manager
from os import getcwd, environ, path
@@ -172,18 +178,20 @@ def main():
environ['DJANGO_TEMPLATE_PATH'] = path.join(getcwd(), 'templates')
sys.path.insert(0, path.join(getcwd(), 'python'))
settings_module = '{{ projectname }}.settings'
- settings = __import__(settings_module, locals(), globals(), [settings_module.split('.')[-1]])
+ settings = __import__(settings_module, locals(), globals(),
+ [settings_module.split('.')[-1]])
execute_manager(settings)
"""
-BASE_HTML_TEMPLATE = """<html>
+BASE_HTML = """<html>
<head>
{% block title %}
<title>{{ projectname }}</title>
{% endblock %}
- <link rel="stylesheet" href="/media/default.css" type="text/css" media="screen" />
+ <link rel="stylesheet" href="/media/default.css"
+ type="text/css" media="screen" />
{% block extrahead %}
- {% endblock %}
+ {% endblock %}
</head>
<body>
{% block content %}
@@ -192,81 +200,86 @@ def main():
</html>
"""
-INDEX_HTML_TEMPLATE = """{% extends "base.html" %}
+INDEX_HTML = """{% extends "base.html" %}
{% block content %}
Hello, world! Greetings from {{ appname }}
{% endblock %}
"""
-TEMPLATE_404_TEMPLATE = """{% extends "base.html" %}
+TEMPLATE_404 = """{% extends "base.html" %}
{% block content %}
<h1>404 Not Found</h1>
<p>Resource {{ request_path }} not found</p>
{% endblock %}
"""
-TEMPLATE_500_TEMPLATE = """{% extends "base.html" %}
+TEMPLATE_500 = """{% extends "base.html" %}
{% block content %}
<h1>500 Internal Server Error</h1>
{% endblock %}
"""
-PROJECT_URLS_PY_TEMPLATE = """from django.conf import settings
+PROJECT_URLS_PY = """#!/usr/bin/env python
+
+from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
+
admin.autodiscover()
-urlpatterns = patterns('',
- url(r'^admin/(.*)', admin.site.root),
- url(r'^$', 'django.views.generic.simple.redirect_to', { 'url': '/{{ appname }}/' }),
+urlpatterns = patterns('',
+ url(r'^admin/(.*)', admin.site.root),
+ url(r'^$', 'django.views.generic.simple.redirect_to',
+ {'url': '/{{ appname }}/'}),
url(r'^{{ appname }}/', include('{{ appname }}.urls')),
)
if settings.DEBUG:
urlpatterns += patterns('',
- url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
- {
- 'document_root': settings.STATIC_ROOT
- }))
-
+ url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
+ {'document_root': settings.STATIC_ROOT}))
"""
-APP_URLS_PY_TEMPLATE = """from django.conf.urls.defaults import *
+APP_URLS_PY = """#!/usr/bin/env python
+
+from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
-urlpatterns = patterns('',
- url('^$', direct_to_template, {'template':'{{ appname }}/index.html'}),
+
+urlpatterns = patterns('',
+ url('^$', direct_to_template, {'template': '{{ appname }}/index.html'}),
)
"""
-MODELS_PY_TEMPLATE = """
+MODELS_PY = """#!/usr/bin/env python
+
from django.db import models
-
+
+
class Entry(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField()
date = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
-
+
def __unicode__(self):
return self.title
"""
-VIEWS_PY_TEMPLATE = """
+VIEWS_PY = """#!/usr/bin/env python
+
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponse
+
def {{ appname }}_index(request):
return HttpResponse('Hello, {{ appname }}')
"""
-FORMS_PY_TEMPLATE = """
-"""
-
-SETTINGS_PROD_PY_TEMPLATE = """from settings import *
+SETTINGS_PROD_PY = """from settings import *
DATABASE_ENGINE = ''
DATABASE_NAME = ''
@@ -279,7 +292,7 @@ def {{ appname }}_index(request):
TEMPLATE_DEBUG = False
"""
-SETTINGS_PY_TEMPLATE = """import os
+SETTINGS_PY = """import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG
@@ -308,7 +321,7 @@ def {{ appname }}_index(request):
os.getenv('DJANGO_TEMPLATE_PATH'),
)
-TEMPLATE_CONTEXT_PROCESSORS = (
+TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.auth',
'django.core.context_processors.media',
@@ -327,11 +340,11 @@ def {{ appname }}_index(request):
'django.middleware.doc.XViewMiddleware',
)
-DATABASE_ENGINE = 'sqlite3'
-DATABASE_NAME = '{{ projectname }}.db'
+DATABASE_ENGINE = 'sqlite3'
+DATABASE_NAME = '{{ projectname }}.db'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
-DATABASE_HOST = ''
+DATABASE_HOST = ''
DATABASE_PORT = ''
TIME_ZONE = 'Europe/Oslo'
@@ -347,12 +360,12 @@ def {{ appname }}_index(request):
SECRET_KEY = '{{ secretkey }}'
"""
-GENERATE_FCGI_SH_TEMPLATE = """#!/bin/bash
+GENERATE_FCGI_SH = """#!/bin/bash
for dir in templates python media; do
if [ ! -d "$dir" ]; then
- echo "expected directory '$dir' in current directory";
- echo "are you running $(basename $0) from project root?";
- exit 1;
+ echo "expected directory '$dir' in current directory";
+ echo "are you running $(basename $0) from project root?";
+ exit 1;
fi
done
cat <<EOF
View
2 setup.py
@@ -8,7 +8,7 @@
author='Steingrim Dovland',
author_email='steingrd@ifi.uio.no',
url='http://prettyprinted.net/code/django-project-template/',
- scripts=['scripts/django-project-template.py'],
+ scripts=['scripts/django-project-template.py'],
classifiers=['Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Framework :: Django',
View
3 template/manage.py
@@ -10,5 +10,6 @@
environ['DJANGO_TEMPLATE_PATH'] = path.join(getcwd(), 'templates')
sys.path.insert(0, path.join(getcwd(), 'python'))
settings_module = 'template.settings'
- settings = __import__(settings_module, locals(), globals(), [settings_module.split('.')[-1]])
+ settings = __import__(settings_module, locals(), globals(),
+ [settings_module.split('.')[-1]])
execute_manager(settings)
View
1 template/python/myapp/forms.py
@@ -1 +0,0 @@
-
View
6 template/python/myapp/models.py
@@ -1,13 +1,15 @@
+#!/usr/bin/env python
from django.db import models
-
+
+
class Entry(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField()
date = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
-
+
def __unicode__(self):
return self.title
View
7 template/python/myapp/urls.py
@@ -1,6 +1,9 @@
+#!/usr/bin/env python
+
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
-urlpatterns = patterns('',
- url('^$', direct_to_template, {'template':'myapp/index.html'}),
+
+urlpatterns = patterns('',
+ url('^$', direct_to_template, {'template': 'myapp/index.html'}),
)
View
2 template/python/myapp/views.py
@@ -1,6 +1,8 @@
+#!/usr/bin/env python
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponse
+
def myapp_index(request):
return HttpResponse('Hello, myapp')
View
10 template/python/template/settings.py
@@ -27,7 +27,7 @@
os.getenv('DJANGO_TEMPLATE_PATH'),
)
-TEMPLATE_CONTEXT_PROCESSORS = (
+TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.auth',
'django.core.context_processors.media',
@@ -46,11 +46,11 @@
'django.middleware.doc.XViewMiddleware',
)
-DATABASE_ENGINE = 'sqlite3'
-DATABASE_NAME = 'template.db'
+DATABASE_ENGINE = 'sqlite3'
+DATABASE_NAME = 'template.db'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
-DATABASE_HOST = ''
+DATABASE_HOST = ''
DATABASE_PORT = ''
TIME_ZONE = 'Europe/Oslo'
@@ -63,4 +63,4 @@
MEDIA_URL = 'http://localhost:8080/media/'
ADMIN_MEDIA_PREFIX = '/admin_media/'
-SECRET_KEY = 'izyh%^aj&w1b0pp^y=+!c8_yiu)@rn4$r(^3*1jyb(tfx_j4+_'
+SECRET_KEY = 'w54sk!qf#^%t3+*s6!cc8s_j$1^uoa__6xl#a2i75#@%n$ah=5'
View
17 template/python/template/urls.py
@@ -1,19 +1,20 @@
+#!/usr/bin/env python
+
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
+
admin.autodiscover()
-urlpatterns = patterns('',
- url(r'^admin/(.*)', admin.site.root),
- url(r'^$', 'django.views.generic.simple.redirect_to', { 'url': '/myapp/' }),
+urlpatterns = patterns('',
+ url(r'^admin/(.*)', admin.site.root),
+ url(r'^$', 'django.views.generic.simple.redirect_to',
+ {'url': '/myapp/'}),
url(r'^myapp/', include('myapp.urls')),
)
if settings.DEBUG:
urlpatterns += patterns('',
- url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
- {
- 'document_root': settings.STATIC_ROOT
- }))
-
+ url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
+ {'document_root': settings.STATIC_ROOT}))
View
6 template/scripts/generate_fcgi.sh
@@ -1,9 +1,9 @@
#!/bin/bash
for dir in templates python media; do
if [ ! -d "$dir" ]; then
- echo "expected directory '$dir' in current directory";
- echo "are you running $(basename $0) from project root?";
- exit 1;
+ echo "expected directory '$dir' in current directory";
+ echo "are you running $(basename $0) from project root?";
+ exit 1;
fi
done
cat <<EOF
View
5 template/templates/base.html
@@ -3,9 +3,10 @@
{% block title %}
<title>template</title>
{% endblock %}
- <link rel="stylesheet" href="/media/default.css" type="text/css" media="screen" />
+ <link rel="stylesheet" href="/media/default.css"
+ type="text/css" media="screen" />
{% block extrahead %}
- {% endblock %}
+ {% endblock %}
</head>
<body>
{% block content %}

0 comments on commit bf47c6a

Please sign in to comment.
Something went wrong with that request. Please try again.