Ejemplo n.º 1
0
    def test_query_all_tags_single_topic(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check title query
        query_dict = self.topic_tag_db.query('title')
        self.assertEqual(query_dict,
                         {'My First Topic Title': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('description')
        self.assertEqual(query_dict,
                         {'This describes my first topic': ['topic-name-1']})

        # Check the category query
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(query_dict, {'General Topics': ['topic-name-1']})

        # Check the related command query
        query_dict = self.topic_tag_db.query('related command')
        self.assertEqual(query_dict, {'aws s3': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict, {'topic-name-2': ['topic-name-1']})
Ejemplo n.º 2
0
    def test_load_and_save_json_index(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics', 'S3'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            },
            'topic-name-2': {
                'title': ['My Second Topic Title'],
                'description': ['This describes my second topic'],
                'category': ['General Topics'],
                'related topic': ['topic-name-1']
            }
        }

        json_index = self.file_creator.create_file('index.json', '')

        # Create a JSON index to be loaded.
        tag_json = json.dumps(tag_dict, indent=4, sort_keys=True)
        with open(json_index, 'w') as f:
            f.write(tag_json)

        # Load the JSON index.
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.load_json_index()

        # Write the loaded json to disk and ensure it is as expected.
        saved_json_index = self.file_creator.create_file('index2.json', '')
        self.topic_tag_db.index_file = saved_json_index

        self.topic_tag_db.save_to_json_index()
        with open(saved_json_index, 'r') as f:
            self.assertEqual(f.read(), tag_json)
Ejemplo n.º 3
0
 def test_get_tag_single_value_no_exists(self):
     topic_name = 'topic-name-1'
     tag_dict = {topic_name: {'title': ['foo']}}
     self.topic_tag_db = TopicTagDB(tag_dict)
     value = self.topic_tag_db.get_tag_single_value('topic-name-1',
                                                    ':title:')
     self.assertEqual(value, None)
Ejemplo n.º 4
0
 def test_get_tag_multi_value(self):
     topic_name = 'topic-name-1'
     tag_dict = {topic_name: {'related topic': ['foo', 'bar']}}
     self.topic_tag_db = TopicTagDB(tag_dict)
     # Check the related topic get tag value
     value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
     self.assertEqual(value, ['foo', 'bar'])
Ejemplo n.º 5
0
 def subcommand_table(self):
     if self._subcommand_table is None:
         if self._topic_tag_db is None:
             self._topic_tag_db = TopicTagDB()
         self._topic_tag_db.load_json_index()
         self._subcommand_table = self._create_subcommand_table()
     return self._subcommand_table
Ejemplo n.º 6
0
    def test_get_tag_value_all_tags(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check the title get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'title')
        self.assertEqual(value, ['My First Topic Title'])

        # Check the description get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'description')
        self.assertEqual(value, ['This describes my first topic'])

        # Check the category get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'category')
        self.assertEqual(value, ['General Topics'])

        # Check the related command get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related command')
        self.assertEqual(value, ['aws s3'])

        # Check the related topic get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
        self.assertEqual(value, ['topic-name-2'])
Ejemplo n.º 7
0
 def test_query_tag_multi_values(self):
     tag_dict = {'topic-name-1': {'related topic': ['foo', 'bar']}}
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('related topic')
     self.assertEqual(query_dict, {
         'foo': ['topic-name-1'],
         'bar': ['topic-name-1']
     })
Ejemplo n.º 8
0
 def assert_json_index(self, file_paths, reference_tag_dict):
     """Asserts the scanned tags by checking the saved JSON index"""
     json_index = self.file_creator.create_file('index.json', '')
     self.topic_tag_db = TopicTagDB(index_file=json_index)
     self.topic_tag_db.scan(file_paths)
     self.topic_tag_db.save_to_json_index()
     with open(json_index, 'r') as f:
         saved_index = json.loads(f.read())
         self.assertEqual(saved_index, reference_tag_dict)
Ejemplo n.º 9
0
    def test_get_all_topic_source_files(self):
        source_files = []
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
        for i in range(5):
            topic_name = 'topic-name-' + str(i)
            source_files.append(self.file_creator.create_file(topic_name, ''))

        self.assertCountEqual(self.topic_tag_db.get_all_topic_src_files(),
                              source_files)
Ejemplo n.º 10
0
 def test_get_all_topic_source_files_ignore_hidden(self):
     topic_filename = 'mytopic'
     hidden_filename = '.' + topic_filename
     source_files = []
     source_files.append(self.file_creator.create_file(topic_filename, ''))
     self.file_creator.create_file(hidden_filename, '')
     topic_dir = self.file_creator.rootdir
     self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
     self.assertCountEqual(self.topic_tag_db.get_all_topic_src_files(),
                           source_files)
Ejemplo n.º 11
0
 def test_get_all_topic_source_files_ignore_index(self):
     topic_filename = 'mytopic'
     index_filename = 'topic-tags.json'
     source_files = []
     source_files.append(self.file_creator.create_file(topic_filename, ''))
     index_file = self.file_creator.create_file(index_filename, '')
     topic_dir = self.file_creator.rootdir
     self.topic_tag_db = TopicTagDB(index_file=index_file,
                                    topic_dir=topic_dir)
     self.assertCountEqual(self.topic_tag_db.get_all_topic_src_files(),
                           source_files)
Ejemplo n.º 12
0
 def test_query_with_limit_single_value(self):
     tag_dict = {
         'topic-name-1': {
             'category': ['foo', 'bar']
         },
         'topic-name-2': {
             'category': ['bar', 'biz']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('category', ['bar'])
     self.assertCountEqual(query_dict,
                           {'bar': ['topic-name-1', 'topic-name-2']})
Ejemplo n.º 13
0
 def test_get_all_topic_names(self):
     tag_dict = {
         'topic-name-1': {
             'title': ['My First Topic Title'],
         },
         'topic-name-2': {
             'title': ['My Second Topic Title'],
         }
     }
     reference_topic_list = ['topic-name-1', 'topic-name-2']
     self.topic_tag_db = TopicTagDB(tag_dict)
     self.assertCountEqual(self.topic_tag_db.get_all_topic_names(),
                           reference_topic_list)
Ejemplo n.º 14
0
 def test_query_multiple_topics(self):
     tag_dict = {
         'topic-name-1': {
             'category': ['foo']
         },
         'topic-name-2': {
             'category': ['bar']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('category')
     self.assertEqual(query_dict, {
         'foo': ['topic-name-1'],
         'bar': ['topic-name-2']
     })
Ejemplo n.º 15
0
class ProviderHelpCommand(HelpCommand):
    """Implements top level help command.

    This is what is called when ``aws help`` is run.

    """
    EventHandlerClass = ProviderDocumentEventHandler

    def __init__(self, session, command_table, arg_table,
                 description, synopsis, usage):
        HelpCommand.__init__(self, session, session.provider,
                             command_table, arg_table)
        self.description = description
        self.synopsis = synopsis
        self.help_usage = usage
        self._subcommand_table = None
        self._topic_tag_db = None
        self._related_items = ['aws help topics']

    @property
    def event_class(self):
        return 'aws'

    @property
    def name(self):
        return self.obj.name

    @property
    def subcommand_table(self):
        if self._subcommand_table is None:
            if self._topic_tag_db is None:
                self._topic_tag_db = TopicTagDB()
            self._topic_tag_db.load_json_index()
            self._subcommand_table = self._create_subcommand_table()
        return self._subcommand_table

    def _create_subcommand_table(self):
        subcommand_table = {}
        # Add the ``aws help topics`` command to the ``topic_table``
        topic_lister_command = TopicListerCommand(self.session)
        subcommand_table['topics'] = topic_lister_command
        topic_names = self._topic_tag_db.get_all_topic_names()

        # Add all of the possible topics to the ``topic_table``
        for topic_name in topic_names:
            topic_help_command = TopicHelpCommand(self.session, topic_name)
            subcommand_table[topic_name] = topic_help_command
        return subcommand_table
Ejemplo n.º 16
0
class ProviderHelpCommand(HelpCommand):
    """Implements top level help command.

    This is what is called when ``aws help`` is run.

    """
    EventHandlerClass = ProviderDocumentEventHandler

    def __init__(self, session, command_table, arg_table,
                 description, synopsis, usage):
        HelpCommand.__init__(self, session, None,
                             command_table, arg_table)
        self.description = description
        self.synopsis = synopsis
        self.help_usage = usage
        self._subcommand_table = None
        self._topic_tag_db = None
        self._related_items = ['aws help topics']

    @property
    def event_class(self):
        return 'aws'

    @property
    def name(self):
        return 'aws'

    @property
    def subcommand_table(self):
        if self._subcommand_table is None:
            if self._topic_tag_db is None:
                self._topic_tag_db = TopicTagDB()
            self._topic_tag_db.load_json_index()
            self._subcommand_table = self._create_subcommand_table()
        return self._subcommand_table

    def _create_subcommand_table(self):
        subcommand_table = {}
        # Add the ``aws help topics`` command to the ``topic_table``
        topic_lister_command = TopicListerCommand(self.session)
        subcommand_table['topics'] = topic_lister_command
        topic_names = self._topic_tag_db.get_all_topic_names()

        # Add all of the possible topics to the ``topic_table``
        for topic_name in topic_names:
            topic_help_command = TopicHelpCommand(self.session, topic_name)
            subcommand_table[topic_name] = topic_help_command
        return subcommand_table
Ejemplo n.º 17
0
    def test_get_tag_value_all_tags(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check the title get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'title')
        self.assertEqual(value, ['My First Topic Title'])

        # Check the description get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'description')
        self.assertEqual(value, ['This describes my first topic'])

        # Check the category get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'category')
        self.assertEqual(value, ['General Topics'])

        # Check the related command get tag value
        value = self.topic_tag_db.get_tag_value(topic_name,
                                                'related command')
        self.assertEqual(value, ['aws s3'])

        # Check the related topic get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
        self.assertEqual(value, ['topic-name-2'])
Ejemplo n.º 18
0
    def test_load_and_save_json_index(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics', 'S3'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            },
            'topic-name-2': {
                'title': ['My Second Topic Title'],
                'description': ['This describes my second topic'],
                'category': ['General Topics'],
                'related topic': ['topic-name-1']
            }
        }

        json_index = self.file_creator.create_file('index.json', '')

        # Create a JSON index to be loaded.
        tag_json = json.dumps(tag_dict, indent=4, sort_keys=True)
        with open(json_index, 'w') as f:
            f.write(tag_json)

        # Load the JSON index.
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.load_json_index()

        # Write the loaded json to disk and ensure it is as expected.
        saved_json_index = self.file_creator.create_file('index2.json', '')
        self.topic_tag_db.index_file = saved_json_index

        self.topic_tag_db.save_to_json_index()
        with open(saved_json_index, 'r') as f:
            self.assertEqual(f.read(), tag_json)
Ejemplo n.º 19
0
 def subcommand_table(self):
     if self._subcommand_table is None:
         if self._topic_tag_db is None:
             self._topic_tag_db = TopicTagDB()
         self._topic_tag_db.load_json_index()
         self._subcommand_table = self._create_subcommand_table()
     return self._subcommand_table
Ejemplo n.º 20
0
 def test_query_multiple_topics_with_overlap_values(self):
     tag_dict = {
         'topic-name-1': {
             'category': ['foo', 'bar']
         },
         'topic-name-2': {
             'category': ['bar', 'biz']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('category')
     self.assertCountEqual(
         query_dict, {
             'foo': ['topic-name-1'],
             'biz': ['topic-name-2'],
             'bar': ['topic-name-1', 'topic-name-2']
         })
Ejemplo n.º 21
0
 def topic_query_with_non_existant_tag(self):
     tag_dict = {
         'topic-name-1': {
             'category': ['foo']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query(':bar:')
     self.assertEqual(query_dict, {})
Ejemplo n.º 22
0
 def assert_json_index(self, file_paths, reference_tag_dict):
     """Asserts the scanned tags by checking the saved JSON index"""
     json_index = self.file_creator.create_file('index.json', '')
     self.topic_tag_db = TopicTagDB(index_file=json_index)
     self.topic_tag_db.scan(file_paths)
     self.topic_tag_db.save_to_json_index()
     with open(json_index, 'r') as f:
         saved_index = json.loads(f.read())
         self.assertEqual(saved_index, reference_tag_dict)
Ejemplo n.º 23
0
 def test_query_tag_multi_values(self):
     tag_dict = {
         'topic-name-1': {
             'related topic': ['foo', 'bar']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('related topic')
     self.assertEqual(query_dict,
                      {'foo': ['topic-name-1'], 'bar': ['topic-name-1']})
Ejemplo n.º 24
0
 def test_get_tag_single_value_exception(self):
     topic_name = 'topic-name-1'
     tag_dict = {
         topic_name: {
             'title': ['foo', 'bar']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     with self.assertRaises(ValueError):
         self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')
Ejemplo n.º 25
0
 def test_get_tag_single_value(self):
     topic_name = 'topic-name-1'
     tag_dict = {
         topic_name: {
             'title': ['foo']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     value = self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')
     self.assertEqual(value, 'foo')
Ejemplo n.º 26
0
 def test_get_tag_no_exist_use_default(self):
     topic_name = 'topic-name-1'
     tag_dict = {
         topic_name: {
             'related topic': ['foo']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     value = self.topic_tag_db.get_tag_value('no-exist', ':foo:', [])
     self.assertEqual(value, [])
Ejemplo n.º 27
0
 def test_get_tag_no_exist_tag(self):
     topic_name = 'topic-name-1'
     tag_dict = {
         topic_name: {
             'related topic': ['foo']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     value = self.topic_tag_db.get_tag_value(topic_name, ':foo:')
     self.assertEqual(value, None)
Ejemplo n.º 28
0
 def test_get_tag_multi_value(self):
     topic_name = 'topic-name-1'
     tag_dict = {
         topic_name: {
             'related topic': ['foo', 'bar']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     # Check the related topic get tag value
     value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
     self.assertEqual(value, ['foo', 'bar'])
Ejemplo n.º 29
0
 def test_get_all_topic_source_files_ignore_hidden(self):
     topic_filename = 'mytopic'
     hidden_filename = '.' + topic_filename
     source_files = []
     source_files.append(self.file_creator.create_file(topic_filename, ''))
     self.file_creator.create_file(hidden_filename, '')
     topic_dir = self.file_creator.rootdir
     self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
     self.assertCountEqual(
         self.topic_tag_db.get_all_topic_src_files(),
         source_files
     )
Ejemplo n.º 30
0
    def test_get_all_topic_source_files(self):
        source_files = []
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
        for i in range(5):
            topic_name = 'topic-name-' + str(i)
            source_files.append(self.file_creator.create_file(topic_name, ''))

        self.assertCountEqual(
            self.topic_tag_db.get_all_topic_src_files(),
            source_files
        )
Ejemplo n.º 31
0
 def test_get_all_topic_source_files_ignore_index(self):
     topic_filename = 'mytopic'
     index_filename = 'topic-tags.json'
     source_files = []
     source_files.append(self.file_creator.create_file(topic_filename, ''))
     index_file = self.file_creator.create_file(index_filename, '')
     topic_dir = self.file_creator.rootdir
     self.topic_tag_db = TopicTagDB(index_file=index_file,
                                    topic_dir=topic_dir)
     self.assertCountEqual(
         self.topic_tag_db.get_all_topic_src_files(),
         source_files
     )
Ejemplo n.º 32
0
 def test_get_all_topic_names(self):
     tag_dict = {
         'topic-name-1': {
             'title': ['My First Topic Title'],
         },
         'topic-name-2': {
             'title': ['My Second Topic Title'],
         }
     }
     reference_topic_list = ['topic-name-1', 'topic-name-2']
     self.topic_tag_db = TopicTagDB(tag_dict)
     self.assertCountEqual(self.topic_tag_db.get_all_topic_names(),
                           reference_topic_list)
Ejemplo n.º 33
0
 def test_query_with_limit_single_value(self):
     tag_dict = {
         'topic-name-1': {
             'category': ['foo', 'bar']
         },
         'topic-name-2': {
             'category': ['bar', 'biz']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('category', ['bar'])
     self.assertCountEqual(query_dict,
                           {'bar': ['topic-name-1', 'topic-name-2']})
Ejemplo n.º 34
0
 def test_query_multiple_topics(self):
     tag_dict = {
         'topic-name-1': {
             'category': ['foo']
         },
         'topic-name-2': {
             'category': ['bar']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('category')
     self.assertEqual(query_dict,
                      {'foo': ['topic-name-1'], 'bar': ['topic-name-2']})
Ejemplo n.º 35
0
 def test_query_multiple_topics_with_overlap_values(self):
     tag_dict = {
         'topic-name-1': {
             'category': ['foo', 'bar']
         },
         'topic-name-2': {
             'category': ['bar', 'biz']
         }
     }
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query('category')
     self.assertCountEqual(
         query_dict, {'foo': ['topic-name-1'], 'biz': ['topic-name-2'],
                      'bar': ['topic-name-1', 'topic-name-2']})
Ejemplo n.º 36
0
    def test_query_all_tags_single_topic(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check title query
        query_dict = self.topic_tag_db.query('title')
        self.assertEqual(query_dict,
                         {'My First Topic Title': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('description')
        self.assertEqual(query_dict,
                         {'This describes my first topic': ['topic-name-1']})

        # Check the category query
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(query_dict,
                         {'General Topics': ['topic-name-1']})

        # Check the related command query
        query_dict = self.topic_tag_db.query('related command')
        self.assertEqual(query_dict,
                         {'aws s3': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict,
                         {'topic-name-2': ['topic-name-1']})
Ejemplo n.º 37
0
 def __init__(self, help_command):
     self.help_command = help_command
     self.register(help_command.session, help_command.event_class)
     self._topic_tag_db = TopicTagDB()
     self._topic_tag_db.load_json_index()
Ejemplo n.º 38
0
class TestTopicDBScan(TestTopicTagDB):
    def create_topic_src_file(self, topic_name, tags):
        """Create a topic source file from a list of tags and topic name"""
        content = '\n'.join(tags)
        topic_name = topic_name + '.rst'
        topic_filepath = self.file_creator.create_file(topic_name, content)
        return topic_filepath

    def assert_json_index(self, file_paths, reference_tag_dict):
        """Asserts the scanned tags by checking the saved JSON index"""
        json_index = self.file_creator.create_file('index.json', '')
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.scan(file_paths)
        self.topic_tag_db.save_to_json_index()
        with open(json_index, 'r') as f:
            saved_index = json.loads(f.read())
            self.assertEqual(saved_index, reference_tag_dict)

    def test_scan_all_valid_tags(self):
        tags = [
            ':description: This is a description',
            ':title: Title',
            ':category: Foo',
            ':related topic: Bar',
            ':related command: ec2'
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'description': ['This is a description'],
                'title': ['Title'],
                'category': ['Foo'],
                'related topic': ['Bar'],
                'related command': ['ec2']
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_invalid_tag(self):
        tags = [
            ':description: This is a description',
            ':title: Title',
            ':category: Foo',
            ':related_topic: Bar',
        ]
        topic_name = 'my-topic'

        topic_filepath = self.create_topic_src_file(topic_name, tags)
        with self.assertRaises(ValueError):
            self.topic_tag_db.scan([topic_filepath])

    def test_scan_no_tags(self):
        tags = []
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {}
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_values(self):
        tags = [
            ':category: Foo, Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_single_and_multi_values(self):
        tags = [
            ':title: Title',
            ':category: Foo, Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'title': ['Title'],
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_duplicate_values(self):
        tags = [
            ':category: Foo, Foo, Bar'
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_values_extra_space(self):
        tags = [
            ':category:    Foo, Bar   ',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_values_no_space(self):
        tags = [
            ':category: Foo,Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_preserve_space(self):
        tags = [
            ':category: Foo Bar, Baz',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo Bar', 'Baz'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_multiple_files(self):
        topic_base = 'my-topic'
        reference_tag_dict = {}
        topic_files = []
        for i in range(5):
            topic_name = topic_base + '-' + str(i)
            tags = [
                ':description: This is about %s' % topic_name,
                ':title: Title',
                ':category: Foo',
                ':related topic: Bar',
                ':related command: ec2'
            ]

            reference_tag_dict[topic_name] = {
                'description': ['This is about %s' % topic_name],
                'title': ['Title'],
                'category': ['Foo'],
                'related topic': ['Bar'],
                'related command': ['ec2']
            }
            topic_files.append(self.create_topic_src_file(topic_name, tags))
        self.assert_json_index(topic_files, reference_tag_dict)
Ejemplo n.º 39
0
 def test_index_file(self):
     self.topic_tag_db = TopicTagDB(index_file='foo')
     self.assertEqual(self.topic_tag_db.index_file, 'foo')
     self.topic_tag_db.index_file = 'bar'
     self.assertEqual(self.topic_tag_db.index_file, 'bar')
Ejemplo n.º 40
0
 def test_topic_dir(self):
     self.topic_tag_db = TopicTagDB(topic_dir='foo')
     self.assertEqual(self.topic_tag_db.topic_dir, 'foo')
     self.topic_tag_db.topic_dir = 'bar'
     self.assertEqual(self.topic_tag_db.topic_dir, 'bar')
Ejemplo n.º 41
0
class TestTopicTagDBQuery(TestTopicTagDB):
    def test_query_all_tags_single_topic(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check title query
        query_dict = self.topic_tag_db.query('title')
        self.assertEqual(query_dict,
                         {'My First Topic Title': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('description')
        self.assertEqual(query_dict,
                         {'This describes my first topic': ['topic-name-1']})

        # Check the category query
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(query_dict,
                         {'General Topics': ['topic-name-1']})

        # Check the related command query
        query_dict = self.topic_tag_db.query('related command')
        self.assertEqual(query_dict,
                         {'aws s3': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict,
                         {'topic-name-2': ['topic-name-1']})

    def test_query_tag_multi_values(self):
        tag_dict = {
            'topic-name-1': {
                'related topic': ['foo', 'bar']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict,
                         {'foo': ['topic-name-1'], 'bar': ['topic-name-1']})

    def test_query_tag_multi_values(self):
        tag_dict = {
            'topic-name-1': {
                'related topic': ['foo', 'bar']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict,
                         {'foo': ['topic-name-1'], 'bar': ['topic-name-1']})

    def test_query_multiple_topics(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo']
            },
            'topic-name-2': {
                'category': ['bar']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(query_dict,
                         {'foo': ['topic-name-1'], 'bar': ['topic-name-2']})

    def test_query_multiple_topics_with_multi_values(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['baz', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(query_dict,
                         {'foo': ['topic-name-1'], 'bar': ['topic-name-1'],
                          'baz': ['topic-name-2'], 'biz': ['topic-name-2']})

    def test_query_multiple_topics_with_overlap_values(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['bar', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category')
        self.assertCountEqual(
            query_dict, {'foo': ['topic-name-1'], 'biz': ['topic-name-2'],
                         'bar': ['topic-name-1', 'topic-name-2']})

    def test_query_with_limit_single_value(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['bar', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category', ['bar'])
        self.assertCountEqual(query_dict,
                              {'bar': ['topic-name-1', 'topic-name-2']})

    def test_query_with_limit_multi_value(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['bar', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category', ['foo', 'bar'])
        self.assertCountEqual(query_dict,
                              {'foo': ['topic-name-1'],
                               'bar': ['topic-name-1', 'topic-name-2']})

    def topic_query_with_non_existant_tag(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query(':bar:')
        self.assertEqual(query_dict, {})
Ejemplo n.º 42
0
class TestTopicTagDBQuery(TestTopicTagDB):
    def test_query_all_tags_single_topic(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check title query
        query_dict = self.topic_tag_db.query('title')
        self.assertEqual(query_dict,
                         {'My First Topic Title': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('description')
        self.assertEqual(query_dict,
                         {'This describes my first topic': ['topic-name-1']})

        # Check the category query
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(query_dict, {'General Topics': ['topic-name-1']})

        # Check the related command query
        query_dict = self.topic_tag_db.query('related command')
        self.assertEqual(query_dict, {'aws s3': ['topic-name-1']})

        # Check the description query
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict, {'topic-name-2': ['topic-name-1']})

    def test_query_tag_multi_values(self):
        tag_dict = {'topic-name-1': {'related topic': ['foo', 'bar']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict, {
            'foo': ['topic-name-1'],
            'bar': ['topic-name-1']
        })

    def test_query_tag_multi_values(self):
        tag_dict = {'topic-name-1': {'related topic': ['foo', 'bar']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('related topic')
        self.assertEqual(query_dict, {
            'foo': ['topic-name-1'],
            'bar': ['topic-name-1']
        })

    def test_query_multiple_topics(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo']
            },
            'topic-name-2': {
                'category': ['bar']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(query_dict, {
            'foo': ['topic-name-1'],
            'bar': ['topic-name-2']
        })

    def test_query_multiple_topics_with_multi_values(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['baz', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category')
        self.assertEqual(
            query_dict, {
                'foo': ['topic-name-1'],
                'bar': ['topic-name-1'],
                'baz': ['topic-name-2'],
                'biz': ['topic-name-2']
            })

    def test_query_multiple_topics_with_overlap_values(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['bar', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category')
        self.assertCountEqual(
            query_dict, {
                'foo': ['topic-name-1'],
                'biz': ['topic-name-2'],
                'bar': ['topic-name-1', 'topic-name-2']
            })

    def test_query_with_limit_single_value(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['bar', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category', ['bar'])
        self.assertCountEqual(query_dict,
                              {'bar': ['topic-name-1', 'topic-name-2']})

    def test_query_with_limit_multi_value(self):
        tag_dict = {
            'topic-name-1': {
                'category': ['foo', 'bar']
            },
            'topic-name-2': {
                'category': ['bar', 'biz']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query('category', ['foo', 'bar'])
        self.assertCountEqual(query_dict, {
            'foo': ['topic-name-1'],
            'bar': ['topic-name-1', 'topic-name-2']
        })

    def topic_query_with_non_existant_tag(self):
        tag_dict = {'topic-name-1': {'category': ['foo']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        query_dict = self.topic_tag_db.query(':bar:')
        self.assertEqual(query_dict, {})
class TopicListerDocumentEventHandler(CLIDocumentEventHandler):
    DESCRIPTION = (
        'This is the AWS CLI Topic Guide. It gives access to a set '
        'of topics that provide a deeper understanding of the CLI. To access '
        'the list of topics from the command line, run ``aws help topics``. '
        'To access a specific topic from the command line, run '
        '``aws help [topicname]``, where ``topicname`` is the name of the '
        'topic as it appears in the output from ``aws help topics``.')

    def __init__(self, help_command):
        self.help_command = help_command
        self.register(help_command.session, help_command.event_class)
        self._topic_tag_db = TopicTagDB()
        self._topic_tag_db.load_json_index()

    def doc_breadcrumbs(self, help_command, **kwargs):
        doc = help_command.doc
        if doc.target != 'man':
            doc.write('[ ')
            doc.style.sphinx_reference_label(label='cli:aws', text='aws')
            doc.write(' ]')

    def doc_title(self, help_command, **kwargs):
        doc = help_command.doc
        doc.style.new_paragraph()
        doc.style.link_target_definition(
            refname='cli:aws help %s' % self.help_command.name,
            link='')
        doc.style.h1('AWS CLI Topic Guide')

    def doc_description(self, help_command, **kwargs):
        doc = help_command.doc
        doc.style.h2('Description')
        doc.include_doc_string(self.DESCRIPTION)
        doc.style.new_paragraph()

    def doc_synopsis_start(self, help_command, **kwargs):
        pass

    def doc_synopsis_end(self, help_command, **kwargs):
        pass

    def doc_options_start(self, help_command, **kwargs):
        pass

    def doc_options_end(self, help_command, **kwargs):
        pass

    def doc_subitems_start(self, help_command, **kwargs):
        doc = help_command.doc
        doc.style.h2('Available Topics')

        categories = self._topic_tag_db.query('category')
        topic_names = self._topic_tag_db.get_all_topic_names()

        # Sort the categories
        category_names = sorted(categories.keys())
        for category_name in category_names:
            doc.style.h3(category_name)
            doc.style.new_paragraph()
            # Write out the topic and a description for each topic under
            # each category.
            for topic_name in sorted(categories[category_name]):
                description = self._topic_tag_db.get_tag_single_value(
                    topic_name, 'description')
                doc.write('* ')
                doc.style.sphinx_reference_label(
                    label='cli:aws help %s' % topic_name,
                    text=topic_name
                )
                doc.write(': %s\n' % description)
        # Add a hidden toctree to make sure everything is connected in
        # the document.
        doc.style.hidden_toctree()
        for topic_name in topic_names:
            doc.style.hidden_tocitem(topic_name)
Ejemplo n.º 44
0
class TestTopicTagDBGeneral(TestTopicTagDB):
    def test_valid_tags(self):
        self.assertCountEqual(self.topic_tag_db.valid_tags, [
            'title', 'description', 'category', 'related command',
            'related topic'
        ])

    def test_topic_dir(self):
        self.topic_tag_db = TopicTagDB(topic_dir='foo')
        self.assertEqual(self.topic_tag_db.topic_dir, 'foo')
        self.topic_tag_db.topic_dir = 'bar'
        self.assertEqual(self.topic_tag_db.topic_dir, 'bar')

    def test_index_file(self):
        self.topic_tag_db = TopicTagDB(index_file='foo')
        self.assertEqual(self.topic_tag_db.index_file, 'foo')
        self.topic_tag_db.index_file = 'bar'
        self.assertEqual(self.topic_tag_db.index_file, 'bar')

    def test_get_all_topic_names(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
            },
            'topic-name-2': {
                'title': ['My Second Topic Title'],
            }
        }
        reference_topic_list = ['topic-name-1', 'topic-name-2']
        self.topic_tag_db = TopicTagDB(tag_dict)
        self.assertCountEqual(self.topic_tag_db.get_all_topic_names(),
                              reference_topic_list)

    def test_get_all_topic_source_files(self):
        source_files = []
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
        for i in range(5):
            topic_name = 'topic-name-' + str(i)
            source_files.append(self.file_creator.create_file(topic_name, ''))

        self.assertCountEqual(self.topic_tag_db.get_all_topic_src_files(),
                              source_files)

    def test_get_all_topic_source_files_ignore_index(self):
        topic_filename = 'mytopic'
        index_filename = 'topic-tags.json'
        source_files = []
        source_files.append(self.file_creator.create_file(topic_filename, ''))
        index_file = self.file_creator.create_file(index_filename, '')
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(index_file=index_file,
                                       topic_dir=topic_dir)
        self.assertCountEqual(self.topic_tag_db.get_all_topic_src_files(),
                              source_files)

    def test_get_all_topic_source_files_ignore_hidden(self):
        topic_filename = 'mytopic'
        hidden_filename = '.' + topic_filename
        source_files = []
        source_files.append(self.file_creator.create_file(topic_filename, ''))
        self.file_creator.create_file(hidden_filename, '')
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
        self.assertCountEqual(self.topic_tag_db.get_all_topic_src_files(),
                              source_files)

    def test_get_tag_value_all_tags(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check the title get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'title')
        self.assertEqual(value, ['My First Topic Title'])

        # Check the description get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'description')
        self.assertEqual(value, ['This describes my first topic'])

        # Check the category get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'category')
        self.assertEqual(value, ['General Topics'])

        # Check the related command get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related command')
        self.assertEqual(value, ['aws s3'])

        # Check the related topic get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
        self.assertEqual(value, ['topic-name-2'])

    def test_get_tag_multi_value(self):
        topic_name = 'topic-name-1'
        tag_dict = {topic_name: {'related topic': ['foo', 'bar']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        # Check the related topic get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
        self.assertEqual(value, ['foo', 'bar'])

    def test_get_tag_topic_no_exists(self):
        topic_name = 'topic-name-1'
        tag_dict = {topic_name: {'related topic': ['foo']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value('no-exist', 'related topic')
        self.assertEqual(value, None)

    def test_get_tag_no_exist_tag(self):
        topic_name = 'topic-name-1'
        tag_dict = {topic_name: {'related topic': ['foo']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value(topic_name, ':foo:')
        self.assertEqual(value, None)

    def test_get_tag_no_exist_use_default(self):
        topic_name = 'topic-name-1'
        tag_dict = {topic_name: {'related topic': ['foo']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value('no-exist', ':foo:', [])
        self.assertEqual(value, [])

    def test_get_tag_single_value(self):
        topic_name = 'topic-name-1'
        tag_dict = {topic_name: {'title': ['foo']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')
        self.assertEqual(value, 'foo')

    def test_get_tag_single_value_exception(self):
        topic_name = 'topic-name-1'
        tag_dict = {topic_name: {'title': ['foo', 'bar']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        with self.assertRaises(ValueError):
            self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')

    def test_get_tag_single_value_no_exists(self):
        topic_name = 'topic-name-1'
        tag_dict = {topic_name: {'title': ['foo']}}
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_single_value('topic-name-1',
                                                       ':title:')
        self.assertEqual(value, None)

    def test_load_and_save_json_index(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics', 'S3'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            },
            'topic-name-2': {
                'title': ['My Second Topic Title'],
                'description': ['This describes my second topic'],
                'category': ['General Topics'],
                'related topic': ['topic-name-1']
            }
        }

        json_index = self.file_creator.create_file('index.json', '')

        # Create a JSON index to be loaded.
        tag_json = json.dumps(tag_dict, indent=4, sort_keys=True)
        with open(json_index, 'w') as f:
            f.write(tag_json)

        # Load the JSON index.
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.load_json_index()

        # Write the loaded json to disk and ensure it is as expected.
        saved_json_index = self.file_creator.create_file('index2.json', '')
        self.topic_tag_db.index_file = saved_json_index

        self.topic_tag_db.save_to_json_index()
        with open(saved_json_index, 'r') as f:
            self.assertEqual(f.read(), tag_json)
Ejemplo n.º 45
0
 def setUp(self):
     self.topic_tag_db = TopicTagDB()
     self.file_creator = FileCreator()
Ejemplo n.º 46
0
 def test_get_tag_no_exist_tag(self):
     topic_name = 'topic-name-1'
     tag_dict = {topic_name: {'related topic': ['foo']}}
     self.topic_tag_db = TopicTagDB(tag_dict)
     value = self.topic_tag_db.get_tag_value(topic_name, ':foo:')
     self.assertEqual(value, None)
Ejemplo n.º 47
0
 def test_get_tag_no_exist_use_default(self):
     topic_name = 'topic-name-1'
     tag_dict = {topic_name: {'related topic': ['foo']}}
     self.topic_tag_db = TopicTagDB(tag_dict)
     value = self.topic_tag_db.get_tag_value('no-exist', ':foo:', [])
     self.assertEqual(value, [])
Ejemplo n.º 48
0
 def test_index_file(self):
     self.topic_tag_db = TopicTagDB(index_file='foo')
     self.assertEqual(self.topic_tag_db.index_file, 'foo')
     self.topic_tag_db.index_file = 'bar'
     self.assertEqual(self.topic_tag_db.index_file, 'bar')
Ejemplo n.º 49
0
 def test_get_tag_single_value_exception(self):
     topic_name = 'topic-name-1'
     tag_dict = {topic_name: {'title': ['foo', 'bar']}}
     self.topic_tag_db = TopicTagDB(tag_dict)
     with self.assertRaises(ValueError):
         self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')
Ejemplo n.º 50
0
 def __init__(self, help_command):
     self.help_command = help_command
     self.register(help_command.session, help_command.event_class)
     self.help_command.doc.translation_map = self.build_translation_map()
     self._topic_tag_db = TopicTagDB()
     self._topic_tag_db.load_json_index()
Ejemplo n.º 51
0
 def topic_query_with_non_existant_tag(self):
     tag_dict = {'topic-name-1': {'category': ['foo']}}
     self.topic_tag_db = TopicTagDB(tag_dict)
     query_dict = self.topic_tag_db.query(':bar:')
     self.assertEqual(query_dict, {})
Ejemplo n.º 52
0
class TopicListerDocumentEventHandler(CLIDocumentEventHandler):
    DESCRIPTION = (
        'This is the AWS CLI Topic Guide. It gives access to a set '
        'of topics that provide a deeper understanding of the CLI. To access '
        'the list of topics from the command line, run ``aws help topics``. '
        'To access a specific topic from the command line, run '
        '``aws help [topicname]``, where ``topicname`` is the name of the '
        'topic as it appears in the output from ``aws help topics``.')

    def __init__(self, help_command):
        self.help_command = help_command
        self.register(help_command.session, help_command.event_class)
        self.help_command.doc.translation_map = self.build_translation_map()
        self._topic_tag_db = TopicTagDB()
        self._topic_tag_db.load_json_index()

    def doc_breadcrumbs(self, help_command, **kwargs):
        doc = help_command.doc
        if doc.target != 'man':
            doc.write('[ ')
            doc.style.sphinx_reference_label(label='cli:aws', text='aws')
            doc.write(' ]')

    def doc_title(self, help_command, **kwargs):
        doc = help_command.doc
        doc.style.new_paragraph()
        doc.style.link_target_definition(
            refname='cli:aws help %s' % self.help_command.name,
            link='')
        doc.style.h1('AWS CLI Topic Guide')

    def doc_description(self, help_command, **kwargs):
        doc = help_command.doc
        doc.style.h2('Description')
        doc.include_doc_string(self.DESCRIPTION)
        doc.style.new_paragraph()

    def doc_synopsis_start(self, help_command, **kwargs):
        pass

    def doc_synopsis_end(self, help_command, **kwargs):
        pass

    def doc_options_start(self, help_command, **kwargs):
        pass

    def doc_options_end(self, help_command, **kwargs):
        pass

    def doc_subitems_start(self, help_command, **kwargs):
        doc = help_command.doc
        doc.style.h2('Available Topics')

        categories = self._topic_tag_db.query('category')
        topic_names = self._topic_tag_db.get_all_topic_names()

        # Sort the categories
        category_names = sorted(categories.keys())
        for category_name in category_names:
            doc.style.h3(category_name)
            doc.style.new_paragraph()
            # Write out the topic and a description for each topic under
            # each category.
            for topic_name in sorted(categories[category_name]):
                description = self._topic_tag_db.get_tag_single_value(
                    topic_name, 'description')
                doc.write('* ')
                doc.style.sphinx_reference_label(
                    label='cli:aws help %s' % topic_name,
                    text=topic_name
                )
                doc.write(': %s\n' % description)
        # Add a hidden toctree to make sure everything is connected in
        # the document.
        doc.style.hidden_toctree()
        for topic_name in topic_names:
            doc.style.hidden_tocitem(topic_name)
Ejemplo n.º 53
0
class TestTopicDBScan(TestTopicTagDB):
    def create_topic_src_file(self, topic_name, tags):
        """Create a topic source file from a list of tags and topic name"""
        content = '\n'.join(tags)
        topic_name = topic_name + '.rst'
        topic_filepath = self.file_creator.create_file(topic_name, content)
        return topic_filepath

    def assert_json_index(self, file_paths, reference_tag_dict):
        """Asserts the scanned tags by checking the saved JSON index"""
        json_index = self.file_creator.create_file('index.json', '')
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.scan(file_paths)
        self.topic_tag_db.save_to_json_index()
        with open(json_index, 'r') as f:
            saved_index = json.loads(f.read())
            self.assertEqual(saved_index, reference_tag_dict)

    def test_scan_all_valid_tags(self):
        tags = [
            ':description: This is a description', ':title: Title',
            ':category: Foo', ':related topic: Bar', ':related command: ec2'
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'description': ['This is a description'],
                'title': ['Title'],
                'category': ['Foo'],
                'related topic': ['Bar'],
                'related command': ['ec2']
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_invalid_tag(self):
        tags = [
            ':description: This is a description',
            ':title: Title',
            ':category: Foo',
            ':related_topic: Bar',
        ]
        topic_name = 'my-topic'

        topic_filepath = self.create_topic_src_file(topic_name, tags)
        with self.assertRaises(ValueError):
            self.topic_tag_db.scan([topic_filepath])

    def test_scan_no_tags(self):
        tags = []
        topic_name = 'my-topic'

        reference_tag_dict = {topic_name: {}}
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_values(self):
        tags = [
            ':category: Foo, Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_single_and_multi_values(self):
        tags = [
            ':title: Title',
            ':category: Foo, Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'title': ['Title'],
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_duplicate_values(self):
        tags = [':category: Foo, Foo, Bar']
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_values_extra_space(self):
        tags = [
            ':category:    Foo, Bar   ',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_values_no_space(self):
        tags = [
            ':category: Foo,Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_preserve_space(self):
        tags = [
            ':category: Foo Bar, Baz',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo Bar', 'Baz'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_multiple_files(self):
        topic_base = 'my-topic'
        reference_tag_dict = {}
        topic_files = []
        for i in range(5):
            topic_name = topic_base + '-' + str(i)
            tags = [
                ':description: This is about %s' % topic_name, ':title: Title',
                ':category: Foo', ':related topic: Bar',
                ':related command: ec2'
            ]

            reference_tag_dict[topic_name] = {
                'description': ['This is about %s' % topic_name],
                'title': ['Title'],
                'category': ['Foo'],
                'related topic': ['Bar'],
                'related command': ['ec2']
            }
            topic_files.append(self.create_topic_src_file(topic_name, tags))
        self.assert_json_index(topic_files, reference_tag_dict)
 def __init__(self, help_command):
     self.help_command = help_command
     self.register(help_command.session, help_command.event_class)
     self._topic_tag_db = TopicTagDB()
     self._topic_tag_db.load_json_index()
Ejemplo n.º 55
0
class TestTopicTagDBGeneral(TestTopicTagDB):
    def test_valid_tags(self):
        self.assertCountEqual(
            self.topic_tag_db.valid_tags,
            ['title', 'description', 'category', 'related command',
             'related topic']
        )

    def test_topic_dir(self):
        self.topic_tag_db = TopicTagDB(topic_dir='foo')
        self.assertEqual(self.topic_tag_db.topic_dir, 'foo')
        self.topic_tag_db.topic_dir = 'bar'
        self.assertEqual(self.topic_tag_db.topic_dir, 'bar')

    def test_index_file(self):
        self.topic_tag_db = TopicTagDB(index_file='foo')
        self.assertEqual(self.topic_tag_db.index_file, 'foo')
        self.topic_tag_db.index_file = 'bar'
        self.assertEqual(self.topic_tag_db.index_file, 'bar')

    def test_get_all_topic_names(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
            },
            'topic-name-2': {
                'title': ['My Second Topic Title'],
            }
        }
        reference_topic_list = ['topic-name-1', 'topic-name-2']
        self.topic_tag_db = TopicTagDB(tag_dict)
        self.assertCountEqual(self.topic_tag_db.get_all_topic_names(),
                              reference_topic_list)

    def test_get_all_topic_source_files(self):
        source_files = []
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
        for i in range(5):
            topic_name = 'topic-name-' + str(i)
            source_files.append(self.file_creator.create_file(topic_name, ''))

        self.assertCountEqual(
            self.topic_tag_db.get_all_topic_src_files(),
            source_files
        )

    def test_get_all_topic_source_files_ignore_index(self):
        topic_filename = 'mytopic'
        index_filename = 'topic-tags.json'
        source_files = []
        source_files.append(self.file_creator.create_file(topic_filename, ''))
        index_file = self.file_creator.create_file(index_filename, '')
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(index_file=index_file,
                                       topic_dir=topic_dir)
        self.assertCountEqual(
            self.topic_tag_db.get_all_topic_src_files(),
            source_files
        )

    def test_get_all_topic_source_files_ignore_hidden(self):
        topic_filename = 'mytopic'
        hidden_filename = '.' + topic_filename
        source_files = []
        source_files.append(self.file_creator.create_file(topic_filename, ''))
        self.file_creator.create_file(hidden_filename, '')
        topic_dir = self.file_creator.rootdir
        self.topic_tag_db = TopicTagDB(topic_dir=topic_dir)
        self.assertCountEqual(
            self.topic_tag_db.get_all_topic_src_files(),
            source_files
        )

    def test_get_tag_value_all_tags(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)

        # Check the title get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'title')
        self.assertEqual(value, ['My First Topic Title'])

        # Check the description get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'description')
        self.assertEqual(value, ['This describes my first topic'])

        # Check the category get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'category')
        self.assertEqual(value, ['General Topics'])

        # Check the related command get tag value
        value = self.topic_tag_db.get_tag_value(topic_name,
                                                'related command')
        self.assertEqual(value, ['aws s3'])

        # Check the related topic get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
        self.assertEqual(value, ['topic-name-2'])

    def test_get_tag_multi_value(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'related topic': ['foo', 'bar']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        # Check the related topic get tag value
        value = self.topic_tag_db.get_tag_value(topic_name, 'related topic')
        self.assertEqual(value, ['foo', 'bar'])

    def test_get_tag_topic_no_exists(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'related topic': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value('no-exist', 'related topic')
        self.assertEqual(value, None)

    def test_get_tag_no_exist_tag(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'related topic': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value(topic_name, ':foo:')
        self.assertEqual(value, None)

    def test_get_tag_no_exist_use_default(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'related topic': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value('no-exist', ':foo:', [])
        self.assertEqual(value, [])

    def test_get_tag_single_value(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')
        self.assertEqual(value, 'foo')

    def test_get_tag_single_value_exception(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['foo', 'bar']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        with self.assertRaises(ValueError):
            self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')

    def test_get_tag_single_value_no_exists(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_single_value(
            'topic-name-1', ':title:')
        self.assertEqual(value, None)

    def test_load_and_save_json_index(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics', 'S3'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            },
            'topic-name-2': {
                'title': ['My Second Topic Title'],
                'description': ['This describes my second topic'],
                'category': ['General Topics'],
                'related topic': ['topic-name-1']
            }
        }

        json_index = self.file_creator.create_file('index.json', '')

        # Create a JSON index to be loaded.
        tag_json = json.dumps(tag_dict, indent=4, sort_keys=True)
        with open(json_index, 'w') as f:
            f.write(tag_json)

        # Load the JSON index.
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.load_json_index()

        # Write the loaded json to disk and ensure it is as expected.
        saved_json_index = self.file_creator.create_file('index2.json', '')
        self.topic_tag_db.index_file = saved_json_index

        self.topic_tag_db.save_to_json_index()
        with open(saved_json_index, 'r') as f:
            self.assertEqual(f.read(), tag_json)
Ejemplo n.º 56
0
 def __init__(self, help_command):
     self.help_command = help_command
     self.register(help_command.session, help_command.event_class)
     self.help_command.doc.translation_map = self.build_translation_map()
     self._topic_tag_db = TopicTagDB()
     self._topic_tag_db.load_json_index()
Ejemplo n.º 57
0
 def test_topic_dir(self):
     self.topic_tag_db = TopicTagDB(topic_dir='foo')
     self.assertEqual(self.topic_tag_db.topic_dir, 'foo')
     self.topic_tag_db.topic_dir = 'bar'
     self.assertEqual(self.topic_tag_db.topic_dir, 'bar')