text:
import json
import time
import telnetlib
import traceback
import sys
from fabric.api import *
from fabric.colors import *
from fabric.contrib.console import *
from fabric.contrib.files import *
import util
import importlib
from gw_cmd import *

config = None

def set_config(config_module):
    global config
    config = config_module

def init():
    global cm_conn

    # Ping Check
    try:
        cm_conn = telnetlib.Telnet(config.CONF_MASTER_IP, config.CONF_MASTER_PORT)
        cm_conn.write('cluster_ls\r\n')
        ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
        ret = json.loads(ret)
        if 'redirect' == ret['state']:
            cm_conn.close()

            config.CONF_MASTER_IP = ret['data']['ip']
            config.CONF_MASTER_PORT = int(ret['data']['port'])
            cm_conn = telnetlib.Telnet(config.CONF_MASTER_IP, config.CONF_MASTER_PORT)
            cm_conn.write('cluster_ls\r\n')
            ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
            ret = json.loads(ret)
            if 'success' != ret['state']:
                warn(red('Can not connect to Conf master. Aborting...'))
                return False

        elif 'success' != ret['state']:
            warn(red('Can not connect to Conf master. Aborting...'))
            return False
    except:
        warn(red('Can not connect to Conf master. Aborting...'))
        return False

    return True

def pm_ls():
    cm_conn.write('pm_ls\r\n')
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret.encode('ascii'))
    if json_data['state'] != 'success':
        warn(red("Get pm_ls fail."))
        return None
    return json_data['data']

def pm_info(pm_name):
    cmd = 'pm_info %s\r\n' % pm_name
    cm_conn.write(cmd)
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red("Get pm_info fail."))
        return None
    return json_data['data']

def pm_add(name, ip):
    cmd = 'pm_add %s %s\r\n' % (name, ip)
    cm_conn.write(cmd)
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red('PM Add fail. cmd:%s, ret:%s' % (cmd[:-2], ret.strip())))
        return False
    return True

def cluster_ls():
    cm_conn.write('cluster_ls\r\n')
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red("Cluster '%s' doesn't exist." % cluster_name))
        return None
    return json_data

def cluster_add(name, quorum_policy):
    cm_conn.write('cluster_add %s %s\r\n' % (name, quorum_policy))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red("Add cluster '%s' fail." % name))
        return False
    return True

def cluster_del(name):
    cm_conn.write('cluster_del %s\r\n' % (name))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red("Delete cluster '%s' fail." % name))
        return False
    return True

def cluster_info(cluster_name):
    cm_conn.write('cluster_info %s\r\n' % cluster_name)
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red("Cluster '%s' doesn't exist." % cluster_name))
        return None
    json_data['data']['cluster_name'] = cluster_name
    return json_data

def pg_add(cluster_name, pg_id):
    cm_conn.write('pg_add %s %d\r\n' % (cluster_name, pg_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red("Add PG '%d' fail." % pg_id))
        return False
    return True

def pg_del(cluster_name, pg_id):
    cm_conn.write('pg_del %s %d\r\n' % (cluster_name, pg_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        warn(red("Delete PG '%d' fail." % pg_id))
        return False
    return True

def pg_infos(cluster_name, pg_ids):
    cluster_json = cluster_info(cluster_name)
    if cluster_json['state'] != 'success':
        warn(red("Cluster '%s' doesn't exist." % cluster_name))
        return None

    slot_map = util.slot_rle_to_map(cluster_json['data']['cluster_info']['PN_PG_Map'])

    cmd = ''
    for pg_id in pg_ids:
        cmd = cmd + 'pg_info %s %s\r\n' % (cluster_name, pg_id.encode('ascii'))

    cm_conn.write(cmd)

    pg_list = []
    for i in range(len(pg_ids)):
        reply = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
        pg_json = pgstr_to_json(int(pg_ids[i]), reply, slot_map)
        pg_list.append(pg_json)

    return sorted(pg_list, key=lambda x: int(x['pg_id']))

"""
Get pg information from mgmt-cc

Args:
    cluster_name : cluster name
    pg_id : partition group id
    cluster_json : It is cached data for cluster_info.
                   When cluster_json is None, pg_info() get cluster-info from MGMT-CC

Returns:
    dict: On success, it returns dick of pg; on error, it returns None.
"""
def pg_info(cluster_name, pg_id, cluster_json=None):
    if cluster_json == None:
        cluster_json = cluster_info(cluster_name)
        if cluster_json['state'] != 'success':
            warn(red("Cluster '%s' doesn't exist." % cluster_name))
            return None

    slot_map = util.slot_rle_to_map(cluster_json['data']['cluster_info']['PN_PG_Map'])

    cm_conn.write('pg_info %s %d\r\n' % (cluster_name, pg_id))
    reply = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    return pgstr_to_json(pg_id, reply, slot_map)

def pgstr_to_json(pg_id, json_str, slot_map):
    json_data = json.loads(json_str)
    if json_data['state'] != 'success':
        warn(red("PG '%d' doesn't exist." % pg_id))
        return None

    pg_json = json_data['data']
    pg_json['pg_id'] = pg_id
    if slot_map.get(pg_id) == None:
        pg_json['slot'] = ''
    else:
        pg_json['slot'] = slot_map[pg_id]

    if len(pg_json['master_Gen_Map']) == 0:
        pg_json['master_Gen'] = -1
    else:
        pg_json['master_Gen'] = max(int(k) for k in pg_json['master_Gen_Map']) + 1

    return pg_json

def get_master_pgs(cluster_name, pg_id):
    pgs_list = get_pgs_list(cluster_name, pg_id)
    if pgs_list == None:
        warn(red("Get PGS list fail. CLUSTER_NAME:%s, PG_ID:%d" % (cluster_name, pg_id)))
        return None

    for pgs_id, pgs_data in pgs_list.items():
        if pgs_data['smr_role'] == 'M':
            return pgs_data

    return None

"""
Get pgs list from mgmt-cc

Args:
    cluster_name : cluster name
    pg_id : partition group id
    cluster_json : It is cached data for cluster_info.
                   When cluster_json is None, pg_info() get cluster_info from MGMT-CC

Returns:
    dict: On success, it returns map of pgs; on error, it returns None.
"""
def get_pgs_list(cluster_name, pg_id, cluster_json=None):
    # Get cluster info from Conf Master
    if cluster_json == None:
        cluster_json = cluster_info(cluster_name)
        if cluster_json == None: return None

    exist = [k for loop_pg_data in cluster_json['data']['pg_list']
                    for k, v in loop_pg_data.iteritems() if k == 'pg_id' and int(v) == pg_id]
    if len(exist) == 0:
        warn(red("PG '%d' doesn't exist." % pg_id))
        return None

    pgs_list = {}
    # Get PG and PGS info from Json
    for pg_data in cluster_json['data']['pg_list']:
        cur_pg_id = int(pg_data['pg_id'])
        if cur_pg_id != pg_id:
            continue

        for pgs_id in pg_data['pg_data']['pgs_ID_List']:
            pgs_json_data = pgs_info(cluster_name, pgs_id)
            if pgs_json_data == None:
                warn(red("PGS '%s' doesn't exist." % pgs_id))
                return None
            pgs_list[pgs_id] = pgs_json_data['data']

    if len(pgs_list) == 0:
        return None

    return pgs_list

def get_joined_pgs_count(cluster_name, pg_id):
    pgs_list = get_pgs_list(cluster_name, pg_id)
    if pgs_list == None:
        return -1

    cnt = 0
    for id, data in pgs_list.items():
        if data['hb'] == 'Y':
            cnt = cnt + 1
    return cnt

def pgs_info(cluster_name, pgs_id):
    cm_conn.write('pgs_info %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        return None

    json_data['data']['pgs_id'] = pgs_id
    json_data['data']['pg_id'] = json_data['data']['pg_ID']
    json_data['data']['ip'] = json_data['data']['pm_IP'].encode('ascii')
    json_data['data']['pm_name'] = json_data['data']['pm_Name'].encode('ascii')
    json_data['data']['redis_port'] = json_data['data']['backend_Port_Of_Redis']
    json_data['data']['smr_base_port'] = json_data['data']['replicator_Port_Of_SMR']
    json_data['data']['mgmt_port'] = json_data['data']['management_Port_Of_SMR']
    json_data['data']['smr_role'] = json_data['data']['smr_Role'].encode('ascii')
    json_data['data']['hb'] = json_data['data']['hb'].encode('ascii')

    return json_data

def pgs_add(cluster_name, pgs_id, pg_id, pm_name, pm_ip, port, host):
    if config.confirm_mode and not confirm(cyan('[%s] PGS Add, CLUSTER:%s, PGS_ID:%d. Continue?' % (host, cluster_name, pgs_id))):
        warn("Aborting at user request.")
        return False

    cmd = 'pgs_add %s %d %d %s %s %d %d\r\n' % (cluster_name, pgs_id, pg_id, pm_name, pm_ip, port, port + 9)
    cm_conn.write(cmd)
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red('[%s] PGS Add fail. cmd:%s, ret:%s' % (host, cmd[:-2], ret.strip())))
        return False

    cm_conn.write('pgs_info %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    print ret.strip() + '\n'

    print green('[%s] PGS Add success' % host)
    return True

def pgs_join(cluster_name, pgs_id, ip, smr_base_port, host):
    print magenta("\n[%s] PGS Join" % host)
    cm_conn.write('pgs_info %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    print ret.strip() + '\n'

    if config.confirm_mode and not confirm(cyan('[%s] PGS Join, PGS_ID:%d. Continue?' % (host, pgs_id))):
        warn("Aborting at user request.")
        return False

    cm_conn.write('pgs_join %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red('[%s] PGS Join fail, ret:%s' % (host, ret.strip())))
        return False

    return util.check_smr_state(ip, smr_base_port, host)

def pgs_leave(cluster_name, pgs_id, ip, redis_port, host, check=True, forced=False):
    print magenta("\n[%s] PGS Leave" % host)
    cm_conn.write('pgs_info %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    print ret.strip() + '\n'

    if config.confirm_mode and not confirm(cyan('[%s] PGS Leave, PGS_ID:%d. Continue?' % (host, pgs_id))):
        warn("Aborting at user request.")
        return False

    cmd = 'pgs_leave %s %d' % (cluster_name, pgs_id)
    if forced:
        cmd = cmd + ' forced'
    cm_conn.write(cmd + '\r\n')
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red('[%s] PGS Leave fail, ret:%s' % (host, ret.strip())))
        return False

    # Check Redis client connection
    if check == True:
        conn = None
        try:
            conn = telnetlib.Telnet(ip, redis_port)

            while True:
                conn.write('info clients\r\n')
                reply = conn.read_until('\r\n', config.TELNET_TIMEOUT)
                size = int(reply[1:])

                readlen = 0
                while readlen <= size:
                    ret = conn.read_until('\r\n', config.TELNET_TIMEOUT)
                    readlen += len(ret)
                    ret = ret.strip()
                    if ret.find('connected_clients') != -1 and ret.find(':') != -1:
                        tokens = ret.split(':')
                        print yellow('[%s:%d] >>> %s' % (ip, redis_port, ret))
                        num_connected = int(tokens[1])

                if (num_connected < 10): break
                time.sleep(0.5)
        except:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type, exc_value, exc_traceback, limit=3, file=sys.stdout)
            warn(red('[%s] PGS Leave fail, Can not connect to Redis server. %s:%d' % (host, ip, redis_port)))
            return False
        finally:
            if conn != None:
                conn.close()

    print green('[%s] PGS Leave success' % host)
    return True

def pgs_lconn(cluster_name, pgs_id, ip, smr_base_port, host):
    print magenta("\n[%s] PGS lconn" % host)
    cm_conn.write('pgs_info %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    print ret.strip() + '\n'

    if config.confirm_mode and not confirm(cyan('[%s] PGS lconn, PGS_ID:%d. Continue?' % (host, pgs_id))):
        warn("Aborting at user request.")
        return False

    cm_conn.write('pgs_lconn %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red('[%s] PGS lconn fail, ret:%s' % (host, ret.strip())))
        return False

    # Check SMR State
    try:
        conn = telnetlib.Telnet(ip, smr_base_port + 3)

        while True:
            conn.write('ping\r\n')
            ret = conn.read_until('\r\n', config.TELNET_TIMEOUT)
            print yellow('[%s] >>> %s' % (host, ret.strip()))
            if '+OK 1' in ret: break
            time.sleep(0.5)
    except:
        warn(red('[%s] PGS lconn fail, Can not connect to SMR replicator. %s:%d' % (host, ip, smr_base_port)))

    conn.close()

    print green('[%s] PGS lconn success' % host)
    return True

def pgs_del(cluster_name, pgs_id, host):
    if config.confirm_mode and not confirm(cyan('[%s] PGS Del, CLUSTER:%s, PGS_ID:%d. Continue?' % (host, cluster_name, pgs_id))):
        warn("Aborting at user request.")
        return False

    cm_conn.write('pgs_del %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red('[%s] PGS Del fail, ret:%s' % (host, ret.strip())))
        return False

    cm_conn.write('pgs_info %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] == 'success':
        warn(red('[%s] PGS Del fail, ret:%s' % (host, ret.strip())))
        return False

    print green('[%s] PGS Del success' % host)
    return True

# On success, role_change() returns pgs_id of new master; on error, it returns -1
def role_change(cluster_name, pgs_id, host):
    print magenta("\n[%s] Role Change" % host)

    cm_conn.write('role_change %s %d\r\n' % (cluster_name, pgs_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        print green("[%s] Role Change fail, ret:%s" % (host, ret.strip()))
        return -1

    print green("[%s] Role Change success" % host)
    if json_data.get('data') != None:
        return int(json_data['data']['master'])
    return pgs_id

def get_gw_list(cluster_name):
    # Get cluster info from Conf Master
    json_data = cluster_info(cluster_name)
    if json_data == None: return None

    gw_list = {}
    # Get GW info from Json
    for gw_id in json_data['data']['gw_id_list']:
        gw_json_data = gw_info(cluster_name, gw_id)
        if gw_json_data == None:
            warn(red("Get gateway info fail. CLUSTER_NAME:%s, GW_ID:%d" % (cluster_name, gw_id)))
            return

        gw_list[gw_id] = gw_json_data['data']

    if len(gw_list) == 0:
        return None

    return gw_list

def gw_info(cluster_name, gw_id):
    cm_conn.write('gw_info %s %d\r\n' % (cluster_name, gw_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success':
        return None
    return json_data

def gw_add(cluster_name, gw_id, pm_name, ip, port):
    host = ip
    print magenta("\n[%s] GW Add" % host)

    if config.confirm_mode and not confirm(cyan('[%s] GW Add, GW_ID:%d. Continue?' % (host, gw_id))):
        warn("Aborting at user request.")
        return False

    cmd = 'gw_add %s %d %s %s %d\r\n' % (cluster_name, gw_id, pm_name, ip, port)
    cm_conn.write(cmd)
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red('[%s] GW Add fail, ret:%s' % (host, ret.strip())))
        return False

    # Check gateway client connection
    try:
        with GwCmd(ip, port) as gw_cmd:
            for i in range(15):
                print yellow('[%s:%d] >>> inactive_connections:%d' % (host, port, gw_cmd.info_redis_discoons()))
                print yellow('[%s:%d] >>> gateway_connected_clients:%d' % (host, port, gw_cmd.info_num_of_clients()))
                if gw_cmd.info_redis_discoons() == 0 and gw_cmd.info_num_of_clients() >= 1 + config.CONF_MASTER_MGMT_CONS:
                    break
                else:
                    time.sleep(0.5)

    except:
        warn(red('[%s] GW Add fail, Can not connect to Gateway server. %s:%d' % (host, ip, port)))
        return False

    print green('[%s] GW Add success' % host)
    return True

def gw_del(cluster_name, gw_id, ip, port):
    host = ip
    print magenta("\n[%s] GW Del" % host)
    info = gw_info(cluster_name, gw_id)
    print info

    if config.confirm_mode and not confirm(cyan('[%s] GW Del, GW_ID:%d. Continue?' % (host, gw_id))):
        warn("Aborting at user request.")
        return False

    cm_conn.write('gw_del %s %d\r\n' % (cluster_name, gw_id))
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red('[%s] GW Del fail, ret:%s' % (host, ret.strip())))
        return False

    # Check gateway client connection
    try:
        with GwCmd(ip, port) as gw_cmd:
            # 1. Wait until all client-connections is disconnected.
            ok = False
            old_total_commands_processed = 0
            for i in range(16):
                num_of_clients = gw_cmd.info_num_of_clients()
                total_commands = gw_cmd.info_total_commands_processed()
                print yellow('[%s:%d] >>> gateway_connected_clients:%d' % (host, port, num_of_clients))
                print yellow('[%s:%d] >>> gateway_total_commands_processed:%d (normally +2)' % (host, port, total_commands))

                if num_of_clients == 1 and old_total_commands_processed + 3 > total_commands:
                    ok = True
                    break
                else:
                    time.sleep(0.5)
                old_total_commands_processed = total_commands

            # 2. Wait a few seconds for client's pipelining operations.
            if ok == True:
                for cti in range(config.CLIENT_TIMEOUT * 4):
                    num_of_clients = gw_cmd.info_num_of_clients()
                    total_commands = gw_cmd.info_total_commands_processed()
                    print yellow('[%s:%d] >>> gateway_connected_clients:%d' % (host, port, num_of_clients))
                    print yellow('[%s:%d] >>> gateway_total_commands_processed:%d (normally +2)' % (host, port, total_commands))

                    if num_of_clients != 1 and old_total_commands_processed + 3 <= total_commands:
                        ok = False
                        break
                    else:
                        time.sleep(0.5)
                    old_total_commands_processed = total_commands

            # Manual mode
            if ok == False:
                print magenta('[%s:%d] Begin manual checking.' % (host, port))

                while True:
                    num_of_clients = gw_cmd.info_num_of_clients()
                    total_commands = gw_cmd.info_total_commands_processed()
                    print yellow('[%s:%d] >>> gateway_connected_clients:%d' % (host, port, num_of_clients))
                    print yellow('[%s:%d] >>> gateway_total_commands_processed:%d (normally +2)' % (host, port, total_commands))

                    ret = prompt(cyan('[%s:%d] GW Del, number of client connection is %d. Wait? (Y/n/r)' % (host, port, gw_cmd.info_num_of_clients()))).strip().upper()
                    if ret == 'N':
                        break
                    elif ret == 'R':
                        # Rollback & Exit
                        warn(red("Rollback and Quit at user request"))
                        if gw_add(cluster_name, gw_id, info['data']['pm_Name'].encode('ascii'), ip, port) != True:
                            warn(red("[%s] Failed to rollback GW Del, GW_ID:%d, PORT:%d" % (host, gw_id, port)))
                            return False
                        return False

    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type, exc_value, exc_traceback)
        warn(red('[%s] GW Del fail. %s:%d' % (host, ip, port)))
        return False

    print green('[%s] GW Del success' % host)
    return True

def mig2pc(cluster_name, src_pg_id, dst_pg_id, range_from, range_to):
    cmd = 'mig2pc %s %d %d %d %d' % (cluster_name, src_pg_id, dst_pg_id, range_from, range_to)
    print cyan('Command : ' + cmd)
    cm_conn.write(cmd + '\r\n')
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    try:
        json_data = json.loads(ret)
        if json_data['state'] != 'success' or json_data['msg'] != '+OK':
            warn(red("mig2pc fail. cluster:%s, ret:\"%s\"" % (cluster_name, ret[:-2])))
            return False
        return True
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type, exc_value, exc_traceback)
        warn("mig2pc fail. cluster:%s" % cluster_name)
        return False

def slot_set_pg(cluster_name, dst_pg_id, range_from, range_to):
    cmd = 'slot_set_pg %s %d:%d %d' % (cluster_name, range_from, range_to, dst_pg_id)
    print cyan('Command : ' + cmd)
    cm_conn.write(cmd + '\r\n')
    ret = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(ret)
    if json_data['state'] != 'success' or json_data['msg'] != '+OK':
        warn(red("%s fail." % cmd))
        return False
    return True

def pgs_ls(cluster_name):
    cm_conn.write('pgs_ls %s\r\n' % cluster_name)
    reply = cm_conn.read_until('\r\n', config.TELNET_TIMEOUT)
    json_data = json.loads(reply)
    if json_data['state'] != 'success':
        return None
    return json_data


text:
package factory;

public interface IWork {

	Work getWork();
}

redpajama_set_name: RedPajamaGithub


text:
package org.ovirt.engine.ui.webadmin.widget.table.column;

import com.google.gwt.cell.client.ImageResourceCell;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class StyledImageResourceCell extends ImageResourceCell {

    private String style = "";

    @Override
    public void render(Context context, ImageResource value, SafeHtmlBuilder sb) {
        if (value != null) {
            SafeHtml html = SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(value).getHTML());

            sb.appendHtmlConstant("
");
            sb.append(html);
            sb.appendHtmlConstant("
");
        }
    }

    public void setStyle(String style) {
        this.style = style;
    }

}


text:
"""This code snippet creates a Dialogflow ES integration.

Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/integrate/dialogflow?method=api#dialogflow_es

This code is based on the https://github.com/google-business-communications/python-businessmessages
Python Business Messages client library.
"""

from oauth2client.service_account import ServiceAccountCredentials
from businesscommunications.businesscommunications_v1_client import BusinesscommunicationsV1
from businesscommunications.businesscommunications_v1_messages import (
    BusinesscommunicationsBrandsAgentsIntegrationsCreateRequest,
    DialogflowEsIntegration
)

# Edit the values below:
BRAND_ID = 'EDIT_HERE'
AGENT_ID = 'EDIT_HERE'
DIALOGFLOW_ES_PROJECT_ID = 'EDIT_HERE'
SCOPES = ['https://www.googleapis.com/auth/businesscommunications']
SERVICE_ACCOUNT_FILE = './service_account_key.json'

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

client = BusinesscommunicationsV1(credentials=credentials)

integrations_service = BusinesscommunicationsV1.BrandsAgentsIntegrationsService(client)

agent_name = 'brands/' + BRAND_ID + '/agents/' + AGENT_ID

integration = integrations_service.Create(BusinesscommunicationsBrandsAgentsIntegrationsCreateRequest(
  integration=DialogflowEsIntegration(
    autoResponseStatus=DialogflowEsIntegration.AutoResponseStatusValueValuesEnum.ENABLED,
    dialogflowProjectId=DIALOGFLOW_ES_PROJECT_ID
  ),
  parent=agent_name
))

print(integration)


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerArgs;

namespace hst.ActionArgs
{
    public class BaseActionArgs
    {
        public BaseActionArgs()
        {
            Result = new HostResult();
        }

        [ArgIgnore]
        public HostResult Result { get; set; }

        [ArgPosition(1)]
        [ArgRequired(PromptIfMissing = true)]
        [ArgDescription("Host name to perform action on")]
        public string Name { get; set; }

        [ArgPosition(2)]
        [DefaultValue("127.0.0.1")]
        [ArgDescription("Network address (v4 | v6)")]
        public string Address { get; set; }

    }
}


text:
package smile.feature;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Haifeng Li
 */
public class BagTest {

    public BagTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    /**
     * Test of the uniqueness of features in the class Bag
     */
    public void testUniquenessOfFeatures() {
        System.out.println("unique features");
        String[] featuresForBirdStories = {"crane", "sparrow", "hawk", "owl", "kiwi"};
        String[] featuresForBuildingStories = {"truck", "concrete", "foundation", "steel", "crane"};
        String testMessage = "This story is about a crane and a sparrow";

        ArrayList<String> mergedFeatureLists = new ArrayList<String>();
        mergedFeatureLists.addAll(Arrays.asList(featuresForBirdStories));
        mergedFeatureLists.addAll(Arrays.asList(featuresForBuildingStories));

        Bag<String> bag = new Bag<String>(mergedFeatureLists.toArray(new String[featuresForBirdStories.length + featuresForBuildingStories.length]));

        double[] result = bag.feature(testMessage.split(" "));
        assertEquals(9, result.length);
    }

    /**
     * Test of feature method, of class Bag.
     */
    @Test
    public void testFeature() {
        System.out.println("feature");
        String[][] text = new String[2000][];

        BufferedReader input = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/smile/data/text/movie.txt")));
        try {
            for (int i = 0; i < text.length; i++) {
                String[] words = input.readLine().trim().split("\\s+");
                text[i] = words;
            }
        } catch (IOException ex) {
            System.err.println(ex);
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                System.err.println(e);
            }
        }

        String[] feature = {
            "outstanding", "wonderfully", "wasted", "lame", "awful", "poorly",
            "ridiculous", "waste", "worst", "bland", "unfunny", "stupid", "dull",
            "fantastic", "laughable", "mess", "pointless", "terrific", "memorable",
            "superb", "boring", "badly", "subtle", "terrible", "excellent",
            "perfectly", "masterpiece", "realistic", "flaws"
        };

        Bag<String> bag = new Bag<String>(feature);

        double[][] x = new double[text.length][];
        for (int i = 0; i < text.length; i++) {
            x[i] = bag.feature(text[i]);
            assertEquals(feature.length, x[i].length);
        }

        assertEquals(1.0, x[0][15], 1E-7);
    }
}


text:
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'background_queue_server'

describe BackgroundQueue::ServerLib::TaskRegistry do

  let(:task) { SimpleTask.new(:owner_id, :job_id, :task_id, 3) }
  let(:task2) { SimpleTask.new(:owner_id, :job_id, :task_id, 3) }

  context "#register_waiting_task" do
    it "will add task to waiting list" do
      subject.register_waiting_task(task)
      subject.waiting_tasks[:task_id].should eq(task)
    end

    it "will remove and existing waiting task with same id" do
      subject.register_waiting_task(task)
      subject.register_waiting_task(task2)
      subject.waiting_tasks[:task_id].should_not eq(task)
      subject.waiting_tasks[:task_id].should eq(task2)
    end
  end

  context "#get_waiting_task" do
    it "will get a matching task and remove it from the list" do
      subject.register_waiting_task(task)
      subject.waiting_tasks.should have(1).item
      subject.get_waiting_task(:task_id).should eq(task)
      subject.waiting_tasks.should have(0).items
    end

    it "will return nil if not waiting task" do
      subject.get_waiting_task(:task_id).should eq(nil)
    end
  end

  context "#register" do
    it "will add the task if it does not exist and return :new" do
      subject.register(task).should eq([:new, nil])
      subject.tasks.should have(1).item
    end

    it "will add the task to waiting list if running and return :waiting" do
      subject.register(task)
      task.running = true
      subject.register(task2).should eq([:waiting, nil])
    end

    it "will replace an existing task and return the existing task" do
      subject.register(task)
      subject.register(task2).should eq([:existing, task])
    end
  end

  context "#de_register" do
    it "will remove the task from the list of tasks and return nil if none are waiting" do
      subject.register(task)
      subject.de_register(task.id).should eq(nil)
    end

    it "will remove the task from the list of tasks and return waiting task if none are waiting" do
      subject.register(task)
      task.running = true
      subject.register(task2)
      subject.de_register(task.id).should eq(task2)
      subject.tasks[task.id].should eq(task2)
    end
  end
end


text:
layout: post
title: Double-ended queue and Binary Tree implementations in Go
tags: golang go deque queue double-ended-queue algorithms tree binary-tree
---
I've been checking out some old code of mine, which I've written years
ago. Some of it dates back almost 20 years ago, and contains my first
ever programming projects, which consist of some games written in C
using the [Allegro
library](https://en.wikipedia.org/wiki/Allegro_(software_library)), a
program for locking binaries by doing a simple
[xor](https://en.wikipedia.org/wiki/Exclusive_or), and some
implementations of various algorithms I've read about back then.

This old code has been recovered from some really old CD-ROM drives,
and was quite interesting for me to go back in time and look at some
of my first code. In a future post I'll probably share more about my
early programming projects, which I managed to recover from these
CD-ROM drives.

At some point I thought about brushing up my algorithms and decided to
implement some of them from scratch. It was a fun weekend exercise for
me to go back to the basics and do a bit of linked lists, binary
trees, queues, stacks, graphs, searching algorithms, sorting, etc.
All in all, a pretty refreshing experience I would say.

Some of the code I managed to clean up and document, so I thought
about publishing it as well on Github, in case this turns out useful
to anyone else eventually.

The first library I've published is an implementation of [double-ended
queues](https://en.wikipedia.org/wiki/Double-ended_queue) in
Go. Pretty simple and straightforward. Here's how to install it.

``` shell
go get -v gopkg.in/dnaeon/go-deque.v1
```

You can find the code of the deque implementation in the
[dnaeon/go-deque](https://github.com/dnaeon/go-deque) repo.

The library supports generics, so you can use it with any type. Here's
an example of a deque, which stores integers.

``` go
package main

import (
	"fmt"
	"os"

	deque "gopkg.in/dnaeon/go-deque.v1"
)

func main() {
	d := deque.New[int]()

	// Insert a few items to work with
	// The deque looks like this: 5 4 1 2 3
	d.PushBack(1)
	d.PushBack(2)
	d.PushBack(3)
	d.PushFront(4)
	d.PushFront(5)

	fmt.Printf("deque len: %d\n", d.Length())

	// Consume all items - front to back
	for !d.IsEmpty() {
		item, err := d.PopFront()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		fmt.Printf("got item: %d\n", item)
	}

	fmt.Printf("deque len: %d\n", d.Length())
}
```

Running above code produces the following output.

``` shell
deque len: 5
got item: 5
got item: 4
got item: 1
got item: 2
got item: 3
deque len: 0
```

The other library I've published is a simple, and generic
implementation of [binary
trees](https://en.wikipedia.org/wiki/Binary_tree) in Go.

It implements various algorithms for binary trees, such as calculating
height and size of the tree, walking the tree in _in-_, _pre-_,
_post-_ and _level-order_, and comes with a number of test predicates
to check whether a tree is _Perfect_, _Complete_, _Binary Search
Tree_, etc.

You can also generate a [Dot
representation](https://en.wikipedia.org/wiki/DOT_(graph_description_language))
of your tree and render it using [graphviz](https://graphviz.org/).

You can find the full code in the
[dnaeon/go-binarytree](https://github.com/dnaeon/go-binarytree) repo.
Here's an example usage of the library.

``` go
package main

import (
	"fmt"

	binarytree "gopkg.in/dnaeon/go-binarytree.v1"
)

func main() {
	root := binarytree.NewNode(10)
	five := root.InsertLeft(5)
	twenty := root.InsertRight(20)
	five.InsertLeft(9)
	five.InsertRight(18)
	twenty.InsertLeft(3)
	twenty.InsertRight(7)

	fmt.Printf("height of tree: %d\n", root.Height())
	fmt.Printf("size of the tree: %d\n", root.Size())
	fmt.Printf("tree is balanced: %t\n", root.IsBalancedTree())
	fmt.Printf("tree is complete: %t\n", root.IsCompleteTree())
	fmt.Printf("tree is perfect: %t\n", root.IsPerfectTree())

	// Function to be called while walking the tree, which simply
	// prints the values of each visited node
	walkFunc := func(n *binarytree.Node[int]) error {
		fmt.Printf("%d ", n.Value)
		return nil
	}

	fmt.Printf("in-order values: ")
	root.WalkInOrder(walkFunc)
	fmt.Println()

	fmt.Printf("pre-order values: ")
	root.WalkPreOrder(walkFunc)
	fmt.Println()

	fmt.Printf("post-orer values: ")
	root.WalkPostOrder(walkFunc)
	fmt.Println()

	fmt.Printf("level-order values: ")
	root.WalkLevelOrder(walkFunc)
	fmt.Println()
}
```

Running above code produces the following output.

``` shell
height of tree: 2
size of the tree: 7
tree is balanced: true
tree is complete: true
tree is perfect: true
in-order values: 9 5 18 10 3 20 7
pre-order values: 10 5 9 18 20 3 7
post-orer values: 9 18 5 3 7 20 10
level-order values: 10 5 20 9 18 3 7
```

We can generate the Dot representation of a tree using this code.

``` go
package main

import (
	"os"

	"gopkg.in/dnaeon/go-binarytree.v1"
)

func main() {
	root := binarytree.NewNode(10)
	five := root.InsertLeft(5)
	twenty := root.InsertRight(20)
	five.InsertLeft(9)
	five.InsertRight(18)
	twenty.InsertLeft(3)
	twenty.InsertRight(7)

	root.WriteDot(os.Stdout)
}
```

Running above example produces an output similar to this one.

``` shell
digraph {
        node [color=lightblue fillcolor=lightblue fontcolor=black shape=record style="filled, rounded"]
        824634441792 [label="<l>|<v> 10|<r>" ]
        824634441792:l -> 824634441856:v
        824634441792:r -> 824634441920:v
        824634441856 [label="<l>|<v> 5|<r>" ]
        824634441856:l -> 824634441984:v
        824634441856:r -> 824634442048:v
        824634441984 [label="<l>|<v> 9|<r>" ]
        824634442048 [label="<l>|<v> 18|<r>" ]
        824634441920 [label="<l>|<v> 20|<r>" ]
        824634441920:l -> 824634442112:v
        824634441920:r -> 824634442176:v
        824634442112 [label="<l>|<v> 3|<r>" ]
        824634442176 [label="<l>|<v> 7|<r>" ]
}
```

Finally, we can render the binarytree using _graphviz_:

``` shell
dot -Tsvg /path/to/file.dot -o /tmp/to/file.svg
```

And here's how our tree looks like.

![_config.yml]({{ site.baseurl }}/images/go-binarytree.svg)


text:
"""
    Knowledge api: test suites
"""
import simplejson as json
from django.test import TestCase
from django.test.client import Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User as DjangoUser
from maker.core.models import User, Group, Perspective, ModuleSetting, Object
from maker.knowledge.models import KnowledgeFolder, KnowledgeItem, KnowledgeCategory

class KnowledgeViewsTest(TestCase):
    "Knowledge functional tests for views"

    username = "api_test"
    password = "api_password"
    prepared = False
    authentication_headers ={"CONTENT_TYPE": "application/json",
                             "HTTP_AUTHORIZATION" : "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" }
    content_type ='application/json'
    prepared = False

    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            # Clean up first
            Object.objects.all().delete()

            # Create objects
            try:
                self.group = Group.objects.get(name='test')
            except Group.DoesNotExist:
                Group.objects.all().delete()
                self.group = Group(name='test')
                self.group.save()

            try:
                self.user = DjangoUser.objects.get(username=self.username)
                self.user.set_password(self.password)
                try:
                    self.profile = self.user.get_profile()
                except Exception:
                    User.objects.all().delete()
                    self.user = DjangoUser(username=self.username, password='')
                    self.user.set_password(self.password)
                    self.user.save()
            except DjangoUser.DoesNotExist:
                User.objects.all().delete()
                self.user = DjangoUser(username=self.username, password='')
                self.user.set_password(self.password)
                self.user.save()

            try:
                perspective = Perspective.objects.get(name='default')
            except Perspective.DoesNotExist:
                Perspective.objects.all().delete()
                perspective = Perspective(name='default')
                perspective.set_default_user()
                perspective.save()
            ModuleSetting.set('default_perspective', perspective.id)

            self.folder = KnowledgeFolder(name='test', treepath='test')
            self.folder.set_default_user()
            self.folder.save()

            self.category = KnowledgeCategory(name='test', treepath='test')
            self.category.set_default_user()
            self.category.save()

            self.item = KnowledgeItem(name='test', folder=self.folder,
                                      category=self.category, treepath='test')
            self.item.set_default_user()
            self.item.save()

            # parent folder
            self.parent = KnowledgeFolder(name='test', treepath='test')
            self.parent.set_default_user()
            self.parent.save()

            self.client = Client()

            self.prepared = True

    def test_unauthenticated_access(self):
        "Test index page at /api/knowledge/folders"
        response = self.client.get('/api/knowledge/folders')
        # Redirects as unauthenticated
        self.assertEquals(response.status_code, 401)

    def test_get_folders_list(self):
        """ Test index page api/knowledge/folders """
        response = self.client.get(path=reverse('api_knowledge_folders'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_folder(self):
        response = self.client.get(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_folder(self):
        updates = {'name': 'Api folder update', 'parent': self.parent.id, 'details': '
api details
'}
        response = self.client.put(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["parent"], data["parent"]["id"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_categories_list(self):
        """ Test index page api/knowledge/categories """
        response = self.client.get(path=reverse('api_knowledge_categories'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_category(self):
        response = self.client.get(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_category(self):
        updates = {'name': 'Api catagory update', 'details': '
api details
'}
        response = self.client.put(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_items_list(self):
        """ Test index page api/knowledge/items """
        response = self.client.get(path=reverse('api_knowledge_items'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_item(self):
        response = self.client.get(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_item(self):
        updates = {'name': 'Api item update', 'folder': self.folder.id, 'category': self.category.id,
                   'body': '
api body
'}
        response = self.client.put(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 400)

        #data = json.loads(response.content)
        #self.assertEquals(updates["name"], data["name"])
        #self.assertEquals(updates["body"], data["body"])
        #self.assertEquals(updates["folder"], data["folder"]["id"])
        #self.assertEquals(updates["category"], data["category"]["id"])


text:
Your dotfiles are how you personalize your system. These are mine.
Forked from https://github.com/holman/dotfiles.git.

This was originally used on OSx, but has been converted for use on WSL.

## install

```sh
zsh
git clone https://github.com/gambtho/dotfiles.git ~/.dotfiles
cd ~/.dotfiles
script/bootstrap
reload!
dot
```

This will symlink the appropriate files in `.dotfiles` to your home directory.
Everything is configured and tweaked within `~/.dotfiles`.

The main file you'll want to change right off the bat is `zsh/zshrc.symlink`,
which sets up a few paths that'll be different on your particular machine.

`dot` is a simple script that installs some dependencies, sets sane
defaults, and so on. Tweak this script, and occasionally run `dot` from
time to time to keep your environment fresh and up-to-date. You can find
this script in `bin/`.

## topical

Everything's built around topic areas. If you're adding a new area to your
forked dotfiles — say, "Java" — you can simply add a `java` directory and put
files in there. Anything with an extension of `.zsh` will get automatically
included into your shell. Anything with an extension of `.symlink` will get
symlinked without extension into `$HOME` when you run `script/bootstrap`.

## components

There's a few special files in the hierarchy.

- **bin/**: Anything in `bin/` will get added to your `$PATH` and be made
  available everywhere.
- **packages.txt**: This is a list of applications for aptto install: Might want to edit this file before running any initial setup.
- **topic/\*.zsh**: Any files ending in `.zsh` get loaded into your
  environment.
- **topic/path.zsh**: Any file named `path.zsh` is loaded first and is
  expected to setup `$PATH` or similar.
- **topic/completion.zsh**: Any file named `completion.zsh` is loaded
  last and is expected to setup autocomplete.
- **topic/install.sh**: Any file named `install.sh` is executed when you run `script/install`. To avoid being loaded automatically, its extension is `.sh`, not `.zsh`.
- **topic/\*.symlink**: Any file ending in `*.symlink` gets symlinked into
  your `$HOME`. This is so you can keep all of those versioned in your dotfiles
  but still keep those autoloaded files in your home directory. These get
  symlinked in when you run `script/bootstrap`.

## notes and links

* cascadia code and night owl light?
* evaluate vi customizations
* https://github.com/luan/vimfiles/wiki/Luan's-Vim-Cheat-Sheet
* https://htr3n.github.io/2018/07/faster-zsh/


text: DROP TABLE IF EXISTS `phpcms_video_store`;


text:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="13dp"
        android:paddingRight="13dp"
        android:background="@drawable/diagnosis_list_item_bg">
    <TextView android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="22dp"
            android:paddingTop="30dp"
            android:paddingBottom="30dp"
            android:textSize="@dimen/text_size_xh_28px"
            android:textColor="@color/font_black_normal"
            android:layout_alignParentLeft="true"
            android:text="New Button"/>
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="30dp"
            android:paddingBottom="30dp"
            android:paddingRight="14dp"
            android:src="@drawable/setting_pic_subwoofer_volume_horn"
            android:layout_alignParentRight="true"/>
</RelativeLayout>


text:
function pyra = cnn_feat_pyramid(im, model, padx, pady, image_id)

if ~exist('padx', 'var') || isempty(padx) || ...
   ~exist('pady', 'var') || isempty(pady)
  [padx, pady] = getpadding(model);
end

if exist('image_id', 'var') || ~isempty(image_id)
  cache_dir = get_feat_cache_dir();
  cache_opts.cache_file = [cache_dir '/' image_id '.mat'];
  cache_opts.write_on_miss = true;
end
cache_opts.debug = false;
pyra = deep_pyramid_cache_wrapper(im, model.cnn, cache_opts);
pyra = deep_pyramid_add_padding(pyra, padx, pady, false);

feat_scale = 1/50;
for i = 1:pyra.num_levels
  pyra.feat{i} = feat_scale * pyra.feat{i};
end


text:
namespace llvm {
class VESubtarget;

namespace VEISD {
enum NodeType : unsigned {
  FIRST_NUMBER = ISD::BUILTIN_OP_END,
  RET_FLAG, // Return with a flag operand.
};
}

class VETargetLowering : public TargetLowering {
  const VESubtarget *Subtarget;

public:
  VETargetLowering(const TargetMachine &TM, const VESubtarget &STI);

  const char *getTargetNodeName(unsigned Opcode) const override;

  Register getRegisterByName(const char *RegName, LLT VT,
                             const MachineFunction &MF) const override;

  /// getSetCCResultType - Return the ISD::SETCC ValueType
  EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
                         EVT VT) const override;

  SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
                               bool isVarArg,
                               const SmallVectorImpl<ISD::InputArg> &Ins,
                               const SDLoc &dl, SelectionDAG &DAG,
                               SmallVectorImpl<SDValue> &InVals) const override;

  bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
                      bool isVarArg,
                      const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
                      LLVMContext &Context) const override;
  SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
                      const SmallVectorImpl<ISD::OutputArg> &Outs,
                      const SmallVectorImpl<SDValue> &OutVals, const SDLoc &dl,
                      SelectionDAG &DAG) const override;
};
} // namespace llvm

#endif // VE_ISELLOWERING_H


text:
using namespace chrono;

namespace hmmwv {

// -----------------------------------------------------------------------------
// Static variables
// -----------------------------------------------------------------------------

static const double in2m = 0.0254;
static const double lb2kg = 0.453592;

const double      HMMWV_Wheel::m_radius = in2m * 18.15;
const double      HMMWV_Wheel::m_width = in2m * 10;

const double      HMMWV_Wheel::m_mass = lb2kg * 100.00;
const ChVector<>  HMMWV_Wheel::m_inertia(0.113, 0.113, 0.113);

const std::string HMMWV_WheelLeft::m_meshName = "wheel_L_POV_geom";
const std::string HMMWV_WheelLeft::m_meshFile = vehicle::GetDataFile("hmmwv/wheel_L.obj");

const std::string HMMWV_WheelRight::m_meshName = "wheel_R_POV_geom";
const std::string HMMWV_WheelRight::m_meshFile = vehicle::GetDataFile("hmmwv/wheel_R.obj");

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
HMMWV_Wheel::HMMWV_Wheel(VisualizationType  visType)
: m_visType(visType)
{
}

HMMWV_WheelLeft::HMMWV_WheelLeft(VisualizationType  visType)
: HMMWV_Wheel(visType)
{
}

HMMWV_WheelRight::HMMWV_WheelRight(VisualizationType  visType)
: HMMWV_Wheel(visType)
{
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void HMMWV_Wheel::Initialize(ChSharedBodyPtr spindle)
{
  // First, invoke the base class method
  ChWheel::Initialize(spindle);

  // Attach visualization
  switch (m_visType) {
  case PRIMITIVES:
  {
    ChSharedPtr<ChCylinderShape> cyl(new ChCylinderShape);
    cyl->GetCylinderGeometry().rad = m_radius;
    cyl->GetCylinderGeometry().p1 = ChVector<>(0, m_width / 2, 0);
    cyl->GetCylinderGeometry().p2 = ChVector<>(0, -m_width / 2, 0);
    spindle->AddAsset(cyl);

    ChSharedPtr<ChTexture> tex(new ChTexture);
    tex->SetTextureFilename(GetChronoDataFile("bluwhite.png"));
    spindle->AddAsset(tex);

    break;
  }
  case MESH:
  {
    geometry::ChTriangleMeshConnected trimesh;
    trimesh.LoadWavefrontMesh(getMeshFile(), false, false);

    ChSharedPtr<ChTriangleMeshShape> trimesh_shape(new ChTriangleMeshShape);
    trimesh_shape->SetMesh(trimesh);
    trimesh_shape->SetName(getMeshName());
    spindle->AddAsset(trimesh_shape);

    ChSharedPtr<ChColorAsset> mcolor(new ChColorAsset(0.3f, 0.3f, 0.3f));
    spindle->AddAsset(mcolor);

    break;
  }
  }
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void HMMWV_WheelLeft::ExportMeshPovray(const std::string& out_dir)
{
  utils::WriteMeshPovray(m_meshFile, m_meshName, out_dir, ChColor(0.15f, 0.15f, 0.15f));
}

void HMMWV_WheelRight::ExportMeshPovray(const std::string& out_dir)
{
  utils::WriteMeshPovray(m_meshFile, m_meshName, out_dir, ChColor(0.15f, 0.15f, 0.15f));
}

} // end namespace hmmwv


text:
AnnuityTouch
========

# Getting Started
--------------

```bash
$ npm install gulp
$ gulp
$ npm start
```
tips: only one port


text:
var slice = Array.prototype.slice;

function logger(namespace) {
  return function(message){
    console.log(namespace, slice.call(arguments,0).join(' '));
  }
}

module.exports = logger


text:
#ifndef _ASM_FCNTL_H
#define _ASM_FCNTL_H

#define O_DIRECTORY      040000	/* must be a directory */
#define O_NOFOLLOW      0100000	/* don't follow links */
#define O_LARGEFILE     0200000
#define O_DIRECT	0400000	/* direct disk access hint */

#include <asm-generic/fcntl.h>

#endif /* _ASM_FCNTL_H */


text:
angular.module('sampleApp')
    .directive('graphql', function () {
        return {
            restrict: 'EA', //E = element, A = attribute, C = class, M = comment
            scope: {
                //@ reads the attribute value, = provides two-way binding, & works with functions
                resource: '='
            },

            templateUrl: 'directive/graphql/graphql.html',
            controller: function($scope,$http,appConfigSvc){

                $scope.input = {};

                $scope.input.gql = " {name { text given family } gender birthDate }";

                $scope.$watch(
                    function() {return $scope.resource},
                    function() {
                        console.log($scope.resource)
                        if ($scope.resource) {
                            //assume this is a Patient

                            //$scope.serverBase= "http://test.fhir.org/r3/" + $scope.resource.resourceType + "/" + $scope.resource.id;
                            $scope.serverBase= appConfigSvc.getCurrentDataServer().url + $scope.resource.resourceType + "/" + $scope.resource.id;
                            $scope.serverBase += "/$graphql?query=";

                        }

                        delete $scope.gqlResult;
                        //delete $scope.input.gql;

                    }
                );

                $scope.executeGQL = function(path) {
                    delete $scope.gqlResult;
                    delete $scope.gqlError

                    var url = $scope.serverBase + $scope.input.gql;

                    $http.get(url).then(
                        function(data) {
                            //console.log(data.data)
                            $scope.gqlResult = data.data;
                        },
                        function (err){
                            $scope.gqlError = err;
                            console.log(err)
                        }
                    );

                }
            }
        }
    });


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SubtypeWithoutContracts {
  public class Subtype1 : BaseClassWithContracts.Base {
    public override int M(int x, bool behave) {
      if (behave) { return -x; }
      if (x == 1) throw new ApplicationException("haha");
      return 3;
    }

    public override int WithUserStrings(int x, bool behave)
    {
      if (behave) { return -x; }
      if (x == 2) throw new ApplicationException("haha");
      return 3;
    }
  }
}


text:
Nightwatch allows you to even define your own assertions, extending the available `.assert` and `.verify` namespaces.

Beginning with v0.4 assertions have a very simple interface which is shared between built-in assertions and custom ones. Therefore, custom assertions must implement the following interface:

exports.assertion = function() {

  /\*\*
   \* The message which will be used in the test output and
   \* inside the XML reports
   \* @type {string}
   \*/
  this.message;

  /\*\*
   \* A value to perform the assertion on. If a function is
   \* defined, its result will be used.
   \* @type {function|\*}
   \*/
  this.expected;

  /\*\*
   \* The method which performs the actual assertion. It is
   \* called with the result of the value method as the argument.
   \* @type {function}
   \*/
  this.pass = function(value) {

  };

  /\*\*
   \* The method which returns the value to be used on the
   \* assertion. It is called with the result of the command's
   \* callback as argument.
   \* @type {function}
   \*/
  this.value = function(result) {

  };

  /\*\*
   \* Performs a protocol command/action and its result is
   \* passed to the value method via the callback argument.
   \* @type {function}
   \*/
  this.command = function(callback) {

    return this;
  };

};

Both custom assertions and custom commands inherit from EventEmitter if defined in the form above.

If, however, your command/assertion is defined in the constructor style, then you must inherit manually, similarly to the pause command.

To see some examples, check the assertions module source-code on Github:
/nightwatch/tree/master/lib/selenium/assertions


text:
"""
Example generation for the scikit learn

Generate the rst files for the examples by iterating over the python
example files.

Files that generate images should start with 'plot'

"""
import os
import shutil
import traceback
import glob

import matplotlib
matplotlib.use('Agg')

import token, tokenize

rst_template = """

.. _example_%(short_fname)s:

%(docstring)s

**Python source code:** :download:`%(fname)s <%(fname)s>`

.. literalinclude:: %(fname)s
    :lines: %(end_row)s-
    """

plot_rst_template = """

.. _example_%(short_fname)s:

%(docstring)s

%(image_list)s

**Python source code:** :download:`%(fname)s <%(fname)s>`

.. literalinclude:: %(fname)s
    :lines: %(end_row)s-
    """

# The following strings are used when we have several pictures: we use
# an html div tag that our CSS uses to turn the lists into horizontal
# lists.
HLIST_HEADER = """
.. rst-class:: horizontal

"""

HLIST_IMAGE_TEMPLATE = """
    *

      .. image:: images/%s
            :scale: 50
"""

SINGLE_IMAGE = """
.. image:: images/%s
    :align: center
"""

def extract_docstring(filename):
    """ Extract a module-level docstring, if any
    """
    lines = file(filename).readlines()
    start_row = 0
    if lines[0].startswith('#!'):
        lines.pop(0)
        start_row = 1

    docstring = ''
    first_par = ''
    tokens = tokenize.generate_tokens(lines.__iter__().next)
    for tok_type, tok_content, _, (erow, _), _ in tokens:
        tok_type = token.tok_name[tok_type]
        if tok_type in ('NEWLINE', 'COMMENT', 'NL', 'INDENT', 'DEDENT'):
            continue
        elif tok_type == 'STRING':
            docstring = eval(tok_content)
            # If the docstring is formatted with several paragraphs, extract
            # the first one:
            paragraphs = '\n'.join(line.rstrip()
                                for line in docstring.split('\n')).split('\n\n')
            if len(paragraphs) > 0:
                first_par = paragraphs[0]
        break
    return docstring, first_par, erow+1+start_row

def generate_example_rst(app):
    """ Generate the list of examples, as well as the contents of
        examples.
    """
    root_dir = os.path.join(app.builder.srcdir, 'auto_examples')
    example_dir = os.path.abspath(app.builder.srcdir +  '/../' + 'examples')
    try:
        plot_gallery = eval(app.builder.config.plot_gallery)
    except TypeError:
        plot_gallery = bool(app.builder.config.plot_gallery)
    if not os.path.exists(example_dir):
        os.makedirs(example_dir)
    if not os.path.exists(root_dir):
        os.makedirs(root_dir)

    # we create an index.rst with all examples
    fhindex = file(os.path.join(root_dir, 'index.rst'), 'w')
    fhindex.write("""\

.. raw:: html

Examples
========

.. _examples-index:
""")
    # Here we don't use an os.walk, but we recurse only twice: flat is
    # better than nested.
    generate_dir_rst('.', fhindex, example_dir, root_dir, plot_gallery)
    for dir in sorted(os.listdir(example_dir)):
        if os.path.isdir(os.path.join(example_dir, dir)):
            generate_dir_rst(dir, fhindex, example_dir, root_dir, plot_gallery)
    fhindex.flush()

def generate_dir_rst(dir, fhindex, example_dir, root_dir, plot_gallery):
    """ Generate the rst file for an example directory.
    """
    if not dir == '.':
        target_dir = os.path.join(root_dir, dir)
        src_dir = os.path.join(example_dir, dir)
    else:
        target_dir = root_dir
        src_dir = example_dir
    if not os.path.exists(os.path.join(src_dir, 'README.txt')):
        print 80*'_'
        print ('Example directory %s does not have a README.txt file'
                        % src_dir)
        print 'Skipping this directory'
        print 80*'_'
        return
    fhindex.write("""

%s

""" % file(os.path.join(src_dir, 'README.txt')).read())
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    def sort_key(a):
        # put last elements without a plot
        if not a.startswith('plot') and a.endswith('.py'):
            return 'zz' + a
        return a
    for fname in sorted(os.listdir(src_dir), key=sort_key):
        if fname.endswith('py'):
            generate_file_rst(fname, target_dir, src_dir, plot_gallery)
            thumb = os.path.join(dir, 'images', 'thumb', fname[:-3] + '.png')
            link_name = os.path.join(dir, fname).replace(os.path.sep, '_')
            fhindex.write('.. figure:: %s\n' % thumb)
            if link_name.startswith('._'):
                link_name = link_name[2:]
            if dir != '.':
                fhindex.write('   :target: ./%s/%s.html\n\n' % (dir, fname[:-3]))
            else:
                fhindex.write('   :target: ./%s.html\n\n' % link_name[:-3])
            fhindex.write('   :ref:`example_%s`\n\n' % link_name)
    fhindex.write("""
.. raw:: html

    """) # clear at the end of the section

def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    base_image_name = os.path.splitext(fname)[0]
    image_fname = '%s_%%s.png' % base_image_name

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = ''
    else:
        last_dir += '_'
    short_fname = last_dir + fname
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, 'images')
    thumb_dir = os.path.join(image_dir, 'thumb')
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png')
    if plot_gallery and fname.startswith('plot'):
        # generate the plot as png image if file name
        # starts with plot and if it is more recent than an
        # existing image.
        first_image_file = image_path % 1

        if (not os.path.exists(first_image_file) or
                os.stat(first_image_file).st_mtime <=
                                    os.stat(src_file).st_mtime):
            # We need to execute the code
            print 'plotting %s' % fname
            import matplotlib.pyplot as plt
            plt.close('all')
            cwd = os.getcwd()
            try:
                # First CD in the original example dir, so that any file created
                # by the example get created in this directory
                os.chdir(os.path.dirname(src_file))
                execfile(os.path.basename(src_file), {'pl' : plt})
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                for fig_num in (fig_mngr.num for fig_mngr in
                          matplotlib._pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    plt.savefig(image_path % fig_num)
                    figure_list.append(image_fname % fig_num)
            except:
                print 80*'_'
                print '%s is not compiling:' % fname
                traceback.print_exc()
                print 80*'_'
            finally:
                os.chdir(cwd)
        else:
            figure_list = [f[len(image_dir):]
                            for f in glob.glob(image_path % '[1-9]')]
                            #for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image
        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)

    if not os.path.exists(thumb_file):
        # create something not to replace the thumbnail
        shutil.copy('images/blank_image.png', thumb_file)

    docstring, short_desc, end_row = extract_docstring(example_file)

    # Depending on whether we have one or more figures, we're using a
    # horizontal list or a single rst call to 'image'.
    if len(figure_list) == 1:
        figure_name = figure_list[0]
        image_list = SINGLE_IMAGE % figure_name.lstrip('/')
    else:
        image_list = HLIST_HEADER
        for figure_name in figure_list:
            image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/')

    f = open(os.path.join(target_dir, fname[:-2] + 'rst'),'w')
    f.write(this_template % locals())
    f.flush()

def setup(app):
    app.connect('builder-inited', generate_example_rst)
    app.add_config_value('plot_gallery', True, 'html')