Esempio n. 1
0
  def __init__(self, impl_paths, path, release_track, construction_id,
               cli_generator, parser_group, parent_group=None):
    """Create a new command.

    Args:
      impl_paths: [str], A list of file paths to the command implementation for
        this command.
      path: [str], A list of group names that got us down to this command
        with respect to the CLI itself.  This path should be used for things
        like error reporting when a specific element in the tree needs to be
        referenced.
      release_track: base.ReleaseTrack, The release track (ga, beta, alpha) that
        this command group is in.  This will apply to all commands under it.
      construction_id: str, A unique identifier for the CLILoader that is
        being constructed.
      cli_generator: cli.CLILoader, The builder used to generate this CLI.
      parser_group: argparse.Parser, The parser to be used for this command.
      parent_group: CommandGroup, The parent of this command.
    """
    common_type = command_loading.LoadCommonType(
        impl_paths, path, release_track, construction_id, is_command=True,
        yaml_command_translator=cli_generator.yaml_command_translator)
    super(Command, self).__init__(
        common_type,
        path=path,
        release_track=release_track,
        cli_generator=cli_generator,
        allow_positional_args=True,
        parser_group=parser_group,
        parent_group=parent_group)

    self._parser.set_defaults(calliope_command=self, command_path=self._path)
Esempio n. 2
0
  def __init__(self, impl_paths, path, release_track, construction_id,
               cli_generator, parser_group, parent_group=None,
               allow_empty=False):
    """Create a new command group.

    Args:
      impl_paths: [str], A list of file paths to the command implementation for
        this group.
      path: [str], A list of group names that got us down to this command group
        with respect to the CLI itself.  This path should be used for things
        like error reporting when a specific element in the tree needs to be
        referenced.
      release_track: base.ReleaseTrack, The release track (ga, beta, alpha) that
        this command group is in.  This will apply to all commands under it.
      construction_id: str, A unique identifier for the CLILoader that is
        being constructed.
      cli_generator: cli.CLILoader, The builder used to generate this CLI.
      parser_group: the current argparse parser, or None if this is the root
        command group.  The root command group will allocate the initial
        top level argparse parser.
      parent_group: CommandGroup, The parent of this group. None if at the
        root.
      allow_empty: bool, True to allow creating this group as empty to start
        with.

    Raises:
      LayoutException: if the module has no sub groups or commands
    """
    common_type = command_loading.LoadCommonType(
        impl_paths, path, release_track, construction_id, is_command=False)
    super(CommandGroup, self).__init__(
        common_type,
        path=path,
        release_track=release_track,
        cli_generator=cli_generator,
        allow_positional_args=False,
        parser_group=parser_group,
        parent_group=parent_group)

    self._construction_id = construction_id

    # find sub groups and commands
    self.groups = {}
    self.commands = {}
    self._groups_to_load = {}
    self._commands_to_load = {}
    self._unloadable_elements = set()

    group_infos, command_infos = command_loading.FindSubElements(impl_paths,
                                                                 path)
    self._groups_to_load.update(group_infos)
    self._commands_to_load.update(command_infos)

    if (not allow_empty and
        not self._groups_to_load and not self._commands_to_load):
      raise command_loading.LayoutException(
          'Group {0} has no subgroups or commands'.format(self.dotted_name))
    # Initialize the sub-parser so sub groups can be found.
    self.SubParser()
Esempio n. 3
0
 def testNoGroups(self):
     with self.assertRaisesRegex(
             command_loading.CommandLoadFailure,
             r'Problem loading foo.bar: Command groups cannot be implemented in '
             r'yaml.'):
         command_loading.LoadCommonType(['dir/foo/bar.yaml'],
                                        ['foo', 'bar'],
                                        calliope_base.ReleaseTrack.GA,
                                        'id',
                                        is_command=False)
     with self.assertRaisesRegex(
             command_loading.CommandLoadFailure,
             r'Problem loading foo.bar: Command groups cannot be implemented in '
             r'yaml.'):
         command_loading.FindSubElements(
             ['dir/foo/bar.py', 'dir/foo/bar.yaml'], ['foo', 'bar'])
Esempio n. 4
0
    def testCommand(self):
        # A stub translator.
        sentinel = object()

        class Translator(command_loading.YamlCommandTranslator):
            def Translate(self, path, command_data):
                return sentinel, command_data.get('release_tracks')

        t = Translator()

        # A single command can be found.
        self.assertEqual((sentinel, None),
                         _FromYaml('file', ['foo', 'bar'], [{}],
                                   calliope_base.ReleaseTrack.GA, t))
        # Explicitly tracked command can be found.
        self.assertEqual((sentinel, ['GA']),
                         _FromYaml('file', ['foo', 'bar'], [{
                             'release_tracks': ['GA']
                         }], calliope_base.ReleaseTrack.GA, t))
        # Make sure we pick the right one.
        self.assertEqual((sentinel, ['GA']),
                         _FromYaml('file', ['foo', 'bar'], [{
                             'release_tracks': ['GA']
                         }, {
                             'release_tracks': ['ALPHA']
                         }], calliope_base.ReleaseTrack.GA, t))
        # No matches with multiple choices.
        with self.assertRaises(
                command_loading.ReleaseTrackNotImplementedException):
            _FromModule('file', [BaseTest.GACommand, BaseTest.AlphaCommand],
                        calliope_base.ReleaseTrack.BETA,
                        is_command=True)

        # Check loading from file.
        self.Touch(
            self.temp_path, 'bar.yaml', """
- release_tracks: [GA, BETA]
- release_tracks: [ALPHA]""")
        self.assertEqual(
            (sentinel, ['GA']),
            command_loading.LoadCommonType(
                [os.path.join(self.temp_path, 'bar.yaml')], ['bar'],
                calliope_base.ReleaseTrack.GA,
                'id',
                is_command=True,
                yaml_command_translator=t))
Esempio n. 5
0
    def __init__(self,
                 module_dir,
                 module_path,
                 path,
                 release_track,
                 construction_id,
                 cli_generator,
                 parser_group,
                 parent_group=None):
        """Create a new command.

    Args:
      module_dir: str, The root of the command tree.
      module_path: a list of command group names that brought us down to this
        command from the top module directory
      path: similar to module_path, but is the path to this command with respect
        to the CLI itself.  This path should be used for things like error
        reporting when a specific element in the tree needs to be referenced.
      release_track: base.ReleaseTrack, The release track (ga, beta, alpha) that
        this command group is in.  This will apply to all commands under it.
      construction_id: str, A unique identifier for the CLILoader that is
        being constructed.
      cli_generator: cli.CLILoader, The builder used to generate this CLI.
      parser_group: argparse.Parser, The parser to be used for this command.
      parent_group: CommandGroup, The parent of this command.
    """
        common_type = command_loading.LoadCommonType(module_dir,
                                                     module_path,
                                                     path,
                                                     release_track,
                                                     construction_id,
                                                     is_command=True)
        super(Command, self).__init__(common_type,
                                      path=path,
                                      release_track=release_track,
                                      cli_generator=cli_generator,
                                      allow_positional_args=True,
                                      parser_group=parser_group,
                                      parent_group=parent_group)

        self._parser.set_defaults(calliope_command=self,
                                  command_path=self._path)