def create_tree(): script_dir = os.path.dirname(__file__) data_file = "topics.dat" abs_file_path = os.path.join(script_dir, data_file) topics_file = open(abs_file_path, "r") stack = {} for line in topics_file: normalized_tags = None index = int(line.count(SPACE) / SPACES_PER_TAB) topic_data = line.strip().split(";") if len(topic_data) < 1 or len(topic_data) > 4: raise TopicImportError("Invalid topic data") topic_identifier = slugify(str(topic_data[IDENTIFIER])) topic_instance_of = "topic" if len(topic_data) == 1: # Only identifier provided topic_name = normalize_topic_name(topic_identifier) elif len(topic_data) == 2: # Both identifier and name is provided topic_name = topic_data[NAME] if topic_data[ NAME] else normalize_topic_name(topic_identifier) elif len(topic_data ) == 3: # Identifier, name and type (instance of) is provided topic_name = topic_data[NAME] if topic_data[ NAME] else normalize_topic_name(topic_identifier) topic_instance_of = slugify( str(topic_data[INSTANCE_OF] )) if topic_data[INSTANCE_OF] else "topic" # All parameters have been provided: identifier, name, type and one or more (comma-separated) tags else: topic_name = topic_data[NAME] if topic_data[ NAME] else normalize_topic_name(topic_identifier) topic_instance_of = slugify( str(topic_data[INSTANCE_OF] )) if topic_data[INSTANCE_OF] else "topic" normalized_tags = ",".join( [slugify(str(tag)) for tag in topic_data[TAGS].split(",")]) topic = Topic(topic_identifier, topic_instance_of, topic_name) if normalized_tags: tags_attribute = Attribute("tags", normalized_tags, topic.identifier, data_type=DataType.STRING) topic.add_attribute(tags_attribute) stack[index] = topic_identifier if index == 0: # Root node tree.add_node( topic_identifier, node_type="identifier", edge_type="relationship", payload=topic, ) else: tree.add_node( topic_identifier, parent_pointer=stack[index - 1], node_type="identifier", edge_type="relationship", payload=topic, )
def test_topic(): topic1 = Topic(identifier="test-topic1", base_name="Test Topic 1", language=Language.SPA) # Instantiate and open topic store. with TopicStore(database_username, database_password, dbname=database_name) as store: # Persist topic to store. if not store.topic_exists(TOPIC_MAP_IDENTIFIER, "test-topic1"): store.set_topic(TOPIC_MAP_IDENTIFIER, topic1) # Retrieve topic from store. topic2 = store.get_topic( TOPIC_MAP_IDENTIFIER, "test-topic1", resolve_attributes=RetrievalMode.RESOLVE_ATTRIBUTES, ) assert topic2.identifier == "test-topic1" assert topic2.instance_of == "topic" assert len(topic2.base_names) == 1 assert topic2.first_base_name.name == "Test Topic 1" assert topic2.first_base_name.language is Language.SPA assert len(topic2.attributes) == 1 assert len(topic2.occurrences) == 0
def initialise(self, map_identifier: int, user_identifier: int) -> None: base_topics = { ("event", "Event"), ("thing", "Thing"), ("participant", "Participant"), ("place", "Place"), ("included-in", "Included In"), ("includes", "Includes"), ("cause", "Cause"), ("effect", "Effect"), ("temporal-navigation", "Temporal Navigation"), ("spatial-navigation", "Spatial Navigation"), ("from", "From"), ("to", "To"), } for item in base_topics: topic = Topic( identifier=item[TopicField.IDENTIFIER.value], name=item[TopicField.BASE_NAME.value], ) self.topic_store.set_topic(map_identifier, topic, TaxonomyMode.LENIENT) self.topic_store.initialise_topic_map(map_identifier, user_identifier)
def test_topic(): topic1 = Topic(identifier='test-topic1', base_name='Test Topic 1', language=Language.SPA) # Instantiate and open topic store. store = TopicStore(username, password) store.open() # Persist topic to store. if not store.topic_exists(TOPIC_MAP_IDENTIFIER, 'test-topic1'): store.set_topic(TOPIC_MAP_IDENTIFIER, topic1) # Retrieve topic from store. topic2 = store.get_topic(TOPIC_MAP_IDENTIFIER, 'test-topic1', resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES) store.close() assert topic2.identifier == 'test-topic1' assert topic2.instance_of == 'topic' assert len(topic2.base_names) == 1 assert topic2.first_base_name.name == 'Test Topic 1' assert topic2.first_base_name.language is Language.SPA assert len(topic2.attributes) == 1 assert len(topic2.occurrences) == 0
def test_init_topic1(): topic1 = Topic(identifier="test-topic1") assert topic1.identifier == "test-topic1" assert topic1.instance_of == "topic" assert len(topic1.base_names) == 1 assert topic1.first_base_name.name == "Undefined" assert topic1.first_base_name.language is Language.ENG assert len(topic1.attributes) == 0 assert len(topic1.occurrences) == 0
def create_topics(store, topic_map_identifier): for node in tree.traverse(ROOT_TOPIC, mode=TraversalMode.DEPTH): # Create the 'instance_of' topic if it doesn't already exist instance_of_topic = Topic( node.payload.instance_of, "topic", normalize_topic_name(node.payload.instance_of), ) store_topic(store, topic_map_identifier, instance_of_topic) # Create the actual node topic store_topic(store, topic_map_identifier, node.payload)
def test_init_topic2(): topic2 = Topic(identifier='test-topic2', instance_of='test', base_name='Test Topic 2', language=Language.SPA) assert topic2.identifier == 'test-topic2' assert topic2.instance_of == 'test' assert len(topic2.base_names) == 1 assert topic2.first_base_name.name == 'Test Topic 2' assert topic2.first_base_name.language is Language.SPA assert len(topic2.attributes) == 0 assert len(topic2.occurrences) == 0
def test_init_topic2(): topic2 = Topic( identifier="test-topic2", instance_of="test", name="Test Topic 2", language=Language.SPA, ) assert topic2.identifier == "test-topic2" assert topic2.instance_of == "test" assert len(topic2.base_names) == 1 assert topic2.first_base_name.name == "Test Topic 2" assert topic2.first_base_name.language is Language.SPA assert len(topic2.attributes) == 0 assert len(topic2.occurrences) == 0
def set_character(self, topic_map_identifier, character, scene_identifier): topic = Topic(character.identifier, character.instance_of, character.name) self.topic_store.set_topic(topic_map_identifier, topic) description_attribute = Attribute( 'description', character.description, topic.identifier) if character.description else None location_attribute = Attribute('location', character.location, topic.identifier) rotation_attribute = Attribute('rotation', character.rotation, topic.identifier) scale_attribute = Attribute('scale', character.scale, topic.identifier) attributes = [ x for x in [ description_attribute, location_attribute, rotation_attribute, scale_attribute ] if x is not None ] self.topic_store.set_attributes(topic_map_identifier, attributes) for asset in character.assets: occurrence = Occurrence(instance_of=asset.instance_of, topic_identifier=topic.identifier, resource_ref=asset.reference, resource_data=asset.data) self.topic_store.set_occurrence(topic_map_identifier, occurrence) scene_association = Association( instance_of='character', src_topic_ref=topic.identifier, # The character's reference. dest_topic_ref=scene_identifier, src_role_spec='included-in', dest_role_spec='includes') self.topic_store.set_association(topic_map_identifier, scene_association) book_association = Association( instance_of='character', src_topic_ref=topic.identifier, # The character's reference. dest_topic_ref='genesis', src_role_spec='included-in', dest_role_spec='includes', scope='book') self.topic_store.set_association(topic_map_identifier, book_association)
def set_scene(self, topic_map_identifier, scene): topic = Topic(scene.identifier, scene.instance_of, scene.name) self.topic_store.set_topic(topic_map_identifier, topic) description_attribute = Attribute( 'description', scene.description, topic.identifier) if scene.description else None location_attribute = Attribute('location', scene.location, topic.identifier) rotation_attribute = Attribute('rotation', scene.rotation, topic.identifier) scale_attribute = Attribute('scale', scene.scale, topic.identifier) ordinal_attribute = Attribute( 'ordinal', scene.ordinal, topic.identifier) if scene.ordinal else None attributes = [ x for x in [ description_attribute, location_attribute, rotation_attribute, scale_attribute, ordinal_attribute ] if x is not None ] self.topic_store.set_attributes(topic_map_identifier, attributes) for asset in scene.assets: occurrence = Occurrence(instance_of=asset.instance_of, topic_identifier=topic.identifier, resource_ref=asset.reference, resource_data=asset.data) self.topic_store.set_occurrence(topic_map_identifier, occurrence) book_association = Association( instance_of='scene', src_topic_ref=topic.identifier, # The scene's reference. dest_topic_ref='genesis', src_role_spec='included-in', dest_role_spec='includes', scope='book') self.topic_store.set_association(topic_map_identifier, book_association)
def create_topic(map_identifier): topic_store = get_topic_store() topic_map = topic_store.get_topic_map(map_identifier, current_user.id) if topic_map is None: return jsonify({"status": "error", "code": 404}), 404 if request.method == "POST": topic_identifier = request.form["topic-identifier"].strip() topic_name = request.form["topic-name"].strip() if not topic_name: topic_name = "Undefined" if topic_store.topic_exists(topic_map.identifier, topic_identifier): return jsonify({"status": "error", "code": 409}), 409 else: topic = Topic(topic_identifier, "topic", topic_name) text_occurrence = Occurrence( instance_of="text", topic_identifier=topic.identifier, scope= UNIVERSAL_SCOPE, # TODO: Should it be session["current_scope"]? resource_data="Topic automatically created.", ) timestamp = str(datetime.now()) modification_attribute = Attribute( "modification-timestamp", timestamp, topic.identifier, data_type=DataType.TIMESTAMP, ) # Persist objects to the topic store topic_store.set_topic(topic_map.identifier, topic) topic_store.set_occurrence(topic_map.identifier, text_occurrence) topic_store.set_attribute(topic_map.identifier, modification_attribute) return jsonify({"status": "success", "code": 201}), 201
def convert(map_identifier, note_identifier): topic_store = get_topic_store() topic_map = topic_store.get_topic_map(map_identifier) if topic_map is None: abort(404) if current_user.id != topic_map.user_identifier: abort(403) topic = topic_store.get_topic( map_identifier, "home", resolve_attributes=RetrievalMode.RESOLVE_ATTRIBUTES) if topic is None: abort(404) note_occurrence = topic_store.get_occurrence( map_identifier, note_identifier, inline_resource_data=RetrievalMode.INLINE_RESOURCE_DATA, resolve_attributes=RetrievalMode.RESOLVE_ATTRIBUTES, ) note_title = note_occurrence.get_attribute_by_name("title").value form_topic_name = "" form_topic_identifier = "" form_topic_text = "## " + note_title + "\n" + note_occurrence.resource_data.decode( ) form_topic_instance_of = "topic" error = 0 if request.method == "POST": form_topic_identifier = request.form["topic-identifier"].strip() form_topic_name = request.form["topic-name"].strip() form_topic_text = request.form["topic-text"] form_topic_instance_of = request.form["topic-instance-of"].strip() # If no values have been provided set their default values if not form_topic_instance_of: form_topic_instance_of = "topic" # Validate form inputs if not form_topic_name: error = error | 1 if topic_store.topic_exists(topic_map.identifier, form_topic_identifier): error = error | 2 if not form_topic_identifier: error = error | 4 if not topic_store.topic_exists(topic_map.identifier, form_topic_instance_of): error = error | 8 if error != 0: flash( "An error occurred when submitting the form. Please review the warnings and fix accordingly.", "warning", ) else: new_topic = Topic(form_topic_identifier, form_topic_instance_of, form_topic_name) text_occurrence = Occurrence( instance_of="text", topic_identifier=new_topic.identifier, resource_data=form_topic_text, ) timestamp = str(datetime.now()) modification_attribute = Attribute( "modification-timestamp", timestamp, new_topic.identifier, data_type=DataType.TIMESTAMP, ) # Persist objects to the topic store topic_store.set_topic(topic_map.identifier, new_topic) topic_store.set_occurrence(topic_map.identifier, text_occurrence) topic_store.set_attribute(topic_map.identifier, modification_attribute) # Remove the original note occurrence topic_store.delete_occurrence(topic_map.identifier, note_identifier) flash("Note successfully converted.", "success") return redirect( url_for( "topic.view", map_identifier=topic_map.identifier, topic_identifier=new_topic.identifier, )) return render_template( "note/convert.html", error=error, topic_map=topic_map, topic=topic, topic_name=form_topic_name, topic_identifier=form_topic_identifier, topic_text=form_topic_text, topic_instance_of=form_topic_instance_of, note_identifier=note_identifier, )
def create(map_identifier, topic_identifier): topic_store = get_topic_store() topic_map = topic_store.get_topic_map(map_identifier, current_user.id) if topic_map is None: current_app.logger.warning( f"Topic map not found: user identifier: [{current_user.id}], topic map identifier: [{map_identifier}]" ) abort(404) # If the map doesn't belong to the user and they don't have the right # collaboration mode on the map, then abort if not topic_map.owner and topic_map.collaboration_mode is not CollaborationMode.EDIT: abort(403) topic = topic_store.get_topic( map_identifier, topic_identifier, resolve_attributes=RetrievalMode.RESOLVE_ATTRIBUTES, ) if topic is None: current_app.logger.warning( f"Topic not found: user identifier: [{current_user.id}], topic map identifier: [{map_identifier}], topic identifier: [{topic_identifier}]" ) abort(404) error = 0 if request.method == "POST": form_topic_identifier = request.form["topic-identifier"].strip() form_topic_name = request.form["topic-name"].strip() form_topic_text = request.form["topic-text"].strip() form_topic_instance_of = request.form["topic-instance-of"].strip() form_topic_text_scope = request.form["topic-text-scope"].strip() # If no values have been provided set their default values if not form_topic_instance_of: form_topic_instance_of = "topic" if not form_topic_text_scope: form_topic_text_scope = session["current_scope"] # Validate form inputs if not form_topic_name: error = error | 1 if topic_store.topic_exists(topic_map.identifier, form_topic_identifier): error = error | 2 if not form_topic_identifier: error = error | 4 if not topic_store.topic_exists(topic_map.identifier, form_topic_instance_of): error = error | 8 if not topic_store.topic_exists(topic_map.identifier, form_topic_text_scope): error = error | 16 if error != 0: flash( "An error occurred when submitting the form. Please review the warnings and fix accordingly.", "warning", ) else: new_topic = Topic(form_topic_identifier, form_topic_instance_of, form_topic_name) text_occurrence = Occurrence( instance_of="text", topic_identifier=new_topic.identifier, scope=form_topic_text_scope, resource_data=form_topic_text, ) timestamp = str(datetime.now()) modification_attribute = Attribute( "modification-timestamp", timestamp, new_topic.identifier, data_type=DataType.TIMESTAMP, ) # Persist objects to the topic store topic_store.set_topic(topic_map.identifier, new_topic) topic_store.set_occurrence(topic_map.identifier, text_occurrence) topic_store.set_attribute(topic_map.identifier, modification_attribute) flash("Topic successfully created.", "success") return redirect( url_for( "topic.view", map_identifier=topic_map.identifier, topic_identifier=new_topic.identifier, )) return render_template( "topic/create.html", error=error, topic_map=topic_map, topic=topic, topic_name=form_topic_name, topic_identifier=form_topic_identifier, topic_text=form_topic_text, topic_instance_of=form_topic_instance_of, topic_text_scope=form_topic_text_scope, ) return render_template("topic/create.html", error=error, topic_map=topic_map, topic=topic)
def create(map_identifier, topic_identifier): topic_store = get_topic_store() topic_map = topic_store.get_topic_map(map_identifier) if topic_map is None: abort(404) if current_user.id != topic_map.user_identifier: abort(403) topic = topic_store.get_topic( map_identifier, topic_identifier, resolve_attributes=RetrievalMode.RESOLVE_ATTRIBUTES, ) if topic is None: abort(404) form_topic_name = "" form_topic_identifier = "" form_topic_text = "" form_topic_instance_of = "topic" form_topic_text_scope = session["current_scope"] error = 0 if request.method == "POST": form_topic_identifier = request.form["topic-identifier"].strip() form_topic_name = request.form["topic-name"].strip() form_topic_text = request.form["topic-text"].strip() form_topic_instance_of = request.form["topic-instance-of"].strip() form_topic_text_scope = request.form["topic-text-scope"].strip() # If no values have been provided set their default values if not form_topic_instance_of: form_topic_instance_of = "topic" # Validate form inputs if not form_topic_name: error = error | 1 if topic_store.topic_exists(topic_map.identifier, form_topic_identifier): error = error | 2 if not form_topic_identifier: error = error | 4 if not topic_store.topic_exists(topic_map.identifier, form_topic_instance_of): error = error | 8 if not topic_store.topic_exists(topic_map.identifier, form_topic_text_scope): error = error | 16 if error != 0: flash( "An error occurred when submitting the form. Please review the warnings and fix accordingly.", "warning", ) else: new_topic = Topic(form_topic_identifier, form_topic_instance_of, form_topic_name) text_occurrence = Occurrence( instance_of="text", topic_identifier=new_topic.identifier, scope=form_topic_text_scope, resource_data=form_topic_text, ) timestamp = str(datetime.now()) modification_attribute = Attribute( "modification-timestamp", timestamp, new_topic.identifier, data_type=DataType.TIMESTAMP, ) query_attribute = Attribute( "knowledge-graph-query", form_topic_name.lower(), new_topic.identifier, data_type=DataType.STRING, ) # Persist objects to the topic store topic_store.set_topic(topic_map.identifier, new_topic) topic_store.set_occurrence(topic_map.identifier, text_occurrence) topic_store.set_attribute(topic_map.identifier, modification_attribute) topic_store.set_attribute(topic_map.identifier, query_attribute) flash("Topic successfully created.", "success") return redirect( url_for( "topic.view", map_identifier=topic_map.identifier, topic_identifier=new_topic.identifier, )) return render_template( "topic/create.html", error=error, topic_map=topic_map, topic=topic, topic_name=form_topic_name, topic_identifier=form_topic_identifier, topic_text=form_topic_text, topic_instance_of=form_topic_instance_of, topic_text_scope=form_topic_text_scope, )
# the player. Although often collected directly through touch, power-ups can sometimes only be gained by collecting # several related items, such as the floating letters of the word 'EXTEND' in _Bubble Bobble_. Well known examples of # power-ups that have entered popular culture include the power pellets from _Pac-Man_ (regarded as the first power-up) # and the Super Mushroom from _Super Mario Bros_., which ranked first in UGO Networks' Top 11 Video Game Powerups. # """ # asset14 = Asset('text', data=prop11_text) # prop11.add_asset(asset14) # store.set_prop(TOPIC_MAP_IDENTIFIER, prop11, 'cafeteria') # Scene 02 - House. # Scene 01 - Living Room. # Scene 01 - Bedroom. # Annotations annotation1 = Topic('palm-tree', 'annotation', 'Palm Tree') scene_store.set_topic(TOPIC_MAP_IDENTIFIER, annotation1) annotation2 = Topic('street-lamp', 'annotation', 'Street Lamp') scene_store.set_topic(TOPIC_MAP_IDENTIFIER, annotation2) annotation3 = Topic('trash-cans', 'annotation', 'Trash Cans') scene_store.set_topic(TOPIC_MAP_IDENTIFIER, annotation3) annotation4 = Topic('chimney', 'annotation', 'Chimney') scene_store.set_topic(TOPIC_MAP_IDENTIFIER, annotation4) # Clean-up. scene_store.close()
def set_entity(self, map_identifier: int, entity: Entity, event_identifier: str = None) -> None: if (self.source_directory is None) or (self.destination_directory is None): raise StoryDbError("Missing resource directories") if not self.topic_store.topic_exists(map_identifier, entity.identifier): topic = Topic(entity.identifier, entity.instance_of, entity.name) timestamp = str(datetime.now()) modification_attribute = Attribute( "modification-timestamp", timestamp, topic.identifier, data_type=DataType.TIMESTAMP, ) self.topic_store.set_topic(map_identifier, topic) self.topic_store.set_attribute(map_identifier, modification_attribute) if hasattr(entity, "description") and entity.description: text_occurrence = Occurrence( instance_of="text", topic_identifier=entity.identifier, resource_data=entity.description, ) self.topic_store.set_occurrence(map_identifier, text_occurrence) if hasattr(entity, "animation") and entity.animation: entity.add_attribute( Attribute( "animation", entity.animation, entity.identifier, data_type=DataType.STRING, )) # Create the file directory for this topic map and topic if it doesn't already exist file_directory = os.path.join(self.destination_directory, str(map_identifier), entity.identifier) if not os.path.isdir(file_directory): os.makedirs(file_directory) for resource in entity.resources: occurrence = Occurrence( instance_of=resource.instance_of, topic_identifier=entity.identifier, resource_ref=resource.reference, resource_data=resource.data, ) title_attribute = Attribute( "title", resource.title, occurrence.identifier, data_type=DataType.STRING, ) self.topic_store.set_occurrence(map_identifier, occurrence) self.topic_store.set_attribute(map_identifier, title_attribute) # Copy resource file to appropriate (topic) directory if occurrence.resource_ref: source_file_path = os.path.join(self.source_directory, occurrence.resource_ref) destination_file_path = os.path.join( self.destination_directory, str(map_identifier), entity.identifier, occurrence.resource_ref, ) if not os.path.isfile(destination_file_path): shutil.copy(source_file_path, destination_file_path) if hasattr(entity, "tags"): self.topic_store.set_tags(map_identifier, entity.identifier, entity.tags) self.topic_store.set_attributes(map_identifier, entity.attributes) if event_identifier: association = Association( instance_of=entity.instance_of, src_topic_ref=entity.identifier, dest_topic_ref=event_identifier, src_role_spec="included-in", dest_role_spec="includes", ) self.topic_store.set_association(map_identifier, association)