[Python-modules-commits] [ansi] 01/04: import ansi_0.1.3.orig.tar.gz

Muri Nicanor muri-guest at moszumanska.debian.org
Sun Sep 10 10:21:07 UTC 2017


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

muri-guest pushed a commit to branch master
in repository ansi.

commit 5007d42def1f89b73c6caeb65f77e8f9f3bf28e4
Author: Muri Nicanor <muri at immerda.ch>
Date:   Sun Sep 10 10:43:13 2017 +0200

    import ansi_0.1.3.orig.tar.gz
---
 LICENSE.md              | 19 +++++++++++++++
 PKG-INFO                | 41 +++++++++++++++++++++++++++++++
 README                  | 41 +++++++++++++++++++++++++++++++
 README.md               | 41 +++++++++++++++++++++++++++++++
 ansi/__init__.py        |  0
 ansi/_compat.py         |  8 ++++++
 ansi/color.py           |  4 +++
 ansi/colour/__init__.py |  1 +
 ansi/colour/base.py     | 34 ++++++++++++++++++++++++++
 ansi/colour/bg.py       | 42 ++++++++++++++++++++++++++++++++
 ansi/colour/fg.py       | 43 ++++++++++++++++++++++++++++++++
 ansi/colour/fx.py       | 49 +++++++++++++++++++++++++++++++++++++
 ansi/colour/rgb.py      | 65 +++++++++++++++++++++++++++++++++++++++++++++++++
 ansi/cursor.py          | 21 ++++++++++++++++
 ansi/sequence.py        | 20 +++++++++++++++
 setup.py                | 43 ++++++++++++++++++++++++++++++++
 16 files changed, 472 insertions(+)

diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 0000000..e290820
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,19 @@
+Copyright (c) 2015 Wijnand Modderman-Lenstra <maze at pyth0n.org>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/PKG-INFO b/PKG-INFO
new file mode 100644
index 0000000..11cc33c
--- /dev/null
+++ b/PKG-INFO
@@ -0,0 +1,41 @@
+Metadata-Version: 1.0
+Name: ansi
+Version: 0.1.3
+Summary: ANSI cursor movement and graphics
+Home-page: https://github.com/tehmaze/ansi/
+Author: Wijnand Modderman-Lenstra
+Author-email: maze at pyth0n.org
+License: UNKNOWN
+Description: 
+        ANSI
+        ====
+        
+        Various ANSI escape codes, used in moving the cursor in a text console or
+        rendering coloured text.
+        
+        
+        Example
+        -------
+        
+        Print something in bold yellow on a red background::
+        
+            >>> from ansi.colour import fg, bg, reset
+            >>> print map(str, [bg.red, fg.yellow, 'Hello world!', reset])
+            ...
+        
+        If you like syntactic sugar, you may also do::
+        
+            >>> print bg.red(fg.yellow('Hello world!'))
+            ...
+        
+        Also, 256 RGB colors are supported::
+        
+            >>> from ansi.colour import rgb, reset
+            >>> print rgb(0xff, 0x80, 0x00) + 'hello world' + reset
+            ...
+        
+        If you prefer to use American English in stead::
+        
+            >>> from ansi.color import ...
+        
+Platform: UNKNOWN
diff --git a/README b/README
new file mode 100644
index 0000000..c251874
--- /dev/null
+++ b/README
@@ -0,0 +1,41 @@
+ANSI
+====
+
+Various ANSI escape codes, used in moving the cursor in a text console or
+rendering coloured text.
+
+
+Example
+-------
+
+Print something in bold yellow on a red background:
+
+    >>> from ansi.colour import fg, bg
+    >>> from ansi.colour.fx import reset
+    >>> msg = (bg.red, fg.yellow, 'Hello world!', reset)
+    >>> print ''.join(map(str, msg))
+    ...
+
+If you like syntactic sugar, you may also do:
+
+    >>> from ansi.colour import fg, bg
+    >>> print bg.red(fg.yellow('Hello world!'))
+    ...
+
+Also, 256 RGB colours are supported:
+
+    >>> from ansi.colour.rgb import rgb256
+    >>> from ansi.colour.fx import reset
+    >>> msg = (rgb256(0xff, 0x80, 0x00), 'hello world', reset)
+    >>> print ''.join(map(str, msg))
+    ...
+
+If you prefer to use American English instead:
+
+    >>> from ansi.color import ...
+    
+
+References
+----------
+
+http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c251874
--- /dev/null
+++ b/README.md
@@ -0,0 +1,41 @@
+ANSI
+====
+
+Various ANSI escape codes, used in moving the cursor in a text console or
+rendering coloured text.
+
+
+Example
+-------
+
+Print something in bold yellow on a red background:
+
+    >>> from ansi.colour import fg, bg
+    >>> from ansi.colour.fx import reset
+    >>> msg = (bg.red, fg.yellow, 'Hello world!', reset)
+    >>> print ''.join(map(str, msg))
+    ...
+
+If you like syntactic sugar, you may also do:
+
+    >>> from ansi.colour import fg, bg
+    >>> print bg.red(fg.yellow('Hello world!'))
+    ...
+
+Also, 256 RGB colours are supported:
+
+    >>> from ansi.colour.rgb import rgb256
+    >>> from ansi.colour.fx import reset
+    >>> msg = (rgb256(0xff, 0x80, 0x00), 'hello world', reset)
+    >>> print ''.join(map(str, msg))
+    ...
+
+If you prefer to use American English instead:
+
+    >>> from ansi.color import ...
+    
+
+References
+----------
+
+http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
diff --git a/ansi/__init__.py b/ansi/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/ansi/_compat.py b/ansi/_compat.py
new file mode 100644
index 0000000..a046734
--- /dev/null
+++ b/ansi/_compat.py
@@ -0,0 +1,8 @@
+import sys
+
+PY2 = sys.version_info[0] == 2
+
+if PY2:
+    string_types = basestring
+else:
+    string_types = (str, bytes)
diff --git a/ansi/color.py b/ansi/color.py
new file mode 100644
index 0000000..56682a5
--- /dev/null
+++ b/ansi/color.py
@@ -0,0 +1,4 @@
+#pylint: disable=C0111,W0611,W0614
+from sys import modules
+import ansi.colour
+modules["ansi.color"] = ansi.colour
\ No newline at end of file
diff --git a/ansi/colour/__init__.py b/ansi/colour/__init__.py
new file mode 100644
index 0000000..dc33ef3
--- /dev/null
+++ b/ansi/colour/__init__.py
@@ -0,0 +1 @@
+__all__ = ['bg', 'fg', 'fx']
\ No newline at end of file
diff --git a/ansi/colour/base.py b/ansi/colour/base.py
new file mode 100644
index 0000000..ab0817a
--- /dev/null
+++ b/ansi/colour/base.py
@@ -0,0 +1,34 @@
+#pylint: disable=C0103,R0903
+
+from ansi.sequence import sequence
+from ansi._compat import string_types
+
+
+__all__ = ['Graphic']
+
+
+class Graphic(object):
+    '''
+    Compose a Select Graphic Rendition (SGR) ANSI escape sequence.
+    '''
+
+    def __init__(self, *values):
+        self.values = values
+        self.sequence = sequence('m', fields=-1)(*values)
+
+    def __add__(self, their):
+        if isinstance(their, str):
+            return ''.join([str(self), their])
+        elif isinstance(their, string_types):
+            raise ValueError('Use str, nothing else.')
+        else:
+            return Graphic(*(self.values + their.values))
+
+    def __call__(self, text, reset=True):
+        result = self.sequence + text
+        if reset:
+            result += str(Graphic('0'))
+        return result
+
+    def __str__(self):
+        return self.sequence
diff --git a/ansi/colour/bg.py b/ansi/colour/bg.py
new file mode 100644
index 0000000..d1af2b3
--- /dev/null
+++ b/ansi/colour/bg.py
@@ -0,0 +1,42 @@
+#pylint: disable=C0103,R0903
+
+from ansi.colour.base import Graphic
+from ansi.colour.fx import bold
+
+# ECMA-048 standard names
+black           = Graphic('40')
+red             = Graphic('41')
+green           = Graphic('42')
+yellow          = Graphic('43')
+blue            = Graphic('44')
+magenta         = Graphic('45')
+cyan            = Graphic('46')
+white           = Graphic('47')
+default         = Graphic('49')
+
+# ECMA-048 bold variants
+boldblack       = bold + black
+boldred         = bold + red
+boldgreen       = bold + green
+boldyellow      = bold + yellow
+boldblue        = bold + blue
+boldmagenta     = bold + magenta
+boldcyan        = bold + cyan
+boldwhite       = bold + white
+
+# Convenience wrappers
+brown           = yellow                # Not in ANSI/ECMA-048 standard
+grey            = white                 # Not in ANSI/ECMA-048 standard
+gray            = white                 # US English
+darkgrey        = boldblack
+darkgray        = boldblack             # US English
+brightred       = boldred
+brightgreen     = boldgreen
+brightyellow    = boldyellow
+brightbrown     = boldyellow            # Not in ANSI/ECMA-048 standard
+brightblue      = boldblue
+brightmagenta   = boldmagenta
+brightcyan      = boldcyan
+brightwhite     = boldwhite
+brightgrey      = boldwhite             # Not in ANSI/ECMA-048 standard
+brightgray      = boldwhite             # Us English
diff --git a/ansi/colour/fg.py b/ansi/colour/fg.py
new file mode 100644
index 0000000..b32c5d5
--- /dev/null
+++ b/ansi/colour/fg.py
@@ -0,0 +1,43 @@
+#pylint: disable=C0103,R0903
+
+from ansi.colour.base import Graphic
+from ansi.colour.fx import bold
+
+
+# ECMA-048 standard names
+black           = Graphic('30')
+red             = Graphic('31')
+green           = Graphic('32')
+yellow          = Graphic('33')
+blue            = Graphic('34')
+magenta         = Graphic('35')
+cyan            = Graphic('36')
+white           = Graphic('37')
+default         = Graphic('39')
+
+# ECMA-048 bold variants
+boldblack       = bold + black
+boldred         = bold + red
+boldgreen       = bold + green
+boldyellow      = bold + yellow
+boldblue        = bold + blue
+boldmagenta     = bold + magenta
+boldcyan        = bold + cyan
+boldwhite       = bold + white
+
+# Convenience wrappers
+brown           = yellow                # Not in ANSI/ECMA-048 standard
+grey            = white                 # Not in ANSI/ECMA-048 standard
+gray            = white                 # US English
+darkgrey        = boldblack
+darkgray        = boldblack             # US English
+brightred       = boldred
+brightgreen     = boldgreen
+brightyellow    = boldyellow
+brightbrown     = boldyellow            # Not in ANSI/ECMA-048 standard
+brightblue      = boldblue
+brightmagenta   = boldmagenta
+brightcyan      = boldcyan
+brightwhite     = boldwhite
+brightgrey      = boldwhite             # Not in ANSI/ECMA-048 standard
+brightgray      = boldwhite             # Us English
diff --git a/ansi/colour/fx.py b/ansi/colour/fx.py
new file mode 100644
index 0000000..30098a2
--- /dev/null
+++ b/ansi/colour/fx.py
@@ -0,0 +1,49 @@
+#pylint: disable=C0103,R0903
+
+from ansi.colour.base import Graphic
+
+
+# ECMA-048 standard names
+reset            = Graphic('0')
+bold             = Graphic('1')
+faint            = Graphic('2')
+italic           = Graphic('3')
+underline        = Graphic('4')
+blink_slow       = Graphic('5')
+blink            = Graphic('6')
+negative         = Graphic('7')
+conceal          = Graphic('8')
+crossed_out      = Graphic('9')
+font_reset       = Graphic('10')
+font_1           = Graphic('11')
+font_2           = Graphic('12')
+font_3           = Graphic('13')
+font_4           = Graphic('14')
+font_5           = Graphic('15')
+font_6           = Graphic('16')
+font_7           = Graphic('17')
+font_8           = Graphic('18')
+font_9           = Graphic('19')
+fraktur          = Graphic('20')
+gothic           = Graphic('20')
+underline_double = Graphic('21')
+normal           = Graphic('22')
+not_italic       = Graphic('23')
+not_fraktur      = Graphic('23')
+not_gothic       = Graphic('23')
+not_underline    = Graphic('24')
+steady           = Graphic('25')
+positive         = Graphic('27')
+reveal           = Graphic('28')
+framed           = Graphic('51')
+encircled        = Graphic('52')
+overlined        = Graphic('53')
+not_framed       = Graphic('54')
+not_encircled    = Graphic('54')
+not_overlined    = Graphic('55')
+
+# Convenience wrappers
+inverse          = negative
+bright           = bold
+not_blink        = steady
+blink_off        = steady
diff --git a/ansi/colour/rgb.py b/ansi/colour/rgb.py
new file mode 100644
index 0000000..fdb3c57
--- /dev/null
+++ b/ansi/colour/rgb.py
@@ -0,0 +1,65 @@
+from ansi.sequence import sequence
+
+
+ANSI_COLOURS = (
+    (0x00, 0x00, 0x00), (0xcd, 0x00, 0x00),
+    (0x00, 0xcd, 0x00), (0xcd, 0xcd, 0x00),
+    (0x00, 0x00, 0xee), (0xcd, 0x00, 0xcd),
+    (0x00, 0xcd, 0xcd), (0xe5, 0xe5, 0xe5),
+    (0x7f, 0x7f, 0x7f), (0xff, 0x00, 0x00),
+    (0x00, 0xff, 0x00), (0xff, 0xff, 0x00),
+    (0x5c, 0x5c, 0xff), (0xff, 0x00, 0xff),
+    (0x00, 0xff, 0xff), (0xff, 0xff, 0xff),
+)
+
+def rgb_distance(rgb1, rgb2):
+    '''
+    Calculate the distance between two RGB sequences.
+    '''
+    return sum(map(lambda c: (c[0] - c[1]) ** 2,
+        zip(rgb1, rgb2)))
+
+def rgb_reduce(r, g, b, mode=8):
+    '''
+    Convert an RGB colour to 8 or 16 colour ANSI graphics.
+    '''
+    colours = ANSI_COLOURS[:mode]
+    matches = [(rgb_distance(c, map(int, [r, g, b])), i)
+        for i, c in enumerate(colours)]
+    matches.sort()
+    return sequence('m')(str(30 + matches[0][1]))
+
+def rgb8(r, g, b):
+    '''
+    Convert an RGB colour to 8 colour ANSI graphics.
+    '''
+    return rgb_reduce(r, g, b, 8)
+
+def rgb16(r, g, b):
+    '''
+    Convert an RGB colour to 16 colour ANSI graphics.
+    '''
+    return rgb_reduce(r, g, b, 16)
+
+def rgb256(r, g, b):
+    '''
+    Convert an RGB colour to 256 colour ANSI graphics.
+    '''
+    grey = False
+    poss = True
+    step = 2.5
+
+    while poss: # As long as the colour could be grey scale
+        if r < step or g < step or b < step:
+            grey = r < step and g < step and b < step
+            poss = False
+
+        step += 42.5
+
+    if grey:
+        colour = 232 + int(float(sum([r, g, b]) / 33.0))
+    else:
+        colour = sum([16] + [int (6 * float(val) / 256) * mod
+            for val, mod in ((r, 36), (g, 6), (b, 1))])
+
+    return sequence('m', fields=3)(38, 5, colour)
diff --git a/ansi/cursor.py b/ansi/cursor.py
new file mode 100644
index 0000000..4eaa11b
--- /dev/null
+++ b/ansi/cursor.py
@@ -0,0 +1,21 @@
+#pylint: disable=C0103
+
+from ansi.sequence import sequence
+
+
+# Cursor movement
+up          = sequence('A')
+down        = sequence('B')
+forward     = sequence('C')
+back        = sequence('D')
+next_line   = sequence('E')
+prev_line   = sequence('F')
+goto_x      = sequence('H')
+goto        = sequence('G', 2)
+erase       = sequence('J')
+erase_data  = erase
+erase_line  = sequence('K')
+scroll_up   = sequence('S')
+scroll_down = sequence('T')
+save_cursor = sequence('s')
+load_cursor = sequence('u')
diff --git a/ansi/sequence.py b/ansi/sequence.py
new file mode 100644
index 0000000..a477f22
--- /dev/null
+++ b/ansi/sequence.py
@@ -0,0 +1,20 @@
+CSI = '\x1b['
+
+
+def sequence(letter, fields=1, default=[]):
+    def _sequence(*values):
+        output = list(values)
+        if fields >= 0 and len(output) > fields:
+            raise ValueError('Invalid number of fields, got %d expected %d' %
+                (len(output), fields))
+
+        while len(output) < fields and len(default) > len(output):
+            output.append(fields[len(default) - 1])
+
+        return ''.join([
+            CSI,
+            ';'.join(map(str, output)),
+            letter,
+        ])
+
+    return _sequence
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..6cea442
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+from distutils.core import setup
+
+setup(name='ansi',
+      version='0.1.3',
+      description='ANSI cursor movement and graphics',
+      author='Wijnand Modderman-Lenstra',
+      author_email='maze at pyth0n.org',
+      url='https://github.com/tehmaze/ansi/',
+      packages = ['ansi', 'ansi.colour'],
+      long_description='''
+ANSI
+====
+
+Various ANSI escape codes, used in moving the cursor in a text console or
+rendering coloured text.
+
+
+Example
+-------
+
+Print something in bold yellow on a red background::
+
+    >>> from ansi.colour import fg, bg, reset
+    >>> print map(str, [bg.red, fg.yellow, 'Hello world!', reset])
+    ...
+
+If you like syntactic sugar, you may also do::
+
+    >>> print bg.red(fg.yellow('Hello world!'))
+    ...
+
+Also, 256 RGB colors are supported::
+
+    >>> from ansi.colour import rgb, reset
+    >>> print rgb(0xff, 0x80, 0x00) + 'hello world' + reset
+    ...
+
+If you prefer to use American English in stead::
+
+    >>> from ansi.color import ...
+''')

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



More information about the Python-modules-commits mailing list