PasteScript-based Project Templates

With the increase in the number of projects, the problem arose of automating the creation of a framework for new applications. Until recently, we created new projects by copying a prepared template project and changing various settings in different places of the project. Of course, this killed a lot of time.

Attempts to solve this problem led me to a project called Python Paste . In general terms, this is a set of utilities for creating web applications, for example, you can create your own framework (in Pylons they use it). In addition to web utilities, Python Paste also contains a Paste Script module, with which you can create stub templates and generate projects based on them.

How does it all work?


  • We create a project that will contain the blanks, specify the paths to the catalogs with the blanks and make other settings.
  • In the template directory, create a file structure, taking into account the fact that the files and directory names will be passed through the Cheetah template   (you can use various variables inside the files, such as the name of the project and any others you configure yourself).
  • We install our project containing blanks.
  • The generation team creates a new project based on the template. Before generation, they will ask the project name and various parameters that were specified in the template settings

Create a simple template for projects in Django


Install PasteScript pip install PasteScript,

Create approximately the following project structure: Now we determine the location of the templates and settings for them:

Project structure


from setuptools import setup, find_packages
 
setup (
    name = 'paster-templates',
    version = "0.1",
    packages = find_packages (),
    install_requires = [
        'setuptools',
        'PasteScript',
        'Cheetah',
    ],
    include_package_data = True,
    zip_safe = False,
    # Here we describe our templates
    entry_points = "" "
        [paste.paster_create_template]
        simple = templates.pastertemplates: SimpleTemplate
    " ""
)

where simple is the name of the template, SimpleTemplate is the class with the settings.

In the SimpleTemplate class, configure the template itself, specify the directory with the files, a brief description and the “database name” parameter, which we will ask to enter the user:
from paste.script.templates import Template
from paste.script.templates import var
 
class SimpleTemplate (Template):
    _template_dir = 'simple'
    summary = "Simple project"
    use_cheetah = True
 
    vars = [
        var ('database',
            'Database name.' ,
            default = "test_db"),
    ]

We go into templates / and generate a new dzhangovsky project django-admin.py startproject simplewhich will be a template.

We proceed to configure the template files. The parameters specified during generation can be used inside the template files, after adding _tmpl to the file name. These variables are of the form $(property_name), the name of the directory to pass through the template are as follows: +property_name+. For example, a piece of settings.py_tmpl file:
...
# Project name - $ (project)
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '$ (database)',
    }
}
...

And a couple of notes on template files:
  • If in the template we write in Russian, you need to insert at the beginning of the file #encoding UTF-8
  • If you need to use the $ symbol in the template and so that cheetah does not take it for your own, it must be wrapped in #raw ... $content...   #end raw

Conclusion


That's all. Install the package with the template. The python setup.py install

team  paster create --list-templateslooks at the list of templates in the system (the templates are searched, as far as I understand, by the scan installed in the egg's system for the template plugins that were described in entry_points).

We  paster create --template=simple SimpleProjectcreate a new project based on the simple template.

It remains to create templates for all occasions. GitHub

project sources Links:



Also popular now: