Example #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)
Example #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)
Example #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
Example #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
Example #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)
Example #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)
Example #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)
Example #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
Example #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
Example #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
Example #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
Example #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
Example #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
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)