Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
Template
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
Template
Commits
a4d8dcd6
Commit
a4d8dcd6
authored
7 years ago
by
nimrod
Browse files
Options
Downloads
Patches
Plain Diff
Use black formatter, added to pre-commit.
parent
97c0329c
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.pre-commit-config.yaml
+8
-2
8 additions, 2 deletions
.pre-commit-config.yaml
setup.py
+19
-22
19 additions, 22 deletions
setup.py
template/__init__.py
+25
-14
25 additions, 14 deletions
template/__init__.py
template/filters.py
+29
-18
29 additions, 18 deletions
template/filters.py
with
81 additions
and
56 deletions
.pre-commit-config.yaml
+
8
−
2
View file @
a4d8dcd6
-
repo
:
git://github.com/pre-commit/pre-commit-hooks
repos
:
sha
:
v0.8.0
-
repo
:
https://github.com/pre-commit/pre-commit-hooks
sha
:
v1.2.3
hooks
:
hooks
:
-
id
:
check-added-large-files
-
id
:
check-added-large-files
-
id
:
check-yaml
-
id
:
check-yaml
-
id
:
check-merge-conflict
-
id
:
check-merge-conflict
-
id
:
flake8
-
id
:
flake8
-
id
:
check-symlinks
-
id
:
check-symlinks
-
repo
:
https://github.com/ambv/black
sha
:
18.5b0
hooks
:
-
id
:
black
args
:
[
--line-length=79
]
This diff is collapsed.
Click to expand it.
setup.py
+
19
−
22
View file @
a4d8dcd6
...
@@ -2,29 +2,26 @@
...
@@ -2,29 +2,26 @@
from
setuptools
import
setup
,
find_packages
from
setuptools
import
setup
,
find_packages
setup
(
setup
(
name
=
'
template
'
,
name
=
"
template
"
,
version
=
open
(
'
VERSION
'
,
'
r
'
).
read
(),
version
=
open
(
"
VERSION
"
,
"
r
"
).
read
(),
description
=
'''
A CLI tool for generating files from Jinja2 templates and
description
=
"""
A CLI tool for generating files from Jinja2 templates and
environment variables.
'''
,
environment variables.
"""
,
long_description
=
open
(
'
README.rst
'
,
'
r
'
).
read
(),
long_description
=
open
(
"
README.rst
"
,
"
r
"
).
read
(),
url
=
'
https://www.shore.co.il/git/template
'
,
url
=
"
https://www.shore.co.il/git/template
"
,
author
=
'
Nimrod Adar
'
,
author
=
"
Nimrod Adar
"
,
author_email
=
'
nimrod@shore.co.il
'
,
author_email
=
"
nimrod@shore.co.il
"
,
license
=
'
MIT
'
,
license
=
"
MIT
"
,
classifiers
=
[
classifiers
=
[
'
Development Status :: 4 - Beta
'
,
"
Development Status :: 4 - Beta
"
,
'
Intended Audience :: Developers
'
,
"
Intended Audience :: Developers
"
,
'
Programming Language :: Python :: 3
'
,
"
Programming Language :: Python :: 3
"
,
'
Programming Language :: Python :: 2
'
,
"
Programming Language :: Python :: 2
"
,
'
Intended Audience :: System Administrators
'
,
"
Intended Audience :: System Administrators
"
,
'
Topic :: Utilities
'
,
"
Topic :: Utilities
"
,
],
],
keywords
=
'
config configuration jinja template environment
'
,
keywords
=
"
config configuration jinja template environment
"
,
packages
=
find_packages
(),
packages
=
find_packages
(),
install_requires
=
[
'
Jinja2
'
,
'
PyYAML
'
,
'
jmespath
'
,
'
toml
'
],
install_requires
=
[
"
Jinja2
"
,
"
PyYAML
"
,
"
jmespath
"
,
"
toml
"
],
extras_require
=
{
extras_require
=
{
"
dev
"
:
[
"
tox
"
]},
'
dev
'
:
[
'
tox
'
],
},
entry_points
=
{
"
console_scripts
"
:
[
"
template=template:main
"
]},
entry_points
=
{
'
console_scripts
'
:
[
'
template=template:main
'
],
},
)
)
This diff is collapsed.
Click to expand it.
template/__init__.py
+
25
−
14
View file @
a4d8dcd6
#!/usr/bin/env python
#!/usr/bin/env python
'''
Generate files from Jinja2 templates and environment variables.
'''
"""
Generate files from Jinja2 templates and environment variables.
"""
from
__future__
import
(
absolute_import
,
division
,
from
__future__
import
(
print_function
,
unicode_literals
)
absolute_import
,
division
,
print_function
,
unicode_literals
,
)
from
jinja2
import
Environment
from
jinja2
import
Environment
from
os
import
environ
from
os
import
environ
from
sys
import
stdin
,
stdout
from
sys
import
stdin
,
stdout
...
@@ -12,9 +16,10 @@ import template.filters
...
@@ -12,9 +16,10 @@ import template.filters
def
render
(
template_string
):
def
render
(
template_string
):
"""
Render the template.
"""
env
=
Environment
(
autoescape
=
True
)
env
=
Environment
(
autoescape
=
True
)
# Add all functions in template.filters as Jinja filters.
# Add all functions in template.filters as Jinja filters.
for
tf
in
filter
(
lambda
x
:
not
x
.
startswith
(
'
_
'
),
dir
(
template
.
filters
)):
for
tf
in
filter
(
lambda
x
:
not
x
.
startswith
(
"
_
"
),
dir
(
template
.
filters
)):
env
.
filters
[
tf
]
=
template
.
filters
.
__getattribute__
(
tf
)
env
.
filters
[
tf
]
=
template
.
filters
.
__getattribute__
(
tf
)
t
=
env
.
from_string
(
template_string
)
t
=
env
.
from_string
(
template_string
)
return
t
.
render
(
environ
)
return
t
.
render
(
environ
)
...
@@ -22,20 +27,26 @@ def render(template_string):
...
@@ -22,20 +27,26 @@ def render(template_string):
def
main
():
def
main
():
parser
=
ArgumentParser
(
parser
=
ArgumentParser
(
description
=
'''
A CLI tool for generating files from Jinja2 templates
description
=
"""
A CLI tool for generating files from Jinja2 templates
and environment variables.
'''
)
and environment variables.
"""
parser
.
add_argument
(
'
filename
'
,
)
help
=
'
Input filename
'
,
parser
.
add_argument
(
type
=
argparse
.
FileType
(
'
r
'
),
"
filename
"
,
nargs
=
'
?
'
)
help
=
"
Input filename
"
,
parser
.
add_argument
(
'
-o
'
,
'
--output
'
,
type
=
argparse
.
FileType
(
"
r
"
),
help
=
'
Output to filename
'
,
nargs
=
"
?
"
,
type
=
argparse
.
FileType
(
'
w
'
))
)
parser
.
add_argument
(
"
-o
"
,
"
--output
"
,
help
=
"
Output to filename
"
,
type
=
argparse
.
FileType
(
"
w
"
),
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
infd
=
args
.
filename
if
args
.
filename
else
stdin
infd
=
args
.
filename
if
args
.
filename
else
stdin
outfd
=
args
.
output
if
args
.
output
else
stdout
outfd
=
args
.
output
if
args
.
output
else
stdout
print
(
render
(
infd
.
read
()),
file
=
outfd
)
print
(
render
(
infd
.
read
()),
file
=
outfd
)
if
__name__
==
'
__main__
'
:
if
__name__
==
"
__main__
"
:
main
()
main
()
This diff is collapsed.
Click to expand it.
template/filters.py
+
29
−
18
View file @
a4d8dcd6
#!/usr/bin/env python
#!/usr/bin/env python
from
__future__
import
(
absolute_import
,
division
,
from
__future__
import
(
print_function
,
unicode_literals
)
absolute_import
,
division
,
print_function
,
unicode_literals
,
)
def
to_yaml
(
value
):
def
to_yaml
(
value
):
'''
"""
Converts given data structure to YAML form.
Converts given data structure to YAML form.
Examples:
Examples:
...
@@ -16,13 +20,14 @@ def to_yaml(value):
...
@@ -16,13 +20,14 @@ def to_yaml(value):
'
1:
\\
n a: [1, 2, 3]
\\
n
'
'
1:
\\
n a: [1, 2, 3]
\\
n
'
>>>
to_yaml
(
"
abc
"
)
>>>
to_yaml
(
"
abc
"
)
'
abc
\\
n...
\\
n
'
'
abc
\\
n...
\\
n
'
'''
"""
from
yaml
import
safe_dump
from
yaml
import
safe_dump
return
safe_dump
(
value
)
return
safe_dump
(
value
)
def
to_json
(
value
):
def
to_json
(
value
):
'''
"""
Converts given data structure to JSON form.
Converts given data structure to JSON form.
Examples:
Examples:
...
@@ -34,13 +39,14 @@ def to_json(value):
...
@@ -34,13 +39,14 @@ def to_json(value):
'
2
'
'
2
'
>>>
to_json
({
1
:
{
'
a
'
:
[
1
,
2
,
3
]}})
>>>
to_json
({
1
:
{
'
a
'
:
[
1
,
2
,
3
]}})
'
{
"
1
"
: {
"
a
"
: [1, 2, 3]}}
'
'
{
"
1
"
: {
"
a
"
: [1, 2, 3]}}
'
'''
"""
from
json
import
dumps
from
json
import
dumps
return
dumps
(
value
)
return
dumps
(
value
)
def
from_json
(
value
):
def
from_json
(
value
):
'''
"""
Returns native data structure from the given JSON string.
Returns native data structure from the given JSON string.
Examples:
Examples:
...
@@ -51,13 +57,14 @@ def from_json(value):
...
@@ -51,13 +57,14 @@ def from_json(value):
True
True
>>>
from_json
(
'
{
"
1
"
: {
"
a
"
: [1, 2, 3]}}
'
)
==
{
'
1
'
:
{
'
a
'
:
[
1
,
2
,
3
]}}
>>>
from_json
(
'
{
"
1
"
: {
"
a
"
: [1, 2, 3]}}
'
)
==
{
'
1
'
:
{
'
a
'
:
[
1
,
2
,
3
]}}
True
True
'''
"""
from
json
import
loads
from
json
import
loads
return
loads
(
value
)
return
loads
(
value
)
def
from_yaml
(
value
):
def
from_yaml
(
value
):
'''
"""
Returns native data structure from the given YAML string.
Returns native data structure from the given YAML string.
Examples:
Examples:
...
@@ -67,49 +74,52 @@ def from_yaml(value):
...
@@ -67,49 +74,52 @@ def from_yaml(value):
[
1
,
2
,
3
]
[
1
,
2
,
3
]
>>>
from_yaml
(
'
{
"
1
"
: {
"
a
"
: [1, 2, 3]}}
'
)
>>>
from_yaml
(
'
{
"
1
"
: {
"
a
"
: [1, 2, 3]}}
'
)
{
'
1
'
:
{
'
a
'
:
[
1
,
2
,
3
]}}
{
'
1
'
:
{
'
a
'
:
[
1
,
2
,
3
]}}
'''
"""
from
yaml
import
safe_load
from
yaml
import
safe_load
return
safe_load
(
value
)
return
safe_load
(
value
)
def
combine
(
default
,
override
):
def
combine
(
default
,
override
):
'''
"""
Returns a combined dictionary of the 2 dictionaries given (with the 2nd
Returns a combined dictionary of the 2 dictionaries given (with the 2nd
overriding the 1st).
overriding the 1st).
Examples:
Examples:
>>>
combine
({
'
a
'
:
1
,
'
b
'
:
2
},
{
'
b
'
:
3
,
'
c
'
:
4
})
==
{
'
a
'
:
1
,
'
b
'
:
3
,
'
c
'
:
4
}
>>>
combine
({
'
a
'
:
1
,
'
b
'
:
2
},
{
'
b
'
:
3
,
'
c
'
:
4
})
==
{
'
a
'
:
1
,
'
b
'
:
3
,
'
c
'
:
4
}
True
True
'''
"""
combined
=
default
.
copy
()
combined
=
default
.
copy
()
combined
.
update
(
override
)
combined
.
update
(
override
)
return
combined
return
combined
def
from_toml
(
value
):
def
from_toml
(
value
):
'''
"""
Returns a data structure from the TOML string given.
Returns a data structure from the TOML string given.
Examples:
Examples:
>>>
from_toml
(
'
[table]
\\
nkey =
"
value
"
\\
n
'
)
==
{
'
table
'
:
{
'
key
'
:
'
value
'
}}
>>>
from_toml
(
'
[table]
\\
nkey =
"
value
"
\\
n
'
)
==
{
'
table
'
:
{
'
key
'
:
'
value
'
}}
True
True
'''
"""
from
toml
import
loads
from
toml
import
loads
return
loads
(
value
)
return
loads
(
value
)
def
to_toml
(
value
):
def
to_toml
(
value
):
'''
"""
Returns a string of the TOML representation for the data structure given.
Returns a string of the TOML representation for the data structure given.
Examples:
Examples:
>>>
import
six
>>>
import
six
>>>
to_toml
({
'
key
'
:
[
1
,
2
]})
==
six
.
text_type
(
"
key = [ 1, 2,]
\\
n
"
)
>>>
to_toml
({
'
key
'
:
[
1
,
2
]})
==
six
.
text_type
(
"
key = [ 1, 2,]
\\
n
"
)
True
True
'''
"""
from
toml
import
dumps
from
toml
import
dumps
return
dumps
(
value
)
return
dumps
(
value
)
def
jmespath
(
value
,
query
):
def
jmespath
(
value
,
query
):
'''
"""
Queries the data using the JMESPath query language.
Queries the data using the JMESPath query language.
Examples:
Examples:
>>>
import
six
>>>
import
six
...
@@ -125,6 +135,7 @@ def jmespath(value, query):
...
@@ -125,6 +135,7 @@ def jmespath(value, query):
...
WACities
==
{
'
WashingtonCities
'
:
'
Bellevue, Olympia, Seattle
'
}
...
WACities
==
{
'
WashingtonCities
'
:
'
Bellevue, Olympia, Seattle
'
}
...
...
True
True
'''
"""
import
jmespath
import
jmespath
return
jmespath
.
search
(
query
,
value
)
return
jmespath
.
search
(
query
,
value
)
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