<commit_before>import ast

from funcy import partial, cat, ikeep, project

from ..asttools import ast_eval, is_name
from . import register_global_usage


def register(args, kwargs):
    # settings_module = kwargs['settings']
    # extra_urlconf = kwargs['urlconf']
    register_global_usage(partial(global_usage, opts=project(kwargs, ['settings', 'urlconf'])))


def global_usage(files, used, opts={}):
    settings = files.get(opts.get('settings'))

    # Mark all settings as used
    if settings:
        for name, nodes in settings.scope.names.items():
            node = nodes[0]
            if is_assign(node) and node.id.isupper():
                used[settings.dotname].add(name)

    mark_used_views(files, used, settings, opts=opts)


def mark_used_views(files, used, settings, opts={}):
    urlconfs = []

    # Get root urlconf from settings, the check is needed in case
    if settings:
        root_urlconf = get_name_val(settings.scope.names['ROOT_URLCONF'][0])
        if root_urlconf in files:
            urlconfs.append(root_urlconf)
    # Get urlconf from options
    if opts.get('urlconf'):
        urlconfs.append(opts['urlconf'])

    # TODO: warn about no urlconf

    for urlconf in urlconfs:
        used[urlconf].add('urlpatterns')

        views = _parse_urlconf(files, files[urlconf])
        views = [v.rsplit('.', 1) for v in views if '.' in v]
        for module, view in views:
            used[module].add(view)


def _parse_urlconf(files, urlconf):
    is_patterns = lambda node: isinstance(node, ast.Call) and is_name(node.func, 'patterns')
    patterns = filter(is_patterns, ast.walk(urlconf.tree))<commit_msg>Refactor django global usage a bit<commit_after>import ast

from funcy import partial, cat, ikeep, project

from ..asttools import ast_eval, is_name
from . import register_global_usage


def register(args, kwargs):
    # settings_module = kwargs['settings']
    # extra_urlconf = kwargs['urlconf']
    register_global_usage(partial(global_usage, opts=project(kwargs, ['settings', 'urlconf'])))


def global_usage(files, used, opts={}):
    mark_used_settings(files, used, opts=opts)
    mark_used_views(files, used, opts=opts)


def mark_used_settings(files, used, opts={}):
    settings = files.get(opts.get('settings'))

    if settings:
        for name, nodes in settings.scope.names.items():
            node = nodes[0]
            if is_assign(node) and node.id.isupper():
                used[settings.dotname].add(name)


def mark_used_views(files, used, opts={}):
    settings = files.get(opts.get('settings'))
    urlconfs = []

    # Get root urlconf from settings, the check is needed in case
    if settings:
        root_urlconf = get_name_val(settings.scope.names['ROOT_URLCONF'][0])
        if root_urlconf in files:
            urlconfs.append(root_urlconf)
    # Get urlconf from options
    if opts.get('urlconf'):
        urlconfs.append(opts['urlconf'])

    # TODO: warn about no urlconf

    for urlconf in urlconfs:
        used[urlconf].add('urlpatterns')

        views = _parse_urlconf(files, files[urlconf])
        views = [v.rsplit('.', 1) for v in views if '.' in v]
        for module, view in views:
            used[module].add(view)


def _parse_urlconf(files, urlconf):
    is_patterns = lambda node: isinstance(node, ast.Call) and is_name(node.func, 'patterns')
    patterns = filter(is_patterns, ast.walk(urlconf.tree))
{'fraction_non_alphanumeric': 0.11956521739130435, 'fraction_numerical': 0.0017626321974148062, 'mean_word_length': 3.337579617834395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2598282', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1061, 'n_words': 272}