<commit_before>#!/usr/bin/env python
# coding: utf-8
from cement.core import controller
from nepho.cli import base, scope
from os import path
import argparse
from termcolor import colored
from textwrap import TextWrapper

import nepho.core.config
from nepho.core import common, cloudlet


# Python 2.x vs 3.x input handling change
try:
    import __builtin__
    input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
    pass


class NephoCloudletController(base.NephoBaseController):
    class Meta:
        label = 'cloudlet'
        interface = controller.IController
        stacked_on = 'base'
        stacked_type = 'nested'
        description = 'find, download, and manage cloudlets'
        usage = "nepho cloudlet <action> [options]"
        arguments = [
            (['--force', '-f'], dict(action='store_true', dest='force', help=argparse.SUPPRESS)),
            (['--location', '-l'], dict(dest='location', help=argparse.SUPPRESS)),
            (['cloudlet'], dict(help=argparse.SUPPRESS, nargs='?')),
            (['query'], dict(help=argparse.SUPPRESS, nargs='*')),
        ]

    def _setup(self, app):
        super(NephoCloudletController, self)._setup(app)
        self.nepho_config = nepho.core.config.ConfigManager(self.app.config)
        self.cloudletManager = cloudlet.CloudletManager(self.nepho_config)

    @controller.expose(help="List all installed cloudlets")
    def list(self):
        cloudlets = self.cloudletManager.list()

        dir = ""
        items = list()

        try:
            for cloudlt in cloudlets:  # sorted(all_cloudlets):
                # Print directory if it changes
                cloudlet_path = cloudlt.get_path()
                if dir != path.dirname(cloudlet_path):
                    dir = path.dirname(cloudlet_path)
                    print colored(dir, "cyan")
                name = path.basename(cloudlet_path)

                # If there are multiple versions of a cloudlet with the same name,
                # subsequent versions will be ignored by other commands
                if name not in items:
                    try:
                        y = cloudlt.definition
                    except:
                        print colored("└──", "yellow"), name, "(", colored("error", "red"), "- missing or malformed cloudlet.yaml )"
                    else:
                        print colored("└──", "yellow"), name, "(", colored("v%s", "blue") % (y['version']), ")"
                    items.append(name)
                else:
                    print colored("└──", "yellow"), name, "(", colored("error", "red"), "- duplicate cloudlet will be ignored )"
        except TypeError:
            print colored("└──", "yellow"), colored("No cloudlets installed.", "blue")

        return

        #cloudlet.list_all_cloudlets(self)

    @controller.expose(help="Describe an installed cloudlet")
    def describe(self):
        if self.app.cloudlet_name is None:
            print "Usage: nepho cloudlet describe <cloudlet>"
            exit(1)
        else:
            scope.print_scope(self)

        c = self.cloudletManager.find(self.app.cloudlet_name)
        if c is None:
            print colored("Error:", "red") + "No cloudlet named \"%s\" found." % (self.cloudlet_name)
            exit(1)
        else:
            wrapper = TextWrapper(width=80, subsequent_indent="              ")
            d = c.definition

            print "-" * 80
            print "Name:         %s" % (d['name'])
            print "Version:      %s" % (d['version'])
            print "Author:       %s" % (d['author'])
            print "License:      %s" % (d['license'])
            print wrapper.fill("Summary:      %s" % (d['summary']))
            print wrapper.fill("Description:  %s" % (d['description']))
            print "-" * 80
        return

    @controller.expose(help="Search the Nepho Cloudlet Registry for cloudlets whose names, summaries, or descriptions match the provided search term")
    def search(self):
        # TODO: Improve search beyond basic string matching

        if self.app.pargs.cloudlet is not None:
            query = self.app.pargs.cloudlet + ' ' + ' '.join(self.app.pargs.query)
        else:
            query = ""
        registry = self.cloudletManager.get_registry()

        print "Searching for %s" % query
        matchList = list()
        for cloudletRepo in registry.keys():
            flattenedText = "%s: %s" % (cloudletRepo, registry[cloudletRepo])
            if (query == '') or (query.trim() in flattenedText):
                matchList.append(cloudletRepo)

        if len(matchList) == 0:
            print "No matches found."
        else:
            for cloudletRepo in sorted(matchList):
                cloudletDict = registry[cloudletRepo]

                wrapper = TextWrapper(width=80, subsequent_indent="              ")
                print "-" * 80
                print "Name:         %s" % (cloudletRepo)
                print "Version:      %s" % (cloudletDict['version'])
                print "Author:       %s" % (cloudletDict['author'])
                print "License:      %s" % (cloudletDict['license'])<commit_msg>Unable to replicate reliably<commit_after>#!/usr/bin/env python
# coding: utf-8
from cement.core import controller
from nepho.cli import base, scope
from os import path
import argparse
from termcolor import colored
from textwrap import TextWrapper

import nepho.core.config
from nepho.core import common, cloudlet


# Python 2.x vs 3.x input handling change
try:
    import __builtin__
    input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
    pass


class NephoCloudletController(base.NephoBaseController):
    class Meta:
        label = 'cloudlet'
        interface = controller.IController
        stacked_on = 'base'
        stacked_type = 'nested'
        description = 'find, download, and manage cloudlets'
        usage = "nepho cloudlet <action> [options]"
        arguments = [
            (['--force', '-f'], dict(action='store_true', dest='force', help=argparse.SUPPRESS)),
            (['--location', '-l'], dict(dest='location', help=argparse.SUPPRESS)),
            (['cloudlet'], dict(help=argparse.SUPPRESS, nargs='?')),
            (['query'], dict(help=argparse.SUPPRESS, nargs='*')),
        ]

    def _setup(self, app):
        super(NephoCloudletController, self)._setup(app)
        self.nepho_config = nepho.core.config.ConfigManager(self.app.config)
        self.cloudletManager = cloudlet.CloudletManager(self.nepho_config)

    @controller.expose(help="List all installed cloudlets")
    def list(self):
        cloudlets = self.cloudletManager.list()

        dir = ""
        items = list()

        try:
            for cloudlt in cloudlets:  # sorted(all_cloudlets):
                # Print directory if it changes
                cloudlet_path = cloudlt.get_path()
                if dir != path.dirname(cloudlet_path):
                    dir = path.dirname(cloudlet_path)
                    print colored(dir, "cyan")
                name = path.basename(cloudlet_path)

                # If there are multiple versions of a cloudlet with the same name,
                # subsequent versions will be ignored by other commands
                if name not in items:
                    try:
                        y = cloudlt.definition
                    except:
                        print colored("└──", "yellow"), name, "(", colored("error", "red"), "- missing or malformed cloudlet.yaml )"
                    else:
                        print colored("└──", "yellow"), name, "(", colored("v%s", "blue") % (y['version']), ")"
                    items.append(name)
                else:
                    print colored("└──", "yellow"), name, "(", colored("error", "red"), "- duplicate cloudlet will be ignored )"
        except TypeError:
            print colored("└──", "yellow"), colored("No cloudlets installed.", "blue")

        return

        #cloudlet.list_all_cloudlets(self)

    @controller.expose(help="Describe an installed cloudlet")
    def describe(self):
        if self.app.cloudlet_name is None:
            print "Usage: nepho cloudlet describe <cloudlet>"
            exit(1)
        else:
            scope.print_scope(self)

        c = self.cloudletManager.find(self.app.cloudlet_name)
        if c is None:
            print colored("Error:", "red") + "No cloudlet named \"%s\" found." % (self.cloudlet_name)
            exit(1)
        else:
            wrapper = TextWrapper(width=80, subsequent_indent="              ")
            d = c.definition

            print "-" * 80
            print "Name:         %s" % (d['name'])
            print "Version:      %s" % (d['version'])
            print "Author:       %s" % (d['author'])
            print "License:      %s" % (d['license'])
            print wrapper.fill("Summary:      %s" % (d['summary']))
            print wrapper.fill("Description:  %s" % (d['description']))
            print "-" * 80
        return

    @controller.expose(help="Search the Nepho Cloudlet Registry for cloudlets whose names, summaries, or descriptions match the provided search term")
    def search(self):
        # TODO: Improve search beyond basic string matching

        if self.app.pargs.cloudlet is not None:
            query = self.app.pargs.cloudlet + ' ' + ' '.join(self.app.pargs.query)
        else:
            query = ""
        registry = self.cloudletManager.get_registry()

        print "Searching for %s" % query
        matchList = list()
        for cloudletRepo in registry.keys():
            flattenedText = "%s: %s" % (cloudletRepo, registry[cloudletRepo])
            if (query == '') or (query.strip() in flattenedText):
                matchList.append(cloudletRepo)

        if len(matchList) == 0:
            print "No matches found."
        else:
            for cloudletRepo in sorted(matchList):
                cloudletDict = registry[cloudletRepo]

                wrapper = TextWrapper(width=80, subsequent_indent="              ")
                print "-" * 80
                print "Name:         %s" % (cloudletRepo)
                print "Version:      %s" % (cloudletDict['version'])
                print "Author:       %s" % (cloudletDict['author'])
                print "License:      %s" % (cloudletDict['license'])
{'fraction_non_alphanumeric': 0.12377474533922737, 'fraction_numerical': 0.003075148952527388, 'mean_word_length': 2.0043302540415704, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4784735', 'n_tokens_mistral': 2914, 'n_tokens_neox': 2803, 'n_words': 742}