def test_query_entity_properties(self): topic = Topic({"name": "Test Title"}) expected_query = """{{#ask:[[Concept:Property]][[Property topic::Concept:Test Title]] |?Property name=name |?Property label=label |?Property type=type |?Property index=index |?Property sortPos=sortPos |?Property primaryKey=primary key |?Property mandatory=mandatory |?Property namespace=namespace |?Property size=size |?Property uploadable=uploadable |?Property defaultValue=default |?Property inputType=inputType |?Property allowedValues=allowedValues |?Property documentation=documentation |?Property values_from=values from |?Property showInGrid=showInGrid |?Property isLink=isLink |?Property nullable=allow nulls? |format=wikitable }}""" actual_query = topic.query_entity_properties() self.assertEqual(expected_query, actual_query)
def test_set_entity_parameter(self): exp_oneliner = "{{#set:isA=Event|Event title={{{EventTitle|}}}|Event year={{{EventYear|}}}}}" exp_pretty = "{{#set:isA=Event\n|Event title={{{EventTitle|}}}\n|Event year={{{EventYear|}}}\n}}" event = Topic({"name": "Event", "pluralName": "Events"}) event = Topic({"pageTitle": "Concept:Event", "name": "Event", "pluralName": "Events"}) properties = [Property({"pageTitle": "Property:Event title", "name": "EventTitle", "label": "Title"}), Property({"pageTitle": "Property:Event year", "name": "EventYear", "label": "Year"})] self.assertEqual(exp_oneliner, SMW.set_entity_parameter(event, properties, oneliner=True)) self.assertEqual(exp_pretty, SMW.set_entity_parameter(event, properties, oneliner=False))
def test_query_topic_overview(self): topic = Topic({"name": "Test Title"}) expected_query = """{{#ask:[[Topic name::Test Title]] |mainlabel=- |?Topic icon=icon |?=Topic |?Topic pluralName=pluralName |?Topic documentation=documentation }}""" actual_query = topic.query_topic_overview() self.assertEqual(expected_query, actual_query)
def table_of_arguments(topic: Topic, properties: list, clickable_links=True): """ ToDo Args: topic: properties: Returns: """ formlink = Form.formlink(form=topic.name, link_text="✎", target="{{FULLPAGENAME}}", tooltip="Start editing this " + topic.name) table_str = "{| class='wikitable'\n! colspan=2 | " + formlink + topic.get_page_link( ) + "\n|-\n" sortBySortPos = lambda property: 99999 if "sortPos" not in property.__dict__ or property.__dict__.get( "sortPos") is None else int(property.sortPos) properties_sorted = sorted(properties, key=sortBySortPos) for property in properties_sorted: # ToDo: refactor Table definition label = property.get_description_page_link( ) if clickable_links else property.label table_str += "!style=\"text-align:left\" |" + label + "\n" table_str += "| {{#if:" + Template.template_arg( property.name) + "|" + Template.template_arg( property.name) + "|}}\n" table_str += "|-\n" return table_str + "|}"
def test_get_related_topics(self): # Test data containing one incoming and one outgoing edge from related topics data_properties = [{ "Property": "Property:Task goals", "name": "Task goals", "label": "Goals", "type": "Special:Types/Page", "inputType": "tokens", "values_from": "concept=Goal", "topic": "Concept:Task" }, { "Property": "Property:Task ", "name": "Task description", "label": "Goals", "type": "Special:Types/Page", "inputType": "tokens", "values_from": "property=Task description", "topic": "Concept:Task" }, { "Property": "Property:Project tasks", "name": "Project tasks", "label": "Goals", "type": "Special:Types/Page", "inputType": "tokens", "values_from": "concept=Task", "topic": "Concept:Project" }] properties = [Property(x) for x in data_properties] rel_topic_names = Topic({ "name": "Task", "pluralName": "Tasks" }).get_related_topics(properties) self.assertTrue(len(rel_topic_names) == 2) for name in rel_topic_names: self.assertTrue(name in ["Project", "Goal"])
def test_get_page_name(self): topic = Topic(topic_properties={"name": "Task", "pluralName": "Tasks"}) smwParts = SMWPart.getAll(None) all_page_names = ["Category:Task", "Concept:Task", "Help:Task", "Template:Task", "Form:Task", "List of Tasks"] for part, smwPart in smwParts.items(): page_name = smwPart.get_page_name(topic) self.assertTrue(page_name in all_page_names) all_page_names.remove(page_name)
def test_render_sample_entity_with_properties(self): exp_oneliner = "{{Event|EventTitle=Some Title|EventYear=Some Year}}" exp_pretty = "{{Event\n|EventTitle=Some Title\n|EventYear=Some Year\n}}" event = Topic({"pageName": "Concept:Event", "name": "Event", "pluralName": "Events"}) properties = [Property({"pageName": "Property:Event title", "name": "EventTitle", "label": "Title"}), Property({"pageName": "Property:Event year", "name": "EventYear", "label": "Year"})] self.assertEqual(exp_oneliner, SMW.render_sample_entity_with_properties(event, properties)) self.assertEqual(exp_pretty, SMW.render_sample_entity_with_properties(event, properties, oneliner=False))
def test_getAllAsPageLink(self): expected_str = """*[[:List of Tests]] *[[:Help:Test]] *[[:Category:Test]] *[[:Concept:Test]] *[[:Form:Test]] *[[:Template:Test]] """ self.assertEqual(expected_str, SMWPart.getAllAsPageLink(Topic({"name": "Test", "pluralName": "Tests"})))
def test_render_entity(self): data = { "name": "Topic", "pluralName": "Topics", "icon": "File:Topic_icon.png", "iconUrl": "/images/a/ae/Index.png", "documentation": "A Topic is a Concept/Class/Thing/Entity", "wikiDocumentation": "A Topic is a Concept/Class/Thing/Entity", "context": "MetaModel" } exp_template_oneliner = "{{Topic|name=Topic|pluralName=Topics|icon=File:Topic_icon.png|iconUrl=/images/a/ae/Index.png|documentation=A Topic is a Concept/Class/Thing/Entity|wikiDocumentation=A Topic is a Concept/Class/Thing/Entity|context=MetaModel}}" exp_template_pretty = exp_template_oneliner.replace("|", "\n|").replace( "}}", "\n}}") topic = Topic(data) self.assertEqual(exp_template_oneliner, topic.render_entity(oneliner=True)) self.assertEqual(exp_template_pretty, topic.render_entity(oneliner=False))
def testTopicFromJson(self): ''' test loading/constructing from Json ''' jsonStr = """{ "data": [{ "pageTitle": "Concept:Commit", "name": "Commit", "pluralName": "Commits", "documentation": "A Git commit", "wikiDocumentation": "see https://git-scm.com/docs/git-commit", "cargo": "true" }] }""" topic = Topic.from_wiki_json(jsonStr) self.assertTrue(isinstance(topic, Topic)) self.assertEqual("Commits", topic.pluralName) self.assertEqual("Commit", topic.name) self.assertTrue(topic.cargo) # Test with properties json_props = """{ "data":[ { "name":"property 1" }, { "name":"property 2" }, { "name":"property 3" } ]}""" prop_list = ["property 1", "property 2", "property 3"] topic_with_props = Topic.from_wiki_json(jsonStr, json_props) self.assertTrue(len(topic_with_props.properties) == 3) # Check if each item in properties is a Property and corresponds to exactly one of the given property configs for p in topic_with_props.properties: self.assertTrue(isinstance(p, Property)) self.assertTrue(p.name in prop_list) prop_list.remove(p.name)
def test_from_wiki_json(self): raw_data = { "data": [{ "name": "Topic", "pluralName": "Topics", "listLimit": 42, "cargo": "f" }] } topic = Topic.from_wiki_json(json.dumps(raw_data), None) self.assertEqual("Topic", topic.name) self.assertEqual("Topics", topic.pluralName) self.assertTrue(topic.listLimit == 42) self.assertFalse(topic.cargo) topic_with_properties = Topic.from_wiki_json( json.dumps(raw_data), json.dumps({"data": [{ "name": "prop1" }, { "name": "prop2" }]})) self.assertTrue(len(topic_with_properties.properties) == 2)
def getTopics(self, wikiId: str) -> list: ''' Args: wikiId: id of the wiki the topic should be queried from Returns: Topics that are present at the given wiki ''' # ToDo: wikiuser should be created based on the site user login info res = WikiPush(fromWikiId=wikiId).formatQueryResult( askQuery= "{{#ask:[[isA::Topic]]|?Topic name=name|?Topic pluralName=pluralName}}" ) return [Topic(x) for x in res]
def testMetaModel(self): ''' test the meta model ''' context = Context({"name": "MetaModel"}) self.assertEqual("MetaModel", context.name) topic = None # try: # topic=Topic({"name":"Context","label":"Context"}) # self.fail("there should be an exception") # except Exception as ex: # self.assertTrue("Invalid property 'label'" in str(ex)) self.assertIsNone(topic) topic = Topic({"name": "Context", "pluralName": "Contexts"}) self.assertEqual("Context", topic.name) self.assertEqual("Contexts", topic.pluralName) prop = Property({"name": "name", "label": "name"}) self.assertEqual("name", prop.name) self.assertEqual("name", prop.label) self.assertIsNone(prop.type) pass
def main(self, argv=None): if argv is None: argv = sys.argv # Modes of operation UPDATE_TEMPLATES_MODE = "update_templates" CREATE_FILE_MODE = "create" GENERATE_ENTITY_PAGES = "generate_entity_pages" modes = [ UPDATE_TEMPLATES_MODE, CREATE_FILE_MODE, GENERATE_ENTITY_PAGES ] # Setup argument parser self.getParser() try: # Process arguments args = self.parser.parse_args(argv) self.debug = args.debug if args.mode not in modes: raise Exception( f"Please select of of the operation modes: {modes}") if args.mode == GENERATE_ENTITY_PAGES: # Check if necessary parameters are set if not args.properties_file or not args.topic_file: raise Exception( "The parameters --properties and --topics must be defined for this mode" ) if args.templates_folder: # update templateEnv self.template_env = self.getTemplateEnv( additional_loader=args.template_folder) properties_json = "" print(1) with open(args.properties_file) as json_file: properties_json = json_file.read() topic_json = "" with open(args.topic_file) as json_file: topic_json = json_file.read() topic = Topic.from_wiki_json(topic_json=topic_json, prop_json=properties_json) # Generate and save the entity pages self.generateTopic(topic, args.backupPath, args.overwrite) elif args.mode == UPDATE_TEMPLATES_MODE: data = {} if "data_input" in args: with open(args.data_input) as json_file: data_str = json_file.read() data = json.loads(data_str) elif args.stdin: data = json.load(sys.stdin) else: pass template_data = data.get(args.template) exclude_keys = ["pageTitle"] self.update_or_create_templates(template_data, name_id="pageTitle", template_name=args.template, exclude_keys=exclude_keys, backup_path=args.backupPath, overwrite=args.overwrite) if args.mode == CREATE_FILE_MODE: # ToDo pass except KeyboardInterrupt: ### handle keyboard interrupt ### return 1 except Exception as e: raise e
def test_query_examples(self): topic = Topic({"name": "Test Title"}) expected_query = "{{#ask:[[Concept:Test Title]]|limit=10|searchlabel=...more examples}}" actual_query = topic.query_examples(oneliner=True) self.assertEqual(expected_query, actual_query)
def test_get_page_name(self): self.assertEqual("Form:Task", Form.get_page_name(Topic(topic_properties={"name": "Task", "pluralName": "Tasks"})))
def testWikiRender(self): if self.inPublicCI(): return topicJson = """{"data": [{"pageName": "Concept:Task", "name": "Task", "pluralName": "Tasks", "documentation": "Problem or issue that needs to be solved", "wikiDocumentation": "Problem or issue that needs to be solved"}]} """ propJson = """{ "data": [{ "pageName": "Property:Task goals", "name": "Task goals", "label": "Goals", "type": null, "index": null, "sortPos": null, "primaryKey": "f", "mandatory": "f", "namespace": null, "size": null, "uploadable": "f", "defaultValue": null, "inputType": "textarea", "allowedValues": null, "documentation": "Goals that must be achieved to complete the task", "values_from": null, "showInGrid": "f", "isLink": "f", "nullable": "f", "topic": "Concept:Task", "regexp": null }, { "pageName": "Property:Task procedure", "name": "Task procedure", "label": "Procedure", "type": null, "index": null, "sortPos": null, "primaryKey": "f", "mandatory": "f", "namespace": null, "size": null, "uploadable": "f", "defaultValue": null, "inputType": "textarea", "allowedValues": null, "documentation": "Explanation or hints how the task should be solved.", "values_from": null, "showInGrid": "f", "isLink": "f", "nullable": "f", "topic": "Concept:Task", "regexp": null }, { "pageName": "Property:Task task", "name": "Task task", "label": "Task", "type": null, "index": null, "sortPos": null, "primaryKey": "f", "mandatory": "f", "namespace": null, "size": null, "uploadable": "f", "defaultValue": null, "inputType": "text", "allowedValues": null, "documentation": "Description of the task defining briefly the topic of the task.", "values_from": null, "showInGrid": "f", "isLink": "f", "nullable": "f", "topic": "Concept:Task", "regexp": null }, { "pageName": "Property:Task workpackage", "name": "Task workpackage", "label": "assigned to", "type": "Special:Types/Page", "index": null, "sortPos": null, "primaryKey": "f", "mandatory": "f", "namespace": null, "size": null, "uploadable": "f", "defaultValue": null, "inputType": null, "allowedValues": null, "documentation": "Workpackage the task is assigned to", "values_from": "concept=Workpackage", "showInGrid": "f", "isLink": "f", "nullable": "f", "topic": "Concept:Task", "regexp": null }] }""" topic = Topic.from_wiki_json(topicJson, propJson) self.assertEqual(4, len(topic.properties)) wikiRender = WikiRender(debug=True) wikiRender.generateTopic(topic, path="/tmp/wikirender/unittest", overwrite=True) pass
def test_get_page_link(self): topic = Topic({"name": "Topic"}) self.assertEqual("[[Concept:Topic|Topic]]", topic.get_page_link()) topic.__dict__.update(pageTitle="Concept:Topic") self.assertEqual("[[Concept:Topic|Topic]]", topic.get_page_link())