<commit_before>"""

.. program:: cyme

``cyme``
========

Cyme management utility

Options
-------

.. cmdoption:: -a, --app

    Application to use.  Required for all operations except for
    when creating, deleting or listing apps.

.. cmdoption:: -n, --nowait

    Don't want for operations to complete (async).

.. cmdoption:: -F, --format

    Output format: pretty (default) or json.

.. cmdoption:: -L, --local

    Flag that if set means that operations will be performed
    locally for the current branch only.
    This means the instance directory must be properly set.

.. cmdoption:: -l, --loglevel

    Set custom log level. One of DEBUG/INFO/WARNING/ERROR/CRITICAL.
    Default is INFO.

.. cmdoption:: -f, --logfile

    Set custom logfile path. Default is :file:`<stderr>`

.. cmdoption:: -D, --instance-dir

    Custom instance directory (default is :file:`instances/`)
    Must be readable by the current user.

    This needs to be properly specified if the :option:`-L` option
    is used.

"""

from __future__ import absolute_import

import json
import os
import pprint
import sys

from inspect import getargspec

from cyme.client import Client
from cyme.client.base import Model
from cyme.utils import cached_property, instantiate

from .base import CymeCommand, Option, die


def json_pretty(obj, out=sys.stdout):
    return json.dumps(obj, out, indent=4)


class I(object):

    def __init__(self, app=None, format=None, nowait=False, url=None, **kwargs):
        self.url = url
        self.app = app
        self.format = format or "pretty"
        self.nowait = nowait
        self.actions = {
            "apps": {
                "all": self.all_apps,
                "get": self.get_app,
                "add": self.add_app,
                "delete": self.delete_app},
            "instances": {
                "all": self.all_instances,
                "get": self.get_instance,
                "add": self.add_instance,
                "delete": self.delete_instance,
                "stats": self.instance_stats,
                "autoscale": self.instance_autoscale},
            "queues": {
                "all": self.all_queues,
                "get": self.get_queue,
                "add": self.add_queue,
                "delete": self.delete_queue},
            "consumers": {
                "all": self.all_consumers,
                "add": self.add_consumer,
                "delete": self.delete_consumer},
        }
        self.needs_app = ("instances", "queues")
        self.formats = {"jsonp": json_pretty,
                        "json": json.dumps,
                        "pprint": pprint.pformat}

    def getsig(self, fun, opt_args=None):
        spec = getargspec(fun)
        args = spec.args[:len(spec.defaults) if spec.defaults else None][1:]
        print("SPEC: %r ARGS %r" % (spec, args, ))
        if args[0] == "self":
            args = args[1:]
        if spec.defaults:
            opt_args = dict(zip(spec.args[len(spec.defaults):], spec.defaults))
        return len(args), args, opt_args

    def _ni(self, *args, **kwargs):
        raise NotImplementedError("subclass responsibility")
    all_apps = get_app = add_app = delete_app = \
        all_instances = get_instances = add_instance = delete_instance = \
            instance_stats = instance_autoscale = \
                all_queues = get_queue = add_queue = delete_queue = \
                    all_consumers = add_consumer = delete_consumer = _ni

    def DISPATCH(self, fqdn, *args):
        type, _, action = fqdn.partition('.')
        if not self.app and type in self.needs_app:
            die("Need to specify --app")
        try:
            handler = self.actions[type][action or "all"]
        except KeyError:
            if type:
                die("No action %r for type %r" % (action, type))
            die("Missing type")
        try:
            response = handler(*args)
        except TypeError:
            arity, args, optargs = self.getsig(handler)
            die("%s.%s requires %s argument%s: %s %s" % (
                type, action, arity, "s" if arity > 1 else "",
                " ".join(args), self.format_optargs(optargs)))
        return self.format_response(self.prepare_response(response))

    def format_optargs(self, optargs):
        if optargs:
            return " ".join("[%s]" % (k, ) for k in optargs.keys())
        return ''

    def format_response(self, ret):
        return self.formats[self.format](ret)

    def prepare_response(self, ret):
        return ret


class WebI(I):

    def all_apps(self):
        return list(self.client.all())

    def get_app(self, name):
        return self.client.get(name).info

    def add_app(self, name, broker=None):
        return self.client.add(name, nowait=self.nowait, broker=broker).info

    def delete_app(self, name):
        return self.client.delete(name, nowait=self.nowait)

    def all_instances(self):
        return list(self.client.instances.all())

    def get_instance(self, name):
        return self.client.instances.get(name)

    def add_instance(self, name=None, broker=None):
        return self.client.instances.add(name=name, broker=broker,
                                         nowait=self.nowait)

    def delete_instance(self, name):
        return self.client.instances.get(name).delete(nowait=self.nowait)

    def instance_stats(self, name):
        return self.client.instances.get(name).stats()

    def instance_autoscale(self, name, max=None, min=None):
        return self.client.instances.get(name).autoscale(max=max, min=min)<commit_msg>Adds 'branches' cmd to cyme util<commit_after>"""

.. program:: cyme

``cyme``
========

Cyme management utility

Options
-------

.. cmdoption:: -a, --app

    Application to use.  Required for all operations except for
    when creating, deleting or listing apps.

.. cmdoption:: -n, --nowait

    Don't want for operations to complete (async).

.. cmdoption:: -F, --format

    Output format: pretty (default) or json.

.. cmdoption:: -L, --local

    Flag that if set means that operations will be performed
    locally for the current branch only.
    This means the instance directory must be properly set.

.. cmdoption:: -l, --loglevel

    Set custom log level. One of DEBUG/INFO/WARNING/ERROR/CRITICAL.
    Default is INFO.

.. cmdoption:: -f, --logfile

    Set custom logfile path. Default is :file:`<stderr>`

.. cmdoption:: -D, --instance-dir

    Custom instance directory (default is :file:`instances/`)
    Must be readable by the current user.

    This needs to be properly specified if the :option:`-L` option
    is used.

"""

from __future__ import absolute_import

import json
import os
import pprint
import sys

from inspect import getargspec

from cyme.client import Client
from cyme.client.base import Model
from cyme.utils import cached_property, instantiate

from .base import CymeCommand, Option, die


def json_pretty(obj, out=sys.stdout):
    return json.dumps(obj, out, indent=4)


class I(object):

    def __init__(self, app=None, format=None, nowait=False, url=None, **kwargs):
        self.url = url
        self.app = app
        self.format = format or "pretty"
        self.nowait = nowait
        self.actions = {
            "branches": {
                "all": self.all_branches,
            },
            "apps": {
                "all": self.all_apps,
                "get": self.get_app,
                "add": self.add_app,
                "delete": self.delete_app},
            "instances": {
                "all": self.all_instances,
                "get": self.get_instance,
                "add": self.add_instance,
                "delete": self.delete_instance,
                "stats": self.instance_stats,
                "autoscale": self.instance_autoscale},
            "queues": {
                "all": self.all_queues,
                "get": self.get_queue,
                "add": self.add_queue,
                "delete": self.delete_queue},
            "consumers": {
                "all": self.all_consumers,
                "add": self.add_consumer,
                "delete": self.delete_consumer},
        }
        self.needs_app = ("instances", "queues")
        self.formats = {"jsonp": json_pretty,
                        "json": json.dumps,
                        "pprint": pprint.pformat}

    def getsig(self, fun, opt_args=None):
        spec = getargspec(fun)
        args = spec.args[:len(spec.defaults) if spec.defaults else None][1:]
        print("SPEC: %r ARGS %r" % (spec, args, ))
        if args[0] == "self":
            args = args[1:]
        if spec.defaults:
            opt_args = dict(zip(spec.args[len(spec.defaults):], spec.defaults))
        return len(args), args, opt_args

    def _ni(self, *args, **kwargs):
        raise NotImplementedError("subclass responsibility")
    all_apps = get_app = add_app = delete_app = \
        all_instances = get_instances = add_instance = delete_instance = \
            instance_stats = instance_autoscale = \
                all_queues = get_queue = add_queue = delete_queue = \
                    all_consumers = add_consumer = delete_consumer = _ni

    def DISPATCH(self, fqdn, *args):
        type, _, action = fqdn.partition('.')
        if not self.app and type in self.needs_app:
            die("Need to specify --app")
        try:
            handler = self.actions[type][action or "all"]
        except KeyError:
            if type:
                die("No action %r for type %r" % (action, type))
            die("Missing type")
        try:
            response = handler(*args)
        except TypeError:
            arity, args, optargs = self.getsig(handler)
            die("%s.%s requires %s argument%s: %s %s" % (
                type, action, arity, "s" if arity > 1 else "",
                " ".join(args), self.format_optargs(optargs)))
        return self.format_response(self.prepare_response(response))

    def format_optargs(self, optargs):
        if optargs:
            return " ".join("[%s]" % (k, ) for k in optargs.keys())
        return ''

    def format_response(self, ret):
        return self.formats[self.format](ret)

    def prepare_response(self, ret):
        return ret


class WebI(I):

    def all_branches(self):
        return list(self.client.branches)

    def all_apps(self):
        return list(self.client.all())

    def get_app(self, name):
        return self.client.get(name).info

    def add_app(self, name, broker=None):
        return self.client.add(name, nowait=self.nowait, broker=broker).info

    def delete_app(self, name):
        return self.client.delete(name, nowait=self.nowait)

    def all_instances(self):
        return list(self.client.instances.all())

    def get_instance(self, name):
        return self.client.instances.get(name)

    def add_instance(self, name=None, broker=None):
        return self.client.instances.add(name=name, broker=broker,
                                         nowait=self.nowait)

    def delete_instance(self, name):
        return self.client.instances.get(name).delete(nowait=self.nowait)

    def instance_stats(self, name):
        return self.client.instances.get(name).stats()

    def instance_autoscale(self, name, max=None, min=None):
        return self.client.instances.get(name).autoscale(max=max, min=min)
{'fraction_non_alphanumeric': 0.12081245054075442, 'fraction_numerical': 0.0008792754770069463, 'mean_word_length': 2.1585670647042488, 'pattern_counts': {'":': 52, '<': 5, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5427543', 'n_tokens_mistral': 3469, 'n_tokens_neox': 3325, 'n_words': 887}