<commit_before># -*- coding: utf-8 -*-
"""
Miscellaneous utilities and tools
"""
from __future__ import absolute_import, print_function

import functools
import keyword
import os
import re
import shutil
import sys
from contextlib import contextmanager
from operator import itemgetter

from .exceptions import InvalidIdentifier, OldSetuptools
from .log import logger
from .templates import licenses
from .contrib.six import PY2
from .contrib.setuptools_scm.version import VERSION_CLASS


@contextmanager
def chdir(path, **kwargs):
    """Contextmanager to change into a directory

    Args:
        path (str): path to change current working directory to

    Keyword Args:
        log (bool): log activity when true. Default: ``False``.
        pretend (bool): skip execution (but log) when pretending.
            Default ``False``.
    """
    should_pretend = kwargs.get('pretend')
    should_log = kwargs.get('log', should_pretend)
    # ^ When pretending, automatically output logs
    #   (after all, this is the primary purpose of pretending)

    curr_dir = os.getcwd()
    if not should_pretend:
        os.chdir(path)

    try:
        if should_log:
            logger.report('chdir', path)
            with logger.indent():
                yield
        else:
            yield
    finally:
        os.chdir(curr_dir)


def move(*src, **kwargs):
    """Move files or directories to (into) a new location

    Args:
        *src (str[]): one or more files/directories to be moved

    Keyword Args:
        target (str): if target is a directory, ``src`` will be moved inside
            it. Otherwise, it will be the new path (note that it may be
            overwritten)
        log (bool): log activity when true. Default: ``False``.
        pretend (bool): skip execution (but log) when pretending.
            Default ``False``.
    """
    target = kwargs['target']  # Required arg
    should_pretend = kwargs.get('pretend')
    should_log = kwargs.get('log', should_pretend)
    # ^ When pretending, automatically output logs
    #   (after all, this is the primary purpose of pretending)

    for path in src:
        if not should_pretend:
            shutil.move(path, target)
        if should_log:
            logger.report('move', path, target=target)
<commit_msg>Fix chdir logging order<commit_after># -*- coding: utf-8 -*-
"""
Miscellaneous utilities and tools
"""
from __future__ import absolute_import, print_function

import functools
import keyword
import os
import re
import shutil
import sys
from contextlib import contextmanager
from operator import itemgetter

from .exceptions import InvalidIdentifier, OldSetuptools
from .log import logger
from .templates import licenses
from .contrib.six import PY2
from .contrib.setuptools_scm.version import VERSION_CLASS


@contextmanager
def _chdir_logginng_context(path, should_log):
    """Private auxiliar function for logging inside chdir"""
    if should_log:
        logger.report('chdir', path)
        with logger.indent():
            yield
    else:
        yield


@contextmanager
def chdir(path, **kwargs):
    """Contextmanager to change into a directory

    Args:
        path (str): path to change current working directory to

    Keyword Args:
        log (bool): log activity when true. Default: ``False``.
        pretend (bool): skip execution (but log) when pretending.
            Default ``False``.
    """
    should_pretend = kwargs.get('pretend')
    should_log = kwargs.get('log', should_pretend)
    # ^ When pretending, automatically output logs
    #   (after all, this is the primary purpose of pretending)

    curr_dir = os.getcwd()

    try:
        with _chdir_logginng_context(path, should_log):
            if not should_pretend:
                os.chdir(path)
            yield
    finally:
        os.chdir(curr_dir)


def move(*src, **kwargs):
    """Move files or directories to (into) a new location

    Args:
        *src (str[]): one or more files/directories to be moved

    Keyword Args:
        target (str): if target is a directory, ``src`` will be moved inside
            it. Otherwise, it will be the new path (note that it may be
            overwritten)
        log (bool): log activity when true. Default: ``False``.
        pretend (bool): skip execution (but log) when pretending.
            Default ``False``.
    """
    target = kwargs['target']  # Required arg
    should_pretend = kwargs.get('pretend')
    should_log = kwargs.get('log', should_pretend)
    # ^ When pretending, automatically output logs
    #   (after all, this is the primary purpose of pretending)

    for path in src:
        if not should_pretend:
            shutil.move(path, target)
        if should_log:
            logger.report('move', path, target=target)

{'fraction_non_alphanumeric': 0.09104227710339054, 'fraction_numerical': 0.0008371703641691084, 'mean_word_length': 2.730679156908665, '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': '3708107', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1314, 'n_words': 488}