Skip to content

Commit 2f2374e

Browse files
committed
add the funtions into the Filer to help read and write datas
1 parent e76eee2 commit 2f2374e

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ Get help ➡️ [Github issue](https://github.com/BertramYe/preparser/issues)
115115

116116
# Update logs
117117

118+
* `version 2.0.8 `: add the func of `read_datas_from_file` into `Filer`, to help read the datas from the specified type files.
119+
118120
* `version 2.0.7 `: add the `ssl_certi_verified` parameter to control weather ignored the error that caused by ssl certi verification when do the requesting.
119121

120122
* `version 2.0.6 `: add the `html_dynamic_scope` parameters to let user can specified the whole dynamic parse scope, which can help faster the preparser speed when the `parser_mode` is `html_dynamic` . and resort the additional tools into the `ToolsHelper` package.

preparser/FileHelper.py

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Literal,Any
22
from os import makedirs
3-
from os.path import dirname
4-
from json import dump
3+
from os.path import dirname,exists,abspath
4+
from json import dump,load
55

66

77
FileType = Literal['txt','json']
@@ -14,7 +14,15 @@ class Filer():
1414
file_type ( Literal['txt','json'] ): to save or read files type, so far mainly support `txt` and `json`, but future will add more
1515
"""
1616
def __init__(self,file_type:FileType='txt') -> None:
17-
self.file_type:FileType = file_type
17+
self._available_file_types = ['txt','json']
18+
self._file_type:FileType = file_type
19+
20+
def __filter_file_types(self,file_path_without_type:str) -> str | None:
21+
if self._file_type in self._available_file_types:
22+
return abspath(f'{file_path_without_type}.{self._file_type}')
23+
else:
24+
print(f'error: so fare , the type {self._file_type} is not supported by the preparser, only {','.join(self._available_file_types)} is avaliable')
25+
return None
1826

1927
def write_data_into_file(self,new_file_name:str,datas:list[Any],ensure_json_ascii:bool=False):
2028
"""
@@ -25,23 +33,47 @@ def write_data_into_file(self,new_file_name:str,datas:list[Any],ensure_json_asci
2533
datas: (list[Any]) : to save the files datas, which should be a `list` Obeject
2634
ensure_json_ascii (bool) : for the json file content writing, as from the object `PreParser` result, which no need ensure_ascii , otherwise may result the content ascii can't be decoded, just keep default False is ok.
2735
"""
28-
29-
file_name = f'{new_file_name}.{self.file_type}'
30-
# 获取文件夹路径
31-
dir_path = dirname(file_name)
32-
33-
# 确保文件夹存在,若不存在则创建
34-
makedirs(dir_path, exist_ok=True)
35-
try:
36-
print(f'begin to save datas into file: {file_name} !')
37-
if self.file_type == 'txt' or self.file_type == 'json':
38-
with open(file_name,'w',encoding="utf-8") as file:
39-
if self.file_type == 'json':
36+
abs_file_path = self.__filter_file_types(new_file_name)
37+
if abs_file_path:
38+
# get file's dir path
39+
dir_path = dirname(abs_file_path)
40+
# make sure files existed
41+
makedirs(dir_path, exist_ok=True)
42+
try:
43+
print(f'begin to save datas into file: {abs_file_path} !')
44+
with open(abs_file_path,'w',encoding="utf-8") as file:
45+
if self._file_type == 'json':
4046
dump(datas, file, indent=4,ensure_ascii=ensure_json_ascii)
4147
else:
4248
file.writelines(datas)
43-
else:
44-
print(f'failed to save datas into file: {file_name}, as current file type not support, file_type: {self.file_type}')
45-
print(f'successd to save datas into file: {file_name} !')
46-
except Exception as err:
47-
print(f'failed to save datas into file: {file_name}, error: {err}')
49+
print(f'successd to save datas into file: {abs_file_path} !')
50+
except Exception as err:
51+
print(f'failed to save datas into file: {abs_file_path}, error: {err}')
52+
53+
def read_datas_from_file(self,file_name:str):
54+
"""
55+
a function to help get the content from txt or json file
56+
57+
Parameters:
58+
file_name ( str ): to read the result filename , it can include absolute or relative path of the files, but the filename can't include files type,
59+
which already was defined when the `Filer` class was initial, otherwise, if you want specified the file type,
60+
you can change the property `_file_type` after you initial the `Filer` class .
61+
for example,if you want read a `result.txt` file, file_name can't be `./test/result.txt`, but the `./test/result` is ok.
62+
"""
63+
abs_file_path = self.__filter_file_types(file_name)
64+
if abs_file_path:
65+
try:
66+
if exists(abs_file_path):
67+
with open(abs_file_path,'r',encoding="utf-8") as file:
68+
if self._file_type == 'json':
69+
return load(file)
70+
else:
71+
return file.read()
72+
else:
73+
print(f"read file {file_name} failed, error: we can't find out current file from the path: {abs_file_path}")
74+
return None
75+
except Exception as error:
76+
print(f"read file {abs_file_path} failed, error: {error}")
77+
return None
78+
else:
79+
return None

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
name="preparser", # packages name
2020
author="BertramYe", # author name
2121
author_email="bertramyerik@gmail.com", # author's email
22-
version="2.0.7", # pakage version
22+
version="2.0.8", # pakage version
2323
description="a slight preparser to help parse webpage content or get request from urls,which supports win, mac and unix.", # short description
2424
long_description=long_description, # get descrition from README.md
2525
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)