Skip to content

Commit

Permalink
Merge pull request #89 from neutrinoceros/formatting_conf
Browse files Browse the repository at this point in the history
STY: copy over yt's linting+formatting configuration
  • Loading branch information
brittonsmith authored Oct 13, 2021
2 parents c310b7c + 7f8a862 commit 1597dbd
Show file tree
Hide file tree
Showing 78 changed files with 2,869 additions and 2,099 deletions.
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# migrate to black + isort + pyupgrade
88e8d9e5afd0ca7d20afb0a15383bcede0f1b1cd
8553023d396182eb2315f3816623f852d2a51dc7
2 changes: 1 addition & 1 deletion .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ BW Keller <kellerbw@mcmaster.ca> Ben Keller <malzraa@gmail.com>
Benjamin Thompson <bthompson2090@gmail.com> Ben Thompson <bthompson2090@gmail.com>
Alex Lindsay <al007@illinois.edu> Alex Lindsay <alexlindsay239@gmail.com>
Yi-Hao Chen (ychen@astro.wisc.edu) Yi-Hao Chen (yihaochentw@gmail.com)
Kacper Kowalik <xarthisius.kk@gmail.com> Kacper Kowalik (Xarthisius) <xarthisius.kk@gmail.com>
Kacper Kowalik <xarthisius.kk@gmail.com> Kacper Kowalik (Xarthisius) <xarthisius.kk@gmail.com>
53 changes: 53 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# pre-commit 1.1.0 is required for `exclude`
# however `minimum_pre_commit_version` itself requires 1.15.0
minimum_pre_commit_version: "1.15.0"

ci:
autofix_prs: true

repos:
#- repo: https://github.com/asottile/setup-cfg-fmt
# rev: v1.17.0
# hooks:
# - id: setup-cfg-fmt
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: no-commit-to-branch
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.0
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/psf/black
rev: 21.9b0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: '5.9.3'
hooks:
- id: isort
name: isort (python)
- id: isort
name: isort (cython)
types: [cython]
- repo: https://github.com/PyCQA/flake8
rev: '3.9.2'
hooks:
- id: flake8
additional_dependencies: [
flake8-bugbear==21.9.1,
flake8-logging-format==0.6.0,
flake8-2020==1.6.0,
]
- repo: https://github.com/asottile/blacken-docs
rev: v1.11.0
hooks:
- id: blacken-docs
additional_dependencies: [black==21.8b0]
#- repo: https://github.com/pre-commit/pygrep-hooks
# rev: v1.9.0
# hooks:
# - id: rst-backticks
2 changes: 1 addition & 1 deletion COPYING.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The yt Development Team is the set of all contributors to the yt project. This
includes all of the yt subprojects.

The core team that coordinates development on BitBucket can be found here:
http://bitbucket.org/yt_analysis/
http://bitbucket.org/yt_analysis/


Our Copyright Policy
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ schedule.
Full installation documentation can also be found
[here](https://yt-astro-analysis.readthedocs.io/en/latest/Installation.html).

To install yt_astro_analysis, you will first need to
To install yt_astro_analysis, you will first need to
[install yt](https://github.com/yt-project/yt#installation). Then do,

```
Expand Down
30 changes: 17 additions & 13 deletions doc/extensions/config_help.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import re
import subprocess

from docutils import statemachine
from sphinx.util.compat import Directive


def setup(app):
app.add_directive('config_help', GetConfigHelp)
app.add_directive("config_help", GetConfigHelp)
setup.app = app
setup.config = app.config
setup.confdir = app.confdir

retdict = dict(
version='1.0',
parallel_read_safe=True,
parallel_write_safe=True
)
retdict = dict(version="1.0", parallel_read_safe=True, parallel_write_safe=True)

return retdict


class GetConfigHelp(Directive):
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True

def run(self):
rst_file = self.state_machine.document.attributes['source']
data = subprocess.check_output(
self.arguments[0].split(" ") + ['-h']).decode('utf8').split('\n')
ind = next((i for i, val in enumerate(data)
if re.match('\s{0,3}\{.*\}\s*$', val)))
lines = ['.. code-block:: none', ''] + data[ind + 1:]
rst_file = self.state_machine.document.attributes["source"]
data = (
subprocess.check_output(self.arguments[0].split(" ") + ["-h"])
.decode("utf8")
.split("\n")
)
ind = next(
(i for i, val in enumerate(data) if re.match(r"\s{0,3}\{.*\}\s*$", val))
)
lines = [".. code-block:: none", ""] + data[ind + 1 :]
self.state_machine.insert_input(
statemachine.string2lines("\n".join(lines)), rst_file)
statemachine.string2lines("\n".join(lines)), rst_file
)
return []
39 changes: 18 additions & 21 deletions doc/extensions/pythonscript_sphinxext.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import tempfile
import time
import os
import errno
import glob
import os
import shutil
import subprocess
import tempfile
import time
import uuid
import errno
from sphinx.util.compat import Directive

from docutils import nodes
from sphinx.util.compat import Directive


class PythonScriptDirective(Directive):
Expand All @@ -18,6 +19,7 @@ class PythonScriptDirective(Directive):
along with the script.
"""

required_arguments = 0
optional_arguments = 0
has_content = True
Expand All @@ -27,7 +29,7 @@ def run(self):
tmpdir = tempfile.mkdtemp()
os.chdir(tmpdir)

rst_file = self.state_machine.document.attributes['source']
rst_file = self.state_machine.document.attributes["source"]
rst_dir = os.path.abspath(os.path.dirname(rst_file))

image_dir, image_rel_dir = make_image_dir(setup, rst_dir)
Expand All @@ -45,20 +47,19 @@ def run(self):
print("")

start = time.time()
subprocess.call(['python', 'temp.py'])
print(">> The execution of the script %s took %f s" %
(uid, time.time() - start))
text = ''
subprocess.call(["python", "temp.py"])
print(f">> The execution of the script {uid} took {time.time() - start:f} s")
text = ""
for im in sorted(glob.glob("*.png")):
text += get_image_tag(im, image_dir, image_rel_dir)

code = content

literal = nodes.literal_block(code, code)
literal['language'] = 'python'
literal["language"] = "python"

attributes = {'format': 'html'}
img_node = nodes.raw('', text, **attributes)
attributes = {"format": "html"}
img_node = nodes.raw("", text, **attributes)

# clean up
os.chdir(cwd)
Expand All @@ -68,16 +69,12 @@ def run(self):


def setup(app):
app.add_directive('python-script', PythonScriptDirective)
app.add_directive("python-script", PythonScriptDirective)
setup.app = app
setup.config = app.config
setup.confdir = app.confdir

retdict = dict(
version='0.1',
parallel_read_safe=True,
parallel_write_safe=True
)
retdict = dict(version="0.1", parallel_read_safe=True, parallel_write_safe=True)

return retdict

Expand All @@ -90,9 +87,9 @@ def get_image_tag(filename, image_dir, image_rel_dir):


def make_image_dir(setup, rst_dir):
image_dir = setup.app.builder.outdir + os.path.sep + '_images'
image_dir = setup.app.builder.outdir + os.path.sep + "_images"
rel_dir = os.path.relpath(setup.confdir, rst_dir)
image_rel_dir = rel_dir + os.path.sep + '_images'
image_rel_dir = rel_dir + os.path.sep + "_images"
thread_safe_mkdir(image_dir)
return image_dir, image_rel_dir

Expand Down
35 changes: 19 additions & 16 deletions doc/extensions/yt_colormaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,51 @@
# 2. This script is added to the document in a literalinclude
# 3. Any _static images found will be added

from sphinx.util.compat import Directive
import glob
import os
import shutil

from docutils.parsers.rst import directives
import os, glob, shutil
from sphinx.util.compat import Directive

# Some of this magic comes from the matplotlib plot_directive.


def setup(app):
app.add_directive('yt_colormaps', ColormapScript)
app.add_directive("yt_colormaps", ColormapScript)
setup.app = app
setup.config = app.config
setup.confdir = app.confdir

retdict = dict(
version='0.1',
parallel_read_safe=True,
parallel_write_safe=True
)
retdict = dict(version="0.1", parallel_read_safe=True, parallel_write_safe=True)

return retdict


class ColormapScript(Directive):
required_arguments = 1
optional_arguments = 0

def run(self):
rst_file = self.state_machine.document.attributes['source']
rst_file = self.state_machine.document.attributes["source"]
rst_dir = os.path.abspath(os.path.dirname(rst_file))
script_fn = directives.path(self.arguments[0])
script_bn = os.path.basename(script_fn)
script_name = os.path.basename(self.arguments[0]).split(".")[0]

# This magic is from matplotlib
dest_dir = os.path.abspath(os.path.join(setup.app.builder.outdir,
os.path.dirname(script_fn)))
dest_dir = os.path.abspath(
os.path.join(setup.app.builder.outdir, os.path.dirname(script_fn))
)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir) # no problem here for me, but just use built-ins
os.makedirs(dest_dir) # no problem here for me, but just use built-ins

rel_dir = os.path.relpath(rst_dir, setup.confdir)
place = os.path.join(dest_dir, rel_dir)
if not os.path.isdir(place): os.makedirs(place)
shutil.copyfile(os.path.join(rst_dir, script_fn),
os.path.join(place, script_bn))
if not os.path.isdir(place):
os.makedirs(place)
shutil.copyfile(
os.path.join(rst_dir, script_fn), os.path.join(place, script_bn)
)

im_path = os.path.join(rst_dir, "_static")
images = sorted(glob.glob(os.path.join(im_path, "*.png")))
Expand Down
49 changes: 28 additions & 21 deletions doc/extensions/yt_cookbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,60 @@
# 2. This script is added to the document in a literalinclude
# 3. Any _static images found will be added

from sphinx.util.compat import Directive
import glob
import os
import shutil

from docutils.parsers.rst import directives
import os, glob, shutil
from sphinx.util.compat import Directive

# Some of this magic comes from the matplotlib plot_directive.


def setup(app):
app.add_directive('yt_cookbook', CookbookScript)
app.add_directive("yt_cookbook", CookbookScript)
setup.app = app
setup.config = app.config
setup.confdir = app.confdir

retdict = dict(
version='0.1',
parallel_read_safe=True,
parallel_write_safe=True
)
retdict = dict(version="0.1", parallel_read_safe=True, parallel_write_safe=True)

return retdict


data_patterns = ["*.h5", "*.out", "*.dat"]


class CookbookScript(Directive):
required_arguments = 1
optional_arguments = 0

def run(self):
rst_file = self.state_machine.document.attributes['source']
rst_file = self.state_machine.document.attributes["source"]
rst_dir = os.path.abspath(os.path.dirname(rst_file))
script_fn = directives.path(self.arguments[0])
script_bn = os.path.basename(script_fn)
script_name = os.path.basename(self.arguments[0]).split(".")[0]

# This magic is from matplotlib
dest_dir = os.path.abspath(os.path.join(setup.app.builder.outdir,
os.path.dirname(script_fn)))
dest_dir = os.path.abspath(
os.path.join(setup.app.builder.outdir, os.path.dirname(script_fn))
)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir) # no problem here for me, but just use built-ins
os.makedirs(dest_dir) # no problem here for me, but just use built-ins

rel_dir = os.path.relpath(rst_dir, setup.confdir)
place = os.path.join(dest_dir, rel_dir)
if not os.path.isdir(place): os.makedirs(place)
shutil.copyfile(os.path.join(rst_dir, script_fn),
os.path.join(place, script_bn))
if not os.path.isdir(place):
os.makedirs(place)
shutil.copyfile(
os.path.join(rst_dir, script_fn), os.path.join(place, script_bn)
)

im_path = os.path.join(rst_dir, "_static")
images = sorted(glob.glob(os.path.join(im_path, "%s__*.png" % script_name)))
lines = []
lines.append("(`%s <%s>`__)" % (script_bn, script_fn))
lines.append(f"(`{script_bn} <{script_fn}>`__)")
lines.append("\n")
lines.append("\n")
lines.append(".. literalinclude:: %s" % self.arguments[0])
Expand All @@ -65,13 +70,15 @@ def run(self):
lines.append("\n")
lines.append("\n")
for ext in data_patterns:
data_files = sorted(glob.glob(os.path.join(
im_path, "%s__*.%s" % (script_name, ext))))
data_files = sorted(
glob.glob(os.path.join(im_path, f"{script_name}__*.{ext}"))
)
for df in data_files:
df_bn = os.path.basename(df)
shutil.copyfile(os.path.join(rst_dir, df),
os.path.join(dest_dir, rel_dir, df_bn))
lines.append(" * Data: `%s <%s>`__)" % (df_bn, df))
shutil.copyfile(
os.path.join(rst_dir, df), os.path.join(dest_dir, rel_dir, df_bn)
)
lines.append(f" * Data: `{df_bn} <{df}>`__)")
lines.append("\n")
self.state_machine.insert_input(lines, rst_file)
return []
Loading

0 comments on commit 1597dbd

Please sign in to comment.