#14 Replace deprecated OptionParser with ArgumentParser
Merged 3 years ago by frantisekz. Opened 3 years ago by frantisekz.

file modified
+9 -31
@@ -18,15 +18,14 @@ 

  #    Josef Skladanka <jskladan@redhat.com>

  

  import os

- import sys

- from optparse import OptionParser

+ from argparse import ArgumentParser

  

  from alembic.config import Config

  from alembic import command as al_command

  from alembic.migration import MigrationContext

  

  from oraculum import app, db, controllers

- from oraculum.utils import db_utils, schedule, libkarma

+ from oraculum.utils import db_utils

  

  def get_alembic_config():

      # the location of the alembic ini file and alembic scripts changes when
@@ -39,7 +38,7 @@ 

      return alembic_cfg

  

  

- def initialize_db():

+ def init_db():

      alembic_cfg = get_alembic_config()

  

      # check to see if the db has already been initialized by checking for an
@@ -70,36 +69,15 @@ 

      print("Refreshing DB Cache")

      app.config['MAX_DB_AGE'] = 0

      app.config['FORCE_CACHED_DATA'] = False

-     db_utils.refresh_data("get_actions", controllers.main.get_actions()),

-     db_utils.refresh_data("api_v1_landing_page", controllers.main.api_v1_landing_page()),

+     db_utils.refresh_data("get_actions", controllers.main.get_actions())

+     db_utils.refresh_data("api_v1_landing_page", controllers.main.api_v1_landing_page())

      db_utils.refresh_data("api_v1_libkarma", controllers.main.api_v1_libkarma('all'))

  

  def main():

-     possible_commands = ['init_db', 'generate_config', 'upgrade_db', 'sync']

- 

-     usage = 'usage: %prog [options] command \n  Possible Commands: ' + ' '.join(

-         possible_commands)

-     parser = OptionParser(usage=usage)

-     parser.add_option('-d', '--dburi', dest='dburi', help='dburi to use')

- 

-     (options, args) = parser.parse_args()

- 

-     if len(args) < 1:

-         print("need to have at least 1 command")

-         sys.exit(1)

- 

-     command = args[0]

-     if not command in possible_commands:

-         print("Invalid command: %s" % command)

-         print("Please use one of the following commands: %s" % str(possible_commands))

-         sys.exit(1)

- 

-     elif command == 'upgrade_db':

-         upgrade_db()

-     elif command == 'init_db':

-         initialize_db()

-     elif command == 'sync':

-         sync()

+     parser = ArgumentParser()

+     parser.add_argument("command", choices=['init_db', 'upgrade_db', 'sync'], help='Available commands')

+     args = parser.parse_args()

+     globals()[args.command]()

  

  if __name__ == '__main__':

      main()

no initial comment

Seems unnecessary to pass the args around, when none of the methods actually need it.

All the subparsers seem quite convoluted, and the only gain seems to be the targeted 'help' message. Please either make sure the help messages are actually usefull, or just use a simple parser.add_argument("command", choices=['init_db', 'upgrade_db', 'sync'], help='Available commands')) instead. Thanks!

Either:

from argparse import ArgumentParser

def init_db():
    print("INIT_DB")

def upgrade_db():
    print("UPGRADE_DB")

def sync():
    print("SYNC")

def main():
    parser = ArgumentParser()
    parser.add_argument("command", choices=['init_db', 'upgrade_db', 'sync'], help='Available commands')
    args = parser.parse_args()
    {'init_db': init_db, 'upgrade_db': upgrade_db, 'sync': sync}[args.command]()

if __name__ == '__main__':
    main()

or (maybe a bit more elegant, while just a little bit more cryptic):

from argparse import ArgumentParser

def init_db():
    print("INIT_DB")

def upgrade_db():
    print("UPGRADE_DB")

def sync():
    print("SYNC")

def main():
    parser = ArgumentParser()
    parser.add_argument("command", choices=['init_db', 'upgrade_db', 'sync'], help='Available commands')
    args = parser.parse_args()
    globals()[args.command]()

if __name__ == '__main__':
    main()

rebased onto b4c165d

3 years ago

Pull-Request has been merged by frantisekz

3 years ago
Metadata