File: copybutton.py

package info (click to toggle)
python-easydev 0.12.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 488 kB
  • sloc: python: 1,899; javascript: 49; makefile: 11
file content (119 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- python -*-
# -*- coding: utf-8 -*-
#
#  This file is part of the easydev software
#
#  Copyright (c) 2011-2014
#
#  File author(s): Thomas Cokelaer <cokelaer@gmail.com>
#
#  Distributed under the GPLv3 License.
#  See accompanying file LICENSE.txt or copy at
#      http://www.gnu.org/licenses/gpl-3.0.html
#
#  Website: https://github.com/cokelaer/easydev
#  Documentation: http://packages.python.org/easydev
#
##############################################################################
"""This module is a copy of a sphinx extension. unknown origin.

    added by Thomas Cokelaer: get_copybutton_path function.

    Create a sphinx extension based on copybutton javascript from python website

Requires sphinx to be installed. imports are inside functions so not stricly
speaking required for the installation.
"""
import os
from os.path import join as pj
import shutil

try:
    from docutils import nodes
except Exception:  # pragma: no cover
    # if docutils is not installed
    class Dummy:
        SkipNode = Exception

    nodes = Dummy()


__all__ = [
    "get_copybutton_path",
]


def copy_javascript_into_static_path(static="_static", filepath="copybutton.js"):
    """This script can be included in a sphinx configuration file to copy the
    copybutton in the static directory

    :param str static: name of the static path (_static by default)
    :param filename: full path of the file to copy

    :Details: If the path *static* does not exists, it is created. If the
        filename in filepath is already in the path *static*, nothing need to be done.
        Otherwise, the file is copied in *static* directory.

    """
    if os.path.isdir(static):
        pass
    else:
        os.mkdir(static)

    filename = os.path.split(filepath)[1]
    if os.path.isfile(static + os.sep + filename):
        pass
    else:
        shutil.copy(filepath, static + os.sep + filename)


def get_copybutton_path():
    """Return the path where the to find the copybutton javascript

    Copy the copybutton.js javascript in the share directory of easydev so
    that it is accesible by all packages that required it by typing:

    .. doctest::

        >>> from easydev import get_copybutton_path
        >>> p = get_copybutton_path()

    It can then be added with a Sphinx configuration file::

        jscopybutton_path = easydev.copybutton.get_copybutton_path()

    """
    import easydev

    try:  # install mode
        packagedir = easydev.__path__[0]
        packagedir = os.path.realpath(pj(packagedir, "share"))
        os.listdir(packagedir)  # if this faisl, we are in deve mode
    except OSError:  # pragma: no cover
        try:
            packagedir = easydev.__path__[0]
            packagedir = os.path.realpath(pj(packagedir, "..", "share"))
        except:
            raise IOError("could not find data directory")
    return pj(packagedir, "copybutton.js")


def setup(app):  # pragma: no cover
    cwd = os.getcwd()

    # From Sphinx, we typing "make html", this is the place where we expect
    # the JS to be found
    staticpath = os.sep.join([cwd, "source", "_static"])
    from easydev.tools import mkdirs

    mkdirs(staticpath)
    if os.path.exists(staticpath + os.sep + "copybutton.js"):
        pass  # the JS file is already there.
    else:
        # Not found, so let us copy it
        import shutil

        shutil.copy(get_copybutton_path(), staticpath)

    # Now that the file is available, use it
    app.add_js_file("copybutton.js")