PYTHON   106

box py

Guest on 14th June 2022 01:32:37 AM

  1. #! box/bin/python
  2.  
  3. """
  4. box.py
  5.  
  6. author: erich@emfeld.com
  7. ========================
  8.  
  9. This file is the main file that binds all the functions in the
  10. emerald directory.
  11.  
  12. The file handles all generator and database operations from the command line.
  13.  
  14. Running the server is performed by this file.
  15.  
  16. The file depends on the emerald module.
  17.  
  18. """
  19. import sys
  20. import os.path
  21.  
  22. from config import SQLALCHEMY_MIGRATE_REPO, VALID_DATA_TYPES
  23.  
  24. from emerald import help, db_create, db_migrate
  25. from emerald import db_upgrade, db_downgrade, db_version
  26. from emerald import install_package
  27. from emerald import add_controller, add_model
  28. from emerald import run_tornado, run_gunicorn, run_testrun
  29.  
  30. if len(sys.argv) > 1:
  31.     sysinput = sys.argv[1].lower()
  32.  
  33.     if sysinput == '--help' or sysinput == '-h':
  34.         help()
  35.  
  36.     elif sysinput == '--add' or sysinput == '-a':
  37.         if sys.argv[2]:
  38.             install_package(sys.argv[2])
  39.         else:
  40.             print "You need to specify a valid package name"
  41.             sys.exit()
  42.  
  43.     elif sysinput == '--create' or sysinput == '-c':
  44.         if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
  45.             db_create()
  46.         else:
  47.             print '\nPrevious database version found. Please use -m or --migrate option\n'
  48.             sys.exit()
  49.  
  50.     elif sysinput == '--migrate' or sysinput == '-m':
  51.         db_migrate()
  52.  
  53.     elif sysinput == '--initiate' or sysinput == '-i':
  54.         if len(sys.argv) > 2 and not sys.argv[2].isdigit():
  55.             add_controller(sys.argv[2])
  56.         else:
  57.             print 'Controller name can not be a number'
  58.             sys.exit()
  59.  
  60.     elif sysinput == '--upgrade' or sysinput == '-u':
  61.         db_upgrade()
  62.  
  63.     elif sysinput == '--version' or sysinput == '-v':
  64.         db_version()
  65.  
  66.     elif sysinput == '--serve' or sysinput == '-s':
  67.         run_tornado()
  68.  
  69.     elif sysinput == '--gserve' or sysinput == '-g':
  70.         if len(sys.argv) > 2:
  71.             run_gunicorn(sys.argv[2:])
  72.         else:
  73.             run_gunicorn()
  74.        
  75.  
  76.     elif sysinput == '--testrun' or sysinput == '-t':
  77.         run_testrun()
  78.  
  79.  
  80.     elif sysinput == '--downgrade' or sysinput == '-d':
  81.         if len(sys.argv) > 2 and sys.argv[2].isdigit():
  82.             db_downgrade(sys.argv[2])
  83.         else:
  84.             db_downgrade()
  85.  
  86.     elif sysinput == '--new' or sysinput == '-n':
  87.         if len(sys.argv) > 2:
  88.             model_name = sys.argv[2].title()
  89.             raw_component = sys.argv[3:]
  90.             model_components = []
  91.             if raw_component:
  92.                 for component in raw_component:
  93.                     raw_field = component.split(':')
  94.                     field_name = raw_field[0]
  95.                     detail_components = raw_field[1].split('--')
  96.                     if detail_components[0].lower() == 'string':
  97.                         if len(detail_components) <= 2:
  98.                             detail_components.append('50')
  99.                             insert_components = {
  100.                                 'field_name': field_name,
  101.                                 'field_property': detail_components
  102.                             }
  103.                     elif detail_components[0].lower() in VALID_DATA_TYPES:
  104.                         insert_components = {
  105.                             'field_name': field_name,
  106.                             'field_property': detail_components
  107.                         }
  108.                     else:
  109.                         print '\n' + detail_components[0].lower() + ' is not a valid data type.'
  110.                         sys.exit()
  111.                     model_components.append(insert_components)
  112.                 add_model(model_name, model_components)
  113.             else:
  114.                 print '\nNot enough parameters are provided. Model requires field definitions. See box.py -h for info\n'
  115.                 sys.exit()
  116.         else:
  117.             print '\nNot enough parameters are provided. See box.py -h for info\n'
  118.  
  119.     else:
  120.         print '\nCommand not found. Please use --help for command options\n'
  121.  
  122. else:
  123.     print '\nNot enough parameters found. Please run box.py -h for complete explanations\n'
  124.  
  125.  
  126. # end of file

Raw Paste


Login or Register to edit or fork this paste. It's free.