def expand_subparts(self, lines, block_name): leading = () parts = () while 1: if len(lines) == 0: if len(leading) > 0: back = Block.with_lines(None, leading) parts = parts + (back,) break else: head = lines[0] rest = lines[1:] match = re.match(r'^\s*\<\<\<([^\>]*)\>\>\>\s*$', head) if match != None: back = Block.with_lines(None, leading) leading = () takeout = Block.empty(match.groups(1)[0]) parts = parts + (back,takeout) else: head = re.subn(r'\<\<\<\<([^\>]*)\>\>\>\>', r'<<<\1>>>', head)[0] leading = leading + (head,) lines = rest return Block.with_parts(block_name, parts)
def write(self, buf, offset): self.inode.rewind() found = False count = 0 block = None while self.inode.exists_next_block() and not found: block = self.inode.next_block() found = count + block.size > offset if not found: count += block.size if not found and (self.inode.file_size - offset) <= 0: if len(self.free) == 0: return byte, key = crypt.GenerateKey() type = 1 newblock = Block(self.free[0], key, new=True, prefix=self.prefix) self.inode.add_direction(type, newblock.src, newblock.size, newblock.key) del self.free[0] block = self.inode.next_block() if block is None: return newfrom = offset newsize = len(buf) written = 0 while newsize > 0 and (newfrom - count) < block.size: block[newfrom - count] = buf[newfrom - offset] newfrom += 1 newsize -= 1 written += 1 if newfrom > self.inode.file_size: self.inode.set_size(self.inode.file_size + 1) if newsize > 0: written += self.write(buf[newfrom - offset:], newfrom) return written
def do_recall(self, source, commands, options, content): block_name = (self.options['name'] if 'name' in self.options else None ) text = self.context.recall(source, block_name) return [Block.just_text(text)]