Changeset - d75d9ce1320d
[Not reviewed]
default
0 20 1
Thomas De Schampheleire - 9 years ago 2016-09-25 15:06:34
thomas.de.schampheleire@gmail.com
model: move code from __init__.py to base.py

Having too much code, in particular too much imports, inside a package's
__init__.py is a recipe for circular imports, and considered bad practice in
Python [1]

Move out everything from kallithea/model/__init__.py to a new file
kallithea/model/base.py and adapt the existing imports.

[1] http://docs.python-guide.org/en/latest/writing/structure/#packages
21 files changed with 21 insertions and 104 deletions:
0 comments (0 inline, 0 general)
kallithea/config/environment.py
Show inline comments
 
@@ -35,7 +35,7 @@ from kallithea.lib.utils import repo2db_
 
    load_rcextensions, check_git_version, set_vcs_config, set_indexer_config
 
from kallithea.lib.utils2 import engine_from_config, str2bool
 
from kallithea.lib.db_manage import DbManage
 
from kallithea.model import init_model
 
from kallithea.model.base import init_model
 
from kallithea.model.scm import ScmModel
 

	
 
log = logging.getLogger(__name__)
kallithea/lib/celerylib/__init__.py
Show inline comments
 
@@ -37,7 +37,7 @@ from decorator import decorator
 
from kallithea import CELERY_ON, CELERY_EAGER
 
from kallithea.lib.utils2 import safe_str
 
from kallithea.lib.pidlock import DaemonLock, LockHeld
 
from kallithea.model import init_model
 
from kallithea.model.base import init_model
 
from kallithea.model import meta
 

	
 
from sqlalchemy import engine_from_config
kallithea/lib/db_manage.py
Show inline comments
 
@@ -38,7 +38,7 @@ import alembic.command
 

	
 
from kallithea.lib.paster_commands.common import ask_ok
 
from kallithea.model.user import UserModel
 
from kallithea.model import init_model
 
from kallithea.model.base import init_model
 
from kallithea.model.db import User, Permission, Ui, \
 
    Setting, UserToPerm, RepoGroup, \
 
    UserRepoGroupToPerm, CacheInvalidation, Repository
kallithea/lib/hooks.py
Show inline comments
 
@@ -384,7 +384,7 @@ def handle_git_receive(repo_path, revs, 
 
    from paste.deploy import appconfig
 
    from sqlalchemy import engine_from_config
 
    from kallithea.config.environment import load_environment
 
    from kallithea.model import init_model
 
    from kallithea.model.base import init_model
 
    from kallithea.model.db import Ui
 
    from kallithea.lib.utils import make_ui
 
    extras = _extract_extras(env)
kallithea/lib/paster_commands/common.py
Show inline comments
 
@@ -100,7 +100,7 @@ class BasePasterCommand(Command):
 
        logging.config.fileConfig(self.path_to_ini_file)
 

	
 
        from pylons import config
 
        from kallithea.model import init_model
 
        from kallithea.model.base import init_model
 
        from kallithea.lib.utils2 import engine_from_config
 
        setup_cache_regions(config)
 
        engine = engine_from_config(config, 'sqlalchemy.')
kallithea/model/__init__.py
Show inline comments
 
@@ -23,87 +23,4 @@ Original author and date, and relevant c
 
:author: marcink
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
 
:license: GPLv3, see LICENSE.md for more details.
 

	
 

	
 
:example:
 

	
 
    .. code-block:: python
 

	
 
       from paste.deploy import appconfig
 
       from pylons import config
 
       from sqlalchemy import engine_from_config
 
       from kallithea.config.environment import load_environment
 

	
 
       conf = appconfig('config:development.ini', relative_to = './../../')
 
       load_environment(conf.global_conf, conf.local_conf)
 

	
 
       engine = engine_from_config(config, 'sqlalchemy.')
 
       init_model(engine)
 
       # RUN YOUR CODE HERE
 

	
 
"""
 

	
 

	
 
import logging
 
from kallithea.model import meta
 
from kallithea.lib.utils2 import obfuscate_url_pw
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
def init_model(engine):
 
    """
 
    Initializes db session, bind the engine with the metadata,
 
    Call this before using any of the tables or classes in the model,
 
    preferably once in application start
 

	
 
    :param engine: engine to bind to
 
    """
 
    engine_str = obfuscate_url_pw(str(engine.url))
 
    log.info("initializing db for %s", engine_str)
 
    meta.Base.metadata.bind = engine
 

	
 

	
 
class BaseModel(object):
 
    """
 
    Base Model for all Kallithea models, it adds sql alchemy session
 
    into instance of model
 

	
 
    :param sa: If passed it reuses this session instead of creating a new one
 
    """
 

	
 
    def __init__(self, sa=None):
 
        if sa is not None:
 
            self.sa = sa
 
        else:
 
            self.sa = meta.Session()
 

	
 
    def _get_user(self, user):
 
        """
 
        Helper method to get user by ID, or username fallback
 

	
 
        :param user: UserID, username, or User instance
 
        """
 
        from kallithea.model.db import User
 
        return User.guess_instance(user,
 
                                  callback=User.get_by_username)
 

	
 
    def _get_repo(self, repository):
 
        """
 
        Helper method to get repository by ID, or repository name
 

	
 
        :param repository: RepoID, repository name or Repository Instance
 
        """
 
        from kallithea.model.db import Repository
 
        return Repository.guess_instance(repository,
 
                                  callback=Repository.get_by_repo_name)
 

	
 
    def _get_perm(self, permission):
 
        """
 
        Helper method to get permission by ID, or permission name
 

	
 
        :param permission: PermissionID, permission_name or Permission instance
 
        """
 
        from kallithea.model.db import Permission
 
        return Permission.guess_instance(permission,
 
                                  callback=Permission.get_by_key)
kallithea/model/api_key.py
Show inline comments
 
@@ -30,7 +30,7 @@ import logging
 
from sqlalchemy import or_
 

	
 
from kallithea.lib.utils2 import generate_api_key
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import UserApiKeys
 
from kallithea.model.meta import Session
 

	
kallithea/model/base.py
Show inline comments
 
file copied from kallithea/model/__init__.py to kallithea/model/base.py
 
@@ -12,8 +12,8 @@
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
"""
 
kallithea.model
 
~~~~~~~~~~~~~~~
 
kallithea.model.base
 
~~~~~~~~~~~~~~~~~~~~
 

	
 
The application's model objects
 

	
kallithea/model/changeset_status.py
Show inline comments
 
@@ -28,7 +28,7 @@ Original author and date, and relevant c
 
import logging
 
from sqlalchemy.orm import joinedload
 

	
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import ChangesetStatus, PullRequest
 
from kallithea.lib.exceptions import StatusChangeOnClosedPullRequestError
 

	
kallithea/model/comment.py
Show inline comments
 
@@ -32,7 +32,7 @@ from collections import defaultdict
 

	
 
from kallithea.lib.utils2 import extract_mentioned_users, safe_unicode
 
from kallithea.lib import helpers as h
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import ChangesetComment, User, \
 
    Notification, PullRequest
 
from kallithea.model.notification import NotificationModel
kallithea/model/gist.py
Show inline comments
 
@@ -34,7 +34,7 @@ import shutil
 
from kallithea.lib.utils2 import safe_unicode, unique_id, safe_int, \
 
    time_to_datetime, AttributeDict
 
from kallithea.lib.compat import json
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import Gist
 
from kallithea.model.repo import RepoModel
 
from kallithea.model.scm import ScmModel
kallithea/model/notification.py
Show inline comments
 
@@ -36,7 +36,7 @@ from sqlalchemy.orm import joinedload, s
 
import kallithea
 
from kallithea.lib import helpers as h
 
from kallithea.lib.utils2 import safe_unicode
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import Notification, User, UserNotification
 
from kallithea.model.meta import Session
 

	
kallithea/model/permission.py
Show inline comments
 
@@ -31,7 +31,7 @@ import traceback
 

	
 
from sqlalchemy.exc import DatabaseError
 

	
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import User, Permission, UserToPerm, UserRepoToPerm, \
 
    UserRepoGroupToPerm, UserUserGroupToPerm
 
from kallithea.lib.utils2 import str2bool
kallithea/model/pull_request.py
Show inline comments
 
@@ -35,7 +35,7 @@ from sqlalchemy.orm import joinedload
 
from kallithea.model.meta import Session
 
from kallithea.lib import helpers as h
 
from kallithea.lib.exceptions import UserInvalidException
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import PullRequest, PullRequestReviewers, Notification, \
 
    ChangesetStatus, User
 
from kallithea.model.notification import NotificationModel
kallithea/model/repo.py
Show inline comments
 
@@ -41,7 +41,7 @@ from kallithea.lib.utils2 import LazyPro
 
from kallithea.lib.caching_query import FromCache
 
from kallithea.lib.hooks import log_delete_repository
 

	
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import Repository, UserRepoToPerm, UserGroupRepoToPerm, \
 
    UserRepoGroupToPerm, UserGroupRepoGroupToPerm, User, Permission, \
 
    Statistics, UserGroup, Ui, RepoGroup, RepositoryField
kallithea/model/repo_group.py
Show inline comments
 
@@ -34,7 +34,7 @@ import datetime
 

	
 
from kallithea.lib.utils2 import LazyProperty
 

	
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import RepoGroup, Ui, UserRepoGroupToPerm, \
 
    User, Permission, UserGroupRepoGroupToPerm, UserGroup, Repository
 

	
kallithea/model/repo_permission.py
Show inline comments
 
@@ -24,7 +24,7 @@ Original author and date, and relevant c
 
"""
 

	
 
import logging
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import UserRepoToPerm, UserGroupRepoToPerm, \
 
    Permission
 

	
kallithea/model/scm.py
Show inline comments
 
@@ -53,7 +53,7 @@ from kallithea.lib.auth import HasRepoPe
 
    HasUserGroupPermissionAny, HasPermissionAny, HasPermissionAny
 
from kallithea.lib.utils import get_filesystem_repos, make_ui, \
 
    action_logger
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import Repository, Ui, CacheInvalidation, \
 
    UserFollowing, UserLog, User, RepoGroup, PullRequest
 
from kallithea.lib.hooks import log_push_action
kallithea/model/user.py
Show inline comments
 
@@ -39,7 +39,7 @@ from sqlalchemy.exc import DatabaseError
 

	
 
from kallithea.lib.utils2 import safe_str, generate_api_key, get_current_authuser
 
from kallithea.lib.caching_query import FromCache
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import User, UserToPerm, Notification, \
 
    UserEmailMap, UserIpMap
 
from kallithea.lib.exceptions import DefaultUserException, \
kallithea/model/user_group.py
Show inline comments
 
@@ -27,7 +27,7 @@ Original author and date, and relevant c
 
import logging
 
import traceback
 

	
 
from kallithea.model import BaseModel
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import UserGroupMember, UserGroup, \
 
    UserGroupRepoToPerm, Permission, UserGroupToPerm, User, UserUserGroupToPerm, \
 
    UserGroupUserGroupToPerm
kallithea/tests/scripts/manual_test_concurrency.py
Show inline comments
 
@@ -39,7 +39,7 @@ from paste.deploy import appconfig
 
from sqlalchemy import engine_from_config
 

	
 
from kallithea.lib.utils import setup_cache_regions
 
from kallithea.model import init_model
 
from kallithea.model.base import init_model
 
from kallithea.model import meta
 
from kallithea.model.db import User, Repository, Ui
 
from kallithea.lib.auth import get_crypt_password
0 comments (0 inline, 0 general)