diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2017-06-30 14:09:02 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2017-06-30 14:09:02 -0400 |
commit | 58027b0b28a08f45a64bbd2417bbdf3683749958 (patch) | |
tree | 6f5035c7dca6b09640bdec418a9b4206ec17d838 /cmd2.py | |
parent | 679401b5dd314843694e92ca5c7d68e4f0f63c4d (diff) | |
download | cmd2-git-58027b0b28a08f45a64bbd2417bbdf3683749958.tar.gz |
Verifying a file to be loaded as a text script is either ASCII or UTF-8
Diffstat (limited to 'cmd2.py')
-rwxr-xr-x | cmd2.py | 49 |
1 files changed, 48 insertions, 1 deletions
@@ -26,6 +26,7 @@ written to use `self.stdout.write()`, Git repository on GitHub at https://github.com/python-cmd2/cmd2 """ import cmd +import codecs import collections import datetime import glob @@ -1758,6 +1759,22 @@ Script should contain one command per line, just like command would be typed in return expanded_path = os.path.abspath(os.path.expanduser(file_path.strip())) + + # Make sure expanded_path points to a file + if not os.path.isfile(expanded_path): + self.perror('{} does not exist or is not a file\n'.format(expanded_path), traceback_war=False) + return + + # Make sure the file is not empty + if os.path.getsize(expanded_path) == 0: + self.perror('{} is empty\n'.format(expanded_path), traceback_war=False) + return + + # Make sure the file is ASCII or UTF-8 encoded text + if not self.is_text_file(expanded_path): + self.perror('{} is not an ASCII or UTF-8 encoded text file\n'.format(expanded_path), traceback_war=False) + return + try: target = open(expanded_path) except IOError as e: @@ -1788,6 +1805,37 @@ Script should contain one command per line, just like command would be typed in if runme: return self.onecmd_plus_hooks(runme) + def is_text_file(self, file_path): + """ + Returns if a file contains only ASCII or UTF-8 encoded text + :param file_path: path to the file being checked + """ + expanded_path = os.path.abspath(os.path.expanduser(file_path.strip())) + valid_text_file = False + + # Check if the file is ASCII + try: + with codecs.open(expanded_path, encoding='ascii', errors='strict') as f: + # Make sure the file has at least one line of text + if sum(1 for line in f) > 0: + valid_text_file = True + except IOError: + pass + 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 + if sum(1 for line in f) > 0: + valid_text_file = True + except IOError: + pass + except UnicodeDecodeError: + # Not UTF-8 + pass + + return valid_text_file + def run_transcript_tests(self, callargs): """Runs transcript tests for provided file(s). @@ -2357,5 +2405,4 @@ class CmdResult(namedtuple_with_two_defaults('CmdResult', ['out', 'err', 'war']) if __name__ == '__main__': # If run as the main application, simply start a bare-bones cmd2 application with only built-in functionality. app = Cmd() - app.debug = True app.cmdloop() |