示例#1
0
def bib2zope(filename, app = None):
    import _bibtex
    file = _bibtex.open_file(filename, True)
    entry = _bibtex.next(file)
    while entry != None:
        keys = entry[4].keys()
        id = entry[0]
        pubtype = entry[1]
        print '-----'
        print 'id:', id
        print 'pubype:', pubtype
        print 'keys:', keys
        for key in keys:
            print key, ':', _bibtex.get_native(entry[4][key]).strip('{} ')

        if app != None:
            publications = app.infnine.publications
            print "Running in Zope, creating objects in ZODB..."
            if not publications.hasObject(id):
                print "Entry", id, "not yet in ZODB, creating"
                admin = app.acl_users.getUser('admin')
                import AccessControl
                AccessControl.SecurityManagement.newSecurityManager(None, admin)
                from Testing.makerequest import makerequest
                app = makerequest(app)
                publications.invokeFactory('Publication', id)
            pub = publications.__getitem__(id)

            print "Updating", id, keys
            from infnine.data.interfaces import IPublication
            for key in keys:
                if not key.replace('-', '_') in (IPublication._v_attrs.keys()):
                    print "!!! Missing key:", key

                value = _bibtex.get_native(entry[4][key]).strip('{} "')

                if key in ['year']:
                    value = int(value)
                elif key == 'bib2html_pubtype':
                   value = unicode(filtered_pubtype(value))
                else:
                    value = unicode(value)

                if '-' in key:
                    pub.__setattr__(key.replace('-', '_'), value)
                else:
                    pub.__setattr__(key, value)

            pub.reindexObject()
            import transaction
            transaction.commit()

        entry = _bibtex.next(file)
示例#2
0
 def __init__(self, context, t):
     """Constructs a BibtexRecord using the _bibtex mystery tuple."""
     ## A hack to deal with _bibtex' incomplete handling of the "month" field
     self.date = [0, 0]  # Month, Year
     ##
     self.handle = t[0]
     self.entry_type = t[1]
     t[2]  #Mystery!
     t[3]  #Mystery!
     self.typemap = {
     }  #If we know that some fields should be a particular type.
     self.data = {}
     items = t[4]
     for k in items.keys():
         ty = self.typemap.get(k, -1)
         x = _bibtex.expand(context, items[k], ty)
         if k == "month":
             month = BIBTEX_MONTHS[self._strip_braces(
                 _bibtex.get_native(items[k]))]
             self.date[0] = month
             self.data["month"] = month
             continue
         if ty == -1: ty = x[0]
         self.data[k] = self.build(ty, x)
     self.date[1] = self.data["year"]
示例#3
0
 def __init__(self, context, t):
     """Constructs a BibtexRecord using the _bibtex mystery tuple."""
     ## A hack to deal with _bibtex' incomplete handling of the "month" field
     self.date = [0,0] # Month, Year
     ##
     self.handle = t[0]
     self.entry_type = t[1]
     t[2] #Mystery!
     t[3] #Mystery!
     self.typemap = {} #If we know that some fields should be a particular type.
     self.data = {}
     items = t[4]
     for k in items.keys():
         ty = self.typemap.get(k, -1)
         x = _bibtex.expand(context, items[k], ty)
         if k == "month":
             month = BIBTEX_MONTHS[_bibtex.get_native(items[k])]
             self.date[0] = month
             self.data["month"] = month
             continue
         if ty == -1: ty = x[0]
         self.data[k] = self.build(ty, x)
     if 'year' in self.data.keys():
         self.date[1] = self.data['year']
     else:
         self.date[1] = 1900
示例#4
0
    def __init__(self, bibtex_entry, path=""):
        """ @param[in] bibtex_entry 
            @param[in] path          (optional path to the bib file containing the entry)
        """
        self.key, self.type, tmp, tmp, entries  = bibtex_entry
        self.orig_entry = dict( [ (key, cleanup(_bibtex.get_native(value))) for key, value in entries.iteritems() ] )
        self.entry = dict( [ (key, cleanup(value)) for key, value in self.orig_entry.iteritems() ] )


        self.path  = path
        if 'author' in self.entry:
            self.entry['author'] = NameFormatter(self.entry['author']).getBibTexAuthors()
示例#5
0
    def __init__(self, key, fieldtype, content, parser, line):
	Base.Entry.__init__(self, key, fieldtype, content)

	self.__text = {}
	self.parser = parser
        self.line   = line
        
	# Check for date fields
	datefields = Config.get ('bibtex/datefield').data
	convert    = Config.get ('bibtex/months').data
	for field in datefields.keys():
	    (yearfield, monthfield) = datefields[field]
	    
	    # check if this entry provides a date field
	    if not self.has_key (yearfield):
                continue

            day   = None
	    month = None
            try:
                year = int(self[yearfield].text)
            except ValueError:
                break
            
	    del self[yearfield]

	    if self.has_key(monthfield):
		mt = _bibtex.get_native(self.dict[monthfield])
		if convert.has_key(mt):
		    month = convert[mt]
		    del self[monthfield]
                else:
                    df = extended_date.match(mt)
                    if df:
                        (gd, gm) = (df.group(1), df.group(2))
                        if convert.has_key(gm):
                            month = convert[gm]
                            try:
                                day = int(gd)
                            except ValueError:
                                pass
                            del self[monthfield]
	    self[field] = Date((year, month, day))
	return
示例#6
0
    def __init__(self, key, fieldtype, content, parser, line):
        Base.Entry.__init__(self, key, fieldtype, content)

        self.__text = {}
        self.parser = parser
        self.line = line

        # Check for date fields
        datefields = Config.get('bibtex/datefield').data
        convert = Config.get('bibtex/months').data
        for field in datefields.keys():
            (yearfield, monthfield) = datefields[field]

            # check if this entry provides a date field
            if not self.has_key(yearfield):
                continue

            day = None
            month = None
            try:
                year = int(self[yearfield].text)
            except ValueError:
                break

            del self[yearfield]

            if self.has_key(monthfield):
                mt = _bibtex.get_native(self.dict[monthfield])
                if convert.has_key(mt):
                    month = convert[mt]
                    del self[monthfield]
                else:
                    df = extended_date.match(mt)
                    if df:
                        (gd, gm) = (df.group(1), df.group(2))
                        if convert.has_key(gm):
                            month = convert[gm]
                            try:
                                day = int(gd)
                            except ValueError:
                                pass
                            del self[monthfield]
            self[field] = Date((year, month, day))
        return
示例#7
0
def writer(it, output, preamble=[]):
    ''' Outputs all the items refered by the iterator <it> to the
    <stdout> stream '''

    # write header
    if Config.get ('base/advertise').data:
        output.write ('@comment{This file has been generated by Pybliographer}\n\n')

    if preamble:
	output.write ('@preamble {')
	output.write (preamble)
	output.write ('}\n\n')
    # locate the entries that belong to a BibTeX database
    # and those who contain crossreferences
    field_table = Config.get ('base/fields').data
    crossref    = None
    
    for key in field_table.keys ():
        if field_table [key].type == Reference:
            crossref = key
            break
    
    header = {}
    refere = {}
    entry  = it.first ()
    while entry:
        if isinstance (entry, Entry):
	    header [entry.key.base] = entry
            
        if entry.has_key (crossref):
            refere [entry.key] = entry
            
	entry = it.next ()

    # write the preamble, if any
    if preamble:
	for i in preamble:
	    output.write ('@Preamble {%s}\n' % i)
	output.write ('\n')
    # write the string definitions corresponding to these entries
    if len (header) > 0:
	user = Config.get ('bibtex/macros').data

	for entry in header.values ():
	    # write the list of strings
	    dict = _bibtex.get_dict (entry.parser)
	    if len (dict.keys ()) == 0: continue

	    for k in dict.keys ():
		if not (user.has_key (k) and user [k][1] == 0):
		    output.write ('@String{ ')
		    value = _bibtex.get_native (dict [k])
		    output.write ('%s \t= %s' % (k, value))
		    output.write ('}\n')

	    output.write ('\n')

    # write the crossreferenced ones
    for entry in refere.values ():
        entry_write (entry, output)
    
    # write the entries with no cross references
    entry = it.first ()
    while entry:
        if not refere.has_key (entry.key):
            entry_write (entry, output)
	entry = it.next ()

    return
示例#8
0
def entry_write(entry, output):
    '''Print a single entry as BiBTeX code.'''
    native = isinstance(entry, Entry)
    tp = entry.type
    # write the type and key
    output.write('@%s{%s,\n' % (tp.name, entry.key.key))
    # create a hash containing all the keys, to keep track
    # of those who have been already written
    dico = {}
    datefields = Config.get('bibtex/datefield').data
    convert    = Config.get('bibtex/months').data
    # we have to handle the special case of the dates
    # create the list of months
    monthlist  = range(0, 12)
    for key in convert.keys():
        monthlist[convert[key]-1] = key

    dateformat = Config.get('bibtex+/dateformat').data
    if native:
	# loop over all the fields
	for field in entry.keys():
	    if datefields.has_key(field):
		# we are processing a date...
		date = entry[field]
		dico[datefields[field][0]] = str(date.year)
		if date.month:
                    month = monthlist[date.month - 1]
                    if date.day:
                        month = dateformat % {'day':    date.day,
                                              'month' : month}
		    dico[datefields[field][1]] = month
	    else:
		# we are processing a normal entry
		dico[field] = _bibtex.get_native(entry.dict[field])
    else:
	for field in entry.keys():
	    # convert the field in a bibtex form
	    if datefields.has_key(field):
		# we are processing a date...
		date = entry[field]
		dico[datefields[field][0]] = str(date.year)
		if date.month:
                    month = monthlist[date.month - 1]
                    if date.day:
                        month = dateformat % {'day':    date.day,
                                              'month' : month}
                        
		    dico[datefields[field][1]] = month
	    else:
		# we are processing a normal entry
                value = entry[field]
                # eventually convert the crossref
                if isinstance(value, Reference):
                    value = string.join(map(lambda item: item.key, value.list), ', ')
                fieldtype = _fieldtype(Types.get_field(field))
                dico[field] = _nativify(value, fieldtype)
    first = True
    # write according to the type order
    for f in tp.mandatory + tp.optional:
	# dico contains all the available fields
	field = string.lower(f.name)
	if not dico.has_key(field):
            continue
        if not first: output.write (',\n')
        else:         first = False
	output.write('  %-14s = ' % f.name)
        output.write(Utils.format (dico[field],
                                   75, 19, 19)[19:])
	del dico[field]
    keys = dico.keys()
    keys.sort()
    for f in keys:
        if not first: output.write (',\n')
        else:         first = False
	output.write('  %-14s = ' % f)
	output.write(Utils.format(dico[f],
                                  75, 19, 19)[19:])
    output.write ('\n}\n\n')
    return
示例#9
0
def _nativify(field, fieldtype):
    '''Private method to convert from field to native format.'''
    obj = _bibtex.reverse(fieldtype, Config.get('bibtex+/braces').data, field)
    return _bibtex.get_native(obj)
示例#10
0
def writer(it, output, preamble=[]):
    ''' Outputs all the items refered by the iterator <it> to the
    <stdout> stream '''

    # write header
    if Config.get('base/advertise').data:
        output.write(
            '@comment{This file has been generated by Pybliographer}\n\n')

    if preamble:
        output.write('@preamble {')
        output.write(preamble)
        output.write('}\n\n')
    # locate the entries that belong to a BibTeX database
    # and those who contain crossreferences
    field_table = Config.get('base/fields').data
    crossref = None

    for key in field_table.keys():
        if field_table[key].type == Reference:
            crossref = key
            break

    header = {}
    refere = {}
    entry = it.first()
    while entry:
        if isinstance(entry, Entry):
            header[entry.key.base] = entry

        if entry.has_key(crossref):
            refere[entry.key] = entry

        entry = it.next()

    # write the preamble, if any
    if preamble:
        for i in preamble:
            output.write('@Preamble {%s}\n' % i)
        output.write('\n')
    # write the string definitions corresponding to these entries
    if len(header) > 0:
        user = Config.get('bibtex/macros').data

        for entry in header.values():
            # write the list of strings
            dict = _bibtex.get_dict(entry.parser)
            if len(dict.keys()) == 0: continue

            for k in dict.keys():
                if not (user.has_key(k) and user[k][1] == 0):
                    output.write('@String{ ')
                    value = _bibtex.get_native(dict[k])
                    output.write('%s \t= %s' % (k, value))
                    output.write('}\n')

            output.write('\n')

    # write the crossreferenced ones
    for entry in refere.values():
        entry_write(entry, output)

    # write the entries with no cross references
    entry = it.first()
    while entry:
        if not refere.has_key(entry.key):
            entry_write(entry, output)
        entry = it.next()

    return
示例#11
0
def entry_write(entry, output):
    '''Print a single entry as BiBTeX code.'''
    native = isinstance(entry, Entry)
    tp = entry.type
    # write the type and key
    output.write('@%s{%s,\n' % (tp.name, entry.key.key))
    # create a hash containing all the keys, to keep track
    # of those who have been already written
    dico = {}
    datefields = Config.get('bibtex/datefield').data
    convert = Config.get('bibtex/months').data
    # we have to handle the special case of the dates
    # create the list of months
    monthlist = range(0, 12)
    for key in convert.keys():
        monthlist[convert[key] - 1] = key

    dateformat = Config.get('bibtex+/dateformat').data
    if native:
        # loop over all the fields
        for field in entry.keys():
            if datefields.has_key(field):
                # we are processing a date...
                date = entry[field]
                dico[datefields[field][0]] = str(date.year)
                if date.month:
                    month = monthlist[date.month - 1]
                    if date.day:
                        month = dateformat % {'day': date.day, 'month': month}
                    dico[datefields[field][1]] = month
            else:
                # we are processing a normal entry
                dico[field] = _bibtex.get_native(entry.dict[field])
    else:
        for field in entry.keys():
            # convert the field in a bibtex form
            if datefields.has_key(field):
                # we are processing a date...
                date = entry[field]
                dico[datefields[field][0]] = str(date.year)
                if date.month:
                    month = monthlist[date.month - 1]
                    if date.day:
                        month = dateformat % {'day': date.day, 'month': month}

                    dico[datefields[field][1]] = month
            else:
                # we are processing a normal entry
                value = entry[field]
                # eventually convert the crossref
                if isinstance(value, Reference):
                    value = string.join(map(lambda item: item.key, value.list),
                                        ', ')
                fieldtype = _fieldtype(Types.get_field(field))
                dico[field] = _nativify(value, fieldtype)
    first = True
    # write according to the type order
    for f in tp.mandatory + tp.optional:
        # dico contains all the available fields
        field = string.lower(f.name)
        if not dico.has_key(field):
            continue
        if not first: output.write(',\n')
        else: first = False
        output.write('  %-14s = ' % f.name)
        output.write(Utils.format(dico[field], 75, 19, 19)[19:])
        del dico[field]
    keys = dico.keys()
    keys.sort()
    for f in keys:
        if not first: output.write(',\n')
        else: first = False
        output.write('  %-14s = ' % f)
        output.write(Utils.format(dico[f], 75, 19, 19)[19:])
    output.write('\n}\n\n')
    return
示例#12
0
def _nativify(field, fieldtype):
    '''Private method to convert from field to native format.'''
    obj = _bibtex.reverse(fieldtype, Config.get('bibtex+/braces').data, field)
    return _bibtex.get_native(obj)