예제 #1
0
파일: __init__.py 프로젝트: dineiar/pybtex
    def parse_string(self, name):
        """Extract various parts of the name from a string.
        Supported formats are:
         - von Last, First
         - von Last, Jr, First
         - First von Last
        (see BibTeX manual for explanation)
        """
        def process_first_middle(parts):
            try:
                self._first.append(parts[0])
                self._middle.extend(parts[1:])
            except IndexError:
                pass

        def process_von_last(parts):
            von, last = rsplit_at(parts, lambda part: part.islower())
            if von and not last:
                last.append(von.pop())
            self._prelast.extend(von)
            self._last.extend(last)

        def find_pos(lst, pred):
            for i, item in enumerate(lst):
                if pred(item):
                    return i
            return i + 1

        def split_at(lst, pred):
            """Split the given list into two parts.

            The second part starts with the first item for which the given
            predicate is True.
            """
            pos = find_pos(lst, pred)
            return lst[:pos], lst[pos:]

        def rsplit_at(lst, pred):
            rpos = find_pos(reversed(lst), pred)
            pos = len(lst) - rpos
            return lst[:pos], lst[pos:]

        parts = split_tex_string(name, ',')
        if len(parts) == 3: # von Last, Jr, First
            process_von_last(split_tex_string(parts[0]))
            self._lineage.extend(split_tex_string(parts[1]))
            process_first_middle(split_tex_string(parts[2]))
        elif len(parts) == 2: # von Last, First
            process_von_last(split_tex_string(parts[0]))
            process_first_middle(split_tex_string(parts[1]))
        elif len(parts) == 1: # First von Last
            parts = split_tex_string(name)
            first_middle, von_last = split_at(parts, lambda part: part.islower())
            if not von_last and first_middle:
                last = first_middle.pop()
                von_last.append(last)
            process_first_middle(first_middle)
            process_von_last(von_last)
        else:
            raise PybtexError('Invalid name format: %s' % name)
예제 #2
0
파일: io.py 프로젝트: cshen/cshen.github.io
def _open(opener, filename, mode, **kwargs):
    write_mode = 'w' in mode
    try:
        if write_mode:
            return _open_or_create(opener, filename, mode, environ, **kwargs)
        else:
            return _open_existing(opener, filename, mode, locate=kpsewhich, **kwargs)
    except EnvironmentError as error:
        raise PybtexError("unable to open %s. %s" % (filename, error.strerror))
예제 #3
0
 def parse_file(self, filename, file_suffix=None):
     if file_suffix is not None:
         filename = filename + file_suffix
     self.filename = filename
     open_file = pybtex.io.open_unicode if self.unicode_io else pybtex.io.open_raw
     with open_file(filename, encoding=self.encoding) as f:
         try:
             self.parse_stream(f)
         except UnicodeDecodeError, e:
             raise PybtexError(unicode(e), filename=self.filename)
예제 #4
0
파일: io.py 프로젝트: skbly7/redmine
def _open(opener, filename_or_file, mode, **kwargs):
    if hasattr(filename_or_file, 'read') and hasattr(filename_or_file, 'close'):
        return filename_or_file
    else:
        filename = filename_or_file
    write_mode = 'w' in mode
    try:
        if write_mode:
            return _open_or_create(opener, filename, mode, environ, **kwargs)
        else:
            return _open_existing(opener, filename, mode, locate=kpsewhich, **kwargs)
    except EnvironmentError as error:
        raise PybtexError("unable to open %s. %s" % (filename, error.strerror))
예제 #5
0
def run_bibtex(style, database, citations=None):
    if citations is None:
        citations = database.entries.keys()
    tmpdir = mkdtemp(prefix='pybtex_test_')
    try:
        write_bib(path.join(tmpdir, 'test.bib'), database)
        write_aux(path.join(tmpdir, 'test.aux'), citations)
        write_bst(path.join(tmpdir, 'test.bst'), style)
        bibtex = Popen(('bibtex', 'test'), cwd=tmpdir, stdout=PIPE, stderr=PIPE)
        stdout, stderr = bibtex.communicate()
        if bibtex.returncode:
            report_error(PybtexError(stdout))
        with open(path.join(tmpdir, 'test.bbl')) as bbl_file:
            result = bbl_file.read()
        return result
    finally:
        pass
        rmtree(tmpdir)