def init_voice_verify(): """ initial voice verify service Example for config.py: "voice_verify": { "enabled": True, "provider": "rong_lian", "rong_lian": { ... ... } } """ provider_name = safe_get_config("voice_verify.provider", None) enabled = safe_get_config("voice_verify.enabled", False) if not enabled: log.warn("voice verify disabled") factory.provide("voice_verify", DisabledVoiceVerify) elif provider_name and safe_get_config("voice_verify." + provider_name, None): log.warn("Voice verify initialized to:" + provider_name) # if provider other than Ronglian is supported, update following lines factory.provide("voice_verify", RonglianVoiceVerify) else: log.warn( "either voice verify provider name or provider config is missing, Please check your configuration" ) raise ConfigurationException("voice_verify.provider")
def __init__(self, app): """Initialize APScheduler :type app: Flask :param app: the Flask app """ self.app = app self.__apscheduler = None # NOT instantiate while in flask DEBUG mode or in the main thread # It's to avoid APScheduler being instantiated twice if not app.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true": self.__apscheduler = BackgroundScheduler(timezone=utc) # add MySQL job store job_store_type = safe_get_config("scheduler.job_store", "memory") if job_store_type == "mysql": log.debug("add aps_cheduler job store based on mysql") self.__apscheduler.add_jobstore('sqlalchemy', alias=self.jobstore, url=get_config("scheduler.job_store_url")) elif job_store_type == "mongodb": log.debug("add aps_cheduler job store based on mongodb") self.__apscheduler.add_jobstore('mongodb', alias=self.jobstore, database=safe_get_config("scheduler.database", "apscheduler"), collection=safe_get_config("scheduler.collection", "jobs"), host=safe_get_config("scheduler.host", "localhost"), port=safe_get_config("scheduler.port", 27017)) # add event listener self.__apscheduler.add_listener(scheduler_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR | EVENT_JOB_ADDED) log.info("APScheduler loaded") self.__apscheduler.start()
def init_sms(): """ initial SMS service """ provider_name = safe_get_config("sms.provider", None) enabled = safe_get_config("sms.enabled", False) if not enabled: log.warn("SMS service disabled") factory.provide("sms", DisabledSms) elif provider_name and safe_get_config("sms." + provider_name, None): log.warn("SMS initialized to:" + provider_name) # if provider other than ChinaTelecom is supported, update following lines factory.provide("sms", ChinaTelecomSms) else: log.warn("Either SMS provider name or provider config is missing, Please check your configuration") raise ConfigurationException("sms.provider")
def __init__(self, app): """Initialize APScheduler :type app: Flask :param app: the Flask app """ self.app = app self.__apscheduler = None # NOT instantiate while in flask DEBUG mode or in the main thread # It's to avoid APScheduler being instantiated twice if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true': self.__apscheduler = BackgroundScheduler(timezone=utc) # add MySQL job store if safe_get_config("scheduler.job_store", "memory") == "mysql": self.jobstore = 'sqlalchemy' self.__apscheduler.add_jobstore( self.jobstore, url=get_config("scheduler.job_store_url")) # add event listener self.__apscheduler.add_listener( scheduler_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) log.info("APScheduler loaded") self.__apscheduler.start()
def __init__(self, app): """Initialize APScheduler :type app: Flask :param app: the Flask app """ self.app = app self.__apscheduler = None # NOT instantiate while in flask DEBUG mode or in the main thread # It's to avoid APScheduler being instantiated twice if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true': self.__apscheduler = BackgroundScheduler(timezone=utc) # add MySQL job store if safe_get_config("scheduler.job_store", "memory") == "mysql": log.debug("add aps_cheduler job store based on mysql") self.__apscheduler.add_jobstore('sqlalchemy', alias=self.jobstore, url=get_config("scheduler.job_store_url")) # add event listener self.__apscheduler.add_listener(scheduler_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) log.info("APScheduler loaded") self.__apscheduler.start()
def __init__(self, app): """Initialize APScheduler :type app: Flask :param app: the Flask app """ self.app = app self.__apscheduler = None # NOT instantiate while in flask DEBUG mode or in the main thread # It's to avoid APScheduler being instantiated twice if not app.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true": self.__apscheduler = BackgroundScheduler(timezone=utc) # add MySQL job store job_store_type = safe_get_config("scheduler.job_store", "memory") if job_store_type == "mysql": log.debug("add aps_cheduler job store based on mysql") self.__apscheduler.add_jobstore( 'sqlalchemy', alias=self.jobstore, url=get_config("scheduler.job_store_url")) elif job_store_type == "mongodb": log.debug("add aps_cheduler job store based on mongodb") self.__apscheduler.add_jobstore( 'mongodb', alias=self.jobstore, database=safe_get_config("scheduler.database", "apscheduler"), collection=safe_get_config("scheduler.collection", "jobs"), host=safe_get_config("scheduler.host", "localhost"), port=safe_get_config("scheduler.port", 27017)) # add event listener self.__apscheduler.add_listener( scheduler_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR | EVENT_JOB_ADDED) log.info("APScheduler loaded") self.__apscheduler.start()
sys.path.append("..") from sqlalchemy import create_engine, exc, event, select from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base # noinspection PyUnresolvedReferences from flask.ext.sqlalchemy import BaseQuery from db_adapters import SQLAlchemyAdapter from hackathon.util import safe_get_config MYSQL_CONNECTION = 'mysql.connection' DEFAULT_CONNECTION_URL = 'mysql://*****:*****@localhost/hackathon' engine = create_engine(safe_get_config(MYSQL_CONNECTION, DEFAULT_CONNECTION_URL), convert_unicode=True, pool_size=50, pool_recycle=3600, max_overflow=100, echo=False) @event.listens_for(engine, "engine_connect") def ping_connection(connection, branch): if branch: # "branch" refers to a sub-connection of a connection, # we don't want to bother pinging on these. return try:
from hackathon.hackathon_scheduler import HackathonScheduler from hackathon.hackathon_response import * from hackathon.hackathon_exception import * from hackathon.log import log from hackathon.context import Context __all__ = [ "app", "Context", "RequiredFeature", "Component", ] # initialize flask and flask restful app = Flask(__name__) app.config['SECRET_KEY'] = safe_get_config("app.secret_key", "secret_key") class HackathonApi(Api): """Customize Api to give a chance to handle exceptions in framework level. So that our restful APIs will always respond with code 200 even if Exception thrown and not caught in our codes We can raise HTTPException and it's inheritances directly in components, they will be caught here. Now we have two ways to response with error: - return bad_request("some message") - raise Bad_Request("some message") You can decide to use either way ,they are of the same. """ def handle_error(self, e): log.error(e)
# -*- coding: utf-8 -*- """ This file is covered by the LICENSING file in the root of this project. """ import sys sys.path.append("..") from mongoengine import connect from hackathon.util import safe_get_config __all__ = ["db", "drop_db"] mongodb_host = safe_get_config("mongodb.host", "localhost") mongodb_port = safe_get_config("mongodb.port", 27017) ohp_db = safe_get_config("mongodb.database", "hackathon") # mongodb client client = connect(ohp_db, host=mongodb_host, port=mongodb_port) # mongodb collection for OHP, authentication disabled for now. db = client[ohp_db] # db.authenticate('user', 'password', mechanism='SCRAM-SHA-1') def drop_db(): client.drop_database(ohp_db)