werkzeug

Форк
0
/
manage-plnt.py 
131 строка · 3.5 Кб
1
import os
2

3
import click
4
from werkzeug.serving import run_simple
5

6

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

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

16

17
@click.group()
18
def cli():
19
    pass
20

21

22
@cli.command()
23
def initdb():
24
    """Initialize the database"""
25
    from plnt.database import Blog, session
26

27
    make_app().init_database()
28
    # and now fill in some python blogs everybody should read (shamelessly
29
    # added my own blog too)
30
    blogs = [
31
        Blog(
32
            "Armin Ronacher",
33
            "https://lucumr.pocoo.org/",
34
            "https://lucumr.pocoo.org/feed.atom",
35
        ),
36
        Blog(
37
            "Georg Brandl",
38
            "https://pyside.blogspot.com/",
39
            "https://pyside.blogspot.com/feeds/posts/default",
40
        ),
41
        Blog(
42
            "Ian Bicking",
43
            "https://blog.ianbicking.org/",
44
            "https://blog.ianbicking.org/feed/",
45
        ),
46
        Blog(
47
            "Amir Salihefendic",
48
            "http://amix.dk/",
49
            "https://feeds.feedburner.com/amixdk",
50
        ),
51
        Blog(
52
            "Christopher Lenz",
53
            "https://www.cmlenz.net/blog/",
54
            "https://www.cmlenz.net/blog/atom.xml",
55
        ),
56
        Blog(
57
            "Frederick Lundh",
58
            "https://effbot.org/",
59
            "https://effbot.org/rss.xml",
60
        ),
61
    ]
62
    # okay. got tired here.  if someone feels that they are missing, drop me
63
    # a line ;-)
64
    for blog in blogs:
65
        session.add(blog)
66
    session.commit()
67
    click.echo("Initialized database, now run manage-plnt.py sync to get the posts")
68

69

70
@cli.command()
71
@click.option("-h", "--hostname", type=str, default="localhost", help="localhost")
72
@click.option("-p", "--port", type=int, default=5000, help="5000")
73
@click.option("--no-reloader", is_flag=True, default=False)
74
@click.option("--debugger", is_flag=True)
75
@click.option("--no-evalex", is_flag=True, default=False)
76
@click.option("--threaded", is_flag=True)
77
@click.option("--processes", type=int, default=1, help="1")
78
def runserver(hostname, port, no_reloader, debugger, no_evalex, threaded, processes):
79
    """Start a new development server."""
80
    app = make_app()
81
    reloader = not no_reloader
82
    evalex = not no_evalex
83
    run_simple(
84
        hostname,
85
        port,
86
        app,
87
        use_reloader=reloader,
88
        use_debugger=debugger,
89
        use_evalex=evalex,
90
        threaded=threaded,
91
        processes=processes,
92
    )
93

94

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

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

110
                sh = IPShellEmbed(banner=banner)
111
        except ImportError:
112
            pass
113
        else:
114
            sh(local_ns=namespace)
115
            return
116
    from code import interact
117

118
    interact(banner, local=namespace)
119

120

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

126
    make_app().bind_to_context()
127
    sync()
128

129

130
if __name__ == "__main__":
131
    cli()
132

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.