Exemplo n.º 1
0
def main():
    f = open("food_diary.json", 'r')
    try:
        data = json.load(f)
    except:
        data = {}

    c = open('calories.json', 'r')
    try:
        calories = json.load(c)
    except:
        calories = {}

    f.close()
    c.close()

    cal = Calories(calories)
    fd = FoodDiary(data, cal)
    cli = CLI(fd)

    cli.start()

    f = open("food_diary.json", 'w')
    json.dump(data, f, indent=4)
    f.close()

    c = open("calories.json", 'w')
    json.dump(calories, c, indent=4)
    c.close()
Exemplo n.º 2
0
def on_receive_ap_command():
    close_tcp_server()
    CLI.start_accesspoint_mode()

    time.sleep(60)

    sys.exit()
Exemplo n.º 3
0
class Config(object):
    
    def __repr__(self):
        return str(self.__config)
    
    def __init__(self, config_file="simple-db-migrate.conf"):
        self.__cli = CLI()
        self.__config = {}
        
        # read configurations
        try:
            f = codecs.open(config_file, "r", "utf-8")
            exec(f.read())
        except IOError:
            self.__cli.error_and_exit("%s: file not found" % config_file)
        else:
            f.close()
        
        try:
            self.put("db_host", HOST)
            self.put("db_user", USERNAME)
            self.put("db_password", PASSWORD)
            self.put("db_name", DATABASE)
            self.put("db_version_table", "__db_version__")
        
            migrations_dir = self.__get_migrations_absolute_dir(config_file, MIGRATIONS_DIR)
            self.put("migrations_dir", migrations_dir)
        except NameError, e:
            self.__cli.error_and_exit("config file error: " + str(e))
Exemplo n.º 4
0
Arquivo: hip.py Projeto: dlink/motiviz
class Hip(object):
    def __init__(self):
        self.sms = Sms()

    def run(self):
        commands = ["inspire"]
        self.cli = CLI(self.process, commands)
        print self.cli.process()

    def process(self, *args):
        args = list(args)

        cmd = args.pop(0)
        if cmd == "inspire":
            self.inspire()
        else:
            raise HipError("Unrecognized command: %s" % cmd)

    def inspire(self):
        inspiration = Inspiration()
        quote = inspiration.getNext()
        print quote["quote"].encode("ascii", "replace")
        for soul in Folks().getActive():
            phone = soul["phonenumber"]
            print phone
            self.sms.send(phone, quote["quote"])
        inspiration.setSentDate(quote["id"], datetime.now())
Exemplo n.º 5
0
def main():
    if len(sys.argv) < 2:
        print("Serial not specified")
        return

    path = sys.argv[1]
    if not Path(path).exists():
        print("Not found:", sys.argv[1])
        return

    cli = CLI(path)
    sensitive = get_sensitive(cli, 'acceleration')

    try:
        data = collect(cli)
        bias = [
            (data[Attitude.ROLL_LEFT] + data[Attitude.ROLL_RIGHT]) / 2,
            (data[Attitude.PITCH_DOWN] + data[Attitude.PITCH_UP]) / 2,
            (data[Attitude.FLAT] + data[Attitude.REVERSE]) / 2,
        ]
        gain = [
            sensitive * sensitive / (data[Attitude.ROLL_RIGHT] - bias[0]),
            sensitive * sensitive / (data[Attitude.PITCH_UP] - bias[1]),
            sensitive * sensitive / (data[Attitude.REVERSE] - bias[2]),
        ]
        print("bias:", bias)
        print("gain:", gain)
    except EOFError:
        pass

    cli.close()
Exemplo n.º 6
0
    def _drop_database(self):
        sql = """\
            SELECT 'DROP PUBLIC SYNONYM ' || SYNONYM_NAME ||';' FROM ALL_SYNONYMS \
            WHERE OWNER = 'PUBLIC' AND TABLE_OWNER = '%s' \
            UNION ALL \
            SELECT 'DROP SYNONYM ' || SYNONYM_NAME ||';' FROM ALL_SYNONYMS \
            WHERE OWNER = '%s' AND TABLE_OWNER = '%s' \
            UNION ALL \
            SELECT 'DROP ' || OBJECT_TYPE || ' ' || OBJECT_NAME ||';'   FROM USER_OBJECTS \
            WHERE OBJECT_TYPE <> 'TABLE' AND OBJECT_TYPE <> 'INDEX' AND \
            OBJECT_TYPE<>'TRIGGER'  AND OBJECT_TYPE<>'LOB' \
            UNION ALL \
            SELECT 'DROP ' || OBJECT_TYPE || ' ' || OBJECT_NAME ||' CASCADE CONSTRAINTS;'   FROM USER_OBJECTS \
            WHERE OBJECT_TYPE = 'TABLE' AND OBJECT_NAME NOT LIKE 'BIN$%%'""" % (self.__user.upper(), self.__user.upper(), self.__user.upper())

        conn = self.__connect()
        cursor = conn.cursor()
        try:
            cursor.execute(sql)
            rows = cursor.fetchall()

            failed_sqls = ''
            for row in rows:
                drop_sql = row[0]
                try:
                    self.__execute(drop_sql)
                except Exception, e:
                    failed_sqls = failed_sqls + "can't execute drop command '%s' in database '%s', %s\n" % (drop_sql, self.__db, str(e).strip())

            if failed_sqls != '':
                CLI.msg('\nThe following drop commands failed:\n%s' % (failed_sqls), "RED")
                CLI.msg('\nDo you want to continue anyway (y/N):', "END")
                to_continue = self.std_in.readline().strip()
                if to_continue.upper() != 'Y':
                    raise Exception("can't drop database objects for user '%s'" % (self.__user) )
Exemplo n.º 7
0
class MySQL(object):
    
    def __init__(self, config=None, mysql_driver=MySQLdb):
        self.__cli = CLI()
        self.__mysql_driver = mysql_driver
        self.__mysql_host = config.get("db_host")
        self.__mysql_user = config.get("db_user")
        self.__mysql_passwd = config.get("db_password")
        self.__mysql_db = config.get("db_name")
        self.__version_table = config.get("db_version_table")

        if config.get("drop_db_first"):
            self._drop_database()
            
        self._create_database_if_not_exists()
        self._create_version_table_if_not_exists()

    def __mysql_connect(self, connect_using_db_name=True):
        try:
            conn = self.__mysql_driver.connect(host=self.__mysql_host, user=self.__mysql_user, passwd=self.__mysql_passwd)
            
            # this should be configured in the config file, not hardcoded
            conn.set_character_set('utf8')
            
            if connect_using_db_name:
                conn.select_db(self.__mysql_db)
            return conn
        except Exception, e:
            self.__cli.error_and_exit("could not connect to database (%s)" % e)
Exemplo n.º 8
0
    def _drop_database(self):
        sql = """\
            SELECT 'DROP PUBLIC SYNONYM ' || SYNONYM_NAME ||';' FROM ALL_SYNONYMS \
            WHERE OWNER = 'PUBLIC' AND TABLE_OWNER = '%s' \
            UNION ALL \
            SELECT 'DROP SYNONYM ' || SYNONYM_NAME ||';' FROM ALL_SYNONYMS \
            WHERE OWNER = '%s' AND TABLE_OWNER = '%s' \
            UNION ALL \
            SELECT 'DROP ' || OBJECT_TYPE || ' ' || OBJECT_NAME ||';'   FROM USER_OBJECTS \
            WHERE OBJECT_TYPE <> 'TABLE' AND OBJECT_TYPE <> 'INDEX' AND \
            OBJECT_TYPE<>'TRIGGER'  AND OBJECT_TYPE<>'LOB' \
            UNION ALL \
            SELECT 'DROP ' || OBJECT_TYPE || ' ' || OBJECT_NAME ||' CASCADE CONSTRAINTS;'   FROM USER_OBJECTS \
            WHERE OBJECT_TYPE = 'TABLE' AND OBJECT_NAME NOT LIKE 'BIN$%%'""" % (self.__user.upper(), self.__user.upper(), self.__user.upper())

        conn = self.__connect()
        cursor = conn.cursor()
        try:
            cursor.execute(sql)
            rows = cursor.fetchall()

            failed_sqls = ''
            for row in rows:
                drop_sql = row[0]
                try:
                    self.__execute(drop_sql)
                except Exception, e:
                    failed_sqls = failed_sqls + "can't execute drop command '%s' in database '%s', %s\n" % (drop_sql, self.__db, str(e).strip())

            if failed_sqls != '':
                CLI.msg('\nThe following drop commands failed:\n%s' % (failed_sqls), "RED")
                CLI.msg('\nDo you want to continue anyway (y/N):', "END")
                to_continue = self.std_in.readline().strip()
                if to_continue.upper() != 'Y':
                    raise Exception("can't drop database '%s'" % (self.__db) )
Exemplo n.º 9
0
def check_network_status():
    print('check_network_status')

    ip = CLI.get_ip()
    if ip is None:
        CLI.start_accesspoint_mode()
        time.sleep(60)
Exemplo n.º 10
0
 def _verify_if_exception_is_invalid_user(self, exception):
     import pdb
     pdb.set_trace()
     #TODO validar como isso funciona no mongodb
     if 'ORA-01017' in exception.__str__():
         try:
             cli = CLI()
             cli.msg(
                 '\nPlease inform dba user/password to connect to database "%s"\nUser:'
                 % (self.__host), "END")
             dba_user = self.std_in.readline().strip()
             passwd = self.get_pass()
             conn = self.__driver.connect(dsn=self.__host,
                                          user=dba_user,
                                          password=passwd)
             cursor = conn.cursor()
             cursor.execute("create user %s identified by %s" %
                            (self.__user, self.__passwd))
             cursor.execute("grant connect, resource to %s" % (self.__user))
             cursor.execute("grant create public synonym to %s" %
                            (self.__user))
             cursor.execute("grant drop public synonym to %s" %
                            (self.__user))
             cursor.close()
             conn.close()
         except Exception, e:
             raise Exception("check error: %s" % e)
Exemplo n.º 11
0
Arquivo: game.py Projeto: dlink/chess
class GameCLI(object):
    '''Game Command Line Interface'''
    def run(self):
        '''Setup ommands and options. Launch CLI process'''

        commands = ['<white:h> <black:c> <board:standard> <game_history:None>']
        self.cli = CLI(self.process, commands)
        self.cli.process()

    def process(self, *args):
        '''Process all Incoming Requests'''

        white = 'h'
        black = 'c'
        board = 'standard'
        game_history = None

        white = args[0]
        if len(args) > 1:
            black = args[1]
        if len(args) > 2:
            board = args[2]
        if len(args) > 3:
            game_history = args[3]

        game = Game(white, black, board, game_history)
        game.play()
Exemplo n.º 12
0
class MSSQL(object):

    def __init__(self, config=None, mssql_driver=_mssql):
        self.__cli = CLI()
        self.__mssql_driver = mssql_driver
        self.__mssql_host = config.get("db_host")
        self.__mssql_user = config.get("db_user")
        self.__mssql_passwd = config.get("db_password")
        self.__mssql_db = config.get("db_name")
        self.__version_table = config.get("db_version_table")
               
        if config.get("drop_db_first"):
            self._drop_database()

        self._create_database_if_not_exists()
        self._create_version_table_if_not_exists()

    def __mssql_connect(self, connect_using_db_name=True):
        try:
            conn = self.__mssql_driver.connect(server=self.__mssql_host, user=self.__mssql_user, password=self.__mssql_passwd)
            if connect_using_db_name:
                conn.select_db(self.__mssql_db)
            return conn
        except Exception, e:
            self.__cli.error_and_exit("could not connect to database (%s)" % e)
Exemplo n.º 13
0
def on_receive_connect_to_ssid(ssid_name, ssid_password):
    close_tcp_server()
    CLI.join_into_wifi(ssid_name, ssid_password)

    print 'Wait 90s'
    time.sleep(90)

    sys.exit()
Exemplo n.º 14
0
    def testInvalidCommand(self):

        cli = CLI()

        # Check for incorrect command
        with self.assertRaises(ClindException):

            cli.processCommand("not a command")
Exemplo n.º 15
0
Arquivo: main.py Projeto: Starbat/tocc
def main(args):
    if len(args) > 0:
        cli = CLI()
        cli.run()
    else:
        appctxt = AppContext()  # 1. Instantiate ApplicationContext
        exit_code = appctxt.run()  # 2. Invoke appctxt.app.exec_()
        sys.exit(exit_code)
Exemplo n.º 16
0
    def testName(self):

        cli = CLI()

        name = cli.processCommand("name")

        # Check that name command returns a string with space
        self.assertTrue(isinstance(name, str))
        self.assertTrue(" " in name)
Exemplo n.º 17
0
    def _migrate(self):
        """ Execute migrations based on git tags """
        source = 'git'
        current_ontology = None
        current_version, origen = self.virtuoso.get_current_version()
        # Making the first migration to the database
        if current_version is None:
            if self.config.get("file_migration", None) is not None:
                self._execution_log(("- Current version is: %s" %
                                                        current_version),
                                    "GREEN",
                                    log_level_limit=1)
                self._execution_log(("- Destination version is: %s" %
                                        self.config.get("file_migration")),
                                    "GREEN",
                                    log_level_limit=1)
                CLI.error_and_exit("Can't execute migration FROM None TO File "
                                   "(TIP: version it using git --tag and then "
                                   "use -m)")
        else:
            if origen == "file":
                if self.config.get("file_migration", None) is not None:
                    self._execution_log(("- Current version is: %s" %
                                                            current_version),
                                        "GREEN",
                                        log_level_limit=1)
                    self._execution_log(("- Destination version is: %s" %
                                            self.config.get("file_migration")),
                                        "GREEN",
                                        log_level_limit=1)
                    CLI.error_and_exit("Can't execute migration FROM File TO "
                                       "File (TIP: version it using git --tag "
                                       "and then use -m)")

            current_ontology = self.virtuoso.get_ontology_by_version(
                                                            current_version)

        if self.config.get("file_migration", None) is not None:
            source = 'file'
            destination_version = self.config.get("file_migration")
            destination_ontology = self.virtuoso.get_ontology_from_file(
                                                        destination_version)
        else:
            destination_version = self._get_destination_version()
            destination_ontology = self.virtuoso.get_ontology_by_version(
                                                        destination_version)

        sparql_up, sparql_down = self.virtuoso.get_sparql(current_ontology,
                                                          destination_ontology,
                                                          current_version,
                                                          destination_version,
                                                          source)

        self._execute_migrations(sparql_up,
                                 sparql_down,
                                 current_version,
                                 destination_version)
Exemplo n.º 18
0
    def testRoll(self):

        cli = CLI()

        # Check roll command
        for i in range(100):

            r = cli.processCommand("roll 1d20")

            self.assertTrue(1 <= r <= 20)
Exemplo n.º 19
0
 def test_parser(self):
     """Tests the correct behaviour of the parser method: an args string without a required input is
     passed to the method and the test checks if an exception is raised"""
     data_handler = DataHandler('')
     method_handler = MethodHandler(data_handler, '')
     cli_app = CLI(method_handler)
     args_exp = {'Ticker': {'Type': 'String', 'Required': 1}, 'StartDate': {'Type': 'Date', 'Required': 1},
                 'EndDate': {'Type': 'Date', 'Required': 1}, 'Currency': {'Type': 'String', 'Required': 1}}
     args_obtained = 'AAPL 2021/01/01 2021/02/14'
     with self.assertRaises(Exception):
         res = cli_app.parse_args(args_obtained, args_exp)
Exemplo n.º 20
0
def main():
    if len(sys.argv) < 2:
        print("Serial not specified")
        return

    path = sys.argv[1]
    if not Path(path).exists():
        print("Not found:", sys.argv[1])
        return

    cli = CLI(path)
    print(cli.tx('telemetry'))
    cli.close()
Exemplo n.º 21
0
def main():
    # resolving the '-' issue
    readline.set_completer_delims(readline.get_completer_delims().replace('-', ''))

    cli = CLI()

    if len(sys.argv) > 1:
        try:
            cli.onecmd(' '.join(sys.argv[1:]))
        except BadResponseError as e:
            print(str(e))
    else:
        prompt_for_credentials()
        cli.cmdloop()
Exemplo n.º 22
0
    def __init__(self):

        self.udp_ip = cfg.UdpServer['ip']
        self.udp_port = cfg.UdpServer['port']
        self.flask_ip = cfg.FlaskServer['ip']
        self.flask_port = cfg.FlaskServer['port']
        self.username = cfg.Application['name']
        self.dir = cfg.Application['dir']
        self.bs_ip = cfg.BoostrapServer['ip']
        self.bs_port = cfg.BoostrapServer['port']

        self.routing_table = RoutingTable()
        self.cli = CLI()
        self.udp_server = UDPServer(self.udp_ip, self.udp_port)
        self.rest_server = RESTServer(self.flask_ip, self.flask_port)
Exemplo n.º 23
0
 def _verify_if_exception_is_invalid_user(self, exception):
     if 'ORA-01017' in exception.__str__():
         CLI.msg('\nPlease inform dba user/password to connect to database "%s"\nUser:' % (self.__host), "END")
         dba_user = self.std_in.readline().strip()
         passwd = self.get_pass()
         conn = self.__driver.connect(dsn=self.__host, user=dba_user, password=passwd)
         cursor = conn.cursor()
         try:
             cursor.execute("create user %s identified by %s" % (self.__user, self.__passwd))
             cursor.execute("grant connect, resource to %s" % (self.__user))
             cursor.execute("grant create public synonym to %s" % (self.__user))
             cursor.execute("grant drop public synonym to %s" % (self.__user))
         except Exception, e:
             raise Exception("check error: %s" % e)
         finally:
Exemplo n.º 24
0
 def _verify_if_exception_is_invalid_user(self, exception):
     if 'ORA-01017' in exception.__str__():
         CLI.msg('\nPlease inform dba user/password to connect to database "%s"\nUser:' % (self.__host), "END")
         dba_user = self.std_in.readline().strip()
         passwd = self.get_pass()
         conn = self.__driver.connect(dsn=self.__host, user=dba_user, password=passwd)
         cursor = conn.cursor()
         try:
             cursor.execute("create user %s identified by %s" % (self.__user, self.__passwd))
             cursor.execute("grant connect, resource to %s" % (self.__user))
             cursor.execute("grant create public synonym to %s" % (self.__user))
             cursor.execute("grant drop public synonym to %s" % (self.__user))
         except Exception, e:
             raise Exception("check error: %s" % e)
         finally:
Exemplo n.º 25
0
    def __init__(self, config, sgdb=None):
        Main._check_configuration(config)

        self.cli = CLI()
        self.config = config
        self.log = LOG(self.config.get("log_dir", None))

        self.sgdb = sgdb
        if self.sgdb is None and not self.config.get("new_migration", None):
            if self.config.get("database_engine") == 'mysql':
                from mysql import MySQL
                self.sgdb = MySQL(config)
            elif self.config.get("database_engine") == 'oracle':
                from oracle import Oracle
                self.sgdb = Oracle(config)
            elif self.config.get("database_engine") == 'mssql':
                from mssql import MSSQL
                self.sgdb = MSSQL(config)
            elif self.config.get("database_engine") == 'postgresql':
                from postgresql import PostgreSQL
                self.sgdb = PostgreSQL(config)
            else:
                raise Exception("engine not supported '%s'" % self.config.get("database_engine"))

        self.db_migrate = SimpleDBMigrate(self.config)
Exemplo n.º 26
0
    def test_clone(self):
        src_url = "/origin/url"
        CLI().main(['progname', 'clone', src_url])

        self.mock_clone_repo.assert_called_once_with(src=src_url,
                                                     dest=self.cwd)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 27
0
def main():
    """
    The main entry point of the program.
    """
    cli = CLI(api_key_file=API_KEY_FILE)
    history = InMemoryHistory()

    # Build Autocomplete
    methods = cli.client.context.allowed_api_calls
    autocomplete = build_autocomplete(cli.client)

    while True:
        try:
            text = prompt('Arsenal >> ',
                          completer=ArsenalCompleter(methods, autocomplete),
                          history=history,
                          auto_suggest=AutoSuggestFromHistory())
            if text:
                text = parse_command(cli.client, text)
                firethread = FireThread(text, cli)
                firethread.start()
                firethread.join()
                print('')
        except Reset:
            cli.client.context = cli.client.get_current_context()
            methods = cli.client.context.allowed_api_calls
            autocomplete = build_autocomplete(cli.client)
        except ShellExit:
            pass
        except EOFError:
            exit_arsenal()
        except KeyboardInterrupt:
            exit_arsenal()
        except Exception as exception:  # pylint: disable=broad-except
            print(exception)
Exemplo n.º 28
0
    def test_migrate(self):
        CLI().main(['progname', 'migrate'])

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_migrate_repo.assert_called_once_with(repo)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 29
0
    def test_list_default(self):
        CLI().main(['progname', 'list'])

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_list_pics.assert_called_once_with(repo, 'all')
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 30
0
    def __init__(self,
                 board_size=8,
                 player1_type=HumanPlayer,
                 player2_type=HumanPlayer):
        self.console = CLI()
        self.board = Board(board_size)
        self.manager = Manager()

        self.p1 = player1_type(Disk.DARK)
        if issubclass(player1_type, HumanPlayer):
            self.p1.set_console(self.console)
        if issubclass(player1_type, SimplePlayer):
            self.p1.set_manager(self.manager)
        if issubclass(player1_type, Player02):
            self.p1.set_minimax_variables(self.manager)

        self.p2 = player2_type(Disk.LIGHT)
        if issubclass(player2_type, HumanPlayer):
            self.p2.set_console(self.console)
        if issubclass(player2_type, SimplePlayer) or issubclass(
                player2_type, Player02):
            self.p2.set_manager(self.manager)
        if issubclass(player2_type, Player02):
            self.p2.set_minimax_variables(self.manager)

        self.current_player = self.p1
Exemplo n.º 31
0
    def test_default_viewer(self):
        CLI().main(['progname', 'view'])

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_view_pics.assert_called_once_with(repo, None)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 32
0
    def __init__(self, config=None, sgdb=None, db_migrate=None, execution_log=None):
        self.cli = CLI()
        self.config = config or {}
        self.log = LOG(self.config.get("log_dir", None))

        self.sgdb = sgdb
        if self.sgdb is None and not self.config.get("new_migration", None):
            if self.config.get("db_engine") is 'mysql':
                from mysql import MySQL
                self.sgdb = MySQL(config)
            elif self.config.get("db_engine") is 'oracle':
                from oracle import Oracle
                self.sgdb = Oracle(config)

        self.db_migrate = db_migrate or SimpleDBMigrate(config)
        if execution_log:
            self.execution_log = execution_log
Exemplo n.º 33
0
    def test_backup_to_many(self):
        backup_urls = ['/backup1/url', '/backup2/url', '/backup3/url']
        CLI().main(['progname', 'backup'] + backup_urls)

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_backup_repo.assert_called_once_with(repo, *backup_urls)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 34
0
    def test_backup(self):
        backup_url = '/backup/url'
        CLI().main(['progname', 'backup', backup_url])

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_backup_repo.assert_called_once_with(repo, backup_url)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 35
0
    def test_merge(self):
        others = ['repoA', 'repoB', 'repoC']
        CLI().main(['progname', 'merge'] + others)

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_merge_repos.assert_called_once_with(repo, *others)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 36
0
    def test_add_without_processing(self):
        files = ['file1', 'file2', 'file3']
        CLI().main(['progname', 'add'] + files + ['--noprocess'])

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_add_pics.assert_called_once_with(repo, files, False, None)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 37
0
    def test_check(self):
        self.mock_check_pics.return_value = ([], [])
        CLI().main(['progname', 'check'])

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_check_pics.assert_called_once_with(repo)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 38
0
    def setUp(self):
        self.mock_sys_exit = self.create_patch("sys.exit")

        self.origcwd = os.getcwd()
        self.tempdir = create_temp_dir()
        os.chdir(self.tempdir)

        self.cli = CLI()
Exemplo n.º 39
0
def main():
    f = open("food_diary.json", 'r')
    try:
        data = json.load(f)
    except:
        data = {}

    f.close()

    fd = FoodDiary(data)
    cli = CLI(fd)

    cli.start()

    f = open("food_diary.json", 'w')
    json.dump(data, f, indent=4)
    f.close()
Exemplo n.º 40
0
    def test_add(self):
        files = ['file1', 'file2', 'file3']
        CLI().main(['progname', 'add'] + files)

        self.mock_load_repo.assert_called_once_with(self.cwd)
        repo = self.mock_load_repo.return_value
        self.mock_add_pics.assert_called_once_with(repo, files, True, None)
        self.mock_sys_exit.assert_called_once_with(0)
Exemplo n.º 41
0
def manual_test():
    """ Manual test for the CLI if needed. """
    to_cli_q = queue.Queue()
    from_cli_q = queue.Queue()
    cli = CLI(to_cli_q, from_cli_q)
    cli.start()
    log.info('CLI running state: %r', cli.running)

    time.sleep(10)

    log.info('CLI running state: %r', cli.running)
    cli.stop()

    try:
        while True:
            log.info('Got CLI input: %r', from_cli_q.get(timeout=0.1))
    except queue.Empty:
        pass
Exemplo n.º 42
0
def run():
    cli = CLI()
    try:
        (options, args) = cli.parse()

        if options.torneira_version:
            msg = "torneira v%s" % torneira.__version__
            cli.info_and_exit(msg)

        if options.show_colors:
            CLI.show_colors()

        Main().excecute()

    except KeyboardInterrupt:
        cli.info_and_exit("\nExecution interrupted by user...")
    except Exception, e:
        cli.error_and_exit(str(e))
Exemplo n.º 43
0
    def __init__(self, config=None, mysql=None, db_migrate=None):
        self.cli = CLI()
        self.config = config or {}

        self.mysql = mysql
        if self.mysql is None and not self.config.get("new_migration"):
            self.mysql = MySQL(config)

        self.db_migrate = db_migrate or SimpleDBMigrate(config)
def main():
    interface = CLI()

    commands = (
        ("I", new_image),
        ("C", clear),
        ("L", colour_pixel),
        ("V", draw_vertical_segment),
        ("H", draw_horizontal_segment),
        ("F", fill_region),
        ("S", show),
        ("X", terminate),
    )

    for token, fn in commands:
        interface.register_command(token, fn)

    interface.main_loop()
Exemplo n.º 45
0
 def _verify_if_exception_is_invalid_user(self, exception):
     import pdb;pdb.set_trace()
     #TODO validar como isso funciona no mongodb
     if 'ORA-01017' in exception.__str__():
         try:
             cli = CLI()
             cli.msg('\nPlease inform dba user/password to connect to database "%s"\nUser:' % (self.__host), "END")
             dba_user = self.std_in.readline().strip()
             passwd = self.get_pass()
             conn = self.__driver.connect(dsn=self.__host, user=dba_user, password=passwd)
             cursor = conn.cursor()
             cursor.execute("create user %s identified by %s" % (self.__user, self.__passwd))
             cursor.execute("grant connect, resource to %s" % (self.__user))
             cursor.execute("grant create public synonym to %s" % (self.__user))
             cursor.execute("grant drop public synonym to %s" % (self.__user))
             cursor.close()
             conn.close()
         except Exception, e:
             raise Exception("check error: %s" % e)
Exemplo n.º 46
0
    def __init__(self, config=None, mysql=None, db_migrate=None):
        self.__cli = CLI()
        self.__config = config

        self.__mysql = mysql
        if self.__mysql is None and not self.__config.get("new_migration"):
            self.__mysql = MySQL(config)

        self.__db_migrate = db_migrate
        if self.__db_migrate is None:
            self.__db_migrate = Migrations(config)
Exemplo n.º 47
0
class End2EndTest(unittest.TestCase):
    def create_patch(self, name):
        patcher = mock.patch(name)
        thing = patcher.start()
        self.addCleanup(patcher.stop)
        return thing

    def setUp(self):
        self.mock_sys_exit = self.create_patch("sys.exit")

        self.origcwd = os.getcwd()
        self.tempdir = create_temp_dir()
        os.chdir(self.tempdir)

        self.cli = CLI()

    def tearDown(self):
        self.cli.shutdown(0)
        os.chdir(self.origcwd)
        print "removing %s" % self.tempdir
        shutil.rmtree(self.tempdir)
Exemplo n.º 48
0
class TestCLI(unittest.TestCase):
    """ Basic CLI functionality test cases. """

    def setUp(self):
        """ Prepare mock stdin and stdout, and a CLI instance to test. """
        # Create stdin and stdout files for testing.
        self.test_stdinout = MockStdInOut()

        # Create queues to pass messages to and get messages from the CLI.
        self.to_cli_q = queue.Queue()
        self.from_cli_q = queue.Queue()

        # Create a test CLI with the test stdin and stdout.
        self.test_cli = CLI(self.to_cli_q, self.from_cli_q,
                            stdin=self.test_stdinout,
                            stdout=self.test_stdinout)
        self.test_cli.start()

    def tearDown(self):
        """ Just stop the test CLI instance. """
        self.test_cli.stop()

    def test_input_and_output(self):
        """ Test basic input and output of the CLI. """
        # Test that the CLI input works first.
        test_input = "This is some test input"
        self.test_stdinout.to_send_data.append(test_input + '\n')
        self.assertEqual(self.from_cli_q.get(timeout=0.5), test_input)

        # Now check the output. Pass a message to be written out.
        test_output = 'This is some test output'
        self.to_cli_q.put(test_output)
        # Wait for the output to be written.
        time.sleep(0.1)
        self.assertEqual(self.test_stdinout.received_data.pop(0),
                         test_output + '\n')

        # Check the CLI is still running happily.
        self.assertTrue(self.test_cli.running)

        # Finally, check there is no unread data in the files.
        self.assertEqual(self.test_stdinout.received_data, [])
        self.assertEqual(self.test_stdinout.to_send_data, [])

    def test_ending_session(self):
        """ Test the user can end the CLI instance cleanly. """
        # Simulate sending an EOF.
        self.test_stdinout.to_send_data.append('')
        self.assertEqual(self.from_cli_q.get(timeout=0.5), None)

        # This stops the reading thread, but nt the whole CLI - stop the rest.
        self.test_cli.stop()

        # Check the CLI is now stopped.
        self.assertFalse(self.test_cli.running)

        # Finally, check there is no unread data in the files.
        self.assertEqual(self.test_stdinout.received_data, [])
        self.assertEqual(self.test_stdinout.to_send_data, [])
Exemplo n.º 49
0
    def __init__(self, config=None, mssql_driver=_mssql):
        self.__cli = CLI()
        self.__mssql_driver = mssql_driver
        self.__mssql_host = config.get("db_host")
        self.__mssql_user = config.get("db_user")
        self.__mssql_passwd = config.get("db_password")
        self.__mssql_db = config.get("db_name")
        self.__version_table = config.get("db_version_table")
               
        if config.get("drop_db_first"):
            self._drop_database()

        self._create_database_if_not_exists()
        self._create_version_table_if_not_exists()
Exemplo n.º 50
0
def run():
    try:
        (options, args) = CLI().parse()

        if options.simple_db_migrate_version:
            msg = "simple-db-migrate v%s" % SIMPLE_DB_MIGRATE_VERSION
            CLI().info_and_exit(msg)

        if options.show_colors:
            CLI.show_colors()

        # Create config
        config = FileConfig(options.config_file)
        config.put("schema_version", options.schema_version)
        config.put("show_sql", options.show_sql)
        config.put("show_sql_only", options.show_sql_only)
        config.put("new_migration", options.new_migration)
        config.put("drop_db_first", options.drop_db_first)

        # If CLI was correctly parsed, execute db-migrate.
        Main(config).execute()
    except Exception, e:
        CLI().error_and_exit(str(e))
Exemplo n.º 51
0
    def setUp(self):
        """ Prepare mock stdin and stdout, and a CLI instance to test. """
        # Create stdin and stdout files for testing.
        self.test_stdinout = MockStdInOut()

        # Create queues to pass messages to and get messages from the CLI.
        self.to_cli_q = queue.Queue()
        self.from_cli_q = queue.Queue()

        # Create a test CLI with the test stdin and stdout.
        self.test_cli = CLI(self.to_cli_q, self.from_cli_q,
                            stdin=self.test_stdinout,
                            stdout=self.test_stdinout)
        self.test_cli.start()
Exemplo n.º 52
0
    def __init__(self, config=None, mysql=None, db_migrate=None):
        self.cli = CLI()
        self.config = config or {}

        self.sgdb = mysql
        if self.sgdb is None and not self.config.get("new_migration"):
            if self.config.get("db_engine") is 'mysql':
                from mysql import MySQL
                self.sgdb = MySQL(config)
            elif self.config.get("db_engine") is 'oracle':
                from oracle import Oracle
                self.sgdb = Oracle(config)

        self.db_migrate = db_migrate or SimpleDBMigrate(config)
Exemplo n.º 53
0
class Main(object):
    def __init__(self):
        self.cli = CLI()

    def start(self, options, args):

        # set path
        sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(options.settings_file)), ".."))
        sys.path.insert(0, os.path.dirname(os.path.abspath(options.settings_file)))

        # set setting
        exec("import %s as settings" % os.path.splitext(os.path.basename(options.settings_file))[0])
        torneira.settings = settings

        from torneira.core.server import TorneiraServer

        server = TorneiraServer(
            pidfile=options.pidfile,
            port=options.port,
            media_dir=os.path.abspath(options.media_dir),
            xheaders=options.xheaders,
        )

        if options.daemon:
            if args[0] == "start":
                server.start()

            elif args[0] == "stop":
                server.stop()

            elif args[0] == "restart":
                server.restart()
        else:
            server.run()

    def excecute(self):

        (options, args) = self.cli.parse()

        if args and args[0] in ("start", "stop", "restart"):
            try:
                self.start(options, args)
            except Exception, e:
                traceback.print_exc(file=sys.stderr)
Exemplo n.º 54
0
def run():
    cli = CLI()
    (options, args) = cli.parse()

    if options.enable_colors:
        cli.enable_colors()

    try:
        main = Main(cli, options, args)
        if options.print_version:
            main.print_version()
        else:
            main.start()
    except KeyboardInterrupt:
        cli.print_info("\nExecution interrupted by user")
        sys.exit(2)

    sys.exit(0)
Exemplo n.º 55
0
def run(options):
    try:
        if options.get('simple_db_migrate_version'):
            msg = 'simple-db-migrate v%s' % SIMPLE_DB_MIGRATE_VERSION
            CLI.info_and_exit(msg)

        if options.get('show_colors'):
            CLI.show_colors()

        # Create config
        if options.get('config_file'):
            config = FileConfig(options.get('config_file'), options.get('environment'))
        else:
            config = Config()

        config.update('schema_version', options.get('schema_version'))
        config.update('show_sql', options.get('show_sql'))
        config.update('show_sql_only', options.get('show_sql_only'))
        config.update('new_migration', options.get('new_migration'))
        config.update('drop_db_first', options.get('drop_db_first'))
        config.update('paused_mode', options.get('paused_mode'))
        config.update('log_dir', options.get('log_dir'))
        config.update('label_version', options.get('label_version'))
        config.update('force_use_files_on_down', options.get('force_use_files_on_down'))
        config.update('force_execute_old_migrations_versions', options.get('force_execute_old_migrations_versions'))
        config.update('utc_timestamp', options.get('utc_timestamp'))
        config.update('database_user', options.get('database_user'))
        config.update('database_password', options.get('database_password'))
        config.update('database_host', options.get('database_host'))
        config.update('database_name', options.get('database_name'))
        if options.get('database_migrations_dir'):
            config.update("database_migrations_dir", Config._parse_migrations_dir(options.get('database_migrations_dir')))

        config.update('database_engine', options.get('database_engine'))
        if not config.get('database_engine', None):
            config.update('database_engine', "mysql")

        config.update('database_version_table', options.get('database_version_table'))
        if not config.get('database_version_table', None):
            config.update('database_version_table', "__db_version__")

        # paused mode forces log_level to 2
        log_level = int(options.get('log_level'))
        if options.get('paused_mode'):
            log_level = 2

        config.update('log_level', log_level)

        # Ask the password for user if configured
        if config.get('database_password') == '<<ask_me>>':
            if options.get('password'):
                passwd = options.get('password')
            else:
                CLI.msg('\nPlease inform password to connect to database "%s@%s:%s"' % (config.get('database_user'), config.get('database_host'), config.get('database_name')))
                passwd = getpass()
            config.update('database_password', passwd)

        # If CLI was correctly parsed, execute db-migrate.
        Main(config).execute()
    except KeyboardInterrupt:
        CLI.info_and_exit("\nExecution interrupted by user...")
    except Exception, e:
        CLI.error_and_exit(str(e))
Exemplo n.º 56
0
def run_from_argv(args=None):
    (options, _) = CLI.parse(args)
    run(options.__dict__)
Exemplo n.º 57
0
class Main(object):

    def __init__(self, config=None, mysql=None, db_migrate=None):
        self.cli = CLI()
        self.config = config or {}

        self.mysql = mysql
        if self.mysql is None and not self.config.get("new_migration"):
            self.mysql = MySQL(config)

        self.db_migrate = db_migrate or SimpleDBMigrate(config)

    def execute(self):
        self.cli.msg("\nStarting DB migration...", "PINK")
        if self.config.get("new_migration"):
            self.create_migration()
        else:
            self.migrate()
        self.cli.msg("\nDone.\n", "PINK")

    def create_migration(self):
        # TODO: create file in the migrations directory, not in current
        new_file = Migration.create(self.config.get("new_migration"))
        self.cli.msg("- Created file '%s'" % (new_file))

    def migrate(self):
        destination_version = self.get_destination_version()
        current_version = self.mysql.get_current_schema_version()

        self.cli.msg("- Current version is: %s" % current_version, "GREEN")
        self.cli.msg("- Destination version is: %s" % destination_version, "GREEN")

        # if current and destination versions are the same,
        # will consider a migration up to execute remaining files
        is_migration_up = True
        if int(current_version) > int(destination_version):
            is_migration_up = False

        # do it!
        self.execute_migrations(current_version, destination_version, is_migration_up)

    def get_destination_version(self):
        destination_version = self.config.get("schema_version")
        if destination_version is None:
            destination_version = self.db_migrate.latest_version_available()

        if destination_version is not '0' and not self.db_migrate.check_if_version_exists(destination_version):
            raise Exception("version not found (%s)" % destination_version)

        return destination_version

    def get_migration_files_to_be_executed(self, current_version, destination_version):
        mysql_versions = self.mysql.get_all_schema_versions()
        migration_versions = self.db_migrate.get_all_migration_versions()

        # migration up: the easy part
        if current_version <= destination_version:
            remaining_versions_to_execute = Lists.subtract(migration_versions, mysql_versions)
            remaining_versions_to_execute = [version for version in remaining_versions_to_execute if version <= destination_version]
            return remaining_versions_to_execute

        # migration down...
        down_versions = [version for version in mysql_versions if version <= current_version and version > destination_version]
        for version in down_versions:
            if version not in migration_versions:
                raise Exception("impossible to migrate down: one of the versions was not found (%s)" % version)
        down_versions.reverse()
        return down_versions

    def execute_migrations(self, current_version, destination_version, is_migration_up):
        # getting only the migration sql files to be executed
        versions_to_be_executed = self.get_migration_files_to_be_executed(current_version, destination_version)

        if versions_to_be_executed is None or len(versions_to_be_executed) == 0:
            self.cli.msg("\nNothing to do.\n", "PINK")
            return

        up_down_label = is_migration_up and "up" or "down"
        if self.config.get("show_sql_only"):
            self.cli.msg("\nWARNING: database migrations are not being executed ('--showsqlonly' activated)", "YELLOW")
        else:
            self.cli.msg("\nStarting migration %s!" % up_down_label)
        
        if self.config.get("log_level") >= 1:
            self.cli.msg("*** versions: %s\n" % versions_to_be_executed, "CYAN")

        sql_statements_executed = []
        for migration_version in versions_to_be_executed:
            migration = self.db_migrate.get_migration_from_version_number(migration_version)
            sql = is_migration_up and migration.sql_up or migration.sql_down

            if not self.config.get("show_sql_only"):
                if self.config.get("log_level") >= 1:
                    self.cli.msg("===== executing %s (%s) =====" % (migration.file_name, up_down_label))
                
                log = None
                if self.config.get("log_level") >= 2:
                    log = self.cli.msg
                
                self.mysql.change(sql, migration_version, is_migration_up, execution_log=log)
                
                # paused mode
                if self.config.get("paused_mode"):
                    raw_input("* press <enter> to continue... ")

            # recording the last statement executed
            sql_statements_executed.append(sql)

        if self.config.get("show_sql") or self.config.get("show_sql_only"):
            self.cli.msg("__________ SQL statements executed __________", "YELLOW")
            for sql in sql_statements_executed:
                self.cli.msg(sql, "YELLOW")
            self.cli.msg("_____________________________________________", "YELLOW")
Exemplo n.º 58
0
 def _execution_log(self, msg, color="CYAN", log_level_limit=2):
     if self.config.get("log_level", 1) >= log_level_limit:
         CLI.msg(msg, color)
     self.log.debug(msg)