Repository navigation
hp_procurve_show_system: handle multi-word wrapped lines and Allow V2 Modules - #2371
Conversation
… Modules
Long System Contact/Location values wrap at column 78 onto indented
continuation lines that can contain several words, e.g.:
System Contact : Network Infrastructure Team (network-infrastructure@exa
mple.com) location Site-A Building-1 Floor-2 SW-ACCESS-
01
The previous swallow rule (^\s+\S+$$) only matched a wrapped line if that was
holding exactly one word and no spaces after the indentation,
so multi-word ones fell through to `^. -> Error`, making the whole parse failed.
Replaced it with `^\s+(?!.*\s:\s)\S.*$$` because every non wrapped line
has a "label : value" separator and continuation lines do
not, so the negative lookahead skips only wrapped text.
The new pattern works like this:
^ \s+ (?!.*\s:\s) \S.* $$
│ │ │ │ │
│ │ │ │ └─ end of line
│ │ │ └─ first non-space character, then anything after it
│ │ └─ negative lookahead check: there is NO "space colon space" anywhere further on the line
│ └─ one or more leading spaces (the line must be indented)
└─ start of line
Also accept "Allow V2 Modules" (seen on a real show system output) in addition to
"Allow V1 Modules".
Add hp_procurve_show_system4 fixture covering both cases.
Fixes networktocode#2370
I would have considered this a bug, not something to replicate. Is there any way to parse the entire contact instead? |
|
@joewesch fair point but, as far as I understood, TextFSM can't join text from different lines into one string value. The only "trick" I could find for multi-line values is by using Applied to this scenario, This introduces problems:
That's why I'd prefer to keep this PR as the minimal fix for the parse failure in #2370 (no output type change). |
Co-authored-by: Joe Wesch <10467633+joewesch@users.noreply.github.com>
Co-authored-by: Joe Wesch <10467633+joewesch@users.noreply.github.com>
| # swallow single-token wrap-continuation lines (long contact/location wrap at col 78) | ||
| ^\s+\S+$$ | ||
| # Skip wrapped Contact/Location continuation lines (no "label : value" separator) | ||
| ^\s{20,}(?!.*\s:\s)\S.*$$ |
There was a problem hiding this comment.
Help me to understand this better, having 20 spaces as a required item here, does that really make sense?
There was a problem hiding this comment.
@jvanderaa this was suggested by @joewesch:
This may be a bit too greedy. I see you rejected the hard-coded \s{23} as being fragile, but we can expect a minimum of 20 spaces at least with \s{20,} before we throw the line away.
matt852
left a comment
There was a problem hiding this comment.
One suggested change to capture the full multi-line contact value
| @@ -1,2 +1,2 @@ | |||
| Value Filldown NAME (\S+) | |||
| Value Filldown CONTACT (.+) | |||
There was a problem hiding this comment.
| Value Filldown CONTACT (.+) | |
| Value Filldown,List CONTACT_CONTINUED (\S.*?) |
Capture the full wrapped contact value. The new fixture stores contact as Network Infrastructure Team (network-infrastructure@exa, so the rest of the address is lost.
There was a problem hiding this comment.
| ^\s*System\s+Contact\s+:\s+${CONTACT} -> ContactContinued |
Same as above
There was a problem hiding this comment.
- since TextFSM stops at the first match, I think we should drop the first
^\s*System\s+Contact\s+:\s+${CONTACT}otherwise^\s*System\s+Contact\s+:\s+${CONTACT} -> Contactwould never be matched. - wouldn't be better to call the new
Contactstate something likeContactContinued? The state is entered after the firstcontactline is captured, so the state name should be really about capturing the possible wrapped part.
There was a problem hiding this comment.
@pastoreerrante Good points, and you're right on them. If just combine them into a single line:
^\s*System\s+Contact\s+:\s+${CONTACT} -> ContactContinued
that might work better.
| # Skip wrapped Contact/Location continuation lines (no "label : value" separator) | ||
| ^\s{20,}(?!.*\s:\s)\S.*$$ | ||
| ^. -> Error | ||
|
|
There was a problem hiding this comment.
| Contact | |
| ^\s{20,}${CONTACT_CONTINUED}\s*$$ | |
| ^\s*System\s+Location\s+:\s+${LOCATION} -> Start | |
| ^. -> Error | |
This should capture the full wrapped contact value along with my other 2 suggested changes.
There was a problem hiding this comment.
@matt852 thanks! so if I understood correctly, your suggestion is to have an additional field called CONTACT_CONTINUED to capture the wrapped contact lines. The template user should then join CONTACT and CONTACT_CONTINUED when needed. If that's correct, how does the user know about this possibility?
There was a problem hiding this comment.
@pastoreerrante Yeah that's what I had in mind, assuming the wrapped lines contains data that may need to be captured. Maybe something like this, with a comment added to line 48, and 49 updated to the ContactContinued suggestion from above in your other comment?
# Join CONTACT and CONTACT_CONTINUED if System Contact contains wrapped lines
ContactContinued
Or something to that effect. Thoughts?
There was a problem hiding this comment.
Applied in 6ff6953, thanks! As discussed, the new -> ContactContinued rule replaces the original System Contact rule. I placed CONTACT_CONTINUED last in the Value block so existing column positions don't shift for anyone reading raw TextFSM rows by index. All show_system fixtures regenerated so they only gain contact_continued.
Long System Contact values wrap at a fixed column (often mid-word) onto indented continuation lines which were previously skipped, so contact held only the first line. With this commit we now collect those lines in a new CONTACT_CONTINUED value (Filldown,List). The System Contact rule now switches to a ContactContinued state, which appends each wrapped line and returns to Start on System Location. CONTACT is unchanged, so existing consumers are unaffected. The full value is CONTACT + "".join(CONTACT_CONTINUED). The new Value is declared last so existing column positions are unchanged for consumers whom might be reading raw TextFSM rows by index. Wrapped System Location lines are still skipped. Regenerated the show_system fixtures as well so each record gains contact_continued (empty when the contact does not wrap). Suggested-by: matt852
ISSUE TYPE
COMPONENT
hp_procurve_show_system.textfsm— hp_procurve,show systemSUMMARY
Fixes #2370
Long
System Contact/System Locationlines wrap at column 78 onto indented continuation lines that can contain several words.The existing rule
^\s+\S+$$only matches continuation lines holding a single word (no spaces), so multi-word ones fell through to^. -> Errorand the whole parse fails.Changes:
Replace the
^\s+\S+$$rule with^\s+(?!.*\s:\s)\S.*$$. Every field line has alabel : valueseparator (whitespace, colon, whitespace) and continuation lines do not, so the negative lookahead skips only wrapped text. A hardcoded indentation width (e.g.^\s{23}) would have worked as well. It was considered but rejected as too fragile (due to the weird hardcoded integer).The new pattern works like this:
Accept
Allow V2 Modules(seen on a real output) in addition toAllow V1 Modules.Added
hp_procurve_show_system4fixture (sanitized real output) covering both cases.Once parsed, theWrapped contact lines are now captured incontactfield keeps only the first line discarding the wrapped continuation text. This is consistent with the existinghp_procurve_show_system3fixture.contact_continued(see 6ff6953).Tested locally with
invoke tests --local(black, flake8, pylint, yamllint, pydocstyle, bandit, pytest): all passed, 4857 tests.Input (excerpt of the new
hp_procurve_show_system4.rawfixture):Before (current
master): the whole parse fails.After (this PR): parses fine. The wrapped contact lines are collected in a new
contact_continuedlist.contactis unchanged. The full value iscontact + "".join(contact_continued)(lines wrap at a fixed column, often mid-word, so join without a separator).