Example #1
0
    def setUp(self):
        Registry.load_default_settings()

        s = Registry.getSchema("org.pybliographer/pubmed/0.1")
        fmt = Store.get("memory")

        self.db = fmt.dbcreate(None, s)
Example #2
0
    def testSimple(self):
        Registry.load_default_settings()

        source = pybut.src('ut_bst', 'abbrv.bst')
        o = BST.BST(open(source))

        db = Store.get('memory').dbcreate(
            None, Registry.getSchema('org.pybliographer/bibtex/0.1'))
        reader = BibTeX.Reader()
        rs = reader.parse(open(pybut.src('ut_bst', 'simple.bib')), db)
        
        state = BST.State(rs, db)
        o.Run(state)

        output = """\
\\begin{thebibliography}{1}

\\bibitem{1}
Frederic Gobry and First Last.
\\newblock This is a title.
\\newblock {\em My journal}, 12(123), 2007.

\\bibitem{2}
Frederic Gobry and First Last.
\\newblock {\em This is a title}.

\\end{thebibliography}
"""
        self.failUnlessEqual(state.output, output)
Example #3
0
    def setUp(self):
        Registry.load_default_settings()

        s = Registry.getSchema("org.pybliographer/pubmed/0.1")
        fmt = Store.get("memory")

        self.db = fmt.dbcreate(None, s)

        self.cnx = PubMed(self.db)
        self.port = reactor.listenTCP(8000, Server())
Example #4
0
    def testResolveA2B(self):
        sa = Schema.Schema(open(pybut.src('ut_adapter/a.sip')))

        fmt = Store.get('memory')
        self.dba = fmt.dbcreate(None, sa)

        from Pyblio import Registry
        Registry.reset()
        Registry.load_settings(pybut.src('ut_adapter'))

        dest = Adapter.adapt_schema(self.dba, 'b')
        self.failUnlessEqual(dest.schema.id, 'b')
Example #5
0
    def setUp(self):
        Registry.load_default_settings()

        s = Registry.getSchema('org.pybliographer/crossref/0.1')
        fmt = Store.get('memory')

        self.db = fmt.dbcreate(None, s)

        self.cnx = CrossRef.DOIQuery(self.db, 'user', 'pass')
        self.cnx.baseURL = 'http://localhost:8000/query'
        
        self.port = reactor.listenTCP(8000, Server())
        return
Example #6
0
    def setUp(self):
        Registry.load_default_settings()

        s = Registry.getSchema("org.pybliographer/wok/0.1")
        fmt = Store.get("memory")

        self.db = fmt.dbcreate(None, s)

        self.cnx = WOK.WOK(self.db)
        self.port = reactor.listenTCP(8000, Server())

        self.cnx.baseURL = "http://localhost:8000/esti"
        return
Example #7
0
    def _check (self, base):

        f = pybut.dbname ()

        s = Registry.getSchema('org.pybliographer/bibtex/0.1')
        
        db = Store.get ('file').dbcreate (f, s)

        self.parser = WithComments ('latin-1')

        rs = self.parser.parse(open(fp('%s.bib' % base)), db)
        db.rs.update(rs)
        db.save ()

        # mess a bit with the file to discard the schema
        tree = Compat.ElementTree.ElementTree(file=open(f))
        for s in tree.findall('./pyblio-schema'):
            s.clear()
        for s in tree.findall('./txo-group'):
            s.clear()

        tree.write(open(f, 'w'), encoding="utf-8")
        
        pybut.fileeq (f, fp('%s.xml' % base))

        Store.get ('file').dbdestroy (f, nobackup = True)
        return
Example #8
0
    def parse (self, file):
        schema = Registry.getSchema("org.pybliographer/wok/0.1")
        
        fd = open (file)

        self.fn = pybut.dbname ()
        self.db = Store.get('file').dbcreate (self.fn, schema)
        
        self.p = ISI.Reader()
        self.p.parse(fd, self.db)
        return
Example #9
0
def adapt_schema(db, target_schema):
    """ Returns a database using the specified 'target_schema', and
    that maps the content of 'db', thanks to one or more
    L{Adapter}s. If no suitable adapter can be found, will raise an
    AdaptError()"""

    # for the moment, we only resolve direct hits. More clever
    # resolutions will hopefully be implemented.

    if db.schema.id == target_schema:
        return db

    # search for target_schema in the adapters for the current schema:
    adapters = Registry.get(db.schema.id, 'adapters')
    
    for adapter in adapters:
        if adapter.target == target_schema:
            return adapter()(db)

    raise AdaptError(_("no adaptor for converting a %s into a %s") % (
        db.schema.id, target_schema))
Example #10
0
    def __init__(self, legacy, citator, wp):
        self.citator = citator
        schema = Registry.getSchema('org.pybliographer/bibtex/0.1')
        self.db = Store.get('memory').dbcreate(None, schema)
        self.legacy = legacy
        self.reader = BibTeX.Reader()
        # TODO: once we have per-field queries in pyblio-1.3, directly
        # search for the key instead of keeping this cache
        self.mapping = {}

        self.citator.prepare(self.db, wp, _extract_id)
        
        # pre-fill the pyblio db with the bibtex records indicated in
        # the document, in the extra field. We don't have a clean way
        # to obtain the same record id as the last time when importing
        # from bibtex, so we create a new db afresh, and shuffle
        # things in a second pass.
        tmp = Store.get('memory').dbcreate(None, schema)
        renaming = {}

        self.missing = []
        fetched = self.citator.wp.fetch()
        if fetched is None:
            return

        for pyblio_uid, name, bibtex_key in fetched:
            full_key = Key(legacy, str(bibtex_key))
            if not legacy.has_key(full_key):
                self.missing.append(bibtex_key)
                continue
            tmp_key = self._transfer_entry(tmp, full_key)
            renaming[tmp_key] = pyblio_uid
            self.mapping[full_key] = pyblio_uid

        for tmp_key, record in tmp.entries.iteritems():
            self.db.add(record, key=renaming[tmp_key])
Example #11
0
    def data_results(self, ctx, data):
        query = IRequest(ctx).args.get('q', None)
        if query is None:
            return None

        query = query[0].decode('utf-8')
        log.msg("QUERYING %r" % query, msgtype="QUERY")

        s = Registry.getSchema('org.pybliographer/pubmed/0.1')
        db = Store.get('memory').dbcreate(None, s)

        remote = PubMed.PubMed(db)
        d, rs = remote.search(query, maxhits=20)

        def success(total):
            bibtex = Adapter.adapt_schema(db, 'org.pybliographer/bibtex/0.1')
            return True, query, bibtex, total

        def failure(failure):
            return False, query, failure, 0

        d.addCallback(success).addErrback(failure)
        
        return d
Example #12
0
 def tearDown(self):
     self.port.stopListening()
     Registry.reset()
     return
Example #13
0
 def testCategories(self):
     c = Registry.get("with-path", "importers")
     assert len(c) == 2
Example #14
0
    def testAdapters(self):
        c = Registry.get("with-adapter", "adapters")
        self.failUnlessEqual(len(c), 1)

        c = c[0]
        self.failUnlessEqual(c.target, "another/format")
Example #15
0
 def tearDown(self):
     Registry.reset()
Example #16
0
 def setUp(self):
     Registry.load_default_settings()
Example #17
0
   python wok.py 'Author=(Gobry)' gobry.bip

"""

import sys

from twisted.internet import reactor

from Pyblio import Registry, Store
from Pyblio.External import WOK

query, output = sys.argv[1:]

# Create a database that is capable of storing Web of Science results.
Registry.load_default_settings()

s = Registry.getSchema('org.pybliographer/wok/0.1')
fmt = Store.get('file')

db = fmt.dbcreate(output, s)

# Initialize the connection to the database
wok = WOK.WOK(db)

# Perform a search. In return, we obtain the result set that will be
# filled in with the results, and a deferred that will fire once the
# query is over.
d, rs = wok.search(query)

def success(total):
Example #18
0
from Pyblio import Store, Registry

Registry.load_default_settings()

schema = Registry.getSchema("org.pybliographer/bibtex/0.1")
store = Store.get('file')

db = store.dbcreate('mydb.bip', schema)
Example #19
0
 def setUp(self):
     Registry.reset()
     Registry.load_settings(pybut.src("ut_registry"))
     return
Example #20
0
            ts = time.strftime('%Y%m%d %H:%M:%S', time.gmtime(info['time']))
        except KeyError:
            ts = '-'

        try:
            s = info['system'].split(',')[-1]
        except KeyError:
            s = '-'
        
        base = '%s [%-15s]' % (ts, s)
        for msg in info['message']:
            querylog.write('%s %s\n' % (base, msg))

log.addObserver(queryObserver)

Registry.load_default_settings()

# This will output the database in BibTeX format
w = Writer()


# Define a simple citation format
from Pyblio.Format import one, all, join, switch, I, B, A
from Pyblio.Format import Person, Date
from Pyblio.Format.HTML import generate

title = B[one('title') | u'(no title)']

title = A(href=one('url'))[title] | title

authors = join(', ', last=' and ')[Person.initialLast(all('author'))]
Example #21
0
    def __init__(self, base):
        OneToOneAdapter.__init__(self, base)

        self.schema = Registry.getSchema('org.pybliographer/bibtex/0.1')
        return
Example #22
0
 def testSchemas(self):
     # The list of schemas only returns those for which we know the
     # path.
     self.failUnlessEqual(Registry.schemas(), ["with-path"])
Example #23
0
"""
Parse a BibTeX file into a BIP (a native pybliographer database) file.

Usage: bibtex2bip.py <BibTeX file> <BIP file>
"""

import sys, os

in_f, out_f = sys.argv[1:3]

from Pyblio.Parsers.Semantic import BibTeX
from Pyblio import Store, Registry

Registry.load_default_settings()

# This id refers to the bibtex format as known by pybliographer by
# default.
sid = "org.pybliographer/bibtex/0.1"

# Get the schema associated with the specified id
schema = Registry.getSchema(sid)

# Create a new db using this schema. We need to ensure the file does
# not exist yet.
try: os.unlink(out_f)
except OSError: pass

db = Store.get('file').dbcreate(out_f, schema)

# Import the content of the bibtex file into it
fd = open(in_f)
Example #24
0
    def __init__ (self, document, query, engine, parent=None):
        self.cell = None
        Utils.GladeWindow.__init__(self, parent)

        s = Registry.getSchema(engine.schema)
        db = Store.get('memory').dbcreate(None, s)
        self.pm = engine(db)

        self.document = document
        self.db = db
        url = Fields.URL('file:/dev/null')
        self.parser = Legacy.Format.BibTeX.DataBase(url)
        self._w_fetch.set_title(_("Results for: %s") % query)
        self.writer = BibTeX.Writer()
        # in order to display a compact form of the results, we need
        # to format them. use a mapping on top of the bibtex version.
        self.bibtex = Adapter.adapt_schema(db, 'org.pybliographer/bibtex/0.1')
        self.cite = Citator.Citator()
        self.cite.xmlload(os.path.join(
            Registry.RIP_dirs['system'], 'full.cip'))
        self.cite.prepare(self.bibtex, None)

        model = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
        self.cell = gtk.CellRendererText()

        l = gtk.Label()
        l.set_markup(_("Results for <i>%s</i>") % escape(query))
        column = gtk.TreeViewColumn(None, self.cell, markup=1)
        column.set_widget(l)
        l.show()

        self._w_view.append_column(column)
        self._w_view.set_model(model)

        callback = self.pm.count(query)

        self._w_progress.set_fraction(0.0)
        self._w_progress.set_text(_("Fetching results"))

        def failure(failure):
            self._w_stop.set_sensitive(False)
            if failure.check(Exceptions.QueryError):
                message = _("Failed to get results")
                secondary = str(failure.value)
            else:
                message = failure.getErrorMessage()
                secondary = failure.getTraceback()
            d = gtk.MessageDialog(self._w_fetch,
                                  gtk.DIALOG_DESTROY_WITH_PARENT,
                                  gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                  message)
            if secondary:
                d.format_secondary_text(secondary)
            d.run()
            d.destroy()

        def fallback(failure):
            print failure

        def done_count(total):
            self._total = total
            self._got = 0.0
            target = min(total, 100)
            def set_progress():
                if target > 0:
                    ratio = min(self._got / target, 1.0)
                else:
                    ratio = 0
                self._w_progress.set_fraction(ratio)
                self._w_progress.set_text(_("Done %d/%d (%d total)") % (
                    len(db.entries), target, self._total))
            set_progress()

            l2cb, rs = self.pm.search(query, maxhits=100)
            def _on_add(k):
                v = self.bibtex[k]
                t = HTML.generate(self.cite.formatter(v))
                model.append((k, t))
                self._got += 1
                set_progress()
            rs.register('add-item', _on_add)
            # ensure this function won't be garbage collected too
            # quickly
            self._on_add = _on_add

            def done(total):
                self._w_stop.set_sensitive(False)
                self._w_progress.set_fraction(1.0)
                self._w_progress.set_text(_("Done %d (%d total)") % (
                    len(db.entries), total))
            l2cb.addCallback(done).\
                addErrback(failure).\
                addErrback(fallback)

        callback.addCallback(done_count).\
            addErrback(failure).\
            addErrback(fallback)