<commit_before># Copyright (c) 2013 <NAME> <<EMAIL>>
# All Rights Reserved.
#
#    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 ConfigParser
import logging
import sys
import os

import redmine
import tempfile
from redmine import exceptions as rm_exc
from subprocess import call


# Setup the basic logging objects
LOG = logging.getLogger('rore')


def get_user(rmine, userdata):
    """Get the user ID from the provided data"""

    # first see if we got an int
    try:
        userdata = int(userdata)
        return userdata
    except ValueError:
        pass
    users = rmine.user.filter(name=userdata)
    if not users:
        raise RuntimeError('Unknown user %s' % userdata)
    if len(users) > 1:
        raise RuntimeError('Multiple users for %s found' % userdata)
    return users[0].id


def print_issue(rmine, issue, verbose=False, oneline=False):
    """Print out a redmine issue object."""

    # handle unauth issues -- github #20
    if issue.id == 0:
        print('Unauthorized to view this issue')
        return
    # handle oneline printing
    if oneline:
        print('%s %s %s %s %s %s' % (issue.id, issue.project.name,
                                     issue.tracker.name, issue.priority.name,
                                     issue.status.name, issue.subject))
        return
    print('%s ( %s )' % (issue.id, issue.url))
    print('subject:     %s' % issue.subject)
    print('type:        %s' % issue.tracker.name)
    try:
        print('assigned to: %s' % issue.assigned_to.name)
    except rm_exc.ResourceAttrError:
        print('assigned to: UNASSIGNED')
    print('project:     %s' % issue.project.name)
    print('status:      %s' % issue.status.name)
    print('priority:    %s' % issue.priority.name)
    print('completion:  %s' % issue.done_ratio)
    if verbose:
        # Here is where we should enumerate all the possible fields
        print('priority:    %s' % issue.priority.name)
        print('start date:  %s' % issue.start_date)
        try:
            print('due date:    %s' % issue.due_date)
        except rm_exc.ResourceAttrError:
            pass
        try:
            print('parent:      %s' % issue.parent['id'])
        except rm_exc.ResourceAttrError:
            pass
        print('updated_on:  %s' % issue.updated_on)
        if hasattr(issue, 'description'):
            print('description:\n')
            print(issue.description)
        print('----')
        for relation in issue.relations:
            # Get the verb right for blocking relationship
            reltype = relation.relation_type
            if relation.issue_id != issue.id:
                if relation.relation_type == 'blocks':
                    reltype = 'blocked by'
                if relation.relation_type == 'blocked':
                    reltype = 'blocks'
                relish = rmine.issue.get(relation.issue_id)
            else:
                relish = rmine.issue.get(relation.issue_to_id)
            # Check for unauth -- wtf? See github #20
            if relish.id == 0:
                continue
            # Get the verb right for blocking relationship
            reltype = relation.relation_type
            if relation.issue_id != issue.id:
                if relation.relation_type == 'blocks':
                    reltype = 'blocked by'
                if relation.relation_type == 'blocked':
                    reltype = 'blocks'
                if relation.relation_type == 'duplicates':
                    reltype = 'duplicated by'
                if relation.relation_type == 'precedes':
                    reltype = 'preceded by'
                if relation.relation_type == 'follows':
                    reltype = 'followed by'
            print('%s %s - %s #%s: %s (%s)') % (reltype,
                                                relish.project.name,
                                                relish.tracker.name,
                                                relish.id,
                                                relish.subject,
                                                relation.id)
        for journ in issue.journals:
            print('\n####')
            print('Updated by %s on %s:' % (journ.user.name,
                                            journ.created_on))
            if hasattr(journ, 'notes'):
                print(journ.notes)
    print('\n')


def print_project(rmine, proj, verbose=False):
    """Print out a redmine project object."""

    print('%s ( %s )' % (proj.name, '%s/projects/%s' % (rmine.url,
                                                        proj.identifier)))
    if verbose:
        # Here is where we should enumerate all the possible fields
        print('description: %s' % proj.description)
        print('identifier: %s' % proj.identifier)
        try:
            print('parent: %s' % proj.parent['name'])
        except rm_exc.ResourceAttrError:
            pass
    print('\n')


def editor_text(initial_description=""):
    EDITOR = os.environ.get('EDITOR')
    with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False,
                                     dir='/tmp/') as tmp:
        tmp.write(initial_description)
        tmp.flush()
        call([EDITOR, tmp.name])
    with open(tmp.name, 'r') as fh:
        text = fh.read()
    os.remove(tmp.name)
    return text
<commit_msg>handles if EDITOR env not set<commit_after># Copyright (c) 2013 <NAME> <<EMAIL>>
# All Rights Reserved.
#
#    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 ConfigParser
import logging
import sys
import os

import redmine
import tempfile
from redmine import exceptions as rm_exc
from subprocess import call


# Setup the basic logging objects
LOG = logging.getLogger('rore')


def get_user(rmine, userdata):
    """Get the user ID from the provided data"""

    # first see if we got an int
    try:
        userdata = int(userdata)
        return userdata
    except ValueError:
        pass
    users = rmine.user.filter(name=userdata)
    if not users:
        raise RuntimeError('Unknown user %s' % userdata)
    if len(users) > 1:
        raise RuntimeError('Multiple users for %s found' % userdata)
    return users[0].id


def print_issue(rmine, issue, verbose=False, oneline=False):
    """Print out a redmine issue object."""

    # handle unauth issues -- github #20
    if issue.id == 0:
        print('Unauthorized to view this issue')
        return
    # handle oneline printing
    if oneline:
        print('%s %s %s %s %s %s' % (issue.id, issue.project.name,
                                     issue.tracker.name, issue.priority.name,
                                     issue.status.name, issue.subject))
        return
    print('%s ( %s )' % (issue.id, issue.url))
    print('subject:     %s' % issue.subject)
    print('type:        %s' % issue.tracker.name)
    try:
        print('assigned to: %s' % issue.assigned_to.name)
    except rm_exc.ResourceAttrError:
        print('assigned to: UNASSIGNED')
    print('project:     %s' % issue.project.name)
    print('status:      %s' % issue.status.name)
    print('priority:    %s' % issue.priority.name)
    print('completion:  %s' % issue.done_ratio)
    if verbose:
        # Here is where we should enumerate all the possible fields
        print('priority:    %s' % issue.priority.name)
        print('start date:  %s' % issue.start_date)
        try:
            print('due date:    %s' % issue.due_date)
        except rm_exc.ResourceAttrError:
            pass
        try:
            print('parent:      %s' % issue.parent['id'])
        except rm_exc.ResourceAttrError:
            pass
        print('updated_on:  %s' % issue.updated_on)
        if hasattr(issue, 'description'):
            print('description:\n')
            print(issue.description)
        print('----')
        for relation in issue.relations:
            # Get the verb right for blocking relationship
            reltype = relation.relation_type
            if relation.issue_id != issue.id:
                if relation.relation_type == 'blocks':
                    reltype = 'blocked by'
                if relation.relation_type == 'blocked':
                    reltype = 'blocks'
                relish = rmine.issue.get(relation.issue_id)
            else:
                relish = rmine.issue.get(relation.issue_to_id)
            # Check for unauth -- wtf? See github #20
            if relish.id == 0:
                continue
            # Get the verb right for blocking relationship
            reltype = relation.relation_type
            if relation.issue_id != issue.id:
                if relation.relation_type == 'blocks':
                    reltype = 'blocked by'
                if relation.relation_type == 'blocked':
                    reltype = 'blocks'
                if relation.relation_type == 'duplicates':
                    reltype = 'duplicated by'
                if relation.relation_type == 'precedes':
                    reltype = 'preceded by'
                if relation.relation_type == 'follows':
                    reltype = 'followed by'
            print('%s %s - %s #%s: %s (%s)') % (reltype,
                                                relish.project.name,
                                                relish.tracker.name,
                                                relish.id,
                                                relish.subject,
                                                relation.id)
        for journ in issue.journals:
            print('\n####')
            print('Updated by %s on %s:' % (journ.user.name,
                                            journ.created_on))
            if hasattr(journ, 'notes'):
                print(journ.notes)
    print('\n')


def print_project(rmine, proj, verbose=False):
    """Print out a redmine project object."""

    print('%s ( %s )' % (proj.name, '%s/projects/%s' % (rmine.url,
                                                        proj.identifier)))
    if verbose:
        # Here is where we should enumerate all the possible fields
        print('description: %s' % proj.description)
        print('identifier: %s' % proj.identifier)
        try:
            print('parent: %s' % proj.parent['name'])
        except rm_exc.ResourceAttrError:
            pass
    print('\n')


def editor_text(initial_description=""):
    EDITOR = os.environ.get('EDITOR')
    text = initial_description
    if EDITOR:
        with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False,
                                         dir='/tmp/') as tmp:
            tmp.write(initial_description)
            tmp.flush()
            call([EDITOR, tmp.name])
        with open(tmp.name, 'r') as fh:
            text = fh.read()
        os.remove(tmp.name)
    return text

{'fraction_non_alphanumeric': 0.10434197239986537, 'fraction_numerical': 0.0026926960619320095, 'mean_word_length': 1.7240430896172358, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1309079', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3156, 'n_words': 935}