<commit_before># -*- coding: utf-8 -*-

from flask import (Flask, request, json, render_template, send_file,
                   url_for, redirect, jsonify)
from werkzeug.contrib.cache import SimpleCache

import itertools
import os
import hashlib
import orgparse


app = Flask('orgviz')
app.config.from_object('orgviz.default_config')
cache = SimpleCache(default_timeout=60 * 60 * 24)


def get_orgnodes(filename):
    """Cached version of orgparse.makelist"""
    cachename_orgnodes = u'org:{0}'.format(filename)         # file path
    cachename_lastmtime = u'org_mtime:{0}'.format(filename)  # modified time
    orgnodes = cache.get(cachename_orgnodes)
    lastmtime = cache.get(cachename_lastmtime)
    mtime = os.path.getmtime(filename)
    if (not app.config['ORG_USE_CACHE'] or
        None in (orgnodes, lastmtime) or
        mtime > lastmtime):
        app.logger.debug("re-load org file '{0}'".format(filename))
        orgnodes = list(orgparse.load(filename)[1:])
        if app.config['ORG_USE_CACHE']:
            try:
                cache.set(cachename_orgnodes, orgnodes)
                cache.set(cachename_lastmtime, mtime)
            except RuntimeError as e:
                app.logger.error(
                    'Error while loading {0}.  Probably it is too big.\n'
                    'Got: {1}'.format(filename, e))
    else:
        app.logger.debug("use cache for org file '{0}'".format(filename))
    return orgnodes


def args_to_str(*args, **kwds):
    """
    Convert arguments and keyword arguments to readable string.

    >>> args_to_str(1, 2)
    '1, 2'
    >>> args_to_str(1, 2, a=3)
    '1, 2, a=3'
    >>> args_to_str(a=3)
    'a=3'

    """
    str_args = ', '.join(map(repr, args))
    str_kwds = ', '.join(map(
        lambda x: '{0}={1}'.format(x[0], repr(x[1])),
        kwds.iteritems()))
    return ', '.join(a for a in [str_args, str_kwds] if a)


def older_than(target_path, compare_list, getmtime=os.path.getmtime):
    """
    Which is old: target_path or an element (a path) in compare_list?

    It returns iterative to compare modified time of target_path and
    each path in compare_list.<commit_msg>Make cache function more generic<commit_after># -*- coding: utf-8 -*-

from flask import (Flask, request, json, render_template, send_file,
                   url_for, redirect, jsonify)
from werkzeug.contrib.cache import SimpleCache

import itertools
import os
import hashlib
import orgparse


app = Flask('orgviz')
app.config.from_object('orgviz.default_config')
cache = SimpleCache(default_timeout=60 * 60 * 24)


def get_cache(cachename, filename, compute, getmtime=os.path.getmtime):
    cachename_value = u'{0}:{1}'.format(cachename, filename)
    cachename_lastmtime = u'{0}_mtime:{1}'.format(cachename, filename)
    value = cache.get(cachename_value)
    lastmtime = cache.get(cachename_lastmtime)
    mtime = getmtime(filename)
    if (not app.config['ORG_USE_CACHE'] or
        None in (value, lastmtime) or
        mtime > lastmtime):
        app.logger.debug("re-compute from '{0}'".format(filename))
        value = compute(filename)
        if app.config['ORG_USE_CACHE']:
            try:
                cache.set(cachename_value, value)
                cache.set(cachename_lastmtime, mtime)
            except RuntimeError as e:
                app.logger.error(
                    'Error while loading {0}.  Probably it is too big.\n'
                    'Got: {1}'.format(filename, e))
    else:
        app.logger.debug("use cache for file '{0}'".format(filename))
    return value


def get_orgnodes(filename):
    """Cached version of orgparse.makelist"""
    return get_cache('org', filename, lambda x: list(orgparse.load(x)[1:]))


def args_to_str(*args, **kwds):
    """
    Convert arguments and keyword arguments to readable string.

    >>> args_to_str(1, 2)
    '1, 2'
    >>> args_to_str(1, 2, a=3)
    '1, 2, a=3'
    >>> args_to_str(a=3)
    'a=3'

    """
    str_args = ', '.join(map(repr, args))
    str_kwds = ', '.join(map(
        lambda x: '{0}={1}'.format(x[0], repr(x[1])),
        kwds.iteritems()))
    return ', '.join(a for a in [str_args, str_kwds] if a)


def older_than(target_path, compare_list, getmtime=os.path.getmtime):
    """
    Which is old: target_path or an element (a path) in compare_list?

    It returns iterative to compare modified time of target_path and
    each path in compare_list.
{'fraction_non_alphanumeric': 0.13166855845629966, 'fraction_numerical': 0.014074914869466515, 'mean_word_length': 2.9025686448184236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '2301960', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1484, 'n_words': 358}