Changeset - 9685f50a69d0
[Not reviewed]
kallithea/lib/annotate.py
Show inline comments
 
@@ -28,6 +28,7 @@ Original author and date, and relevant c
 
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 @@ def annotate_highlight(filenode, annotat
 
    :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,
kallithea/lib/auth_modules/__init__.py
Show inline comments
 
@@ -18,10 +18,12 @@ Authentication modules
 
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 @@ class LazyFormencode(object):
 
        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 @@ class KallitheaAuthPluginBase(object):
 
                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)
 

	
kallithea/lib/auth_modules/auth_internal.py
Show inline comments
 
@@ -28,7 +28,7 @@ Original author and date, and relevant c
 

	
 
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 @@ class KallitheaAuthPlugin(auth_modules.K
 
        }
 
        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',
kallithea/lib/celerylib/tasks.py
Show inline comments
 
@@ -37,14 +37,17 @@ import celery.utils.log
 
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 @@ log = celery.utils.log.get_task_logger(_
 
@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 @@ def send_email(recipients, subject, body
 
@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 @@ def create_repo_fork(form_data, cur_user
 
    :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 create_repo_fork(form_data, cur_user
 

	
 

	
 
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 @@ def __get_codes_stats(repo_name):
 
    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:
kallithea/lib/db_manage.py
Show inline comments
 
@@ -36,6 +36,7 @@ import alembic.config
 
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 @@ class DbManage(object):
 
        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):
kallithea/lib/helpers.py
Show inline comments
 
@@ -28,6 +28,7 @@ import urllib.parse
 
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 @@ from kallithea.lib.annotate import annot
 
# 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 @@ from kallithea.lib.vcs.exceptions import
 
# 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 @@ def _session_flash_messages(append=None,
 
    """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 @@ def user_attr_or_none(author, show_attr)
 
    - 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 email_or_none(author):
 
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(author, show_attr="username")
 

	
 

	
 
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 @@ def action_parser(user_log, feed=False, 
 
        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 @@ def gravatar_url(email_address, size=30,
 
    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 @@ def fancy_file_stats(stats):
 

	
 
    :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 @@ def urlify_issues(newtext, repo_name):
 
    """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 link_to_ref(repo_name, ref_type, ref
 

	
 

	
 
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 not_mapped_error(repo_name):
 

	
 

	
 
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 @@ session_csrf_secret_name = "_session_csr
 

	
 
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()
kallithea/lib/markup_renderer.py
Show inline comments
 
@@ -26,12 +26,15 @@ Original author and date, and relevant c
 
"""
 

	
 

	
 
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 @@ class MarkupRenderer(object):
 

	
 
        :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 @@ class MarkupRenderer(object):
 
    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 @@ class MarkupRenderer(object):
 
                                  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:
kallithea/lib/pygmentsutils.py
Show inline comments
 
@@ -29,6 +29,8 @@ from collections import defaultdict
 

	
 
from pygments import lexers
 

	
 
import kallithea
 

	
 

	
 
def get_extension_descriptions():
 
    """
 
@@ -72,7 +74,6 @@ def get_custom_lexer(extension):
 
    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
kallithea/lib/utils2.py
Show inline comments
 
@@ -36,12 +36,17 @@ import time
 
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 @@ def age(prevdate, show_short_version=Fal
 
    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 @@ def get_changeset_safe(repo, rev):
 
    :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 @@ class AttributeDict(dict):
 

	
 

	
 
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:
kallithea/lib/vcs/backends/base.py
Show inline comments
 
@@ -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 @@ class EmptyChangeset(BaseChangeset):
 

	
 
    @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
kallithea/lib/vcs/nodes.py
Show inline comments
 
@@ -14,6 +14,8 @@ import mimetypes
 
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 @@ class FileNode(Node):
 
                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 @@ class FileNode(Node):
 
        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:
kallithea/lib/vcs/utils/__init__.py
Show inline comments
 
@@ -9,6 +9,10 @@ import datetime
 
import re
 
import time
 

	
 
import chardet
 

	
 
from kallithea.lib.vcs.conf import settings
 

	
 

	
 
def makedate():
 
    lt = time.localtime()
 
@@ -81,7 +85,6 @@ def safe_str(s):
 
    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 @@ def safe_str(s):
 
            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 @@ def safe_bytes(s):
 

	
 
    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)
kallithea/lib/vcs/utils/helpers.py
Show inline comments
 
@@ -3,12 +3,16 @@ Utilities aimed to help achieve mostly b
 
"""
 

	
 
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 @@ def get_highlighted_code(name, code, typ
 
    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()
0 comments (0 inline, 0 general)