<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

# Copyright (c) 2014 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from launchpadlib.launchpad import Launchpad
from launchpadlib.launchpad import uris


class launchpad_client(object):

    def __init__(self, project):

        lp_cache_dir = os.path.expanduser(
            os.environ.get('LAUNCHPAD_CACHE_DIR',
                           '~/.launchpadlib/cache'))

        lp_creds_filename = os.path.expanduser(
            os.environ.get('LAUNCHPAD_CREDS_FILENAME',
                           '~/.launchpadlib/creds'))

        self.Launchpad = Launchpad.login_with(
            project,
            uris.LPNET_SERVICE_ROOT,
            lp_cache_dir,
            credentials_file=lp_creds_filename)

        self.Project = self.launchpad.projects[project]

    @property
    def launchpad(self):
        return self.Launchpad

    @property
    def project(self):
        return self.Project

    def bug(self, bug_id):
        by_id = self.launchpad.bugs[bug_id]
        o_bugs = self.project.searchTasks(owner=by_id.owner)
        for by_own in o_bugs:
            if by_id.self_link == by_own.bug_link:
                return by_id, by_own

    def add_comment(self, bug_id, comment):
        by_id, by_own = self.bug(bug_id)
        return by_id.newMessage(content=comment)


def get_argparser():

    parser = argparse.ArgumentParser(prog='lp_cli.py',
                                     description='Command line client '
                                     'for Launchpad')
    parser.add_argument('project',
                        help='Launchpad project\'s name.')

    subparsers = parser.add_subparsers(title='subcommands')

    parser_comment = subparsers.add_parser('comment')
    parser_comment.set_defaults(func=command_comment)
    parser_comment.add_argument('bug_id', help='Bug id on Launchpad.')
    parser_comment.add_argument('comment', help='Comment body.', nargs='+')
    return parser


def command_comment(launchpad_client, options):
    comment = ' '.join(options.comment).strip()
    print 'Added comment: {}'.format(launchpad_client.add_comment(
        options.bug_id, comment).web_link)


def main():

    parser = get_argparser()
    options = parser.parse_args()
    lp = launchpad_client(project=options.project)
    options.func(lp, options)
<commit_msg>Implemented changing of bug status<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

# Copyright (c) 2014 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from launchpadlib.launchpad import Launchpad
from launchpadlib.launchpad import uris


class launchpad_client(object):

    def __init__(self, project):

        lp_cache_dir = os.path.expanduser(
            os.environ.get('LAUNCHPAD_CACHE_DIR',
                           '~/.launchpadlib/cache'))

        lp_creds_filename = os.path.expanduser(
            os.environ.get('LAUNCHPAD_CREDS_FILENAME',
                           '~/.launchpadlib/creds'))

        self.Launchpad = Launchpad.login_with(
            project,
            uris.LPNET_SERVICE_ROOT,
            lp_cache_dir,
            credentials_file=lp_creds_filename)

        self.Project = self.launchpad.projects[project]

    @property
    def launchpad(self):
        return self.Launchpad

    @property
    def project(self):
        return self.Project

    def bug(self, bug_id):
        by_id = self.launchpad.bugs[bug_id]
        o_bugs = self.project.searchTasks(owner=by_id.owner)
        for by_own in o_bugs:
            if by_id.self_link == by_own.bug_link:
                return by_id, by_own

    def add_comment(self, bug_id, comment):
        by_id, by_own = self.bug(bug_id)
        return by_id.newMessage(content=comment)

    def change_status(self, bug_id, status, current_status=None):
        by_id, by_own = self.bug(bug_id)
        #if current_status:
        #    if by_own.status != current_status:
        #        raise Exception('Bug status is not updated because current '
        #                        'value is not up to expectations')
        by_own.status = status
        return by_own.lp_save()


def get_argparser():

    parser = argparse.ArgumentParser(prog='lp_cli.py',
                                     description='Command line client '
                                     'for Launchpad')
    parser.add_argument('project',
                        help='Launchpad project\'s name.')

    subparsers = parser.add_subparsers(title='subcommands')

    parser_comment = subparsers.add_parser('comment')
    parser_comment.set_defaults(func=command_comment)
    parser_comment.add_argument('bug_id', help='Bug id on Launchpad.')
    parser_comment.add_argument('comment', help='Comment body.', nargs='+')

    parser_status = subparsers.add_parser('status')
    parser_status.set_defaults(func=command_status)
    parser_status.add_argument('bug_id', help='Bug id on Launchpad.')
    parser_status.add_argument('-s', '--status', help='New status for a bug.')
    #parser_status.add_argument('-c', '--current-status',
    #                           help='Current status for bug. If specified, '
    #                           'status will be updated only when according.')

    return parser


def command_comment(launchpad_client, options):
    comment = ' '.join(options.comment).strip()
    print 'Added comment: {}'.format(launchpad_client.add_comment(
        options.bug_id, comment).web_link)


def command_status(launchpad_client, options):
    launchpad_client.change_status(options.bug_id, options.status)
    print 'Status for bug #{} changed to "{}"'.format(options.bug_id,
                                                      options.status)


def main():

    parser = get_argparser()
    options = parser.parse_args()
    lp = launchpad_client(project=options.project)
    options.func(lp, options)

{'fraction_non_alphanumeric': 0.09868894971906066, 'fraction_numerical': 0.002593286269989915, 'mean_word_length': 2.6440944881889763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1390644', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1959, 'n_words': 490}