class ResourceManager(object): # Rules for determining content and medium def __init__(self, moocdb, HIERARCHY_ROOT='https://'): self.resource_hierarchy = ResourceHierarchy( moocdb.csv_writers['resources'], HIERARCHY_ROOT, Resource) self.resource_types = DictionaryTable(moocdb, 'resource_types') self.resources_urls = DictionaryTable(moocdb, 'resources_urls') self.content_rules = { 'video': 'lecture', 'book': 'book', 'problem': 'problem', 'combinedopenended': 'problem', 'wiki': 'wiki', 'thread|forum|discussion': 'forum', 'info': 'informational', 'preview': 'testing', 'about': 'informational', 'progress': 'informational', 'profile|login|account': 'profile', 'open_ended': 'problem' } self.medium_rules = { 'video': 'video', 'book': 'text', 'problem': 'text', 'combinedopenended': 'text', 'wiki': 'text', 'thread|forum|discussion': 'text', 'info': 'text', 'about': 'text', 'progress': 'text', 'profile': 'text', 'open_ended': 'text' } def create_resource(self, event): resource_uri = event.get_uri() resource_name = event.get_resource_display_name() # Sometimes the event comes with no URI if not resource_uri: return None new_resource = Resource(resource_uri, resource_name) new_resource_type = self.determine_resource_type(event) # Inserts resource into hierarchy # Sets the resource's id and type new_resource.content = new_resource_type[0] new_resource.medium = new_resource_type[1] # Record resource url mapping new_resource_id = self.resource_hierarchy.insert(new_resource) self.resources_urls.insert((new_resource_id, event['url_id'])) # Return resource ID generated at insertion in the hierarchy return new_resource_id def determine_resource_type(self, event): # Build an array with the useful attributes # for making the inference array = {} array['class_name'] = event.__class__.__name__ array['uri'] = event.get_uri() array['resource_name'] = event['resource_display_name'] content = None for (regex, category) in self.content_rules.iteritems(): if util.match_regex([regex], "uri", array): content = category break medium = None for (regex, category) in self.medium_rules.iteritems(): if util.match_regex([regex], "uri", array): medium = category break return (content, medium) # Functions to build resource type dictionary table # and set resource_type_id values. def get_resource_types(self): """Called at the end of the event processing, when the resource hierarchy is completely built. Creates the list of resource types that will be used to populate resource_types table""" self.rec_set_resource_type_id(self.resource_hierarchy.hierarchy) def rec_set_resource_type_id(self, node): """Recursively affect resource_type_id to a node and its children""" node.resource_type_id = self.resource_types.insert( (node.content, node.medium)) for child in node.children: self.rec_set_resource_type_id(child) def serialize(self, pretty_print_to=''): self.get_resource_types() self.resource_hierarchy.serialize(pretty_print_to) self.resource_types.serialize() self.resources_urls.serialize()
class ResourceManager: # Rules for determining content and medium def __init__(self,moocdb,HIERARCHY_ROOT='https://',CONFIG_PATH=''): self.CONTENT_RLS = CONFIG_PATH + 'content_rules.txt' self.MEDIUM_RLS = CONFIG_PATH + 'medium_rules.txt' self.resource_hierarchy = ResourceHierarchy(moocdb.resources, HIERARCHY_ROOT, Resource) self.resource_types = DictionaryTable(moocdb,'resource_types') self.resources_urls = DictionaryTable(moocdb,'resources_urls') self.content_rules = util.load_rules(self.CONTENT_RLS, contentmedium) self.medium_rules = util.load_rules(self.MEDIUM_RLS, contentmedium) def create_resource(self,event): resource_uri = event.get_uri() resource_name = event.get_resource_display_name() # When the event comes with no URI if not resource_uri: #print '(!) [resources.create_resource] No URI associated to the event' return None new_resource = Resource(resource_uri, resource_name) new_resource_type = self.determine_resource_type(event) # Inserts resource into hierarchy # Sets the resource's id and type new_resource.content = new_resource_type[0] new_resource.medium = new_resource_type[1] # Record resource url mapping new_resource_id = self.resource_hierarchy.insert(new_resource) self.resources_urls.insert((new_resource_id, event['url_id'])) # Return resource ID generated at insertion in the hierarchy return new_resource_id def determine_resource_type(self,event): # Build an array with the useful attributes # for making the inferrence array = {} array['class_name'] =event.__class__.__name__ array['uri'] = event.get_uri() array['resource_name'] = event['resource_display_name'] content = util.apply_rules(self.content_rules, array) medium = util.apply_rules(self.medium_rules, array) return (content, medium) # Functions to build resource type dictionary table # and set resource_type_id values. def get_resource_types(self): """Called at the end of the event processing, when the resource hierarchy is completely built. Creates the list of resource types that will be used to populate resource_types table""" self.rec_set_resource_type_id(self.resource_hierarchy.hierarchy) def rec_set_resource_type_id(self, node): """Recursively affect resource_type_id to a node and its children""" node.resource_type_id = self.resource_types.insert((node.content,node.medium)) for child in node.children: self.rec_set_resource_type_id(child) def serialize(self,pretty_print_to=''): self.get_resource_types() self.resource_hierarchy.serialize(pretty_print_to) self.resource_types.serialize() self.resources_urls.serialize()
class ResourceManager: # Rules for determining content and medium def __init__(self, moocdb, HIERARCHY_ROOT='https://', CONFIG_PATH=''): self.CONTENT_RLS = CONFIG_PATH + 'content_rules.txt' self.MEDIUM_RLS = CONFIG_PATH + 'medium_rules.txt' self.resource_hierarchy = ResourceHierarchy(moocdb.resources, HIERARCHY_ROOT, Resource) self.resource_types = DictionaryTable(moocdb, 'resource_types') self.resources_urls = DictionaryTable(moocdb, 'resources_urls') self.content_rules = util.load_rules(self.CONTENT_RLS, contentmedium) self.medium_rules = util.load_rules(self.MEDIUM_RLS, contentmedium) def create_resource(self, event): resource_uri = event.get_uri() resource_name = event.get_resource_display_name() # When the event comes with no URI if not resource_uri: #print '(!) [resources.create_resource] No URI associated to the event' return None new_resource = Resource(resource_uri, resource_name) new_resource_type = self.determine_resource_type(event) # Inserts resource into hierarchy # Sets the resource's id and type new_resource.content = new_resource_type[0] new_resource.medium = new_resource_type[1] # Record resource url mapping new_resource_id = self.resource_hierarchy.insert(new_resource) self.resources_urls.insert((new_resource_id, event['url_id'])) # Return resource ID generated at insertion in the hierarchy return new_resource_id def determine_resource_type(self, event): # Build an array with the useful attributes # for making the inferrence array = {} array['class_name'] = event.__class__.__name__ array['uri'] = event.get_uri() array['resource_name'] = event['resource_display_name'] content = util.apply_rules(self.content_rules, array) medium = util.apply_rules(self.medium_rules, array) return (content, medium) # Functions to build resource type dictionary table # and set resource_type_id values. def get_resource_types(self): """Called at the end of the event processing, when the resource hierarchy is completely built. Creates the list of resource types that will be used to populate resource_types table""" self.rec_set_resource_type_id(self.resource_hierarchy.hierarchy) def rec_set_resource_type_id(self, node): """Recursively affect resource_type_id to a node and its children""" node.resource_type_id = self.resource_types.insert( (node.content, node.medium)) for child in node.children: self.rec_set_resource_type_id(child) def serialize(self, pretty_print_to=''): self.get_resource_types() self.resource_hierarchy.serialize(pretty_print_to) self.resource_types.serialize() self.resources_urls.serialize()