Exemplo n.º 1
0
def test_keywords_not_in_PY2():
    """In Python 2 ("True", "False", "None") are not keywords. The isidentifier
    method ensures that those are treated as keywords on both Python 2 and 3.
    """
    assert not isidentifier("True")
    assert not isidentifier("False")
    assert not isidentifier("None")
Exemplo n.º 2
0
    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        result = super(ActionModule, self).run(tmp, task_vars)
        del tmp  # tmp no longer has any effect

        facts = dict()

        cacheable = boolean(self._task.args.pop('cacheable', False))

        if self._task.args:
            for (k, v) in iteritems(self._task.args):
                k = self._templar.template(k)

                if not isidentifier(k):
                    result['failed'] = True
                    result['msg'] = (
                        "The variable name '%s' is not valid. Variables must start with a letter or underscore character, and contain only "
                        "letters, numbers and underscores." % k)
                    return result

                if not C.DEFAULT_JINJA2_NATIVE and isinstance(
                        v, string_types) and v.lower() in ('true', 'false',
                                                           'yes', 'no'):
                    v = boolean(v, strict=False)
                facts[k] = v

        result['changed'] = False
        result['assible_facts'] = facts
        result['_assible_facts_cacheable'] = cacheable
        return result
Exemplo n.º 3
0
    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        result = super(ActionModule, self).run(tmp, task_vars)
        del tmp  # tmp no longer has any effect

        stats = {'data': {}, 'per_host': False, 'aggregate': True}

        if self._task.args:
            data = self._task.args.get('data', {})

            if not isinstance(data, dict):
                data = self._templar.template(data,
                                              convert_bare=False,
                                              fail_on_undefined=True)

            if not isinstance(data, dict):
                result['failed'] = True
                result[
                    'msg'] = "The 'data' option needs to be a dictionary/hash"
                return result

            # set boolean options, defaults are set above in stats init
            for opt in ['per_host', 'aggregate']:
                val = self._task.args.get(opt, None)
                if val is not None:
                    if not isinstance(val, bool):
                        stats[opt] = boolean(self._templar.template(val),
                                             strict=False)
                    else:
                        stats[opt] = val

            for (k, v) in iteritems(data):

                k = self._templar.template(k)

                if not isidentifier(k):
                    result['failed'] = True
                    result['msg'] = (
                        "The variable name '%s' is not valid. Variables must start with a letter or underscore character, and contain only "
                        "letters, numbers and underscores." % k)
                    return result

                stats['data'][k] = self._templar.template(v)

        result['changed'] = False
        result['assible_stats'] = stats

        return result
Exemplo n.º 4
0
def test_non_ascii():
    """In Python 3 non-ascii characters are allowed as opposed to Python 2. The
    isidentifier method ensures that those are treated as keywords on both
    Python 2 and 3.
    """
    assert not isidentifier("křížek")
Exemplo n.º 5
0
def test_invalid_identifier(identifier):
    assert not isidentifier(identifier)
Exemplo n.º 6
0
def test_valid_identifier(identifier):
    assert isidentifier(identifier)
Exemplo n.º 7
0
 def _validate_variable_keys(ds):
     for key in ds:
         if not isidentifier(key):
             raise TypeError("'%s' is not a valid variable name" % key)