예제 #1
0
def load(profile, log):
    if log:
        log_util.set_file_logger(log)
    else:
        log_util.set_std_logger()

    input_params = load_json(profile)

    if not 'mystic' in input_params:
        click.echo('Invalid profile')
    else:
        tohsaka = Tohsaka(input_params.pop('mystic'), input_params)
        tohsaka.go()
예제 #2
0
    def test_load_spell(self, __init__):
        tohsaka = Tohsaka('rest', {})
        tohsaka.config = {
            'spell': {
                'type': 'rest',
                'options': {
                    'endpoint': ''
                }
            }
        }
        tohsaka.spell = tohsaka._load_module('Spell', 'rest',
                                             tohsaka.SPELL_PATH, {})

        assert hasattr(tohsaka, 'spell')
        assert callable(tohsaka.spell.go)
예제 #3
0
    def test_currency(self):
        FILENAME = 'currency'
        tohsaka = Tohsaka(
            'currency', {
                'from': 'USD',
                'to': 'CNY',
                'output_file': FILENAME,
                'folder': tempfile.gettempdir()
            })

        tohsaka.go()

        with open(pathjoin(tohsaka.outputter.output_folder, FILENAME + '.csv'),
                  'r') as csvfile:
            assert csv.Sniffer().has_header(csvfile.read(1024))
예제 #4
0
    def test_load_outputter(self, __init__, outputter_type):
        tohsaka = Tohsaka('test', {})
        tohsaka.config = {
            'outputter': {
                'type': outputter_type,
                'options': {
                    'endpoint': ''
                }
            }
        }

        tohsaka.outputter = tohsaka._load_module('Outputter', 'test',
                                                 tohsaka.OUTPUTTER_PATH, {})

        assert hasattr(tohsaka, 'outputter')
        assert callable(tohsaka.outputter.go)
예제 #5
0
    def test_list_mystic(self):
        codes = Tohsaka.get_mystic_codes()

        for code in codes:
            for prop in ['name', 'description']:
                assert type(code[prop]) == str
                assert len(code[prop]) > 0
예제 #6
0
    def test_replace_params_override_default(self, __init__):
        tohsaka = Tohsaka('test', {})
        tohsaka.config = deepcopy({
            **self.BASE_PARAMS_DEF,
            **self.BASE_SPELL_CONFIG
        })

        base_params = self.BASE_PARAMS
        base_params['param4'] = 'param4'

        tohsaka._replace_params(tohsaka.config['spell']['options'],
                                base_params)

        options = tohsaka.config.get('spell').get('options')

        assert options.get('config5') == 'param4'
예제 #7
0
    def test_replace_params(self, __init__):
        tohsaka = Tohsaka('test', {})
        tohsaka.config = deepcopy({
            **self.BASE_PARAMS_DEF,
            **self.BASE_SPELL_CONFIG
        })

        tohsaka._replace_params(tohsaka.config['spell']['options'],
                                self.BASE_PARAMS)

        options = tohsaka.config.get('spell').get('options')

        assert options.get('config1') == 'no param'
        assert options.get('config2') == '1marap'
        assert options.get('config3') == '2marap123'
        assert options.get('config4') == '1marap4562marap'
        assert options.get('config5') == '4marap'
예제 #8
0
def list_mystic_codes():
    """List all the available Mystic Codes"""
    mystic_codes = Tohsaka.get_mystic_codes()

    click.echo(PRINT_FORMAT.format('Name', 'Description'))
    click.echo('-' * 80)
    for code in mystic_codes:
        click.echo(PRINT_FORMAT.format(code['name'], code['description']))
예제 #9
0
def run(mystic_code, log, save):
    if log:
        log_util.set_file_logger(log)
    else:
        log_util.set_std_logger()

    """Run a Mystic Code"""
    mystic_json = Tohsaka.load_mystic_code(mystic_code)
    params = mystic_json.get('params', {})
    input_params = {}

    # if there are params, start wizard
    # otherwise, run directly
    if params:
        for key, value in params.items():
            required = value.get('required')
            if required:
                name = '(*)' + key
            else:
                name = key

            description = value.get('description')
            if value.get('default'):
                description += f'. (Default: {value.get("default")})'

            result = input(PARAM_INPUT_FORMAT.format(name, description))

            if result:
                if value.get('type') == 'boolean':
                    input_params[key] = str(result).lower() in ['1', 'true', 't']
                else:
                    input_params[key]  = str(result)
            elif value.get('default'):
                input_params[key] = value.get('default')
        click.echo('\n')

    tohsaka = Tohsaka(mystic_code, input_params)
    tohsaka.go()

    if isinstance(save, str):
        click.echo(f'Saving the config to {save}...')
        click.echo('All set.\n')

        with open(save, 'w') as json_file:
            json_file.write(json.dumps(input_params, indent=4))
예제 #10
0
    def test_validate_params_invalid(self, __init__):
        tohsaka = Tohsaka('test', {})
        tohsaka.config = deepcopy({
            **self.BASE_PARAMS_DEF,
            **self.BASE_SPELL_CONFIG
        })

        with pytest.raises(Exception):
            tohsaka._validate_params({'param2': '2', 'param3': '3'})

        with pytest.raises(Exception):
            tohsaka._validate_params({'param2': '1'})

        with pytest.raises(Exception):
            tohsaka._validate_params({'key4': 0})
예제 #11
0
    def test_weather(self):
        FILENAME = 'vancouver'
        tohsaka = Tohsaka(
            'weather', {
                'appid': os.environ['OPENWEATHER_TOKEN'],
                'city': 'vancouver',
                'country': 'ca',
                'output_file': FILENAME,
                'folder': tempfile.gettempdir()
            })

        tohsaka.go()

        result = load_json(
            pathjoin(tohsaka.outputter.output_folder, FILENAME + '.json'))

        assert result
        assert 'city' in result[0]
        assert 'cnt' in result[0]
예제 #12
0
    def test_go_no_item(self, __init__, Qualifier):
        tohsaka = Tohsaka('test', {})
        tohsaka.config = None
        tohsaka.spell = MagicMock()
        tohsaka.spell.go.return_value = []
        tohsaka.outputter = MagicMock()

        tohsaka.go()

        Qualifier.assert_called_once()
        tohsaka.outputter.done.assert_called_once()
예제 #13
0
    def test_go_has_invalid_item(self, __init__, Qualifier):
        tohsaka = Tohsaka('test', {})
        tohsaka.config = None
        tohsaka.spell = MagicMock()
        tohsaka.spell.go.return_value = [1, 2, 3, 4, {}, 6, None, 8]
        tohsaka.outputter = MagicMock()

        tohsaka.go()

        Qualifier.assert_called_once()
        assert Qualifier.return_value.go.call_count == 6
        tohsaka.outputter.done.assert_called_once()
예제 #14
0
def show_mystic_code(mystic_code):
    """Show the details of the Mystic Code"""
    mystic_json = Tohsaka.load_mystic_code(mystic_code)
    params = mystic_json.get('params', {})

    click.echo(f'{mystic_json.get("name")} - {mystic_json.get("description")}')
    click.echo(f'Parameters ({len(params.keys())}):')
    for key, value in params.items():
        required = value.get('required')
        if required:
            name = '(*)' + key
        else:
            name = key

        description = value.get('description')
        if value.get('default'):
            description += f'. Default: value.get("default")'

        click.echo(PARAM_FORMAT.format(name, description))
예제 #15
0
    def test_validate_params(self, __init__):
        tohsaka = Tohsaka('test', {})
        tohsaka.config = deepcopy({
            **self.BASE_PARAMS_DEF,
            **self.BASE_SPELL_CONFIG
        })

        result1 = tohsaka._validate_params({
            'param1': '1',
            'param2': '2',
            'param3': '3'
        })

        result2 = tohsaka._validate_params({'param1': '1', 'param3': '3'})

        result3 = tohsaka._validate_params({
            'param1': False,
            'param2': '2',
            'param3': 0
        })

        assert (result1 & result2 & result3)