Skip to content

Commit de73646

Browse files
committed
Added copyright symbols. Added new decorator to doctools.py to "fix" sphinx links in docstrings. Pulled the actual logic out of the other decorators into separate functions to allow use elsewhere.
1 parent cc3614e commit de73646

File tree

6 files changed

+123
-15
lines changed

6 files changed

+123
-15
lines changed

__pkginfo__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2019 Dominic Davis-Foster <[email protected]>
1+
# Copyright © 2019 Dominic Davis-Foster <[email protected]>
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by

domdf_python_tools/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Compiled 2018-2019 by Dominic Davis-Foster <[email protected]>
22
#
33
# terminal.py
4-
# Copyright 2014-2019 Dominic Davis-Foster <[email protected]>
4+
# Copyright © 2014-2019 Dominic Davis-Foster <[email protected]>
55
#
66
# get_terminal_size, _get_terminal_size_windows, _get_terminal_size_tput and _get_terminal_size_linux
77
# from https://gist.github.com/jtriley/1108174
8-
# Copyright 2011 jtriley
8+
# Copyright © 2011 jtriley
99
#
1010
# paths.py
11-
# Copyright 2018-2019 Dominic Davis-Foster <[email protected]>
11+
# Copyright © 2018-2019 Dominic Davis-Foster <[email protected]>
1212
#
1313
# copytree based on https://stackoverflow.com/a/12514470/3092681
14-
# Copyright 2012 atzz
14+
# Copyright © 2012 atzz
1515
# Licensed under CC-BY-SA
1616
#
1717

domdf_python_tools/doctools.py

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
Utilities for documenting functions, classes and methods
77
"""
88
#
9+
# Copyright © 2020 Dominic Davis-Foster <[email protected]>
910
# Based on https://softwareengineering.stackexchange.com/a/386758
10-
# Copyright (c) amon (https://softwareengineering.stackexchange.com/users/60357/amon)
11+
# Copyright © amon (https://softwareengineering.stackexchange.com/users/60357/amon)
1112
# Licensed under CC BY-SA 4.0
1213
#
1314
# This program is free software; you can redistribute it and/or modify
@@ -26,16 +27,123 @@
2627
# MA 02110-1301, USA.
2728
#
2829

30+
import builtins
31+
32+
33+
def document_object_from_another(target, original):
34+
"""
35+
Sets the docstring of the `target` function to that of the `original` function.
36+
37+
This may be useful for subclasses or wrappers that use the same arguments.
38+
39+
:param target: The object to set the docstring for
40+
:type target: any
41+
:param original: The object to copy the docstring from
42+
:type original: any
43+
"""
44+
45+
target.__doc__ = original.__doc__
46+
2947

3048
def is_documented_by(original):
49+
"""
50+
Sets the docstring of the `target` function to that of the `original` function.
51+
52+
This may be useful for subclasses or wrappers that use the same arguments.
53+
"""
54+
3155
def wrapper(target):
32-
target.__doc__ = original.__doc__
56+
document_object_from_another(target, original)
3357
return target
58+
3459
return wrapper
3560

3661

62+
def append_doctring_from_another(target, original):
63+
"""
64+
Sets the docstring of the `target` function to that of the `original` function.
65+
66+
This may be useful for subclasses or wrappers that use the same arguments.
67+
68+
Any indentation in either docstring is removed to
69+
ensure consistent indentation between the two docstrings.
70+
Bear this in mind if additional indentation is used in the docstring.
71+
72+
:param target: The object to append the docstring to
73+
:type target: any
74+
:param original: The object to copy the docstring from
75+
:type original: any
76+
"""
77+
78+
split_target_doc = target.__doc__.split("\n")
79+
deindented_target_doc = [line.lstrip("\t ") for line in split_target_doc]
80+
81+
split_original_doc = original.__doc__.split("\n")
82+
deindented_original_doc = [line.lstrip("\t ") for line in split_original_doc]
83+
84+
target.__doc__ = f"\n".join(deindented_target_doc + deindented_original_doc)
85+
86+
3787
def append_docstring_from(original):
88+
"""
89+
Appends the docstring from the `original` function to the `target` function.
90+
91+
This may be useful for subclasses or wrappers that use the same arguments.
92+
93+
Any indentation in either docstring is removed to
94+
ensure consistent indentation between the two docstrings.
95+
Bear this in mind if additional indentation is used in the docstring.
96+
"""
97+
98+
def wrapper(target):
99+
append_doctring_from_another(target, original)
100+
return target
101+
102+
return wrapper
103+
104+
105+
def make_sphinx_links(input_string, builtins_list=None):
106+
"""
107+
Make proper sphinx links out of double-backticked strings in docstring.
108+
109+
i.e. \`\`str\`\` becomes \:class\:\`~python:str\`
110+
111+
112+
Make sure to have `'python': ('https://docs.python.org/3/', None),` in the
113+
`intersphinx_mapping` dict of your conf.py for sphinx.
114+
115+
:param input_string: The string to process
116+
:type input_string: str
117+
:param builtins_list: A list of builtins to make links for
118+
:type builtins_list: list of str
119+
120+
:return: processed string with links
121+
:rtype: str
122+
"""
123+
124+
if builtins_list is None:
125+
builtins_list = dir(builtins)
126+
127+
working_string = f"{input_string}"
128+
129+
for builtin in {x for x in builtins_list if not x.startswith("__") and x != "None"}:
130+
working_string = working_string.replace(f"``{builtin}``", f":class:`~python:{builtin}`")
131+
132+
return working_string
133+
134+
135+
def sphinxify_docstring():
136+
"""
137+
Make proper sphinx links out of double-backticked strings in docstring.
138+
139+
i.e. \`\`str\`\` becomes \:class\:\`~python:str\`
140+
141+
Make sure to have `'python': ('https://docs.python.org/3/', None),` in the
142+
`intersphinx_mapping` dict of your conf.py for sphinx.
143+
"""
144+
38145
def wrapper(target):
39-
target.__doc__ = target.__doc__ + "\n" + original.__doc__
146+
target.__doc__ = make_sphinx_links(target.__doc__)
40147
return target
148+
41149
return wrapper

domdf_python_tools/paths.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
Functions for paths and files
77
"""
88
#
9-
# Copyright 2018-2019 Dominic Davis-Foster <[email protected]>
9+
# Copyright © 2018-2019 Dominic Davis-Foster <[email protected]>
1010
#
1111
# check_dependencies based on https://stackoverflow.com/a/29044693/3092681
12-
# Copyright 2015 TehTechGuy
12+
# Copyright © 2015 TehTechGuy
1313
# Licensed under CC-BY-SA
1414
#
1515
# as_text from https://stackoverflow.com/a/40935194
16-
# Copyright 2016 User3759685
16+
# Copyright © 2016 User3759685
1717
# Available under the MIT License
1818
#
1919
# chunks from https://stackoverflow.com/a/312464/3092681
20-
# Copytight 2008 Ned Batchelder
20+
# Copytight © 2008 Ned Batchelder
2121
# Licensed under CC-BY-SA
2222
#
2323
# This program is free software; you can redistribute it and/or modify

domdf_python_tools/terminal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
Useful functions for terminal-based programs
77
"""
88
#
9-
# Copyright 2014-2019 Dominic Davis-Foster <[email protected]>
9+
# Copyright © 2014-2019 Dominic Davis-Foster <[email protected]>
1010
#
1111
# get_terminal_size, _get_terminal_size_windows, _get_terminal_size_tput and _get_terminal_size_linux
1212
# from https://gist.github.com/jtriley/1108174
13-
# Copyright 2011 jtriley
13+
# Copyright © 2011 jtriley
1414
#
1515
# This program is free software; you can redistribute it and/or modify
1616
# it under the terms of the GNU Lesser General Public License as published by

domdf_python_tools/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
General Functions
77
"""
88
#
9-
# Copyright 2018-2019 Dominic Davis-Foster <[email protected]>
9+
# Copyright © 2018-2019 Dominic Davis-Foster <[email protected]>
1010
#
1111
# This program is free software; you can redistribute it and/or modify
1212
# it under the terms of the GNU Lesser General Public License as published by

0 commit comments

Comments
 (0)