def __call__(self, f, *args, **kw):
        services = _check_services_injection(f, self)
        services.update(kw)

        try:
            return f(*args, **services)
        except:
            log.get_logger(".{}.ServicesRepository.__call__".format(__name__)).error("%s %s %s", f, args, services)
            raise
Exemple #2
0
    def create_message(self, subject, from_, to, content, cc=None, bcc=None,
                       type='plain', mpart_type='mixed', attachments=()):
        """Sends an e-mail using the mail section configuration of
           the application.

        Parameters:
        * `subject` -- the subject of the mail,
        * `from_` -- the sender,
        * `to` -- the receiver or list of receivers,
        * `content` -- the content of the mail,
        * `cc` -- the eventual CC or list of CC,
        * `bcc` -- the eventual BCC or list of BCC,
        * `type` -- the eventual type of the email, either `'plain'`
                    or `'html'`.
        * `mpart_type` -- the eventual custom ``MIMEMultipart`` type
                          ('mixed' or 'related') (defaults to mixed)
        * `attachments` -- the eventual list of attachments to add
        """

        # converts recipients to list and provide an empty list for None
        to = [to] if is_string(to) else to
        cc = [cc] if is_string(cc) else (cc or [])
        bcc = [bcc] if is_string(bcc) else (bcc or [])

        # always adds the hidden recipients
        if self.hidden_recipients:
            bcc += self.hidden_recipients

        # creates the message envelop
        msg = MIMEMultipart(mpart_type)
        msg['Subject'] = subject
        msg['Date'] = formatdate(localtime=True)

        if self.reply_to:
            msg['From'] = self.reply_to
            msg['Reply-to'] = from_
        else:
            msg['From'] = from_

        msg['To'] = COMMASPACE.join(to)

        if cc:
            msg['Cc'] = COMMASPACE.join(cc)

        # attaches the mail content
        if isinstance(content, unicode):
            content = content.encode('UTF-8')
        msg.attach(MIMEText(content, type, _charset='UTF-8'))

        # eventually completes the message with attachments
        for attachment in attachments:
            self.add_file_attachment(msg, *attachment)

        # log the email
        logger = log.get_logger('.' + __name__)
        logger.debug('Sending mail: from=%r, to=%r, cc=%r, bcc=%r, subject=%r',
                     from_, to, cc, bcc, subject)
        logger.debug('Mail content:\n' + content)

        return msg
Exemple #3
0
def configure():
    """Configure the batch environment"""

    global apps
    # we suppose that the batch addresses only one application
    app_name, app = apps.items()[0]

    local.request = local.Process()
    local.worker = local.Process()

    log.set_logger('nagare.application.' + app.name)

    security.set_manager(app.security)

    # initialize the application context
    app.set_base_url('http://localhost/')  # dummy server host
    app.set_locale(i18n.Locale('en', 'US'))  # Set the default Locale

    # install the configuration
    registry.configure(app.configuration)

    # log the start of the script
    logger = log.get_logger('.' + __name__)
    logger.debug('----')
    logger.debug('Running %s\n' % sys.argv[0])
def _emit_signal(self, signal, *args, **kwds):
    logger = log.get_logger('.' + __name__)
    logger.debug('%s is sending a signal %s with args=%s and kwargs=%s' % (self, signal, args, kwds))
    listeners = _get_listeners(self)
    if listeners:
        for listener in listeners:
            logger.debug('%s is receiving the signal' % listener)
            _handle_signal(listener, self, signal, *args, **kwds)
    else:
        logger.debug('No listener registered for this sender: %s' % self)
Exemple #5
0
def _emit_signal(self, signal, *args, **kwds):
    logger = log.get_logger('.' + __name__)
    logger.debug('%s is sending a signal %s with args=%s and kwargs=%s' %
                 (self, signal, args, kwds))
    listeners = _get_listeners(self)
    if listeners:
        for listener in listeners:
            logger.debug('%s is receiving the signal' % listener)
            _handle_signal(listener, self, signal, *args, **kwds)
    else:
        logger.debug('No listener registered for this sender: %s' % self)
Exemple #6
0
def main():
    # configure the batch environment
    configure()

    # parse the command line
    description = """
    Rebuild the search indexes
    """
    parser = OptionParser(usage='Usage: %prog [options]',
                          description=textwrap.dedent(description).strip())
    (__, __) = parser.parse_args()

    # start the re-indexing
    rebuild_indexes(log.get_logger())
def get_search_results(type, pattern, query, id_field, max_results=200,
                       default_search_field='text'):
    ordered_ids = get_search_engine().search(type, pattern, rows=max_results,
                                             default_field=default_search_field)[
        0] if pattern else []

    # log the result of the search
    logger = log.get_logger('.' + __name__)
    logger.debug(
        'Search results of type %s for "%s" on search field "%s": %s'
        % (type, pattern, default_search_field, ordered_ids))

    query = query.filter(id_field.in_(ordered_ids))
    if ordered_ids:
        rank_mapping = [(id, rank) for rank, id in enumerate(ordered_ids)]
        query = query.order_by(case(value=id_field, whens=rank_mapping))

    return query
def get_search_results(type,
                       pattern,
                       query,
                       id_field,
                       max_results=200,
                       default_search_field='text'):
    ordered_ids = get_search_engine().search(
        type, pattern, rows=max_results,
        default_field=default_search_field)[0] if pattern else []

    # log the result of the search
    logger = log.get_logger('.' + __name__)
    logger.debug(
        'Search results of type %s for "%s" on search field "%s": %s' %
        (type, pattern, default_search_field, ordered_ids))

    query = query.filter(id_field.in_(ordered_ids))
    if ordered_ids:
        rank_mapping = [(id, rank) for rank, id in enumerate(ordered_ids)]
        query = query.order_by(case(value=id_field, whens=rank_mapping))

    return query
 def log(self):
     return log.get_logger('.' + __name__)
Exemple #10
0
def _handle_signal(self, sender, signal, *args, **kwds):
    # forwards the signal
    logger = log.get_logger('.' + __name__)
    logger.debug('%s is forwarding the signal %s' % (self, signal))
    _emit_signal(self, signal, *args, **kwds)
 def log(self):
     return log.get_logger('.' + __name__)
def init_shell_static_content(self, url, *args):
    logger = log.get_logger('.' + __name__)
    logger.debug('Serving static content: %s' % '/'.join(url))

    path = get_fs_service().expand_path(url)
    serve_static_content(path)
Exemple #13
0
 def log(self):
     return log.get_logger("." + __name__)
    def create_message(self,
                       subject,
                       from_,
                       to,
                       content,
                       cc=None,
                       bcc=None,
                       type='plain',
                       mpart_type='mixed',
                       attachments=()):
        """Sends an e-mail using the mail section configuration of
           the application.

        Parameters:
        * `subject` -- the subject of the mail,
        * `from_` -- the sender,
        * `to` -- the receiver or list of receivers,
        * `content` -- the content of the mail,
        * `cc` -- the eventual CC or list of CC,
        * `bcc` -- the eventual BCC or list of BCC,
        * `type` -- the eventual type of the email, either `'plain'`
                    or `'html'`.
        * `mpart_type` -- the eventual custom ``MIMEMultipart`` type
                          ('mixed' or 'related') (defaults to mixed)
        * `attachments` -- the eventual list of attachments to add
        """

        # converts recipients to list and provide an empty list for None
        to = [to] if is_string(to) else to
        cc = [cc] if is_string(cc) else (cc or [])
        bcc = [bcc] if is_string(bcc) else (bcc or [])

        # always adds the hidden recipients
        if self.hidden_recipients:
            bcc += self.hidden_recipients

        # creates the message envelop
        msg = MIMEMultipart(mpart_type)
        msg['Subject'] = subject
        msg['Date'] = formatdate(localtime=True)

        if self.reply_to:
            msg['From'] = self.reply_to
            msg['Reply-to'] = from_
        else:
            msg['From'] = from_

        msg['To'] = COMMASPACE.join(to)

        if cc:
            msg['Cc'] = COMMASPACE.join(cc)

        # attaches the mail content
        if isinstance(content, unicode):
            content = content.encode('UTF-8')
        msg.attach(MIMEText(content, type, _charset='UTF-8'))

        # eventually completes the message with attachments
        for attachment in attachments:
            self.add_file_attachment(msg, *attachment)

        # log the email
        logger = log.get_logger('.' + __name__)
        logger.debug('Sending mail: from=%r, to=%r, cc=%r, bcc=%r, subject=%r',
                     from_, to, cc, bcc, subject)
        logger.debug('Mail content:\n' + content)

        return msg
Exemple #15
0
 def log_exception(logger_name='nagare.services.exceptions', exc_info=True):
     log.get_logger(logger_name).error('Unhandled exception',
                                       exc_info=exc_info)
def init_shell_static_content(self, url, *args):
    logger = log.get_logger('.' + __name__)
    logger.debug('Serving static content: %s' % '/'.join(url))

    path = get_fs_service().expand_path(url)
    serve_static_content(path)
def _handle_signal(self, sender, signal, *args, **kwds):
    # forwards the signal
    logger = log.get_logger('.' + __name__)
    logger.debug('%s is forwarding the signal %s' % (self, signal))
    _emit_signal(self, signal, *args, **kwds)