text:
from __future__ import absolute_import

import os
import signal
import subprocess
import sys

from six import print_

from ccmlib import common
from ccmlib.cmds.command import Cmd
from ccmlib.node import NodeError

NODE_CMDS = [
    "show",
    "remove",
    "showlog",
    "setlog",
    "start",
    "stop",
    "ring",
    "flush",
    "compact",
    "drain",
    "cleanup",
    "repair",
    "scrub",
    "verify",
    "shuffle",
    "sstablesplit",
    "getsstables",
    "decommission",
    "json",
    "updateconf",
    "updatelog4j",
    "stress",
    "cli",
    "cqlsh",
    "scrub",
    "verify",
    "status",
    "setdir",
    "bulkload",
    "version",
    "nodetool",
    "dsetool",
    "setworkload",
    "dse",
    "hadoop",
    "hive",
    "pig",
    "sqoop",
    "spark",
    "pause",
    "resume",
    "jconsole",
    "versionfrombuild",
    "byteman"
]

def commands():
    return NODE_CMDS

class NodeShowCmd(Cmd):

    descr_text = "Display information on a node"
    usage = "usage: ccm node_name show [options]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        self.node.show()

class NodeRemoveCmd(Cmd):

    descr_text = "Remove a node (stopping it if necessary and deleting all its data)"
    usage = "usage: ccm node_name remove [options]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        self.cluster.remove(self.node)

class NodeShowlogCmd(Cmd):

    descr_text = "Show the log of node name (runs your $PAGER on its system.log)"
    usage = "usage: ccm node_name showlog [options]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        log = self.node.logfilename()
        pager = os.environ.get('PAGER', common.platform_pager())
        os.execvp(pager, (pager, log))

class NodeSetlogCmd(Cmd):

    options_list = [
        (['-c', '--class'], {'type': "string", 'dest': "class_name", 'default': None, 'help': "Optional java class/package. Logging will be set for only this class/package if set"}),
    ]
    descr_text = "Set node name log level (INFO, DEBUG, ...) with/without Java class - require a node restart"
    usage = "usage: ccm node_name setlog [options] level"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        if len(args) == 1:
            print_('Missing log level', file=sys.stderr)
            parser.print_help()
        self.level = args[1]

        try:
            self.class_name = options.class_name
        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

    def run(self):
        try:
            self.node.set_log_level(self.level, self.class_name)

        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

class NodeClearCmd(Cmd):

    options_list = [
        (['-a', '--all'], {'action': "store_true", 'dest': "all", 'help': "Also clear the saved cache and node log files", 'default': False}),
    ]
    descr_text = "Clear the node data & logs (and stop the node)"
    usage = "usage: ccm node_name_clear [options]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        self.node.stop()
        self.node.clear(self.options.all)

class NodeStartCmd(Cmd):

    options_list = [
        (['-v', '--verbose'], {'action': "store_true", 'dest': "verbose", 'help': "Print standard output of cassandra process", 'default': False}),
        (['--no-wait'], {'action': "store_true", 'dest': "no_wait", 'help': "Do not wait for cassandra node to be ready", 'default': False}),
        (['--wait-other-notice'], {'action': "store_true", 'dest': "deprecate", 'help': "DEPRECATED/IGNORED: Use '--skip-wait-other-notice' instead. This is now on by default.", 'default': False}),
        (['--skip-wait-other-notice'], {'action': "store_false", 'dest': "wait_other_notice", 'help': "Skip waiting until all live nodes of the cluster have marked the other nodes UP", 'default': True}),
        (['--wait-for-binary-proto'], {'action': "store_true", 'dest': "wait_for_binary_proto", 'help': "Wait for the binary protocol to start", 'default': False}),
        (['-j', '--dont-join-ring'], {'action': "store_true", 'dest': "no_join_ring", 'help': "Launch the instance without joining the ring", 'default': False}),
        (['--replace-address'], {'type': "string", 'dest': "replace_address", 'default': None, 'help': "Replace a node in the ring through the cassandra.replace_address option"}),
        (['--jvm_arg'], {'action': "append", 'dest': "jvm_args", 'help': "Specify a JVM argument", 'default': []}),
        (['--quiet-windows'], {'action': "store_true", 'dest': "quiet_start", 'help': "Pass -q on Windows 2.2.4+ and 3.0+ startup. Ignored on linux.", 'default': False}),
        (['--root'], {'action': "store_true", 'dest': "allow_root", 'help': "Allow CCM to start cassandra as root", 'default': False}),
    ]
    descr_text = "Start a node"
    usage = "usage: ccm node start [options] name"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        try:
            self.node.start(not self.options.no_join_ring,
                            no_wait=self.options.no_wait,
                            wait_other_notice=self.options.wait_other_notice,
                            wait_for_binary_proto=self.options.wait_for_binary_proto,
                            verbose=self.options.verbose,
                            replace_address=self.options.replace_address,
                            jvm_args=self.options.jvm_args,
                            quiet_start=self.options.quiet_start,
                            allow_root=self.options.allow_root)
        except NodeError as e:
            print_(str(e), file=sys.stderr)
            print_("Standard error output is:", file=sys.stderr)
            for line in e.process.stderr:
                print_(line.rstrip('\n'), file=sys.stderr)
            exit(1)

class NodeStopCmd(Cmd):

    options_list = [
        (['--no-wait'], {'action': "store_true", 'dest': "no_wait", 'help': "Do not wait for the node to be stopped", 'default': False}),
        (['-g', '--gently'], {'action': "store_const", 'dest': "signal_event", 'help': "Shut down gently (default)", 'const': signal.SIGTERM, 'default': signal.SIGTERM}),
        (['--hang-up'], {'action': "store_const", 'dest': "signal_event", 'help': "Shut down via hang up (kill -1)", 'const': common.get_default_signals()['1']}),
        (['--not-gently'], {'action': "store_const", 'dest': "signal_event", 'help': "Shut down immediately (kill -9)", 'const': common.get_default_signals()['9']}),
    ]
    descr_text = "Stop a node"
    usage = "usage: ccm node stop [options] name"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        try:
            if not self.node.stop(wait=not self.options.no_wait, signal_event=self.options.signal_event):
                print_("%s is not running" % self.name, file=sys.stderr)
                exit(1)
        except NodeError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

class _NodeToolCmd(Cmd):
    usage = "This is a private class, how did you get here?"
    descr_text = "This is a private class, how did you get here?"
    nodetool_cmd = ''

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        stdout, stderr, rc = self.node.nodetool(self.nodetool_cmd + " " + " ".join((self.args[1:])))
        print_(stderr)
        print_(stdout)

class NodeNodetoolCmd(_NodeToolCmd):
    usage = "usage: ccm node_name nodetool [options]"
    descr_text = "Run nodetool (connecting to node name)"

    def run(self):
        stdout, stderr, rc = self.node.nodetool(" ".join(self.args[1:]))
        print_(stderr)
        print_(stdout)

class NodeRingCmd(_NodeToolCmd):
    usage = "usage: ccm node_name ring [options]"
    nodetool_cmd = 'ring'
    descr_text = "Print ring (connecting to node name)"

class NodeStatusCmd(_NodeToolCmd):
    usage = "usage: ccm node_name status [options]"
    nodetool_cmd = 'status'
    descr_text = "Print status (connecting to node name)"

class NodeFlushCmd(_NodeToolCmd):
    usage = "usage: ccm node_name flush [options]"
    nodetool_cmd = 'flush'
    descr_text = "Flush node name"

class NodeCompactCmd(_NodeToolCmd):
    usage = "usage: ccm node_name compact [options]"
    nodetool_cmd = 'compact'
    descr_text = "Compact node name"

class NodeDrainCmd(_NodeToolCmd):
    usage = "usage: ccm node_name drain [options]"
    nodetool_cmd = 'drain'
    descr_text = "Drain node name"

class NodeCleanupCmd(_NodeToolCmd):
    usage = "usage: ccm node_name cleanup [options]"
    nodetool_cmd = 'cleanup'
    descr_text = "Run cleanup on node name"

class NodeRepairCmd(_NodeToolCmd):
    usage = "usage: ccm node_name repair [options]"
    nodetool_cmd = 'repair'
    descr_text = "Run repair on node name"

class NodeVersionCmd(_NodeToolCmd):
    usage = "usage: ccm node_name version"
    nodetool_cmd = 'version'
    descr_text = "Get the cassandra version of node"

class NodeDecommissionCmd(_NodeToolCmd):
    usage = "usage: ccm node_name decommission [options]"
    options_list = [
        (['--force'], {'action': "store_true", 'dest': "force", 'help': "Force decommission of this node even when it reduces the number of replicas to below configured RF.  Note: This is only relevant for C* 3.12+.", 'default': False}),
    ]
    nodetool_cmd = 'decommission'
    descr_text = "Run decommission on node name"

    def run(self):
        self.node.decommission(force=self.options.force)

class _DseToolCmd(Cmd):
    usage = "This is a private class, how did you get here?"
    descr_text = "This is a private class, how did you get here?"
    dsetool_cmd = ''

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        stdout, stderr, rc = self.node.dsetool(self.dsetool_cmd + " " + " ".join((self.args[1:])))
        print_(stderr)
        print_(stdout)

class NodeDsetoolCmd(_DseToolCmd):
    usage = "usage: ccm node_name dsetool [options]"
    descr_text = "Run dsetool (connecting to node name)"

    def run(self):
        stdout, stderr, rc = self.node.dsetool(" ".join(self.args[1:]))
        print_(stderr)
        print_(stdout)

class NodeCliCmd(Cmd):

    options_list = [
        (['-x', '--exec'], {'type': "string", 'dest': "cmds", 'default': None, 'help': "Execute the specified commands and exit"}),
        (['-v', '--verbose'], {'action': "store_true", 'dest': "verbose", 'help': "With --exec, show cli output after completion", 'default': False}),
    ]
    descr_text = "Launch a cassandra cli connected to this node"
    usage = "usage: ccm node_name cli [options] [cli_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.cli_options = parser.get_ignored() + args[1:]

    def run(self):
        out, err, rc = self.node.run_cli(self.options.cmds, self.cli_options)
        if self.options.verbose:
            print_("CLI OUTPUT:\n-------------------------------")
            print_(out)
            print_("-------------------------------\nCLI ERROR:\n-------------------------------")
            print_(err)

class NodeCqlshCmd(Cmd):

    options_list = [
        (['-x', '--exec'], {'type': "string", 'dest': "cmds", 'default': None, 'help': "Execute the specified commands and exit"}),
        (['-v', '--verbose'], {'action': "store_true", 'dest': "verbose", 'help': "With --exec, show cli output after completion", 'default': False}),
    ]
    descr_text = "Launch a cqlsh session connected to this node"
    usage = "usage: ccm node_name cqlsh [options] [cli_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.cqlsh_options = parser.get_ignored() + args[1:]

    def run(self):
        self.node.run_cqlsh(self.options.cmds, self.cqlsh_options)

class NodeBulkloadCmd(Cmd):

    descr_text = "Bulkload files into the cluster by connecting to this node"
    usage = "usage: ccm node_name bulkload [options] [sstable_dir]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.loader_options = parser.get_ignored() + args[1:]

    def run(self):
        self.node.bulkload(self.loader_options)

class NodeScrubCmd(Cmd):

    descr_text = "Scrub files"
    usage = "usage: ccm node_name scrub [options] <keyspace> <cf>"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.scrub_options = parser.get_ignored() + args[1:]

    def run(self):
        self.node.scrub(self.scrub_options)

class NodeVerifyCmd(Cmd):

    descr_text = "Verify files"
    usage = "usage: ccm node_name verify [options] <keyspace> <cf>"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.verify_options = parser.get_ignored() + args[1:]

    def run(self):
        self.node.verify(self.verify_options)

class NodeJsonCmd(Cmd):

    options_list = [
        (['-k', '--keyspace'], {'type': "string", 'dest': "keyspace", 'default': None, 'help': "The keyspace to use [use all keyspaces by default]"}),
        (['-c', '--column-families'], {'type': "string", 'dest': "cfs", 'default': None, 'help': "Comma separated list of column families to use (requires -k to be set)"}),
        (['--key'], {'type': "string", 'action': "append", 'dest': "keys", 'default': None, 'help': "The key to include (you may specify multiple --key)"}),
        (['-e', '--enumerate-keys'], {'action': "store_true", 'dest': "enumerate_keys", 'help': "Only enumerate keys (i.e, call sstable2keys)", 'default': False}),
    ]
    descr_text = "Call sstable2json/sstabledump on the sstables of this node"
    usage = "usage: ccm node_name json [options] [file]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.keyspace = options.keyspace
        if self.keyspace is None:
            print_("You must specify a keyspace.")
            parser.print_help()
            exit(1)
        self.outfile = args[-1] if len(args) >= 2 else None
        self.column_families = options.cfs.split(',') if options.cfs else None

    def run(self):
        try:
            f = sys.stdout
            if self.outfile is not None:
                f = open(self.outfile, 'w')
            if self.node.has_cmd('sstable2json'):
                self.node.run_sstable2json(keyspace=self.keyspace,
                                           out_file=f,
                                           column_families=self.column_families,
                                           keys=self.options.keys,
                                           enumerate_keys=self.options.enumerate_keys)
            elif self.node.has_cmd('sstabledump'):
                self.node.run_sstabledump(keyspace=self.keyspace,
                                          column_families=self.column_families,
                                          keys=self.options.keys,
                                          enumerate_keys=self.options.enumerate_keys,
                                          command=True)
        except common.ArgumentError as e:
            print_(e, file=sys.stderr)

class NodeSstablesplitCmd(Cmd):

    options_list = [
        (['-k', '--keyspace'], {'type': "string", 'dest': "keyspace", 'default': None, 'help': "The keyspace to use [use all keyspaces by default]"}),
        (['-c', '--column-families'], {'type': "string", 'dest': 'cfs', 'default': None, 'help': "Comma separated list of column families to use (requires -k to be set)"}),
        (['-s', '--size'], {'type': 'int', 'dest': "size", 'default': None, 'help': "Maximum size in MB for the output sstables (default: 50 MB)"}),
        (['--no-snapshot'], {'action': 'store_true', 'dest': "no_snapshot", 'default': False, 'help': "Don't snapshot the sstables before splitting"}),
    ]
    descr_text = "Run sstablesplit on the sstables of this node"
    usage = "usage: ccm node_name sstablesplit [options] [file]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.keyspace = options.keyspace
        self.size = options.size
        self.no_snapshot = options.no_snapshot
        self.column_families = None
        self.datafiles = None
        if options.cfs is not None:
            if self.keyspace is None:
                print_("You need a keyspace (option -k) if you specify column families", file=sys.stderr)
                exit(1)
            self.column_families = options.cfs.split(',')

        if len(args) > 1:
            if self.column_families is None:
                print_("You need a column family (option -c) if you specify datafiles", file=sys.stderr)
                exit(1)
            self.datafiles = args[1:]

    def run(self):
        self.node.run_sstablesplit(datafiles=self.datafiles, keyspace=self.keyspace,
                                   column_families=self.column_families, size=self.size,
                                   no_snapshot=self.no_snapshot)

class NodeGetsstablesCmd(Cmd):

    options_list = [
        (['-k', '--keyspace'], {'type': "string", 'dest': "keyspace", 'default': None, 'help': "The keyspace to use [use all keyspaces by default]"}),
        (['-t', '--tables'], {'type': "string", 'dest': 'tables', 'default': None, 'help': "Comma separated list of tables to use (requires -k to be set)"}),
    ]
    descr_text = "Run getsstables to get absolute path of sstables in this node"
    usage = "usage: ccm node_name getsstables [options] [file]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.keyspace = options.keyspace
        self.tables = None
        self.datafiles = None
        if options.tables is not None:
            if self.keyspace is None:
                print_("You need a keyspace (option -k) if you specify tables", file=sys.stderr)
                exit(1)
            self.tables = options.tables.split(',')

        if len(args) > 1:
            if self.tables is None:
                print_("You need a tables (option -t) if you specify datafiles", file=sys.stderr)
                exit(1)
            self.datafiles = args[1:]

    def run(self):
        sstablefiles = self.node.get_sstablespath(datafiles=self.datafiles, keyspace=self.keyspace,
                                                  tables=self.tables)
        print_('\n'.join(sstablefiles))

class NodeUpdateconfCmd(Cmd):

    options_list = [
        (['--no-hh', '--no-hinted-handoff'], {'action': "store_false", 'dest': "hinted_handoff", 'default': True, 'help': "Disable hinted handoff"}),
        (['--batch-cl', '--batch-commit-log'], {'action': "store_true", 'dest': "cl_batch", 'default': None, 'help': "Set commit log to batch mode"}),
        (['--periodic-cl', '--periodic-commit-log'], {'action': "store_true", 'dest': "cl_periodic", 'default': None, 'help': "Set commit log to periodic mode"}),
        (['--rt', '--rpc-timeout'], {'action': "store", 'type': 'int', 'dest': "rpc_timeout", 'help': "Set rpc timeout"}),
        (['-y', '--yaml'], {'action': "store_true", 'dest': "literal_yaml", 'default': False, 'help': "Pass in literal yaml string. Option syntax looks like ccm node_name updateconf -y 'a: [b: [c,d]]'"}),
    ]
    descr_text = "Update the cassandra config files for this node (useful when updating cassandra)"
    usage = "usage: ccm node_name updateconf [options] [ new_setting | ...  ], where new_setting should be a string of the form 'compaction_throughput_mb_per_sec: 32'"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        args = args[1:]
        try:
            self.setting = common.parse_settings(args, literal_yaml=self.options.literal_yaml)
            if self.options.cl_batch and self.options.cl_periodic:
                print_("Can't set commitlog to be both batch and periodic.{}".format(os.linesep))
                parser.print_help()
                exit(1)
        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

    def run(self):
        self.setting['hinted_handoff_enabled'] = self.options.hinted_handoff
        if self.options.rpc_timeout is not None:
            if self.node.cluster.cassandra_version() < "1.2":
                self.setting['rpc_timeout_in_ms'] = self.options.rpc_timeout
            else:
                self.setting['read_request_timeout_in_ms'] = self.options.rpc_timeout
                self.setting['range_request_timeout_in_ms'] = self.options.rpc_timeout
                self.setting['write_request_timeout_in_ms'] = self.options.rpc_timeout
                self.setting['truncate_request_timeout_in_ms'] = self.options.rpc_timeout
                self.setting['request_timeout_in_ms'] = self.options.rpc_timeout
        self.node.set_configuration_options(values=self.setting)
        if self.options.cl_batch:
            self.node.set_batch_commitlog(True)
        if self.options.cl_periodic:
            self.node.set_batch_commitlog(False)

class NodeUpdatedseconfCmd(Cmd):

    options_list = [
        (['-y', '--yaml'], {'action': "store_true", 'dest': "literal_yaml", 'default': False, 'help': "Pass in literal yaml string. Option syntax looks like ccm node_name updatedseconf -y 'a: [b: [c,d]]'"}),
    ]
    descr_text = "Update the dse config files for this node"
    usage = "usage: ccm node_name updatedseconf [options] [ new_setting | ...  ], where new setting should be a string of the form 'max_solr_concurrency_per_core: 2'; nested options can be separated with a period like 'cql_slow_log_options.enabled: true'"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        args = args[1:]
        try:
            self.setting = common.parse_settings(args, literal_yaml=self.options.literal_yaml)
        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

    def run(self):
        self.node.set_dse_configuration_options(values=self.setting)

#
# Class implementens the functionality of updating log4j-server.properties
# on the given node by copying the given config into
# ~/.ccm/name-of-cluster/nodeX/conf/log4j-server.properties
#

class NodeUpdatelog4jCmd(Cmd):

    options_list = [
        (['-p', '--path'], {'type': "string", 'dest': "log4jpath", 'help': "Path to new Cassandra log4j configuration file"}),
    ]
    descr_text = "Update the Cassandra log4j-server.properties configuration file under given node"
    usage = "usage: ccm node_name updatelog4j -p <log4j config>"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        try:
            self.log4jpath = options.log4jpath
            if self.log4jpath is None:
                raise KeyError("[Errno] -p or --path <path of new log4j configuration file> is not provided")
        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)
        except KeyError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

    def run(self):
        try:
            self.node.update_log4j(self.log4jpath)
        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

class NodeStressCmd(Cmd):

    descr_text = "Run stress on a node"
    usage = "usage: ccm node_name stress [options] [stress_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.stress_options = args[1:] + parser.get_ignored()

    def run(self):
        try:
            self.node.stress(self.stress_options)
        except OSError:
            print_("Could not find stress binary (you may need to build it)", file=sys.stderr)

class NodeShuffleCmd(Cmd):

    descr_text = "Run shuffle on a node"
    usage = "usage: ccm node_name shuffle [options] [shuffle_cmds]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.shuffle_cmd = args[1]

    def run(self):
        self.node.shuffle(self.shuffle_cmd)

class NodeSetdirCmd(Cmd):

    options_list = [
        (['-v', "--version"], {'type': "string", 'dest': "version", 'help': "Download and use provided cassandra or dse version. If version is of the form 'git:<branch name>', then the specified branch will be downloaded from the git repo and compiled. (takes precedence over --install-dir)", 'default': None}),
        (["--install-dir"], {'type': "string", 'dest': "install_dir", 'help': "Path to the cassandra or dse directory to use [default %default]", 'default': "./"}),
    ]
    descr_text = "Set the cassandra directory to use for the node"
    usage = "usage: ccm node_name setdir [options]"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        try:
            self.node.set_install_dir(install_dir=self.options.install_dir, version=self.options.version, verbose=True)
        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

class NodeSetworkloadCmd(Cmd):

    descr_text = "Sets the workloads for a DSE node"
    usage = "usage: ccm node_name setworkload [cassandra|solr|hadoop|spark|dsefs|cfs|graph],..."

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.workloads = args[1].split(',')
        valid_workloads = ['cassandra', 'solr', 'hadoop', 'spark', 'dsefs', 'cfs', 'graph']
        for workload in self.workloads:
            if workload not in valid_workloads:
                print_(workload, ' is not a valid workload')
                exit(1)

    def run(self):
        try:
            self.node.set_workloads(workloads=self.workloads)
        except common.ArgumentError as e:
            print_(str(e), file=sys.stderr)
            exit(1)

class NodeDseCmd(Cmd):

    descr_text = "Launch a dse client application connected to this node"
    usage = "usage: ccm node_name dse [dse_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.dse_options = args[1:] + parser.get_ignored()

    def run(self):
        self.node.dse(self.dse_options)

class NodeHadoopCmd(Cmd):

    descr_text = "Launch a hadoop session connected to this node"
    usage = "usage: ccm node_name hadoop [options] [hadoop_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.hadoop_options = args[1:] + parser.get_ignored()

    def run(self):
        self.node.hadoop(self.hadoop_options)

class NodeHiveCmd(Cmd):

    descr_text = "Launch a hive session connected to this node"
    usage = "usage: ccm node_name hive [options] [hive_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.hive_options = args[1:] + parser.get_ignored()

    def run(self):
        self.node.hive(self.hive_options)

class NodePigCmd(Cmd):

    descr_text = "Launch a pig session connected to this node"
    usage = "usage: ccm node_name pig [options] [pig_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.pig_options = parser.get_ignored() + args[1:]

    def run(self):
        self.node.pig(self.pig_options)

class NodeSqoopCmd(Cmd):

    descr_text = "Launch a sqoop session connected to this node"
    usage = "usage: ccm node_name sqoop [options] [sqoop_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.sqoop_options = args[1:] + parser.get_ignored()

    def run(self):
        self.node.sqoop(self.sqoop_options)

class NodeSparkCmd(Cmd):

    descr_text = "Launch a spark session connected to this node"
    usage = "usage: ccm node_name spark [options] [spark_options]"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.spark_options = args[1:] + parser.get_ignored()

    def run(self):
        self.node.spark(self.spark_options)

class NodePauseCmd(Cmd):

    descr_text = "Send a SIGSTOP to this node"
    usage = "usage: ccm node_name pause"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        self.node.pause()

class NodeResumeCmd(Cmd):

    descr_text = "Send a SIGCONT to this node"
    usage = "usage: ccm node_name resume"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        self.node.resume()

class NodeJconsoleCmd(Cmd):

    descr_text = "Opens jconsole client and connect to running node"
    usage = "usage: ccm node_name jconsole"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        cmds = ["jconsole", "localhost:%s" % self.node.jmx_port]
        try:
            subprocess.call(cmds)
        except OSError:
            print_("Could not start jconsole. Please make sure jconsole can be found in your $PATH.")
            exit(1)

class NodeVersionfrombuildCmd(Cmd):

    descr_text = "Print the node's version as grepped from build.xml. Can be used when the node isn't running."
    usage = "usage: ccm node_name versionfrombuild"

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)

    def run(self):
        print_(common.get_version_from_build(self.node.get_install_dir()))

class NodeBytemanCmd(Cmd):

    descr_text = "Invoke byteman-submit "
    usage = "usage: ccm node_name byteman-submit"
    ignore_unknown_options = True

    def validate(self, parser, options, args):
        Cmd.validate(self, parser, options, args, node_name=True, load_cluster=True)
        self.byteman_options = args[1:] + parser.get_ignored()

    def run(self):
        self.node.byteman_submit(self.byteman_options)


text:
import {FindOperator} from "../FindOperator";

/**
 * Find Options Operator.
 * Example: { someField: Any([...]) }
 */
export function Any<T>(value: T[]|FindOperator<T>): FindOperator<T> {
    return new FindOperator("any", value as any);
}

redpajama_set_name: RedPajamaGithub


text:
package com.bfd.es;

import com.alibaba.fastjson.JSONObject;
import com.bfd.test.util.EsWriter;
import com.bfd.test.util.FileLoader;
import com.bfd.test.util.RandomDate;

import org.junit.Ignore;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;

/**
 * Created by BFD-483 on 2017/6/6.
 */
public class TestAggs {

    @Ignore
    public void basic() {
        try {
            EsWriter.prepareData();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("basic");
    }

    @Ignore
    public void date_stats() {
        String index = "cars";
        String[] types = {"transactions"};
        String dateField = "sold";
        String termField = "color.keyword";
        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            JSONObject result = client.dateHistogram_matchall(index, types, dateField, termField);
            System.out.println(result);
            //System.out.println(result.toJSONString(result,true));
            assert (result.containsKey("2014-01-01"));
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Ignore
    public void timeAnalysis() {
        String index = "cars";
        String[] types = {"transactions"};
        String[] ids = {"AVyfaoNddZyJEjcQ1aiB",
                "AVyfaoNddZyJEjcQ1aiF",
                "AVyfaoNddZyJEjcQ1aiC"};
        String dateField = "sold";
        String termField = "color.keyword";
        String expected = "{\"2014-08-01\":{\"all\":1,\"green\":1},\"2014-11-01\":{\"all\":1,\"red\":1}," +
                "\"2014-10-01\":{\"all\":1,\"red\":1}}";
        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            JSONObject result = client.dateHistogramSubterm(index, types, ids, dateField, termField);
            System.out.println(result.toString());
            //assert (result.containsKey("2014-08-01"));
            //assert (expected.compareTo(result.toString()) == 0);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void timeAnalysisNew() {
        String index = "atom";
        String[] types = {"发邮件"};

        String[] ids = {"2001"};

        String dateField = "date_time";
        String termField = "from.keyword";

        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            JSONObject result = client.dateHistogramSubterm(index, types, ids, dateField, termField);
            System.out.println(result.toString());
            //assert (result.containsKey("2014-08-01"));
            //assert (expected.compareTo(result.toString()) == 0);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void dumpData() {
        String index = "atom";
        String[] types = {"人类"};
        String[] includes = {"name"};
        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            int page = 1000;
            for (int i = 0; i < 10; i++) {
                int from = i * page;
                client.dump(index, types, includes, from, page);
            }

        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void dumpDataScrollPeople() {
        String index = "atom";
        String[] types = {"人类"};
        String[] includes = {"name"};
        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            int page = 1000;
            client.dumpScroll(index, types, includes, page);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void dumpDataScrollMagzine() {
        String index = "atom";
        String[] types = {"杂志", "短篇", "学术期刊"};
        String[] includes = {"name", "datetime"};
        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            int page = 1000;
            client.dumpScroll(index, types, includes, page);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void dumpDataScrollEvent() {
        String index = "atom_v3";
        //String[] types = {"战役", "内战","通信","发邮件"};
        String[] types = {"打电话"};
        String[] includes = {"name", "create_time"};
        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            int page = 1000;
            client.dumpScroll(index, types, includes, page);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void bulkInsert() throws IOException, URISyntaxException {
        File s = FileLoader.loadFile("story/people.json");
        FileReader fr = new FileReader(s);
        BufferedReader bfr = new BufferedReader(fr);
        String line = null;

        int asize = 965;
        String index = "atom";
        String type = "通信";
        String name = "通信";

        String[] ids = new String[asize];
        String[] docs = new String[asize];

        int i = 0;
        while ((line = bfr.readLine()) != null && i < asize) {
            ids[i] = String.format("%s", 1000 + i + 1);

            JSONObject mDoc = new JSONObject();
            mDoc.put("ontology_type", "event");
            mDoc.put("type", type);
            mDoc.put("id", ids[i]);
            mDoc.put("name", name);

            String dateTimeString = RandomDate.randomDateString("2015-01-01 00:00:00", "2017-06-29 00:00:00");
            Date date = RandomDate.randomDate("2015-01-01 00:00:00", "2017-06-29 00:00:00");
            mDoc.put("datetime", dateTimeString);
            mDoc.put("date_time", date);
            mDoc.put("modify_time", "2017-06-29 17:29:32");
            mDoc.put("create_time", "2017-06-26 23:12:02");
            String[] items = line.trim().split(",");

            mDoc.put("from", items[0].trim());
            mDoc.put("to", items[1].trim());

            docs[i] = mDoc.toJSONString();
            i = i + 1;
        }

        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            client.bulkInsert(index, type, ids, docs);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void bulkInsert_email() throws IOException, URISyntaxException {
        File s = FileLoader.loadFile("story/people.json");
        FileReader fr = new FileReader(s);
        BufferedReader bfr = new BufferedReader(fr);
        String line = null;

        int asize = 965;
        String index = "atom";
        String type = "打电话";
        String name = "打电话";

        String[] ids = new String[asize];
        String[] docs = new String[asize];

        int i = 0;
        while ((line = bfr.readLine()) != null && i < asize) {
            ids[i] = String.format("%s", 2 * 1000 + i + 1);

            JSONObject mDoc = new JSONObject();
            mDoc.put("ontology_type", "event");
            mDoc.put("type", type);
            mDoc.put("id", ids[i]);
            mDoc.put("name", name);

            String dateTimeString = RandomDate.randomDateString("2015-01-01 00:00:00", "2017-06-29 00:00:00");
            Date date = RandomDate.randomDate("2015-01-01 00:00:00", "2017-06-29 00:00:00");
            mDoc.put("datetime", dateTimeString);
            mDoc.put("date_time", date);
            mDoc.put("modify_time", "2017-06-29 17:29:32");
            mDoc.put("create_time", "2017-06-26 23:12:02");
            String[] items = line.trim().split(",");

            mDoc.put("from", items[0].trim());
            mDoc.put("to", items[1].trim());

            docs[i] = mDoc.toJSONString();
            i = i + 1;
        }

        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            client.bulkInsert(index, type, ids, docs);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void bulkInsert_phone() throws IOException, URISyntaxException {
        File s = FileLoader.loadFile("story/people.json");
        FileReader fr = new FileReader(s);
        BufferedReader bfr = new BufferedReader(fr);
        String line = null;

        int asize = 965;
        String index = "atom";
        String type = "打电话";
        String name = "打电话";

        String[] ids = new String[asize];
        String[] docs = new String[asize];

        int i = 0;
        while ((line = bfr.readLine()) != null && i < asize) {
            ids[i] = String.format("%s", 3 * 1000 + i + 1);

            JSONObject mDoc = new JSONObject();
            mDoc.put("ontology_type", "event");
            mDoc.put("type", type);
            mDoc.put("id", ids[i]);
            mDoc.put("name", name);
            mDoc.put("image","");

            String dateTimeString = RandomDate.randomDateString("2015-01-01 00:00:00", "2017-06-29 00:00:00");
            Date date = RandomDate.randomDate("2015-01-01 00:00:00", "2017-06-29 00:00:00");
            mDoc.put("datetime", dateTimeString);
            mDoc.put("date_time", date);
            mDoc.put("modify_time", "2017-06-29 17:29:32");
            mDoc.put("create_time", "2017-06-26 23:12:02");
            mDoc.put("modifier","zf");

            String[] items = line.trim().split(",");

            mDoc.put("from", items[0].trim());
            mDoc.put("to", items[1].trim());

            docs[i] = mDoc.toJSONString();
            i = i + 1;
        }

        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            client.bulkInsert(index, type, ids, docs);
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }
    }

    @Test
    public void addMapping() {
        String index = "atom";
        String type = "通信";
        String mappingSource = "{\n" +
                "    \"user\":{\n" +
                "        \"properties\": {\n" +
                "            \"name\": {\n" +
                "                \"type\": \"string\"\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}";

        try {
            EsClient client = new EsClient("172.24.5.149", "9300", "logging-dev");
            client.addMapping(index, type, mappingSource);

        } catch (Exception e) {
            e.printStackTrace();
            assert (false);
        }

    }

    @Test
    public void constructIds(){
        for(int i=2001;i<2966;i++){
            String tmp= String.format("{ \"delete\": {\"_id\":\"%s\"}}",i);
            System.out.println(tmp);
        }
    }

    //scp -P 3222  /cygdrive/d/temp/atom/BFD-ATOM/information/target/information-0.0.1-SNAPSHOT.jar
    // root@172.24.8.34:/root/dzn/

    //scp -P 3222  /cygdrive/d/temp/atom/BFD-ATOM/information/src/main/resources/application.yml
    // root@172.24.8.34:/root/dzn/

    //D:\temp\atom\BFD-ATOM\information\src\main\resources\application.yml
}


text:
/*
 * 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.
 */
package org.apache.karaf.cellar.dosgi;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

public class ExportServiceListenerTest {

    private ExportServiceListener listener = new ExportServiceListener();

    @Test
    public void testGetServiceInterfaces() throws Exception {
        System.out.println("Test Service interfaces with null service");
        Set<String> expectedResult = new LinkedHashSet<String>();
        Set<String> result = listener.getServiceInterfaces(null,null);
        Assert.assertEquals(expectedResult,result);

        result = listener.getServiceInterfaces(null,new String[] {"*"});
        Assert.assertEquals(expectedResult,result);

        System.out.println("Test Service interfaces with ArrayList and wildcard services");
        result = listener.getServiceInterfaces(new ArrayList(),new String[] {"*"});
        Assert.assertTrue(result.contains("java.util.List"));

        System.out.println("Test Service interfaces with ArrayList and List/Serializable services");
        result = listener.getServiceInterfaces(new ArrayList(),new String[] {"java.util.List","java.io.Serializable"});
        Assert.assertTrue(result.contains("java.util.List"));
        Assert.assertTrue(result.contains("java.io.Serializable"));
    }

}


text:
#include "QmitkWatershedToolGUI.h"

#include "QmitkNewSegmentationDialog.h"

#include <qlabel.h>
#include <qslider.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qpainter.h>
#include <qapplication.h>

MITK_TOOL_GUI_MACRO(SegmentationUI_EXPORT, QmitkWatershedToolGUI, "")

QmitkWatershedToolGUI::QmitkWatershedToolGUI()
:QmitkToolGUI(),
 m_SliderThreshold(NULL),
 m_SliderLevel(NULL)
{
  // create the visible widgets
  QGridLayout* layout = new QGridLayout( this );
  this->setContentsMargins( 0, 0, 0, 0 );

  QLabel* label = new QLabel( "Threshold ", this );
  QFont f = label->font();
  f.setBold(false);
  label->setFont( f );
  layout->addWidget(label,0,0);

  QLabel* label2 = new QLabel( "Level ", this );
  f = label2->font();
  f.setBold(false);
  label2->setFont( f );
  layout->addWidget(label2,2,0);

  m_ThresholdLabel = new QLabel( " 0.5", this );
  f = m_ThresholdLabel->font();
  f.setBold(false);
  m_ThresholdLabel->setFont( f );
  layout->addWidget(m_ThresholdLabel,0,1);

  m_SliderThreshold = new QSlider( Qt::Horizontal, this );
  m_SliderThreshold->setMinimum(0);
  m_SliderThreshold->setMaximum(100);
  m_SliderThreshold->setPageStep(1);
  m_SliderThreshold->setValue(50);
  connect( m_SliderThreshold, SIGNAL(valueChanged(int)), this, SLOT(OnSliderValueThresholdChanged(int)));
  layout->addWidget( m_SliderThreshold, 1, 0, 1, 2 );

  m_LevelLabel = new QLabel( " 0.5", this );
  f = m_LevelLabel->font();
  f.setBold(false);
  m_LevelLabel->setFont( f );
  layout->addWidget(m_LevelLabel,2,1);

  m_SliderLevel = new QSlider( Qt::Horizontal, this );
  m_SliderLevel->setMinimum(0);
  m_SliderLevel->setMaximum(100);
  m_SliderLevel->setPageStep(1);
  m_SliderLevel->setValue(50);
  connect( m_SliderLevel, SIGNAL(valueChanged(int)), this, SLOT(OnSliderValueLevelChanged(int)));
  layout->addWidget( m_SliderLevel, 3, 0, 1, 2 );

  QPushButton* okButton = new QPushButton("Create Segmentation", this);
  connect( okButton, SIGNAL(clicked()), this, SLOT(OnCreateSegmentation()));
  okButton->setFont( f );
  layout->addWidget( okButton, 4, 0, 1, 2 );

  m_InformationLabel = new QLabel("", this);
  f = m_InformationLabel->font();
  f.setBold(false);
  m_InformationLabel->setFont( f );
  layout->addWidget( m_InformationLabel, 5,0,1,2);

  connect( this, SIGNAL(NewToolAssociated(mitk::Tool*)), this, SLOT(OnNewToolAssociated(mitk::Tool*)) );
}

QmitkWatershedToolGUI::~QmitkWatershedToolGUI()
{
  if (m_WatershedTool.IsNotNull())
  {
    //m_WatershedTool->SizeChanged -= mitk::MessageDelegate1<QmitkWatershedToolGUI, int>( this, &QmitkWatershedToolGUI::OnSizeChanged );
  }

}

void QmitkWatershedToolGUI::OnNewToolAssociated(mitk::Tool* tool)
{
  if (m_WatershedTool.IsNotNull())
  {
    //m_WatershedTool->SizeChanged -= mitk::MessageDelegate1<QmitkWatershedToolGUI, int>( this, &QmitkWatershedToolGUI::OnSizeChanged );
  }

  m_WatershedTool = dynamic_cast<mitk::WatershedTool*>( tool );

  if (m_WatershedTool.IsNotNull())
  {
//    m_WatershedTool->SizeChanged += mitk::MessageDelegate1<QmitkWatershedToolGUI, int>( this, &QmitkWatershedToolGUI::OnSizeChanged );
  }
}

void QmitkWatershedToolGUI::OnSliderValueThresholdChanged(int value)
{
  if (m_WatershedTool.IsNotNull())
  {
      double realValue = value / 100.;
      m_WatershedTool->SetThreshold( realValue );
      m_ThresholdLabel->setText(QString::number(realValue));
  }
}

void QmitkWatershedToolGUI::OnSliderValueLevelChanged(int value)
{
  if (m_WatershedTool.IsNotNull())
  {
      double realValue = value / 100.;
      m_WatershedTool->SetLevel( realValue );
      m_LevelLabel->setText(QString::number(realValue));
  }
}

void QmitkWatershedToolGUI::OnCreateSegmentation()
{
  QCursor oldCursor = *QApplication::overrideCursor();
  QApplication::setOverrideCursor(Qt::BusyCursor);
  m_InformationLabel->setText(QString("Please wait some time for computation..."));
  m_InformationLabel->repaint();
  QApplication::processEvents();

  m_WatershedTool->DoIt();
  m_InformationLabel->setText(QString(""));
  QApplication::setOverrideCursor(oldCursor);
}


text:
import staticRegister from './../utils/staticRegister';

import autocompleteValidator from './autocompleteValidator';
import dateValidator from './dateValidator';
import numericValidator from './numericValidator';
import timeValidator from './timeValidator';

const {
  register,
  getItem,
  hasItem,
  getNames,
  getValues,
} = staticRegister('validators');

register('autocomplete', autocompleteValidator);
register('date', dateValidator);
register('numeric', numericValidator);
register('time', timeValidator);

/**
 * Retrieve validator function.
 *
 * @param {String} name Validator identification.
 * @returns {Function} Returns validator function.
 */
function _getItem(name) {
  if (typeof name === 'function') {
    return name;
  }
  if (!hasItem(name)) {
    throw Error(`No registered validator found under "${name}" name`);
  }

  return getItem(name);
}

export {
  register as registerValidator,
  _getItem as getValidator,
  hasItem as hasValidator,
  getNames as getRegisteredValidatorNames,
  getValues as getRegisteredValidators,
};


text:
'use strict';

var _ = require('lodash')
  , usb = require(process.env['TRAVIS'] ? './fake/usb' : 'usb')
  , p = require('hw-promise')
  , logger = require('hw-logger')
  , hwError = require('hw-error')
  , events = require('events')
  , eventHandlers = require('./event-handlers')
  , util = require('./util')
  , keyLayouts = require('./key-layouts')
  , log = logger.log
  , that;

hwError.initErrors([
  {
    constructor: function HidHandlerError(code) {
      this.code = code;
    }
  }, {
    constructor: function HidAlreadyRegisteredError(deviceKey) {
      this.deviceKey = deviceKey;
    },
    parent: 'HidHandlerError'
  }, {
    constructor: function HidEventHandlerNameError(eventHandlerName) {
      this.eventHandlerName = eventHandlerName;
    },
    parent: 'HidHandlerError'
  }
]);

that = _.extend(new events.EventEmitter(), {
  config: {
    defaultDeviceType: 'generic',
    supportedDevices: []
  },
  registeredHids: {},
  getRegisteredDevices: function () {
    var scannedDevices, devices;
    scannedDevices = util.scanDevices();
    devices = _.filter(scannedDevices, function (device) {
      return !!that.getRegisteredHid(device);
    });
    devices = _.unique(_.map(devices, function (device) {
      return {
        deviceKey: util.getDeviceKey(device),
        product: device.product.trim(),
        manufacturer: device.manufacturer.trim()
      };
    }), function (device) {
      return device.deviceKey.toString();
    });
    return devices;
  },
  getRegisteredHid: function (vendorId, productId) {
    var deviceKey;
    if (typeof productId === 'undefined' && typeof vendorId === 'string') {
      deviceKey = vendorId;
    } else {
      deviceKey = util.getDeviceKey(vendorId, productId);
    }
    return that.registeredHids[deviceKey];
  },
  getRegisteredHids: function () {
    return _.values(that.registeredHids);
  },
  getRegisteredHidKeys: function () {
    return _.keys(that.registeredHids);
  },
  getSupportedDevice: function (vendorId, productId) {
    return _.first(_.filter(that.config.supportedDevices, function (supportedDevice) {
      return supportedDevice.vendorId === vendorId && supportedDevice.productId === productId;
    }));
  },
  handleEndpointData: function (hid, ifaceIndex, inEndpointIndex, data) {
    var eventClass, event;
    logger.enabledLevels.trace && log.trace('data :', data);
    eventClass = hid.device.eventClass[ifaceIndex];
    event = new eventClass(hid, ifaceIndex, inEndpointIndex, data);
    logger.enabledLevels.trace && log.trace('created event :', event);
    event.emit([that, hid.ee]);
  },
  handleEndpointError: function (hid, ifaceIndex, inEndpointIndex, err) {
    if (err.errno === usb.LIBUSB_ERROR_NO_DEVICE) {
      log.warn(err);
      that.unregisterHid(hid);
      hid.device.reset();
      return;
    }
    logger.enabledLevels.warn && log.warn('error received :', err);
    if (hid.device) {
      hid.device.reset();
    }
  },
  handleEndpointEnd: function (hid, ifaceIndex, inEndpointIndex) {
    var inEndpoint;
    inEndpoint = hid.device.interfaces[ifaceIndex].endpoints[inEndpointIndex];
    logger.enabledLevels.debug && log.debug('endpoint end.');
    inEndpoint.removeListener('data', that.handleEndpointData);
    inEndpoint.removeListener('error', that.handleEndpointError);
    inEndpoint.removeListener('end', that.handleEndpointEnd);
  },
  handleUsbAttach: function (device) {
    var deviceKey, supportedDevice, deviceType;
    deviceKey = util.getDeviceKey(device.deviceDescriptor.idVendor, device.deviceDescriptor.idProduct);
    logger.enabledLevels.debug && log.debug('detected new attached device "%s"', deviceKey);
    if (that.config.supportedDevices.length) {
      supportedDevice = that.getSupportedDevice(device.deviceDescriptor.idVendor, device.deviceDescriptor.idProduct);
    }
    if (!supportedDevice) {
      logger.enabledLevels.debug && log.debug('attached device "%s" is not supported : ignore', deviceKey);
      return;
    }
    logger.enabledLevels.debug && log.debug('register attached device "%s"', deviceKey);
    deviceType = supportedDevice.type || that.config.defaultDeviceType;
    deviceType = typeof deviceType === 'string' ? {0: deviceType} : deviceType;
    that.registerHid({type: deviceType, device: device}).then(function () {
      that.emit('usb', {action: 'attach', device: device});
    });
  },
  handleUsbDetach: function (device) {
    var deviceKey, hid;
    deviceKey = util.getDeviceKey(device.deviceDescriptor.idVendor, device.deviceDescriptor.idProduct);
    logger.enabledLevels.debug && log.debug('detected new detached device "%s"', deviceKey);
    if (that.config.supportedDevices.length && !that.getSupportedDevice(device.deviceDescriptor.idVendor, device.deviceDescriptor.idProduct)) {
      logger.enabledLevels.debug && log.debug('detached device "%s" is not supported : ignore', deviceKey);
      return;
    }
    hid = that.getRegisteredHid(deviceKey);
    if (!hid) {
      logger.enabledLevels.debug && log.debug('detached device "%s" is not registered : ignore', deviceKey);
      return;
    }
    logger.enabledLevels.debug && log.debug('unregister detached device "%s"', deviceKey);
    that.unregisterHid(hid).then(function () {
      that.emit('usb', {action: 'detach', device: device});
    });
  },
  registerEventHandler: function (eventHandlerClass, eventHandlerClassName) {
    eventHandlerClassName = eventHandlerClassName || eventHandlerClass.name;
    if (that.hasOwnProperty(eventHandlerClassName)) {
      throw new that.HidEventHandlerNameError(eventHandlerClassName);
    }
    if (!eventHandlerClass.super_ && eventHandlerClassName !== 'GenericEvent') {
      (function () {
        var proto = eventHandlerClass.prototype;
        util.inherits(eventHandlerClass, that.GenericEvent);
        _.forIn(proto, function (fn, name) {
          eventHandlerClass.prototype[name] = fn;
        });
      })();
    }
    logger.enabledLevels.debug && log.debug('register event handler class "%s"', eventHandlerClassName);
    that[eventHandlerClassName] = eventHandlerClass;
  },
  registerHid: function (opt, cb) {
    var deviceKey, hid;
    deviceKey = util.getDeviceKey(opt.device.deviceDescriptor.idVendor, opt.device.deviceDescriptor.idProduct);
    logger.enabledLevels.debug && log.debug('register hid device "%s"', deviceKey);
    logger.enabledLevels.trace && log.trace('device :', util.logObject(opt.device));
    return p.do(
      function checkAlreadyRegistered() {
        if (that.getRegisteredHid(deviceKey)) {
          throw new that.HidAlreadyRegisteredError(deviceKey);
        }
      },
      function initHid() {
        hid = {
          deviceKey: deviceKey,
          device: opt.device
        };
        hid.device.type = opt.type || that.config.defaultDeviceType;
        hid.device.type = typeof hid.device.type === 'string' ? {0: hid.device.type} : hid.device.type;
        hid.device.eventClass = {};
        hid.device.keyLayoutHandler = {};
        _.forIn(hid.device.type, function (type, key) {
          var eventClassName;
          if (typeof type === 'string') {
            (function (typeParts) {
              type = {
                value: typeParts[0]
              };
              if (typeParts.length > 1) {
                type.layout = typeParts[1];
              }
              if (typeParts.length > 2) {
                type.variant = typeParts[2];
              }
            })(type.split('-'));
          }
          hid.device.type[key] = type;
          eventClassName = util.format('%sEvent', _.capitalize(_.camelCase(type.value)));
          hid.device.eventClass[key] = that[eventClassName] || that.GenericEvent;
        });
      },
      function openDevice() {
        logger.enabledLevels.debug && log.debug('open device "%s"', hid.deviceKey);
        opt.device.open();
      },
      function claimInterfaces() {
        logger.enabledLevels.debug && log.debug('get interfaces of device "%s"', hid.deviceKey);
        logger.enabledLevels.trace && log.trace('device has %s interfaces :', hid.device.interfaces.length, util.logObject(hid.device.interfaces));
        if (!hid.device.interfaces.length) {
          logger.enabledLevels.warn && log.warn('no interface for device "%s"', hid.deviceKey);
          throw new that.HidHandlerError({code: 'NO_INTERFACE'});
        }
        hid.device.interfaces.forEach(function (iface, ifaceIndex) {
          if (typeof opt.device.type[ifaceIndex] === 'undefined') {
            logger.enabledLevels.debug && log.debug('no type found for interface #%s of device "%s" : ignore', ifaceIndex, hid.deviceKey);
            return;
          }
          logger.enabledLevels.trace && log.trace('found device interface #%s of type %s :', ifaceIndex, opt.device.type[ifaceIndex], util.logObject(iface));
          if (iface.isKernelDriverActive()) {
            logger.enabledLevels.debug && log.debug('detach interface #%s of device "%s" from kernel', ifaceIndex, hid.deviceKey);
            iface.detachKernelDriver();
          }
          logger.enabledLevels.debug && log.debug('claim interface #%s of device "%s"', ifaceIndex, hid.deviceKey);
          iface.claim();
          logger.enabledLevels.debug && log.debug('searching "in" endpoints of interface #%s for device "%s"', ifaceIndex, hid.deviceKey);
          logger.enabledLevels.trace && log.trace('device interface #%s has %s endpoints :', ifaceIndex, iface.endpoints.length, util.logObject(iface.endpoints));
          (function handleEndpoints(inEndpoints) {
            if (!inEndpoints.length) {
              logger.enabledLevels.warn && log.warn('no "in" endpoint in interface #%s for device "%s"', ifaceIndex, hid.deviceKey);
              return;
            }
            inEndpoints.forEach(function (inEndpoint, inEndpointIndex) {
              logger.enabledLevels.trace && log.trace('registered endpoint :', util.logObject(inEndpoint));
              logger.enabledLevels.debug && log.debug('register endpoint listeners');
              inEndpoint.on('data', that.handleEndpointData.bind(that, hid, ifaceIndex, inEndpointIndex));
              inEndpoint.on('error', that.handleEndpointError.bind(that, hid, ifaceIndex, inEndpointIndex));
              inEndpoint.on('end', that.handleEndpointEnd.bind(that, hid, ifaceIndex, inEndpointIndex));
              logger.enabledLevels.debug && log.debug('start endpoint polling');
              inEndpoint.startPoll();
            });
          })(_.filter(iface.endpoints, function (endpoint) {
            return endpoint.direction === 'in';
          }));
        });
      },
      function registerNewHid() {
        hid.ee = new events.EventEmitter();
        that.registeredHids[hid.deviceKey] = hid;
      })
      .catch(that.HidAlreadyRegisteredError, function (err) {
        logger.enabledLevels.debug && log.debug('device "%s" already registered : ignore', err.deviceKey);
      })
      .nodeify(cb);
  },
  unregisterHid: function (hid, cb) {
    logger.enabledLevels.debug && log.debug('unregister hid device "%s"', hid.deviceKey);
    return p.do(
      function emitHidEnd() {
        hid.ee.emit('end');
        hid.ee.removeAllListeners();
      },
      function stopEndpoints() {
        if (!hid.device.interfaces || !hid.device.interfaces.length) {
          logger.enabledLevels.warn && log.warn('no interface for device "%s" : ignore', hid.deviceKey);
          return;
        }
        return p.map(hid.device.interfaces, function (iface, ifaceIndex) {
          var inEndpoints;
          if (typeof hid.device.type[ifaceIndex] === 'undefined') {
            logger.enabledLevels.debug && log.debug('no type found for interface #%s of device "%s" : ignore', ifaceIndex, hid.deviceKey);
            return;
          }
          inEndpoints = _.filter(iface.endpoints, function (endpoint) {
            return endpoint.direction === 'in';
          });
          return p.do(
            function closeEndpoints() {
              if (!inEndpoints.length) {
                logger.enabledLevels.warn && log.warn('no "in" endpoint in interface #%s for device "%s"', ifaceIndex, hid.deviceKey);
                return;
              }
              return p.map(inEndpoints, function (inEndpoint, inEndpointIndex) {
                if (!inEndpoint.pollActive) {
                  return;
                }
                logger.enabledLevels.debug && log.debug('stopping endpoint #%s polling of interface #%s for device "%s"', inEndpointIndex, ifaceIndex, hid.deviceKey);
                return p.fromNode(inEndpoint.stopPoll.bind(inEndpoint))
                  .then(function () {
                    logger.enabledLevels.debug && log.debug('endpoint #%s polling of interface #%s stopped for device "%s"', inEndpointIndex, ifaceIndex, hid.deviceKey);
                  });
              });
            },
            function releaseInterface() {
              logger.enabledLevels.debug && log.debug('release interface #%s of device "%s"', ifaceIndex, hid.deviceKey);
              return p.fromNode(iface.release.bind(iface, true))
                .then(function () {
                  logger.enabledLevels.debug && log.debug('interface #%s released for device "%s"', ifaceIndex, hid.deviceKey);
                  if (!iface.isKernelDriverActive()) {
                    logger.enabledLevels.debug && log.debug('reattach interface #%s of device "%s" from kernel', ifaceIndex, hid.deviceKey);
                    iface.attachKernelDriver();
                  }
                })
                .catch(function (err) {
                  if (err.errno !== usb.LIBUSB_ERROR_NO_DEVICE) {
                    throw err;
                  }
                });

            });
        });
      })
      .finally(function () {
        return p.do(
          function closeDevice() {
            if (!hid.device) {
              return;
            }
            logger.enabledLevels.debug && log.debug('close device "%s"', hid.deviceKey);
            //return p.fromNode(hid.device.reset.bind(hid.device)).then(function () {
            hid.device.close();
            //});
          },
          function removeDevice() {
            logger.enabledLevels.debug && log.debug('remove device "%s" from registered', hid.deviceKey);
            delete that.registeredHids[hid.deviceKey];
          });
      })
      .nodeify(cb);
  },
  init: function (opt) {
    opt = opt || {};
    _.extend(that.config, {
      defaultDeviceType: opt.defaultDeviceType,
      supportedDevices: Array.isArray(opt.supportedDevices) ? opt.supportedDevices : (opt.supportedDevices ? [opt.supportedDevices] : []),
      keyLayouts: opt.keyLayouts
    });
    that.initialized = true;
  },
  isStarted: function () {
    return that.started;
  },
  start: function (opt, cb) {
    if (typeof cb === 'undefined' && typeof opt === 'function') {
      cb = opt;
      opt = null;
    }
    logger.enabledLevels.debug && log.debug('start hid handler');
    return p.do(
      function init() {
        if (!that.initialized) {
          that.init(opt);
        }
        logger.enabledLevels.debug && log.debug('register usb listeners');
        usb.on('attach', that.handleUsbAttach);
        usb.on('detach', that.handleUsbDetach);
      },
      function initKeyLayouts() {
        return keyLayouts.init(that.config.keyLayouts);
      },
      function registerDevices() {
        var supportedDevices;
        logger.enabledLevels.debug && log.debug('check supported devices');
        supportedDevices = that.config.supportedDevices || _.map(usb.getDeviceList(), function (value) {
            return {
              vendorId: value.deviceDescriptor.idVendor,
              productId: value.deviceDescriptor.idProduct,
              type: that.config.defaultDeviceType
            };
          });
        logger.enabledLevels.trace && log.trace('supported devices :', supportedDevices);
        logger.enabledLevels.debug && log.debug('init devices');
        return p.each(_.values(supportedDevices),
          function (supportedDevice) {
            var deviceKey, device, deviceType;
            deviceKey = util.getDeviceKey(supportedDevice.vendorId, supportedDevice.productId);
            logger.enabledLevels.debug && log.debug('searching usb device from "%s"', deviceKey);
            device = usb.findByIds(supportedDevice.vendorId, supportedDevice.productId);
            if (!device) {
              logger.enabledLevels.warn && log.warn('no usb device matching "%s"', deviceKey);
              return;
            }
            deviceType = supportedDevice.type || that.config.defaultDeviceType;
            logger.enabledLevels.debug && log.debug('found %s usb device matching "%s"', deviceType, deviceKey);
            return that.registerHid({type: deviceType, device: device});
          });
      })
      .then(function setStartedState() {
        that.started = true;
      })
      .nodeify(cb);
  },
  stop: function (cb) {
    logger.enabledLevels.debug && log.debug('stop hid handler');
    return p.do(
      function () {
        logger.enabledLevels.debug && log.debug('stop hid handler');
        return p.map(_.values(that.registeredHids), function (hid) {
          return that.unregisterHid(hid);
        });
      },
      function removeListeners() {
        logger.enabledLevels.debug && log.debug('remove listeners');
        usb.removeListener('attach', that.handleUsbAttach);
        usb.removeListener('detach', that.handleUsbDetach);
        that.removeAllListeners();
      })
      .then(function setStartedState() {
        that.started = false;
      })
      .nodeify(cb);
  }
});

(function registerErrors() {
  _.forIn(hwError, function (value, key) {
    if (key.match(/^Hid.*Error$/)) {
      exports[key] = that[key] = value;
    }
  });
})();

(function registerEventHandlers() {
  that.registerEventHandler(eventHandlers.GenericEvent);
  _.forIn(_.omit(eventHandlers, 'GenericEvent'), function (eventHandler) {
    that.registerEventHandler(eventHandler);
  });
  _.extend(exports, eventHandlers);
})();

(function exportsEventEmitter() {
  var eventEmittersKeys = Object.keys(events.EventEmitter.prototype);
  eventEmittersKeys.forEach(function (key) {
    if (typeof that[key] === 'function') {
      logger.enabledLevels.trace && log.trace('exports event emitter function "%s"', key);
      exports[key] = that[key].bind(that);
    }
  });
})();

(function exportsFuncs(moduleFuncs) {
  moduleFuncs.forEach(function (key) {
    logger.enabledLevels.trace && log.trace('exports "%s"', key);
    exports[key] = that[key].bind(that);
  });
})([
  'getSupportedDevice',
  'init',
  'isStarted',
  'start',
  'stop',
  'getRegisteredDevices',
  'getRegisteredHid',
  'getRegisteredHids',
  'getRegisteredHidKeys',
  'registerEventHandler'
]);

exports.keyLayouts = keyLayouts;
exports.util = util;


text:
package aceim.protocol.snuk182.xmpp.common;

public final class XMPPApiConstants {

	private XMPPApiConstants() {}

	/*public static final byte STATUS_OFFLINE = -1;
	public static final byte STATUS_ONLINE = 0;
	public static final byte STATUS_AWAY = 1;
	public static final byte STATUS_NA = 2;
	public static final byte STATUS_DND = 3;
	public static final byte STATUS_FREE4CHAT = 4;
	public static final byte STATUS_INVISIBLE = 5;*/

	public static final String DEFAULT_PORT = "5222";
	public static final String DEFAULT_HOST = "jabber.org";

	public static final String PROTOCOL_NAME = "XMPP";

	public static final String FEATURE_ADD_BUDDY = "AddBuddy";
	public static final String FEATURE_AUTHORIZATION = "Authorization";
	public static final String FEATURE_GROUPCHATS = "Groupchats";
	public static final String FEATURE_ADD_GROUPCHAT = "AddGroupchat";
	public static final String FEATURE_CONFIGURE_CHAT_ROOM_ACTION = "ConfigureChatRoomAction";
	public static final String FEATURE_CONFIGURE_CHAT_ROOM_RESULT = "ConfigureChatRoomResult";
	public static final String FEATURE_DESTROY_CHAT_ROOM = "DestroyChatRoom";

	public static final String FEATURE_ADD_PUBLIC_KEY = "AddPublicKey";
	public static final String FEATURE_REMOVE_PUBLIC_KEY = "RemovePublicKey";
	public static final String FEATURE_ENCRYPTION_ON = "EncryptionOn";
	public static final String FEATURE_ENCRYPTION_OFF = "EncryptionOff";
}


text:
<!DOCTYPE html>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>pyshgp.gp.variation — PyshGP 0.1.8 documentation</title>
    <link rel="stylesheet" href="../../../_static/nature.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />

    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />

Navigation

          index

          modules |

PyshGP 0.1.8 documentation »

Module code »

pyshgp.gp.variation

Source code for pyshgp.gp.variation

"""The :mod:`variation` module defines classes for variation operators.

Variation operators (aka genetic operators) are used in evolutionary/genetic
algorithms to create "child" genomes from "parent" genomes.

"""
from abc import ABC, abstractmethod
from typing import Sequence, Union
import math

from numpy.random import random, choice

from pyshgp.push.types import PushType
from pyshgp.push.atoms import Literal
from pyshgp.gp.genome import Genome, GeneSpawner
from pyshgp.tap import tap
from pyshgp.utils import DiscreteProbDistrib, instantiate_using

[docs]class VariationOperator(ABC):
    """Base class of all VariationOperators.

    Parameters
    ----------
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    Attributes
    ----------
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self, num_parents: int):
        self.num_parents = num_parents

[docs]    def checknum_parents(self, parents: Sequence[Genome]):
        """Raise error if given too few parents.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.

        """
        if not len(parents) >= self.num_parents:
            raise ValueError("Variation operator given {a} parents. Expected {e}.".format(
                a=len(parents),
                e=self.num_parents)
            )

[docs]    @tap
    @abstractmethod
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        pass

[docs]class VariationStrategy(DiscreteProbDistrib):
    """A collection of VariationOperator and how frequently to use them."""

[docs]    def add(self, op: VariationOperator, p: float):
        """Add an element with a relative probability.

        Parameters
        ----------
        op : VariationOperator
            The VariationOperator to add to the variation strategy.
        p : float
            The probability of using the given operator relative to the other
            operators that have been added to the VariationStrategy.

        """
        super().add(op, p)

[docs]class VariationPipeline(VariationOperator):
    """Variation operator that sequentially applies multiple others variation operators.

    Parameters
    ----------
    operators : Sequence[VariationOperators]
        A list of operators to apply in order to produce the child Genome.

    Attributes
    ----------
    operators : Sequence[VariationOperators]
        A list of operators to apply in order to produce the child Genome.
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self, operators: Sequence[VariationOperator]):
        num_parents_needed = max([op.num_parents for op in operators])
        super().__init__(num_parents_needed)
        self.operators = operators

[docs]    @tap
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        self.checknum_parents(parents)
        child = parents[0]
        for op in self.operators:
            child = op.produce([child] + parents[1:], spawner)
        return child

# Utilities

def _gaussian_noise_factor():
    """Return Gaussian noise of mean 0, std dev 1.

    Returns
    --------
    Float samples from Gaussian distribution.

    Examples
    --------
    >>> gaussian_noise_factor()
    1.43412557975
    >>> gaussian_noise_factor()
    -0.0410900866765

    """
    return math.sqrt(-2.0 * math.log(random())) * math.cos(2.0 * math.pi * random())

# Mutations

# @TODO: Implement all the common literal mutations.

[docs]class LiteralMutation(VariationOperator, ABC):
    """Base class for mutations of literal Atoms.

    Parameters
    ----------
    push_type : pyshgp.push.types.PushType
        The PushType which the operator can mutate.
    rate : float
        The probability of applying the mutation to a given Literal.

    Attributes
    ----------
    push_type : pyshgp.push.types.PushType
        The PushType which the operator can mutate.
    rate : float
        The probability of applying the mutation to a given Literal.
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self, push_type: PushType, rate: float = 0.01):
        super().__init__(1)
        self.rate = rate
        self.push_type = push_type

    @abstractmethod
    def _mutate_literal(self, literal: Literal) -> Literal:
        ...

[docs]    @tap
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner = None) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        self.checknum_parents(parents)
        new_genome = Genome()
        for atom in parents[0]:
            if isinstance(atom, Literal) and self.push_type == atom.push_type and random() < self.rate:
                new_atom = self._mutate_literal(atom)
            else:
                new_atom = atom
            new_genome = new_genome.append(new_atom)
        return new_genome

[docs]class DeletionMutation(VariationOperator):
    """Uniformly randomly removes some Atoms from parent.

    Parameters
    ----------
    rate : float
        The probability of removing any given Atom in the parent Genome.
        Default is 0.01.

    Attributes
    ----------
    rate : float
        The probability of removing any given Atom in the parent Genome.
        Default is 0.01.
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self, deletion_rate: float = 0.01):
        super().__init__(1)
        self.rate = deletion_rate

[docs]    @tap
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        self.checknum_parents(parents)
        new_genome = Genome()
        for gene in parents[0]:
            if random() < self.rate:
                continue
            new_genome = new_genome.append(gene)
        return new_genome

[docs]class AdditionMutation(VariationOperator):
    """Uniformly randomly adds some Atoms to parent.

    Parameters
    ----------
    rate : float
        The probability of adding a new Atom at any given point in the parent
        Genome. Default is 0.01.

    Attributes
    ----------
    rate : float
        The probability of adding a new Atom at any given point in the parent
        Genome. Default is 0.01.
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self, addition_rate: float = 0.01):
        super().__init__(1)
        self.rate = addition_rate

[docs]    @tap
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        self.checknum_parents(parents)
        new_genome = Genome()
        for gene in parents[0]:
            if random() < self.rate:
                new_genome = new_genome.append(spawner.random_gene())
            new_genome = new_genome.append(gene)
        return new_genome

# Recombinations

[docs]class Alternation(VariationOperator):
    """Uniformly alternates between the two parent genomes.

    Parameters
    ----------
    rate : float, optional (default=0.01)
        The probability of switching which parent program elements are being
        copied from. Must be 0 <= rate <= 1. Defaults to 0.1.
    alignment_deviation : int, optional (default=10)
        The standard deviation of how far alternation may jump between indices
        when switching between parents.

    Attributes
    ----------
    rate : float, optional (default=0.01)
        The probability of switching which parent program elements are being
        copied from. Must be 0 <= rate <= 1. Defaults to 0.1.
    alignment_deviation : int, optional (default=10)
        The standard deviation of how far alternation may jump between indices
        when switching between parents.
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self, alternation_rate=0.01, alignment_deviation=10):
        super().__init__(2)
        self.rate = alternation_rate
        self.alignment_deviation = alignment_deviation

[docs]    @tap
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner = None) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        self.checknum_parents(parents)
        gn1 = parents[0]
        gn2 = parents[1]
        new_genome = Genome()
        # Random pick which parent to start from
        use_parent_1 = choice([True, False])
        loop_times = len(gn1)
        if not use_parent_1:
            loop_times = len(gn2)
        i = 0
        while i < loop_times:
            if random() < self.rate:
                # Switch which parent we are pulling genes from
                i += round(self.alignment_deviation * _gaussian_noise_factor())
                i = int(max(0, i))
                use_parent_1 = not use_parent_1
            else:
                # Pull gene from parent
                if use_parent_1:
                    new_genome = new_genome.append(gn1[i])
                else:
                    new_genome = new_genome.append(gn2[i])
                i = int(i + 1)
            # Change loop stop condition
            loop_times = len(gn1)
            if not use_parent_1:
                loop_times = len(gn2)
        return new_genome

# Other

[docs]class Genesis(VariationOperator):
    """Creates an entirely new (and random) genome.

    Parameters
    ----------
    size
        The child genome will contain this many Atoms if size is an integer.
        If size is a pair of integers, the genome will be of a random
        size in the range of the two integers.

    Attributes
    ----------
    size
        The child genome will contain this many Atoms if size is an integer.
        If size is a pair of integers, the genome will be of a random
        size in the range of the two integers.
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self, *, size: Union[int, Sequence[int]]):
        super().__init__(0)
        self.size = size

[docs]    @tap
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        return spawner.spawn_genome(self.size)

[docs]class Cloning(VariationOperator):
    """Clones the parent genome.

    Attributes
    ----------
    num_parents : int
        Number of parent Genomes the operator needs to produce a child
        Individual.

    """

    def __init__(self):
        super().__init__(1)

[docs]    @tap
    def produce(self, parents: Sequence[Genome], spawner: GeneSpawner = None) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        return parents[0]

[docs]def get_variation_operator(name: str, **kwargs) -> VariationOperator:
    """Get the variaton operator class with the given name."""
    name_to_cls = {
        "deletion": DeletionMutation,
        "addition": AdditionMutation,
        "alternation": Alternation,
        "genesis": Genesis,
        "cloning": Cloning,
        # UMAD citation: https://dl.acm.org/citation.cfm?id=3205455.3205603
        "umad": VariationPipeline([AdditionMutation(0.09), DeletionMutation(0.0826)]),
        "umad-shrink": VariationPipeline([AdditionMutation(0.09), DeletionMutation(0.1)]),
        "umad-grow": VariationPipeline([AdditionMutation(0.09), DeletionMutation(0.0652)])
    }
    op = name_to_cls.get(name, None)
    if op is None:
        raise ValueError("No variation operator '{nm}'. Supported names: {lst}.".format(
            nm=name,
            lst=list(name_to_cls.keys())
        ))
    if isinstance(op, type):
        op = instantiate_using(op, kwargs)
    return op

Quick search

    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>

Navigation

          index

          modules |

PyshGP 0.1.8 documentation »

Module code »

pyshgp.gp.variation

        © Copyright 2020, Edward Pantridge.
      Created using Sphinx 3.2.1.


text:
using namespace seqan;
using namespace std;

template<typename TIterator>
int recGoDown(TIterator it){
    if (repLength(it)<=3){
	cout << representative(it) << " ";
	while(goDown(it) && repLength(it)<=3)
	    cout << representative(it) << " ";
    }
    while(!goRight(it)){
	if (!goUp(it))
	    return 0;
    }
    return recGoDown(it);
}

template < typename TIndex >
int myIndexExample(){
    String<char> haystack = "tobeornottobe";

    TIndex index(haystack);

    typedef typename Iterator<TIndex, TopDown<ParentLinks<> > >::Type TIteratorIndex;
    TIteratorIndex it(index);

    recGoDown(it);
    cout << endl;
}

int main()
{
    myIndexExample<Index<String<char>, IndexEsa<> > >();
    myIndexExample<Index<String<char>, IndexWotd<> > >();
    return 0;
}


text:
package vars

import (
	"encoding/json"
	"fmt"
	"sort"

	"github.com/Sirupsen/logrus"
	"github.com/catalyzeio/cli/commands/services"
	"gopkg.in/yaml.v2"
)

type Formatter interface {
	Output(envVars map[string]string) error
}

type JSONFormatter struct{}

func (j *JSONFormatter) Output(envVars map[string]string) error {
	jsonVars := map[string]string{}
	for k, v := range envVars {
		jsonVars[k] = v
	}
	b, err := json.MarshalIndent(jsonVars, "", "    ")
	if err != nil {
		return err
	}
	logrus.Println(string(b))
	return nil
}

type YAMLFormatter struct{}

func (y *YAMLFormatter) Output(envVars map[string]string) error {
	b, err := yaml.Marshal(envVars)
	if err != nil {
		return err
	}
	logrus.Println(string(b))
	return nil
}

type PlainFormatter struct{}

func (p *PlainFormatter) Output(envVars map[string]string) error {
	var keys []string
	for k := range envVars {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, key := range keys {
		logrus.Printf("%s=%s", key, envVars[key])
	}
	return nil
}

func CmdList(svcName, defaultSvcID string, formatter Formatter, iv IVars, is services.IServices) error {
	if svcName != "" {
		service, err := is.RetrieveByLabel(svcName)
		if err != nil {
			return err
		}
		if service == nil {
			return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
		}
		defaultSvcID = service.ID
	}
	envVars, err := iv.List(defaultSvcID)
	if err != nil {
		return err
	}
	if len(envVars) == 0 {
		logrus.Println("No environment variables found")
		return nil
	}
	return formatter.Output(envVars)
}

// List lists all environment variables.
func (v *SVars) List(svcID string) (map[string]string, error) {
	headers := v.Settings.HTTPManager.GetHeaders(v.Settings.SessionToken, v.Settings.Version, v.Settings.Pod, v.Settings.UsersID)
	resp, statusCode, err := v.Settings.HTTPManager.Get(nil, fmt.Sprintf("%s%s/environments/%s/services/%s/env", v.Settings.PaasHost, v.Settings.PaasHostVersion, v.Settings.EnvironmentID, svcID), headers)
	if err != nil {
		return nil, err
	}
	var envVars map[string]string
	err = v.Settings.HTTPManager.ConvertResp(resp, statusCode, &envVars)
	if err != nil {
		return nil, err
	}
	return envVars, nil
}


text:
<?php

header('Content-type: text/html; charset=utf-8');

$prumoStyle       = pGetTheme('style.css', true);

echo '<!DOCTYPE HTML>'."\n";
echo '<html>'."\n\n";

echo '<head>'."\n";
echo '    <title>' . $GLOBALS['pConfig']['appIdent'] . ' ' . $GLOBALS['pConfig']['appName'].'</title>'."\n";

if (file_exists(dirname(__DIR__).'/favicon.ico')) {
    echo '    <link rel="shortcut icon" href="'.$GLOBALS['pConfig']['appWebPath'].'/favicon.ico" type="image/x-icon" />'."\n";
}

echo '    <link type="text/css" rel="stylesheet" media="screen" href="'.$prumoStyle.'" />'."\n";

if (file_exists(dirname(__DIR__).'/style.css')) {
    echo '    <link type="text/css" rel="stylesheet" media="screen" href="'.$GLOBALS['pConfig']['appWebPath'].'/style.css" />'."\n";
}

echo '</head>'."\n";
echo '<script type="text/javascript" src="'.$GLOBALS['pConfig']['prumoWebPath'].'/prumo-'.$GLOBALS['pConfig']['version'].'.js"></script>'."\n";
echo '<body>'."\n";