Skip to content

Commit

Permalink
Merge pull request #23 from aeu485/st_new_line_hook
Browse files Browse the repository at this point in the history
add new lines to the start and end of each structured text section
  • Loading branch information
ZLLentz authored Apr 10, 2024
2 parents 771de97 + 4e4bdff commit 3ef4f68
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
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
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
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

0 comments on commit 3ef4f68

Please sign in to comment.