Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
muppet-strings
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
Container 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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hugo Hörnquist
muppet-strings
Commits
66a1ea31
Commit
66a1ea31
authored
2 years ago
by
Hugo Hörnquist
Browse files
Options
Downloads
Patches
Plain Diff
Add template generation for Serializers.
parent
6b5c4286
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
muppet/puppet/ast.py
+104
-0
104 additions, 0 deletions
muppet/puppet/ast.py
with
104 additions
and
0 deletions
muppet/puppet/ast.py
+
104
−
0
View file @
66a1ea31
...
...
@@ -722,3 +722,107 @@ def build_ast(form: Any) -> Puppet:
case
default
:
logger
.
warning
(
"
Unhandled item: %a
"
,
default
)
return
PuppetParseError
(
default
)
def
generate_template
()
->
None
:
"""
Output code for a formater class.
Each formatter needs to implement a rather large number of
methods, and keeping track of them all is hard. This generates a
ready-to-use template.
.. code-block:: sh
python -m muppet.puppet.ast > muppet/puppet/format/NAME.py
"""
def
pyind
(
level
:
int
)
->
str
:
"""
Indent string for python code.
"""
return
'
'
*
4
*
level
subclasses
=
sorted
(
subclass
.
__name__
for
subclass
in
Puppet
.
__subclasses__
())
def
tokenize_class
(
s
:
str
)
->
list
[
str
]:
"""
Split a camel or pascal cased string into words.
"""
out
:
list
[
str
]
=
[]
current
:
str
=
''
for
c
in
s
:
if
c
.
isupper
():
if
current
.
isupper
():
current
+=
c
else
:
out
.
append
(
current
)
current
=
c
else
:
current
+=
c
out
.
append
(
current
)
if
out
[
0
]
==
''
:
out
=
out
[
1
:]
return
out
def
setup_override
()
->
None
:
print
(
'''
from typing import (
TypeVar,
Callable,
)
F = TypeVar(
'
F
'
, bound=Callable[..., object])
# TODO replace this decorator with
# from typing import override
# once the target python version is changed to 3.12
def override(f: F) -> F:
"""
Return function unchanged.
Placeholder @override annotator if the actual annotation isn
'
t
implemented in the current python version.
"""
return f
'''
.
lstrip
())
print
(
'''"""
__
TODOFormatter
TODO_RETURN_TYPE
"""'''
)
print
()
print
(
'
import logging
'
)
print
(
'
from .base import Serializer
'
)
print
(
'
from muppet.puppet.ast import (
'
)
for
subclass
in
subclasses
:
print
(
pyind
(
1
)
+
subclass
+
'
,
'
)
print
(
'
)
'
)
setup_override
()
print
()
print
()
print
(
'
logger = logging.getLogger(__name__)
'
)
print
()
print
()
print
(
'
class TODOFormatter(Serializer[TODO_RETURN_TYPE]):
'
)
print
(
pyind
(
1
)
+
'"""
TODO document me!!!
"""'
)
for
subclass
in
subclasses
:
func_name
=
'
_
'
.
join
(
tokenize_class
(
subclass
)).
lower
()
print
()
print
(
pyind
(
1
)
+
'
@override
'
)
print
(
pyind
(
1
)
+
'
@classmethod
'
)
decl
=
f
"
def _
{
func_name
}
(cls, it:
{
subclass
}
, indent: int) -> TODO_RETURN_TYPE:
"
print
(
pyind
(
1
)
+
decl
)
if
__name__
==
'
__main__
'
:
generate_template
()
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