summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2021-03-26 13:56:33 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2021-03-26 15:24:34 -0400
commit62ed8aebf6eefcf68a15fdd4b7a7cd5dfe4c6f6b (patch)
tree1e72725041a8e3f83f31dddbfd564fcd02f27401 /cmd2/utils.py
parent070262e1f397e2297cdb1ad611db6b6d5bed8830 (diff)
downloadcmd2-git-py_refactor.tar.gz
Renamed use_ipython keyword parameter of cmd2.Cmd.__init__() to include_ipy.py_refactor
Added include_py keyword parameter to cmd2.Cmd.__init__(). If False, then the py command will not be available. Removed ability to run Python commands from the command line with py. Made banners and exit messages of Python and IPython consistent. Changed utils.is_text_file() to raise OSError if file cannot be read.
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py32
1 files changed, 11 insertions, 21 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py
index 2787c079..7e350f96 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -186,38 +186,28 @@ class Settable:
def is_text_file(file_path: str) -> bool:
- """Returns if a file contains only ASCII or UTF-8 encoded text.
+ """Returns if a file contains only ASCII or UTF-8 encoded text and isn't empty.
:param file_path: path to the file being checked
- :return: True if the file is a text file, False if it is binary.
+ :return: True if the file is a non-empty text file, otherwise False
+ :raises OSError if file can't be read
"""
import codecs
expanded_path = os.path.abspath(os.path.expanduser(file_path.strip()))
valid_text_file = False
- # Check if the file is ASCII
+ # Only need to check for utf-8 compliance since that covers ASCII, too
try:
- with codecs.open(expanded_path, encoding='ascii', errors='strict') as f:
- # Make sure the file has at least one line of text
- # noinspection PyUnusedLocal
- if sum(1 for line in f) > 0:
+ with codecs.open(expanded_path, encoding='utf-8', errors='strict') as f:
+ # Make sure the file has only utf-8 text and is not empty
+ if sum(1 for _ in f) > 0:
valid_text_file = True
- except OSError: # pragma: no cover
- pass
+ except OSError:
+ raise
except UnicodeDecodeError:
- # The file is not ASCII. Check if it is UTF-8.
- try:
- with codecs.open(expanded_path, encoding='utf-8', errors='strict') as f:
- # Make sure the file has at least one line of text
- # noinspection PyUnusedLocal
- if sum(1 for line in f) > 0:
- valid_text_file = True
- except OSError: # pragma: no cover
- pass
- except UnicodeDecodeError:
- # Not UTF-8
- pass
+ # Not UTF-8
+ pass
return valid_text_file