Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor 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
calibration
pycalibration
Commits
9d9a7ef4
Commit
9d9a7ef4
authored
3 years ago
by
Thomas Kluyver
Browse files
Options
Downloads
Patches
Plain Diff
Refactor serve_overview, separating code paths into methods
parent
920ab5f2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!593
Refactor serve_overview code a bit
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
webservice/serve_overview.py
+91
-80
91 additions, 80 deletions
webservice/serve_overview.py
with
91 additions
and
80 deletions
webservice/serve_overview.py
+
91
−
80
View file @
9d9a7ef4
...
...
@@ -6,6 +6,7 @@ from collections import OrderedDict
from
datetime
import
datetime
,
timezone
from
http.server
import
BaseHTTPRequestHandler
,
HTTPServer
from
pathlib
import
Path
from
shutil
import
copyfileobj
from
subprocess
import
check_output
from
typing
import
Optional
...
...
@@ -45,9 +46,6 @@ class RequestHandler(BaseHTTPRequestHandler):
conf_was_init
=
False
def
init_config
(
self
):
global
cal_config
self
.
total_jobs_cmd
=
config
[
"
shell-commands
"
][
"
total-jobs
"
]
self
.
tail_log_cmd
=
config
[
"
shell-commands
"
][
"
tail-log
"
]
self
.
cat_log_cmd
=
config
[
"
shell-commands
"
][
"
cat-log
"
]
...
...
@@ -57,98 +55,111 @@ class RequestHandler(BaseHTTPRequestHandler):
for
template
,
tfile
in
config
[
"
templates
"
].
items
():
with
open
(
tfile
,
"
r
"
)
as
tf
:
self
.
templates
[
template
]
=
tf
.
read
()
global
pdf_queue
self
.
pdf_queue
=
pdf_queue
self
.
conf_was_init
=
True
def
serve_css
(
self
):
"""
Serve /serve_overview.css
"""
self
.
send_response
(
200
)
self
.
send_header
(
'
Content-type
'
,
'
text/css
'
)
self
.
end_headers
()
self
.
wfile
.
write
(
self
.
templates
[
"
css
"
].
encode
(
'
utf-8
'
))
def
serve_pdf
(
self
):
"""
Serve pdf?id
"""
puuid
=
self
.
path
.
split
(
"
?
"
)[
1
]
fpath
=
self
.
pdf_queue
.
get
(
puuid
,
None
)
if
fpath
is
None
:
return
self
.
send_error
(
404
)
self
.
send_response
(
200
)
self
.
send_header
(
'
Content-type
'
,
'
application/pdf
'
)
self
.
send_header
(
'
Content-Length
'
,
str
(
os
.
stat
(
fpath
).
st_size
))
self
.
end_headers
()
with
open
(
fpath
,
"
rb
"
)
as
f
:
copyfileobj
(
f
,
self
.
wfile
)
def
serve_dark_overview
(
self
):
# Send headers
self
.
send_response
(
200
)
self
.
send_header
(
'
Content-type
'
,
'
text/html
'
)
self
.
end_headers
()
host
=
config
[
"
server-config
"
][
"
host
"
]
port
=
config
[
"
server-config
"
][
"
port
"
]
reports
=
{}
for
instrument
,
detectors
in
cal_config
[
'
dark
'
].
items
():
reports
[
instrument
]
=
{}
for
detector
in
detectors
:
tmpl
=
f
'
/gpfs/exfel/d/cal/caldb_store/xfel/reports/
{
instrument
}
/
{
detector
}
/dark/*pdf
'
files
=
glob
.
glob
(
tmpl
)
files
.
sort
(
key
=
os
.
path
.
getmtime
,
reverse
=
True
)
file_info
=
[]
for
i
,
file
in
enumerate
(
files
):
if
(
len
(
file_info
)
%
2
)
==
0
:
bgcolor
=
'
EEEEEE
'
else
:
bgcolor
=
'
FFFFFF
'
time
=
os
.
stat
(
file
).
st_mtime
d_time
=
datetime
.
fromtimestamp
(
time
).
replace
(
tzinfo
=
timezone
.
utc
)
s_time
=
d_time
.
strftime
(
'
%y-%m-%d %H:%M
'
)
file_info
.
append
([
file
,
s_time
,
bgcolor
])
reports
[
instrument
][
detector
]
=
file_info
tmpl
=
Template
(
self
.
templates
[
"
dark-overview
"
])
message
=
tmpl
.
render
(
reports
=
reports
,
host
=
host
,
port
=
port
)
self
.
wfile
.
write
(
bytes
(
message
,
"
utf8
"
))
def
serve_file_from_gpfs
(
self
):
"""
Serve a file from a path starting with /gpfs
"""
if
self
.
path
.
endswith
(
"
.html
"
):
mimetype
=
'
text/html
'
elif
self
.
path
.
endswith
(
"
.jpg
"
):
mimetype
=
'
image/jpg
'
elif
self
.
path
.
endswith
(
"
.gif
"
):
mimetype
=
'
image/gif
'
elif
self
.
path
.
endswith
(
"
.png
"
):
mimetype
=
'
image/png
'
elif
self
.
path
.
endswith
(
"
.pdf
"
):
mimetype
=
'
application/pdf
'
elif
self
.
path
.
endswith
(
"
.js
"
):
mimetype
=
'
application/javascript
'
elif
self
.
path
.
endswith
(
"
.css
"
):
mimetype
=
'
text/css
'
else
:
return
self
.
send_error
(
404
)
if
os
.
path
.
isfile
(
self
.
path
):
self
.
send_response
(
200
)
self
.
send_header
(
'
Content-Length
'
,
str
(
os
.
stat
(
self
.
path
).
st_size
))
self
.
send_header
(
'
Content-type
'
,
mimetype
)
self
.
end_headers
()
with
open
(
self
.
path
,
"
rb
"
)
as
f
:
copyfileobj
(
f
,
self
.
wfile
)
else
:
self
.
send_error
(
404
)
def
do_GET
(
self
):
if
not
self
.
conf_was_init
:
self
.
init_config
()
# Send response status code
self
.
send_response
(
200
)
if
"
/serve_overview.css
"
in
self
.
path
:
self
.
send_header
(
'
Content-type
'
,
'
text/css
'
)
self
.
end_headers
()
for
s
in
self
.
templates
[
"
css
"
].
split
(
"
\n
"
):
self
.
wfile
.
write
(
s
.
encode
())
return
return
self
.
serve_css
()
if
"
pdf?
"
in
self
.
path
:
puuid
=
self
.
path
.
split
(
"
?
"
)[
1
]
fpath
=
self
.
pdf_queue
.
get
(
puuid
,
None
)
if
fpath
is
None
:
return
self
.
send_header
(
'
Content-type
'
,
'
application/pdf
'
)
self
.
end_headers
()
with
open
(
fpath
,
"
rb
"
)
as
f
:
self
.
wfile
.
write
(
f
.
read
())
return
return
self
.
serve_pdf
()
if
"
dark?
"
in
self
.
path
:
# Send headers
self
.
send_header
(
'
Content-type
'
,
'
text/html
'
)
self
.
end_headers
()
host
=
config
[
"
server-config
"
][
"
host
"
]
port
=
config
[
"
server-config
"
][
"
port
"
]
reports
=
{}
for
instrument
,
detectors
in
cal_config
[
'
dark
'
].
items
():
reports
[
instrument
]
=
{}
for
detector
in
detectors
:
tmpl
=
f
'
/gpfs/exfel/d/cal/caldb_store/xfel/reports/
{
instrument
}
/
{
detector
}
/dark/*pdf
'
files
=
glob
.
glob
(
tmpl
)
files
.
sort
(
key
=
os
.
path
.
getmtime
,
reverse
=
True
)
file_info
=
[]
for
i
,
file
in
enumerate
(
files
):
if
(
len
(
file_info
)
%
2
)
==
0
:
bgcolor
=
'
EEEEEE
'
else
:
bgcolor
=
'
FFFFFF
'
time
=
os
.
stat
(
file
).
st_mtime
d_time
=
datetime
.
fromtimestamp
(
time
).
replace
(
tzinfo
=
timezone
.
utc
)
s_time
=
d_time
.
strftime
(
'
%y-%m-%d %H:%M
'
)
file_info
.
append
([
file
,
s_time
,
bgcolor
])
reports
[
instrument
][
detector
]
=
file_info
tmpl
=
Template
(
self
.
templates
[
"
dark-overview
"
])
message
=
tmpl
.
render
(
reports
=
reports
,
host
=
host
,
port
=
port
)
self
.
wfile
.
write
(
bytes
(
message
,
"
utf8
"
))
return
return
self
.
serve_dark_overview
()
if
"
/gpfs
"
in
self
.
path
:
sendReply
=
False
if
self
.
path
.
endswith
(
"
.html
"
):
mimetype
=
'
text/html
'
sendReply
=
True
if
self
.
path
.
endswith
(
"
.jpg
"
):
mimetype
=
'
image/jpg
'
sendReply
=
True
if
self
.
path
.
endswith
(
"
.gif
"
):
mimetype
=
'
image/gif
'
sendReply
=
True
if
self
.
path
.
endswith
(
"
.png
"
):
mimetype
=
'
image/png
'
sendReply
=
True
if
self
.
path
.
endswith
(
"
.pdf
"
):
mimetype
=
'
application/pdf
'
sendReply
=
True
if
self
.
path
.
endswith
(
"
.js
"
):
mimetype
=
'
application/javascript
'
sendReply
=
True
if
self
.
path
.
endswith
(
"
.css
"
):
mimetype
=
'
text/css
'
sendReply
=
True
if
sendReply
==
True
and
os
.
path
.
isfile
(
self
.
path
):
with
open
(
self
.
path
,
"
rb
"
)
as
f
:
self
.
send_header
(
'
Content-type
'
,
mimetype
)
self
.
end_headers
()
self
.
wfile
.
write
(
f
.
read
())
return
return
self
.
serve_file_from_gpfs
()
# Send response status code
self
.
send_response
(
200
)
# Send headers
self
.
send_header
(
'
Content-type
'
,
'
text/html
'
)
...
...
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