Skip to content

Commit

Permalink
Merge pull request #228 from chinapnr/v1.1.10_develop
Browse files Browse the repository at this point in the history
V1.1.10 develop
  • Loading branch information
itaa authored May 4, 2019
2 parents 23c5147 + 6ec611d commit 7b57775
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/change_log.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
更新记录
===========================
2019.4.30 v1.1.10
---------------------------
* `#226 <https://github.com/chinapnr/fishbase/issues/226>`_, file, add function :meth:`fish_file.get_file_encoding`, doc and unittest;


2019.4.15 v1.1.9
---------------------------
* `#222 <https://github.com/chinapnr/fishbase/issues/222>`_, common, edit function :meth:`fish_logger.conf_as_dict`, optimize
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
# built documents.
#
# The short X.Y version.
version = '1.1.9'
version = '1.1.10'
# The full version, including alpha/beta/rc tags.
release = '1.1.9'
release = '1.1.10'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
1 change: 1 addition & 0 deletions docs/fish_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.. autosummary::
fish_file.check_sub_path_create
fish_file.get_abs_filename_with_sub_path
fish_file.get_file_encoding

.. automodule:: fish_file
:members:
2 changes: 1 addition & 1 deletion fishbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
from .fish_project import *
from .fish_random import *

__version__ = '1.1.9' # type: str
__version__ = '1.1.10' # type: str
45 changes: 45 additions & 0 deletions fishbase/fish_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# 2017.1.8 v1.0.9 created

import chardet
import pathlib


Expand Down Expand Up @@ -140,6 +141,50 @@ def check_sub_path_create(sub_path):
return False, True


# 2019.4.1 v1.1.10 edit by Hu Jun, #226
def get_file_encoding(file_path):

"""
获取给定文件的编码;
:param:
* file_path: (string) 文件的完整路径
:return:
* file_encoding (string),文件编码
举例如下::
print('--- get_file_encoding demo ---')
result = get_file_encoding(__file__)
print(result)
print('---')
输出结果::
--- get_file_encoding demo ---
utf-8
---
"""
# 判断给定路径是否是一个文件
flag = pathlib.Path(file_path).is_file()
if not flag:
raise RuntimeError('file {}, does not exist'.format(file_path))

# 读入文件
with open(file_path, 'rb') as f:
buf = f.read()
# 获取文件信息
file_info = chardet.detect(buf)

encoding = file_info['encoding']
if encoding.startswith(('utf-8', 'UTF-8')):
file_encoding = encoding
else:
file_encoding = 'GB2312'
return file_encoding


# 生成使用模块时的下一级路径某文件的完整文件名
# 2016.5.18 create by David Yi
# 2017.1.8 v1.0.9 edit the function name, #19004
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pytest==4.0.2
pytest-cov
coveralls
pyyaml
chardet
14 changes: 14 additions & 0 deletions test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
# 2018.5.28 create by David Yi

import os
import pytest
from fishbase.fish_file import *

# 定义当前路径
current_path = os.path.dirname(os.path.abspath(__file__))
# 定义配置文件名
test_filename = os.path.join(current_path, 'test_gb2312_encoding.txt')


# 2018.5.28 v1.0.13 #19040,#19042, create by David Yi, fish_file unittest
Expand Down Expand Up @@ -67,3 +70,14 @@ def test_check_sub_path_create_01(self):

# 删除临时文件
os.rmdir(abs_path)

# tc for get_file_encoding()
def test_get_file_encoding_01(self):
assert get_file_encoding(__file__) == 'utf-8'
# 应该读不到返回的 dict 内容
with pytest.raises(RuntimeError):
temp_file = __file__ + '.for_test'
get_file_encoding(temp_file)

gbk_file_path = test_filename
assert get_file_encoding(gbk_file_path) == 'GB2312'
1 change: 1 addition & 0 deletions test/test_gb2312_encoding.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a test file for fish_file.get_file_encoding

0 comments on commit 7b57775

Please sign in to comment.