Commits

Andy Mikhailenko committed 3acca6b

Re issue #24: rename argh.output to argh.io and move input-related stuff from argh.interaction to argh.io.

  • Participants
  • Parent commits f3616dc

Comments (0)

Files changed (6)

 .. automodule:: argh.exceptions
    :members:
 
-.. automodule:: argh.output
+.. automodule:: argh.io
    :members:
 
 .. automodule:: argh.utils

argh/dispatching.py

 from argh.completion import autocomplete
 from argh.assembling import add_commands, set_default_command
 from argh.exceptions import CommandError
-from argh import output
+from argh import io
 
 
 __all__ = ['dispatch', 'dispatch_command', 'dispatch_commands']
         # displayed to the user before anything else happens, e.g.
         # raw_input() is called
 
-        output.dump(line, f)
+        io.dump(line, f)
         if not raw_output:
             # in most cases user wants on message per line
-            output.dump('\n', f)
+            io.dump('\n', f)
 
     if output_file is None:
         # user wanted a string; return contents of our temporary file-like obj

argh/interaction.py

 Interaction
 ===========
 """
-from argh.six import text_type, PY3
+from argh.six import text_type
+from argh.io import safe_input
 
 
 __all__ = ['confirm']
 
 
-def _input(prompt):
-    # this function can be mocked up in tests
-    if PY3:
-        return input(prompt)
-    else:
-        return raw_input(prompt)
-
-
-def safe_input(prompt):
-    "Prompts user for input. Correctly handles prompt message encoding."
-
-    if PY3:
-        if not isinstance(prompt, text_type):
-            # Python 3.x: bytes →  unicode
-            prompt = prompt.decode()
-    else:
-        if isinstance(prompt, text_type):
-            # Python 2.x: unicode →  bytes
-            prompt = prompt.encode('utf-8')
-
-    return _input(prompt)
-
-
 def confirm(action, default=None, skip=False):
     """A shortcut for typical confirmation prompt.
 
+# -*- coding: utf-8 -*-
+#
+#  Copyright (c) 2010—2012 Andrey Mikhailenko and contributors
+#
+#  This file is part of Argh.
+#
+#  Argh is free software under terms of the GNU Lesser
+#  General Public License version 3 (LGPLv3) as published by the Free
+#  Software Foundation. See the file README for copying conditions.
+#
+"""
+Output Processing
+=================
+"""
+import locale
+
+from argh.six import binary_type, text_type, PY3
+
+
+__all__ = ['dump', 'encode_output', 'safe_input']
+
+
+def _input(prompt):
+    # this function can be mocked up in tests
+    if PY3:
+        return input(prompt)
+    else:
+        return raw_input(prompt)
+
+
+def safe_input(prompt):
+    "Prompts user for input. Correctly handles prompt message encoding."
+
+    if PY3:
+        if not isinstance(prompt, text_type):
+            # Python 3.x: bytes →  unicode
+            prompt = prompt.decode()
+    else:
+        if isinstance(prompt, text_type):
+            # Python 2.x: unicode →  bytes
+            encoding = locale.getpreferredencoding() or 'utf-8'
+            prompt = prompt.encode(encoding)
+
+    return _input(prompt)
+
+
+def encode_output(value, output_file):
+    """ Encodes given value so it can be written to given file object.
+
+    Value may be Unicode, binary string or any other data type.
+
+    The exact behaviour depends on the Python version:
+
+    Python 3.x
+
+        `sys.stdout` is a `_io.TextIOWrapper` instance that accepts `str`
+        (unicode) and breaks on `bytes`.
+
+        It is OK to simply assume that everything is Unicode unless special
+        handling is introduced in the client code.
+
+        Thus, no additional processing is performed.
+
+    Python 2.x
+
+        `sys.stdout` is a file-like object that accepts `str` (bytes)
+        and breaks when `unicode` is passed to `sys.stdout.write()`.
+
+        We can expect both Unicode and bytes. They need to be encoded so as
+        to match the file object encoding.
+
+        The output is binary if the object is a TTY; otherwise it's Unicode.
+
+    """
+    if not PY3:
+        # handle special cases in Python 2.x
+
+        if output_file.isatty():
+            # TTY expects bytes
+            if isinstance(value, text_type):
+                encoding = getattr(output_file, 'encoding', None)
+                encoding = encoding or locale.getpreferredencoding() or 'utf-8'
+                # unicode →  binary
+                return value.encode(encoding)
+            return binary_type(value)
+
+        if isinstance(value, binary_type):
+            # binary →  unicode
+            return value.decode('utf-8')
+
+    # whatever → unicode
+    return text_type(value)
+
+
+def dump(raw_data, output_file):
+    """ Writes given line to given output file.
+    See :func:`encode_output` for details.
+    """
+    data = encode_output(raw_data, output_file)
+    output_file.write(data)

argh/output.py

-# -*- coding: utf-8 -*-
-#
-#  Copyright (c) 2010—2012 Andrey Mikhailenko and contributors
-#
-#  This file is part of Argh.
-#
-#  Argh is free software under terms of the GNU Lesser
-#  General Public License version 3 (LGPLv3) as published by the Free
-#  Software Foundation. See the file README for copying conditions.
-#
-"""
-Output Processing
-=================
-"""
-import locale
-
-from argh.six import binary_type, text_type, PY3
-
-
-__all__ = ['dump', 'encode_output']
-
-
-def encode_output(value, output_file):
-    """ Encodes given value so it can be written to given file object.
-
-    Value may be Unicode, binary string or any other data type.
-
-    The exact behaviour depends on the Python version:
-
-    Python 3.x
-
-        `sys.stdout` is a `_io.TextIOWrapper` instance that accepts `str`
-        (unicode) and breaks on `bytes`.
-
-        It is OK to simply assume that everything is Unicode unless special
-        handling is introduced in the client code.
-
-        Thus, no additional processing is performed.
-
-    Python 2.x
-
-        `sys.stdout` is a file-like object that accepts `str` (bytes)
-        and breaks when `unicode` is passed to `sys.stdout.write()`.
-
-        We can expect both Unicode and bytes. They need to be encoded so as
-        to match the file object encoding.
-
-        The output is binary if the object is a TTY; otherwise it's Unicode.
-
-    """
-    if not PY3:
-        # handle special cases in Python 2.x
-
-        if output_file.isatty():
-            # TTY expects bytes
-            if isinstance(value, text_type):
-                encoding = getattr(output_file, 'encoding', None)
-                encoding = encoding or locale.getpreferredencoding() or 'utf-8'
-                # unicode →  binary
-                return value.encode(encoding)
-            return binary_type(value)
-
-        if isinstance(value, binary_type):
-            # binary →  unicode
-            return value.decode('utf-8')
-
-    # whatever → unicode
-    return text_type(value)
-
-
-def dump(raw_data, output_file):
-    """ Writes given line to given output file.
-    See :func:`encode_output` for details.
-    """
-    data = encode_output(raw_data, output_file)
-    output_file.write(data)
 
 class ConfirmTestCase(unittest.TestCase):
     def assert_choice(self, choice, expected, **kwargs):
-        argh.interaction._input = lambda prompt: choice
+        argh.io._input = lambda prompt: choice
         self.assertEqual(argh.confirm('test', **kwargs), expected)
 
     def test_simple(self):
 
         def raw_input_mock(prompt):
             prompts.append(prompt)
-        argh.interaction._input = raw_input_mock
+        argh.io._input = raw_input_mock
 
         argh.confirm('do smth')
         self.assertEqual(prompts[-1], 'do smth? (y/n)')
         def raw_input_mock(prompt):
             if not PY3:
                 assert isinstance(prompt, binary_type)
-        argh.interaction._input = raw_input_mock
+        argh.io._input = raw_input_mock
         argh.confirm(u('привет'))