Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Toy implementation of automatic translation of GMT docs to PyGMT docstrings #917

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Toy implementation of automatic translation of GMT docs to PyGMT docs…
…trings
  • Loading branch information
seisman committed Feb 17, 2021
commit 087569653e302db11e955521b085bbc6c3f3150b
51 changes: 51 additions & 0 deletions sandbox/pygmt_docstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import textwrap
import difflib
import pygmt


gmt_doc = r"""
Select painting or dumping country polygons from the Digital Chart of the World.
This is another dataset independent of GSHHG and hence the **-A** and **-D** options do not apply.
Append one or more comma-separated countries using the
`2-character ISO 3166-1 alpha-2 convention <https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2>`_.
To select a state of a country
(if available), append .state, e.g, US.TX for Texas. To specify a
whole continent, prepend = to any of the continent codes AF (Africa),
AN (Antarctica), AS (Asia), EU (Europe), OC (Oceania),
NA (North America), or SA (South America). Append **+l** to
just list the countries and their codes [no data extraction or plotting takes place].
Use **+L** to see states/territories for Argentina, Australia, Brazil, Canada, China, India, Russia and the US.
Finally, you can append **+l**\|\ **+L** to **-E**\ =\ *continent* to only list countries in that continent;
repeat if more than one continent is requested.
Append **+p**\ *pen* to draw polygon outlines [no outline] and
**+g**\ *fill* to fill them [no fill]. One of **+p**\|\ **g** must be
specified unless **-M** is in effect, in which case only one **-E** option can be given;
append **+z** to place the country code in the segment headers via **-Z**\ *code* settings.
Otherwise, you may repeat **-E** to give different groups of items their own pen/fill settings.
If neither **-J** nor **-M** are set then we just print the **-R**\ *wesn* string.
"""

# function to document
function = pygmt.Figure.coast

# translation
gmt_doc = gmt_doc.lstrip("\n")
pygmt_doc = gmt_doc
for key, value in function.aliases.items():
pygmt_doc = pygmt_doc.replace(f"**-{key}**", f"**{value}**")


# output
diff = difflib.unified_diff(gmt_doc.splitlines(), pygmt_doc.splitlines(), lineterm="")
print("Diff view:\n")
print("\n".join(diff))
print("\n")

print("Raw GMT docstring:\n")
print(gmt_doc)

print("Translate PyGMT docstring:\n")
pygmt_doc = textwrap.indent(
textwrap.fill(textwrap.dedent(pygmt_doc), width=72), " " * 8
)
print(pygmt_doc)