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

add new lines to the start and end of each structured text section #23

Merged
merged 6 commits into from
Apr 10, 2024
Merged
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
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@
entry: no-product-version
language: python
files: .*\.(TcPOU|TcDUT|TcGVL)$
- id: twincat-st-newline
name: TwinCAT ST Newline Formatter
description: Affixes newlines to ST segments
entry: twincat-st-newline
language: python
files: .*\.(TcPOU|TcGVL|TcDUT)$
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Pre-commit hooks for PCDS projects (https://pre-commit.com/)

If `.pre-config-config.yaml` does not already exist in the repository, copy
the appropriate file from this repository to the top-level of your local
repository, or add the folowing to an existing `.pre-config-config.yaml`
repository, or add the folowing to an existing `.pre-config-config.yaml`
file and commit the addition.

```yaml
Expand All @@ -25,13 +25,14 @@ repos:
files: \.(TcPOU|TcDUT|TcGVL)$

- repo: https://github.com/pcdshub/pre-commit-hooks.git
rev: v1.4.0
rev: v1.5.0
hooks:
- id: twincat-leading-tabs-remover
- id: twincat-lineids-remover
- id: twincat-xml-format
- id: check-fixed-library-versions
- id: no-product-version
- id: twincat-st-newline
# Optional, if you use pytmc to generate EPICS IOCs:
# - id: pytmc-pragma-linter
```
Expand Down
3 changes: 2 additions & 1 deletion forTwinCatRepos/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ repos:
files: \.(TcPOU|TcDUT|TcGVL)$

- repo: https://github.com/pcdshub/pre-commit-hooks.git
rev: v1.4.0
rev: v1.5.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for updating these too!

hooks:
- id: twincat-leading-tabs-remover
- id: twincat-lineids-remover
- id: twincat-xml-format
- id: check-fixed-library-versions
- id: no-product-version
- id: twincat-st-newline
# Optional, if you use pytmc to generate EPICS IOCs:
# - id: pytmc-pragma-linter
32 changes: 32 additions & 0 deletions pre_commit_hooks/twincat_st_new_lines.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be named twincat_st_newline.py for setup.py to automatically pick it up and provide it to pre-commit as a console script. (The entry names are generically mapped to module names).

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import argparse
from lxml import etree

def structured_text_formatter(path: str, lines: int = 1) -> None:
root = etree.parse(path).getroot()
sect = root.xpath('.//Declaration|.//Implementation/ST')
if len(sect) == 0:
return

newlines = '\n' * lines
for section in sect:
try:
section.text = etree.CDATA(newlines + section.text.strip('\n') + newlines)
except AttributeError:
pass
etree.ElementTree(root).write(path, encoding='utf-8', xml_declaration=True)

def main():
parser = argparse.ArgumentParser()
parser.add_argument('--lines', type=int, default=1)
parser.add_argument('files', nargs='*')
args = parser.parse_args()

try:
for file in args.files:
structured_text_formatter(file, args.lines)
except:
return 1


if __name__ == "__main__":
exit(main())
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'xml-format',
'check-fixed-library-versions',
'no-product-version',
'twincat-st-newline',
]
console_scripts = []
for name in hook_names:
Expand Down