[Python-modules-commits] [python-clint] 02/05: Imported Upstream version 0.5.1

Takaki Taniguchi takaki at moszumanska.debian.org
Wed Nov 25 13:12:42 UTC 2015


This is an automated email from the git hooks/post-receive script.

takaki pushed a commit to branch master
in repository python-clint.

commit 85e755b7a8e8e0b88d434ef3c5e41a4cccd809a0
Author: TANIGUCHI Takaki <takaki at asis.media-as.org>
Date:   Wed Nov 25 22:10:04 2015 +0900

    Imported Upstream version 0.5.1
---
 .DS_Store                   | Bin 12292 -> 6148 bytes
 .gitignore                  |  42 ++++++++++++++++----
 AUTHORS                     |   1 +
 HISTORY.rst                 |   9 +++++
 PKG-INFO                    |  17 ++++++--
 README.rst                  |   2 +-
 clint.egg-info/PKG-INFO     |  17 ++++++--
 clint.egg-info/SOURCES.txt  |   1 +
 clint.egg-info/pbr.json     |   1 +
 clint.egg-info/requires.txt |   2 +-
 clint/__init__.py           |   4 +-
 clint/textui/formatters.py  |  32 +++++++++++----
 clint/textui/prompt.py      |  94 +++++++++++++++++++++++++++++++++++++++++++-
 clint/textui/validators.py  |  18 +++++++++
 setup.py                    |   8 ++--
 test_clint.py               |  25 ++++++++++++
 16 files changed, 243 insertions(+), 30 deletions(-)

diff --git a/.DS_Store b/.DS_Store
index 90233a3..178bd76 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/.gitignore b/.gitignore
index d7a8809..bf54ee8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,11 +1,37 @@
-.idea
+*.py[cod]
+
+# Packages
+*.egg
+*.egg-info
+dist
+build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+lib
+lib64
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+nosetests.xml
+
+# Translations
+*.mo
+
+# Mr Developer
+.mr.developer.cfg
 .project
-.settings
 .pydevproject
-MANIFEST
-*.pyc
-.tox
-build/
-clint.egg-info/
-dist/
 
+*.DS_Store
+*.dylib
+.idea/
+docs/_*
diff --git a/AUTHORS b/AUTHORS
index af8b8e7..b251a66 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -31,3 +31,4 @@ Patches and Suggestions
 - Eric Anderson
 - Joshua Richardson
 - Brandon Liu <thenovices>
+- Kentaro Wada <www.kentaro.wada at gmail.com>
diff --git a/HISTORY.rst b/HISTORY.rst
index 21c978a..533aa3e 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -1,6 +1,15 @@
 History
 -------
 
+0.5.1
++++++
+* Fix line width calculation in max_width when using coloured text (thanks to @wkentaro) 
+
+0.5.0
++++++
+* Added option prompt
+
+
 0.4.1
 +++++
 * Fix bug in logic that decides whether progress bars should be hidden or not
diff --git a/PKG-INFO b/PKG-INFO
index d6e71b9..22f8630 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,7 +1,7 @@
 Metadata-Version: 1.1
 Name: clint
-Version: 0.4.1
-Summary: Python Command-line Application Tools
+Version: 0.5.1
+Summary: Python Command Line Interface Tools
 Home-page: https://github.com/kennethreitz/clint
 Author: Kenneth Reitz
 Author-email: me at kennethreitz.com
@@ -82,7 +82,7 @@ Description: Clint: Python Command-line Application Tools
         
         I want to color my console text. ::
         
-            >>> from clint.textui import colored
+            >>> from clint.textui import colored, puts
         
             >>> puts(colored.red('red text'))
             red text
@@ -183,6 +183,15 @@ Description: Clint: Python Command-line Application Tools
         History
         -------
         
+        0.5.1
+        +++++
+        * Fix line width calculation in max_width when using coloured text (thanks to @wkentaro) 
+        
+        0.5.0
+        +++++
+        * Added option prompt
+        
+        
         0.4.1
         +++++
         * Fix bug in logic that decides whether progress bars should be hidden or not
@@ -315,4 +324,6 @@ Classifier: Programming Language :: Python :: 2.7
 Classifier: Programming Language :: Python :: 3
 Classifier: Programming Language :: Python :: 3.1
 Classifier: Programming Language :: Python :: 3.2
+Classifier: Programming Language :: Python :: 3.3
+Classifier: Programming Language :: Python :: 3.4
 Classifier: Topic :: Terminals :: Terminal Emulators/X Terminals
diff --git a/README.rst b/README.rst
index bddda80..bb2064c 100644
--- a/README.rst
+++ b/README.rst
@@ -74,7 +74,7 @@ I want to quote my console text (like email). ::
 
 I want to color my console text. ::
 
-    >>> from clint.textui import colored
+    >>> from clint.textui import colored, puts
 
     >>> puts(colored.red('red text'))
     red text
diff --git a/clint.egg-info/PKG-INFO b/clint.egg-info/PKG-INFO
index d6e71b9..22f8630 100644
--- a/clint.egg-info/PKG-INFO
+++ b/clint.egg-info/PKG-INFO
@@ -1,7 +1,7 @@
 Metadata-Version: 1.1
 Name: clint
-Version: 0.4.1
-Summary: Python Command-line Application Tools
+Version: 0.5.1
+Summary: Python Command Line Interface Tools
 Home-page: https://github.com/kennethreitz/clint
 Author: Kenneth Reitz
 Author-email: me at kennethreitz.com
@@ -82,7 +82,7 @@ Description: Clint: Python Command-line Application Tools
         
         I want to color my console text. ::
         
-            >>> from clint.textui import colored
+            >>> from clint.textui import colored, puts
         
             >>> puts(colored.red('red text'))
             red text
@@ -183,6 +183,15 @@ Description: Clint: Python Command-line Application Tools
         History
         -------
         
+        0.5.1
+        +++++
+        * Fix line width calculation in max_width when using coloured text (thanks to @wkentaro) 
+        
+        0.5.0
+        +++++
+        * Added option prompt
+        
+        
         0.4.1
         +++++
         * Fix bug in logic that decides whether progress bars should be hidden or not
@@ -315,4 +324,6 @@ Classifier: Programming Language :: Python :: 2.7
 Classifier: Programming Language :: Python :: 3
 Classifier: Programming Language :: Python :: 3.1
 Classifier: Programming Language :: Python :: 3.2
+Classifier: Programming Language :: Python :: 3.3
+Classifier: Programming Language :: Python :: 3.4
 Classifier: Topic :: Terminals :: Terminal Emulators/X Terminals
diff --git a/clint.egg-info/SOURCES.txt b/clint.egg-info/SOURCES.txt
index 34cd937..c602c7c 100644
--- a/clint.egg-info/SOURCES.txt
+++ b/clint.egg-info/SOURCES.txt
@@ -19,6 +19,7 @@ clint/utils.py
 clint.egg-info/PKG-INFO
 clint.egg-info/SOURCES.txt
 clint.egg-info/dependency_links.txt
+clint.egg-info/pbr.json
 clint.egg-info/requires.txt
 clint.egg-info/top_level.txt
 clint/packages/__init__.py
diff --git a/clint.egg-info/pbr.json b/clint.egg-info/pbr.json
new file mode 100644
index 0000000..b60ddc4
--- /dev/null
+++ b/clint.egg-info/pbr.json
@@ -0,0 +1 @@
+{"is_release": false, "git_version": "fa79d0d"}
\ No newline at end of file
diff --git a/clint.egg-info/requires.txt b/clint.egg-info/requires.txt
index a19a006..9564f5a 100644
--- a/clint.egg-info/requires.txt
+++ b/clint.egg-info/requires.txt
@@ -1 +1 @@
-args
\ No newline at end of file
+args
diff --git a/clint/__init__.py b/clint/__init__.py
index 198877e..a8d5a62 100644
--- a/clint/__init__.py
+++ b/clint/__init__.py
@@ -26,8 +26,8 @@ from .pipes import piped_in
 
 
 __title__ = 'clint'
-__version__ = '0.4.1'
-__build__ = 0x000401
+__version__ = '0.5.1'
+__build__ = 0x000501
 __author__ = 'Kenneth Reitz'
 __license__ = 'ISC'
 __copyright__ = 'Copyright 2012 Kenneth Reitz'
diff --git a/clint/textui/formatters.py b/clint/textui/formatters.py
index 4bc2f10..9962351 100644
--- a/clint/textui/formatters.py
+++ b/clint/textui/formatters.py
@@ -34,16 +34,27 @@ def min_width(string, cols, padding=' '):
 
 
 def max_width(string, cols, separator='\n'):
-    """Returns a freshly formatted """
+    """Returns a freshly formatted
+    :param string: string to be formatted
+    :type string: basestring or clint.textui.colorred.ColoredString
+    :param cols: max width the text to be formatted
+    :type cols: int
+    :param separator: separator to break rows
+    :type separator: basestring
+
+        >>> formatters.max_width('123 5678', 8)
+        '123 5678'
+        >>> formatters.max_width('123 5678', 7)
+        '123 \n5678'
+
+    """
 
     is_color = isinstance(string, ColoredString)
 
     if is_color:
-        offset = 10
         string_copy = string._new('')
-    else:
-        offset = 0
-        
+        string = string.s
+
     stack = tsplit(string, NEWLINES)
 
     for i, substring in enumerate(stack):
@@ -56,31 +67,36 @@ def max_width(string, cols, separator='\n'):
         _row_i = 0
 
         for word in row:
-            if (len(_row[_row_i]) + len(word)) < (cols + offset):
+            if (len(_row[_row_i]) + len(word)) <= cols:
                 _row[_row_i] += word
                 _row[_row_i] += ' '
                 
-            elif len(word) > (cols - offset):
+            elif len(word) > cols:
 
                 # ensure empty row
                 if len(_row[_row_i]):
+                    _row[_row_i] = _row[_row_i].rstrip()
                     _row.append('')
                     _row_i += 1
 
-                chunks = schunk(word, (cols + offset))
+                chunks = schunk(word, cols)
                 for i, chunk in enumerate(chunks):
                     if not (i + 1) == len(chunks):
                         _row[_row_i] += chunk
+                        _row[_row_i] = _row[_row_i].rstrip()
                         _row.append('')
                         _row_i += 1
                     else:
                         _row[_row_i] += chunk
                         _row[_row_i] += ' '
             else:
+                _row[_row_i] = _row[_row_i].rstrip()
                 _row.append('')
                 _row_i += 1
                 _row[_row_i] += word
                 _row[_row_i] += ' '
+        else:
+            _row[_row_i] = _row[_row_i].rstrip()
 
         _row = map(str, _row)
         _stack.append(separator.join(_row))
diff --git a/clint/textui/prompt.py b/clint/textui/prompt.py
index 7a76dac..7995e97 100644
--- a/clint/textui/prompt.py
+++ b/clint/textui/prompt.py
@@ -14,7 +14,7 @@ from re import match, I
 
 from .core import puts
 from .colored import yellow
-from .validators import RegexValidator
+from .validators import RegexValidator, OptionValidator
 
 try:
     raw_input
@@ -88,3 +88,95 @@ def query(prompt, default='', validators=None, batch=False):
             return user_input
         except Exception as e:
             puts(yellow(e.message))
+
+
+
+def options(prompt, options, default=None, batch=False):
+    '''
+
+    :param prompt:
+    :param options:
+        this can be either a list of strings, in which case it will be presented like:
+        prompt:
+            (1) this is the first string
+            (2) this is the second string
+            (3) this is the third string
+
+            please select 1-3:
+
+        or a list of dictionaries in the format of:
+            { { 'selector' : 'this is what the user will enter to select the option'
+                'prompt': 'this is the string that will be displayed, this can be omitted if the selector is also a prompt',
+                'return': 'this is what is returned to the calling procedure, if omitted, the option selector will be used' }
+
+        so, to replicate the above, the dict could look like:
+
+        [ {'selector':1,'prompt':'this is the first string','return':1},
+          {'selector':2,'prompt':'this is the second string','return':2},
+          {'selector':3,'prompt':'this is the third string'}
+
+    :param default: should be set to the default selector (if desired)
+    :param batch: True/False, will auto-return the default
+    :return:
+    '''
+    # Build fix options and build validator
+
+    validator_list = []
+    return_dict = {}
+
+    if isinstance(options[0],dict):
+        for item in options:
+            item['selector'] = str(item['selector'])
+            item['prompt'] = str(item['prompt'])
+            if 'return' not in item:
+                item['return'] = item['selector']
+            validator_list.append(item['selector'])
+            return_dict[item['selector']] = item['return']
+    else:
+        options_strings = options
+        options = []
+        for key, opt in enumerate(options_strings):
+            item = {}
+            item['selector'] = str(key+1)
+            item['prompt'] = str(opt)
+            item['return'] = key+1
+
+            return_dict[item['selector']] = item['return']
+            validator_list.append(item['selector'])
+            options.append(item)
+
+    validators = [OptionValidator(validator_list)]
+
+    # Let's build the prompt
+
+    prompt += '\n'
+
+    # building the options list
+    for o in options:
+        prompt += '[{selector}] {prompt}\n'.format(**o)
+
+    prompt += '\n'
+
+    if default:
+        prompt += '[' + default + '] '
+
+    # If input is not valid keep asking
+    while True:
+        # If batch option is True then auto reply
+        # with default input
+        if not batch:
+            user_input = raw_input(prompt).strip() or default
+        else:
+            print(prompt)
+            user_input = ''
+
+
+        # Validate the user input
+        try:
+            for validator in validators:
+                user_input = validator(user_input)
+                # convert user input to defined return value
+                user_input = return_dict[user_input]
+            return user_input
+        except Exception as e:
+            puts(yellow(e.message))
diff --git a/clint/textui/validators.py b/clint/textui/validators.py
index 7a12094..78100b3 100644
--- a/clint/textui/validators.py
+++ b/clint/textui/validators.py
@@ -102,3 +102,21 @@ class IntegerValidator(object):
             return int(value)
         except (TypeError, ValueError):
             raise ValidationError(self.message)
+
+class OptionValidator(object):
+    message = 'Select from the list of valid options.'
+
+    def __init__(self, options, message=None):
+        self.options = options
+        if message is not None:
+            self.message = message
+
+    def __call__(self, value):
+        """
+        Validates that the input is in the options list.
+        """
+        if value in self.options:
+            return value
+        else:
+            raise ValidationError(self.message)
+
diff --git a/setup.py b/setup.py
index e1dd436..8b306e1 100755
--- a/setup.py
+++ b/setup.py
@@ -24,7 +24,7 @@ required = ['args']
 setup(
     name='clint',
     version=clint.__version__,
-    description='Python Command-line Application Tools',
+    description='Python Command Line Interface Tools',
     long_description=open('README.rst').read() + '\n\n' +
                      open('HISTORY.rst').read(),
     author='Kenneth Reitz',
@@ -37,7 +37,7 @@ setup(
     ],
     install_requires=required,
     license='ISC',
-    classifiers=(
+    classifiers=[
 #       'Development Status :: 5 - Production/Stable',
         'Environment :: Console',
         'Intended Audience :: Developers',
@@ -50,6 +50,8 @@ setup(
         'Programming Language :: Python :: 3',
         'Programming Language :: Python :: 3.1',
         'Programming Language :: Python :: 3.2',
+        'Programming Language :: Python :: 3.3',
+        'Programming Language :: Python :: 3.4',
         'Topic :: Terminals :: Terminal Emulators/X Terminals',
-    ),
+    ],
 )
diff --git a/test_clint.py b/test_clint.py
index c0c7d58..0e22548 100755
--- a/test_clint.py
+++ b/test_clint.py
@@ -58,5 +58,30 @@ class ColoredStringTestCase(unittest.TestCase):
         assert new_str.always_color == True
 
 
+class TextuiFormatterTestCase(unittest.TestCase):
+
+    def test_max_width(self):
+
+        def _test_n_rows_width(ins, rows, n_rows, max_width):
+            ins.assertEqual(len(rows), n_rows)
+            for row in rows:
+                ins.assertLessEqual(len(row), max_width)
+
+        from clint.textui.formatters import max_width
+        from clint.textui import colored
+        # normal text
+        text = ' '.join(['XXX'] * 3)
+        rows = max_width(text, 6).split('\n')
+        _test_n_rows_width(self, rows, 3, 6)
+        rows = max_width(text, 7).split('\n')
+        _test_n_rows_width(self, rows, 2, 7)
+        # colored text
+        c_text = colored.yellow(text)
+        rows = max_width(c_text, 6).split('\n')
+        _test_n_rows_width(self, rows, 3, 6)
+        rows = max_width(c_text, 7).split('\n')
+        _test_n_rows_width(self, rows, 2, 7)
+
+
 if __name__ == '__main__':
     unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-clint.git



More information about the Python-modules-commits mailing list