Package linda :: Module output
[hide private]

Source Code for Module linda.output

  1  import sys, os, re, textwrap, traceback, linda 
  2  from linda import clparser 
  3  from linda.debug import dprint 
  4  from linda.mygettext import translation 
  5   
6 -class Output:
7 - def __init__(self):
8 self.check_type = self.tag = self.pkg_name = self.desc = '' 9 self.level = 0 10 self.exit_status = 0 11 if hasattr(self, 'init'): 12 getattr(self, 'init')()
13
14 - def add_type(self, type, level):
15 self.check_type = type 16 self.level = level
17
18 - def type_single_char(self):
19 for x in self.type: 20 if x.isupper(): 21 self.type_char = x 22 break
23
24 - def change_exit_status(self):
25 if self.type_char == 'E' and self.exit_status == 2: 26 self.exit_status = 2
27
28 - def fetch_desc(self):
29 self.description = [] 30 for desc in self.return_trans(self.tag): 31 self.description.append(desc) 32 self.description[1] = '\n'.join(map(lambda x: ' %s' % x, \ 33 textwrap.wrap(self.description[1])))
34
35 - def check_formatargs(self):
36 formatarg = len(re.findall(r'(?<!%)%\d*[asdful]', self.description[0])) 37 if formatarg != len(self.data): 38 dprint(_("Description: \"%s\"; Data: \"%s\"") % \ 39 (self.description[0], self.data), 3) 40 raise OutputException("Format args for %s don't match Description. (%d vs %d)" % (self.tag, formatarg, len(self.data)))
41
42 - def print_out(self, tag, data, output_data, pkg_name):
43 self.tag = tag 44 self.data = data 45 self.output_data = output_data 46 self.pkg_name = pkg_name 47 self.type = self.output_data['Type'] 48 self.type_single_char() 49 self.fetch_desc() 50 self.change_exit_status() 51 self.check_formatargs() 52 self.print_short() 53 if clparser['info']: 54 print_desc = 0 55 if self.output_data.has_key('seen'): 56 if not self.output_data['seen']: 57 print_desc = 1 58 else: 59 print_desc = 1 60 if print_desc: 61 self.output_data['seen'] = 1 62 self.print_long()
63
64 - def print_long(self):
65 print self.description[1]
66
67 - def print_justification(self):
68 if self.output_data.has_key('Justification'): 69 print " Justification: %s" % self.output_data['Justification']
70
71 - def return_trans(self, tag):
72 trans = translation('linda') 73 translations = [] 74 for type in ('s', 'l'): 75 translations.append(unicode(trans.gettext('%s_%s' % (tag, type)), \ 76 trans.charset(), 'replace').encode(trans.charset())) 77 return translations
78
79 -class OutputFormats:
80 - def __init__(self):
81 self.registry = {}
82
83 - def register(self, klass, tag):
84 self.registry[tag] = klass
85
86 - def register_all(self, dir):
87 for file in map(lambda x: '%s/%s' % (dir, x), os.listdir(dir)): 88 if file.endswith('.py'): 89 try: 90 execfile(file, {'_': __builtins__['_']}) 91 except StandardError: 92 print _("Failed to import %s.") % file 93 traceback.print_exc(file=sys.stdout)
94
95 - def keys(self):
96 return self.registry.keys()
97
98 - def __getitem__(self, key):
99 return self.registry[key]
100
101 -class OutputException(Exception):
102 pass
103