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])
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
def testMiscPlural(self): zm = Misc.plural(all("gronf"), zero="zero", more="more")(self.db) zom = Misc.plural(all("gronf"), zero="zero", one="one", more="more")(self.db) zotm = Misc.plural(all("gronf"), zero="zero", one="one", two="two", more="more")(self.db) def run(format, vals): r = Store.Record() r["gronf"] = vals return Text.generate(format(r)) self.failUnlessEqual(run(zm, []), "zero") self.failUnlessEqual(run(zm, [1]), "more") self.failUnlessEqual(run(zom, [1]), "one") self.failUnlessEqual(run(zom, [1, 2]), "more") self.failUnlessEqual(run(zotm, [1, 2]), "two") self.failUnlessEqual(run(zotm, [1, 2, 3]), "more")
if 'date' in rec: k.append(str(rec['date'][0].year)[-2:]) 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.
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')) ] default = Date.year(one('date')) place = switch('doctype')
def run(fn): formatter = fn(all("author"))(self.db) return formatter(r)