Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update attachment validation to latest ruamel.yaml version #131

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions docassemble/ALDashboard/data/questions/validate_attachment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
include:
- nav.yml
---
modules:
- .validate_attachment
---
question: |
Paste the contents of a long attachment block
(beginning with the line `fields:`)
Expand All @@ -14,30 +17,7 @@ fields:
rows: 10
---
code: |
import ruamel.yaml as yaml
parsed_blocks = yaml.load(fields_statement)
---
#mandatory: True
question: |
${ parsed_blocks['fields'] }
---
code: |
import mako.template
import mako.runtime
import re

mako.runtime.UNDEFINED = DAEmpty()
from mako import exceptions
errors = []
empty_rows = []
for index, row in enumerate(parsed_blocks['fields']):
try:
mytemplate = mako.template.Template(next(iter(row.values())))
content = mytemplate.render()
except:
# Row in XLSX file is 1 indexed, and it has a header row
errors.append((f"Error on row {index}, id: {row}",exceptions.text_error_template().render()))
del mytemplate
errors = validate_attachment_block(fields_statement)
load_all_errors = True
---
need:
Expand All @@ -58,11 +38,4 @@ subquestion: |
```

% endfor
% endif

% if len(empty_rows):
#### ${ len(empty_rows) } untranslated strings found

Rows ${ comma_and_list(empty_rows) } have untranslated text.
% endif

% endif
22 changes: 22 additions & 0 deletions docassemble/ALDashboard/validate_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from docassemble.base.util import DAEmpty
import ruamel.yaml
import mako.template
import mako.runtime
mako.runtime.UNDEFINED = DAEmpty()
from mako import exceptions
from typing import List, Tuple

__all__ = ['validate_attachment_block']

def validate_attachment_block(fields_statement: str) -> List[Tuple[str,str]]:
yaml = ruamel.yaml.YAML(typ='rt')
parsed_blocks = yaml.load(fields_statement)

errors = []
for index, row in enumerate(parsed_blocks['fields']):
try:
mytemplate = mako.template.Template(next(iter(row.values())))
content = mytemplate.render()
except:
errors.append((f"Error on row {index}, id: {row}",exceptions.text_error_template().render()))
return errors
Loading