def __init__(self): self._rootid = None self._rootpath = None self._nodefs = None self._filefs = FileFS(self._get_node_path)
class NoteBookConnectionFSRaw (NoteBookConnection): """ Provides a NoteBookConnection using NodeFS as a backing. """ def __init__(self): self._rootid = None self._rootpath = None self._nodefs = None self._filefs = FileFS(self._get_node_path) def _create_rootdir(self, url): os.makedirs(url) def _get_node_path(self, nodeid): return self._nodefs.get_nodedir(nodeid) def _get_node_attr_file(self, nodepath): return os.path.join(nodepath, NODE_META_FILE) #====================== # connection API def connect(self, url): """Make a new connection.""" self._rootpath = url if not os.path.exists(url): self._create_rootdir(url) self._nodefs = NodeFS(self._rootpath) def close(self): """Close connection.""" self._nodefs.close() def save(self): """Save any unsynced state.""" pass #====================== # Node I/O API def create_node(self, nodeid, attr): """Create a node.""" # First node is root. if self._rootid is None: self._rootid = nodeid nodepath = self._nodefs.create_nodedir(nodeid) attr_file = self._get_node_attr_file(nodepath) write_attr(attr_file, nodeid, attr) def read_node(self, nodeid): """Read a node attr.""" nodepath = self._nodefs.get_nodedir(nodeid) if not os.path.exists(nodepath): raise UnknownNode() attr_file = self._get_node_attr_file(nodepath) attrs, extra = read_attr(attr_file, set_extra=False) return attrs def update_node(self, nodeid, attr): """Write node attr.""" nodepath = self._nodefs.get_nodedir(nodeid) if not os.path.exists(nodepath): raise UnknownNode() attr_file = self._get_node_attr_file(nodepath) write_attr(attr_file, nodeid, attr) def delete_node(self, nodeid): """Delete node.""" self._nodefs.delete_nodedir(nodeid) def has_node(self, nodeid): """Returns True if node exists.""" return self._nodefs.has_nodedir(nodeid) def get_rootid(self): """Returns nodeid of notebook root node.""" return self._rootid #=============== # file API def open_file(self, nodeid, filename, mode="r", codec=None, _path=None): """Open a node file.""" return self._filefs.open_file( nodeid, filename, mode=mode, codec=codec, _path=_path) def delete_file(self, nodeid, filename, _path=None): """Delete a node file.""" return self._filefs.delete_file(nodeid, filename, _path) def create_dir(self, nodeid, filename, _path=None): """Create directory within node.""" return self._filefs.create_dir(nodeid, filename, _path) def list_dir(self, nodeid, filename="/", _path=None): """List data files in node.""" return self._filefs.list_dir(nodeid, filename, _path) def has_file(self, nodeid, filename, _path=None): """Return True if file exists.""" return self._filefs.has_file(nodeid, filename, _path) #--------------------------------- # indexing def index(self, query): # TODO: make this plugable # also plug-ability will ensure safer fall back to unhandeled queries # built-in queries # ["index_attr", key, (index_value)] # ["search", "title", text] # ["search_fulltext", text] # ["has_fulltext"] # ["node_path", nodeid] # ["get_attr", nodeid, key] if query[0] == "index_attr": return elif query[0] == "search": assert query[1] == "title" return [ (nodeid, node["title"]) for nodeid, node in ( (nodeid, self.read_node(nodeid)) for nodeid in self._nodefs.iter_nodeids()) if query[2] in node.get("title", "")] elif query[0] == "search_fulltext": # TODO: could implement brute-force backup return [] elif query[0] == "has_fulltext": return False elif query[0] == "node_path": nodeid = query[1] path = [] node = self.read_node(nodeid) while node: path.append(node["nodeid"]) parentids = node.get("parentids") if parentids: node = self.read_node(parentids[0]) else: break path.reverse() return path elif query[0] == "get_attr": return self.read_node(query[1])[query[2]] # FS-specific elif query[0] == "init": return elif query[0] == "index_needed": return False elif query[0] == "clear": return elif query[0] == "index_all": return
class NoteBookConnectionFSRaw(NoteBookConnection): """ Provides a NoteBookConnection using NodeFS as a backing. """ def __init__(self): self._rootid = None self._rootpath = None self._nodefs = None self._filefs = FileFS(self._get_node_path) def _create_rootdir(self, url): os.makedirs(url) def _get_node_path(self, nodeid): return self._nodefs.get_nodedir(nodeid) def _get_node_attr_file(self, nodepath): return os.path.join(nodepath, NODE_META_FILE) #====================== # connection API def connect(self, url): """Make a new connection.""" self._rootpath = url if not os.path.exists(url): self._create_rootdir(url) self._nodefs = NodeFS(self._rootpath) def close(self): """Close connection.""" self._nodefs.close() def save(self): """Save any unsynced state.""" pass #====================== # Node I/O API def create_node(self, nodeid, attr): """Create a node.""" # First node is root. if self._rootid is None: self._rootid = nodeid nodepath = self._nodefs.create_nodedir(nodeid) attr_file = self._get_node_attr_file(nodepath) write_attr(attr_file, nodeid, attr) def read_node(self, nodeid): """Read a node attr.""" nodepath = self._nodefs.get_nodedir(nodeid) if not os.path.exists(nodepath): raise UnknownNode() attr_file = self._get_node_attr_file(nodepath) attrs, extra = read_attr(attr_file, set_extra=False) return attrs def update_node(self, nodeid, attr): """Write node attr.""" nodepath = self._nodefs.get_nodedir(nodeid) if not os.path.exists(nodepath): raise UnknownNode() attr_file = self._get_node_attr_file(nodepath) write_attr(attr_file, nodeid, attr) def delete_node(self, nodeid): """Delete node.""" self._nodefs.delete_nodedir(nodeid) def has_node(self, nodeid): """Returns True if node exists.""" return self._nodefs.has_nodedir(nodeid) def get_rootid(self): """Returns nodeid of notebook root node.""" return self._rootid #=============== # file API def open_file(self, nodeid, filename, mode="r", codec=None, _path=None): """Open a node file.""" return self._filefs.open_file(nodeid, filename, mode=mode, codec=codec, _path=_path) def delete_file(self, nodeid, filename, _path=None): """Delete a node file.""" return self._filefs.delete_file(nodeid, filename, _path) def create_dir(self, nodeid, filename, _path=None): """Create directory within node.""" return self._filefs.create_dir(nodeid, filename, _path) def list_dir(self, nodeid, filename="/", _path=None): """List data files in node.""" return self._filefs.list_dir(nodeid, filename, _path) def has_file(self, nodeid, filename, _path=None): """Return True if file exists.""" return self._filefs.has_file(nodeid, filename, _path) #--------------------------------- # indexing def index(self, query): # TODO: make this plugable # also plug-ability will ensure safer fall back to unhandeled queries # built-in queries # ["index_attr", key, (index_value)] # ["search", "title", text] # ["search_fulltext", text] # ["has_fulltext"] # ["node_path", nodeid] # ["get_attr", nodeid, key] if query[0] == "index_attr": return elif query[0] == "search": assert query[1] == "title" return [ (nodeid, node["title"]) for nodeid, node in ((nodeid, self.read_node(nodeid)) for nodeid in self._nodefs.iter_nodeids()) if query[2] in node.get("title", "") ] elif query[0] == "search_fulltext": # TODO: could implement brute-force backup return [] elif query[0] == "has_fulltext": return False elif query[0] == "node_path": nodeid = query[1] path = [] node = self.read_node(nodeid) while node: path.append(node["nodeid"]) parentids = node.get("parentids") if parentids: node = self.read_node(parentids[0]) else: break path.reverse() return path elif query[0] == "get_attr": return self.read_node(query[1])[query[2]] # FS-specific elif query[0] == "init": return elif query[0] == "index_needed": return False elif query[0] == "clear": return elif query[0] == "index_all": return