Skip to content

Commit

Permalink
Removed last dependent of utils.state
Browse files Browse the repository at this point in the history
  • Loading branch information
chintal committed Sep 25, 2015
1 parent db1b8ab commit 2fb4758
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 16 deletions.
31 changes: 31 additions & 0 deletions alembic/versions/205d6ab2febf_create_sno_series_table.py
@@ -0,0 +1,31 @@
"""create_sno_series_table
Revision ID: 205d6ab2febf
Revises: 1f03af77d718
Create Date: 2015-09-25 10:55:07.669159
"""

# revision identifiers, used by Alembic.
revision = '205d6ab2febf'
down_revision = '1f03af77d718'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
op.create_table(
'SerialNumberSeries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('series', sa.String, nullable=False, unique=True),
sa.Column('last_seed', sa.String, nullable=False, unique=False)
)


def downgrade():
op.drop_Table(
'SerialNumberSeries'
)
1 change: 0 additions & 1 deletion requirements.txt
Expand Up @@ -8,7 +8,6 @@ wsgiref==0.1.2
matplotlib==1.4.3
jsmin==2.1.2
psycopg2==2.6.1
dataset==0.6.0
pycallgraph==1.0.1
arrow==0.6.0
qrcode==5.1
Expand Down
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -73,7 +73,6 @@ def read(fname):

# Perhaps require reconsideration
'matplotlib', # Pulls in numpy!
'dataset', # Replace with db instead?
'splinter', # Replace with direct selenium usage

# Extracted Modules
Expand Down
18 changes: 18 additions & 0 deletions tendril/entityhub/db/controller.py
Expand Up @@ -26,13 +26,31 @@

from tendril.utils.db import with_db

from model import SerialNumberSeries
from model import SerialNumber
from model import SerialNumberAssociation

from tendril.utils import log
logger = log.get_logger(__name__, log.DEFAULT)


@with_db
def get_series_obj(series=None, session=None):
return session.query(SerialNumberSeries).filter_by(series=series).one()


@with_db
def create_series_obj(series=None, start_seed=None, session=None):
if not series or not isinstance(series, str):
raise ValueError('series should be a string, got : ' + repr(series))
if not start_seed or not isinstance(series, str):
raise ValueError('start_seed should be a string, got : ' + repr(start_seed))
sobj = SerialNumberSeries(series=series, last_seed=start_seed)
session.add(sobj)
session.flush()
return sobj


@with_db
def get_all_serialnos(session=None):
return session.query(SerialNumber).all()
Expand Down
5 changes: 5 additions & 0 deletions tendril/entityhub/db/model.py
Expand Up @@ -31,6 +31,11 @@
logger = log.get_logger(__name__, log.DEFAULT)


class SerialNumberSeries(BaseMixin, DeclBase):
series = Column(String, unique=True, nullable=False)
last_seed = Column(String, unique=False, nullable=False)


class SerialNumber(TimestampMixin, BaseMixin, DeclBase):
sno = Column(String, unique=True, nullable=False)
efield = Column(String, unique=False, nullable=True)
Expand Down
16 changes: 7 additions & 9 deletions tendril/entityhub/serialnos.py
Expand Up @@ -21,7 +21,6 @@

import idstring

import tendril.utils.state
from tendril.utils.db import with_db

from db import controller
Expand All @@ -30,8 +29,6 @@
from tendril.utils import log
logger = log.get_logger(__name__, log.DEBUG)

nsno_table = tendril.utils.state.state_ds['nsno']


def get_series(sno):
return sno.split('-')[0]
Expand Down Expand Up @@ -112,18 +109,17 @@ def delete_serialno(sno, recurse=False, session=None):
def get_serialno(series=None, efield=None, register=True,
start_seed='100A', session=None):
series = series.upper()
if series in [x['series'] for x in nsno_table]:
last_seed = nsno_table.find_one(series=series)['last_seed']
series_obj = controller.get_series_obj(series=series, session=session)
if series_obj is not None:
last_seed = series_obj.last_seed
logger.debug("Found last seed for series " + str(series) +
" : " + str(last_seed))
generator = idstring.IDstring(seed=last_seed)
new_sno = generator + 1
if register is True:
logger.info("Updating seed for series " + series +
" : " + str(new_sno.get_seed()))
nsno_table.update(
dict(series=series, last_seed=new_sno.get_seed()), ['series']
)
series_obj.last_seed = new_sno.get_seed()
register_serialno(
sno=series + '-' + new_sno,
efield=efield, session=session
Expand All @@ -137,7 +133,9 @@ def get_serialno(series=None, efield=None, register=True,
generator = idstring.IDstring(seed=start_seed)
if register is True:
logger.info("Creating series in db : " + series)
nsno_table.insert(dict(series=series, last_seed=start_seed))
controller.create_series_obj(
series=series, start_seed=start_seed, session=session
)
register_serialno(
sno=series + '-' + generator,
efield=efield, session=session
Expand Down
10 changes: 5 additions & 5 deletions tendril/utils/state.py
Expand Up @@ -18,12 +18,12 @@
The Persistent State Utils Module (:mod:`tendril.utils.state`)
==============================================================
.. warning:: This module is deprecated in favor of
:mod:`tendril.utils.db`, and it's dependencies have
been purged from the tendril requirements.
This module uses :mod:`dataset` to provide a means to store persistent
state information. Note that this means of storing persistent information
is under review, since most of the present usages will benefit from full
database integration. This will likely be replaced by integrating it into
the application database using the interfaces in :mod:`tendril.utils.db`
instead.
state information.
The primary storage is an sqlite3 database, located within the INSTANCE_ROOT.
This storage is accessible by application code by importing this module's
Expand Down

0 comments on commit 2fb4758

Please sign in to comment.