added file reader offset
This commit is contained in:
parent
723d004e5c
commit
c23b286540
5 changed files with 80 additions and 2 deletions
17
src/utils/readoffset.py
Normal file
17
src/utils/readoffset.py
Normal 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)
|
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue