Skip to content

Commit 3ef4f68

Browse files
authored
Merge pull request #23 from aeu485/st_new_line_hook
add new lines to the start and end of each structured text section
2 parents 771de97 + 4e4bdff commit 3ef4f68

5 files changed

Lines changed: 44 additions & 3 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@
4646
entry: no-product-version
4747
language: python
4848
files: .*\.(TcPOU|TcDUT|TcGVL)$
49+
- id: twincat-st-newline
50+
name: TwinCAT ST Newline Formatter
51+
description: Affixes newlines to ST segments
52+
entry: twincat-st-newline
53+
language: python
54+
files: .*\.(TcPOU|TcGVL|TcDUT)$

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Pre-commit hooks for PCDS projects (https://pre-commit.com/)
1212

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

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

2727
- repo: https://github.com/pcdshub/pre-commit-hooks.git
28-
rev: v1.4.0
28+
rev: v1.5.0
2929
hooks:
3030
- id: twincat-leading-tabs-remover
3131
- id: twincat-lineids-remover
3232
- id: twincat-xml-format
3333
- id: check-fixed-library-versions
3434
- id: no-product-version
35+
- id: twincat-st-newline
3536
# Optional, if you use pytmc to generate EPICS IOCs:
3637
# - id: pytmc-pragma-linter
3738
```

forTwinCatRepos/.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ repos:
99
files: \.(TcPOU|TcDUT|TcGVL)$
1010

1111
- repo: https://github.com/pcdshub/pre-commit-hooks.git
12-
rev: v1.4.0
12+
rev: v1.5.0
1313
hooks:
1414
- id: twincat-leading-tabs-remover
1515
- id: twincat-lineids-remover
1616
- id: twincat-xml-format
1717
- id: check-fixed-library-versions
1818
- id: no-product-version
19+
- id: twincat-st-newline
1920
# Optional, if you use pytmc to generate EPICS IOCs:
2021
# - id: pytmc-pragma-linter
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import argparse
2+
from lxml import etree
3+
4+
def structured_text_formatter(path: str, lines: int = 1) -> None:
5+
root = etree.parse(path).getroot()
6+
sect = root.xpath('.//Declaration|.//Implementation/ST')
7+
if len(sect) == 0:
8+
return
9+
10+
newlines = '\n' * lines
11+
for section in sect:
12+
try:
13+
section.text = etree.CDATA(newlines + section.text.strip('\n') + newlines)
14+
except AttributeError:
15+
pass
16+
etree.ElementTree(root).write(path, encoding='utf-8', xml_declaration=True)
17+
18+
def main():
19+
parser = argparse.ArgumentParser()
20+
parser.add_argument('--lines', type=int, default=1)
21+
parser.add_argument('files', nargs='*')
22+
args = parser.parse_args()
23+
24+
try:
25+
for file in args.files:
26+
structured_text_formatter(file, args.lines)
27+
except:
28+
return 1
29+
30+
31+
if __name__ == "__main__":
32+
exit(main())

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'xml-format',
1111
'check-fixed-library-versions',
1212
'no-product-version',
13+
'twincat-st-newline',
1314
]
1415
console_scripts = []
1516
for name in hook_names:

0 commit comments

Comments
 (0)