added file reader offset

This commit is contained in:
Kevin Froman 2020-10-17 07:51:28 +00:00
parent 723d004e5c
commit c23b286540
5 changed files with 80 additions and 2 deletions

17
src/utils/readoffset.py Normal file
View file

@ -0,0 +1,17 @@
"""Onionr - Private P2P Communication.
read from a file from an offset (efficiently)
"""
from collections import namedtuple
OffsetReadResult = namedtuple('OffsetReadResult', ['data', 'new_offset'])
def read_from_offset(file_path, offset=0):
with open(file_path, 'rb') as f:
if offset:
f.seek(offset)
data = f.read()
offset = f.tell()
return OffsetReadResult(data, offset)

View file

@ -20,11 +20,11 @@ import os
"""
def get_static_dir()->str:
def get_static_dir() -> str:
return os.path.dirname(os.path.realpath(__file__)) + '/../../static-data/'
def read_static(file:str, ret_bin:bool=False)->Union[str, bytes]:
def read_static(file: str, ret_bin: bool = False) -> Union[str, bytes]:
static_file = get_static_dir() + file
if ret_bin: