output.py :  » Web-Services » python-xmltv » pytvgrab-lib-0.5.1 » lib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Services » python xmltv 
python xmltv » pytvgrab lib 0.5.1 » lib » output.py
#!/usr/bin/env python

### COPYRIGHT NOTICE:
### This file was taken from Gentoo's portage package
### Original copyright notice:
#------------------------------------------------------------------------------
# Copyright 1998-2003 Daniel Robbins, Gentoo Technologies, Inc.
# Distributed under the GNU Public License v2
# $Header: /home/cvsroot/gentoo-src/portage/pym/output.py,v 1.17 2003/12/08 15:11:28 nakano Exp $
#------------------------------------------------------------------------------

import os
import sys
import re

from config_locale import toprint

havecolor=1
dotitles=1

codes={}
codes["reset"]="\x1b[0m"
codes["bold"]="\x1b[01m"

codes["teal"]="\x1b[36;06m"
codes["turquoise"]="\x1b[36;01m"

codes["fuscia"]="\x1b[35;01m"
codes["purple"]="\x1b[35;06m"

codes["blue"]="\x1b[34;01m"
codes["darkblue"]="\x1b[34;06m"

codes["green"]="\x1b[32;01m"
codes["darkgreen"]="\x1b[32;06m"

codes["yellow"]="\x1b[33;01m"
codes["brown"]="\x1b[33;06m"

codes["red"]="\x1b[31;01m"
codes["darkred"]="\x1b[31;06m"

def xtermTitle(mystr):
  if havecolor and dotitles and os.environ.has_key("TERM"):
    myt=os.environ["TERM"]
    if myt in ["xterm","Eterm","aterm","rxvt","screen"]:
      sys.stderr.write("\x1b]1;\x07\x1b]2;"+
           toprint( mystr ) +"\x07")
      sys.stderr.flush()

def xtermTitleReset():
  if havecolor and dotitles and os.environ.has_key("TERM"):
    myt=os.environ["TERM"]
    xtermTitle(os.environ["TERM"])


def notitles():
  "turn off title setting"
  dotitles=0

def nocolor():
  "turn off colorization"
  havecolor=0
  for x in codes.keys():
    codes[x]=""

def resetColor():
  return codes["reset"]

def ctext(color,text):
  return codes[ctext]+text+codes["reset"]

def bold(text):
  return codes["bold"]+text+codes["reset"]
def white(text):
  return bold(text)

def teal(text):
  return codes["teal"]+text+codes["reset"]
def turquoise(text):
  return codes["turquoise"]+text+codes["reset"]
def darkteal(text):
  return turquoise(text)

def fuscia(text):
  return codes["fuscia"]+text+codes["reset"]
def purple(text):
  return codes["purple"]+text+codes["reset"]

def blue(text):
  return codes["blue"]+text+codes["reset"]
def darkblue(text):
  return codes["darkblue"]+text+codes["reset"]

def green(text):
  return codes["green"]+text+codes["reset"]
def darkgreen(text):
  return codes["darkgreen"]+text+codes["reset"]

def yellow(text):
  return codes["yellow"]+text+codes["reset"]
def brown(text):
  return codes["brown"]+text+codes["reset"]
def darkyellow(text):
  return brown(text)

def red(text):
  return codes["red"]+text+codes["reset"]
def darkred(text):
  return codes["darkred"]+text+codes["reset"]


def strip_escape(s): return re.sub('\x1b.*?m', '', s)

def tabulate(heading, table):
  """
    >>> heading=[('a','A'), ('b', 'BB'), ('c','CCCCCCCCCCCCCCCC')]
    >>> table=[
           {'b':'there',       'a':'hello',   'c':'cat'    },
           {'b':'are',         'a':'howwwww', 'c':'dog'    },
           {'b':'this evening','a':'you',     'c':'fish'   },
           {'b':'chris',       'a':'mr',      'c':'giraffe'},
          ]
    >>> print tabulate(heading, table)
    A       BB           CCCCCCCCCCCCCCCC
    -------------------------------------
    hello   there        cat             
    howwwww are          dog             
    you     this evening fish            
    mr      chris        giraffe         
  """
  def get_max(x,y):
    result={}
    for k in x.keys():
      s1=strip_escape(x[k])
      s2=strip_escape(y[k])
      if len(s1) > len(s2):
        result[k]=s1
      else:
        result[k]=s2
    return result
  # get_max()

  max_lengths=dict(map(lambda x: (x[0], len(x[1])), reduce(get_max, table, dict(heading)).items()))

  result=''  
  for k,v in heading:
    v=bold(v)
    n=max_lengths[k]+len(v)-len(strip_escape(v))
    fmt='%%-%ss ' % n
    result+=fmt % v
  result=result[:-1]+'\n'

  result+='%s\n' % ('-'*reduce(lambda x,y: x+y, max_lengths.values(), len(max_lengths.values())-1))

  for tuple in table:
    for k,v in heading:
      n=max_lengths[k]+len(tuple[k])-len(strip_escape(tuple[k]))
      fmt='%%-%ss ' % n
      result+=fmt % tuple[k]
    result=result[:-1]+'\n'

  return toprint(result)
# tabulate()

#heading=[('a','A'), ('b', '\x1b[32;01mBB\x1b[0m'), ('c','CCCCCCCCCCCCCCCC')]
#table=[
#       {'b':'there',                 'a':'hello',  'c':'cat'    },
#       {'b':'\x1b[34;01mare\x1b[0m', 'a':'howwwww','c':'dog'    },
#       {'b':'this evening'         , 'a':'you',    'c':'fish'   },
#       {'b':'chris',                 'a':'mr',     'c':'giraffe'},
#      ]
#print tabulate(heading, table)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.