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

fix: modify fsclient yaml dump in attempt to have it conform ot prettier standards #1231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 28 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
default_language_version:
python: python3
python: python3
ci:
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit hooks
autofix_prs: true
autoupdate_branch: 'pre-commit-autoupdate'
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
autoupdate_schedule: monthly
skip: [no-commit-to-branch]
submodules: false
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit hooks
autofix_prs: true
autoupdate_branch: "pre-commit-autoupdate"
autoupdate_commit_msg: "[pre-commit.ci] pre-commit autoupdate"
autoupdate_schedule: monthly
skip: [no-commit-to-branch]
submodules: false
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
exclude: '\.(rst|txt)$'
- id: check-case-conflict
- id: check-merge-conflict
- id: check-toml
Expand All @@ -25,11 +24,11 @@ repos:
rev: 24.4.2
hooks:
- id: black
exclude: ^tests/test_helpers.py$|^tests/test_commands.py$
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
exclude: ^tests/test_helpers.py$|^tests/test_commands.py$
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
Expand All @@ -49,6 +48,21 @@ repos:
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
- id: codespell
additional_dependencies:
- tomli
- id: codespell
additional_dependencies:
- tomli
# prettier - multi formatter for .json, .yml, and .md files
- repo: https://github.com/pre-commit/mirrors-prettier
rev: f12edd9c7be1c20cfa42420fd0e6df71e42b51ea # frozen: v4.0.0-alpha.8
hooks:
- id: prettier
types: [yaml]
additional_dependencies:
- "prettier@^3.2.4"
# docformatter - PEP 257 compliant docstring formatter
- repo: https://github.com/s-weigand/docformatter
rev: 5757c5190d95e5449f102ace83df92e7d3b06c6c
hooks:
- id: docformatter
additional_dependencies: [tomli]
args: [--in-place, --config, ./pyproject.toml]
23 changes: 23 additions & 0 deletions news/prettier_yml_dump.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* update yaml.dumper in fsclient so that it outputs code that passes prettier and black tests

**Security:**

* <news item>
13 changes: 8 additions & 5 deletions src/regolith/fsclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __exit__(self, type, value, traceback):


def _rec_re_type(i):
"""Destroy this when ruamel.yaml supports basetypes again"""
"""Destroy this when ruamel.yaml supports basetypes again."""
if type(i) in YAML_BASE_MAP:
base = YAML_BASE_MAP[type(i)]()
if isinstance(base, dict):
Expand Down Expand Up @@ -97,8 +97,11 @@ def load_yaml(filename, return_inst=False, loader=None):
def dump_yaml(filename, docs, inst=None):
"""Dumps a dict of documents into a file."""
inst = YAML() if inst is None else inst
inst.indent(mapping=2, sequence=4, offset=2) # Match Prettier's default
inst.preserve_quotes = True # Prevent unnecessary quote changes
inst.width = 79 # Keep line width under control
inst.explicit_start = True # Add `---` at the start, Prettier is fine with it
inst.representer.ignore_aliases = lambda *data: True
inst.indent(mapping=2, sequence=4, offset=2)
sorted_dict = ruamel.yaml.comments.CommentedMap()
for k in sorted(docs):
doc = docs[k]
Expand Down Expand Up @@ -187,14 +190,14 @@ def load_database(self, db):
self.load_yaml(db, dbpath)

def dump_json(self, docs, collname, dbpath):
"""Dumps json docs and returns filename"""
"""Dumps json docs and returns filename."""
f = os.path.join(dbpath, collname + ".json")
dump_json(f, docs)
filename = os.path.split(f)[-1]
return filename

def dump_yaml(self, docs, collname, dbpath):
"""Dumps json docs and returns filename"""
"""Dumps json docs and returns filename."""
f = os.path.join(dbpath, collname + self._collexts.get(collname, ".yaml"))
inst = self._yamlinsts.get((dbpath, collname), None)
dump_yaml(f, docs, inst=inst)
Expand Down Expand Up @@ -250,7 +253,7 @@ def insert_many(self, dbname, collname, docs):
coll[doc["_id"]] = doc

def delete_one(self, dbname, collname, doc):
"""Removes a single document from a collection"""
"""Removes a single document from a collection."""
coll = self.dbs[dbname][collname]
del coll[doc["_id"]]

Expand Down
Loading