Exemplo n.º 1
0
def send_email(config, content):
    '''Sends an email with the information configured
    
    Configuration:
    * email: Dictionary with email informacion
    * subject: Email subject
    * subject_fields: Fields to use in subject
    * body: Email body
    * body_fields: Fields to use in body
    '''
    logging.info('Sending notification email')
    if 'subtitle' in content and content['subtitle']:
        subject = config['subject']
        if config['subject_fields']:
            kwargs = utils.format_parameters(content, config['subject_fields'])
            subject = subject.format(**kwargs)
        body = config['body']
        if config['body_fields']:
            kwargs = utils.format_parameters(content, config['body_fields'])
            body = body.format(**kwargs)

            msg = MIMEText(body)  
            msg['Subject'] = subject     
            msg['From'] = config['email']['from']
            msg['To'] = ', '.join(config['destination'])
            server = smtplib.SMTP(config['email']['smtp_server'],
                                  config['email']['smtp_port'])
            if config['email']['use_tls']:
                server.starttls()
            server.login(config['email']['smtp_user'], config['email']['smtp_pass'])
            server.sendmail(config['email']['from'], config['destination'],
                            msg.as_string())
            server.quit()
    return content
Exemplo n.º 2
0
def system_command(config, content):
    '''Executes command and send the parameters as configured
    
    Configuration
    * command : Command to run. Content fields should be added as said in here:
                http://docs.python.org/library/stdtypes.html#str.format
                Ex: /path/to/command {link}
    * fields: Fields that should be extracted from content to be used as
              command params.
    '''
    kwargs = utils.format_parameters(content, config['fields'])
    cmd = config['command'].format(**kwargs)
    logging.info('Running %s', cmd)
    os.system(cmd)
    return content
Exemplo n.º 3
0
def download_file(config, content):
    '''Downloads a link as specified
    
    Configuration
    * dst_file: Destination file. Content fields should be adeda as said in
                here: http://docs.python.org/library/stdtypes.html#str.format
                Ex: /path/to/new_{link}_destination
    * dst_fields: Fields that should be extracted from content to be used as
                  command params.
    
    '''
    logging.info('Downloading %s', content['name'])
    kwargs = utils.format_parameters(content, config['dst_fields'])
    dst_file = config['dst_file'].format(**kwargs)
    f = urllib2.urlopen(content['link'])
    o = open(dst_file,'wb')
    o.write(f.read())
    o.close()
    f.close()
    return content