コード例 #1
0
def update_config(bag, name, config):
    from configconvert import handle_option_error, handle_section_error
    if not bag.app.option.has_key(name):
        raise handle_section_error(
            bag, 
            name, 
            (
                "'%s.plugin' (the name of the database plugin to use"%(name)
            )
        )
    from stringconvert import unicodeToUnicode, unicodeToInteger,\
       unicodeToBoolean
    from recordconvert import toRecord
    from configconvert import stringToObject
    from conversionkit import Conversion, chainConverters

    # Re-use the converters   
    unicode_to_integer = unicodeToInteger()
    null = unicodeToUnicode()

    database_config = toRecord(
        missing_defaults=dict(
            creator=connect,
            fetch_converter=None,
            execute_converter=None,
            on_connect_sql=None,
        ),
        missing_or_empty_errors = dict(
            dsn="The required option '%s.dsn' is missing"%(name,),
            unicode_results="The required option '%s.uniocde_results' is missing"%(name,),
        ),
        converters=dict(
            dsn = null,
            unicode_results = unicodeToBoolean(),
            on_connect_sql = unicodeToUnicode(),
            creator = stringToObject(),
            fetch_converter = stringToObject(),
            execute_converter = stringToObject(),
        ),
    ) 
    import base64
    if config.has_key('odsn'):
        odsn = config['odsn']
        parts = odsn.split('|')
        s = str(parts[1])
        p = base64.urlsafe_b64decode((s[:-4] + '=' * (4 - (len(s[4:])-4) % 4))[4:])
        config['dsn'] = parts[0] + p + parts[2]
        del config['odsn']
    conversion = Conversion(config).perform(database_config)
    if not conversion.successful:
        handle_option_error(conversion, name)
    else:
        config = conversion.result
    return config
コード例 #2
0
ファイル: service.py プロジェクト: bopopescu/vobileETLCode
    def config(flow, name):
        from configconvert import handle_option_error, handle_section_error
        if not flow.config.option_group.has_key(name):
            raise handle_section_error(
                flow, name, "'%s.sendmail' or '%s.smtp.host'" % (name, name))
        from nestedrecord import decodeNestedRecord
        from conversionkit import chainConverters, chainPostConverters, Conversion
        from stringconvert import unicodeToInteger, unicodeToBoolean, unicodeToUnicode
        from stringconvert.email import listOfEmails
        from recordconvert import toRecord
        from configconvert import existingFile, existingDirectory

        to_unicode = unicodeToUnicode()
        smtp_converter = chainPostConverters(
            toRecord(missing_errors=dict(
                host="The required option '%s.smtp.host' is missing" %
                (name, ), ),
                     empty_errors=dict(
                         host="The option '%s.smtp.host' cannot be empty" %
                         (name, ), ),
                     missing_defaults=dict(starttls=False),
                     converters=dict(
                         host=to_unicode,
                         username=to_unicode,
                         password=to_unicode,
                         port=unicodeToInteger(),
                         starttls=unicodeToBoolean(),
                         verbose=unicodeToBoolean(),
                     )),
            requireIfPresent('username', ['password']),
        )
        mail_converter = chainConverters(
            decodeNestedRecord(depth=1),
            chainPostConverters(
                toRecord(converters=dict(
                    sendmail=existingFile(),
                    smtp=smtp_converter,
                    debug_folder=existingDirectory(),
                    to_email_override=listOfEmails(split_name=False),
                ), ),
                exacltyOneFieldFrom('sendmail', 'smtp'),
            ))
        conversion = Conversion(
            flow.config.option_group[name]).perform(mail_converter)
        if not conversion.successful:
            handle_option_error(conversion, name)
        else:
            flow.config[name] = conversion.result
        return flow.config[name]
コード例 #3
0
ファイル: __init__.py プロジェクト: DarKsp123/My_projects
def psycopg2_update_config(flow, name, config):
    from configconvert import handle_option_error, handle_section_error
    from stringconvert import unicodeToUnicode, unicodeToInteger,\
       unicodeToBoolean
    from recordconvert import toRecord
    from configconvert import stringToObject
    from conversionkit import Conversion, chainConverters

    # Re-use the converters   
    unicode_to_integer = unicodeToInteger()
    null = unicodeToUnicode()

    database_config = chainConverters(
        dbport,
        toRecord(
            missing_defaults=dict(
                creator=psycopg2.connect,
                fetch_converter=psycopg2_utf8_fetch,
                execute_converter=None,
            ),
            missing_or_empty_errors = dict(
                database="The required option '%s.database' is missing"%(name,),
            ),
            converters=dict(
                database = null,
                user = null,
                # Could use formencode.validators.URL() for host?
                host = null,
                password = null,
                port = unicode_to_integer,

                creator = stringToObject(),
                fetch_converter = stringToObject(),
                execute_converter = stringToObject(),
            ),
            # Keep the pool options as they are
            filter_extra_fields = False
        ),
    )
    conversion = Conversion(config).perform(database_config)
    if not conversion.successful:
        handle_option_error(conversion, name)
    else:
        config = conversion.result
    return config
コード例 #4
0
ファイル: __init__.py プロジェクト: Semenyshyn/myblog
def update_config(bag, name, config):
    if config.get('pool', False):
        raise Exception('The %s.pool option must be False for SQLite3'%name)
    from configconvert import handle_option_error, handle_section_error
    if not bag.app.option.has_key(name):
        raise handle_section_error(
            bag, 
            name, 
            (
                "'%s.database' and '%s.plugin' (the module module and "
                "function to use to create a connection)"%(name, name)
            )
        )
    from stringconvert import unicodeToUnicode, unicodeToInteger,\
       unicodeToBoolean
    from recordconvert import toRecord
    from configconvert import stringToObject
    from conversionkit import Conversion, chainConverters

    # Re-use the converters   
    unicode_to_integer = unicodeToInteger()
    null = unicodeToUnicode()

    database_config = toRecord(
        missing_defaults=dict(
            creator=connect,
            fetch_converter=sqlite3_utf8_fetch,
            execute_converter=None,
        ),
        missing_or_empty_errors = dict(
            database="The required option '%s.database' is missing"%(name,),
        ),
        converters=dict(
            database = null,
            creator = stringToObject(),
            fetch_converter = stringToObject(),
            execute_converter = stringToObject(),
        ),
    ) 
    conversion = Conversion(bag.app.option[name]).perform(database_config)
    if not conversion.successful:
        handle_option_error(conversion, name)
    else:
        config = conversion.result
    return config
コード例 #5
0
def update_config(bag, name, config):
    if config.get('pool', False):
        raise Exception('The %s.pool option must be False for SQLite3' % name)
    from configconvert import handle_option_error, handle_section_error
    if not bag.app.option.has_key(name):
        raise handle_section_error(
            bag, name,
            ("'%s.database' and '%s.plugin' (the module module and "
             "function to use to create a connection)" % (name, name)))
    from stringconvert import unicodeToUnicode, unicodeToInteger,\
       unicodeToBoolean
    from recordconvert import toRecord
    from configconvert import stringToObject
    from conversionkit import Conversion, chainConverters

    # Re-use the converters
    unicode_to_integer = unicodeToInteger()
    null = unicodeToUnicode()

    database_config = toRecord(
        missing_defaults=dict(
            creator=connect,
            fetch_converter=sqlite3_utf8_fetch,
            execute_converter=None,
        ),
        missing_or_empty_errors=dict(
            database="The required option '%s.database' is missing" %
            (name, ), ),
        converters=dict(
            database=null,
            creator=stringToObject(),
            fetch_converter=stringToObject(),
            execute_converter=stringToObject(),
        ),
    )
    conversion = Conversion(bag.app.option[name]).perform(database_config)
    if not conversion.successful:
        handle_option_error(conversion, name)
    else:
        config = conversion.result
    return config
コード例 #6
0
ファイル: service.py プロジェクト: karatemelli/smart-home
    def mailService_constructor(service, name, *k, **p):
        from mail.helper import send_smtp, send_sendmail, prepare, plain
        # Imports
        from configconvert import handle_option_error, handle_section_error
        from nestedrecord import decodeNestedRecord
        from conversionkit import chainConverters, chainPostConverters, Conversion
        from stringconvert import unicodeToInteger, unicodeToBoolean, unicodeToUnicode
        from stringconvert.email import listOfEmails
        from recordconvert import toRecord
        from configconvert import existingFile, existingDirectory

        # Config parsing
        if not service.app.option.has_key(name):
            raise handle_section_error(service, name, "'%s.sendmail' or '%s.smtp.host'"%(name, name))

        to_unicode = unicodeToUnicode()
        smtp_converter = chainPostConverters(
            toRecord(
                 missing_errors = dict(
                     host = "The required option '%s.smtp.host' is missing" % (name,),
                 ),
                 empty_errors = dict(
                     host="The option '%s.smtp.host' cannot be empty"%(name,),
                 ),
                 missing_defaults = dict(starttls=False),
                 converters = dict(
                     host = to_unicode,
                     username = to_unicode,
                     password = to_unicode,
                     port = unicodeToInteger(),
                     starttls = unicodeToBoolean(),
                     verbose = unicodeToBoolean(),
                 )
            ),
            requireIfPresent('username', ['password']),
        )
        mail_converter = chainConverters(
            decodeNestedRecord(depth=1),
            chainPostConverters(
                toRecord(
                    converters = dict(
                        sendmail = existingFile(),
                        smtp = smtp_converter,
                        debug_folder = existingDirectory(),
                        to_email_override = listOfEmails(split_name=False),
                    ),
                ),
                exacltyOneFieldFrom('sendmail', 'smtp'),
            )
        )
        conversion = Conversion(service.app.option[name]).perform(
            mail_converter
        )
        if not conversion.successful:
            handle_option_error(conversion, name)
        else:
            service.app.config[name] = conversion.result

        if service.app.config[name].get('debug_folder') and not \
           os.path.exists(service.app.config[name]['debug_folder']):
            os.mkdir(service.app.config[name]['debug_folder']) 

        def send(
            message, 
            to, 
            from_email, 
            from_name, 
            subject=None,
            type='plain',
            charset=None,
        ):
            if service.app.config[name].get('to_email_override'):
                log.warning(
                    'Replacing the email %s with %s', 
                    to, 
                    service.app.config[name].get('to_email_override'),
                )
                to = service.app.config[name].get('to_email_override')
            if to and isinstance(to, list) and isinstance(to[0], dict):
                to = ['%s <%s>'%(x.name, x.email) for x in to]
            subject = subject or service.app.config[name]['subject']
            message = prepare(
                plain(message, type=type, charset=charset),
                from_name = from_name,
                from_email = from_email,
                to=to, 
                subject=subject,
            )
            debug_folder = service.app.config[name].get('debug_folder')
            if debug_folder:
                log.warning(
                    'Writing message to the debug folder, not sending '
                    'it directly'
                )
                fp = open(
                    os.path.join(
                        debug_folder, 
                        '%s - %s.txt'%(subject, to) 
                    ),
                    'wb'
                )
                fp.write(str(message))
                fp.close()
            else:
                sendmail = service.app.config[name].get('sendmail')
                if sendmail:
                    return send_sendmail(
                        message, 
                        sendmail,
                    )
                smtp_args = service.app.config[name]['smtp']
                return send_smtp(message, **str_dict(smtp_args))

        def start(service):
            service[name] = AttributeDict(send=send)
        def leave(service, error=False):
            pass
        return AttributeDict(start=start, enter=start, leave=leave)
コード例 #7
0
ファイル: service.py プロジェクト: anand700/savier
    def mailService_constructor(service, name, *k, **p):
        from mail.helper import send_smtp, send_sendmail, prepare, plain
        # Imports
        from configconvert import handle_option_error, handle_section_error
        from nestedrecord import decodeNestedRecord
        from conversionkit import chainConverters, chainPostConverters, Conversion
        from stringconvert import unicodeToInteger, unicodeToBoolean, unicodeToUnicode
        from stringconvert.email import listOfEmails
        from recordconvert import toRecord
        from configconvert import existingFile, existingDirectory

        # Config parsing
        if not service.app.option.has_key(name):
            raise handle_section_error(
                service, name,
                "'%s.sendmail' or '%s.smtp.host'" % (name, name))

        to_unicode = unicodeToUnicode()
        smtp_converter = chainPostConverters(
            toRecord(missing_errors=dict(
                host="The required option '%s.smtp.host' is missing" %
                (name, ), ),
                     empty_errors=dict(
                         host="The option '%s.smtp.host' cannot be empty" %
                         (name, ), ),
                     missing_defaults=dict(starttls=False),
                     converters=dict(
                         host=to_unicode,
                         username=to_unicode,
                         password=to_unicode,
                         port=unicodeToInteger(),
                         starttls=unicodeToBoolean(),
                         verbose=unicodeToBoolean(),
                     )),
            requireIfPresent('username', ['password']),
        )
        mail_converter = chainConverters(
            decodeNestedRecord(depth=1),
            chainPostConverters(
                toRecord(converters=dict(
                    sendmail=existingFile(),
                    smtp=smtp_converter,
                    debug_folder=existingDirectory(),
                    to_email_override=listOfEmails(split_name=False),
                ), ),
                exacltyOneFieldFrom('sendmail', 'smtp'),
            ))
        conversion = Conversion(
            service.app.option[name]).perform(mail_converter)
        if not conversion.successful:
            handle_option_error(conversion, name)
        else:
            service.app.config[name] = conversion.result

        if service.app.config[name].get('debug_folder') and not \
           os.path.exists(service.app.config[name]['debug_folder']):
            os.mkdir(service.app.config[name]['debug_folder'])

        def send(
            message,
            to,
            from_email,
            from_name,
            subject=None,
            type='plain',
            charset=None,
        ):
            if service.app.config[name].get('to_email_override'):
                log.warning(
                    'Replacing the email %s with %s',
                    to,
                    service.app.config[name].get('to_email_override'),
                )
                to = service.app.config[name].get('to_email_override')
            if to and isinstance(to, list) and isinstance(to[0], dict):
                to = ['%s <%s>' % (x.name, x.email) for x in to]
            subject = subject or service.app.config[name]['subject']
            message = prepare(
                plain(message, type=type, charset=charset),
                from_name=from_name,
                from_email=from_email,
                to=to,
                subject=subject,
            )
            debug_folder = service.app.config[name].get('debug_folder')
            if debug_folder:
                log.warning('Writing message to the debug folder, not sending '
                            'it directly')
                fp = open(
                    os.path.join(debug_folder, '%s - %s.txt' % (subject, to)),
                    'wb')
                fp.write(str(message))
                fp.close()
            else:
                sendmail = service.app.config[name].get('sendmail')
                if sendmail:
                    return send_sendmail(
                        message,
                        sendmail,
                    )
                smtp_args = service.app.config[name]['smtp']
                return send_smtp(message, **str_dict(smtp_args))

        def start(service):
            service[name] = AttributeDict(send=send)

        def leave(service, error=False):
            pass

        return AttributeDict(start=start, enter=start, leave=leave)