Onionr/src/utils/readstatic.py

37 lines
1.1 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
2019-09-22 02:45:36 +00:00
get static directory and read static data files
2019-09-22 02:45:36 +00:00
"""
from typing import Union
import os
2019-09-22 02:45:36 +00:00
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-09-22 02:45:36 +00:00
"""
2020-10-17 07:51:28 +00:00
def get_static_dir() -> str:
2019-09-23 22:56:05 +00:00
return os.path.dirname(os.path.realpath(__file__)) + '/../../static-data/'
2019-09-22 02:45:36 +00:00
2020-10-17 07:51:28 +00:00
def read_static(file: str, ret_bin: bool = False) -> Union[str, bytes]:
2019-07-30 05:19:22 +00:00
static_file = get_static_dir() + file
2019-07-18 17:40:48 +00:00
if ret_bin:
mode = 'rb'
else:
mode = 'r'
2019-11-05 02:10:03 +00:00
with open(static_file, mode, encoding='utf-8') as f:
data = f.read()
2019-09-22 02:45:36 +00:00
return data