entrepreneur-interet-general / OpenScraper

An open source webapp for scraping: towards a public service for webscraping
http://www.cis-openscraper.com/
MIT License
93 stars 22 forks source link

better switch when running main #50

Closed JulienParis closed 5 years ago

JulienParis commented 5 years ago

add a CLI to main function in order to switch between default config files (settings_example.py / settings_secret.py, ...)

JulienParis commented 5 years ago

solution proposed :

### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
### IMPORT COMMAND LINE INTERFACE (CLI) 
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
# cf : http://click.pocoo.org/5/ 
import click

then add a decorator for def main(): to use variable"mode" to switch between config files... like

@click.command()
@click.option('--mode', default="dev",  nargs=1,    help="The <mode> you need to run the app : dev, prod, dev_email" )
def main():
    """
    start / run app
    """
JulienParis commented 5 years ago

I just realized I had already put something similar in the main function, but not at an effective place... So I will certainly patch it (without using click) putting that part upper in the main.py file :

# # define default port to listen to + mode
define( "port", default=8000,   help="run on the given port", type=int )
define( "mode", default='default',  help="mode for run : default, production", type=str )

# parse command line arguments if any port arg
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', type=int, default=8000)
parser.add_argument('-m', '--mode', type=str, default="default") ### choices : "default" | "production"
args = parser.parse_args()

# update port with args.port
options.port = args.port
options.mode = args.mode

print "--- default options : \n",  pformat(options.__dict__)
print "--- mode : ",  options.mode 
print "--- port : ",  options.port 

and only then :

print( "---------- MAIN.PY : import settings -------- " )
### import app settings from .config.settings (keep that file confidential)
from config.settings_corefields import * 

finally at the very beginning of settings_scrapy.py :

from tornado.options import options

### for debugging purposes
print "current port from options : ", options.port 
print "current mode from options : ", options.mode 

if options.mode == "default" :
    from settings_example import *
if options.mode == "production" :
    from settings_secret import *

So that way it should be possible to run the main.py from command line with a correct comprehension of the arguments (-p / --port, -m / --mode), like :

(venv)~/openscraper$ python main.py -p 8100 --mode=production"

JulienParis commented 5 years ago

corrected with this commit : https://github.com/entrepreneur-interet-general/OpenScraper/commit/a5ded0f465b2c5b9f3dd2c6cedc43f4f85cf367e