示例#1
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
示例#2
0
# 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],
                  Span(color='#505050')[one('abstract')]]