File: manage-plnt.py

package info (click to toggle)
python-werkzeug 1.0.1%2Bdfsg1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,888 kB
  • sloc: python: 21,897; javascript: 173; makefile: 36; xml: 16
file content (140 lines) | stat: -rwxr-xr-x 3,723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    Manage plnt
    ~~~~~~~~~~~

    This script manages the plnt application.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
"""
import os

import click
from werkzeug.serving import run_simple


def make_app():
    """Helper function that creates a plnt app."""
    from plnt import Plnt

    database_uri = os.environ.get("PLNT_DATABASE_URI")
    app = Plnt(database_uri or "sqlite:////tmp/plnt.db")
    app.bind_to_context()
    return app


@click.group()
def cli():
    pass


@cli.command()
def initdb():
    """Initialize the database"""
    from plnt.database import Blog, session

    make_app().init_database()
    # and now fill in some python blogs everybody should read (shamelessly
    # added my own blog too)
    blogs = [
        Blog(
            "Armin Ronacher",
            "http://lucumr.pocoo.org/",
            "http://lucumr.pocoo.org/cogitations/feed/",
        ),
        Blog(
            "Georg Brandl",
            "http://pyside.blogspot.com/",
            "http://pyside.blogspot.com/feeds/posts/default",
        ),
        Blog(
            "Ian Bicking",
            "http://blog.ianbicking.org/",
            "http://blog.ianbicking.org/feed/",
        ),
        Blog(
            "Amir Salihefendic", "http://amix.dk/", "http://feeds.feedburner.com/amixdk"
        ),
        Blog(
            "Christopher Lenz",
            "http://www.cmlenz.net/blog/",
            "http://www.cmlenz.net/blog/atom.xml",
        ),
        Blog(
            "Frederick Lundh",
            "http://online.effbot.org/",
            "http://online.effbot.org/rss.xml",
        ),
    ]
    # okay. got tired here.  if someone feels that he is missing, drop me
    # a line ;-)
    for blog in blogs:
        session.add(blog)
    session.commit()
    click.echo("Initialized database, now run manage-plnt.py sync to get the posts")


@cli.command()
@click.option("-h", "--hostname", type=str, default="localhost", help="localhost")
@click.option("-p", "--port", type=int, default=5000, help="5000")
@click.option("--no-reloader", is_flag=True, default=False)
@click.option("--debugger", is_flag=True)
@click.option("--no-evalex", is_flag=True, default=False)
@click.option("--threaded", is_flag=True)
@click.option("--processes", type=int, default=1, help="1")
def runserver(hostname, port, no_reloader, debugger, no_evalex, threaded, processes):
    """Start a new development server."""
    app = make_app()
    reloader = not no_reloader
    evalex = not no_evalex
    run_simple(
        hostname,
        port,
        app,
        use_reloader=reloader,
        use_debugger=debugger,
        use_evalex=evalex,
        threaded=threaded,
        processes=processes,
    )


@cli.command()
@click.option("--no-ipython", is_flag=True, default=False)
def shell(no_ipython):
    """Start a new interactive python session."""
    banner = "Interactive Werkzeug Shell"
    namespace = {"app": make_app()}
    if not no_ipython:
        try:
            try:
                from IPython.frontend.terminal.embed import InteractiveShellEmbed

                sh = InteractiveShellEmbed.instance(banner1=banner)
            except ImportError:
                from IPython.Shell import IPShellEmbed

                sh = IPShellEmbed(banner=banner)
        except ImportError:
            pass
        else:
            sh(local_ns=namespace)
            return
    from code import interact

    interact(banner, local=namespace)


@cli.command()
def sync():
    """Sync the blogs in the planet.  Call this from a cronjob."""
    from plnt.sync import sync

    make_app().bind_to_context()
    sync()


if __name__ == "__main__":
    cli()