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

'''rtcshell

Copyright (C) 2009-2010
    <NAME>
    RT-Synthesis Research Group
    Intelligent Systems Research Institute,
    National Institute of Advanced Industrial Science and Technology (AIST),
    Japan
    All rights reserved.
Licensed under the Eclipse Public License -v 1.0 (EPL)
http://www.opensource.org/licenses/eclipse-1.0.txt

File: rtcat.py

Implementation of the command to display component information.

'''

# $Source$


from optparse import OptionParser, OptionError
import os
from rtctree.exceptions import RtcTreeError
from rtctree.tree import create_rtctree
from rtctree.path import parse_path
from rtctree.utils import build_attr_string, get_num_columns_and_rows, \
                            get_terminal_size
import sys

from rtcshell import RTSH_PATH_USAGE, RTSH_VERSION
from rtcshell.path import cmd_path_to_full_path


def format_component(object, use_colour=True, long=False, really_long=False):
    result = []
    result.append('{0}  {1}'.format(object.name,
            object.get_state_string(add_colour=use_colour)))

    indent = 2
    profile_items = [('Category', object.category),
                     ('Description', object.description),
                     ('Instance name', object.instance_name),
                     ('Type name', object.type_name),
                     ('Vendor', object.vendor),
                     ('Version', object.version)]
    if object.parent:
        profile_items.append(('Parent', object.parent_object))
    pad_length = max([len(item[0]) for item in profile_items]) + 2
    for item in profile_items:
        result.append('{0}{1}{2}'.format(''.ljust(indent),
                                         item[0].ljust(pad_length),
                                         item[1]))

    if object.properties:
        result.append('{0}Extra properties:'.format(''.ljust(indent)))
        indent += 2
        extra_props = object.properties
        keys = extra_props.keys()
        keys.sort()
        pad_length = max([len(key) for key in keys]) + 2
        for key in keys:
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                                             key.ljust(pad_length),
                                             extra_props[key]))
        indent -= 2

    for ec in object.owned_ecs:
        if long:
            result.append('{0}Execution Context {1}'.format(\
                    '-'.rjust(indent), ec.handle))
            padding = 7 # = len('State') + 2
            indent += 2
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                'State'.ljust(padding),
                ec.running_as_string(add_colour=use_colour)))
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                'Kind'.ljust(padding),
                ec.kind_as_string(add_colour=use_colour)))
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                'Rate'.ljust(padding), ec.rate))
            if ec.owner_name:
                result.append('{0}{1}{2}'.format(''.ljust(indent),
                    'Owner'.ljust(padding), ec.owner_name))
            if ec.participant_names:
                if really_long:
                        result.append('{0}{1}'.format('-'.rjust(indent),
                            'Participants'.ljust(padding)))
                        indent += 2
                        for pn in ec.participant_names:
                            result.append('{0}{1}'.format(''.ljust(indent),
                                pn))
                        indent -= 2
                else:
                    result.append('{0}{1}'.format('+'.rjust(indent),
                        'Participants'.ljust(padding)))
            if ec.properties:
                if really_long:
                    result.append('{0}{1}'.format('-'.rjust(indent),
                        'Extra properties'.ljust(padding)))
                    indent += 2
                    keys = ec.properties.keys()
                    keys.sort()
                    pad_length = max([len(key) for key in keys]) + 2
                    for key in keys:
                        result.append('{0}{1}{2}'.format(''.ljust(indent),
                             key.ljust(pad_length), ec.properties[key]))
                    indent -= 2
                else:
                    result.append('{0}{1}'.format('+'.rjust(indent),
                        'Extra properties'.ljust(padding)))
            indent -= 2
        else:
            result.append('{0}Execution Context {1}'.format(\
                    '+'.rjust(indent), ec.handle))

    for port in object.ports:
        if long:
            tag = '-'
        else:
            tag = '+'
        name_string = build_attr_string('bold', supported=use_colour) + \
                port.name + build_attr_string('reset', supported=use_colour)
        result.append('{0}{1}: {2}'.format(tag.rjust(indent), port.porttype,
                                           name_string))
        if long:
            indent += 2
            keys = port.properties.keys()
            keys.sort()
            pad_length = max([len(key) for key in keys]) + 2
            for key in keys:
                result.append('{0}{1}{2}'.format(''.ljust(indent),
                        key.ljust(pad_length), port.properties[key]))
            if port.porttype == 'CorbaPort' and port.interfaces:
                for intf in port.interfaces:
                    result.append('{0}Interface:'.format(''.ljust(indent)))
                    pad_length = 15 # = len('Instance name') + 2
                    indent += 2
                    result.append('{0}{1}{2}'.format(''.ljust(indent),
                            'Instance name'.ljust(pad_length),
                            intf.instance_name))
                    result.append('{0}{1}{2}'.format(''.ljust(indent),
                            'Type name'.ljust(pad_length),
                            intf.type_name))
                    result.append('{0}{1}{2}'.format(''.ljust(indent),
                            'Polarity'.ljust(pad_length),
                            intf.polarity_as_string(add_colour=use_colour)))
                    indent -= 2
            num_conns = len(port.connections)
            for conn in port.connections:
                if really_long:
                    tag2 = '-'
                else:
                    tag2 = '+'
                dest_ports = []
                for name, p in conn.ports:
                    if not object.get_port_by_ref(p.object):
                        dest_ports.append(name)
                        num_conns -= 1
                if dest_ports:
                    result.append('{0}Connected to  {1}'.format(\
                            tag2.rjust(indent),
                            build_attr_string('bold', supported=use_colour) +\
                            dest_ports[0] + \
                            build_attr_string('reset', supported=use_colour)))
                    if len(dest_ports) > 1:
                        for dp in dest_ports[1:]:
                            result.append('{0}{1}{2}'.format(''.ljust(indent),
                                                             ''.ljust(14),
                            build_attr_string('bold', supported=use_colour) +\
                            dp + \
                            build_attr_string('reset', supported=use_colour)))
                    if really_long:
                        indent += 2
                        keys = [k for k in conn.properties.keys() \
                                if not k.endswith('inport_ref') \
                                if not k.endswith('inport_ior')]
                        pad_length = max([len('Name')] + \
                                         [len(key) for key in keys]) + 2
                        result.append('{0}{1}{2}'.format(''.ljust(indent),
                                'Name'.ljust(pad_length), conn.name))<commit_msg>Handle the case where a port's owner is unknown<commit_after>#!/usr/bin/env python
# -*- Python -*-
# -*- coding: utf-8 -*-

'''rtcshell

Copyright (C) 2009-2010
    <NAME>
    RT-Synthesis Research Group
    Intelligent Systems Research Institute,
    National Institute of Advanced Industrial Science and Technology (AIST),
    Japan
    All rights reserved.
Licensed under the Eclipse Public License -v 1.0 (EPL)
http://www.opensource.org/licenses/eclipse-1.0.txt

File: rtcat.py

Implementation of the command to display component information.

'''

# $Source$


from optparse import OptionParser, OptionError
import os
from rtctree.exceptions import RtcTreeError
from rtctree.tree import create_rtctree
from rtctree.path import parse_path
from rtctree.utils import build_attr_string, get_num_columns_and_rows, \
                            get_terminal_size
import sys

from rtcshell import RTSH_PATH_USAGE, RTSH_VERSION
from rtcshell.path import cmd_path_to_full_path


def format_component(object, use_colour=True, long=False, really_long=False):
    result = []
    result.append('{0}  {1}'.format(object.name,
            object.get_state_string(add_colour=use_colour)))

    indent = 2
    profile_items = [('Category', object.category),
                     ('Description', object.description),
                     ('Instance name', object.instance_name),
                     ('Type name', object.type_name),
                     ('Vendor', object.vendor),
                     ('Version', object.version)]
    if object.parent:
        profile_items.append(('Parent', object.parent_object))
    pad_length = max([len(item[0]) for item in profile_items]) + 2
    for item in profile_items:
        result.append('{0}{1}{2}'.format(''.ljust(indent),
                                         item[0].ljust(pad_length),
                                         item[1]))

    if object.properties:
        result.append('{0}Extra properties:'.format(''.ljust(indent)))
        indent += 2
        extra_props = object.properties
        keys = extra_props.keys()
        keys.sort()
        pad_length = max([len(key) for key in keys]) + 2
        for key in keys:
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                                             key.ljust(pad_length),
                                             extra_props[key]))
        indent -= 2

    for ec in object.owned_ecs:
        if long:
            result.append('{0}Execution Context {1}'.format(\
                    '-'.rjust(indent), ec.handle))
            padding = 7 # = len('State') + 2
            indent += 2
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                'State'.ljust(padding),
                ec.running_as_string(add_colour=use_colour)))
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                'Kind'.ljust(padding),
                ec.kind_as_string(add_colour=use_colour)))
            result.append('{0}{1}{2}'.format(''.ljust(indent),
                'Rate'.ljust(padding), ec.rate))
            if ec.owner_name:
                result.append('{0}{1}{2}'.format(''.ljust(indent),
                    'Owner'.ljust(padding), ec.owner_name))
            if ec.participant_names:
                if really_long:
                        result.append('{0}{1}'.format('-'.rjust(indent),
                            'Participants'.ljust(padding)))
                        indent += 2
                        for pn in ec.participant_names:
                            result.append('{0}{1}'.format(''.ljust(indent),
                                pn))
                        indent -= 2
                else:
                    result.append('{0}{1}'.format('+'.rjust(indent),
                        'Participants'.ljust(padding)))
            if ec.properties:
                if really_long:
                    result.append('{0}{1}'.format('-'.rjust(indent),
                        'Extra properties'.ljust(padding)))
                    indent += 2
                    keys = ec.properties.keys()
                    keys.sort()
                    pad_length = max([len(key) for key in keys]) + 2
                    for key in keys:
                        result.append('{0}{1}{2}'.format(''.ljust(indent),
                             key.ljust(pad_length), ec.properties[key]))
                    indent -= 2
                else:
                    result.append('{0}{1}'.format('+'.rjust(indent),
                        'Extra properties'.ljust(padding)))
            indent -= 2
        else:
            result.append('{0}Execution Context {1}'.format(\
                    '+'.rjust(indent), ec.handle))

    for port in object.ports:
        if long:
            tag = '-'
        else:
            tag = '+'
        name_string = build_attr_string('bold', supported=use_colour) + \
                port.name + build_attr_string('reset', supported=use_colour)
        result.append('{0}{1}: {2}'.format(tag.rjust(indent), port.porttype,
                                           name_string))
        if long:
            indent += 2
            keys = port.properties.keys()
            keys.sort()
            pad_length = max([len(key) for key in keys]) + 2
            for key in keys:
                result.append('{0}{1}{2}'.format(''.ljust(indent),
                        key.ljust(pad_length), port.properties[key]))
            if port.porttype == 'CorbaPort' and port.interfaces:
                for intf in port.interfaces:
                    result.append('{0}Interface:'.format(''.ljust(indent)))
                    pad_length = 15 # = len('Instance name') + 2
                    indent += 2
                    result.append('{0}{1}{2}'.format(''.ljust(indent),
                            'Instance name'.ljust(pad_length),
                            intf.instance_name))
                    result.append('{0}{1}{2}'.format(''.ljust(indent),
                            'Type name'.ljust(pad_length),
                            intf.type_name))
                    result.append('{0}{1}{2}'.format(''.ljust(indent),
                            'Polarity'.ljust(pad_length),
                            intf.polarity_as_string(add_colour=use_colour)))
                    indent -= 2
            num_conns = len(port.connections)
            for conn in port.connections:
                if really_long:
                    tag2 = '-'
                else:
                    tag2 = '+'
                dest_ports = []
                for name, p in conn.ports:
                    # Handle the case of unknown port owners
                    if not p:
                        dest_ports.append(name)
                        num_conns -= 1
                    # Filter out ports belonging to this object
                    elif not object.get_port_by_ref(p.object):
                        dest_ports.append(name)
                        num_conns -= 1
                if dest_ports:
                    result.append('{0}Connected to  {1}'.format(\
                            tag2.rjust(indent),
                            build_attr_string('bold', supported=use_colour) +\
                            dest_ports[0] + \
                            build_attr_string('reset', supported=use_colour)))
                    if len(dest_ports) > 1:
                        for dp in dest_ports[1:]:
                            result.append('{0}{1}{2}'.format(''.ljust(indent),
                                                             ''.ljust(14),
                            build_attr_string('bold', supported=use_colour) +\
                            dp + \
                            build_attr_string('reset', supported=use_colour)))
                    if really_long:
                        indent += 2
                        keys = [k for k in conn.properties.keys() \
                                if not k.endswith('inport_ref') \
                                if not k.endswith('inport_ior')]
                        pad_length = max([len('Name')] + \
                                         [len(key) for key in keys]) + 2
                        result.append('{0}{1}{2}'.format(''.ljust(indent),
                                'Name'.ljust(pad_length), conn.name))
{'fraction_non_alphanumeric': 0.12452576184065597, 'fraction_numerical': 0.01352343654387468, 'mean_word_length': 1.3617052023121388, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1953491', 'n_tokens_mistral': 4467, 'n_tokens_neox': 4173, 'n_words': 813}