Skip to content

Commit 190fecb

Browse files
committed
Make changes requested in PR #413
1 parent db881e1 commit 190fecb

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

cmd2/cmd2.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
2323
Git repository on GitHub at https://github.com/python-cmd2/cmd2
2424
"""
25-
# many imports are lazy-loaded when they are needed
25+
# This module has many imports, quite a few of which are only
26+
# infrequently utilized. To reduce the initial overhead of
27+
# import this module, many of these imports are lazy-loaded
28+
# i.e. we only import the module when we use it
29+
# For example, we don't import the 'traceback' module
30+
# until the perror() function is called and the debug
31+
# setting is True
2632
import argparse
2733
import cmd
2834
import collections
@@ -33,7 +39,7 @@
3339
import re
3440
import shlex
3541
import sys
36-
from typing import Callable, List, Optional, Union, Tuple
42+
from typing import Callable, List, Union, Tuple
3743

3844
import pyperclip
3945

cmd2/transcript.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
#
22
# -*- coding: utf-8 -*-
3+
"""Machinery for running and validating transcripts.
4+
5+
If the user wants to run a transcript (see docs/transcript.rst),
6+
we need a mechanism to run each command in the transcript as
7+
a unit test, comparing the expected output to the actual output.
8+
9+
This file contains the classess necessary to make that work. These
10+
classes are used in cmd2.py::run_transcript_tests()
11+
"""
312
import re
413
import glob
514
import unittest
615

716
from . import utils
817

918
class Cmd2TestCase(unittest.TestCase):
10-
"""Subclass this, setting CmdApp, to make a unittest.TestCase class
11-
that will execute the commands in a transcript file and expect the results shown.
12-
See example.py"""
19+
"""A unittest class used for transcript testing.
20+
21+
Subclass this, setting CmdApp, to make a unittest.TestCase class
22+
that will execute the commands in a transcript file and expect the
23+
results shown.
24+
25+
See example.py
26+
"""
1327
cmdapp = None
1428

1529
def fetchTranscripts(self):
@@ -90,7 +104,7 @@ def _test_transcript(self, fname, transcript):
90104
self.assertTrue(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
91105

92106
def _transform_transcript_expected(self, s):
93-
"""parse the string with slashed regexes into a valid regex
107+
"""Parse the string with slashed regexes into a valid regex.
94108
95109
Given a string like:
96110
@@ -138,8 +152,7 @@ def _transform_transcript_expected(self, s):
138152

139153
@staticmethod
140154
def _escaped_find(regex, s, start, in_regex):
141-
"""
142-
Find the next slash in {s} after {start} that is not preceded by a backslash.
155+
"""Find the next slash in {s} after {start} that is not preceded by a backslash.
143156
144157
If we find an escaped slash, add everything up to and including it to regex,
145158
updating {start}. {start} therefore serves two purposes, tells us where to start
@@ -191,7 +204,9 @@ def tearDown(self):
191204
self.cmdapp.stdout = self._orig_stdout
192205

193206
class OutputTrap(object):
194-
"""Instantiate an OutputTrap to divert/capture ALL stdout output. For use in transcript testing."""
207+
"""Instantiate an OutputTrap to divert/capture ALL stdout output.
208+
For use in transcript testing.
209+
"""
195210

196211
def __init__(self):
197212
self.contents = ''

cmd2/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ def cast(current, new):
102102
return current
103103

104104
def which(editor: str) -> Optional[str]:
105+
"""Find the full path of a given editor.
106+
107+
Return the full path of the given editor, or None if the editor can
108+
not be found.
109+
110+
:param editor: filename of the editor to check, ie 'notepad.exe' or 'vi'
111+
:return: a full path or None
112+
"""
105113
import subprocess
106114
try:
107115
editor_path = subprocess.check_output(['which', editor], stderr=subprocess.STDOUT).strip()
@@ -111,9 +119,10 @@ def which(editor: str) -> Optional[str]:
111119
return editor_path
112120

113121
def is_text_file(file_path):
114-
"""
115-
Returns if a file contains only ASCII or UTF-8 encoded text
122+
"""Returns if a file contains only ASCII or UTF-8 encoded text
123+
116124
:param file_path: path to the file being checked
125+
:return: True if the file is a text file, False if it is binary.
117126
"""
118127
import codecs
119128

0 commit comments

Comments
 (0)