<commit_before># -*- coding: utf-8 -*-
##
## This file is part of Invenio.
## Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 CERN.
##
## Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

__revision__ = "$Id$"

import os
import time
import shutil
from tempfile import NamedTemporaryFile

from invenio.config import (CFG_SITE_URL,
                            CFG_SITE_LANG,
                            CFG_WEBDIR,
                            CFG_BIBRANK_SHOW_CITATION_GRAPHS)
from invenio.messages import gettext_set_language
from invenio.bibrank_grapher import (create_temporary_image,
                                     write_coordinates_in_tmp_file)
from invenio.bibrank_citation_searcher import get_cited_by
from invenio.search_engine_utils import get_fieldvalues
from invenio.dateutils import strptime


CFG_BIBRANK_PRINT_CITATION_HISTORY = 1
# Do not generate graphs that have less than three points
CFG_BIBRANK_CITATION_HISTORY_MIN_X_POINTS = 2

REL_PATH = 'img/citation-graphs'
BASE_DIR = os.path.join(CFG_WEBDIR, os.path.join(*REL_PATH.split('/')))


def calculate_citation_graphe_x_coordinates(recid):
    """Return a range of year from the publication year of record RECID
       until the current year."""
    record_date = get_fieldvalues(recid, '269__c')
    if not record_date:
        record_date = get_fieldvalues(recid, '773__y')
        if not record_date:
            record_date = get_fieldvalues(recid, '260__c')

    currentyear = time.localtime()[0]

    recordyear = None
    if record_date:
        recordyear = find_year(record_date[0])
    if not recordyear:
        recordyear = currentyear

    return range(recordyear, currentyear+1)


def calculate_citation_history_coordinates(recid):
    """Return a list of citation graph coordinates for RECID, sorted by year."""
    result = {}
    for year in calculate_citation_graphe_x_coordinates(recid):
        result[year] = 0

    if len(result) < CFG_BIBRANK_CITATION_HISTORY_MIN_X_POINTS:
        # do not generate graphs that have less than X points
        return []

    for recid in get_cited_by(recid):
        rec_date = get_fieldvalues(recid, '269__c')
        if not rec_date:
            rec_date = get_fieldvalues(recid, '773__y')
            if not rec_date:
                rec_date = get_fieldvalues(recid, '260__c')
        # Some records simlpy do not have these fields
        if rec_date:
            # Maybe rec_date[0][0:4] has a typo and cannot
            # be converted to an int
            try:
                d = strptime(rec_date[0], '%Y-%m')
            except ValueError:
                pass
            else:
                if d.year in result:
                    result[d.year] += 1

    return sorted(result.iteritems())


def find_year(recordyear):
    """find the year in the string as a suite of 4 int"""
    year = None
    for i in range(len(recordyear)-3):
        s = recordyear[i:i+4]
        if s.isalnum():
            year = int(s)
            break
    return year


def html_command(file):
    t = ''
    if CFG_BIBRANK_SHOW_CITATION_GRAPHS == 1:
        t = '<img src="%s/%s/%s" align="center" alt="Citation Graph">' \
                                           % (CFG_SITE_URL, REL_PATH, filename)
    elif CFG_BIBRANK_SHOW_CITATION_GRAPHS == 2:
        t = open(os.path.join(BASE_DIR, filename)).read()
    return t


def remove_old_graph_if_needed(filename):
    """Delete graph if it is older than x seconds"""
    if not os.path.isfile(filename):
        return True

    time_diff = time.time() - os.stat(filename).st_mtime
    if time_diff > 3600*24:
        try:
            os.unlink(filename)
        except OSError, e:
            # File does not exist is ok<commit_msg>BibRank: fix bad variable name<commit_after># -*- coding: utf-8 -*-
##
## This file is part of Invenio.
## Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 CERN.
##
## Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

__revision__ = "$Id$"

import os
import time
import shutil
from tempfile import NamedTemporaryFile

from invenio.config import (CFG_SITE_URL,
                            CFG_SITE_LANG,
                            CFG_WEBDIR,
                            CFG_BIBRANK_SHOW_CITATION_GRAPHS)
from invenio.messages import gettext_set_language
from invenio.bibrank_grapher import (create_temporary_image,
                                     write_coordinates_in_tmp_file)
from invenio.bibrank_citation_searcher import get_cited_by
from invenio.search_engine_utils import get_fieldvalues
from invenio.dateutils import strptime


CFG_BIBRANK_PRINT_CITATION_HISTORY = 1
# Do not generate graphs that have less than three points
CFG_BIBRANK_CITATION_HISTORY_MIN_X_POINTS = 2

REL_PATH = 'img/citation-graphs'
BASE_DIR = os.path.join(CFG_WEBDIR, os.path.join(*REL_PATH.split('/')))


def calculate_citation_graphe_x_coordinates(recid):
    """Return a range of year from the publication year of record RECID
       until the current year."""
    record_date = get_fieldvalues(recid, '269__c')
    if not record_date:
        record_date = get_fieldvalues(recid, '773__y')
        if not record_date:
            record_date = get_fieldvalues(recid, '260__c')

    currentyear = time.localtime()[0]

    recordyear = None
    if record_date:
        recordyear = find_year(record_date[0])
    if not recordyear:
        recordyear = currentyear

    return range(recordyear, currentyear+1)


def calculate_citation_history_coordinates(recid):
    """Return a list of citation graph coordinates for RECID, sorted by year."""
    result = {}
    for year in calculate_citation_graphe_x_coordinates(recid):
        result[year] = 0

    if len(result) < CFG_BIBRANK_CITATION_HISTORY_MIN_X_POINTS:
        # do not generate graphs that have less than X points
        return []

    for recid in get_cited_by(recid):
        rec_date = get_fieldvalues(recid, '269__c')
        if not rec_date:
            rec_date = get_fieldvalues(recid, '773__y')
            if not rec_date:
                rec_date = get_fieldvalues(recid, '260__c')
        # Some records simlpy do not have these fields
        if rec_date:
            # Maybe rec_date[0][0:4] has a typo and cannot
            # be converted to an int
            try:
                d = strptime(rec_date[0], '%Y-%m')
            except ValueError:
                pass
            else:
                if d.year in result:
                    result[d.year] += 1

    return sorted(result.iteritems())


def find_year(recordyear):
    """find the year in the string as a suite of 4 int"""
    year = None
    for i in range(len(recordyear)-3):
        s = recordyear[i:i+4]
        if s.isalnum():
            year = int(s)
            break
    return year


def html_command(filename):
    t = ''
    if CFG_BIBRANK_SHOW_CITATION_GRAPHS == 1:
        t = '<img src="%s/%s/%s" align="center" alt="Citation Graph">' \
                                           % (CFG_SITE_URL, REL_PATH, filename)
    elif CFG_BIBRANK_SHOW_CITATION_GRAPHS == 2:
        t = open(os.path.join(BASE_DIR, filename)).read()
    return t


def remove_old_graph_if_needed(filename):
    """Delete graph if it is older than x seconds"""
    if not os.path.isfile(filename):
        return True

    time_diff = time.time() - os.stat(filename).st_mtime
    if time_diff > 3600*24:
        try:
            os.unlink(filename)
        except OSError, e:
            # File does not exist is ok
{'fraction_non_alphanumeric': 0.08355240598925591, 'fraction_numerical': 0.01920219453651846, 'mean_word_length': 2.653444676409186, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3306119', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2662, 'n_words': 833}