Skip to content

Commit 85fa86d

Browse files
authored
Merge pull request #293 from klauer/ref_sort_by_tcname
REF: sort generated records by TwinCAT symbol name (tcname)
2 parents a583d16 + 35cceb1 commit 85fa86d

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

pytmc/bin/db.py

+44-9
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import pathlib
1111
import sys
1212
from collections import defaultdict
13+
from typing import List, Optional, Tuple
1314

1415
from .. import linter, parser
1516
from ..pragmas import find_pytmc_symbols, record_packages_from_symbol
16-
from ..record import generate_archive_settings
17+
from ..record import RecordPackage, generate_archive_settings
1718

1819
logger = logging.getLogger(__name__)
1920
DESCRIPTION = __doc__
@@ -74,21 +75,45 @@ def validate_with_dbd(packages, dbd_file, remove_invalid_fields=True,
7475
return results, db_text
7576

7677

77-
def process(tmc, *, dbd_file=None, allow_errors=False, show_error_context=True,
78-
allow_no_pragma=False, debug=False):
78+
def process(
79+
tmc: parser.TcModuleClass,
80+
*,
81+
dbd_file: Optional[parser.AnyPath] = None,
82+
allow_errors: bool = False,
83+
show_error_context: bool = True,
84+
allow_no_pragma: bool = False,
85+
debug: bool = False
86+
) -> Tuple[List[RecordPackage], List[Exception]]:
7987
'''
8088
Process a TMC
8189
8290
Parameters
8391
----------
8492
tmc : TcModuleClass
93+
The parsed TMC file instance.
94+
dbd_file : str or DbdFile, optional
95+
The dbd file with which to validate
96+
allow_errors : bool, optional
97+
Allow processing to continue even with errors.
98+
show_error_context : bool, optional
99+
Show context from the database file around the error.
100+
allow_no_pragma : bool, optional
101+
Allow intermediate objects to not have a pytmc pragma and still regard
102+
it as valid, assuming at least the first and last item have pragmas.
103+
debug : bool, optional
104+
Debug mode.
85105
86106
Returns
87107
-------
88108
records : list
89109
List of RecordPackage
90110
exceptions : list
91111
List of exceptions raised during parsing
112+
113+
Raises
114+
------
115+
LinterError
116+
If ``allow_errors`` is unset and an error is found.
92117
'''
93118
def _show_context_from_line(rendered, from_line):
94119
lines = list(enumerate(rendered.splitlines()[:from_line + 1], 1))
@@ -107,8 +132,8 @@ def _show_context_from_line(rendered, from_line):
107132
record
108133
for symbol in find_pytmc_symbols(tmc, allow_no_pragma=allow_no_pragma)
109134
for record in record_packages_from_symbol(
110-
symbol, yield_exceptions=not debug,
111-
allow_no_pragma=allow_no_pragma)
135+
symbol, yield_exceptions=not debug, allow_no_pragma=allow_no_pragma
136+
)
112137
]
113138

114139
exceptions = [ex for ex in records
@@ -121,10 +146,20 @@ def _show_context_from_line(rendered, from_line):
121146
if exceptions and not allow_errors:
122147
raise LinterError('Failed to create database')
123148

124-
record_names = [single_record.pvname
125-
for record_package in records if record_package.valid
126-
for single_record in record_package.records
127-
]
149+
# We removed exceptions from the record list above:
150+
records: List[RecordPackage]
151+
152+
def by_tcname(record: RecordPackage):
153+
return record.tcname
154+
155+
records.sort(key=by_tcname)
156+
157+
record_names = [
158+
single_record.pvname
159+
for record_package in records
160+
if record_package.valid
161+
for single_record in record_package.records
162+
]
128163

129164
if len(record_names) != len(set(record_names)):
130165
dupes = {name: record_names.count(name)

0 commit comments

Comments
 (0)