Moving the "dependency injection printer tools" over to tools/common.py
This commit is contained in:
parent
8a22617ffe
commit
35a24fc263
@ -19,13 +19,7 @@ import sys
|
|||||||
from mediagoblin.db.sql.base import Session
|
from mediagoblin.db.sql.base import Session
|
||||||
from mediagoblin.db.sql.models import MediaEntry, Tag, MediaTag
|
from mediagoblin.db.sql.models import MediaEntry, Tag, MediaTag
|
||||||
|
|
||||||
|
from mediagoblin.tools.common import simple_printer
|
||||||
def _simple_printer(string):
|
|
||||||
"""
|
|
||||||
Prints a string, but without an auto \n at the end.
|
|
||||||
"""
|
|
||||||
sys.stdout.write(string)
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
|
|
||||||
class MigrationManager(object):
|
class MigrationManager(object):
|
||||||
@ -37,7 +31,7 @@ class MigrationManager(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name, models, migration_registry, session,
|
def __init__(self, name, models, migration_registry, session,
|
||||||
printer=_simple_printer):
|
printer=simple_printer):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
- name: identifier of this section of the database
|
- name: identifier of this section of the database
|
||||||
|
@ -27,6 +27,7 @@ from migrate import changeset
|
|||||||
|
|
||||||
from mediagoblin.db.sql.base import GMGTableBase
|
from mediagoblin.db.sql.base import GMGTableBase
|
||||||
from mediagoblin.db.sql.util import MigrationManager, RegisterMigration
|
from mediagoblin.db.sql.util import MigrationManager, RegisterMigration
|
||||||
|
from mediagoblin.tools.common import CollectingPrinter
|
||||||
|
|
||||||
|
|
||||||
# This one will get filled with local migrations
|
# This one will get filled with local migrations
|
||||||
@ -520,18 +521,6 @@ def _insert_migration3_objects(session):
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
class CollectingPrinter(object):
|
|
||||||
def __init__(self):
|
|
||||||
self.collection = []
|
|
||||||
|
|
||||||
def __call__(self, string):
|
|
||||||
self.collection.append(string)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def combined_string(self):
|
|
||||||
return u''.join(self.collection)
|
|
||||||
|
|
||||||
|
|
||||||
def create_test_engine():
|
def create_test_engine():
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
engine = create_engine('sqlite:///:memory:', echo=False)
|
engine = create_engine('sqlite:///:memory:', echo=False)
|
||||||
|
@ -36,3 +36,39 @@ def import_component(import_string):
|
|||||||
module = sys.modules[module_name]
|
module = sys.modules[module_name]
|
||||||
func = getattr(module, func_name)
|
func = getattr(module, func_name)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
def simple_printer(string):
|
||||||
|
"""
|
||||||
|
Prints a string, but without an auto \n at the end.
|
||||||
|
|
||||||
|
Useful for places where we want to dependency inject for printing.
|
||||||
|
"""
|
||||||
|
sys.stdout.write(string)
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
|
class CollectingPrinter(object):
|
||||||
|
"""
|
||||||
|
Another printer object, this one useful for capturing output for
|
||||||
|
examination during testing or otherwise.
|
||||||
|
|
||||||
|
Use this like:
|
||||||
|
|
||||||
|
>>> printer = CollectingPrinter()
|
||||||
|
>>> printer("herp derp\n")
|
||||||
|
>>> printer("lollerskates\n")
|
||||||
|
>>> printer.combined_string
|
||||||
|
"herp derp\nlollerskates\n"
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
self.collection = []
|
||||||
|
|
||||||
|
def __call__(self, string):
|
||||||
|
self.collection.append(string)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def combined_string(self):
|
||||||
|
return u''.join(self.collection)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user