<commit_before># -*- coding: utf-8 -*-
import os
import locale
import logging

from os.path import isabs


logger = logging.getLogger(__name__)


DEFAULT_THEME = os.sep.join([os.path.dirname(os.path.abspath(__file__)),
                              "themes/notmyidea"])
_DEFAULT_CONFIG = {'PATH': '.',
                   'ARTICLE_DIR': '',
                   'ARTICLE_EXCLUDES': ('pages',),
                   'PAGE_DIR': 'pages',
                   'PAGE_EXCLUDES': (),
                   'THEME': DEFAULT_THEME,
                   'OUTPUT_PATH': 'output/',
                   'MARKUP': ('rst', 'md'),
                   'STATIC_PATHS': ['images', ],
                   'THEME_STATIC_PATHS': ['static', ],
                   'FEED_ATOM': 'feeds/all.atom.xml',
                   'CATEGORY_FEED_ATOM': 'feeds/%s.atom.xml',
                   'TRANSLATION_FEED': 'feeds/all-%s.atom.xml',
                   'FEED_MAX_ITEMS': '',
                   'SITEURL': '',
                   'SITENAME': 'A Pelican Blog',
                   'DISPLAY_PAGES_ON_MENU': True,
                   'PDF_GENERATOR': False,
                   'DEFAULT_CATEGORY': 'misc',
                   'DEFAULT_DATE': 'fs',
                   'WITH_FUTURE_DATES': True,
                   'CSS_FILE': 'main.css',
                   'NEWEST_FIRST_ARCHIVES': True,
                   'REVERSE_CATEGORY_ORDER': False,
                   'DELETE_OUTPUT_DIRECTORY': False,
                   'ARTICLE_URL': '{slug}.html',
                   'ARTICLE_SAVE_AS': '{slug}.html',
                   'ARTICLE_LANG_URL': '{slug}-{lang}.html',
                   'ARTICLE_LANG_SAVE_AS': '{slug}-{lang}.html',
                   'PAGE_URL': 'pages/{slug}.html',
                   'PAGE_SAVE_AS': 'pages/{slug}.html',
                   'PAGE_LANG_URL': 'pages/{slug}-{lang}.html',
                   'PAGE_LANG_SAVE_AS': 'pages/{slug}-{lang}.html',
                   'CATEGORY_URL': 'category/{slug}.html',
                   'CATEGORY_SAVE_AS': 'category/{slug}.html',
                   'TAG_URL': 'tag/{slug}.html',
                   'TAG_SAVE_AS': 'tag/{slug}.html',
                   'AUTHOR_URL': u'author/{slug}.html',
                   'AUTHOR_SAVE_AS': u'author/{slug}.html',
                   'RELATIVE_URLS': True,
                   'DEFAULT_LANG': 'en',
                   'TAG_CLOUD_STEPS': 4,
                   'TAG_CLOUD_MAX_ITEMS': 100,
                   'DIRECT_TEMPLATES': ('index', 'tags', 'categories', 'archives'),
                   'PAGINATED_DIRECT_TEMPLATES': ('index', ),
                   'PELICAN_CLASS': 'pelican.Pelican',
                   'DEFAULT_DATE_FORMAT': '%a %d %B %Y',
                   'DATE_FORMATS': {},
                   'JINJA_EXTENSIONS': [],
                   'LOCALE': '',  # default to user locale
                   'DEFAULT_PAGINATION': False,
                   'DEFAULT_ORPHANS': 0,
                   'DEFAULT_METADATA': (),
                   'FILES_TO_COPY': (),
                   'DEFAULT_STATUS': 'published',
                   'ARTICLE_PERMALINK_STRUCTURE': '',
                   'TYPOGRIFY': False,
                   'LESS_GENERATOR': False,
                   'SUMMARY_MAX_LENGTH': 50,
                   'WEBASSETS': False,
                   'PLUGINS': [],
                   }


def read_settings(filename=None):
    if filename:
        local_settings = get_settings_from_file(filename)
    else:
        local_settings = _DEFAULT_CONFIG
    configured_settings = configure_settings(local_settings, None, filename)
    return configured_settings


def get_settings_from_file(filename, default_settings=None):
    """Load a Python file into a dictionary.
    """
    if default_settings == None:
        default_settings = _DEFAULT_CONFIG
    context = default_settings.copy()
    if filename:
        tempdict = {}
        execfile(filename, tempdict)
        for key in tempdict:
            if key.isupper():
                context[key] = tempdict[key]
    return context


def configure_settings(settings, default_settings=None, filename=None):
    """Provide optimizations, error checking, and warnings for loaded settings"""
    if default_settings is None:
        default_settings = _DEFAULT_CONFIG

    # Make the paths relative to the settings file
    if filename:
        for path in ['PATH', 'OUTPUT_PATH']:
            if path in settings:
                if settings[path] is not None and not isabs(settings[path]):
                    settings[path] = os.path.abspath(os.path.normpath(
                        os.path.join(os.path.dirname(filename), settings[path]))
                    )

    # if locales is not a list, make it one
    locales = settings['LOCALE']

    if isinstance(locales, basestring):<commit_msg>Reimplement settings loading to preserve __file__<commit_after># -*- coding: utf-8 -*-
import imp
import inspect
import os
import locale
import logging

from os.path import isabs


logger = logging.getLogger(__name__)


DEFAULT_THEME = os.sep.join([os.path.dirname(os.path.abspath(__file__)),
                              "themes/notmyidea"])
_DEFAULT_CONFIG = {'PATH': '.',
                   'ARTICLE_DIR': '',
                   'ARTICLE_EXCLUDES': ('pages',),
                   'PAGE_DIR': 'pages',
                   'PAGE_EXCLUDES': (),
                   'THEME': DEFAULT_THEME,
                   'OUTPUT_PATH': 'output/',
                   'MARKUP': ('rst', 'md'),
                   'STATIC_PATHS': ['images', ],
                   'THEME_STATIC_PATHS': ['static', ],
                   'FEED_ATOM': 'feeds/all.atom.xml',
                   'CATEGORY_FEED_ATOM': 'feeds/%s.atom.xml',
                   'TRANSLATION_FEED': 'feeds/all-%s.atom.xml',
                   'FEED_MAX_ITEMS': '',
                   'SITEURL': '',
                   'SITENAME': 'A Pelican Blog',
                   'DISPLAY_PAGES_ON_MENU': True,
                   'PDF_GENERATOR': False,
                   'DEFAULT_CATEGORY': 'misc',
                   'DEFAULT_DATE': 'fs',
                   'WITH_FUTURE_DATES': True,
                   'CSS_FILE': 'main.css',
                   'NEWEST_FIRST_ARCHIVES': True,
                   'REVERSE_CATEGORY_ORDER': False,
                   'DELETE_OUTPUT_DIRECTORY': False,
                   'ARTICLE_URL': '{slug}.html',
                   'ARTICLE_SAVE_AS': '{slug}.html',
                   'ARTICLE_LANG_URL': '{slug}-{lang}.html',
                   'ARTICLE_LANG_SAVE_AS': '{slug}-{lang}.html',
                   'PAGE_URL': 'pages/{slug}.html',
                   'PAGE_SAVE_AS': 'pages/{slug}.html',
                   'PAGE_LANG_URL': 'pages/{slug}-{lang}.html',
                   'PAGE_LANG_SAVE_AS': 'pages/{slug}-{lang}.html',
                   'CATEGORY_URL': 'category/{slug}.html',
                   'CATEGORY_SAVE_AS': 'category/{slug}.html',
                   'TAG_URL': 'tag/{slug}.html',
                   'TAG_SAVE_AS': 'tag/{slug}.html',
                   'AUTHOR_URL': u'author/{slug}.html',
                   'AUTHOR_SAVE_AS': u'author/{slug}.html',
                   'RELATIVE_URLS': True,
                   'DEFAULT_LANG': 'en',
                   'TAG_CLOUD_STEPS': 4,
                   'TAG_CLOUD_MAX_ITEMS': 100,
                   'DIRECT_TEMPLATES': ('index', 'tags', 'categories', 'archives'),
                   'PAGINATED_DIRECT_TEMPLATES': ('index', ),
                   'PELICAN_CLASS': 'pelican.Pelican',
                   'DEFAULT_DATE_FORMAT': '%a %d %B %Y',
                   'DATE_FORMATS': {},
                   'JINJA_EXTENSIONS': [],
                   'LOCALE': '',  # default to user locale
                   'DEFAULT_PAGINATION': False,
                   'DEFAULT_ORPHANS': 0,
                   'DEFAULT_METADATA': (),
                   'FILES_TO_COPY': (),
                   'DEFAULT_STATUS': 'published',
                   'ARTICLE_PERMALINK_STRUCTURE': '',
                   'TYPOGRIFY': False,
                   'LESS_GENERATOR': False,
                   'SUMMARY_MAX_LENGTH': 50,
                   'WEBASSETS': False,
                   'PLUGINS': [],
                   }


def read_settings(filename=None):
    if filename:
        local_settings = get_settings_from_file(filename)
    else:
        local_settings = _DEFAULT_CONFIG
    configured_settings = configure_settings(local_settings, None, filename)
    return configured_settings


def get_settings_from_module(module=None, default_settings=_DEFAULT_CONFIG):
    """
    Load settings from a module, returning a dict.

    """

    context = default_settings.copy()
    if module is not None:
         context.update(
             (k, v) for k, v in inspect.getmembers(module) if k.isupper()
         )
    return context


def get_settings_from_file(filename, default_settings=_DEFAULT_CONFIG):
    """
    Load settings from a file path, returning a dict.

    """

    name = os.path.basename(filename).rpartition(".")[0]
    module = imp.load_source(name, filename)
    return get_settings_from_module(module, default_settings=default_settings)


def configure_settings(settings, default_settings=None, filename=None):
    """Provide optimizations, error checking, and warnings for loaded settings"""
    if default_settings is None:
        default_settings = _DEFAULT_CONFIG

    # Make the paths relative to the settings file
    if filename:
        for path in ['PATH', 'OUTPUT_PATH']:
            if path in settings:
                if settings[path] is not None and not isabs(settings[path]):
                    settings[path] = os.path.abspath(os.path.normpath(
                        os.path.join(os.path.dirname(filename), settings[path]))
                    )

    # if locales is not a list, make it one
    locales = settings['LOCALE']

    if isinstance(locales, basestring):
{'fraction_non_alphanumeric': 0.1261682242990654, 'fraction_numerical': 0.001726940268183665, 'mean_word_length': 1.6869541484716157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '4077067', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2778, 'n_words': 532}