<commit_before># -*- coding: utf-8 -*-

# Copyright(C) 2010  <NAME>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import sys

import weboob
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter
from weboob.capabilities.dating import ICapDating, OptimizationNotFound
from weboob.capabilities.contact import Contact


__all__ = ['HaveSex']


class ProfileFormatter(IFormatter):
    def flush(self):
        pass

    def print_node(self, node, level=1):
        result = u''
        if node.flags & node.SECTION:
            result += u'\t' * level + node.label + '\n'
            for sub in node.value:
                result += self.print_node(sub, level+1)
        else:
            if isinstance(node.value, (tuple,list)):
                value = ','.join([unicode(v) for v in node.value])
            else:
                value = node.value
            result += u'\t' * level + u'%-20s %s\n' % (node.label + ':', value)
        return result

    def format_dict(self, item):
        result = u'Nickname: %s\n' % item['name']
        if item['status'] & Contact.STATUS_ONLINE:
            s = 'online'
        elif item['status'] & Contact.STATUS_OFFLINE:
            s = 'offline'
        elif item['status'] & Contact.STATUS_AWAY:
            s = 'away'
        else:
            s = 'unknown'
        result += u'Status: %s (%s)\n' % (s, item['status_msg'])
        result += u'Photos:\n'
        for name, photo in item['photos'].iteritems():
            result += u'\t%s\n' % photo
        result += u'Profile:\n'
        for head in item['profile']:
            result += self.print_node(head)
        result += u'Description:\n'
        for s in item['summary'].split('\n'):
            result += u'\t%s\n' % s
        return result

class HaveSex(ReplApplication):
    APPNAME = 'havesex'
    VERSION = '0.4'
    COPYRIGHT = 'Copyright(C) 2010 <NAME>'
    STORAGE_FILENAME = 'dating.storage'
    STORAGE = {'optims': {}}
    CAPS = ICapDating
    EXTRA_FORMATTERS = {'profile': ProfileFormatter}
    COMMANDS_FORMATTERS = {'optim':   'table',
                           'profile': 'profile'}

    def load_default_backends(self):
        self.load_backends(ICapDating, storage=self.create_storage(self.STORAGE_FILENAME))

    def main(self, argv):
        self.load_config()

        try:
            self.do('init_optimizations').wait()
        except weboob.core.CallErrors, e:
            self.bcall_errors_handler(e)

        optimizations = self.storage.get('optims')
        for optim, backends in optimizations.iteritems():
            self.optims('start', backends, optim, store=False)

        return ReplApplication.main(self, argv)

    def do_profile(self, id):
        """
        profile ID

        Display a profile
        """
        _id, backend_name = self.parse_id(id)

        found = 0
        for backend, contact in self.do('get_contact', _id, backends=backend_name):
            if contact:
                self.format(contact)
                found = 1

        if not found:
            self.logger.error(u'Profile not found')
        else:
            self.flush()

        return True

    def edit_optims(self, backend_names, optims_names, stop=False):
        if optims_names is None:
            print >>sys.stderr, 'Error: missing parameters.'
            return 1

        for optim_name in optims_names.split():
            backends_optims = {}
            for backend, optim in self.do('get_optimization', optim_name, backends=backend_names):
                if optim:
                    backends_optims[backend.name] = optim
            for backend_name, optim in backends_optims.iteritems():
                if len(optim.CONFIG) == 0:
                    print '%s.%s does not require configuration.' % (backend_name, optim_name)
                    continue

                was_running = optim.is_running()
                if stop and was_running:<commit_msg>new command 'query'<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2010  <NAME>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import sys

import weboob
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter
from weboob.capabilities.dating import ICapDating, OptimizationNotFound
from weboob.capabilities.contact import Contact


__all__ = ['HaveSex']


class ProfileFormatter(IFormatter):
    def flush(self):
        pass

    def print_node(self, node, level=1):
        result = u''
        if node.flags & node.SECTION:
            result += u'\t' * level + node.label + '\n'
            for sub in node.value:
                result += self.print_node(sub, level+1)
        else:
            if isinstance(node.value, (tuple,list)):
                value = ','.join([unicode(v) for v in node.value])
            else:
                value = node.value
            result += u'\t' * level + u'%-20s %s\n' % (node.label + ':', value)
        return result

    def format_dict(self, item):
        result = u'Nickname: %s\n' % item['name']
        if item['status'] & Contact.STATUS_ONLINE:
            s = 'online'
        elif item['status'] & Contact.STATUS_OFFLINE:
            s = 'offline'
        elif item['status'] & Contact.STATUS_AWAY:
            s = 'away'
        else:
            s = 'unknown'
        result += u'Status: %s (%s)\n' % (s, item['status_msg'])
        result += u'Photos:\n'
        for name, photo in item['photos'].iteritems():
            result += u'\t%s\n' % photo
        result += u'Profile:\n'
        for head in item['profile']:
            result += self.print_node(head)
        result += u'Description:\n'
        for s in item['summary'].split('\n'):
            result += u'\t%s\n' % s
        return result

class HaveSex(ReplApplication):
    APPNAME = 'havesex'
    VERSION = '0.4'
    COPYRIGHT = 'Copyright(C) 2010 <NAME>'
    STORAGE_FILENAME = 'dating.storage'
    STORAGE = {'optims': {}}
    CAPS = ICapDating
    EXTRA_FORMATTERS = {'profile': ProfileFormatter}
    COMMANDS_FORMATTERS = {'optim':   'table',
                           'profile': 'profile'}

    def load_default_backends(self):
        self.load_backends(ICapDating, storage=self.create_storage(self.STORAGE_FILENAME))

    def main(self, argv):
        self.load_config()

        try:
            self.do('init_optimizations').wait()
        except weboob.core.CallErrors, e:
            self.bcall_errors_handler(e)

        optimizations = self.storage.get('optims')
        for optim, backends in optimizations.iteritems():
            self.optims('start', backends, optim, store=False)

        return ReplApplication.main(self, argv)

    def do_profile(self, id):
        """
        profile ID

        Display a profile
        """
        _id, backend_name = self.parse_id(id)

        found = 0
        for backend, contact in self.do('get_contact', _id, backends=backend_name):
            if contact:
                self.format(contact)
                found = 1

        if not found:
            self.logger.error(u'Profile not found')
        else:
            self.flush()

        return True

    def do_query(self, id):
        """
        query ID

        Send a query to someone.
        """
        _id, backend_name = self.parse_id(id)

        for backend, query in self.do('send_query', _id, backends=backend_name):
            print '%s' % query.message

    def edit_optims(self, backend_names, optims_names, stop=False):
        if optims_names is None:
            print >>sys.stderr, 'Error: missing parameters.'
            return 1

        for optim_name in optims_names.split():
            backends_optims = {}
            for backend, optim in self.do('get_optimization', optim_name, backends=backend_names):
                if optim:
                    backends_optims[backend.name] = optim
            for backend_name, optim in backends_optims.iteritems():
                if len(optim.CONFIG) == 0:
                    print '%s.%s does not require configuration.' % (backend_name, optim_name)
                    continue

                was_running = optim.is_running()
                if stop and was_running:
{'fraction_non_alphanumeric': 0.10881266490765172, 'fraction_numerical': 0.007176781002638522, 'mean_word_length': 2.1916470191983835, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4809134', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2675, 'n_words': 792}