tinkerer / tinkerer / cmdline.py

    Tinkerer command line

    Automates the following blog operations:

    setup - to create a new blog
    build - to clean build blog
    post - to create a new post
    page - to create a new page

    :copyright: Copyright 2011-2012 by Vlad Riscutia and contributors (see
    :license: FreeBSD, see LICENSE file
import argparse
import os
import shutil
import sphinx
import sys
from tinkerer import draft, page, paths, post, writer, utils

def setup(quiet=False, filename_only=False):
    Sets up a new blog in the current directory.

    if filename_only:
    elif not quiet:
        print("Your new blog is almost ready!")
        print("You just need to edit a couple of lines in %s" % (os.path.relpath(paths.conf_file), ))

def build(quiet=False, filename_only=False):
    Runs a clean Sphinx build of the blog.
    # clean build directory
    if os.path.exists(paths.blog):

    flags = ["sphinx-build"]
    # silence Sphinx if in quiet mode
    if quiet or filename_only:
    # look up the project root in the directory structure
    flags += ["-d", paths.doctree, "-b", "html",
              utils.find_project_root(), paths.html]

    # build always prints "index.html"
    if filename_only:

    return sphinx.main(flags)

def create_post(title, quiet=False, filename_only=False):
    Creates a new post with the given title or makes an existing file a post.
    move = os.path.exists(title)

    if move:
        new_post = post.move(title)
        new_post = post.create(title)

    if filename_only:
    elif not quiet:
        if move:
            print("Draft moved to post '%s'" % new_post.path)
            print("New post created as '%s'" % new_post.path)

def create_page(title, quiet=False, filename_only=False):
    Creates a new page with the given title or makes an existing file a page.
    move = os.path.exists(title)

    if move:
        new_page = page.move(title)
        new_page = page.create(title)

    if filename_only:
    elif not quiet:
        if move:
            print("Draft moved to page '%s'" % new_page.path)
            print("New page created as '%s'" % new_page.path)

def create_draft(title, quiet=False, filename_only=False):
    Creates a new draft with the given title or makes an existing file a draft.
    move = os.path.exists(title)

    if move:
        new_draft = draft.move(title)
        new_draft = draft.create(title)

    if filename_only:
    elif not quiet:
        if move:
            print("File moved to draft '%s'" % new_draft)
            print("New draft created as '%s'" % new_draft)

def parse_arguments(argv):
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument("-s", "--setup", action="store_true", help="setup a new blog")
    group.add_argument("-b", "--build", action="store_true", help="build blog")
    group.add_argument("-p", "--post", nargs=1,
            help="create a new post with the title POST (if a file named POST "
                 "exists, it is moved to a new post instead)")
    group.add_argument("--page", nargs=1,
            help="create a new page with the title PAGE (if a file named PAGE "
                 "exists, it is moved to a new page instead)")
    group.add_argument("-d", "--draft", nargs=1,
            help="creates a new draft with the title DRAFT (if a file named DRAFT "
                 "exists, it is moved to a new draft instead)")

    group = parser.add_mutually_exclusive_group()
    group.add_argument("-q", "--quiet", action="store_true", help="quiet mode")
    group.add_argument("-f", "--filename", action="store_true",
            help="output filename only - useful to pipe Tinkerer commands")

    return parser.parse_args(argv)

def main(argv=None):
    Parses command line and executes required action.
    command = parse_arguments(argv)

    # in setup mode it should fail if run from a blog root
    if command.setup and os.path.exists(paths.conf_file):
        sys.stderr.write("'conf.py' already exists in current directory, "
                "choose a different directory to setup your blog.\n")
        return -1

    if command.setup:
        setup(command.quiet, command.filename)
    elif command.build:
        return build(command.quiet, command.filename)
    elif command.post:
        create_post(command.post[0], command.quiet, command.filename)
    elif command.page:
        create_page(command.page[0], command.quiet, command.filename)
    elif command.draft:
        create_draft(command.draft[0], command.quiet, command.filename)

    return 0
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.