Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Y
YAML tool
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nimrod
YAML tool
Commits
4dae990a
Commit
4dae990a
authored
3 years ago
by
nimrod
Browse files
Options
Downloads
Patches
Plain Diff
Allow pretty printing files in place.
So now this is sort-of Black for YAML files. Next, pre-commit hooks.
parent
2b6c9fe2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.pre-commit-config.yaml
+0
-7
0 additions, 7 deletions
.pre-commit-config.yaml
README.rst
+18
-10
18 additions, 10 deletions
README.rst
yamltool/__main__.py
+50
-21
50 additions, 21 deletions
yamltool/__main__.py
with
68 additions
and
38 deletions
.pre-commit-config.yaml
+
0
−
7
View file @
4dae990a
...
@@ -24,13 +24,6 @@ repos:
...
@@ -24,13 +24,6 @@ repos:
hooks
:
hooks
:
-
id
:
detect-secrets
-
id
:
detect-secrets
-
repo
:
https://github.com/amperser/proselint.git
rev
:
0.10.2
hooks
:
-
id
:
proselint
types
:
[
plain-text
]
exclude
:
LICENSE
-
repo
:
https://gitlab.com/devopshq/gitlab-ci-linter.git
-
repo
:
https://gitlab.com/devopshq/gitlab-ci-linter.git
rev
:
v1.0.2
rev
:
v1.0.2
hooks
:
hooks
:
...
...
This diff is collapsed.
Click to expand it.
README.rst
+
18
−
10
View file @
4dae990a
...
@@ -16,7 +16,7 @@ Usage
...
@@ -16,7 +16,7 @@ Usage
.. code:: shell
.. code:: shell
usage: yt [-h] [
infile] [outfile
]
usage: yt [-h] [
-i] [files ...
]
YAML tool, a clone of the json.tool Python module for YAML.
YAML tool, a clone of the json.tool Python module for YAML.
...
@@ -25,11 +25,19 @@ Usage
...
@@ -25,11 +25,19 @@ Usage
documents (like comments and anchors).
documents (like comments and anchors).
positional arguments:
positional arguments:
infile a YAML file to be validated or pretty-printed
files a YAML file to be validated or pretty-printed
outfile write the output of infile to outfile
optional arguments:
optional arguments:
-h, --help show this help message and exit
-h, --help show this help message and exit
-i, --in-place Perform the pretty-print in place, overwriting the existing files.
When enabling --in-place, all files are processed as input files.
When --in-place is not enabled and there are more then 2 files
passed, the last files is considered as the output file. If you
wish to pretty-print multiple files and output to standard out,
specify the last file as "-" .
Please note that specifying multiple input files will concatenate
them, resulting in a single file that has multiple documents.
License
License
-------
-------
...
...
This diff is collapsed.
Click to expand it.
yamltool/__main__.py
+
50
−
21
View file @
4dae990a
...
@@ -9,41 +9,70 @@ documents (like comments and anchors).
...
@@ -9,41 +9,70 @@ documents (like comments and anchors).
import
argparse
import
argparse
import
pathlib
import
pathlib
import
sys
import
sys
import
textwrap
import
ruamel.yaml
# pylint: disable=import-error
import
ruamel.yaml
# pylint: disable=import-error
def
main
():
def
main
():
"""
Main entrypoint.
"""
"""
Main entrypoint.
"""
epilog
=
textwrap
.
dedent
(
"""
When enabling --in-place, all files are processed as input files.
When --in-place is not enabled and there are more then 2 files
passed, the last files is considered as the output file. If you
wish to pretty-print multiple files and output to standard out,
specify the last file as
"
-
"
.
Please note that specifying multiple input files will concatenate
them, resulting in a single file that has multiple documents.
"""
)
parser
=
argparse
.
ArgumentParser
(
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
,
description
=
__doc__
,
epilog
=
epilog
,
formatter_class
=
argparse
.
RawDescriptionHelpFormatter
,
formatter_class
=
argparse
.
RawDescriptionHelpFormatter
,
)
)
parser
.
add_argument
(
parser
.
add_argument
(
"
in
file
"
,
"
file
s
"
,
nargs
=
"
?
"
,
nargs
=
"
*
"
,
type
=
argparse
.
FileType
(
encoding
=
"
utf-8
"
)
,
type
=
pathlib
.
Path
,
help
=
"
a YAML file to be validated or pretty-printed
"
,
help
=
"
a YAML file to be validated or pretty-printed
"
,
default
=
sys
.
stdin
,
)
)
parser
.
add_argument
(
parser
.
add_argument
(
"
outfile
"
,
"
-i
"
,
nargs
=
"
?
"
,
"
--in-place
"
,
type
=
pathlib
.
Path
,
help
=
"
Perform the pretty-print in place, overwriting the existing files.
"
,
# noqa: E501
help
=
"
write the output of infile to outfile
"
,
action
=
"
store_true
"
,
default
=
None
,
)
)
options
=
parser
.
parse_args
()
options
=
parser
.
parse_args
()
with
options
.
infile
as
infile
:
try
:
try
:
yaml
=
ruamel
.
yaml
.
YAML
(
typ
=
"
rt
"
)
yaml
=
ruamel
.
yaml
.
YAML
(
typ
=
"
rt
"
)
yaml
.
explicit_start
=
True
yaml
.
explicit_start
=
True
yaml
.
indent
(
mapping
=
2
,
sequence
=
4
,
offset
=
2
)
yaml
.
indent
(
mapping
=
2
,
sequence
=
4
,
offset
=
2
)
yaml
.
preserve_quotes
=
True
yaml
.
preserve_quotes
=
True
if
options
.
outfile
is
None
:
if
options
.
in_place
:
out
=
sys
.
stdout
if
len
(
options
.
files
)
==
0
:
raise
Exception
(
"
You must provide a list of files to process.
"
)
for
file
in
options
.
files
:
with
open
(
file
,
"
r
"
,
encoding
=
"
utf-8
"
)
as
infile
:
docs
=
list
(
yaml
.
load_all
(
infile
))
with
open
(
file
,
"
w
"
,
encoding
=
"
utf-8
"
)
as
outfile
:
yaml
.
dump_all
(
docs
,
outfile
)
else
:
if
len
(
options
.
files
)
<
2
:
outfile
=
sys
.
stdout
elif
options
.
files
[
-
1
]
==
"
-
"
:
outfile
=
sys
.
stdout
options
.
files
.
pop
(
-
1
)
else
:
outfile
=
open
(
# pylint: disable=consider-using-with
options
.
files
[
-
1
],
"
w
"
,
encoding
=
"
utf-8
"
)
options
.
files
.
pop
(
-
1
)
if
options
.
files
:
for
file
in
options
.
files
:
with
open
(
file
,
"
r
"
,
encoding
=
"
utf-8
"
)
as
infile
:
yaml
.
dump_all
(
yaml
.
load_all
(
infile
),
outfile
)
else
:
else
:
out
=
options
.
outfile
.
open
(
"
w
"
,
encoding
=
"
utf-8
"
)
yaml
.
dump_all
(
yaml
.
load_all
(
sys
.
stdin
),
outfile
)
yaml
.
dump_all
(
yaml
.
load_all
(
infile
),
out
)
except
Exception
as
ex
:
except
Exception
as
ex
:
raise
SystemExit
(
ex
)
# pylint: disable=raise-missing-from
raise
SystemExit
(
ex
)
# pylint: disable=raise-missing-from
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment