diff --git a/next.py b/next.py index eb9b32c..837ad62 100644 --- a/next.py +++ b/next.py @@ -49,61 +49,6 @@ class Atom(): else: return msgpack.dumps([False, self.name, self.cont]) -class Inode(): - def __init__(self, name: str, iotaApi: Iota, type: str = None) -> None: - self.name = name - self.type = type - self.ref = None - self.iotaApi = iotaApi - - def setType(self, type: str) -> None: - self.type = type - - def change(self, size: int=None, hash: int=None, milestoneIndex: int=None) -> Atom: - delta = {} - if size!=None: - self.size = size - delta[b's'] = size - if size!=None: - self.size = hash - delta[b'h'] = hash - if milestoneIndex!=None: - self.milestoneIndex = milestoneIndex - delta[b'm'] = milestoneIndex - return Atom(False, delta, self.name) - - def applyAtom(self, atom: Atom) -> None: - if atom.name != self.name: - raise Exception("Cannot apply atom ment for a different inode (names differ)") - if atom.milestone: - stones = atom.cont - if self.name in stones: - self.applyAtom(Atom(False, stones[self.name], self.name)) - else: - delta = atom.cont - if b's' in delta: - self.size = delta[b's'] - if b'm' in delta: - self.milestoneIndex = delta[b'm'] - - def toStone(self) -> None: - if self.type=="file": - return [self.size, self.hash] - else: - return [self.milestoneIndex] - - def getRef(self): - if self.name == "*": - return None - if not self.ref: - if self.type=="dir": - self.elem = TangleFileTreeElement(self.name, self.milestoneIndex, self, self.iotaApi) - elif self.type=="file": - self.ref = TangleFile(self.name, self, self.api) - else: - raise Exception("Cannot get reference of an inode of type "+self.type) - return self.ref - class BlobChunk(): def __init__(self, data: bytes = b'', sealed: bool = False) -> None: self.data = data @@ -250,7 +195,7 @@ class TangleFileTreeElement(TangleBlob): if isinstance(parent, bytes): self.token = hashlib.sha3_384(parent + name.encode()).digest() else: - self.token = hashlib.sha3_384(parent.getRef().token + name.encode()).digest() + self.token = hashlib.sha3_384(parent.token + name.encode()).digest() super(TangleFileTreeElement, self).__init__(self.token, iotaApi) self.name = name self.inodes = {} @@ -309,7 +254,7 @@ class TangleFileTreeElement(TangleBlob): self._requireFetched() if name in self.getNameList(): return False - inode = Inode(name, self.iotaApi, "dir") + inode = Inode(name, self.iotaApi, self, "dir") atom = inode.change(milestoneIndex=0) self._newAtom(atom) self.inodes[name] = inode @@ -382,6 +327,62 @@ class TangleFile(): self.size = self.blob.getSize() self.parent._updateFileToken(self.name, self.token, self.size) +class Inode(): + def __init__(self, name: str, iotaApi: Iota, parent: TangleFileTreeElement = None, type: str = None) -> None: + self.parent = parent + self.name = name + self.type = type + self.ref = None + self.iotaApi = iotaApi + + def setType(self, type: str) -> None: + self.type = type + + def change(self, size: int=None, hash: int=None, milestoneIndex: int=None) -> Atom: + delta = {} + if size!=None: + self.size = size + delta[b's'] = size + if size!=None: + self.size = hash + delta[b'h'] = hash + if milestoneIndex!=None: + self.milestoneIndex = milestoneIndex + delta[b'm'] = milestoneIndex + return Atom(False, delta, self.name) + + def applyAtom(self, atom: Atom) -> None: + if atom.name != self.name: + raise Exception("Cannot apply atom ment for a different inode (names differ)") + if atom.milestone: + stones = atom.cont + if self.name in stones: + self.applyAtom(Atom(False, stones[self.name], self.name)) + else: + delta = atom.cont + if b's' in delta: + self.size = delta[b's'] + if b'm' in delta: + self.milestoneIndex = delta[b'm'] + + def toStone(self) -> None: + if self.type=="file": + return [self.size, self.hash] + else: + return [self.milestoneIndex] + + def getRef(self): + if self.name == "*": + return None + if not self.ref: + if self.type=="dir": + self.elem = TangleFileTreeElement(self.name, self.milestoneIndex, self.parent, self.iotaApi) + elif self.type=="file": + self.ref = TangleFile(self.name, self, self.api) + else: + raise Exception("Cannot get reference of an inode of type "+self.type) + return self.ref + class IotaFs(): def __init__(self, token) -> None: self.api = Iota('https://nodes.thetangle.org:443', local_pow=True) @@ -405,9 +406,8 @@ class IotaFs(): def createNewFile(self, name) -> None: pass -def main(): - api = Iota('https://nodes.thetangle.org:443', local_pow=True) - token = b'testToken' - genesis = TangleFileTreeElement("*", 0, token, api) - genesis.mkdir("/") - root = genesis.inodes["/"].getRef() +api = Iota('https://nodes.thetangle.org:443', local_pow=True) +token = b'testToken' +genesis = TangleFileTreeElement("*", 0, token, api) +genesis.mkdir("/") +root = genesis.inodes["/"].getRef()