Пример #1
0
    def __init__ (self, arg = (None, None, None)):

        if type (arg) is types.StringType:
            try:
                year  = int (arg)
            except ValueError:
                g = year_match.search (arg)
                if g:
                    year = int (g.group (1))
                else:
                    year = None
                
            month = None
            day   = None
        else:
            year, month, day = arg
        
        if year and year < 0:
            raise Exceptions.DateError (_("Illegal year value"))
        self.year = year
        
        if month and (month < 1 or month > 12):
            raise Exceptions.DateError (_("Illegal month value"))
        self.month = month
        
        if day and (day < 1 or day > 31):
            raise Exceptions.DateError (_("Illegal day value"))
        self.day = day

        self.text = None
        return
Пример #2
0
 def set_native(self, key, value):
     if self.__text.has_key(key):
         del self.__text[key]
     try:
         self.dict[key] = _bibtex.set_native(value, 0)
     except IOError, err:
         raise Exceptions.ParserError([str(err)])
Пример #3
0
def bibopen (entity, how = None):
    ''' Generic function to open a bibliographic database '''

    def simple_try (url, how):
	# url is Fields.URL instance, only to be passed to opener
	base = None

	if how == None:
	    listedmethods = Autoload.available ("format")

	    for method in listedmethods:
		opener = get_by_name (method, 'open')
		if opener:
		    base = opener (url, 1)
		    if base is not None:
			return base
	    return None

	opener = get_by_name (how, 'open')

	if opener:
	    base = opener (url, 0)
	else:
	    raise Exceptions.FormatError (_(u"method “%s” provides no opener") % how)

	return base

    # Consider the reference as an URL: url is an Fields.URL instance
    url = Fields.URL (entity)

    if url.url [0] == 'file' and not os.path.exists (url.url [2]):
	raise Exceptions.FileError (_(u"File “%s” does not exist") % url.get_url ())

    # eventually load a new module
    if how is None:
	handler = Autoload.get_by_regexp ("format", url.get_url ())
	if handler:
	    how = handler.name

    base = simple_try (url, how)

    if base is None:
	raise Exceptions.FormatError (_(u"don’t know how to open “%s”") % entity)

    return base
Пример #4
0
    def simple_try (url, how):
	# url is Fields.URL instance, only to be passed to opener
	base = None

	if how == None:
	    listedmethods = Autoload.available ("format")

	    for method in listedmethods:
		opener = get_by_name (method, 'open')
		if opener:
		    base = opener (url, 1)
		    if base is not None:
			return base
	    return None

	opener = get_by_name (how, 'open')

	if opener:
	    base = opener (url, 0)
	else:
	    raise Exceptions.FormatError (_(u"method “%s” provides no opener") % how)

	return base
Пример #5
0
 def next(self):
     while True:
         try:
             retval = _bibtex.next_unfiltered(self.parser)
         except IOError, error:
             raise Exceptions.ParserError((str(error), ))
         if retval == None:
             self.db.set_metadata('bibtex-preamble', self.preamble)
             return None
         elif retval[0] == 'entry':
             retval = retval[1]
             name, fieldtype, offset, line, object = retval
             if name:
                 key = Key.Key(self.db, name)
             else:
                 key = None
             fieldtype = Types.get_entry(fieldtype)
             return Entry(key, fieldtype, object, self.parser, line)
         elif retval[0] == 'preamble':
             self.preamble.append(retval[1])
             continue
         else:
             continue
Пример #6
0
            except Exceptions.ParserError, err:
                errors.append(str(err))
                continue
            if entry is None:
                break
            if entry.key is None:
                self.add(entry)
            else:
                if self.dict.has_key(entry.key):
                    errors.append(
                        _(u"%s:%d: key “%s” already defined") %
                        (repr(self.key), entry.line, repr(entry.key.key)))
                else:
                    self.dict[entry.key] = entry
        if len(errors) > 0:
            raise Exceptions.ParserError(errors)
        # Incorporer les definitions de l'utilisateur
        if Config.get('bibtex+/override').data:
            user = Config.get('bibtex/macros').data
            valid = re.compile('^\w+$')
            for k in user.keys():
                if not valid.match(k):
                    raise TypeError, _(u"key “%s” is malformed") % k
                _bibtex.set_string(
                    self.parser, k,
                    _bibtex.reverse(_base_fieldtype[Text],
                                    Config.get('bibtex+/braces').data,
                                    user[k][0]))
        return

    def __str__(self):