File: BaseConverter.py

package info (click to toggle)
zope-textindexng2 1%3A2.2.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,888 kB
  • ctags: 1,598
  • sloc: ansic: 6,836; python: 6,596; xml: 185; makefile: 137; sh: 41
file content (60 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
###########################################################################
#
# TextIndexNG                The next generation TextIndex for Zope
#
# This software is governed by a license. See
# LICENSE.txt for the terms of this license.
#
###########################################################################

import os, tempfile
from interfaces import IConverter

class BaseConverterError(Exception): pass

class TmpFile:

    def __init__(self, data):
        self.fname = tempfile.mktemp()
        open(self.fname,'w+b').write(data)

    def __str__(self): return self.fname
    __repr__ = __str__

    def __del__(self):
        os.unlink(self.fname)
	

class BaseConverter:
    """ Base class for all converters """

    content_type = None
    content_description = None

    __implements__ = IConverter.ConverterInterface

    def __init__(self):
        if not self.content_type:
            raise BaseConverterError,'content_type undefinied'

        if not self.content_description:
            raise BaseConverterError,'content_description undefinied'


    def execute(self,com):

        try:
            import win32pipe
            return win32pipe.popen(com).read()

        except ImportError:
            return os.popen(com).read()

    def saveFile(self, data):
        return TmpFile(data)

    def getDescription(self):   return self.content_description
    def getType(self):          return self.content_type
    def getDependency(self):    return getattr(self,'depends_on','')
    def __call__(self, s):      return self.convert(s)