예제 #1
0
def mail(args, kwargs):
    # Get config file location
    default_dir = os.path.join(variables['GRAMEXDATA'], 'mail')
    _mkdir(default_dir)
    if 'conf' in kwargs:
        confpath = kwargs.conf
    elif os.path.exists('gramex.yaml'):
        confpath = os.path.abspath('gramex.yaml')
    else:
        confpath = os.path.join(default_dir, 'gramexmail.yaml')

    if not os.path.exists(confpath):
        if 'init' in kwargs:
            with io.open(confpath, 'w', encoding='utf-8') as handle:
                handle.write(default_mail_config.format(confpath=confpath))
            app_log.info('Initialized %s', confpath)
        elif not args and not kwargs:
            app_log.error(show_usage('mail'))
        else:
            app_log.error('Missing config %s. Use --init to generate skeleton',
                          confpath)
        return

    conf = PathConfig(confpath)
    if 'list' in kwargs:
        for key, alert in conf.get('alert', {}).items():
            to = alert.get('to', '')
            if isinstance(to, list):
                to = ', '.join(to)
            gramex.console('{:15}\t"{}" to {}'.format(key,
                                                      alert.get('subject'),
                                                      to))
        return

    if 'init' in kwargs:
        app_log.error('Config already exists at %s', confpath)
        return

    if len(args) < 1:
        app_log.error(show_usage('mail'))
        return

    from gramex.services import email as setup_email, create_alert
    alert_conf = conf.get('alert', {})
    email_conf = conf.get('email', {})
    setup_email(email_conf)
    sys.path += os.path.dirname(confpath)
    for key in args:
        if key not in alert_conf:
            app_log.error('Missing key %s in %s', key, confpath)
            continue
        alert = create_alert(key, alert_conf[key])
        alert()
예제 #2
0
def sendmail(handler):
    # Create a key: value configuration from the arguments
    conf = yaml.load(handler.get_arg('conf'),
                     Loader=AttrDictYAMLLoader)  # nosec
    if not isinstance(conf, dict):
        raise HTTPError(INTERNAL_SERVER_ERROR,
                        reason='Config should be a dict')
    conf.setdefault('service', 'alert-gmail')
    alert = create_alert('guide-alert', conf)
    if alert is None:
        raise HTTPError(INTERNAL_SERVER_ERROR,
                        reason='Cannot create alert with this config')
    kwargs = yield info.threadpool.submit(alert)
    if kwargs is None:
        raise HTTPError(INTERNAL_SERVER_ERROR,
                        reason='Could not run this config')
    raise Return(json.dumps(kwargs, indent=2))