'''
Basic interscanners for sagator, version 0.6.0

(c) 2003-2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
    
'''

from avlib import *
import os

__all__=['file2stream','stream2file','f2s','s2f','stream2mbox']

#####################################################################
### convertors

class file2stream(interscanner):
  '''
  file2stream interscanner
  
  Usage: file2stream(scanner())
  
  Where: scanner() is an interscanner or realscanner.
           It must be a streamscanner.
  
  This scanner converts a filescanner input into streamscanner.
  '''
  name='file2stream()'
  def __init__(self,scanner):
      self.rename([scanner])
      self.scanner=scanner
  def scanfile(self,files,dir=''):
      for fn in files:
        f=safe.open(fn)
        self.scanner.prescan()
        level,vir,ret=self.scanner.scanstream(f.read())
        self.scanner.postscan(level,vir,ret)
        f.close()
        if vir:
          return level,vir,ret
      return level,'',[]

class f2s(file2stream):
  '''An alias for file2stream interscanner'''
  name='f2s()'

class stream2file(interscanner):
  '''
  stream2file interscanner
  
  Usage: stream2file(scanner())
  
  Where: scanner() is a interscanner or realscanner.
           It must be a filescanner.
  
  You can use it to save streams into files.
  '''
  name='stream2file()'
  is_multistream=1
  def __init__(self,scanner,addheader=0):
      self.rename([scanner])
      self.scanner=scanner
      self.addheader=addheader
      self.filename=''
      self.mboxdir=''
      self.filenames=[]
      self.counter=1
  def destroy(self):
      if self.mboxdir:
        if debug.debug_level<10:
          debug.echo(5,"s2f: destroy():",self.mboxdir)
          rdel([self.mboxdir])
        else:
          debug.echo(4,"s2f: unfinished destroy:",self.mboxdir)
      self.scanner.destroy()
      self.filenames=[]
      self.mboxdir=''
  def creatembox(self,f,data,addheader=0):
      try:
        if addheader:
          f.write(fromhdr())
        f.write(data)
        f.close()
      except os.error,(ec,es):
        debug.echo(0,'creatembox: ERROR: Open: ',es)
        raise
      except IOError,(ec,es):
        debug.echo(0,'creatembox: ERROR: IO: ',es)
        raise
  def addstream(self,stream):
      if stream:
        if self.mboxdir=='':
          self.mbox=mktemp('/tmp/s2f-','.mbd','d',0700)
          self.mboxdir=self.mbox.name
        if self.filename=='':
          self.filename='part-%06d' % (self.counter)
          self.counter=self.counter+1
        mboxfile=mktemp(self.mboxdir+'/','-'+self.filename,'w',0600)
        self.creatembox(mboxfile.f,stream,self.addheader)
        self.filenames.append(mboxfile.name)
  def scanstream(self,stream):
      level,vir,ret=0.0,'',[]
      self.addstream(stream)
      try:
        self.scanner.prescan()
        level,vir,ret=self.scanner.scanfile(self.filenames,self.mboxdir)
        self.scanner.postscan(level,vir,ret)
      except:
        raise
      return level,vir,ret
  def scanmultistream(self,streams):
      level,vir,ret=0.0,'',[]
      if type(streams)==type({}):
        for filename,stream in streams.items():
          self.filename=filename
          self.addstream(stream)
      else:
        for stream in streams:
          self.addstream(stream)
      try:
        self.scanner.prescan()
        level,vir,ret=self.scanner.scanfile(self.filenames,self.mboxdir)
        self.scanner.postscan(level,vir,ret)
      except:
        raise
      return level,vir,ret

class s2f(stream2file):
  '''An alias for stream2file interscanner.'''
  name='s2f()'

class stream2mbox(stream2file):
  '''An alias for stream2file(...,1) interscanner.'''
  name='stream2mbox()'
  def __init__(self,scanner):
      stream2file.__init__(self,scanner,1)
