예제 #1
0
class AllHandler(BaseHandler):
    def prepare(self):
        self._bag = DataBag('DataBag', self.application.settings['dbpaste'])

    def get(self):
        pastes = (Paste(**d) for k, d in self._bag.by_created())
        self.render('pastebin_all.html', pastes=pastes)
예제 #2
0
파일: pastebin.py 프로젝트: nod/site33
class AllHandler(BaseHandler):

    def prepare(self):
        self._bag = DataBag('DataBag', self.application.settings['dbpaste'])

    def get(self):
        pastes = (Paste(**d) for k,d in self._bag.by_created())
        self.render('pastebin_all.html', pastes=pastes)
예제 #3
0
파일: collib.py 프로젝트: nod/site33
class DataBagCollection(object):

    def __init__(self, fpath, tblname=None, versioned=False, history=10):
        self._db = DataBag(
                    tblname or 'dbc',
                    fpath,
                    versioned=versioned,
                    history=history
                    )

    def _member(self, k, d):
        """
        children can overwrite this to return anything out of the collection
        """
        return DataBagMember._fd(d)

    def __contains__(self, key_or_obj):
        if isinstance(key_or_obj, DataBagMember):
            return key_or_obj.slug in self._db
        else:
            return key_or_obj in self._db

    def __iter__(self):
        for k,d in self._db.by_created():
            yield self._member(k,d)

    def meta_lists(self):
        """ returns a tuple (years, tags) """
        tags, years = set(), set()
        for p in self:
            years.add(p.c_at.year)
            tags.update(p.tags)
        return (
            sorted(years, reverse=True),
            sorted(tags, cmp=lambda x,y: cmp(x.lower(), y.lower()))
            )

    def _sorted_members(self, iter):
        return sorted(
            iter,
            cmp = lambda x,y: cmp(x.c_at, y.c_at),
            reverse = True
            )

    def all_members(self, year=None):
        """ returns list of posts, limited to year if requested """
        return self._sorted_members(
            p for p in self if (True if not year else year == p.c_at.year)
            )

    def members_with_tag(self, tag):
        return self._sorted_members(p for p in self if (tag in p.tags))

    def member(self, slug, version=None):
        if version is None: version = 0
        return self._member(slug, self._db.get(slug, version=version))

    def save(self, member):
        self._update_member(member.slug, member._d())

    def remove(self, slug_or_mem):
        if isinstance(slug_or_mem, DataBagMember): slug = slug_or_mem.slug
        else: slug = slug_or_mem
        del self._db[slug]

    def _update_member(self, slug, d):
        self._db[slug] = d
예제 #4
0
파일: test_dbag.py 프로젝트: nod/databag
class TestDataBag(unittest.TestCase):
    def setUp(self):
        self.dbag = DataBag('dbag')

    def test_ensure_table(self):
        cur = self.dbag._db.cursor()

        cur.execute(
            '''SELECT name FROM sqlite_master WHERE type='table' AND name=?''',
            (self.dbag._table, ))
        self.assertEqual(cur.fetchone()['name'], self.dbag._table)

        # check the unique index
        sql = '''insert into {} (keyf, data, ver) values ('xx','zzz', 0)
              '''.format(self.dbag._table)
        cur.execute(sql)
        with self.assertRaises(sqlite3.IntegrityError):
            cur.execute(sql)

    def test_get(self):
        k, val = 'blah', 'blip'
        self.dbag[k] = val
        self.assertEqual(None, self.dbag.get('whack'))
        self.assertEqual('soup', self.dbag.get('whack', 'soup'))
        self.assertEqual('soup', self.dbag.get('whack', default='soup'))
        self.assertEqual(val, self.dbag.get(k))

    def test_versioning(self):
        d_v = DataBag(versioned=True, history=2)
        key, orig_text, new_text = 'versioned key', 'blah', 'blip'
        d_v[key] = orig_text
        d_v[key] = new_text

        old = d_v.get(key, version=-1)
        self.assertEqual(old, orig_text)
        self.assertEqual(d_v[key], new_text)

        # now just create more versions past the history size of 2
        # exceptions will rise if this fails
        for x in range(1, 10):
            d_v[key] = 'again' * x

    def test_add_no_key(self):
        val = 'jabberwocky'
        k = self.dbag.add(val)
        self.assertEqual(val, self.dbag[k])

    def test_set_get(self):
        k, val = 'zz', 'more stuff'
        self.dbag[k] = val
        self.assertEqual(val, self.dbag[k])

    def test_set_int(self):
        k, val = 'abc', 555
        self.dbag[k] = val
        self.assertEqual(self.dbag[k], val)

    def test_lame_uniqueness(self):
        # this doesn't gaurantee that _genkeys won't ever return a dup, only
        # that it's not doing it right now in the first n iterations
        keys = set()
        while len(keys) < 100:
            k = self.dbag._genkey()
            self.assertNotIn(k, keys)
            keys.add(k)

    def test_set_list(self):
        k, val = 'abc', [1, 2, 3, 4, 'xyz']
        self.dbag[k] = val
        self.assertListEqual(self.dbag[k], val)

    def test_set_dict(self):
        k, val = 'abc', {'x': 2, '99': 55}
        self.dbag[k] = val
        self.assertDictEqual(self.dbag[k], val)

    def test_set_long_string(self):
        x, y = list(letters), list(letters)
        shuffle(x)
        shuffle(y)
        k, val = 'abc', ''.join([i + j for i in x for j in y])
        self.dbag[k] = val
        self.assertEqual(val, self.dbag[k])

    def test_set_datetime(self):
        k, v = 'dt', datetime.now()
        self.dbag[k] = v
        self.assertEqual(v.isoformat(), self.dbag[k])

    def test_delitem(self):
        k, val = 'abc', 'defghij'
        self.dbag[k] = val
        del self.dbag[k]
        with self.assertRaises(KeyError):
            self.dbag[k]

        # ensure can't delete nonexistent item
        with self.assertRaises(KeyError):
            del self.dbag[k]

    def test_when(self):
        # really just want a datetime back
        k, val = 'xyz', 'abcdef'
        self.dbag[k] = val
        self.assertEqual(type(self.dbag.when(k)), datetime)

    def test_iter(self):
        self.dbag['xxx'] = '123'
        self.dbag['aaa'] = 'def'
        keys = [k for k in self.dbag]
        self.assertListEqual(['aaa', 'xxx'], keys)
        for k in self.dbag:
            self.assertTrue(self.dbag[k])

    def test_by_created(self):
        self.dbag['xxx'] = '123'
        self.dbag['aaa'] = '123'
        test_d = [('xxx', '123'), ('aaa', '123')]
        self.assertListEqual(test_d, [x for x in self.dbag.by_created()])
        self.assertListEqual(test_d[::-1],
                             [x for x in self.dbag.by_created(desc=True)])

    def test_in(self):
        k, val = '123', 123
        self.dbag[k] = val
        self.assertTrue(k in self.dbag)
        self.assertFalse('not there' in self.dbag)

    def test_nondefault_tablename(self):
        self.assertTrue(DataBag(table='something', fpath=':memory:'))
예제 #5
0
class TestDataBag(unittest.TestCase):

    def setUp(self):
        self.dbag = DataBag()

    def test_ensure_table(self):
        cur = self.dbag._db.cursor()

        cur.execute(
            '''SELECT name FROM sqlite_master WHERE type='table' AND name=?''',
            (self.dbag._bag,)
            )
        self.assertEqual(cur.fetchone()['name'], self.dbag._bag)

        # check the unique index
        sql = '''insert into {} (keyf, data, ver) values ('xx','zzz', 0)
              '''.format( self.dbag._bag )
        cur.execute(sql)
        with self.assertRaises(sqlite3.IntegrityError): cur.execute(sql)

    def test_get(self):
        k,val = 'blah', 'blip'
        self.dbag[k] = val
        self.assertEqual( None, self.dbag.get('whack') )
        self.assertEqual( 'soup', self.dbag.get('whack', 'soup') )
        self.assertEqual( 'soup', self.dbag.get('whack', default='soup') )
        self.assertEqual( val, self.dbag.get(k) )

    def test_versioning(self):
        d_v = DataBag(versioned=True, history=2)
        key, orig_text, new_text = 'versioned key', 'blah', 'blip'
        d_v[key] = orig_text
        d_v[key] = new_text

        old = d_v.get(key, version=-1)
        self.assertEqual( old, orig_text )
        self.assertEqual( d_v[key], new_text )

        # now just create more versions past the history size of 2
        # exceptions will rise if this fails
        for x in xrange(1,10):
            d_v[key] = 'again'*x

    def test_add_no_key(self):
        val = 'jabberwocky'
        k = self.dbag.add(val)
        self.assertEqual(val, self.dbag[k])

    def test_set_get(self):
        k,val = 'zz', 'more stuff'
        self.dbag[k] = val
        self.assertEqual(val, self.dbag[k])

    def test_set_int(self):
        k,val = 'abc', 555
        self.dbag[k] = val
        self.assertEqual(self.dbag[k], val)

    def test_lame_uniqueness(self):
        # this doesn't gaurantee that _genkeys won't ever return a dup, only
        # that it's not doing it right now in the first n iterations
        keys = set()
        while len(keys) < 100:
            k = self.dbag._genkey()
            self.assertNotIn(k, keys)
            keys.add(k)

    def test_set_list(self):
        k,val = 'abc', [1,2,3,4,'xyz']
        self.dbag[k] = val
        self.assertListEqual(self.dbag[k], val)

    def test_set_dict(self):
        k,val = 'abc', {'x': 2, '99':55}
        self.dbag[k] = val
        self.assertDictEqual(self.dbag[k], val)

    def test_set_long_string(self):
        x,y = list(letters), list(letters)
        shuffle(x)
        shuffle(y)
        k,val = 'abc',''.join( [ i+j for i in x for j in y ] )
        self.dbag[k] = val
        self.assertEqual(val, self.dbag[k])

    def test_set_datetime(self):
        k,v = 'dt', datetime.now()
        self.dbag[k] = v
        self.assertEqual(v.isoformat(), self.dbag[k])

    def test_delitem(self):
        k, val = 'abc', 'defghij'
        self.dbag[k] = val
        del self.dbag[k]
        with self.assertRaises(KeyError): self.dbag[k]

        # ensure can't delete nonexistent item
        with self.assertRaises(KeyError): del self.dbag[k]

    def test_when(self):
        k, val = 'xyz', 'abcdef'
        self.dbag[k] = val
        self.assertEqual(type(self.dbag.when(k)), datetime)

    def test_iter(self):
        self.dbag['xxx'] = '123'
        self.dbag['aaa'] = 'def'
        keys = [k for k in self.dbag]
        self.assertListEqual(['aaa','xxx'], keys)
        for k in self.dbag:
            self.assertTrue( self.dbag[k] )

    def test_by_created(self):
        self.dbag['xxx'] = '123'
        self.dbag['aaa'] = '123'
        test_d = [('xxx','123'), ('aaa', '123')]
        self.assertListEqual(test_d, [x for x in self.dbag.by_created()])
        self.assertListEqual(
            test_d[::-1],
            [x for x in self.dbag.by_created(desc=True)]
            )

    def test_in(self):
        k, val = '123', 123
        self.dbag[k] = val
        self.assertTrue( k in self.dbag )
        self.assertFalse( 'not there' in self.dbag )

    def test_nondefault_tablename(self):
        self.assertTrue( DataBag(fpath=':memory:', bag='something') )