Skip to content

Commit f848d9e

Browse files
committed
more case insensitivity and no more : or ; in item names
1 parent 822fa0d commit f848d9e

4 files changed

Lines changed: 49 additions & 27 deletions

File tree

blark/plain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .input import (BlarkCompositeSourceItem, BlarkSourceItem,
99
register_input_handler)
1010
from .output import OutputBlock, register_output_handler
11-
from .util import AnyPath, SourceType, find_pou_type_and_identifier
11+
from .util import AnyPath, SourceType, find_pou_type_and_identifier_plain
1212

1313

1414
@dataclasses.dataclass
@@ -40,7 +40,7 @@ def load(
4040
with open(filename, "rt") as fp:
4141
contents = fp.read()
4242

43-
source_type, identifier = find_pou_type_and_identifier(contents)
43+
source_type, identifier = find_pou_type_and_identifier_plain(contents)
4444
# if source_type is None:
4545
# return []
4646
source_type = SourceType.general

blark/solution.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,7 @@ def from_xml(
351351
) -> Self:
352352
declaration = get_child_located_text(xml, "Declaration", filename=filename)
353353
if declaration is not None:
354-
source_type, identifier = util.find_pou_type_and_identifier(
355-
declaration.value
356-
)
354+
source_type, identifier = util.find_pou_type_and_identifier_xml(xml)
357355
else:
358356
source_type, identifier = None, None
359357

blark/summary.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class MethodSummary(Summary):
370370
declarations: Dict[str, DeclarationSummary] = field(default_factory=dict)
371371

372372
def __getitem__(self, key: str) -> DeclarationSummary:
373-
return self.declarations[key]
373+
return get_case_insensitive(self.declarations, key)
374374

375375
@property
376376
def declarations_by_block(self) -> Dict[str, Dict[str, DeclarationSummary]]:
@@ -415,7 +415,7 @@ class PropertyGetSetSummary(Summary):
415415
implementation_source: Optional[str] = None
416416

417417
def __getitem__(self, key: str) -> DeclarationSummary:
418-
return self.declarations[key]
418+
return get_case_insensitive(self.declarations, key)
419419

420420

421421
@dataclass
@@ -486,7 +486,7 @@ class FunctionSummary(Summary):
486486
declarations: Dict[str, DeclarationSummary] = field(default_factory=dict)
487487

488488
def __getitem__(self, key: str) -> DeclarationSummary:
489-
return self.declarations[key]
489+
return get_case_insensitive(self.declarations, key)
490490

491491
@property
492492
def declarations_by_block(self) -> Dict[str, Dict[str, DeclarationSummary]]:
@@ -540,11 +540,11 @@ class FunctionBlockSummary(Summary):
540540
def __getitem__(
541541
self, key: str
542542
) -> Union[DeclarationSummary, MethodSummary, PropertySummary, ActionSummary]:
543-
key = key.strip(":;")
544-
if key in self.declarations:
545-
return self.declarations[key]
543+
decl = get_case_insensitive(self.declarations, key)
544+
if decl is not None:
545+
return decl
546546
for item in self.actions + self.methods + self.properties:
547-
if item.name == key:
547+
if item.name.lower() == key.lower():
548548
return item
549549
raise KeyError(key)
550550

@@ -647,10 +647,11 @@ class InterfaceSummary(Summary):
647647
def __getitem__(
648648
self, key: str
649649
) -> Union[DeclarationSummary, MethodSummary, PropertySummary]:
650-
if key in self.declarations:
651-
return self.declarations[key]
652-
for item in self.methods + self.properties:
653-
if item.name == key:
650+
decl = get_case_insensitive(self.declarations, key)
651+
if decl is not None:
652+
return decl
653+
for item in self.actions + self.methods + self.properties:
654+
if item.name.lower() == key.lower():
654655
return item
655656
raise KeyError(key)
656657

@@ -744,7 +745,7 @@ class DataTypeSummary(Summary):
744745
declarations: Dict[str, DeclarationSummary] = field(default_factory=dict)
745746

746747
def __getitem__(self, key: str) -> DeclarationSummary:
747-
return self.declarations[key]
748+
return get_case_insensitive(self.declarations, key)
748749

749750
@property
750751
def declarations_by_block(self) -> Dict[str, Dict[str, DeclarationSummary]]:
@@ -853,7 +854,7 @@ class GlobalVariableSummary(Summary):
853854
declarations: Dict[str, DeclarationSummary] = field(default_factory=dict)
854855

855856
def __getitem__(self, key: str) -> DeclarationSummary:
856-
return self.declarations[key]
857+
return get_case_insensitive(self.declarations, key)
857858

858859
@property
859860
def declarations_by_block(self) -> Dict[str, Dict[str, DeclarationSummary]]:
@@ -908,10 +909,11 @@ class ProgramSummary(Summary):
908909
properties: List[PropertySummary] = field(default_factory=list)
909910

910911
def __getitem__(self, key: str) -> DeclarationSummary:
911-
if key in self.declarations:
912-
return self.declarations[key]
912+
decl = get_case_insensitive(self.declarations, key)
913+
if decl is not None:
914+
return decl
913915
for item in self.actions + self.methods + self.properties:
914-
if item.name == key:
916+
if item.name.lower() == key.lower():
915917
return item
916918
raise KeyError(key)
917919

@@ -1129,10 +1131,9 @@ def get_all_items_by_name(
11291131
self.data_types,
11301132
):
11311133
# Very inefficient, be warned
1132-
try:
1133-
yield dct[name]
1134-
except KeyError:
1135-
...
1134+
decl = get_case_insensitive(dct, name)
1135+
if decl is not None:
1136+
yield decl
11361137

11371138
def get_item_by_name(self, name: str) -> Optional[TopLevelCodeSummaryType]:
11381139
"""

blark/util.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,12 @@ def python_debug_session(namespace: Dict[str, Any], message: str):
224224
embed()
225225

226226

227-
def find_pou_type_and_identifier(code: str) -> tuple[Optional[SourceType], Optional[str]]:
227+
def find_pou_type_and_identifier_plain(code: str) -> tuple[Optional[SourceType], Optional[str]]:
228228
types = {source.name for source in SourceType}
229229
clean_code = remove_all_comments(code)
230230
for line in clean_code.splitlines():
231-
parts = line.lstrip().split()
231+
# split line on non-word and non-dot characters
232+
parts = re.split(r"[^\w.]+", line.lstrip())
232233
if parts and parts[0].lower() in types:
233234
source_type = SourceType[parts[0].lower()]
234235
identifier = None
@@ -247,6 +248,28 @@ def find_pou_type_and_identifier(code: str) -> tuple[Optional[SourceType], Optio
247248
return None, None
248249

249250

251+
def find_pou_type_and_identifier_xml(
252+
xml: lxml.etree.Element
253+
) -> tuple[Optional[SourceType], Optional[str]]:
254+
tag_source_type_map = {
255+
"Get": SourceType.property_get,
256+
"Set": SourceType.property_set,
257+
"Itf": SourceType.interface,
258+
"GVL": SourceType.var_global,
259+
}
260+
261+
if xml.tag in tag_source_type_map:
262+
source_type = tag_source_type_map[xml.tag]
263+
elif xml.tag == "POU":
264+
# POU may be function block or function (or maybe others)
265+
# so we figure it out the 'old fashioned' way
266+
source_type, _ = find_pou_type_and_identifier_plain(xml.xpath("Declaration")[0].text)
267+
else:
268+
source_type = SourceType[xml.tag.lower()]
269+
270+
return source_type, xml.attrib.get("Name")
271+
272+
250273
def remove_all_comments(text: str, *, replace_char: str = " ") -> str:
251274
"""
252275
Remove all comments and replace them with the provided character.

0 commit comments

Comments
 (0)