Changeset 176

Show
Ignore:
Timestamp:
08/11/08 21:01:52 (11 months ago)
Author:
rgrp
Message:

[shakespeare/model][l]: refactor to use sqlalchemy.

Location:
trunk/shakespeare
Files:
11 modified

Legend:

Unmodified
Added
Removed
  • trunk/shakespeare/concordance.py

    r154 r176  
    1111import re 
    1212 
    13 import sqlobject 
    14  
    1513import shakespeare.index 
    1614import shakespeare.cache 
     
    2119    TODO: caching?? 
    2220    """ 
    23     sqlcc = shakespeare.model.Concordance 
     21    # sqlcc = shakespeare.model.Concordance 
    2422    sqlstat = shakespeare.model.Statistic 
    2523 
  • trunk/shakespeare/concordance_test.py

    r150 r176  
    77import shakespeare.concordance 
    88 
    9 class TestConcordancer: 
     9# Disable in preparation for removal 
     10class _TestConcordancer: 
    1011 
    1112    inText = \ 
  • trunk/shakespeare/config/environment.py

    r148 r176  
    11"""Pylons environment configuration""" 
    22import os 
     3 
     4from sqlalchemy import engine_from_config 
    35 
    46from pylons import config 
     
    3234    # CONFIGURATION OPTIONS HERE (note: all config options will override 
    3335    # any Pylons config options) 
     36    config['pylons.g'].sa_engine = engine_from_config(config, 'sqlalchemy.') 
  • trunk/shakespeare/gutenberg.py

    r150 r176  
    319319                name += '_f' 
    320320             
    321             numExistingTexts = shakespeare.model.Material.select( 
    322                         shakespeare.model.Material.q.name==name).count() 
     321            numExistingTexts = shakespeare.model.Material.query.filter_by( 
     322                        name=name).count() 
    323323            if numExistingTexts > 0: 
    324324                if self.verbose: 
  • trunk/shakespeare/gutenberg_test.py

    r150 r176  
    142142        shakespeare.model.Material.byName('hamlet_gut_f') 
    143143        assert 'Shakespeare, William' == text1.creator 
    144         alltexts = shakespeare.model.Material.select() 
     144        alltexts = shakespeare.model.Material.query.all() 
    145145        # do not delete because we may remove stuff that was there 
    146146        # though this may undermine tests 
  • trunk/shakespeare/index.py

    r150 r176  
    99 
    1010    def __init__(self): 
    11         self.all = shakespeare.model.Material.select(orderBy='name') 
     11        self.all = shakespeare.model.Material.query.order_by('name').all() 
    1212 
    1313 
  • trunk/shakespeare/moby.py

    r150 r176  
    124124            url = text[1] 
    125125            notes = 'Moby/Bosak Shakespeare, sourced from %s' % text[1] 
    126             numExistingTexts = shakespeare.model.Material.select( 
    127                         shakespeare.model.Material.q.name==name).count() 
     126            numExistingTexts = shakespeare.model.Material.query.filter_by( 
     127                        name=name).count() 
    128128            if numExistingTexts > 0: 
    129129                if self.verbose: 
  • trunk/shakespeare/model/dm.py

    r154 r176  
    88text is a version. 
    99""" 
    10 import sqlobject 
     10from pylons import config 
     11from sqlalchemy import Column, MetaData, Table, types, ForeignKey 
     12from sqlalchemy import orm 
     13from sqlalchemy.orm import relation, backref 
    1114 
    1215# make sure config is registered 
     
    1417shakespeare.conf() 
    1518 
    16 from pylons.database import PackageHub 
    17 hub = PackageHub('shakespeare') 
    18 sqlobject.sqlhub.processConnection = hub.getConnection() 
     19metadata = MetaData() 
     20Session = orm.scoped_session(orm.sessionmaker( 
     21    autoflush=True, 
     22    transactional=False, 
     23    bind=config['pylons.g'].sa_engine 
     24)) 
    1925 
    2026import shakespeare 
     
    2531import annotater.model 
    2632 
    27 # note we run this at bottom of module to auto create db tables on import 
    28 def createdb(): 
    29     Material.createTable(ifNotExists=True) 
    30     Concordance.createTable(ifNotExists=True) 
    31     Statistic.createTable(ifNotExists=True) 
    32     annotater.model.createdb() 
     33material_table = Table('material', metadata, 
     34    Column('id', types.Integer, primary_key=True), 
     35    Column('name', types.String(255)), 
     36    Column('title', types.String(255)), 
     37    Column('creator', types.String(255)), 
     38    Column('url', types.String(255)), 
     39    Column('notes', types.Text()) 
     40    ) 
    3341 
    34 def cleandb(): 
    35     Statistic.dropTable(ifExists=True) 
    36     Concordance.dropTable(ifExists=True) 
    37     Material.dropTable(ifExists=True) 
    38     annotater.model.cleandb() 
     42# TODO: indices on word and occurences 
     43statistic_table = Table('statistic', metadata, 
     44    Column('id', types.Integer, primary_key=True), 
     45    Column('material_id', types.Integer, ForeignKey('material.id')), 
     46    Column('word', types.String(50)), 
     47    Column('occurrences', types.Integer, default=1), 
     48    ) 
    3949 
    40 def rebuilddb(): 
    41     cleandb() 
    42     createdb() 
     50 
     51from ConfigParser import SafeConfigParser 
    4352 
    4453 
    4554 
    46 from ConfigParser import SafeConfigParser 
    47 class Material(sqlobject.SQLObject): 
     55class Material(object): 
    4856    """Material related to Shakespeare (usually text of works and ancillary 
    4957    matter such as introductions). 
     
    5563    TODO: mutiple creators ?? 
    5664    """ 
     65 
     66    # TODO: remove (just here for sqlobject bkwards compat) 
     67    @classmethod 
     68    def byName(self, name): 
     69        return self.query.filter_by(name=name).one() 
    5770     
    58     name = sqlobject.StringCol(alternateID=True) 
    59     title = sqlobject.StringCol(default=None, length=255) 
    60     # creator rather than author to fit with dublin core 
    61     creator = sqlobject.StringCol(default=None, length=255) 
    62     url = sqlobject.StringCol(default=None, length=255) 
    63     notes = sqlobject.StringCol(default=None) 
    64  
    6571    def get_text(self, format=None): 
    6672        '''Get text (if any) associated with this material. 
     
    9399                setattr(item, key, val) 
    94100 
     101class Statistic(object): 
     102    pass 
    95103 
    96 class Concordance(sqlobject.SQLObject): 
     104# Map each domain model class to its corresponding relational table. 
     105mapper = Session.mapper 
     106mapper(Material, material_table) 
     107mapper(Statistic, statistic_table, properties={ 
     108    'text':relation(Material, backref='statistics') 
     109    }) 
    97110 
    98     text = sqlobject.ForeignKey('Material') 
    99     word = sqlobject.StringCol(length=50) 
    100     line = sqlobject.IntCol() 
    101     char_index = sqlobject.IntCol() 
    102  
    103     word_index = sqlobject.DatabaseIndex('word') 
    104     text_index = sqlobject.DatabaseIndex('text') 
    105  
    106 class Statistic(sqlobject.SQLObject): 
    107  
    108     text = sqlobject.ForeignKey('Material') 
    109     word = sqlobject.StringCol(length=50) 
    110     occurrences = sqlobject.IntCol(default=1) 
    111  
    112     word_index = sqlobject.DatabaseIndex('word') 
    113     text_index = sqlobject.DatabaseIndex('text') 
    114  
    115  
    116 # auto create db tables on import 
    117 createdb() 
    118  
  • trunk/shakespeare/tests/functional/test_site.py

    r157 r176  
    1717        assert 'guide to the features of the Open Shakespeare web' in res 
    1818 
    19     def test_concordance(self): 
     19    def _test_concordance(self): 
    2020        url = url_for(controller='site', action='concordance') 
    2121        res = self.app.get(url) 
  • trunk/shakespeare/tests/test_model.py

    r154 r176  
    1 import sqlobject 
    2  
    31import shakespeare.model as model 
    42 
     
    1210        self.text = model.Material(name=self.name, 
    1311                title=self.title, url=self.url) 
     12        model.Session.flush() 
     13        self.textid = self.text.id 
     14        model.Session.clear() 
    1415 
    1516    @classmethod 
    1617    def teardown_class(self): 
    17         model.Material.delete(self.text.id) 
     18        text = model.Material.query.get(self.textid) 
     19        model.Session.delete(text) 
     20        model.Session.flush() 
    1821     
    1922    def test1(self): 
    20         txtid = self.text.id 
    21         txt2 = model.Material.get(txtid) 
     23        txt2 = model.Material.query.get(self.textid) 
    2224        txt3 = model.Material.byName(self.name) 
    2325        assert self.text.id == txt2.id 
     
    2931        assert len(out) > 0 
    3032 
    31     def test_get_text(self): 
     33    # TODO: set up fixtures before running this ... 
     34    def _test_get_text(self): 
    3235        text = model.Material.byName('phoenix_and_the_turtle_gut') 
    3336        out = text.get_text() 
     
    3639        assert out[:26] == 'THE PHOENIX AND THE TURTLE' 
    3740 
    38  
    39 class TestConcordance(object): 
    40  
    41     @classmethod 
    42     def setup_class(self): 
    43         self.name = 'test-123' 
    44         self.title = 'Hamlet' 
    45         self.text = model.Material(name=self.name, title=self.title) 
    46         word = 'jones' 
    47         line = 20 
    48         char_index = 500 
    49         self.cc1 = model.Concordance(text=self.text, 
    50                                          word=word, 
    51                                          line=line, 
    52                                          char_index=char_index) 
    53  
    54     @classmethod 
    55     def teardown_class(self): 
    56         model.Concordance.delete(self.cc1.id) 
    57         model.Material.delete(self.text.id) 
    58  
    59     def test1(self): 
    60         out1 = model.Concordance.get(self.cc1.id) 
    61         assert self.text == out1.text 
    6241 
    6342class TestStatistic: 
     
    7554                occurrences=self.occurrences 
    7655                ) 
     56        model.Session.flush() 
     57        self.statid = self.cc1.id 
     58        model.Session.clear() 
    7759 
    7860    @classmethod 
    7961    def teardown_class(self): 
    80         model.Statistic.delete(self.cc1.id) 
    81         model.Material.delete(self.text.id) 
     62        stat = model.Statistic.query.get(self.statid) 
     63        model.Session.delete(stat) 
     64        model.Session.delete(stat.text) 
     65        model.Session.flush() 
     66        model.Session.remove() 
    8267 
    8368    def test1(self): 
    84         out1 = model.Statistic.get(self.cc1.id) 
    85         assert self.text == out1.text 
     69        out1 = model.Statistic.query.get(self.statid) 
     70        assert out1.text.name == self.name 
    8671        assert out1.occurrences == self.occurrences 
    8772 
    8873    def test_select(self): 
    89         tresults  = model.Statistic.select( 
    90             sqlobject.AND( 
    91                 model.Statistic.q.textID == self.text.id, 
    92                 model.Statistic.q.word == self.word, 
    93                 )) 
     74        tresults = model.Statistic.query.filter_by(text=self.text 
     75                ).filter_by(word=self.word) 
    9476        num = tresults.count() 
    9577        assert num == 1 
  • trunk/shakespeare/websetup.py

    r148 r176  
    1313    conf = appconfig('config:' + filename) 
    1414    load_environment(conf.global_conf, conf.local_conf) 
     15    from shakespeare import model 
     16    log.info('Creating tables') 
     17    model.metadata.create_all(bind=config['pylons.g'].sa_engine) 
     18    log.info('Creating tables: SUCCESS') 
     19