Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F86470109
show_highlighted_svg_file.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Oct 6, 16:39
Size
3 KB
Mime Type
text/x-python
Expires
Tue, Oct 8, 16:39 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21394566
Attached To
rNIETZSCHEPYTHON nietzsche-python
show_highlighted_svg_file.py
View Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This program can be used to show a svg file(s) with highlighted words.
"""
# Copyright (C) University of Basel 2019 {{{1
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/> 1}}}
import
getopt
from
progress.bar
import
Bar
import
os
from
os
import
listdir
,
sep
,
path
,
setpgrp
,
devnull
,
makedirs
from
os.path
import
basename
,
commonpath
,
dirname
,
exists
,
isfile
,
isdir
,
join
,
realpath
,
splitext
import
lxml.etree
as
ET
import
sys
import
tempfile
if
dirname
(
__file__
)
not
in
sys
.
path
:
sys
.
path
.
append
(
dirname
(
__file__
))
from
util
import
create_highlighted_svg_file
,
ExternalViewer
__author__
=
"Christian Steiner"
__maintainer__
=
__author__
__copyright__
=
'University of Basel'
__email__
=
"christian.steiner@unibas.ch"
__status__
=
"Development"
__license__
=
"GPL v3"
__version__
=
"0.0.1"
UNITTESTING
=
False
def
get_node_ids4word
(
faksimile_tree
,
word
,
namespaces
=
None
)
->
list
:
"""Return a list of node ids for word.
"""
if
namespaces
is
None
:
namespaces
=
{
k
if
k
is
not
None
else
'ns'
:
v
for
k
,
v
in
faksimile_tree
.
getroot
()
.
nsmap
.
items
()
}
return
[
item
.
getparent
()
.
get
(
'id'
)
for
item
in
faksimile_tree
.
xpath
(
f
'//ns:rect/ns:title[text() = "{word}"]'
,
namespaces
=
namespaces
)
]
def
usage
():
"""prints information on how to use the script
"""
if
__package__
is
not
None
:
print
(
main
.
__doc__
.
format
(
package
=
__package__
,
file
=
basename
(
__file__
)))
else
:
print
(
main
.
__doc__
.
format
(
package
=
dirname
(
__file__
),
file
=
basename
(
__file__
)))
def
main
(
argv
):
"""This program can be used to copy a svg file(s) to a target directory, and also updating its image-path.
{package}/{file} [OPTIONS] <faksimile_svg_file> <word> [<word1> <word2> ...]
<faksimile_svg_file> a svg file containing information about the word positions on the faksimile.
<word> word that should be highlighted.
OPTIONS:
-h|--help: show help
:return: exit code (int)
"""
try
:
opts
,
args
=
getopt
.
getopt
(
argv
,
"h"
,
[
"help"
])
except
getopt
.
GetoptError
:
usage
()
return
2
for
opt
,
arg
in
opts
:
if
opt
in
(
'-h'
,
'--help'
):
usage
()
return
0
if
len
(
args
)
<
2
:
usage
()
return
2
exit_status
=
0
messages
=
[]
svg_file
=
args
[
0
]
target_file
=
join
(
tempfile
.
mkdtemp
(),
basename
(
svg_file
))
if
exists
(
svg_file
):
if
not
UNITTESTING
:
print
(
f
'Showing {basename(svg_file)} with highlighted words {[text for text in args[1:]]}'
)
faksimile_tree
=
ET
.
parse
(
svg_file
)
namespaces
=
{
k
if
k
is
not
None
else
'ns'
:
v
for
k
,
v
in
faksimile_tree
.
getroot
()
.
nsmap
.
items
()
}
node_ids
=
[]
for
word
in
args
[
1
:]:
node_ids
+=
get_node_ids4word
(
faksimile_tree
,
word
,
namespaces
=
namespaces
)
create_highlighted_svg_file
(
faksimile_tree
,
node_ids
,
target_file
=
target_file
)
ExternalViewer
.
show_files
(
target_file
)
else
:
raise
FileNotFoundError
(
f
'File {svg_file} does not exist!'
)
return
exit_status
if
__name__
==
"__main__"
:
sys
.
exit
(
main
(
sys
.
argv
[
1
:]))
Event Timeline
Log In to Comment