rookeries/admin.py

88 lines
3.6 KiB
Python

#
# Copyright (c) 2013 Dorian Pula <dorian.pula@amber-penguin-software.ca>
#
# Rookeries is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# Rookeries is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with Rookeries. If not, see <http://www.gnu.org/licenses/>.
#
# Please share and enjoy!
#
"""Admin module for Rookeries that adds some special utilities to run outside of the main webapp.
@author: Dorian Pula <dorian.pula@amber-penguin-software.ca>
"""
from rookeries import __version__ as __app_version
from rookeries.core.config import Config
from rookeries.core.database import Base, engine, manage_db
import argparse
def rebuild_database(arguments):
"""Re-synchronizes the database which is pretty useful for rapid development."""
# TODO Turn this more into a mode thing than multiple arguments.
manage_db(Base, engine, arguments.create_tables, arguments.drop_tables, arguments.init_data)
def print_configuration_opt(arguments):
"""Outputs the application configuration as a JSON formated string."""
pretty_print_opt = not arguments.inline
config = Config()
print(config.to_json(pretty_print=pretty_print_opt))
def main():
"""The main function of the admin utility."""
parser = build_argument_parser()
arguments = parser.parse_args()
arguments.func(arguments)
def build_argument_parser():
"""Builds out the argument parser for the admin utility."""
parser = argparse.ArgumentParser(
description="Admin utility for managing a Rookeries site for development and production.")
parser.add_argument("--version", "-v", action="version", version="Rookeries Admin - " + __app_version)
sub_parser = parser.add_subparsers(title="commands", description="Valid commands that can be run by the admin.",
help="command help")
config_parser = sub_parser.add_parser("config_json", help="Output the app configuration as JSON.")
config_parser.add_argument("-i", "--inline", action="store_true", default=False,
help="Print out in a single line. Useful for environment variables.")
config_parser.set_defaults(func=print_configuration_opt)
# rebuild_db_parser = sub_parser.add_parser("rebuild_db", help="Rebuilds the database.")
# rebuild_db_parser.add_argument("--drop_tables", action="store_true", default=False,
# help="Drops the existing tables.")
# rebuild_db_parser.add_argument("--init_data", action="store_true", default=False, help="Initializes the database.")
# rebuild_db_parser.set_defaults(func=rebuild_database)
# TODO Clean this up into commands.
manage_db_parser = sub_parser.add_parser("manage_db", help="Manages the database.")
manage_db_parser.add_argument("--create_tables", action="store_true", default=False, help="Creates the tables.")
manage_db_parser.add_argument("--drop_tables", action="store_true", default=False,
help="Drops the existing tables.")
manage_db_parser.add_argument("--init_data", action="store_true", default=False, help="Initializes the database.")
manage_db_parser.set_defaults(func=rebuild_database)
return parser
if __name__ == "__main__":
main()