Package python-module-logilab-mtconverter-0 :: Package 8 :: Package 4 :: Module transform
[frames] | no frames]

Source Code for Module python-module-logilab-mtconverter-0.8.4.transform

  1  # copyright 2006-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  3  # 
  4  # This file is part of logilab-mtconverter. 
  5  # 
  6  # logilab-mtconverter is free software: you can redistribute it and/or modify it 
  7  # under the terms of the GNU Lesser General Public License as published by the 
  8  # Free Software Foundation, either version 2.1 of the License, or (at your 
  9  # option) any later version. 
 10  # 
 11  # logilab-mtconverter is distributed in the hope that it will be useful, but 
 12  # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 13  # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
 14  # for more details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License along 
 17  # with logilab-mtconverter. If not, see <http://www.gnu.org/licenses/>. 
 18  """base transformation objects""" 
 19   
 20  __docformat__ = "restructuredtext en" 
 21   
 22   
23 -class Transform(object):
24 """a transform is converting some content in a acceptable MIME type 25 into another MIME type 26 """ 27 name = None 28 inputs = () 29 output = None 30 input_encoding = None 31 output_encoding = None 32
33 - def __init__(self, **kwargs):
34 self.__dict__.update(kwargs) 35 if not getattr(self, 'name', None): 36 self.name = self.__class__.__name__
37
38 - def convert(self, trdata):
39 """convert the given data structure into transform output's mime type 40 41 :param trdata: `TransformData` 42 :rtype: `TransformData` 43 """ 44 # this is not true when transform accept wildcard 45 #assert trdata.mimetype in self.inputs 46 trdata.data = self._convert(trdata) 47 trdata.mimetype = self.output 48 if self.output_encoding: 49 trdata.encoding = self.output_encoding 50 return trdata
51
52 - def _convert(self, trdata):
53 raise NotImplementedError
54 55
56 -class TransformsChain(list):
57 """A chain of transforms used to transform data""" 58 59 inputs = ('application/octet-stream',) 60 output = 'application/octet-stream' 61 name = None 62
63 - def __init__(self, name=None, *args):
64 list.__init__(self, *args) 65 if name is not None: 66 self.name = name 67 if args: 68 self._update()
69
70 - def convert(self, trdata):
71 for transform in self: 72 trdata = transform.convert(trdata) 73 return trdata
74
75 - def __setitem__(self, key, value):
76 list.__setitem__(self, key, value) 77 self._update()
78
79 - def append(self, value):
80 list.append(self, value) 81 self._update()
82
83 - def insert(self, *args):
84 list.insert(*args) 85 self._update()
86
87 - def remove(self, *args):
88 list.remove(*args) 89 self._update()
90
91 - def pop(self, *args):
92 list.pop(*args) 93 self._update()
94
95 - def _update(self):
96 self.inputs = self[0].inputs 97 self.output = self[-1].output 98 for i in range(len(self)): 99 if hasattr(self[-i-1], 'output_encoding'): 100 self.output_encoding = self[-i-1].output_encoding 101 break 102 else: 103 try: 104 del self.output_encoding 105 except: 106 pass
107