Logo Search packages:      
Sourcecode: zope-cmfbibliographyat version File versions  Download package

base.py

############################################################################
#                                                                          #
#             copyright (c) 2003 ITB, Humboldt-University Berlin           #
#             written by: Raphael Ritz, r.ritz@biologie.hu-berlin.de       #
#                                                                          #
############################################################################

"""BibliographyParser main class"""

# Python stuff
import re

# Zope stuff
from Interface import Interface
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo
from OFS.Folder import Folder
from OFS.SimpleItem import SimpleItem
from Acquisition import Implicit
import Products


00023 class IBibliographyParser(Interface):
    """
    Interface for the input parsers of the bibliography tool.
    """
00027     def getFormatName():
        """
        returns the name of the format
        """

00032     def getFormatExtension():
        """
        returns the filename extension of the format
        """
        
00037     def getDelimiter():
        """
        returns the delimiter used to split a list of entries into pieces
        """

00042     def getPattern():
        """
        returns the pattern used to parse a single entry
        """

00047     def setDelimiter(delimiter="\n\n", flags=None):
        """
        sets the delimiter used to split a list of entries into pieces
        the 'delimiter' argument and the flags are passed to 're.compile'
        """

00053     def setPattern(pattern="\n", flags=None):
        """
        sets the pattern used to parse a single entry
        the 'pattern' argument is passed to 're.compile')
        """

00059     def checkFormat(source):
        """
        checks whether source has the right format
        returns true (1) if so and false (0) otherwise
        """
        
00065     def getEntries(source):
        """
        splits a (text) file with several entries
        parses the entries
        returns a list of the parsed entries
        """

00072     def splitSource(source):
        """
        splits a (text) file with several entries
        returns a list of those entries
        """

00078     def parseEntry(entry):
        """
        parses a single entry
        
        returns a dictionary to be passed to
        BibliographyEntry's edit method
        """


00087 class BibliographyParser(SimpleItem):
    """
    Base class for the input parser of the bibliography tool.
    """
    __implements__ = (IBibliographyParser ,)

    meta_type = 'Bibliography Parser'

    format = {'name':'name of the format',
              'extension':'typical filename extension'
              }
    
    security = ClassSecurityInfo()

    delimiter = "\n\n"       # used to split a text into a list of entries
    pattern = r'(^.{0,4}- )' # the Medline pattern as default

00104     def __init__(self, id, title=''):
        """
        minimal initialization
        """
        self.id = id
        self.title = title
        self.format = format

00112     def getFormatName(self):
        """ returns the name of the format """
        return self.format.get('name', 'No name specified')

00116     def getFormatExtension(self):
        """ returns the filename extension of the format """
        return self.format.get('extension', 'no extension specified')

00120     def getDelimiter(self):
        """
        returns the delimiter used to split a list of entries into pieces
        """
        return self.delimiter

00126     def getPattern(self):
        """
        returns the pattern used to parse a single entry
        """
        return self.pattern

00132     def setDelimiter(self, delimiter="\n\n", flags=None):
        """
        sets the delimiter used to split a list of entries into pieces
        the 'delimiter' argument and the flags are passed to 're.compile'
        """
        if flags:
            self.delimiter = re.compile(delimiter, flags)
        else:
            self.delimiter = re.compile(delimiter)
        return None

00143     def setPattern(self, pattern="\n", flags=None):
        """
        sets the pattern used to parse a single entry
        the 'pattern' argument is passed to 're.compile')
        """
        if flags:
            self.pattern = re.compile(pattern, flags)
        else:
            self.pattern = re.compile(pattern)
        return None

00154     def checkFormat(source):
        """
        checks whether source has the right format
        returns true (1) if so and false (0) otherwise
        """
        pass # needs to be provided by the individual parsers
    
00161     def splitSourceFile(self, source):
        """
        splits a (text) file with several entries
        returns a list of those entries
        """
        if hasattr(self, 'preprocess'):
            source = self.preprocess(source)
        return self.delimiter.split(source)

00170     def parseEntry(self, entry):
        """
        parses a single entry
        
        returns a dictionary to be passed to
        BibliographyEntry's edit method
        """
        pass  # needs to be overwriten by the individual parser
        
00179     def getEntries(self, source):
        """
        splits a (text) file with several entries
        parses the entries
        returns a list of the parsed entries
        """
        return [self.parseEntry(entry) \
                for entry in self.splitSourceFile(source)]
        

 # Class instanciation
InitializeClass(BibliographyParser)

00192 class ParserFolder(Folder):
    """
    A folder that only offers to add objects that implement the
    IBibliographyParser interface.
    """
    meta_type = 'Parser Folder'

    id = 'Parsers'
    title = "BibliographyTool's parser folder"

    # we don't want 'View'
    manage_options = ( Folder.manage_options[0], ) \
                     + Folder.manage_options[2:]
    index_html = None

00207     def __init__(self, id, title=''):
        """
        minimal initialization
        """
        self.id = id
        # self.title = title
    
    def all_meta_types(self):
        product_infos = Products.meta_types
        possibles = []
        for p in product_infos:
            try:
                if IBibliographyParser in p.get('interfaces', []):
                    possibles.append(p)
            except TypeError:
                pass
        definites = map(lambda x: x.meta_type, self.objectValues())
        return filter(lambda x,y=definites: x['name'] not in y, possibles)

InitializeClass(ParserFolder)


00229 class EntryParseError(Implicit):
    """Parsers can return instances of this class when the parsing of an entry fails
    for whatever reason.  The description is then taken and added to the import_report.
    """

    security = ClassSecurityInfo()
    security.declareObjectPublic()

    def __init__(self, description):
        self.description = description.replace('\n', '\\n')

    security.declarePublic('get')
    def get(self, attr_name):
        # XXX Hack! import.cpy does::
        #    if entry.get('title'):
        #        ...
        # So we need to mimic that here
        if attr_name == 'description':
            return self.description
        else:
            return 'EntryParseError'

InitializeClass(EntryParseError)

Generated by  Doxygen 1.6.0   Back to index