[PATCH 1 of 2] model: move code from __init__.py to base.py

Thomas De Schampheleire patrickdepinguin at gmail.com
Sun Sep 25 13:21:32 UTC 2016


# HG changeset patch
# User Thomas De Schampheleire <thomas.de.schampheleire at gmail.com>
# Date 1474808794 -7200
#      Sun Sep 25 15:06:34 2016 +0200
# Node ID 4da83b467a145ce5281157d0bc935cbcac5343c6
# Parent  4e918cbc155d5281b2c39391834ad503c6d3e7f8
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

diff --git a/kallithea/config/environment.py b/kallithea/config/environment.py
--- a/kallithea/config/environment.py
+++ b/kallithea/config/environment.py
@@ -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__)
diff --git a/kallithea/lib/celerylib/__init__.py b/kallithea/lib/celerylib/__init__.py
--- a/kallithea/lib/celerylib/__init__.py
+++ b/kallithea/lib/celerylib/__init__.py
@@ -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
diff --git a/kallithea/lib/db_manage.py b/kallithea/lib/db_manage.py
--- a/kallithea/lib/db_manage.py
+++ b/kallithea/lib/db_manage.py
@@ -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
diff --git a/kallithea/lib/hooks.py b/kallithea/lib/hooks.py
--- a/kallithea/lib/hooks.py
+++ b/kallithea/lib/hooks.py
@@ -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)
diff --git a/kallithea/lib/paster_commands/common.py b/kallithea/lib/paster_commands/common.py
--- a/kallithea/lib/paster_commands/common.py
+++ b/kallithea/lib/paster_commands/common.py
@@ -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.')
diff --git a/kallithea/model/__init__.py b/kallithea/model/__init__.py
--- a/kallithea/model/__init__.py
+++ b/kallithea/model/__init__.py
@@ -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)
diff --git a/kallithea/model/api_key.py b/kallithea/model/api_key.py
--- a/kallithea/model/api_key.py
+++ b/kallithea/model/api_key.py
@@ -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
 
diff --git a/kallithea/model/__init__.py b/kallithea/model/base.py
copy from kallithea/model/__init__.py
copy to kallithea/model/base.py
--- a/kallithea/model/__init__.py
+++ b/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
 
diff --git a/kallithea/model/changeset_status.py b/kallithea/model/changeset_status.py
--- a/kallithea/model/changeset_status.py
+++ b/kallithea/model/changeset_status.py
@@ -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
 
diff --git a/kallithea/model/comment.py b/kallithea/model/comment.py
--- a/kallithea/model/comment.py
+++ b/kallithea/model/comment.py
@@ -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
diff --git a/kallithea/model/gist.py b/kallithea/model/gist.py
--- a/kallithea/model/gist.py
+++ b/kallithea/model/gist.py
@@ -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
diff --git a/kallithea/model/notification.py b/kallithea/model/notification.py
--- a/kallithea/model/notification.py
+++ b/kallithea/model/notification.py
@@ -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
 
diff --git a/kallithea/model/permission.py b/kallithea/model/permission.py
--- a/kallithea/model/permission.py
+++ b/kallithea/model/permission.py
@@ -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
diff --git a/kallithea/model/pull_request.py b/kallithea/model/pull_request.py
--- a/kallithea/model/pull_request.py
+++ b/kallithea/model/pull_request.py
@@ -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
diff --git a/kallithea/model/repo.py b/kallithea/model/repo.py
--- a/kallithea/model/repo.py
+++ b/kallithea/model/repo.py
@@ -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
diff --git a/kallithea/model/repo_group.py b/kallithea/model/repo_group.py
--- a/kallithea/model/repo_group.py
+++ b/kallithea/model/repo_group.py
@@ -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
 
diff --git a/kallithea/model/repo_permission.py b/kallithea/model/repo_permission.py
--- a/kallithea/model/repo_permission.py
+++ b/kallithea/model/repo_permission.py
@@ -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
 
diff --git a/kallithea/model/scm.py b/kallithea/model/scm.py
--- a/kallithea/model/scm.py
+++ b/kallithea/model/scm.py
@@ -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
diff --git a/kallithea/model/user.py b/kallithea/model/user.py
--- a/kallithea/model/user.py
+++ b/kallithea/model/user.py
@@ -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, \
diff --git a/kallithea/model/user_group.py b/kallithea/model/user_group.py
--- a/kallithea/model/user_group.py
+++ b/kallithea/model/user_group.py
@@ -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
diff --git a/kallithea/tests/scripts/manual_test_concurrency.py b/kallithea/tests/scripts/manual_test_concurrency.py
--- a/kallithea/tests/scripts/manual_test_concurrency.py
+++ b/kallithea/tests/scripts/manual_test_concurrency.py
@@ -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


More information about the kallithea-general mailing list