def make_content_node(kind, source_id, title, license, filepath, optionals): """ Create `kind` subclass of ContentNode based on required args and optionals. """ content_node = None if kind == content_kinds.VIDEO: content_node = nodes.VideoNode( source_id=source_id, title=title, license=license, author=optionals.get("author", None), description=optionals.get("description", None), derive_thumbnail=True, # video-specific data files=[files.VideoFile(path=filepath)], ) elif kind == content_kinds.AUDIO: content_node = nodes.AudioNode( source_id=source_id, title=title, license=license, author=optionals.get("author", None), description=optionals.get("description", None), thumbnail=optionals.get("thumbnail", None), files=[files.AudioFile(path=filepath)], ) elif kind == content_kinds.DOCUMENT: content_node = nodes.DocumentNode( source_id=source_id, title=title, license=license, author=optionals.get("author", None), description=optionals.get("description", None), thumbnail=optionals.get("thumbnail", None), files=[files.DocumentFile(path=filepath)], ) return content_node
def _build_tree(node, sourcetree): """ Parse nodes given in `sourcetree` and add as children of `node`. """ for child_source_node in sourcetree: try: main_file = child_source_node['files'][0] if 'files' in child_source_node else {} kind = guess_content_kind(path=main_file.get('path'), web_video_data=main_file.get('youtube_id') or main_file.get('web_url'), questions=child_source_node.get("questions")) except UnknownContentKindError: continue if kind == content_kinds.TOPIC: child_node = nodes.TopicNode( source_id=child_source_node["id"], title=child_source_node["title"], author=child_source_node.get("author"), description=child_source_node.get("description"), thumbnail=child_source_node.get("thumbnail"), ) node.add_child(child_node) source_tree_children = child_source_node.get("children", []) _build_tree(child_node, source_tree_children) elif kind == content_kinds.VIDEO: child_node = nodes.VideoNode( source_id=child_source_node["id"], title=child_source_node["title"], license=get_license(child_source_node.get("license"), description="Description of license"), author=child_source_node.get("author"), description=child_source_node.get("description"), derive_thumbnail=True, # video-specific data thumbnail=child_source_node.get('thumbnail'), ) add_files(child_node, child_source_node.get("files") or []) node.add_child(child_node) elif kind == content_kinds.AUDIO: child_node = nodes.AudioNode( source_id=child_source_node["id"], title=child_source_node["title"], license=child_source_node.get("license"), author=child_source_node.get("author"), description=child_source_node.get("description"), thumbnail=child_source_node.get("thumbnail"), ) add_files(child_node, child_source_node.get("files") or []) node.add_child(child_node) elif kind == content_kinds.DOCUMENT: child_node = nodes.DocumentNode( source_id=child_source_node["id"], title=child_source_node["title"], license=child_source_node.get("license"), author=child_source_node.get("author"), description=child_source_node.get("description"), thumbnail=child_source_node.get("thumbnail"), ) add_files(child_node, child_source_node.get("files") or []) node.add_child(child_node) elif kind == content_kinds.EXERCISE: child_node = nodes.ExerciseNode( source_id=child_source_node["id"], title=child_source_node["title"], license=child_source_node.get("license"), author=child_source_node.get("author"), description=child_source_node.get("description"), exercise_data={}, # Just set to default thumbnail=child_source_node.get("thumbnail"), ) add_files(child_node, child_source_node.get("files") or []) for q in child_source_node.get("questions"): question = create_question(q) child_node.add_question(question) node.add_child(child_node) elif kind == content_kinds.HTML5: child_node = nodes.HTML5AppNode( source_id=child_source_node["id"], title=child_source_node["title"], license=child_source_node.get("license"), author=child_source_node.get("author"), description=child_source_node.get("description"), thumbnail=child_source_node.get("thumbnail"), ) add_files(child_node, child_source_node.get("files") or []) node.add_child(child_node) else: # unknown content file format continue return node
def build_tree_from_json(parent_node, sourcetree): """ Recusively parse nodes in the list `sourcetree` and add them as children to the `parent_node`. Usually called with `parent_node` being a `ChannelNode`. """ EXPECTED_NODE_TYPES = [ TOPIC_NODE, VIDEO_NODE, AUDIO_NODE, EXERCISE_NODE, DOCUMENT_NODE, HTML5_NODE ] for source_node in sourcetree: kind = source_node['kind'] if kind not in EXPECTED_NODE_TYPES: LOGGER.critical('Unexpected node type found: ' + kind) raise NotImplementedError( 'Unexpected node type found in json data.') if kind == TOPIC_NODE: child_node = nodes.TopicNode( source_id=source_node.get("source_id", None), title=source_node["title"], author=source_node.get("author"), description=source_node.get("description"), language=source_node.get('language', None), thumbnail=source_node.get("thumbnail"), ) parent_node.add_child(child_node) source_tree_children = source_node.get("children", []) build_tree_from_json(child_node, source_tree_children) elif kind == VIDEO_NODE: child_node = nodes.VideoNode( source_id=source_node["source_id"], title=source_node["title"], license=get_license(**source_node['license']), author=source_node.get("author"), description=source_node.get("description"), language=source_node.get('language', None), derive_thumbnail=source_node.get( 'derive_thumbnail', True), # video-specific option thumbnail=source_node.get('thumbnail'), ) add_files(child_node, source_node.get("files") or []) parent_node.add_child(child_node) elif kind == AUDIO_NODE: child_node = nodes.AudioNode( source_id=source_node["source_id"], title=source_node["title"], license=get_license(**source_node['license']), author=source_node.get("author"), description=source_node.get("description"), language=source_node.get('language', None), thumbnail=source_node.get('thumbnail'), ) add_files(child_node, source_node.get("files") or []) parent_node.add_child(child_node) elif kind == EXERCISE_NODE: child_node = nodes.ExerciseNode( source_id=source_node["source_id"], title=source_node["title"], license=get_license(**source_node['license']), author=source_node.get("author"), description=source_node.get("description"), language=source_node.get('language', None), thumbnail=source_node.get("thumbnail"), questions=[], ) add_questions(child_node, source_node.get("questions") or []) parent_node.add_child(child_node) elif kind == DOCUMENT_NODE: child_node = nodes.DocumentNode( source_id=source_node["source_id"], title=source_node["title"], license=get_license(**source_node['license']), author=source_node.get("author"), description=source_node.get("description"), language=source_node.get('language', None), thumbnail=source_node.get("thumbnail"), ) add_files(child_node, source_node.get("files") or []) parent_node.add_child(child_node) elif kind == HTML5_NODE: child_node = nodes.HTML5AppNode( source_id=source_node["source_id"], title=source_node["title"], license=get_license(**source_node['license']), author=source_node.get("author"), description=source_node.get("description"), language=source_node.get('language', None), thumbnail=source_node.get("thumbnail"), ) add_files(child_node, source_node.get("files") or []) parent_node.add_child(child_node) else: LOGGER.critical("Encountered an unknown kind: " + str(source_node)) continue return parent_node
def build_tree_from_json(parent_node, sourcetree): """ Recusively parse nodes in the list `sourcetree` and add them as children to the `parent_node`. Usually called with `parent_node` being a `ChannelNode`. """ EXPECTED_NODE_TYPES = [TOPIC_NODE, VIDEO_NODE, AUDIO_NODE, EXERCISE_NODE, DOCUMENT_NODE, HTML5_NODE, SLIDESHOW_NODE] for source_node in sourcetree: kind = source_node['kind'] if kind not in EXPECTED_NODE_TYPES: LOGGER.critical('Unexpected node kind found: ' + kind) raise NotImplementedError('Unexpected node kind found in json data.') if kind == TOPIC_NODE: child_node = nodes.TopicNode( source_id=source_node.get('source_id', None), title=source_node['title'], description=source_node.get('description'), author=source_node.get('author'), aggregator=source_node.get('aggregator'), provider=source_node.get('provider'), # no role for topics (computed dynaically from descendants) language=source_node.get('language'), thumbnail=source_node.get('thumbnail'), tags=source_node.get('tags'), ) parent_node.add_child(child_node) source_tree_children = source_node.get('children', []) build_tree_from_json(child_node, source_tree_children) elif kind == VIDEO_NODE: child_node = nodes.VideoNode( source_id=source_node['source_id'], title=source_node['title'], description=source_node.get('description'), license=get_license(**source_node['license']), author=source_node.get('author'), aggregator=source_node.get('aggregator'), provider=source_node.get('provider'), role=source_node.get('role', roles.LEARNER), language=source_node.get('language'), derive_thumbnail=source_node.get('derive_thumbnail', True), # video-specific option thumbnail=source_node.get('thumbnail'), tags=source_node.get('tags'), ) add_files(child_node, source_node.get('files') or []) parent_node.add_child(child_node) elif kind == AUDIO_NODE: child_node = nodes.AudioNode( source_id=source_node['source_id'], title=source_node['title'], description=source_node.get('description'), license=get_license(**source_node['license']), author=source_node.get('author'), aggregator=source_node.get('aggregator'), provider=source_node.get('provider'), role=source_node.get('role', roles.LEARNER), language=source_node.get('language'), thumbnail=source_node.get('thumbnail'), tags=source_node.get('tags'), ) add_files(child_node, source_node.get('files') or []) parent_node.add_child(child_node) elif kind == EXERCISE_NODE: child_node = nodes.ExerciseNode( source_id=source_node['source_id'], title=source_node['title'], description=source_node.get('description'), license=get_license(**source_node['license']), author=source_node.get('author'), aggregator=source_node.get('aggregator'), provider=source_node.get('provider'), role=source_node.get('role', roles.LEARNER), language=source_node.get('language'), thumbnail=source_node.get('thumbnail'), tags=source_node.get('tags'), exercise_data=source_node.get('exercise_data'), questions=[], ) add_questions(child_node, source_node.get('questions') or []) parent_node.add_child(child_node) elif kind == DOCUMENT_NODE: child_node = nodes.DocumentNode( source_id=source_node['source_id'], title=source_node['title'], description=source_node.get('description'), license=get_license(**source_node['license']), author=source_node.get('author'), aggregator=source_node.get('aggregator'), provider=source_node.get('provider'), role=source_node.get('role', roles.LEARNER), language=source_node.get('language'), thumbnail=source_node.get('thumbnail'), tags=source_node.get('tags'), ) add_files(child_node, source_node.get('files') or []) parent_node.add_child(child_node) elif kind == HTML5_NODE: child_node = nodes.HTML5AppNode( source_id=source_node['source_id'], title=source_node['title'], description=source_node.get('description'), license=get_license(**source_node['license']), author=source_node.get('author'), aggregator=source_node.get('aggregator'), provider=source_node.get('provider'), role=source_node.get('role', roles.LEARNER), language=source_node.get('language'), thumbnail=source_node.get('thumbnail'), tags=source_node.get('tags'), ) add_files(child_node, source_node.get('files') or []) parent_node.add_child(child_node) elif kind == SLIDESHOW_NODE: child_node = nodes.SlideshowNode( source_id=source_node['source_id'], title=source_node['title'], description=source_node.get('description'), license=get_license(**source_node['license']), author=source_node.get('author'), aggregator=source_node.get('aggregator'), provider=source_node.get('provider'), role=source_node.get('role', roles.LEARNER), language=source_node.get('language'), thumbnail=source_node.get('thumbnail'), tags=source_node.get('tags') ) add_files(child_node, source_node.get('files') or []) parent_node.add_child(child_node) else: LOGGER.critical('Encountered an unknown kind: ' + str(source_node)) continue return parent_node