Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Macao Legacy
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
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MACAO
Macao Legacy
Commits
33eb15de
Commit
33eb15de
authored
1 year ago
by
Eliott Sammier
Browse files
Options
Downloads
Patches
Plain Diff
Parse QCU correct answers with Xpath and Regex
parent
25f3425a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tetras_extraction/macao_12/script/extract_page.py
+34
-14
34 additions, 14 deletions
tetras_extraction/macao_12/script/extract_page.py
with
34 additions
and
14 deletions
tetras_extraction/macao_12/script/extract_page.py
+
34
−
14
View file @
33eb15de
...
...
@@ -50,14 +50,12 @@ class RegexParser:
raise
ParseError
(
"
Failed to find function
'
entrerDonnees
'"
)
body
=
func_split
[
1
]
activity_type
,
activity_var_name
=
self
.
parse_activity_constructor
(
body
)
print
(
activity_type
,
file
=
output
)
activity_type
,
activity_var_name
=
self
.
_parse_activity_constructor
(
body
)
print
(
activity_type
,
end
=
""
,
file
=
output
)
if
activity_type
==
"
ExerciceQC_QCU
"
:
print
(
"
"
,
self
.
_parse_qcu_answers
(
body
),
end
=
""
,
file
=
output
)
for
line
in
body
.
splitlines
():
line
=
line
.
strip
()
pass
def
parse_activity_constructor
(
self
,
code
:
str
)
->
tuple
[
str
,
str
]:
def
_parse_activity_constructor
(
self
,
code
:
str
)
->
tuple
[
str
,
str
]:
"""
Find activity constructor call, return the activity type
and resulting variable name.
...
...
@@ -80,6 +78,24 @@ class RegexParser:
act_type
+=
"
_
"
+
args
.
replace
(
'"'
,
""
)
return
act_type
,
var_name
def
_parse_qcu_answers
(
self
,
code
:
str
)
->
list
[
bool
]:
"""
Parse the correct answers for a QCU activity, as a list of booleans
"""
correct_choices
=
[]
index
=
0
for
line
in
code
.
splitlines
():
line
=
line
.
strip
()
m
=
re
.
match
(
r
"
var nr = (\d+);
"
,
line
)
if
m
is
not
None
:
# "index" line
index
=
int
(
m
.
group
(
1
))
elif
line
==
"
exo.tabStylesR[nr] = CODE_F;
"
:
# "incorrect answer" line
insert_grow
(
correct_choices
,
index
,
False
,
fill_value
=
False
)
elif
line
==
"
exo.tabStylesR[nr] = CODE_V;
"
:
# "correct answer" line
insert_grow
(
correct_choices
,
index
,
True
,
fill_value
=
False
)
return
correct_choices
def
__str__
(
self
)
->
str
:
return
"
RegexParser
"
...
...
@@ -108,8 +124,10 @@ class XpathParser:
xml
=
self
.
to_xml
(
jstree
.
toDict
(),
"
jstree
"
)
try
:
self
.
fun
=
self
.
request_function
(
xml
)[
0
]
print
(
self
.
_parse_activity_type
(),
file
=
output
)
# self._parse_qcu_choices()
act_type
=
self
.
_parse_activity_type
()
print
(
act_type
,
end
=
""
,
file
=
output
)
if
act_type
==
"
ExerciceQC_QCU
"
:
print
(
"
"
,
self
.
_parse_qcu_answers
(),
end
=
""
,
file
=
output
)
except
Exception
as
e
:
raise
ParseError
(
e
)
...
...
@@ -124,18 +142,20 @@ class XpathParser:
case
other
:
return
other
def
_parse_qcu_choices
(
self
):
def
_parse_qcu_answers
(
self
)
->
list
[
bool
]:
"""
Parse the correct answers for a QCU activity, as a list of booleans
"""
indexes_and_values
=
self
.
request_index_and_values
(
self
.
fun
)
correct_choices
=
[]
index
=
0
for
e
in
indexes_and_values
:
value
=
e
.
xpath
(
"
@value
"
)
if
len
(
value
)
!=
0
:
# "index line"
index
=
value
[
0
]
index
=
int
(
value
[
0
]
)
else
:
# "true line"
insert_grow
(
correct_choices
,
index
,
True
,
fill_value
=
False
)
print
(
correct_choices
)
return
correct_choices
def
to_xml
(
self
,
obj
,
tag_name
:
Optional
[
str
]
=
None
):
"""
Recursively convert an object structure to an XML `ElementTree`.
...
...
@@ -242,7 +262,7 @@ class MatchParser:
pass
def
print
(
self
,
s
:
str
):
print
(
s
,
file
=
self
.
output
)
print
(
s
,
end
=
""
,
file
=
self
.
output
)
def
match_function
(
self
,
func
:
dict
):
"""
Checks if `func` matches a function declaration named `entrerDonnees`,
...
...
@@ -301,7 +321,7 @@ def parse_page(graph: Graph, filepath: str, id: str):
# Try different parsers, each writing to a different file to compare their results
for
parser
in
[
XpathParser
(),
MatchParser
(
graph
,
id
),
RegexParser
()]:
with
open
(
f
"
/tmp/
{
str
(
parser
)
}
.txt
"
,
"
a
"
)
as
f
:
print
(
f
"
{
id
:
8
}
"
,
end
=
""
,
file
=
f
)
print
(
f
"
\n
{
id
:
8
}
"
,
end
=
""
,
file
=
f
)
try
:
parser
.
parse
(
js
,
output
=
f
)
except
ParseError
as
e
:
...
...
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