changeset 8671:9685f50a69d0

imports: move more imports to top level
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 11 Oct 2020 12:27:26 +0200
parents bf2286a3fc7e
children 2d4bea1c7898
files kallithea/lib/annotate.py kallithea/lib/auth_modules/__init__.py kallithea/lib/auth_modules/auth_internal.py kallithea/lib/celerylib/tasks.py kallithea/lib/db_manage.py kallithea/lib/helpers.py kallithea/lib/markup_renderer.py kallithea/lib/pygmentsutils.py kallithea/lib/utils2.py kallithea/lib/vcs/backends/base.py kallithea/lib/vcs/nodes.py kallithea/lib/vcs/utils/__init__.py kallithea/lib/vcs/utils/helpers.py
diffstat 13 files changed, 38 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/annotate.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/annotate.py	Sun Oct 11 12:27:26 2020 +0200
@@ -28,6 +28,7 @@
 from pygments import highlight
 from pygments.formatters import HtmlFormatter
 
+from kallithea.lib.pygmentsutils import get_custom_lexer
 from kallithea.lib.vcs.exceptions import VCSError
 from kallithea.lib.vcs.nodes import FileNode
 from kallithea.lib.vcs.utils import safe_str
@@ -48,7 +49,6 @@
     :param headers: dictionary with headers (keys are whats in ``order``
       parameter)
     """
-    from kallithea.lib.pygmentsutils import get_custom_lexer
     options['linenos'] = True
     formatter = AnnotateHtmlFormatter(filenode=filenode,
         annotate_from_changeset_func=annotate_from_changeset_func, order=order,
--- a/kallithea/lib/auth_modules/__init__.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/auth_modules/__init__.py	Sun Oct 11 12:27:26 2020 +0200
@@ -18,10 +18,12 @@
 import importlib
 import logging
 import traceback
+from inspect import isfunction
 
 from kallithea.lib.auth import AuthUser, PasswordGenerator
 from kallithea.lib.compat import hybrid_property
 from kallithea.lib.utils2 import asbool
+from kallithea.model import validators
 from kallithea.model.db import Setting, User
 from kallithea.model.meta import Session
 from kallithea.model.user import UserModel
@@ -38,7 +40,6 @@
         self.kwargs = kwargs
 
     def __call__(self, *args, **kwargs):
-        from inspect import isfunction
         formencode_obj = self.formencode_obj
         if isfunction(formencode_obj):
             # case we wrap validators into functions
@@ -69,8 +70,7 @@
                 self.validator_name = name
 
             def __call__(self, *args, **kwargs):
-                from kallithea.model import validators as v
-                obj = getattr(v, self.validator_name)
+                obj = getattr(validators, self.validator_name)
                 #log.debug('Initializing lazy formencode object: %s', obj)
                 return LazyFormencode(obj, *args, **kwargs)
 
--- a/kallithea/lib/auth_modules/auth_internal.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/auth_modules/auth_internal.py	Sun Oct 11 12:27:26 2020 +0200
@@ -28,7 +28,7 @@
 
 import logging
 
-from kallithea.lib import auth_modules
+from kallithea.lib import auth, auth_modules
 from kallithea.lib.compat import hybrid_property
 
 
@@ -78,7 +78,6 @@
         }
         log.debug('user data: %s', user_data)
 
-        from kallithea.lib import auth
         password_match = auth.check_password(password, userobj.password)
         if userobj.is_default_user:
             log.info('user %s authenticated correctly as anonymous user',
--- a/kallithea/lib/celerylib/tasks.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/celerylib/tasks.py	Sun Oct 11 12:27:26 2020 +0200
@@ -37,14 +37,17 @@
 from tg import config
 
 import kallithea
+from kallithea.config import conf
 from kallithea.lib import celerylib, ext_json
 from kallithea.lib.helpers import person
 from kallithea.lib.hooks import log_create_repository
+from kallithea.lib.indexers.daemon import WhooshIndexingDaemon
 from kallithea.lib.rcmail.smtp_mailer import SmtpMailer
 from kallithea.lib.utils import action_logger
 from kallithea.lib.utils2 import asbool, ascii_bytes
 from kallithea.lib.vcs.utils import author_email
-from kallithea.model.db import RepoGroup, Repository, Statistics, User
+from kallithea.model.db import RepoGroup, Repository, Setting, Statistics, User
+from kallithea.model.repo import RepoModel
 
 
 __all__ = ['whoosh_index', 'get_commits_stats', 'send_email']
@@ -57,7 +60,6 @@
 @celerylib.locked_task
 @celerylib.dbsession
 def whoosh_index(repo_location, full_index):
-    from kallithea.lib.indexers.daemon import WhooshIndexingDaemon
     celerylib.get_session() # initialize database connection
 
     index_location = config['index_dir']
@@ -323,9 +325,6 @@
 @celerylib.task
 @celerylib.dbsession
 def create_repo(form_data, cur_user):
-    from kallithea.model.db import Setting
-    from kallithea.model.repo import RepoModel
-
     DBS = celerylib.get_session()
 
     cur_user = User.guess_instance(cur_user)
@@ -410,8 +409,6 @@
     :param form_data:
     :param cur_user:
     """
-    from kallithea.model.repo import RepoModel
-
     DBS = celerylib.get_session()
 
     base_path = kallithea.CONFIG['base_path']
@@ -480,7 +477,6 @@
 
 
 def __get_codes_stats(repo_name):
-    from kallithea.config.conf import LANGUAGES_EXTENSIONS_MAP
     repo = Repository.get_by_repo_name(repo_name).scm_instance
 
     tip = repo.get_changeset()
@@ -489,7 +485,7 @@
     for _topnode, _dirnodes, filenodes in tip.walk('/'):
         for filenode in filenodes:
             ext = filenode.extension.lower()
-            if ext in LANGUAGES_EXTENSIONS_MAP and not filenode.is_binary:
+            if ext in conf.LANGUAGES_EXTENSIONS_MAP and not filenode.is_binary:
                 if ext in code_stats:
                     code_stats[ext] += 1
                 else:
--- a/kallithea/lib/db_manage.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/db_manage.py	Sun Oct 11 12:27:26 2020 +0200
@@ -36,6 +36,7 @@
 import sqlalchemy
 from sqlalchemy.engine import create_engine
 
+from kallithea.lib.utils2 import ask_ok
 from kallithea.model.base import init_model
 from kallithea.model.db import Repository, Setting, Ui, User
 from kallithea.model.meta import Base, Session
@@ -60,7 +61,6 @@
         force_ask = self.cli_args.get('force_ask')
         if force_ask is not None:
             return force_ask
-        from kallithea.lib.utils2 import ask_ok
         return ask_ok(msg)
 
     def init_db(self, SESSION=None):
--- a/kallithea/lib/helpers.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/helpers.py	Sun Oct 11 12:27:26 2020 +0200
@@ -28,6 +28,7 @@
 from beaker.cache import cache_region
 from pygments import highlight as code_highlight
 from pygments.formatters.html import HtmlFormatter
+from tg import session
 from tg.i18n import ugettext as _
 from webhelpers2.html import HTML, escape, literal
 from webhelpers2.html.tags import NotGiven, Option, Options, _input, _make_safe_id_component, checkbox, end_form
@@ -45,6 +46,7 @@
 # PERMS
 #==============================================================================
 from kallithea.lib.auth import HasPermissionAny, HasRepoGroupPermissionLevel, HasRepoPermissionLevel
+from kallithea.lib.diffs import BIN_FILENODE, CHMOD_FILENODE, DEL_FILENODE, MOD_FILENODE, NEW_FILENODE, RENAMED_FILENODE
 from kallithea.lib.markup_renderer import url_re
 from kallithea.lib.pygmentsutils import get_custom_lexer
 from kallithea.lib.utils2 import MENTIONS_REGEX, AttributeDict
@@ -56,6 +58,8 @@
 # SCM FILTERS available via h.
 #==============================================================================
 from kallithea.lib.vcs.utils import author_email, author_name
+from kallithea.model.changeset_status import ChangesetStatusModel
+from kallithea.model.db import URL_SEP, ChangesetStatus, Permission, PullRequest, User, UserIpMap
 
 
 # mute pyflakes "imported but unused"
@@ -485,7 +489,6 @@
     """Manage a message queue in tg.session: return the current message queue
     after appending the given message, and possibly clearing the queue."""
     key = 'flash'
-    from tg import session
     if key in session:
         flash_messages = session[key]
     else:
@@ -601,7 +604,6 @@
     - or return None if user not found"""
     email = author_email(author)
     if email:
-        from kallithea.model.db import User
         user = User.get_by_email(email)
         if user is not None:
             return getattr(user, show_attr)
@@ -629,8 +631,6 @@
 def person(author, show_attr="username"):
     """Find the user identified by 'author', return one of the users attributes,
     default to the username attribute, None if there is no user"""
-    from kallithea.model.db import User
-
     # if author is already an instance use it for extraction
     if isinstance(author, User):
         return getattr(author, show_attr)
@@ -644,8 +644,6 @@
 
 
 def person_by_id(id_, show_attr="username"):
-    from kallithea.model.db import User
-
     # maybe it's an ID ?
     if str(id_).isdigit() or isinstance(id_, int):
         id_ = int(id_)
@@ -827,7 +825,6 @@
         return group_name
 
     def get_pull_request():
-        from kallithea.model.db import PullRequest
         pull_request_id = action_params
         nice_id = PullRequest.make_nice_id(pull_request_id)
 
@@ -985,8 +982,6 @@
     if email_address == _def:
         return default
 
-    from kallithea.model.db import User
-
     parsed_url = urllib.parse.urlparse(url.current(qualified=True))
     return (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \
                .replace('{email}', email_address) \
@@ -1021,7 +1016,6 @@
 
     :param stats: two element list of added/deleted lines of code
     """
-    from kallithea.lib.diffs import BIN_FILENODE, CHMOD_FILENODE, DEL_FILENODE, MOD_FILENODE, NEW_FILENODE, RENAMED_FILENODE
 
     a, d = stats['added'], stats['deleted']
     width = 100
@@ -1206,7 +1200,6 @@
     """Urlify issue references according to .ini configuration"""
     global _urlify_issues_f
     if _urlify_issues_f is None:
-        from kallithea.model.db import URL_SEP
         assert kallithea.CONFIG['sqlalchemy.url'] # make sure config has been loaded
 
         # Build chain of urlify functions, starting with not doing any transformation
@@ -1317,17 +1310,14 @@
 
 
 def changeset_status(repo, revision):
-    from kallithea.model.changeset_status import ChangesetStatusModel
     return ChangesetStatusModel().get_status(repo, revision)
 
 
 def changeset_status_lbl(changeset_status):
-    from kallithea.model.db import ChangesetStatus
     return ChangesetStatus.get_status_lbl(changeset_status)
 
 
 def get_permission_name(key):
-    from kallithea.model.db import Permission
     return dict(Permission.PERMS).get(key)
 
 
@@ -1359,7 +1349,6 @@
 
 
 def ip_range(ip_addr):
-    from kallithea.model.db import UserIpMap
     s, e = UserIpMap._get_ip_range(ip_addr)
     return '%s - %s' % (s, e)
 
@@ -1368,7 +1357,6 @@
 
 def session_csrf_secret_token():
     """Return (and create) the current session's CSRF protection token."""
-    from tg import session
     if not session_csrf_secret_name in session:
         session[session_csrf_secret_name] = str(random.getrandbits(128))
         session.save()
--- a/kallithea/lib/markup_renderer.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/markup_renderer.py	Sun Oct 11 12:27:26 2020 +0200
@@ -26,12 +26,15 @@
 """
 
 
+import hashlib
 import logging
 import re
 import traceback
 
 import bleach
 import markdown as markdown_mod
+from docutils.core import publish_parts
+from docutils.parsers.rst import directives
 
 from kallithea.lib.utils2 import MENTIONS_REGEX, safe_str
 
@@ -74,13 +77,12 @@
 
         :param text:
         """
-        from hashlib import sha1
 
         # Extract pre blocks.
         extractions = {}
 
         def pre_extraction_callback(matchobj):
-            digest = sha1(matchobj.group(0)).hexdigest()
+            digest = hashlib.sha1(matchobj.group(0)).hexdigest()
             extractions[digest] = matchobj.group(0)
             return "{gfm-extraction-%s}" % digest
         pattern = re.compile(r'<pre>.*?</pre>', re.MULTILINE | re.DOTALL)
@@ -215,8 +217,6 @@
     def rst(cls, source, safe=True):
         source = safe_str(source)
         try:
-            from docutils.core import publish_parts
-            from docutils.parsers.rst import directives
             docutils_settings = dict([(alias, None) for alias in
                                 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES])
 
@@ -231,9 +231,6 @@
                                   settings_overrides=docutils_settings)
 
             return parts['html_title'] + parts["fragment"]
-        except ImportError:
-            log.warning('Install docutils to use this function')
-            return cls.plain(source)
         except Exception:
             log.error(traceback.format_exc())
             if safe:
--- a/kallithea/lib/pygmentsutils.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/pygmentsutils.py	Sun Oct 11 12:27:26 2020 +0200
@@ -29,6 +29,8 @@
 
 from pygments import lexers
 
+import kallithea
+
 
 def get_extension_descriptions():
     """
@@ -72,7 +74,6 @@
     returns a custom lexer if it's defined in the extensions module, or None
     if there's no custom lexer defined
     """
-    import kallithea
     lexer_name = getattr(kallithea.EXTENSIONS, 'EXTRA_LEXERS', {}).get(extension)
     if lexer_name is None:
         return None
--- a/kallithea/lib/utils2.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/utils2.py	Sun Oct 11 12:27:26 2020 +0200
@@ -36,12 +36,17 @@
 import urllib.parse
 
 import urlobject
+from dateutil import relativedelta
+from sqlalchemy.engine import url as sa_url
+from sqlalchemy.exc import ArgumentError
 from tg.i18n import ugettext as _
 from tg.i18n import ungettext
 from tg.support.converters import asbool, aslist
 from webhelpers2.text import collapse, remove_formatting, strip_tags
 
 import kallithea
+from kallithea.lib.vcs.backends.base import BaseRepository, EmptyChangeset
+from kallithea.lib.vcs.exceptions import RepositoryError
 from kallithea.lib.vcs.utils import ascii_bytes, ascii_str, safe_bytes, safe_str  # re-export
 from kallithea.lib.vcs.utils.lazy import LazyProperty
 
@@ -170,7 +175,6 @@
     if future:
         prevdate = prevdate.replace(microsecond=0)
     # Get date parts deltas
-    from dateutil import relativedelta
     for part in order:
         d = relativedelta.relativedelta(now, prevdate)
         deltas[part] = getattr(d, part + 's')
@@ -332,8 +336,6 @@
     :param repo:
     :param rev:
     """
-    from kallithea.lib.vcs.backends.base import BaseRepository, EmptyChangeset
-    from kallithea.lib.vcs.exceptions import RepositoryError
     if not isinstance(repo, BaseRepository):
         raise Exception('You must pass an Repository '
                         'object as first argument got %s' % type(repo))
@@ -395,8 +397,6 @@
 
 
 def obfuscate_url_pw(engine):
-    from sqlalchemy.engine import url as sa_url
-    from sqlalchemy.exc import ArgumentError
     try:
         _url = sa_url.make_url(engine or '')
     except ArgumentError:
--- a/kallithea/lib/vcs/backends/base.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/vcs/backends/base.py	Sun Oct 11 12:27:26 2020 +0200
@@ -12,6 +12,7 @@
 import datetime
 import itertools
 
+from kallithea.lib.vcs.backends import get_backend
 from kallithea.lib.vcs.conf import settings
 from kallithea.lib.vcs.exceptions import (ChangesetError, EmptyRepositoryError, NodeAlreadyAddedError, NodeAlreadyChangedError, NodeAlreadyExistsError,
                                           NodeAlreadyRemovedError, NodeDoesNotExistError, NodeNotChangedError, RepositoryError)
@@ -1007,12 +1008,10 @@
 
     @LazyProperty
     def branch(self):
-        from kallithea.lib.vcs.backends import get_backend
         return get_backend(self.alias).DEFAULT_BRANCH_NAME
 
     @LazyProperty
     def branches(self):
-        from kallithea.lib.vcs.backends import get_backend
         return [get_backend(self.alias).DEFAULT_BRANCH_NAME]
 
     @LazyProperty
--- a/kallithea/lib/vcs/nodes.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/vcs/nodes.py	Sun Oct 11 12:27:26 2020 +0200
@@ -14,6 +14,8 @@
 import posixpath
 import stat
 
+from pygments import lexers
+
 from kallithea.lib.vcs.backends.base import EmptyChangeset
 from kallithea.lib.vcs.exceptions import NodeError, RemovedFileNodeError
 from kallithea.lib.vcs.utils import safe_bytes, safe_str
@@ -305,7 +307,6 @@
                 encoding = None
 
                 # try with pygments
-                from pygments import lexers
                 try:
                     mt = lexers.get_lexer_for_filename(self.name).mimetypes
                 except lexers.ClassNotFound:
@@ -335,7 +336,6 @@
         Returns pygment's lexer class. Would try to guess lexer taking file's
         content, name and mimetype.
         """
-        from pygments import lexers
         try:
             lexer = lexers.guess_lexer_for_filename(self.name, safe_str(self.content), stripnl=False)
         except lexers.ClassNotFound:
--- a/kallithea/lib/vcs/utils/__init__.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/vcs/utils/__init__.py	Sun Oct 11 12:27:26 2020 +0200
@@ -9,6 +9,10 @@
 import re
 import time
 
+import chardet
+
+from kallithea.lib.vcs.conf import settings
+
 
 def makedate():
     lt = time.localtime()
@@ -81,7 +85,6 @@
     if not isinstance(s, bytes):  # use __str__ and don't expect UnicodeDecodeError
         return str(s)
 
-    from kallithea.lib.vcs.conf import settings
     for enc in settings.DEFAULT_ENCODINGS:
         try:
             return str(s, enc)
@@ -89,11 +92,10 @@
             pass
 
     try:
-        import chardet
         encoding = chardet.detect(s)['encoding']
         if encoding is not None:
             return s.decode(encoding)
-    except (ImportError, UnicodeDecodeError):
+    except UnicodeDecodeError:
         pass
 
     return str(s, settings.DEFAULT_ENCODINGS[0], 'replace')
@@ -110,7 +112,6 @@
 
     assert isinstance(s, str), repr(s)  # bytes cannot coerse with __str__ or handle None or int
 
-    from kallithea.lib.vcs.conf import settings
     for enc in settings.DEFAULT_ENCODINGS:
         try:
             return s.encode(enc)
--- a/kallithea/lib/vcs/utils/helpers.py	Sun Oct 11 17:10:38 2020 +0200
+++ b/kallithea/lib/vcs/utils/helpers.py	Sun Oct 11 12:27:26 2020 +0200
@@ -3,12 +3,16 @@
 """
 
 import datetime
+import logging
 import os
 import re
 import time
 import urllib.request
 
 import mercurial.url
+from pygments import highlight
+from pygments.formatters import TerminalFormatter
+from pygments.lexers import ClassNotFound, guess_lexer_for_filename
 
 from kallithea.lib.vcs.exceptions import RepositoryError, VCSError
 from kallithea.lib.vcs.utils import safe_str
@@ -105,16 +109,6 @@
     then returned output is colored. Otherwise
     unchanged content is returned.
     """
-    import logging
-    try:
-        import pygments
-        pygments
-    except ImportError:
-        return code
-    from pygments import highlight
-    from pygments.formatters import TerminalFormatter
-    from pygments.lexers import ClassNotFound, guess_lexer_for_filename
-
     try:
         lexer = guess_lexer_for_filename(name, code)
         formatter = TerminalFormatter()