Beispiel #1
0
    def _fill_in_contexts(self, context={}, strict=True):
        """Parses self._options and updates it with the supplied context.

        Parses the objects self._options dict (by converting it into a JSON
        string, substituting, and then turning it back into a dict) and the
        self._desc string and replaces any {KEY}s with the valoues from the
        context dict that was supplied.

        Args:
            strict: bool whether or not to allow missing context keys to be
                    skipped over.

        Raises:
            exceptions.InvalidOptions
        """
        # Inject contexts into Description
        try:
            self._desc = utils.populate_with_tokens(
                str(self),
                context,
                self.left_context_separator,
                self.right_context_separator,
                strict=strict)
        except LookupError as e:
            msg = 'Context for description failed: %s' % e
            raise exceptions.InvalidOptions(msg)

        # Inject contexts into condition
        try:
            self._condition = utils.populate_with_tokens(
                str(self._condition),
                context,
                self.left_context_separator,
                self.right_context_separator,
                strict=strict)
        except LookupError as e:
            msg = 'Context for condition failed: %s' % e
            raise exceptions.InvalidOptions(msg)

        # Convert our self._options dict into a string for fast parsing
        options_string = json.dumps(self._options)

        # Generate a new string with the values parsed out. At this point, if
        # any value is un-matched, an exception is raised and execution fails.
        # This stops execution during a dry run, before any live changes are
        # made.
        try:
            new_options_string = utils.populate_with_tokens(
                options_string,
                context,
                self.left_context_separator,
                self.right_context_separator,
                strict=strict)
        except LookupError as e:
            msg = 'Context for options failed: %s' % e
            raise exceptions.InvalidOptions(msg)

        # Finally, convert the string back into a dict and store it.
        self._options = json.loads(new_options_string)
Beispiel #2
0
    def _fill_in_contexts(self, context={}, strict=True):
        """Parses self._options and updates it with the supplied context.

        Parses the objects self._options dict (by converting it into a JSON
        string, substituting, and then turning it back into a dict) and the
        self._desc string and replaces any {KEY}s with the valoues from the
        context dict that was supplied.

        Args:
            strict: bool whether or not to allow missing context keys to be
                    skipped over.

        Raises:
            exceptions.InvalidOptions
        """
        # Inject contexts into Description
        try:
            self._desc = utils.populate_with_tokens(
                str(self),
                context,
                self.left_context_separator,
                self.right_context_separator,
                strict=strict)
        except LookupError as e:
            msg = 'Context for description failed: %s' % e
            raise exceptions.InvalidOptions(msg)

        # Inject contexts into condition
        try:
            self._condition = utils.populate_with_tokens(
                str(self._condition),
                context,
                self.left_context_separator,
                self.right_context_separator,
                strict=strict)
        except LookupError as e:
            msg = 'Context for condition failed: %s' % e
            raise exceptions.InvalidOptions(msg)

        # Convert our self._options dict into a string for fast parsing
        options_string = json.dumps(self._options)

        # Generate a new string with the values parsed out. At this point, if
        # any value is un-matched, an exception is raised and execution fails.
        # This stops execution during a dry run, before any live changes are
        # made.
        try:
            new_options_string = utils.populate_with_tokens(
                options_string,
                context,
                self.left_context_separator,
                self.right_context_separator,
                strict=strict)
        except LookupError as e:
            msg = 'Context for options failed: %s' % e
            raise exceptions.InvalidOptions(msg)

        # Finally, convert the string back into a dict and store it.
        self._options = json.loads(new_options_string)
Beispiel #3
0
    def _read_source(self):
        """Reads in a RightScript source file.

        Reads in the file contents, and swaps in any tokens that may have
        been left in the script.

        args:
            source: Path to the script

        returns:
            <string contents of the script>
        """
        source = self.option('source')

        try:
            fh = open(source)
            raw = fh.read()
        except IOError as e:
            raise exceptions.InvalidOptions('Error reading script %s: %s' %
                                            (source, e))

        try:
            parsed = utils.populate_with_tokens(raw, self._init_tokens)
        except LookupError as e:
            raise exceptions.InvalidOptions('Error parsing tokens in %s: %s' %
                                            (source, e))

        return parsed
Beispiel #4
0
    def _read_source(self):
        """Reads in a RightScript source file.

        Reads in the file contents, and swaps in any tokens that may have
        been left in the script.

        args:
            source: Path to the script

        returns:
            <string contents of the script>
        """
        source = self.option('source')

        try:
            fh = open(source)
            raw = fh.read()
        except IOError as e:
            raise exceptions.InvalidOptions('Error reading script %s: %s' %
                                            (source, e))

        try:
            parsed = utils.populate_with_tokens(raw, self._init_tokens)
        except LookupError as e:
            raise exceptions.InvalidOptions('Error parsing tokens in %s: %s' %
                                            (source, e))

        return parsed
Beispiel #5
0
 def test_populate_with_escape_non_strict_no_escape(self):
     tokens = {'UNIT_TEST': 'FOOBAR'}
     string = 'Unit \%UNIT_TEST\% Test'
     expect = 'Unit \%UNIT_TEST\% Test'
     result = utils.populate_with_tokens(string, tokens,
                                         strict=False,
                                         remove_escape_sequence=False)
     self.assertEquals(result, expect)
Beispiel #6
0
 def test_populate_with_not_strict(self):
     tokens = {'UNIT_TEST': 'FOOBAR'}
     string = 'Unit {UNIT_TEST} {FAIL} Test'
     expect = 'Unit FOOBAR {FAIL} Test'
     result = utils.populate_with_tokens(
         string, tokens,
         left_wrapper='{', right_wrapper='}',
         strict=False)
     self.assertEqual(result, expect)
Beispiel #7
0
 def test_populate_with_not_strict(self):
     tokens = {'UNIT_TEST': 'FOOBAR'}
     string = 'Unit {UNIT_TEST} {FAIL} Test'
     expect = 'Unit FOOBAR {FAIL} Test'
     result = utils.populate_with_tokens(
         string, tokens,
         left_wrapper='{', right_wrapper='}',
         strict=False)
     self.assertEquals(result, expect)
Beispiel #8
0
    def _build_actions(self):
        """Builds either a single set of actions, or multiple sets.

        If no 'contexts' were passed in, then we simply build the actors that
        are defined in the 'acts' option for the group.

        If any 'contexts' were passed in, then this method will create as many
        groups of actions as there are in the list of contexts. For each dict
        in the 'contexts' list, a new group of actors is created with that
        information.

        Note: Because groups may contain nested group actors, any options
        passed into this actors 'init_context' are also passed into the
        actors that we're intantiating.
        """
        contexts = self.option('contexts')
        if not contexts:
            return self._build_action_group(self._init_context)

        # If the data passed into the 'contexts' is a list of dicts, we take it
        # as is and do nothing to it.
        if type(contexts) == list:
            context_data = self.option('contexts')
        # DEPRECATE IN v0.5.0
        elif type(contexts) == dict:
            context_string = open(contexts['file']).read()
            context_string = kp_utils.populate_with_tokens(
                string=context_string,
                tokens=contexts.get('tokens', {}),
                strict=True)
            context_data = demjson.decode(context_string)
        # END DEPRECATION

        # If the data passed in is a string, it must be a pointer to a file
        # with contexts in it. We read that file, and we parse it for any
        # missing tokens. We use the "init tokens" that made it into this actor
        # as available token substitutions.
        elif isinstance(contexts, str):
            context_data = kp_utils.convert_script_to_dict(
                contexts, self._init_tokens)

        actions = []
        for context in context_data:
            combined_context = dict(
                list(self._init_context.items()) + list(context.items()))
            self.log.debug('Inherited context %s' %
                           list(self._init_context.items()))
            self.log.debug('Specified context %s' % list(context.items()))
            self.log.debug('Building acts with parameters: %s' %
                           combined_context)
            for action in self._build_action_group(context=combined_context):
                actions.append(action)

        return actions
Beispiel #9
0
    def _build_actions(self):
        """Builds either a single set of actions, or multiple sets.

        If no 'contexts' were passed in, then we simply build the actors that
        are defined in the 'acts' option for the group.

        If any 'contexts' were passed in, then this method will create as many
        groups of actions as there are in the list of contexts. For each dict
        in the 'contexts' list, a new group of actors is created with that
        information.

        Note: Because groups may contain nested group actors, any options
        passed into this actors 'init_context' are also passed into the
        actors that we're intantiating.
        """
        contexts = self.option('contexts')
        if not contexts:
            return self._build_action_group(self._init_context)

        # If the data passed into the 'contexts' is a list of dicts, we take it
        # as is and do nothing to it.
        if type(contexts) == list:
            context_data = self.option('contexts')
        # DEPRECATE IN v0.5.0
        elif type(contexts) == dict:
            context_string = open(contexts['file']).read()
            context_string = kp_utils.populate_with_tokens(
                string=context_string,
                tokens=contexts.get('tokens', {}),
                strict=True)
            context_data = demjson.decode(context_string)
        # END DEPRECATION

        # If the data passed in is a string, it must be a pointer to a file
        # with contexts in it. We read that file, and we parse it for any
        # missing tokens. We use the "init tokens" that made it into this actor
        # as available token substitutions.
        elif isinstance(contexts, basestring):
            context_data = kp_utils.convert_script_to_dict(
                contexts, self._init_tokens)

        actions = []
        for context in context_data:
            combined_context = dict(self._init_context.items() +
                                    context.items())
            self.log.debug('Inherited context %s' % self._init_context.items())
            self.log.debug('Specified context %s' % context.items())
            self.log.debug('Building acts with parameters: %s' %
                           combined_context)
            for action in self._build_action_group(context=combined_context):
                actions.append(action)

        return actions
Beispiel #10
0
    def _build_actions(self):
        """Builds either a single set of actions, or multiple sets.

        If no 'contexts' were passed in, then we simply build the actors that
        are defined in the 'acts' option for the group.

        If any 'contexts' were passed in, then this method will create as many
        groups of actions as there are in the list of contexts. For each dict
        in the 'contexts' list, a new group of actors is created with that
        information.

        Note: Because groups may contain nested group actors, any options
        passed into this actors 'init_context' are also passed into the
        actors that we're intantiating.
        """
        contexts = self.option('contexts')
        if not contexts:
            return self._build_action_group(self._init_context)

        if type(contexts) == list:
            context_data = self.option('contexts')
        elif type(contexts) == dict:
            context_string = open(contexts['file']).read()
            context_string = kp_utils.populate_with_tokens(
                string=context_string,
                tokens=contexts.get('tokens', {}),
                strict=True)
            context_data = demjson.decode(context_string)

        actions = []
        for context in context_data:
            combined_context = dict(self._init_context.items() +
                                    context.items())
            self.log.debug('Inherited context %s' % self._init_context.items())
            self.log.debug('Specified context %s' % context.items())
            self.log.debug('Building acts with parameters: %s' %
                           combined_context)
            for action in self._build_action_group(context=combined_context):
                actions.append(action)

        return actions
Beispiel #11
0
    def _build_actions(self):
        """Builds either a single set of actions, or multiple sets.

        If no 'contexts' were passed in, then we simply build the actors that
        are defined in the 'acts' option for the group.

        If any 'contexts' were passed in, then this method will create as many
        groups of actions as there are in the list of contexts. For each dict
        in the 'contexts' list, a new group of actors is created with that
        information.

        Note: Because groups may contain nested group actors, any options
        passed into this actors 'init_context' are also passed into the
        actors that we're intantiating.
        """
        contexts = self.option('contexts')
        if not contexts:
            return self._build_action_group(self._init_context)

        if type(contexts) == list:
            context_data = self.option('contexts')
        elif type(contexts) == dict:
            context_string = open(contexts['file']).read()
            context_string = kp_utils.populate_with_tokens(
                string=context_string,
                tokens=contexts.get('tokens', {}),
                strict=True)
            context_data = demjson.decode(context_string)

        actions = []
        for context in context_data:
            combined_context = dict(self._init_context.items() +
                                    context.items())
            self.log.debug('Inherited context %s' % self._init_context.items())
            self.log.debug('Specified context %s' % context.items())
            self.log.debug('Building acts with parameters: %s' %
                           combined_context)
            for action in self._build_action_group(context=combined_context):
                actions.append(action)

        return actions
Beispiel #12
0
    def _replace_path_tokens(self, path, tokens):
        """Search and replace %xxx% with values from tokens.

        Used to replace any values of %xxx% with 'xxx' from tokens. Can replace
        one, or many fields at aonce.

        Args:
            path: String of the path
            tokens: A dictionary of tokens to search through.

        Returns:
            path: A modified string
        """
        if not path:
            return

        try:
            path = utils.populate_with_tokens(path, tokens)
        except LookupError as e:
            msg = 'Path (%s), tokens: (%s) error: %s' % (path, tokens, e)
            raise TypeError(msg)

        return path
Beispiel #13
0
    def _replace_path_tokens(self, path, tokens):
        """Search and replace %xxx% with values from tokens.

        Used to replace any values of %xxx% with 'xxx' from tokens. Can replace
        one, or many fields at aonce.

        Args:
            path: String of the path
            tokens: A dictionary of tokens to search through.

        Returns:
            path: A modified string
        """
        if not path:
            return

        try:
            path = utils.populate_with_tokens(path, tokens)
        except LookupError as e:
            msg = 'Path (%s), tokens: (%s) error: %s' % (path, tokens, e)
            raise TypeError(msg)

        return path
Beispiel #14
0
 def test_populate_with_unicode_env(self):
     tokens = {'UNIT_TEST': 'FOOBAR'}
     string = 'Unit %UNIT_TEST% Test'
     expect = 'Unit FOOBAR Test'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEqual(result, expect)
Beispiel #15
0
 def test_populate_with_default(self):
     tokens = {'OTHER': 'TOKEN'}
     string = 'Unit %UNIT_TEST|DEFAULT% Test %SECOND_UNIT|1.2,3;4-5%'
     expect = 'Unit DEFAULT Test 1.2,3;4-5'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEquals(result, expect)
Beispiel #16
0
 def test_populate_with_unicode_env(self):
     tokens = {'UNIT_TEST': u'FOOBAR'}
     string = 'Unit %UNIT_TEST% Test'
     expect = 'Unit FOOBAR Test'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEquals(result, expect)
Beispiel #17
0
 def test_populate_with_bool(self):
     tokens = {'UNIT_TEST': True}
     string = 'Unit %UNIT_TEST% Test'
     expect = 'Unit True Test'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEquals(result, expect)
Beispiel #18
0
 def test_populate_with_escape_strict(self):
     tokens = {'UNIT_TEST': 'FOOBAR'}
     string = 'Unit \%UNIT_TEST\% Test'
     expect = 'Unit %UNIT_TEST% Test'
     result = utils.populate_with_tokens(string, tokens, strict=True)
     self.assertEquals(result, expect)
Beispiel #19
0
 def test_populate_with_escape_strict_fail(self):
     tokens = {'UNIT_TEST': 'FOOBAR'}
     string = 'Unit \%UNIT_TEST\% Test %TEST_TWO%'
     with self.assertRaises(LookupError):
         utils.populate_with_tokens(string, tokens, strict=True)
Beispiel #20
0
 def test_populate_with_bogus_data_OK(self):
     tokens = {'UNIT_TEST': {'foobar': 'bat'}}
     string = 'Unit %UNIT_TEST% Test'
     expect = 'Unit %UNIT_TEST% Test'
     result = utils.populate_with_tokens(string, tokens, strict=False)
     self.assertEquals(result, expect)
Beispiel #21
0
 def test_populate_with_env_with_non_string_tokens(self):
     tokens = {'foo': False}
     string = 'Unit test'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEqual(result, string)
Beispiel #22
0
 def test_populate_with_env_with_missing_variables(self):
     os.environ['UNIT_TEST'] = 'FOOBAR'
     string = 'Unit %UNIT_TEST% Test %NOTFOUNDVARIABLE%'
     with self.assertRaises(LookupError):
         utils.populate_with_tokens(string, os.environ)
Beispiel #23
0
 def test_populate_with_bogus_data_OK(self):
     tokens = {'UNIT_TEST': {'foobar': 'bat'}}
     string = 'Unit %UNIT_TEST% Test'
     expect = 'Unit %UNIT_TEST% Test'
     result = utils.populate_with_tokens(string, tokens, strict=False)
     self.assertEqual(result, expect)
Beispiel #24
0
 def test_populate_with_env_with_missing_variables(self):
     os.environ['UNIT_TEST'] = 'FOOBAR'
     string = 'Unit %UNIT_TEST% Test %NOTFOUNDVARIABLE%'
     with self.assertRaises(LookupError):
         utils.populate_with_tokens(string, os.environ)
Beispiel #25
0
 def test_populate_with_values_not_default(self):
     tokens = {'UNIT_TEST': 'FOOBAR', 'SECOND_UNIT': 'BARBAR'}
     string = 'Unit %UNIT_TEST|DEFAULT% Test %SECOND_UNIT|DEFAULT2%'
     expect = 'Unit FOOBAR Test BARBAR'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEquals(result, expect)
Beispiel #26
0
 def test_populate_with_values_not_default(self):
     tokens = {'UNIT_TEST': 'FOOBAR', 'SECOND_UNIT': 'BARBAR'}
     string = 'Unit %UNIT_TEST|DEFAULT% Test %SECOND_UNIT|DEFAULT2%'
     expect = 'Unit FOOBAR Test BARBAR'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEqual(result, expect)
Beispiel #27
0
 def test_populate_with_env_with_non_string_tokens(self):
     tokens = {'foo': False}
     string = 'Unit test'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEquals(result, string)
Beispiel #28
0
 def test_populate_with_default(self):
     tokens = {'OTHER': 'TOKEN'}
     string = 'Unit %UNIT_TEST|DEFAULT% Test %SECOND_UNIT|1.2,3;4-5%'
     expect = 'Unit DEFAULT Test 1.2,3;4-5'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEqual(result, expect)
Beispiel #29
0
 def test_populate_with_bool(self):
     tokens = {'UNIT_TEST': True}
     string = 'Unit %UNIT_TEST% Test'
     expect = 'Unit True Test'
     result = utils.populate_with_tokens(string, tokens)
     self.assertEqual(result, expect)