From 40bbbd091ae330d572408613f3efdc82b3cb6a4e Mon Sep 17 00:00:00 2001 From: JCTutorius Date: Thu, 19 Nov 2009 19:19:17 +0000 Subject: Implemented the query() vault function with search parameters. --- diff --git a/tests/vaulttests.py b/tests/vaulttests.py index e8e732e..729d36d 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -92,7 +92,7 @@ class VaultInterfaceTest(unittest.TestCase): ini_file2.write('guid=' + str(self.test_guid2) + '\n') ini_file2.write('name=TestTutorial2\n') ini_file2.write('version=2\n') - ini_file2.write('description=This is a test tutorial 2\n') + ini_file2.write('description=This is a test tutorial 2. FOR_TEST\n') ini_file2.write('rating=4\n') ini_file2.write('category=Test2\n') ini_file2.write('publish_state=false\n') @@ -125,7 +125,7 @@ class VaultInterfaceTest(unittest.TestCase): self.test_metadata_dict['publish_state'] = 'false' activities_dict = {} activities_dict['org.laptop.tutoriusactivity'] = '1' - activities_dict['org.laptop,writus'] = '1' + activities_dict['org.laptop.writus'] = '1' self.test_metadata_dict['activities'] = activities_dict @@ -142,7 +142,7 @@ class VaultInterfaceTest(unittest.TestCase): shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp')) os.makedirs(test_path) - # Creat a dummy tutorial .xml file + # Create a dummy tutorial .xml file serializer = XMLSerializer() with file(os.path.join(test_path, 'tutorial.xml'), 'w') as fsmfile: @@ -159,7 +159,7 @@ class VaultInterfaceTest(unittest.TestCase): zout = zipfile.ZipFile(os.path.join(test_path, zfilename), "w") for fname in archive_list: fname_splitted = fname.rsplit('/') - file_only_name = fname_splitted[fname_splitted.__len__() - 1] + file_only_name = fname_splitted[-1] zout.write(fname, file_only_name) zout.close() @@ -186,12 +186,10 @@ class VaultInterfaceTest(unittest.TestCase): correspond to the specified parameters. """ - # Note : Temporary only test query that return ALL tutorials in the vault. - # TODO : Test with varying parameters - + # Test the return of all the tutorials tutorial_list = Vault.query() - if tutorial_list.__len__() < 2: + if len(tutorial_list) < 2: assert False, 'Error, list doesnt have enough tutorial in it : ' + str(tutorial_list.__len__()) + ' element' for tuto_dictionnary in tutorial_list: @@ -208,7 +206,7 @@ class VaultInterfaceTest(unittest.TestCase): elif tuto_dictionnary['name'] == 'TestTutorial2': related = tuto_dictionnary['activities'] assert tuto_dictionnary['version'] == '2' - assert tuto_dictionnary['description'] == 'This is a test tutorial 2' + assert tuto_dictionnary['description'] == 'This is a test tutorial 2. FOR_TEST' assert tuto_dictionnary['rating'] == '4' assert tuto_dictionnary['category'] == 'Test2' assert tuto_dictionnary['publish_state'] == 'false' @@ -218,7 +216,55 @@ class VaultInterfaceTest(unittest.TestCase): else: assert False, 'list is empty or name property is wrong' - + + # Test the return of just a given tutorial with a given keyword present in one + # or more of is metadata fields. + tutorial_list = Vault.query(keyword=['FOR_TEST']) + + # Should return only tutorial with metadata like tutorial # 2 + if len(tutorial_list) == 0: + assert False, 'Error on keyword, at least 1 tutorial should has been returned. ' + related = tutorial_list[0]['activities'] + assert tutorial_list[0]['version'] == '2' + assert tutorial_list[0]['description'] == 'This is a test tutorial 2. FOR_TEST' + assert tutorial_list[0]['rating'] == '4' + assert tutorial_list[0]['category'] == 'Test2' + assert tutorial_list[0]['publish_state'] == 'false' + assert related.has_key('org.laptop.tutoriusactivity') + assert related.has_key('org.laptop.writus') + assert related.has_key('org.laptop.testus') + + # Test the return of just a given tutorial with a given related activity in is metadata + tutorial_list = Vault.query(relatedActivityNames=['org.laptop.testus']) + + # Should return only tutorials like tutorial # 2 + if len(tutorial_list) == 0: + assert False, 'Error on related activity, at least 1 tutorial should has been returned. ' + related = tutorial_list[0]['activities'] + assert tutorial_list[0]['version'] == '2' + assert tutorial_list[0]['description'] == 'This is a test tutorial 2. FOR_TEST' + assert tutorial_list[0]['rating'] == '4' + assert tutorial_list[0]['category'] == 'Test2' + assert tutorial_list[0]['publish_state'] == 'false' + assert related.has_key('org.laptop.tutoriusactivity') + assert related.has_key('org.laptop.writus') + assert related.has_key('org.laptop.testus') + + # Test the return of just a given tutorial with a given category in is metadata + tutorial_list = Vault.query(category=['test']) + + # Should return only tutorials like tutorial # 1 + if len(tutorial_list) == 0: + assert False, 'Error on category, at least 1 tutorial should has been returned. ' + related = tutorial_list[0]['activities'] + assert tutorial_list[0]['version'] == '1' + assert tutorial_list[0]['description'] == 'This is a test tutorial 1' + assert tutorial_list[0]['rating'] == '3.5' + assert tutorial_list[0]['category'] == 'Test' + assert tutorial_list[0]['publish_state'] == 'false' + assert related.has_key('org.laptop.tutoriusactivity') + assert related.has_key('org.laptop.writus') + def test_loadTutorial(self): """ diff --git a/tutorius/vault.py b/tutorius/vault.py index fbe9e75..a3b84a4 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -56,6 +56,7 @@ INI_GUID_PROPERTY = "guid" INI_NAME_PROPERTY = "name" INI_XML_FSM_PROPERTY = "fsm_filename" INI_VERSION_PROPERTY = 'version' +INI_CATEGORY_PROPERTY = 'category' INI_FILENAME = "meta.ini" TUTORIAL_FILENAME = "tutorial.xml" RESOURCES_FOLDER = 'resources' @@ -196,7 +197,6 @@ class Vault(object): @returns a list of Tutorial meta-data (TutorialID, Description, Rating, Category, PublishState, etc...) - TODO : Search for tuto caracterised by the entry : OR between [], and between each The returned dictionnary is of this format : key = property name, value = property value The dictionnary also contain one dictionnary element whose key is the string 'activities' @@ -204,25 +204,25 @@ class Vault(object): value = related activity version number """ - # Temp solution for returning all tutorials metadata - tutorial_list = [] tuto_guid_list = [] ini_file = SafeConfigParser() - if keyword == [] and relatedActivityNames == [] and category == []: - # get all tutorials tuples (name:guid) for all activities and version - tuto_dict = Vault.list_available_tutorials() - for id in tuto_dict.keys(): - tuto_guid_list.append(id) + # get all tutorials tuples (name:guid) for all activities and version + tuto_dict = Vault.list_available_tutorials() + for id in tuto_dict.keys(): + tuto_guid_list.append(id) - # Find .ini metadata files with the guid list + # Find .ini metadata files with the guid list # Get the guid from the tuto tuples for guid in tuto_guid_list: + addition_flag = True # Create a dictionnary containing the metadata and also - # another dictionnary containing the tutorial Related Acttivities, + # another dictionnary containing the tutorial Related Activities, # and add it to a list + ini_file = SafeConfigParser() + # Create a TutorialBundler object from the guid bundler = TutorialBundler(guid) # Find the .ini file path for this guid @@ -233,6 +233,7 @@ class Vault(object): metadata_dictionnary = {} related_act_dictionnary = {} metadata_list = ini_file.options(INI_METADATA_SECTION) + for metadata_name in metadata_list: # Create a dictionnary of tutorial metadata metadata_dictionnary[metadata_name] = ini_file.get(INI_METADATA_SECTION, metadata_name) @@ -245,8 +246,45 @@ class Vault(object): # Add Related Activities dictionnary to metadata dictionnary metadata_dictionnary['activities'] = related_act_dictionnary - # Add this dictionnary to tutorial list - tutorial_list.append(metadata_dictionnary) + # Filter tutorials for keyword (full or partial) + if keyword != []: + addition_flag = False + # Check if at least one keyword of the list is present + for key in keyword: + for value in metadata_dictionnary.values(): + if isinstance(value, str) == True: + if value.lower().count(key.lower()) > 0: + addition_flag = True + else: + # Check one layer of depth in the metadata to find the keyword + # (for exemple, related activites are a dictionnary stocked + # in a value of the main dictionnary) + if isinstance(value, dict) == True: + for inner_key, inner_value in value.items(): + if isinstance(inner_value, str) == True and isinstance(inner_key, str) == True: + if inner_value.lower().count(key.lower()) > 0 or inner_key.count(key.lower()) > 0: + addition_flag = True + + # Filter tutorials for related activities + if relatedActivityNames != []: + addition_flag = False + # Check if at least one element of the list is present + for related in relatedActivityNames: + if related.lower() in related_act_dictionnary.keys(): + addition_flag = True + + # Filter tutorials for categories + if category != []: + addition_flag = False + # Check if at least one element of the list is present + for cat in category: + if metadata_dictionnary.has_key(INI_CATEGORY_PROPERTY): + if metadata_dictionnary[INI_CATEGORY_PROPERTY].lower() == cat.lower(): + addition_flag = True + + # Add this dictionnary to tutorial list if it has not been filtered + if addition_flag == True: + tutorial_list.append(metadata_dictionnary) # Return tutorial list return tutorial_list @@ -319,9 +357,6 @@ class Vault(object): else: # Error, tutorial already exist return False - - # TODO : wait for Ben input on how to unpublish tuto before coding this function - # For now, no unpublishing will occur. @staticmethod -- cgit v0.9.1