Esempio n. 1
0
def test_add_python_folder_to_path_default():
    bashrc_text = config.read_file(os.path.expanduser('~/.bashrc'))
    result_list = config.add_python_folder_to_path(bashrc_text)
    b_update, result = result_list[0], result_list[1],

    result_lines = result.splitlines()

    b_anaconda_in_path = False

    for line in result_lines:
        if line.startswith("export"):
            words = line.strip().split()
            if "export" == words[0] and words[1].startswith('PATH='):
                # located PATH line
                bash_path = str(
                    subprocess.check_output([
                        config.get_bash_path(), '-c', f"{words[1]};echo $PATH"
                    ]), 'utf-8')
                unix_path_list = bash_path.split(':')

                for folder in unix_path_list:
                    if 'Anaconda3' in folder:
                        b_anaconda_in_path = True
                        break

    assert (b_anaconda_in_path) or (
        (not b_update) and config.can_bash_find_python()
    ), ("\n"
        f"b_anaconda_in_path = {b_anaconda_in_path}\n"
        f"not b_update = {not b_update}\n"
        f"config.can_bash_find_python() = {config.can_bash_find_python()}\n")
def color():
    # go = TranslateGoogle()
    baidu = TranslatorBaidu()
    colors_dict = {"颜色名称": [], "中文名称": [], "颜色RGB": [], "颜色16进制": []}

    def catch_color(body, color_dict):
        trs = body.find_all("tr")
        for tr in tqdm(trs):
            tds = tr.find_all("td")
            color_name = tds[1].text
            color_rgb = tds[2].text
            color_num = tds[3].text
            text_translate = baidu.request_translate(_from="en",
                                                     to="zh",
                                                     text=color_name)
            color_dict["颜色名称"].append(color_name)
            color_dict["中文名称"].append(text_translate)
            color_dict["颜色RGB"].append(color_rgb)
            color_dict["颜色16进制"].append(color_num)
        return color_dict

    str_html = read_file(r"C:\Users\Xiaoi\Desktop\demo.html")
    html_bs = BeautifulSoup(str_html, 'html.parser')
    bodys = html_bs.find_all("tbody")
    colors_dict = catch_color(bodys[2], colors_dict)
    colors_dict = catch_color(bodys[3], colors_dict)
    save_excel(model=colors_dict, path=r"C:\Users\Xiaoi\Desktop\color.xlsx")
def main(prog_name, argv):
    global config_file
    try:
        opts, _ = getopt.getopt(argv, 'hc:', ['config_file='])
    except getopt.GetoptError:
        print(prog_name + ' -c <config_file>')
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print(prog_name + ' -c <config_file>')
            sys.exit()
        elif opt in ('-c', '--config_file'):
            config_file = arg

    if not config.read_file(config_file):
        print('Error in read config file. Using default params.')

    thread = threading.Thread(target=run, args=(config.config,))
    thread.start()

    config_server.run()
    print('stopping...')
    global stop
    stop = True
    thread.join()
Esempio n. 4
0
    def rank_uri_basing_on_label(term, uri_pairs_list):
        scores = []
        uris = []
        for uri_pair in uri_pairs_list:
            term = ' '.join(re.sub('(?!^)([A-Z][a-z]+)', r' \1', term).split())

            score = compareString.phraseSimilarity(uri_pair['label'], term)
            # if score >= 0.6:
            #     print('\n=================================')
            #     print('==== score ====', score)
            #     print('==== term ====', term)
            #     print('==== label ====', uri_pair['label'])
            #     print('=================================\n')

            scores.append(score)

            uris.append(uri_pair['uri'])
        sorted_uris = [x for _, x in sorted(zip(scores, uris), reverse=True)]
        dbpedia_property_recitifier_dictionary = config.read_file(
            config.DBPEDIA_PROPERTY_RECTIFIER_DICTIONARY)
        if sorted_uris:
            if sorted_uris[0].strip(
            ) in dbpedia_property_recitifier_dictionary.keys():
                sorted_uris[0] = dbpedia_property_recitifier_dictionary[
                    sorted_uris[0]]
            return sorted_uris[0]
Esempio n. 5
0
def test_add_python_folder_to_path_default_new_path_value():
    bashrc_text = config.read_file(os.path.expanduser('~/.bashrc'))
    b_update, result, new_path_value = config.add_python_folder_to_path(
        bashrc_text)

    import_pylab_attempt = config.run_cmd_in_bash(' && '.join([
        f"export PATH={new_path_value}",
        'python -c "import pylab as py"',
    ]))

    assert 'error' not in import_pylab_attempt.lower(), {
        'import attempt response': import_pylab_attempt,
        'b_update': b_update,
        'bash_string': result,
    }
Esempio n. 6
0
def testfn():
    import config
    cfg = config.read_file("bitcoin.conf")

    rpcc = BitcoinRPCClient(
        rpcuser=cfg["rpcuser"],
        rpcpassword=cfg["rpcpassword"],
    )

    gevent.spawn(rpcc.run)

    while True:
        for i in xrange(10):
            rpcc.request("getblockchaininfo")
        print
        gevent.sleep(2)
        print
def authenticate():
    conf = config.read_file(CONFIG_FILE)
    # Get client ID and secret from auth.ini

    client = ImgurClient(conf.imgur.client_id, conf.imgur.client_secret)

    # Authorization flow, pin example (see docs for other auth types)
    authorization_url = client.get_auth_url('pin')

    print("Go to the following URL: {0}".format(authorization_url))

    # Read in the pin
    pin = input("Enter pin code: ")

    # ... redirect user to `authorization_url`, obtain pin (or code or token) ...
    credentials = client.authorize(pin, 'pin')
    client.set_user_auth(credentials['access_token'], credentials['refresh_token'])

    print("Authentication successful! Here are the details:")
    print("   Access token:  {0}".format(credentials['access_token']))
    print("   Refresh token: {0}".format(credentials['refresh_token']))
Esempio n. 8
0
def authenticate():
    conf = config.read_file(CONFIG_FILE)
    # Get client ID and secret from auth.ini

    client = ImgurClient(conf.imgur.client_id, conf.imgur.client_secret)

    # Authorization flow, pin example (see docs for other auth types)
    authorization_url = client.get_auth_url('pin')

    print("Go to the following URL: {0}".format(authorization_url))

    # Read in the pin
    pin = input("Enter pin code: ")

    # ... redirect user to `authorization_url`, obtain pin (or code or token) ...
    credentials = client.authorize(pin, 'pin')
    client.set_user_auth(credentials['access_token'],
                         credentials['refresh_token'])

    print("Authentication successful! Here are the details:")
    print("   Access token:  {0}".format(credentials['access_token']))
    print("   Refresh token: {0}".format(credentials['refresh_token']))
def main():
    # Determine console logging level from arguments
    console_level = logging.WARN
    if len(sys.argv) > 1:
        if sys.argv[1].isdigit():
            console_level = int(sys.argv[1])
        else:
            console_level = getattr(logging, sys.argv[1].upper(),
                                    console_level)
    # Read config and init logging
    conf = config.read_file(CONFIG_FILE)
    all_log_level = init_logging(conf=conf, console_level=console_level)
    l.info("config: {!s}", conf)

    # Verify other config
    if not verify_config(conf):
        return 2

    # Load user database
    user_db = UserDatabase(conf.storage.user_database or "users.json")

    # Start IRC bot
    irc_bot = IRCBot(host=conf.irc.host,
                     port=conf.irc.port or 6667,
                     nick=conf.irc.nick or "TelegramBot",
                     realname=conf.irc.nick,
                     password=conf.irc.password or None,
                     use_ssl=conf.irc.ssl or False)
    irc_bot.start()
    if not irc_bot.wait_connected(conf.irc.timeout or 7):
        l.critical("couldn't connect to IRC")
        irc_bot.stop()
        return 3
    l.info("connected to IRC")
    irc_bot.join(conf.irc.channel)

    # Start Telegram bot
    tg_bot = TelegramImageBot(conf, user_db, token=conf.telegram.token)
    l.info("Me: {}", tg_bot.update_bot_info().wait())

    # Register image callback as a closure
    def on_image(img):
        nonlocal conf, irc_bot, tg_bot, user_db
        thread = ImageHandler(conf=conf,
                              irc_bot=irc_bot,
                              tg_bot=tg_bot,
                              user_db=user_db,
                              img=img)
        thread.start()
        return thread

    tg_bot.on_image = on_image

    # Register image callback as a closure
    def on_auth(message):
        nonlocal conf, irc_bot, tg_bot, user_db
        thread = AuthHandler(conf=conf,
                             irc_bot=irc_bot,
                             tg_bot=tg_bot,
                             user_db=user_db,
                             message=message)
        thread.start()
        return thread

    tg_bot.on_auth = on_auth

    # Go through backlog and reschedule failed image uploads
    if conf.storage.database:
        with ImageDatabase(conf.storage.database) as db:
            backlog = db.get_unfinished_images()
        if backlog:
            l.info("Going through backlog, size: {}", len(backlog))
            for img in backlog:
                on_image(img).join()
            l.info("Finished backlog")

    # Main loop
    try:
        tg_bot.poll_loop()
    except KeyboardInterrupt:
        print("user interrupt...")
    except:
        l.exception()
    finally:
        logging.log(all_log_level, "shutting down")
        irc_bot.stop()
Esempio n. 10
0
if __name__ == '__main__':
    # initialise queues
    interface_queue = multiprocessing.Queue()
    rpc_queue = multiprocessing.Queue()

    # parse commandline arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-c", "--config",
                        help="path to config file [bitcoin.conf]",
                        default="bitcoin.conf")
    args = parser.parse_args()

    # parse config file
    try:
        cfg = config.read_file(args.config)
    except IOError:
        cfg = {}
        s = {'stop': "configuration file [" + args.config + "] does not exist or could not be read"}
        interface_queue.put(s)

    # initialise interrupt signal handler (^C)
    signal.signal(signal.SIGINT, interrupt_signal)

    # start RPC thread
    rpc_process = multiprocessing.Process(target=rpc.loop, args = (interface_queue, rpc_queue, cfg))
    rpc_process.daemon = True
    rpc_process.start()

    #debug(rpc_queue)
Esempio n. 11
0
def main():
    # Determine console logging level from arguments
    console_level = logging.WARN
    if len(sys.argv) > 1:
        if sys.argv[1].isdigit():
            console_level = int(sys.argv[1])
        else:
            console_level = getattr(logging, sys.argv[1].upper(), console_level)
    # Read config and init logging
    conf = config.read_file(CONFIG_FILE)
    all_log_level = init_logging(conf=conf, console_level=console_level)
    l.info("config: {!s}", conf)

    # Verify other config
    if not verify_config(conf):
        return 2

    # Load user database
    user_db = UserDatabase(conf.storage.user_database or "users.json")

    # Start IRC bot
    irc_bot = IRCBot(
        host=conf.irc.host,
        port=conf.irc.port or 6667,
        nick=conf.irc.nick or "TelegramBot",
        realname=conf.irc.nick,
        password=conf.irc.password or None,
        use_ssl=conf.irc.ssl or False
    )
    irc_bot.start()
    if not irc_bot.wait_connected(conf.irc.timeout or 7):
        l.critical("couldn't connect to IRC")
        irc_bot.stop()
        return 3
    l.info("connected to IRC")
    irc_bot.join(conf.irc.channel)

    # Start Telegram bot
    tg_bot = TelegramImageBot(conf, user_db, token=conf.telegram.token)
    l.info("Me: {}", tg_bot.update_bot_info().wait())

    # Register image callback as a closure
    def on_image(img):
        nonlocal conf, irc_bot, tg_bot, user_db
        thread = ImageHandler(
            conf=conf,
            irc_bot=irc_bot,
            tg_bot=tg_bot,
            user_db=user_db,
            img=img
        )
        thread.start()
        return thread

    tg_bot.on_image = on_image

    # Register image callback as a closure
    def on_auth(message):
        nonlocal conf, irc_bot, tg_bot, user_db
        thread = AuthHandler(
            conf=conf,
            irc_bot=irc_bot,
            tg_bot=tg_bot,
            user_db=user_db,
            message=message
        )
        thread.start()
        return thread

    tg_bot.on_auth = on_auth

    # Go through backlog and reschedule failed image uploads
    if conf.storage.database:
        with ImageDatabase(conf.storage.database) as db:
            backlog = db.get_unfinished_images()
        if backlog:
            l.info("Going through backlog, size: {}", len(backlog))
            for img in backlog:
                on_image(img).join()
            l.info("Finished backlog")

    # Main loop
    try:
        tg_bot.poll_loop()
    except KeyboardInterrupt:
        print("user interrupt...")
    except:
        l.exception()
    finally:
        logging.log(all_log_level, "shutting down")
        irc_bot.stop()
Esempio n. 12
0
def mainfn(window):
    # initialise queues
    response_queue = gevent.queue.Queue()
    rpc_queue = gevent.queue.Queue()

    # parse commandline arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-c", "--config",
                        help="path to config file [bitcoin.conf]",
                        default="bitcoin.conf")
    parser.add_argument("-m", "--mode",
                        help="initial mode",
                        default=None)
    args = parser.parse_args()

    # parse config file
    try:
        cfg = config.read_file(args.config)
    except IOError:
        cfg = {}
        s = {'stop': "configuration file [" + args.config + "] does not exist or could not be read"}
        response_queue.put(s)

    # initialise interrupt signal handler (^C)
    signal.signal(signal.SIGINT, interrupt_signal)

    bstore = block_store.BlockStore()
    bviewer = block_viewer.BlockViewer(bstore, window)

    bstore._on_block = bviewer.on_block

    # start RPC thread
    rpcc = rpc2.BitcoinRPCClient(
        response_queue=response_queue, # TODO: refactor this
        block_store=bstore,
        rpcuser=cfg["rpcuser"],
        rpcpassword=cfg["rpcpassword"],
        rpcip=(cfg["rpcip"] if "rpcip" in cfg else "localhost"),
        rpcport=(cfg["rpcport"] if "rpcport" in cfg else 18332 if "testnet" in cfg else 8332),
        protocol=(cfg["protocol"] if "protocol" in cfg else "http"),
    )
    connected = rpcc.connect()
    if not connected:
        print "RPCC failed to connect"
        sys.exit(1)

    rpc2_process = gevent.spawn(rpcc.run)

    poller = rpc2.Poller(rpcc)
    poller_process = gevent.spawn(poller.run)

    if args.mode is not None:
        initial_mode = args.mode
    elif "mode" in cfg:
        initial_mode = cfg["mode"]
    else:
        initial_mode = None

    # main loop
    try:
        interface.main(bviewer, window, response_queue, rpcc, poller, initial_mode)
    finally:
        rpcc.stop()
        rpc2_process.join()
Esempio n. 13
0
        cfg["rpcip"] = "localhost"

    if "rpcport" not in cfg:
        if "testnet" in cfg:
            cfg["rpcport"] = 18332
        else:
            cfg["rpcport"] = 8332

    if "protocol" not in cfg:
        cfg["protocol"] = "http"

    return cfg


if __name__ == "__main__":
    cfg = config.read_file("bitcoin.conf")
    cfg = process_config(cfg)

    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, "hashtx")
    socket.connect("tcp://127.0.0.1:10000")

    hash_queue = gevent.queue.Queue()
    zmq_process = gevent.spawn(zmqpoller, hash_queue)

    try:
        rpcrequester(hash_queue, cfg)
    finally:
        socket.close()
        context.term()
Esempio n. 14
0
def mainfn(window):
    # initialise queues
    response_queue = gevent.queue.Queue()
    rpc_queue = gevent.queue.Queue()

    # parse commandline arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-c",
                        "--config",
                        help="path to config file [bitcoin.conf]",
                        default=os.path.expanduser("~/.bitcoin/bitcoin.conf"))
    parser.add_argument("-m", "--mode", help="initial mode", default=None)
    args = parser.parse_args()

    # parse config file
    try:
        cfg = config.read_file(args.config)
    except IOError:
        cfg = {}
        return "configuration file [{}] does not exist or could not be read".format(
            args.config)

    # initialise interrupt signal handler (^C)
    signal.signal(signal.SIGINT, interrupt_signal)

    bstore = block_store.BlockStore()
    bviewer = block_viewer.BlockViewer(bstore, window)

    bstore._on_block = bviewer.on_block

    # start RPC thread
    rpcc = rpc2.BitcoinRPCClient(
        response_queue=response_queue,  # TODO: refactor this
        block_store=bstore,
        rpcuser=cfg["rpcuser"],
        rpcpassword=cfg["rpcpassword"],
        rpcip=(cfg["rpcip"] if "rpcip" in cfg else "localhost"),
        rpcport=(cfg["rpcport"]
                 if "rpcport" in cfg else 18332 if "testnet" in cfg else 8332),
        protocol=(cfg["protocol"] if "protocol" in cfg else "http"),
    )

    bstore._rpcc = rpcc

    connected = rpcc.connect()
    if not connected:
        return "RPCC failed to connect"

    rpc2_process = gevent.spawn(rpcc.run)

    poller = rpc2.Poller(rpcc)
    poller_process = gevent.spawn(poller.run)

    if args.mode is not None:
        initial_mode = args.mode
    elif "mode" in cfg:
        initial_mode = cfg["mode"]
    else:
        initial_mode = None

    # main loop
    try:
        interface.main(bviewer, window, response_queue, rpcc, poller,
                       initial_mode)
    finally:
        rpcc.stop()
        rpc2_process.join()
Esempio n. 15
0
from __future__ import print_function
from collections import OrderedDict
import subprocess
import argparse
import readline
import multiwii
import strconv
import config
import struct
import socket
import gzip
import time


config = __import__('configparser').ConfigParser()
config.read_file(open('/etc/magpi.ini', 'r'))


recv_buffer = 1048576


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--header-every', type=int,
        default=config['read_telemetry'].getint('header_every'),
        help='Broadcast data header every N broadcasts'
    )
    parser.add_argument(
        '-n', '--name',
        default=config['read_telemetry'].get('name'),
Esempio n. 16
0
 def __init__(self):
     # initiate the PreProcessor, load dictionaries for different purposes
     self.tagging_rectifier_dictionary = config.read_file(
         config.TAGGING_RECTIFIER_DICTIONARY)
     self.key_word_rectifier_dictionary = config.read_file(
         config.KEY_WORD_RECTIFIER_DICTIONARY)
Esempio n. 17
0
from __future__ import print_function
from collections import OrderedDict
import subprocess
import argparse
import readline
import multiwii
import strconv
import config
import struct
import socket
import gzip
import time

config = __import__('configparser').ConfigParser()
config.read_file(open('/etc/magpi.ini', 'r'))

recv_buffer = 1048576


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--header-every',
        type=int,
        default=config['read_telemetry'].getint('header_every'),
        help='Broadcast data header every N broadcasts')
    parser.add_argument('-n',
                        '--name',
                        default=config['read_telemetry'].get('name'),
                        help='Name/ID of quadcopter.')