Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DSI Parser Frontend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
DigitalDynamicMeasurement
DSI Parser Frontend
Commits
0a2753b6
Commit
0a2753b6
authored
8 months ago
by
Benedikt
Browse files
Options
Downloads
Patches
Plain Diff
starting with XML Unit validator
parent
af8c932e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
XMLUnitExtractor.py
+55
-0
55 additions, 0 deletions
XMLUnitExtractor.py
restAPIServer.py
+14
-0
14 additions, 0 deletions
restAPIServer.py
test_API.py
+37
-0
37 additions, 0 deletions
test_API.py
with
106 additions
and
0 deletions
XMLUnitExtractor.py
0 → 100644
+
55
−
0
View file @
0a2753b6
import
re
from
dsiUnits
import
dsiUnit
def
parse_plain_utf8_xml
(
xml_string
):
result
=
{}
# Regular expressions to match the required XML elements
unit_regex
=
re
.
compile
(
r
'
<si:unit>(.*?)</si:unit>
'
)
unit_xml_list_regex
=
re
.
compile
(
r
'
<si:unitXMLList>(.*?)</si:unitXMLList>
'
)
lines
=
xml_string
.
split
(
'
\n
'
)
for
line_num
,
line
in
enumerate
(
lines
,
1
):
# Check for si:unit elements
unit_match
=
unit_regex
.
search
(
line
)
if
unit_match
:
content
=
unit_match
.
group
(
1
).
strip
()
result
[
f
"
{
line_num
}
"
]
=
content
# Check for si:unitXMLList elements
unit_xml_list_match
=
unit_xml_list_regex
.
search
(
line
)
if
unit_xml_list_match
:
contents
=
unit_xml_list_match
.
group
(
1
).
strip
().
split
()
for
idx
,
content
in
enumerate
(
contents
):
result
[
f
"
{
line_num
}
:
{
idx
}
"
]
=
content
return
result
def
process_units
(
unit_dict
):
valid_units
=
{}
invalid_units
=
{}
for
key
,
value
in
unit_dict
.
items
():
try
:
unit
=
dsiUnit
(
value
)
if
unit
.
valid
:
valid_units
[
key
]
=
value
# Assuming you want to return the string value
else
:
invalid_units
[
key
]
=
value
print
(
f
"
Warning: Invalid unit at
{
key
}
with value:
{
value
}
"
)
except
Exception
as
e
:
print
(
f
"
Error processing unit at
{
key
}
with value:
{
value
}
. Error:
{
e
}
"
)
invalid_units
[
key
]
=
value
# Optionally store the raw value or create an error object
return
valid_units
,
invalid_units
def
parse_and_process
(
xml_string
):
unit_dict
=
parse_plain_utf8_xml
(
xml_string
)
valid_units
,
invalid_units
=
process_units
(
unit_dict
)
return
{
"
valid_units
"
:
valid_units
,
"
invalid_units
"
:
invalid_units
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
restAPIServer.py
+
14
−
0
View file @
0a2753b6
...
...
@@ -2,6 +2,8 @@ import numpy as np
from
fastapi
import
FastAPI
,
HTTPException
from
pydantic
import
BaseModel
from
dsiUnits
import
dsiUnit
import
re
import
XMLUnitExtractor
# Import the newly created module
app
=
FastAPI
()
class
UnitRequest
(
BaseModel
):
...
...
@@ -12,6 +14,10 @@ class UnitComparisonRequest(BaseModel):
unit_string2
:
str
complete
:
bool
=
False
class
XMLRequest
(
BaseModel
):
xml
:
str
@app.post
(
"
/convert/utf8/
"
)
async
def
convert_to_utf8
(
request
:
UnitRequest
):
try
:
...
...
@@ -54,3 +60,11 @@ async def compare_units(request: UnitComparisonRequest):
except
Exception
as
e
:
raise
HTTPException
(
status_code
=
500
,
detail
=
str
(
e
))
@app.post
(
"
/validateUnitsInXML/
"
)
async
def
parse_xml
(
request
:
XMLRequest
):
try
:
result
=
XMLUnitExtractor
.
parse_and_process
(
request
.
xml
)
return
result
except
Exception
as
e
:
raise
HTTPException
(
status_code
=
500
,
detail
=
str
(
e
))
This diff is collapsed.
Click to expand it.
test_API.py
+
37
−
0
View file @
0a2753b6
...
...
@@ -34,3 +34,40 @@ def test_compare_units_not_equal_but_equal_WithCompleate():
def
test_invalid_unit
():
response
=
client
.
post
(
"
/convert/utf8/
"
,
json
=
{
"
unit_string
"
:
"
not_a_unit
"
})
assert
response
.
status_code
==
500
# Assuming your API returns 500 for invalid units
def
test_parse_xml
():
xml_content
=
"""
<root>
<si:unit>\metre</si:unit>
<si:unitXMLList>\metre \second \kilogram</si:unitXMLList>
<si:unit>not_a_unit</si:unit>
<si:unit>\seconds</si:unit>
</root>
"""
response
=
client
.
post
(
"
/validateUnitsInXML/
"
,
json
=
{
"
xml
"
:
xml_content
})
assert
response
.
status_code
==
200
result
=
response
.
json
()
assert
"
valid_units
"
in
result
assert
"
invalid_units
"
in
result
valid_units
=
result
[
"
valid_units
"
]
invalid_units
=
result
[
"
invalid_units
"
]
# Check valid units
assert
"
2
"
in
valid_units
# Line number for <si:unit>\metre</si:unit>
assert
valid_units
[
"
2
"
]
==
"
\\
metre
"
assert
"
3:0
"
in
valid_units
# Line number and index for the first entry in <si:unitXMLList>
assert
valid_units
[
"
3:0
"
]
==
"
\\
metre
"
assert
"
3:1
"
in
valid_units
# Line number and index for the second entry in <si:unitXMLList>
assert
valid_units
[
"
3:1
"
]
==
"
\\
second
"
assert
"
3:2
"
in
valid_units
# Line number and index for the third entry in <si:unitXMLList>
assert
valid_units
[
"
3:2
"
]
==
"
\\
kilogram
"
# Check invalid units
assert
"
4
"
in
invalid_units
# Line number for <si:unit>not_a_unit</si:unit>
assert
invalid_units
[
"
4
"
]
==
"
not_a_unit
"
# Check invalid units
assert
"
5
"
in
invalid_units
# Line number for <si:unit>not_a_unit</si:unit>
assert
invalid_units
[
"
5
"
]
==
"
\seconds
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment