Esempio n. 1
0
def basicConfig(outputFile=None,
                level='INFO',
                redirectLogging=False,
                colorized=False,
                loop=None):
    if outputFile:
        if platform.system() == 'Linux' and 0:
            handler = AsyncDatedFileHandler(outputFile, date_format='%Y-%m-%d')
        else:
            handler = TimedRotatingFileHandler(outputFile,
                                               level=level,
                                               bubble=True,
                                               date_format='%Y-%m-%d')
    else:
        if not colorized:
            handler = StreamHandler(sys.stderr, level=level, bubble=True)
        else:
            handler = ColorizedHandler(level=level, bubble=True)
            handler.force_color()

    handler.format_string = mainFormatString
    handler.push_application()

    if redirectLogging:
        redirect_logging()
        redirect_warnings()
def main():
    if args.debug:
        out_dir = '/tmp'
        log_level = 'DEBUG'
    else:
        out_dir = args.out_dir
        log_level = 'INFO'

    inputs = glob.glob(args.inputs)

    mbytes = estimate_bytes(inputs) // (10 ** 6)
    out_fileroot = get_output_fileroot(
        out_dir,
        'dna2vec',
        'k{}to{}-{}d-{}c-{}Mbp-{}'.format(
            args.k_low,
            args.k_high,
            args.vec_dim,
            args.context,
            mbytes * args.epochs,  # total Mb including epochs
            args.kmer_fragmenter))

    out_txt_filename = '{}.txt'.format(out_fileroot)
    with open(out_txt_filename, 'w') as summary_fptr:
        with Tee(summary_fptr):
            logbook.StreamHandler(sys.stdout, level=log_level).push_application()
            redirect_logging()
            run_main(args, inputs, out_fileroot)
Esempio n. 3
0
File: cmd.py Progetto: mbr/githome
def cli(ctx, githome, loglevel):
    ctx.obj = {}

    if loglevel is None:
        loglevel = logbook.INFO

    # setup sqlalchemy loglevel
    if loglevel is logbook.DEBUG:
        redirect_logging()
        logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)

    # setup console logging
    NullHandler().push_application()
    handler = ColorizedStderrHandler(level=loglevel)
    handler.format_string = '{record.channel}: {record.message}'
    handler.push_application()

    ctx.obj['githome_path'] = pathlib.Path(githome)

    # if we're just calling init, do not initialize githome
    if ctx.invoked_subcommand == 'init':
        return

    # check if the home is valid
    if not GitHome.check(ctx.obj['githome_path']):
        log.critical('Not a valid githome: "{}"; use {} init to initialize it '
                     'first.'.format(githome, 'githome'))
        abort(1)

    # create and add to context
    gh = GitHome(ctx.obj['githome_path'])
    ctx.obj['githome'] = gh
Esempio n. 4
0
def create_app(config=None):
    if config is None:
        config = {}

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__, static_folder=os.path.join(ROOT_DIR, "..", "static"))

    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY', os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(sorted(os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH) if x.endswith(".yml")))

    configs.append(os.path.expanduser('~/.config/backslash/devconfig.yml'))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    if 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = os.path.expandvars(
            os.environ.get('SQLALCHEMY_DATABASE_URI', 'postgresql://localhost/{0}'.format(app.config['app_name'])))


    if os.path.exists("/dev/log"):
        syslog_handler = logbook.SyslogHandler(app.config['app_name'], "/dev/log")
        syslog_handler.push_application()

    del app.logger.handlers[:]
    redirect_logging()

    app.logger.info("Started")

    Mail(app)

    from . import models
    from .blueprints import rest, api, views, runtoken

    models.db.init_app(app)

    from . import auth
    Security(app, auth.user_datastore, register_blueprint=False)

    from .setup import setup
    blueprints = [auth.auth, views.blueprint, setup, api.blueprint, rest.blueprint, runtoken.blueprint]

    from .errors import errors

    for blueprint in blueprints:
        app.register_blueprint(blueprint)

    for code in errors:
        app.errorhandler(code)(errors[code])

    return app
Esempio n. 5
0
File: cli.py Progetto: mbr/legitfs
def main(mountpoint, root, debug, fuse_debug):
    if fuse_debug:
        redirect_logging()

    # setup logging
    StderrHandler(level=DEBUG if debug else INFO).push_application()

    log.info('mounting {} onto {}'.format(root, mountpoint))
    fuse.FUSE(LegitFS(root, mountpoint), mountpoint, foreground=True)
Esempio n. 6
0
def basicConfig(level='INFO', redirectLogging=False, colorized=False):
    if not colorized:
        handler = StreamHandler(sys.stderr, level=level, bubble=True)
    else:
        handler = ColorizedHandler(level=level, bubble=True)
        handler.force_color()

    handler.format_string = mainFormatString
    handler.push_application()

    if redirectLogging:
        redirect_logging()
        redirect_warnings()
Esempio n. 7
0
    def __init__(self, *args, **kwargs):
        super().__init__(command_prefix=when_mentioned_or(setup_file["discord"]["command_prefix"]),
                         description="A bot for weebs programmed by Recchan")

        # Set a custom user agent for Pixie
        self.http.user_agent = user_agent

        # Logging setup
        redirect_logging()
        StreamHandler(sys.stderr).push_application()
        self.logger = Logger("Pixie")
        self.logger.level = getattr(logbook, setup_file.get("log_level", "INFO"), logbook.INFO)
        logging.root.setLevel(self.logger.level)
Esempio n. 8
0
def init():
    import logging
    from logbook.compat import redirect_logging

    from django_logbook import handlers

    db_handler = handlers.LogbookDjangoDatabaseHandler(
        level=settings.LOG_LEVEL_DB_HANDLER)
    db_handler.push_application()

    redirect_logging()  # redirect stdlib logging to logbook

    # this should be done after redirect_logging to be effective
    root = logging.getLogger()
    root.setLevel(settings.LOG_LEVEL_LOGGING_GLOBAL)
Esempio n. 9
0
def init():
    import logging
    from logbook.compat import redirect_logging

    from django_logbook import handlers

    db_handler = handlers.LogbookDjangoDatabaseHandler(
        level=settings.LOG_LEVEL_DB_HANDLER)
    db_handler.push_application()

    redirect_logging()  # redirect stdlib logging to logbook

    # this should be done after redirect_logging to be effective
    root = logging.getLogger()
    root.setLevel(settings.LOG_LEVEL_LOGGING_GLOBAL)
Esempio n. 10
0
def main(mountpoint, root, debug, fuse_debug):
    if fuse_debug:
        redirect_logging()

    # setup logging
    StderrHandler(level=DEBUG if debug else INFO).push_application()

    log.info('mounting index of {} onto {}'.format(root, mountpoint))

    try:
        repo = Repo(root)
    except NotGitRepository:
        log.info('Error: {} is not a git repository'.format(root))
        sys.exit(1)

    fuse.FUSE(IndexFS(root, repo, mountpoint), mountpoint, foreground=False)
Esempio n. 11
0
    def __init__(self, *args, **kwargs):
        super().__init__(command_prefix=when_mentioned_or(
            setup_file["discord"]["command_prefix"]),
                         description="A bot for weebs programmed by Recchan")

        # Set a custom user agent for Pixie
        self.http.user_agent = user_agent

        # Logging setup
        redirect_logging()
        StreamHandler(sys.stderr).push_application()
        self.logger = Logger("Pixie")
        self.logger.level = getattr(logbook,
                                    setup_file.get("log_level",
                                                   "INFO"), logbook.INFO)
        logging.root.setLevel(self.logger.level)
Esempio n. 12
0
def main():
    argp = configargparse.get_argument_parser()
    argp.add('-c', is_config_file=True, help='config file path')
    argp.add_argument('--kmer-fragmenter', help='disjoint or sliding', choices=['disjoint', 'sliding'], default='sliding')
    argp.add_argument('--vec-dim', help='vector dimension', type=int, default=12)
    argp.add_argument('--rseed', help='general np.random seed', type=int, default=7)
    argp.add_argument('--rseed-trainset', help='random seed for generating training data', type=int, default=123)
    argp.add_argument('--inputs', help='FASTA files', nargs='+', required=True)
    argp.add_argument('--k-low', help='k-mer start range (inclusive)', type=int, default=5)
    argp.add_argument('--k-high', help='k-mer end range (inclusive)', type=int, default=5)
    argp.add_argument('--context', help='half size of context window (the total size is 2*c+1)', type=int, default=4)
    argp.add_argument('--epochs', help='number of epochs', type=int, default=1)
    argp.add_argument('--workers', help='number of workers', type=int, default=4)
    argp.add_argument('--out-dir', help="output directory", default='.')
    argp.add_argument('--debug', help='', action='store_true')
    args = argp.parse_args()

    if args.debug:
        out_dir = '/tmp'
        log_level = 'DEBUG'
    else:
        out_dir = args.out_dir
        log_level = 'INFO'

    inputs = []
    for s in args.inputs:
        inputs.extend(list(glob.glob(s)))

    mbytes = util.estimate_bytes(inputs) // (10 ** 6)
    out_fileroot = util.get_output_fileroot(
        out_dir,
        'dna2vec',
        'k{}to{}-{}d-{}c-{}Mbp-{}'.format(
            args.k_low,
            args.k_high,
            args.vec_dim,
            args.context,
            mbytes * args.epochs,  # total Mb including epochs
            args.kmer_fragmenter))

    out_txt_filename = '{}.txt'.format(out_fileroot)
    print(out_txt_filename)
    # with open(out_txt_filename, 'w') as summary_fptr:
    # with Tee(summary_fptr):
    logbook.TimedRotatingFileHandler(out_txt_filename, level=log_level).push_application()
    redirect_logging()
    run_main(args, inputs, out_fileroot)
Esempio n. 13
0
def cli(ctx, debug, logtostderr):
    """
    Matrix: A distributed ABM platform.
    """

    cfg = AttrDict()
    ctx.obj = cfg

    if logtostderr:
        if debug:
            handler = logbook.StderrHandler(logbook.DEBUG)
            handler.push_application()
        else:
            handler = logbook.StderrHandler(logbook.INFO)
            handler.push_application()

        redirect_logging()
Esempio n. 14
0
    def init_app(self, app):
        redirect_logging()
        logging.root.level = logging.DEBUG

        def process_request():
            logger = create_logger(app)
            _request_ctx_stack.top.logbook_logger = logger
            _request_ctx_stack.top.logbook_request_id = uuid.uuid4()
            logger.push_thread()

        def teardown_request(exc):
            logger = getattr(_request_ctx_stack.top, 'logbook_logger', None)
            if logger is not None:
                logger.pop_thread()

        app.before_request(process_request)
        app.teardown_request(teardown_request)
Esempio n. 15
0
def serve():
    """Run a development server."""
    from logging        import getLogger, DEBUG
    from logbook.compat import redirect_logging

    redirect_logging()
    getLogger().setLevel(DEBUG)

    opts = options.serve
    app = _get_application()
    app.log_handler.push_application()

    from werkzeug.serving import run_simple
    run_simple(opts.hostname, int(opts.port), app,
               use_reloader = not opts.no_reloader,
               use_debugger = not opts.no_debugger,
               use_evalex   = not opts.no_evalex)
Esempio n. 16
0
    def init_app(self, app):
        redirect_logging()
        logging.root.level = logging.DEBUG

        def process_request():
            logger = create_logger(app)
            _request_ctx_stack.top.logbook_logger = logger
            _request_ctx_stack.top.logbook_request_id = uuid.uuid4()
            logger.push_thread()

        def teardown_request(exc):
            logger = getattr(_request_ctx_stack.top, 'logbook_logger', None)
            if logger is not None:
                logger.pop_thread()

        app.before_request(process_request)
        app.teardown_request(teardown_request)
Esempio n. 17
0
def run_backend(bypass_checks=False, flags_dict=None, frontend_pid=None):
    """
    Run the backend for the application.

    :param bypass_checks: whether we should bypass the checks or not
    :type bypass_checks: bool
    :param flags_dict: a dict containing the flag values set on app start.
    :type flags_dict: dict
    """
    # In the backend, we want all the components to log into logbook
    # that is: logging handlers and twisted logs
    from logbook.compat import redirect_logging
    from twisted.python.log import PythonLoggingObserver
    redirect_logging()
    observer = PythonLoggingObserver()
    observer.start()

    # NOTE: this needs to be used here, within the call since this function is
    # executed in a different process and it seems that the process/thread
    # identification isn't working 100%
    logger = get_logger()  # noqa

    # The backend is the one who always creates the certificates. Either if it
    # is run separately or in a process in the same app as the frontend.
    if flags.ZMQ_HAS_CURVE:
        generate_zmq_certificates()

    # ignore SIGINT since app.py takes care of signaling SIGTERM to us.
    signal.signal(signal.SIGINT, signal.SIG_IGN)
    signal.signal(signal.SIGTERM, signal_handler)

    if flags_dict is not None:
        dict_to_flags(flags_dict)

    # HACK we should be able to run the ensure_server anyway but right now it
    # breaks if we run it twice.
    if not flags.STANDALONE:
        # start the events server
        # This is not needed for the standalone bundle since the launcher takes
        # care of it.
        event_server.ensure_server()

    backend = LeapBackend(bypass_checks=bypass_checks,
                          frontend_pid=frontend_pid)
    backend.run()
Esempio n. 18
0
def cli(ctx, debug, logtostderr):
    """
    Bluepill agents.
    """

    cfg = AttrDict()

    ctx.obj = cfg

    if logtostderr:
        if debug:
            handler = logbook.StderrHandler(logbook.DEBUG)
            handler.push_application()
        else:
            handler = logbook.StderrHandler(logbook.INFO)
            handler.push_application()

        redirect_logging()
Esempio n. 19
0
def run_backend(bypass_checks=False, flags_dict=None, frontend_pid=None):
    """
    Run the backend for the application.
    This is called from the main app.py entrypoint, and is run in a child
    subprocess.

    :param bypass_checks: whether we should bypass the checks or not
    :type bypass_checks: bool
    :param flags_dict: a dict containing the flag values set on app start.
    :type flags_dict: dict
    """
    # In the backend, we want all the components to log into logbook
    # that is: logging handlers and twisted logs
    from logbook.compat import redirect_logging
    from twisted.python.log import PythonLoggingObserver
    redirect_logging()
    observer = PythonLoggingObserver()
    observer.start()

    if flags_dict is not None:
        dict_to_flags(flags_dict)

    common_flags.STANDALONE = flags.STANDALONE

    # NOTE: this needs to be used here, within the call since this function is
    # executed in a different process and it seems that the process/thread
    # identification isn't working 100%
    logger = get_logger()  # noqa

    # The backend is the one who always creates the certificates. Either if it
    # is run separately or in a process in the same app as the frontend.
    if flags.ZMQ_HAS_CURVE:
        generate_zmq_certificates()

    # ignore SIGINT since app.py takes care of signaling SIGTERM to us.
    signal.signal(signal.SIGINT, signal.SIG_IGN)
    signal.signal(signal.SIGTERM, signal_handler)

    reactor.callWhenRunning(start_events_and_updater, logger)

    backend = LeapBackend(bypass_checks=bypass_checks,
                          frontend_pid=frontend_pid)
    backend.run()
Esempio n. 20
0
    def __init__(self, *args, **kwargs):
        config_file = os.path.join(os.getcwd(), "config.yaml")

        with open(config_file) as f:
            self.config = yaml.load(f)

        super().__init__(*args, **kwargs)

        # Define the logging set up.
        redirect_logging()
        StreamHandler(sys.stderr).push_application()

        self.logger = Logger("Casca_Best_Bot")
        self.logger.level = getattr(logbook,
                                    self.config.get("log_level", "INFO"),
                                    logbook.INFO)

        # Set the root logger level, too.
        logging.root.setLevel(self.logger.level)

        self._loaded = False
Esempio n. 21
0
    def setup_logging(self):

        from logbook.compat import redirect_logging
        from logbook import INFO, Logger

        redirect_logging()
        self.components['log'].handler.level = INFO
        self.components['log'].handler.push_application()

        self._logger = Logger(self.name)

        def handle_exception(exc_type, exc_value, exc_traceback):

            if issubclass(exc_type, KeyboardInterrupt):
                sys.__excepthook__(exc_type, exc_value, exc_traceback)
                return

            self._logger.error("Uncaught exception occurred",
                               exc_info=(exc_type, exc_value, exc_traceback))

        sys.excepthook = handle_exception
Esempio n. 22
0
    #save_config(os.path.join(cfg.general.common.save_path, "ran_cfg"), cfg)

    # Now actually run the driving_benchmark
    #import pdb; pdb.set_trace()
    run_driving_benchmark(agent, benchmark_agent, cfg.simulator.town,
                          cfg.simulator.carla_log_name,
                          cfg.simulator.continue_experiment,
                          cfg.simulator.host, cfg.simulator.port)

    simulator.kill_process()


if __name__ == '__main__':

    # Initialize logger properties
    StreamHandler(sys.stdout).push_application()
    log.info("[MODE] Train")
    log.warn('Logbook is too awesome for most applications')

    # -- Parse config file & generate
    procs_no, arg_list = generate_configs()

    log.info("Starting simulation...")
    redirect_logging()
    if len(arg_list) > 1:
        # Run batch of experiments:
        pool = mp.Pool(procs_no)
        pool.map(run_once, arg_list)
    else:
        run_once(arg_list[0])
Esempio n. 23
0
def main():
    # Setup logging
    logging.getLogger("pdfminer").setLevel(logging.WARNING)
    logging.getLogger("ocrmypdf").setLevel(logging.WARNING)
    redirect_logging()
    format_string = "{record.level_name}: {record.message}"
    StreamHandler(sys.stdout, format_string=format_string,
                  level="INFO").push_application()
    log = Logger()

    q = mq.MessageQueue(all_burst_limit=3, all_time_limit_ms=3000)
    request = Request(con_pool_size=8)
    pdf_bot = MQBot(TELE_TOKEN, request=request, mqueue=q)

    # Create the EventHandler and pass it your bot's token.
    updater = Updater(
        bot=pdf_bot,
        use_context=True,
        request_kwargs={
            "connect_timeout": TIMEOUT,
            "read_timeout": TIMEOUT
        },
    )

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # General commands handlers
    dispatcher.add_handler(CommandHandler("start", start_msg))
    dispatcher.add_handler(CommandHandler("help", help_msg))
    dispatcher.add_handler(CommandHandler("setlang", send_lang))
    dispatcher.add_handler(
        CommandHandler("support", send_support_options_with_async))
    dispatcher.add_handler(
        CommandHandler("send", send_msg, Filters.user(DEV_TELE_ID)))
    dispatcher.add_handler(
        CommandHandler("stats", get_stats, Filters.user(DEV_TELE_ID)))

    # Callback query handler
    dispatcher.add_handler(CallbackQueryHandler(process_callback_query))

    # Payment handlers
    dispatcher.add_handler(
        MessageHandler(Filters.reply & TEXT_FILTER, receive_custom_amount))
    dispatcher.add_handler(PreCheckoutQueryHandler(precheckout_check))
    dispatcher.add_handler(
        MessageHandler(Filters.successful_payment, successful_payment))

    # URL handler
    dispatcher.add_handler(
        MessageHandler(Filters.entity(MessageEntity.URL), url_to_pdf))

    # PDF commands handlers
    dispatcher.add_handler(compare_cov_handler())
    dispatcher.add_handler(merge_cov_handler())
    dispatcher.add_handler(photo_cov_handler())
    dispatcher.add_handler(text_cov_handler())
    dispatcher.add_handler(watermark_cov_handler())

    # PDF file handler
    dispatcher.add_handler(file_cov_handler())

    # Feedback handler
    dispatcher.add_handler(feedback_cov_handler())

    # Log all errors
    dispatcher.add_error_handler(error_callback)

    # Start the Bot
    if APP_URL is not None:
        updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TELE_TOKEN)
        updater.bot.set_webhook(APP_URL + TELE_TOKEN)
        log.notice("Bot started webhook")
    else:
        updater.start_polling()
        log.notice("Bot started polling")

    # Run the bot until the you presses Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
Esempio n. 24
0
# coding=utf-8

import sys

from logbook import Logger, StreamHandler, compat
from py_dice import api_routes

if __name__ == "__main__":
    log = Logger(__name__)
    handler = StreamHandler(
        sys.stdout,
        level="INFO",
        format_string=
        "{record.channel}: [{record.level_name}] {record.message}",
    )
    compat.redirect_logging()
    handler.push_application()
    api_routes.start_api()
Esempio n. 25
0
def init_logging():
    StreamHandler(sys.stdout).push_application()
    redirect_logging()
Esempio n. 26
0
def create_app(config=None, setup_logging=True):
    if config is None:
        config = {}

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__, static_folder=None)
    app.wsgi_app = ProxyFix(app.wsgi_app)

    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY', os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(sorted(os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH) if x.endswith(".yml")))

    configs.append(os.path.expanduser('~/.config/backslash/devconfig.yml'))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    app.before_request(profile_request_start)
    app.after_request(profile_request_end)

    db_uri = os.environ.get('BACKSLASH_DATABASE_URI', None)
    if db_uri is not None or 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = db_uri or 'postgresql://localhost/{0}'.format(app.config['app_name'])

    if setup_logging:
        del app.logger.handlers[:]
        redirect_logging()

    if os.environ.get('BACKSLASH_TESTING', '').lower() in {'1', 'yes', 'true'}:
        app.config['TESTING'] = True

    if app.config['TESTING']:
        app.config['TRACEBACK_DIR'] = '/tmp/backslash_tracebacks'
    else:
        _disable_logs(['dogpile.lock'])

    logging.getLogger('urllib3').setLevel(logging.WARNING)

    if not app.config['DEBUG'] and not app.config['TESTING']:
        app.config['RAVEN_IGNORE_EXCEPTIONS'] = (HTTPException, SystemExit,)
        sentry = Sentry(app)    # pylint: disable=unused-variable

    override_tb_location = os.environ.get('BACKSLASH_TRACEBACKS_PATH', None)
    if override_tb_location:
        app.config['TRACEBACK_DIR'] = override_tb_location

    app.logger.info("Started")

    Mail(app)

    from . import models
    from .blueprints import rest, views, runtoken
    from .blueprints.api.main import blueprint as api_blueprint

    models.db.init_app(app)

    from . import auth
    Security(app, auth.user_datastore, register_blueprint=False)

    blueprints = [auth.auth, views.blueprint, api_blueprint, rest.blueprint, runtoken.blueprint]

    from .errors import errors

    for blueprint in blueprints:
        app.register_blueprint(blueprint)

    for code in errors:
        app.errorhandler(code)(errors[code])

    return app
Esempio n. 27
0
def main():
    # Setup logging
    redirect_logging()
    logbook.set_datetime_format("local")
    format_string = (
        "[{record.time:%Y-%m-%d %H:%M:%S}] {record.level_name}: {record.message}"
    )
    StreamHandler(
        sys.stdout, format_string=format_string, level="INFO"
    ).push_application()
    log = Logger()

    # Create the EventHandler and pass it your bot's token.
    updater = Updater(
        TELE_TOKEN,
        use_context=True,
        request_kwargs={"connect_timeout": TIMEOUT, "read_timeout": TIMEOUT},
    )

    # Setup job
    job_queue = updater.job_queue
    job_queue.run_repeating(delete_expired_msg, timedelta(days=MSG_LIFETIME), 0)

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # Commands handlers
    dispatcher.add_handler(CommandHandler("start", start_msg))
    dispatcher.add_handler(CommandHandler("help", help_msg))
    dispatcher.add_handler(CommandHandler("donate", send_payment_options))
    dispatcher.add_handler(CommandHandler("send", send, Filters.user(DEV_TELE_ID)))
    dispatcher.add_handler(
        CommandHandler("stats", get_stats, Filters.user(DEV_TELE_ID))
    )

    # Callback query handler
    dispatcher.add_handler(CallbackQueryHandler(process_callback_query))

    # Group Defender handlers
    dispatcher.add_handler(
        MessageHandler(Filters.status_update.new_chat_members, greet_group)
    )
    dispatcher.add_handler(
        MessageHandler(
            (
                Filters.animation
                | Filters.audio
                | Filters.document
                | Filters.photo
                | Filters.sticker
                | Filters.video
            ),
            process_file,
        )
    )
    dispatcher.add_handler(MessageHandler(Filters.entity(MessageEntity.URL), check_url))

    # Payment handlers
    dispatcher.add_handler(
        MessageHandler(
            Filters.regex(
                rf"^({re.escape(PAYMENT_THANKS)}|{re.escape(PAYMENT_COFFEE)}|{re.escape(PAYMENT_BEER)}|"
                rf"{re.escape(PAYMENT_MEAL)})$"
            ),
            send_payment_invoice,
        )
    )
    dispatcher.add_handler(payment_cov_handler())
    dispatcher.add_handler(PreCheckoutQueryHandler(precheckout_check))
    dispatcher.add_handler(
        MessageHandler(Filters.successful_payment, successful_payment)
    )

    # Feedback handler
    dispatcher.add_handler(feedback_cov_handler())

    # Log all errors
    dispatcher.add_error_handler(error_callback)

    # Start the Bot
    if APP_URL is not None:
        updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TELE_TOKEN)
        updater.bot.set_webhook(APP_URL + TELE_TOKEN)
        log.notice("Bot started webhook")
    else:
        updater.start_polling()
        log.notice("Bot started polling")

    # Run the bot until the you presses Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
Esempio n. 28
0
def create_app(config=None):
    if config is None:
        config = {}

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__, static_folder=os.path.join(ROOT_DIR, "..", "static"))

    app.config['COMBADGE_CONTACT_TIMEOUT'] = 60 * 60
    app.config['SHA512SUM'] = '/usr/bin/sha512sum'
    app.config['STORAGE_PATH'] = os.environ.get('STORAGE_PATH')
    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY', os.path.join(ROOT_DIR, "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(sorted(os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH) if x.endswith(".yml")))
    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    if 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = os.path.expandvars(
            os.environ.get('SQLALCHEMY_DATABASE_URI', 'postgresql://localhost/{0}'.format(app.config['app_name'])))

    del app.logger.handlers[:]
    redirect_logging()

    app.logger.info("Started")

    Mail(app)
    
    if os.environ.get("SQLALCHEMY_LOG_QUERIES"):
        logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)

    app.raven = Sentry(
        app,
        client=raven.Client(
            dsn=app.config.get("SENTRY_DSN"),
            ignore_exceptions=[
                SSHException,
                JIRAError
            ],
        ),
    )

    from . import models

    models.db.init_app(app)

    from .errors import errors
    from .blueprints import auth, beams, files, issues, trackers, user_datastore, users, views, keys

    Security(app, user_datastore, register_blueprint=False)

    app.register_blueprint(auth)
    app.register_blueprint(beams, url_prefix="/beams")
    app.register_blueprint(files, url_prefix="/files")
    app.register_blueprint(issues, url_prefix="/issues")
    app.register_blueprint(trackers, url_prefix="/trackers")
    app.register_blueprint(users, url_prefix="/users")
    app.register_blueprint(keys, url_prefix="/keys")
    app.register_blueprint(views)

    if app.config.get('DEBUG'):
        from .blueprints import test_methods
        app.register_blueprint(test_methods)

    for code in errors:
        app.errorhandler(code)(errors[code])

    return app
Esempio n. 29
0
def redirect_python_logging_to_logbook():
    from logbook.compat import redirect_logging
    redirect_logging()
Esempio n. 30
0
def create_app(config=None):
    if config is None:
        config = {}

    # Defaults
    config.update({
        'MAX_MAILBOX_AGE_SECONDS': 7 * 24 * 60 * 60, # 1 week
    })


    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__, static_folder=os.path.join(ROOT_DIR, "..", "static"))

    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY', os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(sorted(os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH) if x.endswith(".yml")))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    if 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = os.path.expandvars(
            os.environ.get('SQLALCHEMY_DATABASE_URI', 'postgresql://localhost/{0}'.format(app.config['app_name'])))


    if os.path.exists("/dev/log"):
        syslog_handler = logbook.SyslogHandler(app.config['app_name'], "/dev/log")
        syslog_handler.push_application()

    del app.logger.handlers[:]
    redirect_logging()

    Mail(app)

    from . import models
    from . import errors

    models.db.init_app(app)

    from . import auth
    Security(app, auth.user_datastore, register_blueprint=False)

    from .errors import errors

    from .auth import auth
    from .views import views
    from .setup import setup
    blueprints = [auth, views, setup]


    for blueprint in blueprints:
        app.register_blueprint(blueprint)

    from .api import blueprint as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix="/v2")

    for code in errors:
        app.errorhandler(code)(errors[code])
    return app
Esempio n. 31
0
def main(mountpoint, root):
    redirect_logging()
    log.debug('mounting {} onto {}'.format(root, mountpoint))

    fuse.FUSE(LegitFS(root, mountpoint), mountpoint, foreground=True)
Esempio n. 32
0
def main():
    # Setup logging
    logging.getLogger("pdfminer").setLevel(logging.WARNING)
    logging.getLogger("ocrmypdf").setLevel(logging.WARNING)
    redirect_logging()
    format_string = "{record.level_name}: {record.message}"
    StreamHandler(sys.stdout, format_string=format_string,
                  level="INFO").push_application()
    log = Logger()

    q = mq.MessageQueue(all_burst_limit=3, all_time_limit_ms=3000)
    request = Request(con_pool_size=8)
    pdf_bot = MQBot(TELE_TOKEN, request=request, mqueue=q)

    # Create the EventHandler and pass it your bot's token.
    updater = Updater(
        bot=pdf_bot,
        use_context=True,
        request_kwargs={
            "connect_timeout": TIMEOUT,
            "read_timeout": TIMEOUT
        },
    )

    def stop_and_restart():
        updater.stop()
        os.execl(sys.executable, sys.executable, *sys.argv)

    def restart(_):
        Thread(target=stop_and_restart).start()

    job_queue = updater.job_queue
    job_queue.run_repeating(restart, interval=dt.timedelta(minutes=30))

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # General commands handlers
    dispatcher.add_handler(
        CommandHandler("start",
                       send_support_options,
                       Filters.regex("support"),
                       run_async=True))
    dispatcher.add_handler(CommandHandler("start", start_msg, run_async=True))

    dispatcher.add_handler(CommandHandler("help", help_msg, run_async=True))
    dispatcher.add_handler(CommandHandler("setlang", send_lang,
                                          run_async=True))
    dispatcher.add_handler(
        CommandHandler("support", send_support_options, run_async=True))

    # Callback query handler
    dispatcher.add_handler(
        CallbackQueryHandler(process_callback_query, run_async=True))

    # Payment handlers
    dispatcher.add_handler(
        PreCheckoutQueryHandler(precheckout_check, run_async=True))
    dispatcher.add_handler(
        MessageHandler(Filters.successful_payment,
                       successful_payment,
                       run_async=True))

    # URL handler
    dispatcher.add_handler(
        MessageHandler(Filters.entity(MessageEntity.URL),
                       url_to_pdf,
                       run_async=True))

    # PDF commands handlers
    dispatcher.add_handler(compare_cov_handler())
    dispatcher.add_handler(merge_cov_handler())
    dispatcher.add_handler(photo_cov_handler())
    dispatcher.add_handler(text_cov_handler())
    dispatcher.add_handler(watermark_cov_handler())

    # PDF file handler
    dispatcher.add_handler(file_cov_handler())

    # Feedback handler
    dispatcher.add_handler(feedback_cov_handler())

    # Dev commands handlers
    dispatcher.add_handler(
        CommandHandler("send", send_msg, Filters.user(DEV_TELE_ID)))
    dispatcher.add_handler(
        CommandHandler("stats", get_stats, Filters.user(DEV_TELE_ID)))

    # Log all errors
    dispatcher.add_error_handler(error_callback)

    # Start the Bot
    updater.start_polling()
    log.notice("Bot started polling")

    # Run the bot until the you presses Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
Esempio n. 33
0
def create_app(config=None):
    if config is None:
        config = {}

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__, static_folder=os.path.join(ROOT_DIR, "..", "static"))

    app.config['COMBADGE_CONTACT_TIMEOUT'] = 60 * 60
    app.config['SHA512SUM'] = '/usr/bin/sha512sum'
    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY', os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(sorted(os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH) if x.endswith(".yml")))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    if 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = os.path.expandvars(
            os.environ.get('SQLALCHEMY_DATABASE_URI', 'postgresql://localhost/{0}'.format(app.config['app_name'])))


    if os.path.exists("/dev/log"):
        syslog_handler = logbook.SyslogHandler(app.config['app_name'], "/dev/log")
        syslog_handler.push_application()

    del app.logger.handlers[:]
    redirect_logging()

    app.logger.info("Started")

    Mail(app)

    app.raven = Sentry(app, dsn=app.config.get('SENTRY_DSN'))

    from . import models

    models.db.init_app(app)

    from .errors import errors
    from .blueprints import auth, beams, files, issues, trackers, user_datastore, users, views, keys

    Security(app, user_datastore, register_blueprint=False)

    app.register_blueprint(auth)
    app.register_blueprint(beams, url_prefix="/beams")
    app.register_blueprint(files, url_prefix="/files")
    app.register_blueprint(issues, url_prefix="/issues")
    app.register_blueprint(trackers, url_prefix="/trackers")
    app.register_blueprint(users, url_prefix="/users")
    app.register_blueprint(keys, url_prefix="/keys")
    app.register_blueprint(views)

    if app.config.get('TESTING'):
        from .blueprints import test_methods
        app.register_blueprint(test_methods)

    for code in errors:
        app.errorhandler(code)(errors[code])

    return app
Esempio n. 34
0
from collections import OrderedDict
from datetime import datetime, timedelta
from os import path

from flask import flash, Flask, request, render_template, redirect, Response, url_for

from logbook import FileHandler
from logbook.compat import redirect_logging
redirect_logging()

from kettle import settings
from kettle.db import session, make_session
from kettle.log_utils import log_filename
from kettle.rollout import ALL_SIGNALS, SIGNAL_DESCRIPTIONS

from kettleweb.middleware import ReverseProxied, RemoteUserMiddleware


app = Flask(__name__)
app.wsgi_app = ReverseProxied(RemoteUserMiddleware(app.wsgi_app))
app.secret_key = settings.SECRET_KEY
rollout_cls = settings.get_cls(settings.ROLLOUT_CLS)
rollout_form_cls = settings.get_cls(settings.ROLLOUT_FORM_CLS)

SIGNAL_LABELS = OrderedDict((sig, sig.replace('_', ' ').title()) for sig in ALL_SIGNALS)

def available_signals(rollout_id):
    result = []

    for sig, sig_label in SIGNAL_LABELS.iteritems():
        if not rollout_cls._can_signal(rollout_id, sig):
Esempio n. 35
0
def create_app(config=None):
    if config is None:
        config = {}

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__,
                      static_folder=os.path.join(ROOT_DIR, "..", "static"))

    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY',
                                  os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(
            sorted(
                os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH)
                if x.endswith(".yml")))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    if 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = os.path.expandvars(
            os.environ.get(
                'SQLALCHEMY_DATABASE_URI',
                'postgresql://localhost/{0}'.format(app.config['app_name'])))

    if os.path.exists("/dev/log"):
        syslog_handler = logbook.SyslogHandler(app.config['app_name'],
                                               "/dev/log")
        syslog_handler.push_application()

    del app.logger.handlers[:]
    redirect_logging()

    app.logger.info("Started")

    Mail(app)

    from . import models

    models.db.init_app(app)

    from . import auth
    from .blueprints import investigations, entities, events, roles, users, event_types
    Security(app, auth.user_datastore, register_blueprint=False)
    from .auth import auth
    from .views import views
    from .setup import setup
    app.register_blueprint(auth)
    app.register_blueprint(views)
    app.register_blueprint(setup)
    app.register_blueprint(investigations, url_prefix="/investigations")
    app.register_blueprint(entities, url_prefix="/entities")
    app.register_blueprint(events, url_prefix="/events")
    app.register_blueprint(roles, url_prefix="/roles")
    app.register_blueprint(users, url_prefix="/users")
    app.register_blueprint(event_types, url_prefix="/event-types")
    from .errors import errors
    for code in errors:
        app.errorhandler(code)(errors[code])

    return app
Esempio n. 36
0
def create_app(config=None):
    if config is None:
        config = {}

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__,
                      static_folder=os.path.join(ROOT_DIR, "..", "static"))

    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY',
                                  os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(
            sorted(
                os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH)
                if x.endswith(".yml")))

    configs.append(os.path.expanduser('~/.config/backslash/devconfig.yml'))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    if 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = os.path.expandvars(
            os.environ.get(
                'SQLALCHEMY_DATABASE_URI',
                'postgresql://localhost/{0}'.format(app.config['app_name'])))

    if os.path.exists("/dev/log"):
        syslog_handler = logbook.SyslogHandler(app.config['app_name'],
                                               "/dev/log")
        syslog_handler.push_application()

    del app.logger.handlers[:]
    redirect_logging()

    if app.config['TESTING']:
        app.config['TRACEBACK_DIR'] = '/tmp/backslash_tracebacks'
    else:
        _disable_logs(['dogpile.lock'])

    app.logger.info("Started")

    Mail(app)

    from . import models
    from .blueprints import rest, views, runtoken
    from .blueprints.api.main import blueprint as api_blueprint

    models.db.init_app(app)

    from . import auth
    Security(app, auth.user_datastore, register_blueprint=False)

    blueprints = [
        auth.auth, views.blueprint, api_blueprint, rest.blueprint,
        runtoken.blueprint
    ]

    from .errors import errors

    for blueprint in blueprints:
        app.register_blueprint(blueprint)

    for code in errors:
        app.errorhandler(code)(errors[code])

    return app
Esempio n. 37
0
def create_app(config=None, setup_logging=True):
    if config is None:
        config = {}

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__, static_folder=None)
    app.wsgi_app = ProxyFix(app.wsgi_app)

    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY',
                                  os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(
            sorted(
                os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH)
                if x.endswith(".yml")))

    configs.append(os.path.expanduser('~/.config/backslash/devconfig.yml'))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    app.before_request(profile_request_start)
    app.after_request(profile_request_end)

    db_uri = os.environ.get('BACKSLASH_DATABASE_URI', None)
    if db_uri is not None or 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config[
            'SQLALCHEMY_DATABASE_URI'] = db_uri or 'postgresql://localhost/{0}'.format(
                app.config['app_name'])

    if setup_logging:
        del app.logger.handlers[:]
        redirect_logging()

    if os.environ.get('BACKSLASH_TESTING', '').lower() in {'1', 'yes', 'true'}:
        app.config['TESTING'] = True

    if app.config['TESTING']:
        app.config['TRACEBACK_DIR'] = '/tmp/backslash_tracebacks'
    else:
        _disable_logs(['dogpile.lock'])

    logging.getLogger('urllib3').setLevel(logging.WARNING)

    if not app.config['DEBUG'] and not app.config['TESTING']:
        app.config['RAVEN_IGNORE_EXCEPTIONS'] = (
            HTTPException,
            SystemExit,
        )
        sentry = Sentry(app)  # pylint: disable=unused-variable

    override_tb_location = os.environ.get('BACKSLASH_TRACEBACKS_PATH', None)
    if override_tb_location:
        app.config['TRACEBACK_DIR'] = override_tb_location

    app.logger.info("Started")

    Mail(app)

    from . import models
    from .blueprints import rest, views, runtoken
    from .blueprints.api.main import blueprint as api_blueprint

    models.db.init_app(app)

    from . import auth
    Security(app, auth.user_datastore, register_blueprint=False)

    blueprints = [
        auth.auth, views.blueprint, api_blueprint, rest.blueprint,
        runtoken.blueprint
    ]

    from .errors import errors

    for blueprint in blueprints:
        app.register_blueprint(blueprint)

    for code in errors:
        app.errorhandler(code)(errors[code])

    return app
Esempio n. 38
0
def create_app(config=None):
    if config is None:
        config = {}

    # Defaults
    config.update({
        'MAX_MAILBOX_AGE_SECONDS': 7 * 24 * 60 * 60,  # 1 week
    })

    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

    app = flask.Flask(__name__,
                      static_folder=os.path.join(ROOT_DIR, "..", "static"))

    _CONF_D_PATH = os.environ.get('CONFIG_DIRECTORY',
                                  os.path.join(ROOT_DIR, "..", "..", "conf.d"))

    configs = [os.path.join(ROOT_DIR, "app.yml")]

    if os.path.isdir(_CONF_D_PATH):
        configs.extend(
            sorted(
                os.path.join(_CONF_D_PATH, x) for x in os.listdir(_CONF_D_PATH)
                if x.endswith(".yml")))

    for yaml_path in configs:
        if os.path.isfile(yaml_path):
            with open(yaml_path) as yaml_file:
                app.config.update(yaml.load(yaml_file))

    app.config.update(config)

    if 'SQLALCHEMY_DATABASE_URI' not in app.config:
        app.config['SQLALCHEMY_DATABASE_URI'] = os.path.expandvars(
            os.environ.get(
                'SQLALCHEMY_DATABASE_URI',
                'postgresql://localhost/{0}'.format(app.config['app_name'])))

    if os.path.exists("/dev/log"):
        syslog_handler = logbook.SyslogHandler(app.config['app_name'],
                                               "/dev/log")
        syslog_handler.push_application()

    del app.logger.handlers[:]
    redirect_logging()

    Mail(app)

    from . import models
    from . import errors

    models.db.init_app(app)

    from . import auth
    Security(app, auth.user_datastore, register_blueprint=False)

    from .errors import errors

    from .auth import auth
    from .views import views
    from .setup import setup
    blueprints = [auth, views, setup]

    for blueprint in blueprints:
        app.register_blueprint(blueprint)

    from .api import blueprint as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix="/v2")

    for code in errors:
        app.errorhandler(code)(errors[code])
    return app