changeset 6242:d75d9ce1320d

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
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Sun, 25 Sep 2016 15:06:34 +0200
parents 19c9f4f5428e
children 457f1de3ec5d
files kallithea/config/environment.py kallithea/lib/celerylib/__init__.py kallithea/lib/db_manage.py kallithea/lib/hooks.py kallithea/lib/paster_commands/common.py kallithea/model/__init__.py kallithea/model/api_key.py kallithea/model/base.py kallithea/model/changeset_status.py kallithea/model/comment.py kallithea/model/gist.py kallithea/model/notification.py kallithea/model/permission.py kallithea/model/pull_request.py kallithea/model/repo.py kallithea/model/repo_group.py kallithea/model/repo_permission.py kallithea/model/scm.py kallithea/model/user.py kallithea/model/user_group.py kallithea/tests/scripts/manual_test_concurrency.py
diffstat 21 files changed, 128 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/config/environment.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/config/environment.py	Sun Sep 25 15:06:34 2016 +0200
@@ -35,7 +35,7 @@
     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__)
--- a/kallithea/lib/celerylib/__init__.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/lib/celerylib/__init__.py	Sun Sep 25 15:06:34 2016 +0200
@@ -37,7 +37,7 @@
 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
--- a/kallithea/lib/db_manage.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/lib/db_manage.py	Sun Sep 25 15:06:34 2016 +0200
@@ -38,7 +38,7 @@
 
 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
--- a/kallithea/lib/hooks.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/lib/hooks.py	Sun Sep 25 15:06:34 2016 +0200
@@ -384,7 +384,7 @@
     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)
--- a/kallithea/lib/paster_commands/common.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/lib/paster_commands/common.py	Sun Sep 25 15:06:34 2016 +0200
@@ -100,7 +100,7 @@
         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.')
--- a/kallithea/model/__init__.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/__init__.py	Sun Sep 25 15:06:34 2016 +0200
@@ -23,87 +23,4 @@
 :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)
--- a/kallithea/model/api_key.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/api_key.py	Sun Sep 25 15:06:34 2016 +0200
@@ -30,7 +30,7 @@
 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
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/model/base.py	Sun Sep 25 15:06:34 2016 +0200
@@ -0,0 +1,109 @@
+# -*- coding: utf-8 -*-
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# 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.base
+~~~~~~~~~~~~~~~~~~~~
+
+The application's model objects
+
+This file was forked by the Kallithea project in July 2014.
+Original author and date, and relevant copyright and licensing information is below:
+:created_on: Nov 25, 2010
+: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)
--- a/kallithea/model/changeset_status.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/changeset_status.py	Sun Sep 25 15:06:34 2016 +0200
@@ -28,7 +28,7 @@
 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
 
--- a/kallithea/model/comment.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/comment.py	Sun Sep 25 15:06:34 2016 +0200
@@ -32,7 +32,7 @@
 
 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
--- a/kallithea/model/gist.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/gist.py	Sun Sep 25 15:06:34 2016 +0200
@@ -34,7 +34,7 @@
 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
--- a/kallithea/model/notification.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/notification.py	Sun Sep 25 15:06:34 2016 +0200
@@ -36,7 +36,7 @@
 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
 
--- a/kallithea/model/permission.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/permission.py	Sun Sep 25 15:06:34 2016 +0200
@@ -31,7 +31,7 @@
 
 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
--- a/kallithea/model/pull_request.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/pull_request.py	Sun Sep 25 15:06:34 2016 +0200
@@ -35,7 +35,7 @@
 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
--- a/kallithea/model/repo.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/repo.py	Sun Sep 25 15:06:34 2016 +0200
@@ -41,7 +41,7 @@
 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
--- a/kallithea/model/repo_group.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/repo_group.py	Sun Sep 25 15:06:34 2016 +0200
@@ -34,7 +34,7 @@
 
 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
 
--- a/kallithea/model/repo_permission.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/repo_permission.py	Sun Sep 25 15:06:34 2016 +0200
@@ -24,7 +24,7 @@
 """
 
 import logging
-from kallithea.model import BaseModel
+from kallithea.model.base import BaseModel
 from kallithea.model.db import UserRepoToPerm, UserGroupRepoToPerm, \
     Permission
 
--- a/kallithea/model/scm.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/scm.py	Sun Sep 25 15:06:34 2016 +0200
@@ -53,7 +53,7 @@
     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
--- a/kallithea/model/user.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/user.py	Sun Sep 25 15:06:34 2016 +0200
@@ -39,7 +39,7 @@
 
 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, \
--- a/kallithea/model/user_group.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/model/user_group.py	Sun Sep 25 15:06:34 2016 +0200
@@ -27,7 +27,7 @@
 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
--- a/kallithea/tests/scripts/manual_test_concurrency.py	Sun Sep 25 17:42:50 2016 +0200
+++ b/kallithea/tests/scripts/manual_test_concurrency.py	Sun Sep 25 15:06:34 2016 +0200
@@ -39,7 +39,7 @@
 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