Skip to content

Commit

Permalink
Move from flask-scripts to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
UlrichB22 committed Mar 9, 2023
1 parent b1eb359 commit ec7178f
Show file tree
Hide file tree
Showing 34 changed files with 1,170 additions and 1,076 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ minversion = 2.0
max-line-length = 120
per-file-ignores =
src/moin/apps/frontend/views.py:F405 # 'name' may be undefined, or defined from star imports
src/moin/scripts/migration/moin19/import19.py:F405
src/moin/cli/migration/moin19/import19.py:F405
src/moin/storage/middleware/indexing.py:F405
src/moin/*/_tests/test_*.py:E501
src/moin/config/wikiconfig.py:E501 # line too long
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
'Flask<2.1.0', # micro framework
'Flask-Babel<3.0.0', # i18n support
'Flask-Caching>=1.2.0', # caching support
'Flask-Script==2.0.5', # scripting support
'Flask-Theme>=0.3.5', # theme support
'emeraldtree>=0.10.0', # xml processing
'feedgen==0.9.*', # Atom feed
Expand Down Expand Up @@ -115,7 +114,7 @@
# windows binaries available from 3rd parties
},
entry_points=dict(
console_scripts=['moin = moin.scripts:main'],
console_scripts=['moin = moin.cli:cli',],
),

# stuff for babel:
Expand Down
68 changes: 45 additions & 23 deletions src/moin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright: 2002-2011 MoinMoin:ThomasWaldmann
# Copyright: 2008 MoinMoin:FlorianKrupicka
# Copyright: 2010 MoinMoin:DiogenesAugusto
# Copyright: 2023 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Expand All @@ -14,10 +15,6 @@
from os import path
import sys

# do this early, but not in moin/__init__.py because we need to be able to
# "import moin" from setup.py even before flask, werkzeug, ... is installed.
from moin.utils import monkeypatch # noqa

from flask import Flask, request, session
from flask import current_app as app
from flask import g as flaskg
Expand All @@ -26,14 +23,16 @@
from flask_theme import setup_themes

from jinja2 import ChoiceLoader, FileSystemLoader
from whoosh.index import EmptyIndexError

# do this early, but not in moin/__init__.py because we need to be able to
# "import moin" from setup.py even before flask, werkzeug, ... is installed.
from moin.utils import monkeypatch # noqa
from moin.utils.clock import Clock
from moin import auth, user, config
from moin.constants.misc import ANON
from moin.i18n import i18n_init
from moin.themes import setup_jinja_env, themed_error
from moin.utils.clock import Clock
from moin.storage.middleware import protecting, indexing, routing
from moin import auth, user, config

from moin import log
logging = log.getLogger(__name__)
Expand Down Expand Up @@ -74,8 +73,11 @@ def create_app_ext(flask_config_file=None, flask_config_dict=None,
"""
clock = Clock()
clock.start('create_app total')
logging.debug("running create_app_ext")
app = Flask('moin')

logging.debug("app.request_context: %s", str(app.request_context))

clock.start('create_app load config')
if flask_config_file:
app.config.from_pyfile(path.abspath(flask_config_file))
Expand All @@ -88,7 +90,7 @@ def create_app_ext(flask_config_file=None, flask_config_dict=None,
if not path.exists(flask_config_file):
# we should be here only because wiki admin is running
# `moin help` or `moin create-instance`
if 'create-instance' in sys.argv or 'help' in sys.argv:
if 'create-instance' in sys.argv or 'help' in sys.argv or '--help' in sys.argv:
config_path = path.dirname(config.__file__)
flask_config_file = path.join(config_path, 'wikiconfig.py')
else:
Expand Down Expand Up @@ -179,31 +181,38 @@ def destroy_app(app):
deinit_backends(app)


def init_backends(app):
def init_backends(app, create_backend=False):
"""
initialize the backends
"""
# A ns_mapping consists of several lines, where each line is made up like this:
# mountpoint, unprotected backend
# Just initialize with unprotected backends.
logging.debug("running init_backends")
app.router = routing.Backend(app.cfg.namespace_mapping, app.cfg.backend_mapping)
if app.cfg.create_storage:
if create_backend or getattr(app.cfg, 'create_backend', False):
app.router.create()
app.router.open()
app.storage = indexing.IndexingMiddleware(app.cfg.index_storage, app.router,
wiki_name=app.cfg.interwikiname,
acl_rights_contents=app.cfg.acl_rights_contents)
if app.cfg.create_index:

if 'index-create' in sys.argv: # makes options -i and -s obsolete
app.cfg.create_index = True
app.cfg.create_storage = True

# TODO: remove create_index after full migration to cli
logging.debug("create_index: %s index_create: %s create_backend: %s",
getattr(app.cfg, 'create_index', False),
getattr(app.cfg, 'index_create', False), str(create_backend))
if create_backend or getattr(app.cfg, 'create_backend', False): # 2. call of init_backends
app.storage.create()
try:
app.storage.open()
except EmptyIndexError:
# we should be here only because wiki admin is running
# `moin help` or `moin create-instance`
if 'create-instance' in sys.argv or 'help' in sys.argv:
pass
else:
raise
if 'create-instance' in sys.argv or 'index-create' in sys.argv or \
'help' in sys.argv or '--help' in sys.argv:
pass
else:
app.storage.open()


def deinit_backends(app):
Expand All @@ -220,6 +229,7 @@ def setup_user():
Try to retrieve a valid user object from the request, be it
either through the session or through a login.
"""
logging.debug("running setup_user")
# init some stuff for auth processing:
flaskg._login_multistage = None
flaskg._login_multistage_name = None
Expand Down Expand Up @@ -257,6 +267,11 @@ def setup_user():
return userobj


def setup_user_anon():
""" Setup anonymous user when no request available - CLI """
flaskg.user = user.User(name=ANON, auth_method='invalid')


def before_wiki():
"""
Setup environment for wiki requests, start timers.
Expand All @@ -267,8 +282,13 @@ def before_wiki():
flaskg.clock.start('init')
try:
flaskg.unprotected_storage = app.storage
cli_no_request_ctx = False
try:
flaskg.user = setup_user()
except RuntimeError: # CLI call has no valid request context, create dummy
flaskg.user = user.User(name=ANON, auth_method='invalid')
cli_no_request_ctx = True

flaskg.user = setup_user()
flaskg.storage = protecting.ProtectingMiddleware(app.storage, flaskg.user, app.cfg.acl_mapping)

flaskg.dicts = app.cfg.dicts()
Expand All @@ -277,10 +297,12 @@ def before_wiki():
flaskg.content_lang = app.cfg.language_default
flaskg.current_lang = app.cfg.language_default

setup_jinja_env()
if cli_no_request_ctx: # no request.user_agent if this is pytest or cli
flaskg.add_lineno_attr = False
else:
setup_jinja_env()
flaskg.add_lineno_attr = request.user_agent and flaskg.user.edit_on_doubleclick

# request.user_agent == '' if this is pytest
flaskg.add_lineno_attr = request.user_agent and flaskg.user.edit_on_doubleclick
finally:
flaskg.clock.stop('init')

Expand Down
4 changes: 2 additions & 2 deletions src/moin/apps/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
from moin.constants.contenttypes import * # noqa
from moin.constants.rights import SUPERUSER
from moin.utils import crypto, rev_navigation, close_file, show_time
from moin.utils.crypto import make_uuid
from moin.utils.crypto import make_uuid, hash_hexdigest
from moin.utils.interwiki import url_for_item, split_fqname, CompositeName
from moin.utils.mime import Type, type_moin_document
from moin.utils.tree import html, docbook
Expand All @@ -79,7 +79,7 @@
from moin.signalling import item_displayed, item_modified
from moin.storage.middleware.protecting import AccessDenied, gen_fqnames
from moin.converters import default_registry as reg
from moin.scripts.migration.moin19.import19 import hash_hexdigest
# from moin.cli.migration.moin19.import19 import hash_hexdigest
from moin.storage.middleware.validation import validate_data
import moin.utils.mimetype as mime_type

Expand Down
146 changes: 70 additions & 76 deletions src/moin/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,91 @@
# Copyright: 2000-2002 Juergen Hermann <[email protected]>
# Copyright: 2006,2011 MoinMoin:ThomasWaldmann
# Copyright: 2023 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - Extension Script Package
MoinMoin CLI - Extension Script Package
"""

import sys
import click

from flask_script import Manager, Server, Command
from flask.cli import FlaskGroup

from moin.app import create_app
from moin.cli.maint import create_instance, index, modify_item, set_meta, serialization, reduce_revisions, dump_html
from moin.cli.account import create, disable, resetpw
from moin.cli.migration.moin19 import import19

class Help(Command):
description = 'Quick help'
from moin import log
logging = log.getLogger(__name__)

def run(self):
# TODO: add more help here as soon as stuff has been migrated from "m" to "moin".
print("""\

def Help():
""" Moin initial help"""
print("""\
Quick help / most important commands overview:
moin index-create # create index (optionally also create empty storage)
moin create-instance # Create wikiconfig and wiki instance directories
moin index-create # Create empty indexes and storage
moin run # Run moin's builtin web server
moin import19 # Import wiki data from moin 1.9
moin moin # run moin's builtin web server
moin import19 # import data from moin 1.9
For more information please run:
moin index-build # (re)build index
moin --help
For more information please see:
moin <subcommand> --help
- "moin --help" command output
- "moin <subcommand> --help" command output
- docs
or read the Docs at https://moin-20.readthedocs.io/
""")


def main(default_command='help', wiki_config=None):
"""
console_script entry point
"""
from moin.app import create_app

manager = Manager(create_app)
manager.add_option('-c', '--config', dest='config', required=False, default=wiki_config)
manager.add_option('-i', '--index-create', action='store_true', dest='create_index',
required=False, default=False)
manager.add_option('-s', '--storage-create', action='store_true', dest='create_storage',
required=False, default=False)

manager.add_command("help", Help())
manager.add_command("moin", Server(host='127.0.0.1', port=8080))
manager.add_command("run", Server(host='127.0.0.1', port=8080))

from moin.scripts.maint import create_instance
manager.add_command("create-instance", create_instance.CreateInstance())

from moin.scripts.maint import index
manager.add_command("index-create", index.IndexCreate())
manager.add_command("index-build", index.IndexBuild())
manager.add_command("index-update", index.IndexUpdate())
manager.add_command("index-destroy", index.IndexDestroy())
manager.add_command("index-move", index.IndexMove())
manager.add_command("index-optimize", index.IndexOptimize())
manager.add_command("index-dump", index.IndexDump())
from moin.scripts.maint import serialization
manager.add_command("save", serialization.Serialize())
manager.add_command("load", serialization.Deserialize())
manager.add_command("load-sample", serialization.LoadSample())
from moin.scripts.maint.dump_html import Dump
manager.add_command("dump-html", Dump())
from moin.scripts.account.create import Create_User
manager.add_command("account-create", Create_User())
from moin.scripts.account.disable import Disable_User
manager.add_command("account-disable", Disable_User())
from moin.scripts.account.resetpw import Set_Password
manager.add_command("account-password", Set_Password())
from moin.scripts.maint.reduce_revisions import Reduce_Revisions
manager.add_command("maint-reduce-revisions", Reduce_Revisions())
from moin.scripts.maint.set_meta import Set_Meta
manager.add_command("maint-set-meta", Set_Meta())
from moin.scripts.maint import modify_item
manager.add_command("item-get", modify_item.GetItem())
manager.add_command("item-put", modify_item.PutItem())
manager.add_command("load-help", modify_item.LoadHelp())
manager.add_command("dump-help", modify_item.DumpHelp())
from moin.scripts.migration.moin19.import19 import ImportMoin19
manager.add_command("import19", ImportMoin19())

from moin.scripts.maint.moinshell import MoinShell
manager.add_command("shell", MoinShell())

return manager.run(default_command=default_command)


def fatal(msg):
sys.exit(msg)
# @click.option('--config', required=False, default=None)
@click.group(cls=FlaskGroup, create_app=create_app, invoke_without_command=True)
@click.pass_context
def cli(ctx):
""" Moin extensions to the Flask CLI"""
logging.debug("invoked_subcommand: %s", ctx.invoked_subcommand)
if ctx.invoked_subcommand is None:
Help()


@cli.command('help', help='Quick help')
def _Help():
Help()


cli.add_command(create_instance.CreateInstance)

cli.add_command(index.IndexCreate)
cli.add_command(index.IndexBuild)
cli.add_command(index.IndexUpdate)
cli.add_command(index.IndexDestroy)
cli.add_command(index.IndexMove)
cli.add_command(index.IndexOptimize)
cli.add_command(index.IndexDump)

cli.add_command(serialization.Serialize)
cli.add_command(serialization.Deserialize)
cli.add_command(serialization.LoadSample)

cli.add_command(dump_html.Dump)

cli.add_command(create.CreateUser)
cli.add_command(disable.DisableUser)
cli.add_command(resetpw.SetPassword)

cli.add_command(reduce_revisions.ReduceRevisions)

cli.add_command(set_meta.SetMeta)

cli.add_command(modify_item.GetItem)
cli.add_command(modify_item.PutItem)
cli.add_command(modify_item.LoadHelp)
cli.add_command(modify_item.DumpHelp)

cli.add_command(import19.ImportMoin19)
3 changes: 2 additions & 1 deletion src/moin/cli/account/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright: 2006,2011 MoinMoin:ThomasWaldmann
# Copyright: 2023 MoinMoin project
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - User Accounts Management Scripts
MoinMoin CLI - User Accounts Management Scripts
"""
Loading

0 comments on commit ec7178f

Please sign in to comment.