def create_app(*config_cls) -> Flask: # preprocess.preprocess_deepec() # preprocess.preprocess_ecpred() # preprocess.preprocess_ecami() # preprocess.preprocess_detect_v2() if len(config_cls) == 0: config_cls = (ProductionLevelConfig, RemoteConfig) config_cls = [ config() if isinstance(config, type) else config for config in config_cls ] log( message="Flask application initialized with {}".format(", ".join( [config.__class__.__name__ for config in config_cls])), keyword="INFO", ) flask_app = Flask(__name__) for config in config_cls: flask_app.config.from_object(config) register_extensions(flask_app) register_views(flask_app) register_hooks(flask_app) with flask_app.app_context(): c = flask_app.config Database(c['DB_HOST'], c['DB_USER'], c['DB_PASSWORD'], c['DB_DATABASE']) return flask_app
def create_app(*config_cls) -> Flask: log(message='Flask application initialized with {}'.format(', '.join( [config.__name__ for config in config_cls])), keyword='INFO') flask_app = Flask(__name__) for config in config_cls: flask_app.config.from_object(config) connect(**flask_app.config['MONGODB_SETTINGS']) CORS(flask_app, resources={ r"*": { "origin": "*" }, }) swagger = Swagger(template=Config.template) swagger.init_app(flask_app) JWTManager().init_app(flask_app) Router().init_app(flask_app) return flask_app
def flask_app() -> Flask: app = create_app(TestConfig) app_context = app.app_context() app_context.push() log(message="flask app created", keyword="INFO") yield app log(message="flask app released", keyword="INFO") app_context.pop()
def preprocess_ecami(): log(message=f"Check eCAMI Program...", keyword="INFO") vendor_path = get_vendor_path() ecami_path = get_ecami_path() # region [ eCAMI Program Check ] if not os.path.exists(ecami_path): # Cloning eCAMI Program from github log(message=f"Cloning eCAMI Program...", keyword="INFO") git.Git(vendor_path).clone("https://github.com/yinlabniu/eCAMI.git") subprocess.run(f"cd {vendor_path}; mv eCAMI ecami", shell=True) # endregion # region [ eCAMI Python Venv Check ] log(message=f"Check eCAMI Venv...", keyword="INFO") if not os.path.exists(f"{ecami_path}/venv"): # Create venv subprocess.call(f"{sys.executable} -m venv {ecami_path}/venv", shell=True) venv_python_path = ecami_path + "/venv/bin/python" # Update pip subprocess.call(f"{venv_python_path} -m pip install -U pip", shell=True) # Install eCAMI dependencies dependencies = set() dependencies.add("scipy") dependencies.add("argparse") dependencies.add("psutil") dependencies.add("numpy") subprocess.call(f"{venv_python_path} -m pip install {' '.join(dependencies)}", shell=True) # endregion log(message=f"eCAMI Path: {ecami_path}", keyword="INFO")
def create_app(*config_cls) -> Flask: log(message='Flask application initialized with {}'.format(', '.join([config.__name__ for config in config_cls])), keyword='INFO') flask_app = Flask(__name__) for config in config_cls: flask_app.config.from_object(config) register_extensions(flask_app) register_views(flask_app) register_hooks(flask_app) return flask_app
def create_app(*config_cls) -> Sanic: log(message='Sanic application initialized with {}'.format(', '.join([config.__name__ for config in config_cls])), keyword='INFO') sanic_app = Sanic(__name__) for config in config_cls: sanic_app.config.from_object(config) register_extensions(sanic_app) register_views(sanic_app) register_hooks(sanic_app) return sanic_app
def preprocess_detect_v2(): log(message=f"Check DETECTv2 Program...", keyword="INFO") vendor_path = get_vendor_path() detect_v2_path = get_detect_v2_path() # region [ DETECTv2 Program Check ] if not os.path.exists(detect_v2_path): os.makedirs(detect_v2_path) # Download DETECTv2 Program log(message=f"Download DETECTv2 Program...", keyword="INFO") detect_v2_url = "https://download.cpslab.tech/DETECTv2.tar.gz" response = requests.get(detect_v2_url, stream=True) total_size_in_bytes = int(response.headers.get('content-length', 0)) block_size = 1024 progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True) with open(detect_v2_path + "/DETECTv2.tar.gz", "wb") as file: for data in response.iter_content(block_size): progress_bar.update(len(data)) file.write(data) progress_bar.close() if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes: raise OSError("DETECTv2 download error") # Decompress DETECTv2 Program if platform.system() == "Linux": subprocess.run(f"tar -xvzf {detect_v2_path}/DETECTv2.tar.gz -C {detect_v2_path}; " f"cd {detect_v2_path}; " f"mv {detect_v2_path}/DETECTv2/* {detect_v2_path}; " f"rm -rf {detect_v2_path}/DETECTv2", shell=True) else: raise OSError("Unsupported OS") # delete origin file os.remove(detect_v2_path + "/DETECTv2.tar.gz") # endregion # region [ DETECTv2 Python Venv Check ] log(message=f"Check DETECTv2 Venv...", keyword="INFO") if not os.path.exists(f"{detect_v2_path}/venv"): executable = "/usr/bin/python" # Install Virtualenv subprocess.run(f"{executable} -m pip install -U pip; {executable} -m pip install virtualenv", shell=True) # Create venv subprocess.run(f"{executable} -m virtualenv {detect_v2_path}/venv --python=python2.7", shell=True) venv_python_path = detect_v2_path + "/venv/bin/python" # Update pip subprocess.run(f"{venv_python_path} -m pip install -U pip", shell=True) # Install DETECTv2 dependencies dependencies = set() dependencies.add("biopython==1.76") subprocess.run(f"{venv_python_path} -m pip install {' '.join(dependencies)}", shell=True) # endregion log(message=f"DETECTv2 Path: {detect_v2_path}", keyword="INFO")
def pay(cls, rfid: str, booth_id: int, point: int): from app.models.history import HistoryTable user = RFIDTable.get_info_by_rfid(rfid) # if HistoryTable.is_used(rfid, booth_id): # raise UsedBoothException() # remain_point = user.get('point') + point # level = user.get('level') # pay_level = RFIDTable.is_payable(remain_point, level) if user.get('level') == 0: query = RFIDTable.update(point=point).where(RFIDTable.rfid == rfid) db.execute_sql(str(query)) name = user['name'] pay_point = point - user.get('point') log(message=f'{name}님이 {booth_id}번 부스에서 {pay_point} 포인트를 결제했습니다.') HistoryTable.set_history(rfid, booth_id, point=pay_point)
def db() -> Dict[Engine, sessionmaker]: engine = create_engine(TestConfig.SQLALCHEMY_DATABASE_URI, echo=True) session = sessionmaker(bind=engine) _db = {'engine': engine, 'session': session} try: alembic_config = AlembicConfig(os.path.abspath("../../alembic.ini")) alembic_config.set_main_option('script_location', os.path.abspath("../../meant_alembic")) alembic_config.set_main_option('sqlalchemy.url', TestConfig.SQLALCHEMY_DATABASE_URI) alembic_upgrade(alembic_config, 'head') except CommandError: log(message="testing only specified TCs", keyword="INFO") alembic_config = AlembicConfig(os.path.abspath("../../../alembic.ini")) alembic_config.set_main_option( 'script_location', os.path.abspath("../../../meant_alembic")) alembic_config.set_main_option('sqlalchemy.url', TestConfig.SQLALCHEMY_DATABASE_URI) alembic_upgrade(alembic_config, 'head') log(message="database created", keyword="INFO") yield _db log(message="database disposed", keyword="INFO") engine.dispose()
def create_app(*config_cls) -> Flask: config_cls = [ config() if isinstance(config, type) else config for config in config_cls ] log( message="Flask application initialized with {}".format( ", ".join([config.__class__.__name__ for config in config_cls]) ), keyword="INFO", ) flask_app = Flask(__name__) for config in config_cls: flask_app.config.from_object(config) register_extensions(flask_app) register_views(flask_app) register_hooks(flask_app) return flask_app
def session(db: Dict[Engine, sessionmaker]) -> Session: session: Session = db['session']() g.db = session log(message="database session created", keyword="INFO") yield session log(message="database session closed", keyword="INFO") session.rollback() i: str for i in [ "idea_has_tag", "funding_has_tag", "funding_has_idea", "tag", "funding", "idea", "order", "funding_status", ]: session.execute('DELETE FROM meant.{}'.format(i)) session.close()
def preprocess_ecpred(): log(message=f"Check ECPred Program...", keyword="INFO") ecpred_path = get_ecpred_path() # region [ ECPred Program Check ] if not os.path.exists(ecpred_path): # Create ECPred Folder log(message=f"Create ECPred Folder to {ecpred_path}", keyword="INFO") os.makedirs(ecpred_path) # Download ECPred Program log(message=f"Download ECPred Program...", keyword="INFO") ecpred_url = "https://download.cpslab.tech/ECPred.tar.gz" response = requests.get(ecpred_url, stream=True) total_size_in_bytes = int(response.headers.get('content-length', 0)) block_size = 1024 progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True) with open(ecpred_path + "/ECPred.tar.gz", "wb") as file: for data in response.iter_content(block_size): progress_bar.update(len(data)) file.write(data) progress_bar.close() if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes: raise OSError("ECPred download error") # Decompress Diamond Program if platform.system() == "Linux": subprocess.run(f"tar -xvzf {ecpred_path}/ECPred.tar.gz -C {ecpred_path}", shell=True) else: raise OSError("Unsupported OS") # delete origin file os.remove(ecpred_path + "/ECPred.tar.gz") # move folder subprocess.run(f"mv {ecpred_path}/ECPred/* {ecpred_path}", shell=True) # delete temp folder os.removedirs(f"{ecpred_path}/ECPred") # install ECPred subprocess.run(f"cd {ecpred_path}; ./runLinux.sh", shell=True) # endregion log(message=f"ECPred Path: {ecpred_path}", keyword="INFO")
def preprocess_deepec(): vendor_path = get_vendor_path() # region [ DeepEC Program Check ] log(message=f"Check DeepEC Program...", keyword="INFO") deepec_path = os.path.join(vendor_path, "deepec") if not os.path.exists(deepec_path): # Cloning DeepEC Program from bitbucket log(message=f"Cloning DeepEC Program...", keyword="INFO") git.Git(vendor_path).clone("https://bitbucket.org/kaistsystemsbiology/deepec.git") # endregion # region [ DeepEC Python Venv Check ] log(message=f"Check Diamond Program in DeepEC...", keyword="INFO") if not os.path.exists(f"{deepec_path}/venv"): # Create venv subprocess.call(f"{sys.executable} -m venv {deepec_path}/venv", shell=True) venv_python_path = deepec_path + "/venv/bin/" + "python" if platform.system() == "Linux" else "python.exe" # Update pip subprocess.call(f"{venv_python_path} -m pip install -U pip", shell=True) # Install DeepEC dependencies dependencies = set() dependencies.add("tensorflow==1.5.0") dependencies.add("numpy==1.16.2") dependencies.add("biopython==1.78") dependencies.add("h5py==2.7.1") dependencies.add("keras==2.1.6") dependencies.add("markdown==2.6.11") dependencies.add("mock==2.0.0") dependencies.add("pandas==0.19.2") dependencies.add("scikit-learn==0.19.0") dependencies.add("scipy==1.1.0") subprocess.call(f"{venv_python_path} -m pip install {' '.join(dependencies)}", shell=True) # endregion # region [ Diamond Program Check ] log(message=f"Check Diamond Program...", keyword="INFO") diamond_path = os.path.join(deepec_path, "diamond") if platform.system() == "Linux": file_name = "/diamond" elif platform.system() == "Windows": file_name = "/diamond.exe" else: raise OSError("Unsupported OS") if not os.path.exists(diamond_path + file_name): if not os.path.exists(diamond_path): os.makedirs(diamond_path) # Download Diamond Program from github log(message=f"Download Diamond Program...", keyword="INFO") if platform.system() == "Linux": diamond_url = "https://download.cpslab.tech/diamond-linux64.tar.gz" compressed_file_name = "diamond-linux64.tar.gz" elif platform.system() == "Windows": diamond_url = "https://download.cpslab.tech/diamond-windows.zip" compressed_file_name = "diamond-windows.zip" else: raise OSError("Unsupported OS") response = requests.get(diamond_url, stream=True) total_size_in_bytes = int(response.headers.get('content-length', 0)) block_size = 1024 progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True) with open(diamond_path + "/" + compressed_file_name, "wb") as file: for data in response.iter_content(block_size): progress_bar.update(len(data)) file.write(data) progress_bar.close() if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes: raise OSError("Diamond download error") # Decompress Diamond Program if platform.system() == "Linux": subprocess.run(f"tar -xvzf {diamond_path + '/' + compressed_file_name} -C {diamond_path}", shell=True) elif platform.system() == "Windows": import zipfile diamond_zip = zipfile.ZipFile(diamond_path + "/" + compressed_file_name) diamond_zip.extractall(diamond_path) diamond_zip.close() else: raise OSError("Unsupported OS") os.remove(diamond_path + "/" + compressed_file_name) sys.path.append(diamond_path) # endregion log(message=f"DeepEC Path: {deepec_path}", keyword="INFO") log(message=f"Diamond Path: {diamond_path}", keyword="INFO")
def flask_client(flask_app: Flask) -> FlaskClient: log(message="flask test client created", keyword="INFO") return flask_app.test_client()
import optparse from app import create_app from app.misc.log import log from config.dev import DevConfig from config.production import ProductionConfig if __name__ == '__main__': parser = optparse.OptionParser() parser.add_option("-d", "--debug", action="store_true", dest="debug", help=optparse.SUPPRESS_HELP) options, _ = parser.parse_args() if options.debug: app = create_app(DevConfig) else: print(options.debug) app = create_app(ProductionConfig) if 'SECRET_KEY' not in os.environ: log(message='SECRET KEY is not set in the environment variable.', keyword='WARN') app.run(**app.config['RUN_SETTING'])