Beispiel #1
0
    def testJoin(self):
        """ The join function takes lists of items and joins them """

        v = join(", ")[Person.lastFirst(all("author"))]
        self._cmp(v, u"Gobry, Frédéric, Fobry, Grédéric, Dobry, Lrédéric")

        v = join(", ")["a", "b", "c"]
        self._cmp(v, u"a, b, c")

        v = join(", ", last="; ")["a", "b", "c"]
        self._cmp(v, u"a, b; c")

        v = join(", ")["a", "b", "c"] + " ok"
        self._cmp(v, u"a, b, c ok")

        # join skip missing values
        v = join(", ")[one("title"), one("journal"), one("gronf")]
        self._cmp(v, u"My title")

        # join fails when _no_ value is available
        v = join(", ")[one("gronf"), one("regronf")]
        phase2 = v(self.db)
        try:
            phase2(self.rec)
            assert False
        except DSL.Missing:
            pass

        # Join with a weird tag in the middle.
        v = "a " + join(BR)["toto", "tutu"] + " b"

        self._cmp(v, u"a toto<br>tutu b")
        return
Beispiel #2
0
    def testAccessor(self):
        """ Check that accessors behave properly """

        # Failure modes are identical for both all and one

        for f in (all, one):
            f("gronf")

            try:
                f("jenexistepas")(self.db)
                assert False
            except KeyError:
                pass

            try:
                f("journal.zoglu")(self.db)
                assert False
            except KeyError:
                pass

        # Access modes
        assert all("title")(self.db)(self.rec) == self.rec["title"]
        assert all("author")(self.db)(self.rec) == self.rec["author"]

        assert one("title")(self.db)(self.rec) == self.rec["title"][0]
        assert one("author")(self.db)(self.rec) == self.rec["author"][0]

        self.failUnlessEqual(all("nest.sub")(self.db)(self.rec), self.rec["nest"][0].q["sub"])
        self.failUnlessEqual(one("nest.sub")(self.db)(self.rec), self.rec["nest"][0].q["sub"][0])
Beispiel #3
0
    def testI18n(self):

        cite = i18n(fr=u"titre " + one("title"), en=u"title " + one("title"), default=u"default " + one("title"))

        r = []
        for ln in ("en", "fr", "it"):
            f = cite(self.db, props={"ln": ln})
            r.append(Text.generate(f(self.rec)))

        self.failUnlessEqual(r, [u"title My title", u"titre My title", u"default My title"])
Beispiel #4
0
    def testAlternate(self):
        """ When the left version fails, use the right version """

        v = one("title") | "success"
        self._cmp(v, "My title")

        v = one("gronf") | "success"
        self._cmp(v, "success")

        v = one("gronf") | one("regronf") | "success"
        self._cmp(v, "success")

        return
Beispiel #5
0
    def testMarkup(self):

        v = I[one("title")]
        self._cmp(v, u"<i>My title</i>")

        v = A(href="http://pybliographer.org/")[one("title")]
        self._cmp(v, u'<a href="http://pybliographer.org/">My title</a>')

        v = I["simple"]
        self._cmp(v, u"<i>simple</i>")

        v = "a " + I["simple ", B[one("title")]]
        self._cmp(v, u"a <i>simple <b>My title</b></i>")

        v = "a " + BR + "in the middle of a phrase"
        self._cmp(v, u"a <br>in the middle of a phrase")

        return
Beispiel #6
0
    def testSwitch(self):
        """ Test the 'switch' operator."""

        def run(c, r):
            f = c(self.db)
            return Text.generate(f(r))

        def txo(name):
            return Attribute.Txo(self.db.schema.txo["type"].byname(name))

        # One cannot use switch on a non-txo attribute
        citation = switch("title").default(one("title"))
        try:
            citation(self.db)
            assert False, "should not be accepted"
        except TypeError:
            pass

        # A switch fails when the value to switch on does not exist
        # and there is no default case.
        citation = switch("type").case(ARTICLE=one("title"))

        try:
            run(citation, self.rec)
            assert False, "should not succeed"
        except DSL.Missing:
            pass

        # With a default case, it should pass
        citation = switch("type").case(ARTICLE=one("singlepage"))
        citation = citation.default(one("title"))

        self.failUnlessEqual(run(citation, self.rec), "My title")

        # Test with the actual value
        self.rec["type"] = [txo("ARTICLE")]
        self.failUnlessEqual(run(citation, self.rec), "123")

        # Another value will also return the default
        self.rec["type"] = [txo("BOOK")]
        self.failUnlessEqual(run(citation, self.rec), "My title")
        return
Beispiel #7
0
    def testOneAdd(self):
        """ It is possible to add one fields together or with text """

        v = one("title") + " ok"
        self._cmp(v, "My title ok")

        v = "ok " + one("title")
        self._cmp(v, "ok My title")

        v = "ok " + one("title") + " ok"
        self._cmp(v, "ok My title ok")

        # a missing field causes a delayed error
        v = "ok " + one("gronf") + " ok"
        try:
            v(self.db)(self.rec)
            assert False
        except DSL.Missing:
            pass
        return
Beispiel #8
0
        return ''.join(k)

# This formats a list of authors according to the Chicago manual of
# style.
def Chicago(people):
    return plural(people,
                  one  = join ('') [ people ],
                  two  = join (' and ') [ people ],
                  more = join (', ', last = ', and ') [ people ])


# Definitions of the "Plain" (and derived) citation format.
plain_author = Chicago(firstLast(all('author')))

plain_journal = join(', ')[
    I[one('journal')],
    join('')[join(':')[one('volume'), one('number')],
             '(' + one('pages') + ')'],
    year(one('date'))
]

plain_place = switch('doctype')
plain_place = plain_place.case(article=plain_journal)
plain_place = plain_place.default(year(one('date')))

plain = join('. ')[plain_author, one('title'), plain_place] + '.'

# The "full" format also provides an abstract.
full = join('\n')[Span(size='large', weight='bold')[one('title')],
                  Span(size='large')[plain_author],
                  Span(size='large')[plain_place],
Beispiel #9
0
            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'))]

article = join(u', ')[
    I[one('journal')],
    
    u'vol. ' + one('volume'),
    u'nr. '  + one('number'),
    u'pp. '  + one('pages'),
    
    Date.year(one('date'))
    ]
Beispiel #10
0
# regression testsuite for OpenOffice.org integration

from Pyblio.Cite.WP.OpenOffice import OOo
from Pyblio.Format import B, one
from Pyblio import Registry, Attribute, Store

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

style = u'This has key ' + B[one('id')]
formatter = style(db)

# tests begin here
oo = OOo()
oo.connect()

oo.text.setString(u'')

refs = [(1, 'a', 'bibtex-a'),
        (2, 'b', 'bibtex-b')]
oo.cite(refs, None)

# check that the citations have been inserted in the document
r = oo.text.getString()
assert r == u'[a][b]', repr(r)

# check that the document can return the existing citations
r = oo.fetch()
assert r == refs, repr(r)
Beispiel #11
0
 def testEscape(self):
     v = one("title") + " ok &"
     self._cmp(v, "My < title & ok &")
Beispiel #12
0
 def testEscape(self):
     v = one("title") + " ok &"
     self._cmp(v, "My &lt; title &amp; ok &amp;")
Beispiel #13
0
    def testPages(self):

        assert Pages.pagesLong(one("singlepage"))(self.db)(self.rec) == u"page\xa0123"
        assert Pages.pagesLong(one("pagerange"))(self.db)(self.rec) == u"pages\xa0123-134"