Skip to content

Commit

Permalink
Creating the SQLAlchemy code for 4 simple tables:
Browse files Browse the repository at this point in the history
    PhyloplumberGroup - simply holds named groups of users
    PhyloplumberUser - holds the log-in info for a user
    PhyloplumberGroupUser - lists pairs of groups and users to indicate which groups a user belongs to
    ExternalProcess - holds the information about a launched process that is running on the FS
  • Loading branch information
mtholder committed Jun 18, 2010
1 parent 84d5140 commit 7b48d2f
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
*.db
phyloplumber.egg-info

4 changes: 4 additions & 0 deletions development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ debug = true
smtp_server = localhost
error_email_from = paste@localhost

top_internal_dir = /Users/mholder/Library/Application Support/phyloplumber
top_external_dir = /Users/mholder/Documents/phyloplumberExternal


[server:main]
use = egg:Paste#http
host = 127.0.0.1
Expand Down
36 changes: 36 additions & 0 deletions phyloplumber/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from pylons import config

_INTERNAL_DIR = os.path.abspath(os.path.expandvars(os.path.expanduser(config.get('top_internal_dir', '~/internals_phyloplumber'))))
_EXTERNAL_DIR = os.path.abspath(os.path.expandvars(os.path.expanduser(config.get('top_external_dir', '~/phyloplumber'))))


_MISSING_SETTING_MSG = 'A %(setting)s setting is required in the initialization file used to start the server'

def verify_dir(dir):
if os.path.exists(dir):
if os.path.isdir(dir):
return True
raise OSError("File %(f)s exists, so a directory cannot be created at that location" % {f : dir})
os.makedirs(dir)

def get_top_internal_dir():
'''Returns the absolute path to the top directory for "internal" storage.
Raises OSError if the directory does not exist and cannot be created.
'''
if not _INTERNAL_DIR:
raise OSError(_MISSING_SETTING_MSG % {'setting' : 'top_internal_dir'})
verify_dir(_INTERNAL_DIR)
return _INTERNAL_DIR

def get_top_external_dir():
'''Returns the absolute path to the top directory for "external" storage.
Raises OSError if the directory does not exist and cannot be created.
'''
if not _EXTERNAL_DIR:
raise OSError(_MISSING_SETTING_MSG % {'setting' : 'top_external_dir'})
verify_dir(_EXTERNAL_DIR)
return _EXTERNAL_DIR

66 changes: 64 additions & 2 deletions phyloplumber/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,69 @@
"""The application's model objects"""
from phyloplumber.model.meta import Session, Base
import sqlalchemy as sa
from sqlalchemy import orm, schema
from sqlalchemy.ext.declarative import declarative_base
from phyloplumber.model import meta

def now():
return datetime.datetime.now()

def init_model(engine):
"""Call me before using any of the tables or classes in the model"""
Session.configure(bind=engine)
#meta.Session.configure(bind=engine)
sm = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
meta.engine = engine
meta.Session = orm.scoped_session(sm)
meta.set_metadata(schema.MetaData(bind=engine))


t_group = schema.Table("PhyloplumberGroup", meta.get_metadata(),
schema.Column('id', sa.types.Integer,
schema.Sequence('groupuser_seq_id', optional=True), primary_key=True),
sa.Column("name", sa.types.String, nullable=False),
)

t_user = schema.Table("PhyloplumberUser", meta.metadata,
schema.Column('id', sa.types.Integer,
schema.Sequence('groupuser_seq_id', optional=True), primary_key=True),
sa.Column("username", sa.types.String, nullable=False),
sa.Column("fullname", sa.types.String, nullable=False),
sa.Column("date_created", sa.types.DateTime, nullable=False),
sa.Column("email", sa.types.DateTime, nullable=False),
)

t_process = schema.Table("ExternalProcess", meta.metadata,
sa.Column("id", sa.types.String, nullable=False, primary_key=True),
sa.Column("parent_dirname", sa.types.String, nullable=False),
sa.Column("launch_timestamp", sa.types.DateTime, nullable=False),
sa.Column("invocation", sa.types.String, nullable=False),
sa.Column("label", sa.types.String, nullable=False),
sa.Column("status", sa.types.Integer, nullable=False),
sa.Column("service_name", sa.types.String, nullable=False), # this is the controller that launched the job
sa.Column("read_group", sa.types.Integer, schema.ForeignKey('PhyloplumberGroup.id')),
sa.Column("write_group", sa.types.Integer, schema.ForeignKey('PhyloplumberGroup.id')),
)



t_group_user = schema.Table('PhyloplumberGroupUser', meta.metadata,
schema.Column('id', sa.types.Integer,
schema.Sequence('groupuser_seq_id', optional=True), primary_key=True),
schema.Column('groupid', sa.types.Integer, schema.ForeignKey('PhyloplumberGroup.id')),
schema.Column('userid', sa.types.Integer, schema.ForeignKey('PhyloplumberUser.id')),
)


class PhyloplumberGroup(object):
pass
class PhyloplumberUser(object):
pass
class PhyloplumberGroupUser(object):
pass
class ExternalProcess(object):
pass

orm.mapper(PhyloplumberGroup, t_group)
orm.mapper(PhyloplumberUser, t_user)
orm.mapper(ExternalProcess, t_process)
orm.mapper(PhyloplumberGroupUser, t_group_user)

17 changes: 15 additions & 2 deletions phyloplumber/model/meta.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
"""SQLAlchemy Metadata and Session object"""
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import schema


__all__ = ['Base', 'Session']

# SQLAlchemy session manager. Updated by model.init_model()
Session = scoped_session(sessionmaker())

# The declarative Base
Base = declarative_base()
metadata = None
def get_metadata():
global metadata
return metadata
def set_metadata(n):
global metadata
metadata = n

# Assign the same metadata object we created earlier.
#Base = declarative_base(metadata=metadata)



6 changes: 3 additions & 3 deletions phyloplumber/websetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pylons.test

from phyloplumber.config.environment import load_environment
from phyloplumber.model.meta import Session, Base
from phyloplumber.model.meta import Session, get_metadata

log = logging.getLogger(__name__)

Expand All @@ -13,6 +13,6 @@ def setup_app(command, conf, vars):
# Don't reload the app if it was loaded under the testing environment
if not pylons.test.pylonsapp:
load_environment(conf.global_conf, conf.local_conf)

# Create the tables if they don't already exist
Base.metadata.create_all(bind=Session.bind)
get_metadata().create_all(bind=Session.bind)
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ verbose = True
verbosity = 2
with-pylons = test.ini
detailed-errors = 1
with-doctest = True

# Babel configuration
[compile_catalog]
Expand Down

0 comments on commit 7b48d2f

Please sign in to comment.