class Loader(object): """Load blog entries into Neo4j.""" def __init__(self, config, graph): #self.changelog = ChangeLog(config) self.path = Path(config) changelog_dir = self.path.get_working_etc() self.changelog = ChangeLog(changelog_dir) self.changelog.initialize(config) self.parser = Parser(config) self.graph = graph def changelog_exists(self): return self.changelog.exists() def update_entries(self): if self.changelog_exists(): print "UPDATING CHANGED" self.update_changed_entries() else: print "UPDATING ALL" self.update_all_entries() def update_all_entries(self): for source_abspath in self.parser.get_all_files(): self.update_entry(source_abspath) def update_changed_entries(self): update_count = 0 data = self.changelog.data if data is None: return update_count last_updated = self.get_last_updated() # Data is an OrderedDict, most recent changes last for source_path in reversed(data): status, timestamp = data[source_path] if self.old_timestamp(timestamp, last_updated): break source_abspath = self.path.get_source_abspath(source_path) update_count += self.update_entry(source_abspath) return update_count def old_timestamp(self, timestamp, last_updated): # Timestamps with a time before the last_updated time # were updated during the previous push return (timestamp <= last_updated) def update_entry(self, source_abspath): data = self.parser.get_data(source_abspath) fragment_abspath = self.path.get_fragment_abspath(source_abspath) if os.path.exists(fragment_abspath) is False: print "WARNING: Fragment Not Found", fragment_abspath return False # TODO: remove entry if fragment doesn't exist entry = self.graph.entries.save(data) return True def set_last_updated(self, last_updated): # Metadata methods are Neo4j-only right now self.graph.set_metadata("entries:last_updated", last_updated) def get_last_updated(self): # Metadata methods are Neo4j-only right now result = self.graph.get_metadata("entries:last_updated") last_updated = result.raw return last_updated