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

"""
Process docstrings with Sphinx

AUTHORS:
- <NAME> (2009-09-29): initial version
- The Spyder Development Team: Maintenance

Copyright (C) 2009 <NAME> <<EMAIL>>
Copyright (C) 2013- The Spyder Development Team
Distributed under the terms of the BSD License

Taken from the Sage project (www.sagemath.org).
See here for the original version:
http://doc.sagemath.org/html/en/reference/notebook/sagenb/misc/sphinxify.html
"""

# Stdlib imports
import codecs
import inspect
import os
import os.path as osp
import shutil
import sys
import tempfile
from xml.sax.saxutils import escape

# 3rd party imports
from docutils.utils import SystemMessage as SystemMessage
from jinja2 import Environment, FileSystemLoader
import sphinx
from sphinx.application import Sphinx

# Local imports
from . import options
from .utils import to_unicode_from_fs, to_binary_string


#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

CONFDIR_PATH = osp.dirname(__file__)
CSS_PATH = osp.join(CONFDIR_PATH, 'static', 'css')
JS_PATH = osp.join(CONFDIR_PATH, 'js')
JQUERY_PATH = JS_PATH

if os.name == 'nt':
    CACHEDIR = tempfile.gettempdir() + osp.sep + 'oinspect'
else:
    username = to_unicode_from_fs(os.environ.get('USER'))
    CACHEDIR = tempfile.gettempdir() + osp.sep + 'oinspect-' + username


#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------

def is_sphinx_markup(docstring):
    """Returns whether a string contains Sphinx-style ReST markup."""
    # this could be made much more clever
    return ("`" in docstring or "::" in docstring)


def warning(message):
    """Print a warning message on the rich text view"""
    env = Environment()
    env.loader = FileSystemLoader(osp.join(CONFDIR_PATH, 'templates'))
    warning = env.get_template("warning.html")
    return warning.render(css_path=CSS_PATH, text=message)


def format_argspec(argspec):
    """Format argspect, convenience wrapper around inspect's.

    This takes a dict instead of ordered arguments and calls
    inspect.format_argspec with the arguments in the necessary order.
    """
    return inspect.formatargspec(argspec['args'], argspec['varargs'],
                                 argspec['varkw'], argspec['defaults'])


def generate_conf(directory):
    """
    Generates a Sphinx configuration file in `directory`.

    Parameters
    ----------
    directory : str
        Base directory to use
    """
    # conf.py file for Sphinx
    conf = osp.join(CONFDIR_PATH, 'conf.py')

    # Docstring layout page (in Jinja):
    layout = osp.join(osp.join(CONFDIR_PATH, 'templates'), 'layout.html')

    os.makedirs(osp.join(directory, 'templates'))
    os.makedirs(osp.join(directory, 'static'))
    shutil.copy(conf, directory)
    shutil.copy(layout, osp.join(directory, 'templates'))
    open(osp.join(directory, '__init__.py'), 'w').write('')
    open(osp.join(directory, 'static', 'empty'), 'w').write('')


def global_template_vars():
    """Generate a dictionary of global variables for our templates"""
    if options['local_mathjax']:
        # TODO: Fix local use of MathJax
        MATHJAX_PATH = "file:///" + osp.join(JS_PATH, 'mathjax')
    else:
        MATHJAX_PATH = "https://cdn.mathjax.org/mathjax/latest"

    global_vars = \
    {
      'css_path': CSS_PATH,
      'js_path': JS_PATH,
      'jquery_path': JQUERY_PATH,
      'mathjax_path': MATHJAX_PATH,
      'math_on': 'true' if options['render_math'] else '',
      'platform': sys.platform,
      'collapse': options['collapse_sections'],
      'use_qt4': options['use_qt4'],
      'outline': options['outline']
    }

    return global_vars


def init_template_vars(oinfo):
    """
    Initialize variables for our templates.

    It gives default values to the most important variables
    """
    tmpl_vars = global_template_vars()
    
    # Object name
    if oinfo['name'] is None:
        tmpl_vars['name'] = 'foo'
    else:
        tmpl_vars['name'] = oinfo['name']

    # Argspec
    if oinfo['argspec'] is None:
        tmpl_vars['argspec'] = '(...)'
    else:
        argspec = oinfo['argspec']
        try:
            has_self = argspec['args'][0] == 'self'
        except (KeyError, IndexError):
            tmpl_vars['argspec'] = '(...)'
        else:
            if has_self:
                argspec['args'] = argspec['args'][1:]
        tmpl_vars['argspec'] = format_argspec(argspec)

    # Type
    if oinfo['type_name'] is None:
        tmpl_vars['note'] = 'Function of Bar module'
    else:
        tmpl_vars['note'] = '%s' % oinfo['type_name']

    return tmpl_vars
    


def generate_extensions(render_math):
    """Generate list of Sphinx extensions"""
    # We need jsmath to get pretty plain-text latex in docstrings
    extensions = []
    if sphinx.__version__ < "1.1" or not render_math:
        extensions = ['sphinx.ext.jsmath']
    else:
        extensions = ['sphinx.ext.mathjax']<commit_msg>Add a new function (from Spyder) to get signatures from some text<commit_after># -*- coding: utf-8 -*

"""
Process docstrings with Sphinx

AUTHORS:
- <NAME> (2009-09-29): initial version
- The Spyder Development Team: Maintenance

Copyright (C) 2009 <NAME> <<EMAIL>>
Copyright (C) 2013- The Spyder Development Team
Distributed under the terms of the BSD License

Taken from the Sage project (www.sagemath.org).
See here for the original version:
http://doc.sagemath.org/html/en/reference/notebook/sagenb/misc/sphinxify.html
"""

# Stdlib imports
import codecs
import inspect
import os
import os.path as osp
import re
import shutil
import sys
import tempfile
from xml.sax.saxutils import escape

# 3rd party imports
from docutils.utils import SystemMessage as SystemMessage
from jinja2 import Environment, FileSystemLoader
import sphinx
from sphinx.application import Sphinx

# Local imports
from . import options
from .utils import to_unicode_from_fs, to_binary_string


#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

CONFDIR_PATH = osp.dirname(__file__)
CSS_PATH = osp.join(CONFDIR_PATH, 'static', 'css')
JS_PATH = osp.join(CONFDIR_PATH, 'js')
JQUERY_PATH = JS_PATH

if os.name == 'nt':
    CACHEDIR = tempfile.gettempdir() + osp.sep + 'oinspect'
else:
    username = to_unicode_from_fs(os.environ.get('USER'))
    CACHEDIR = tempfile.gettempdir() + osp.sep + 'oinspect-' + username


#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------

def is_sphinx_markup(docstring):
    """Returns whether a string contains Sphinx-style ReST markup."""
    # this could be made much more clever
    return ("`" in docstring or "::" in docstring)


def warning(message):
    """Print a warning message on the rich text view"""
    env = Environment()
    env.loader = FileSystemLoader(osp.join(CONFDIR_PATH, 'templates'))
    warning = env.get_template("warning.html")
    return warning.render(css_path=CSS_PATH, text=message)


def format_argspec(argspec):
    """Format argspect, convenience wrapper around inspect's.

    This takes a dict instead of ordered arguments and calls
    inspect.format_argspec with the arguments in the necessary order.
    """
    return inspect.formatargspec(argspec['args'], argspec['varargs'],
                                 argspec['varkw'], argspec['defaults'])


def getsignaturefromtext(text, objname):
    """Get object signatures from text (object documentation)
    Return a list containing a single string in most cases
    Example of multiple signatures: PyQt4 objects"""
    # Regexps
    oneline_re = objname + r'\([^\)].+?(?<=[\w\]\}\'"])\)(?!,)'
    multiline_re = objname + r'\([^\)]+(?<=[\w\]\}\'"])\)(?!,)'
    multiline_end_parenleft_re = r'(%s\([^\)]+(\),\n.+)+(?<=[\w\]\}\'"])\))'
    # Grabbing signatures
    if not text:
        text = ''
    sigs_1 = re.findall(oneline_re + '|' + multiline_re, text)
    sigs_2 = [g[0] for g in re.findall(multiline_end_parenleft_re % objname, text)]
    all_sigs = sigs_1 + sigs_2
    # The most relevant signature is usually the first one. There could be
    # others in doctests but those are not so important
    if all_sigs:
        sig = all_sigs[0]
        sig = '(' + sig.split('(')[-1] # Remove objname
        return sig
    else:
        return ''


def generate_conf(directory):
    """
    Generates a Sphinx configuration file in `directory`.

    Parameters
    ----------
    directory : str
        Base directory to use
    """
    # conf.py file for Sphinx
    conf = osp.join(CONFDIR_PATH, 'conf.py')

    # Docstring layout page (in Jinja):
    layout = osp.join(osp.join(CONFDIR_PATH, 'templates'), 'layout.html')

    os.makedirs(osp.join(directory, 'templates'))
    os.makedirs(osp.join(directory, 'static'))
    shutil.copy(conf, directory)
    shutil.copy(layout, osp.join(directory, 'templates'))
    open(osp.join(directory, '__init__.py'), 'w').write('')
    open(osp.join(directory, 'static', 'empty'), 'w').write('')


def global_template_vars():
    """Generate a dictionary of global variables for our templates"""
    if options['local_mathjax']:
        # TODO: Fix local use of MathJax
        MATHJAX_PATH = "file:///" + osp.join(JS_PATH, 'mathjax')
    else:
        MATHJAX_PATH = "https://cdn.mathjax.org/mathjax/latest"

    global_vars = \
    {
      'css_path': CSS_PATH,
      'js_path': JS_PATH,
      'jquery_path': JQUERY_PATH,
      'mathjax_path': MATHJAX_PATH,
      'math_on': 'true' if options['render_math'] else '',
      'platform': sys.platform,
      'collapse': options['collapse_sections'],
      'use_qt4': options['use_qt4'],
      'outline': options['outline']
    }

    return global_vars


def init_template_vars(oinfo):
    """
    Initialize variables for our templates.

    It gives default values to the most important variables
    """
    tmpl_vars = global_template_vars()
    
    # Object name
    if oinfo['name'] is None:
        tmpl_vars['name'] = 'foo'
    else:
        tmpl_vars['name'] = oinfo['name']

    # Argspec
    if oinfo['argspec'] is None:
        argspec = getsignaturefromtext(oinfo['docstring'], oinfo['name'])
        if argspec:
            tmpl_vars['argspec'] = argspec
        else:
            tmpl_vars['argspec'] = ''
    else:
        argspec = oinfo['argspec']
        try:
            has_self = argspec['args'][0] == 'self'
        except (KeyError, IndexError):
            argspec = getsignaturefromtext(oinfo['docstring'], oinfo['name'])
            if argspec:
                tmpl_vars['argspec'] = argspec
            else:
                tmpl_vars['argspec'] = '(...)'
        else:
            if has_self:
                argspec['args'] = argspec['args'][1:]
        tmpl_vars['argspec'] = format_argspec(argspec)

    # Type
    if oinfo['type_name'] is None:
        tmpl_vars['note'] = 'Function of Bar module'
    else:
        tmpl_vars['note'] = '%s' % oinfo['type_name']

    return tmpl_vars
    


def generate_extensions(render_math):
    """Generate list of Sphinx extensions"""
    # We need jsmath to get pretty plain-text latex in docstrings
    extensions = []
    if sphinx.__version__ < "1.1" or not render_math:
        extensions = ['sphinx.ext.jsmath']
    else:
        extensions = ['sphinx.ext.mathjax']
{'fraction_non_alphanumeric': 0.17689468715131748, 'fraction_numerical': 0.004978113466655222, 'mean_word_length': 2.562213390400489, 'pattern_counts': {'":': 2, '<': 16, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '6398835', 'n_tokens_mistral': 3612, 'n_tokens_neox': 3520, 'n_words': 925}