Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OSUG
RESIF
ws-sacpz-resp
Commits
37ee72a2
Commit
37ee72a2
authored
Jul 08, 2020
by
Jerome Touvier
Browse files
Merge branch 'refactoring' into 'master'
code refactoring See merge request
!2
parents
61f89d87
eef664fc
Changes
11
Hide whitespace changes
Inline
Side-by-side
apps/constants.py
View file @
37ee72a2
VERSION
=
"1.0.1"
VERSION
=
"1.0.0"
parameters
=
{
"network"
:
None
,
...
...
@@ -27,7 +26,3 @@ ALIAS = [
(
"starttime"
,
"start"
),
(
"endtime"
,
"end"
),
]
BOOLEANS
=
[]
FLOATS
=
[]
NOT_NONE
=
[]
apps/
resp/
output.py
→
apps/output.py
View file @
37ee72a2
...
...
@@ -12,14 +12,15 @@ from obspy.io.xseed.parser import Parser
from
apps.globals
import
Error
from
apps.globals
import
FDSN_CLIENT
from
apps.globals
import
STATIONXML_CONVERTER
from
apps.globals
import
USER_AGENT_RESP
from
apps.globals
import
USER_AGENT_SACPZ
from
apps.utils
import
error_500
from
apps.utils
import
error_request
from
apps.utils
import
tictac
def
get_inventory
(
params
):
client
=
Client
(
FDSN_CLIENT
,
user_agent
=
USER_AGENT_SACPZ
)
def
get_inventory
(
params
,
user_agent
):
client
=
Client
(
FDSN_CLIENT
,
user_agent
=
user_agent
)
start
=
params
[
"time"
]
if
params
[
"time"
]
is
not
None
else
params
[
"starttime"
]
end
=
params
[
"time"
]
if
params
[
"time"
]
is
not
None
else
params
[
"endtime"
]
return
client
.
get_stations
(
...
...
@@ -33,6 +34,42 @@ def get_inventory(params):
)
def
get_sacpz
(
params
):
""" Builds the ws-sacpz response.
params: parameters """
try
:
tic0
=
time
.
time
()
response
=
None
cid
=
f
"
{
params
[
'network'
]
}
.
{
params
[
'station'
]
}
.
{
params
[
'location'
]
}
.
{
params
[
'channel'
]
}
"
start
=
(
params
[
"starttime"
]
if
params
[
"starttime"
]
is
not
None
else
params
[
"time"
]
)
fcid
=
cid
+
"."
+
str
(
start
).
replace
(
" "
,
"T"
)
logging
.
debug
(
fcid
)
try
:
inventory
=
get_inventory
(
params
,
USER_AGENT_SACPZ
)
except
Exception
as
ex
:
logging
.
debug
(
str
(
ex
))
code
=
int
(
params
[
"nodata"
])
return
error_request
(
msg
=
f
"HTTP._
{
code
}
_"
,
details
=
Error
.
NODATA
,
code
=
code
)
data
=
StringIO
()
inventory
.
write
(
data
,
format
=
"SACPZ"
)
headers
=
{
"Content-type"
:
"text/plain ; charset=utf-8"
}
response
=
make_response
(
data
.
getvalue
(),
headers
)
return
response
except
Exception
as
ex
:
logging
.
exception
(
str
(
ex
))
return
error_500
(
Error
.
UNSPECIFIED
)
finally
:
if
response
:
bytes
=
response
.
headers
.
get
(
"Content-Length"
)
logging
.
info
(
f
"
{
bytes
}
bytes rendered in
{
tictac
(
tic0
)
}
seconds."
)
def
get_resp
(
params
):
""" Builds the ws-resp response.
params: parameters """
...
...
@@ -49,7 +86,7 @@ def get_resp(params):
logging
.
debug
(
fcid
)
try
:
inventory
=
get_inventory
(
params
)
inventory
=
get_inventory
(
params
,
USER_AGENT_RESP
)
except
Exception
as
ex
:
logging
.
debug
(
str
(
ex
))
code
=
int
(
params
[
"nodata"
])
...
...
apps/root.py
View file @
37ee72a2
import
logging
import
queue
import
re
from
copy
import
copy
from
multiprocessing
import
Process
,
Queue
from
apps.constants
import
ALIAS
from
apps.constants
import
BOOLEANS
from
apps.constants
import
FLOATS
from
apps.constants
import
NOT_NONE
from
apps.constants
import
parameters
from
apps.resp.output
import
get_resp
from
apps.sacpz.output
import
get_sacpz
from
apps.globals
import
Error
from
apps.globals
import
HTTP
from
apps.globals
import
TIMEOUT
from
apps.output
import
get_resp
from
apps.output
import
get_sacpz
from
apps.utils
import
check_base_parameters
from
apps.utils
import
check_sacz_resp_parameters
from
apps.utils
import
check_request
from
apps.utils
import
currentutcday
from
apps.utils
import
error_param
from
apps.utils
import
error_request
from
apps.utils
import
is_valid_starttime
from
apps.utils
import
is_valid_datetime
from
apps.utils
import
is_valid_nodata
def
check_parameters
(
params
):
#
C
heck base parameters
(
params
,
error
)
=
check_base_parameters
(
params
,
NOT_NONE
,
BOOLEANS
,
FLOATS
)
#
c
heck base parameters
(
params
,
error
)
=
check_base_parameters
(
params
)
if
error
[
"code"
]
!=
200
:
return
(
params
,
error
)
# Check sacpz-resp parameters
(
params
,
error
)
=
check_sacz_resp_parameters
(
params
)
if
error
[
"code"
]
!=
200
:
return
(
params
,
error
)
# is both time and starttime (or endtime) are defined ?
if
params
[
"time"
]
is
not
None
and
params
[
"start"
]
is
not
None
:
return
error_param
(
params
,
Error
.
MULTI_TIME
+
" starttime parameter."
)
if
params
[
"time"
]
is
not
None
and
params
[
"end"
]
is
not
None
:
return
error_param
(
params
,
Error
.
MULTI_TIME
+
" endtime parameter."
)
# time validations
if
params
[
"time"
]
is
not
None
:
if
not
is_valid_starttime
(
params
[
"time"
]):
return
error_param
(
params
,
Error
.
TIME
+
str
(
params
[
"time"
]))
if
is_valid_datetime
(
params
[
"time"
]):
params
[
"time"
]
=
is_valid_datetime
(
params
[
"time"
])
elif
params
[
"start"
]
is
None
and
params
[
"end"
]
is
None
:
params
[
"time"
]
=
currentutcday
()
# nodata parameter validation
if
not
is_valid_nodata
(
params
[
"nodata"
]):
return
error_param
(
params
,
Error
.
NODATA_CODE
+
str
(
params
[
"nodata"
]))
params
[
"nodata"
]
=
params
[
"nodata"
].
lower
()
# wildcards or list are allowed only with location and channel parameters
for
key
in
(
"network"
,
"station"
):
if
re
.
search
(
r
"[,*?]"
,
params
[
key
]):
return
error_param
(
params
,
Error
.
NO_WILDCARDS
+
key
)
for
key
,
val
in
params
.
items
():
logging
.
debug
(
key
+
": "
+
str
(
val
))
return
(
params
,
error
)
return
(
params
,
{
"msg"
:
HTTP
.
_200_
,
"details"
:
Error
.
VALID_PARAM
,
"code"
:
200
}
)
def
checks_get
(
request
):
...
...
apps/sacpz/output.py
deleted
100644 → 0
View file @
61f89d87
import
logging
import
time
from
io
import
StringIO
from
flask
import
make_response
from
obspy.clients.fdsn
import
Client
from
apps.globals
import
Error
from
apps.globals
import
FDSN_CLIENT
from
apps.globals
import
USER_AGENT_SACPZ
from
apps.utils
import
error_500
from
apps.utils
import
error_request
from
apps.utils
import
tictac
def
get_inventory
(
params
):
client
=
Client
(
FDSN_CLIENT
,
user_agent
=
USER_AGENT_SACPZ
)
start
=
params
[
"time"
]
if
params
[
"time"
]
is
not
None
else
params
[
"starttime"
]
end
=
params
[
"time"
]
if
params
[
"time"
]
is
not
None
else
params
[
"endtime"
]
return
client
.
get_stations
(
network
=
params
[
"network"
],
station
=
params
[
"station"
],
location
=
params
[
"location"
],
channel
=
params
[
"channel"
],
starttime
=
start
,
endtime
=
end
,
level
=
"response"
,
)
def
get_sacpz
(
params
):
""" Builds the ws-sacpz response.
params: parameters """
try
:
tic0
=
time
.
time
()
response
=
None
cid
=
f
"
{
params
[
'network'
]
}
.
{
params
[
'station'
]
}
.
{
params
[
'location'
]
}
.
{
params
[
'channel'
]
}
"
start
=
(
params
[
"starttime"
]
if
params
[
"starttime"
]
is
not
None
else
params
[
"time"
]
)
fcid
=
cid
+
"."
+
str
(
start
).
replace
(
" "
,
"T"
)
logging
.
debug
(
fcid
)
try
:
inventory
=
get_inventory
(
params
)
except
Exception
as
ex
:
logging
.
debug
(
str
(
ex
))
code
=
int
(
params
[
"nodata"
])
return
error_request
(
msg
=
f
"HTTP._
{
code
}
_"
,
details
=
Error
.
NODATA
,
code
=
code
)
data
=
StringIO
()
inventory
.
write
(
data
,
format
=
"SACPZ"
)
headers
=
{
"Content-type"
:
"text/plain ; charset=utf-8"
}
response
=
make_response
(
data
.
getvalue
(),
headers
)
return
response
except
Exception
as
ex
:
logging
.
exception
(
str
(
ex
))
return
error_500
(
Error
.
UNSPECIFIED
)
finally
:
if
response
:
bytes
=
response
.
headers
.
get
(
"Content-Length"
)
logging
.
info
(
f
"
{
bytes
}
bytes rendered in
{
tictac
(
tic0
)
}
seconds."
)
apps/utils.py
View file @
37ee72a2
...
...
@@ -6,6 +6,7 @@ from datetime import datetime, timedelta
from
flask
import
Response
,
request
from
apps.constants
import
VERSION
from
apps.globals
import
Error
from
apps.globals
import
HTTP
from
apps.globals
import
NODATA_CODE
...
...
@@ -13,9 +14,6 @@ from apps.globals import STRING_FALSE
from
apps.globals
import
STRING_TRUE
from
apps.constants
import
VERSION
def
is_valid_integer
(
dimension
,
mini
=
0
,
maxi
=
sys
.
maxsize
):
# by default valid for positive integers
try
:
...
...
@@ -138,7 +136,9 @@ def check_request(request, params, alias):
return
(
keys
,
{
"msg"
:
HTTP
.
_200_
,
"details"
:
Error
.
VALID_PARAM
,
"code"
:
200
})
def
check_base_parameters
(
params
,
not_none
=
None
,
booleans
=
None
,
floats
=
None
):
def
check_base_parameters
(
params
,
max_days
=
None
,
not_none
=
None
,
booleans
=
None
,
floats
=
None
):
# Search for missing mandatory parameters
if
not_none
is
not
None
:
...
...
@@ -214,40 +214,11 @@ def check_base_parameters(params, not_none=None, booleans=None, floats=None):
end
=
str
(
params
[
"end"
])
return
error_param
(
params
,
Error
.
START_LATER
+
start
+
" > "
+
end
)
if
max_days
and
params
[
"end"
]
-
params
[
"start"
]
>
timedelta
(
days
=
max_days
):
return
error_param
(
params
,
Error
.
TOO_LONG_DURATION
+
f
"
{
max_days
}
days)."
)
# Search for empty selection
if
params
[
"network"
]
==
params
[
"station"
]
==
params
[
"channel"
]
==
"*"
:
return
error_param
(
params
,
Error
.
NO_SELECTION
)
return
(
params
,
{
"msg"
:
HTTP
.
_200_
,
"details"
:
Error
.
VALID_PARAM
,
"code"
:
200
})
def
check_sacz_resp_parameters
(
params
):
# is both time and starttime (or endtime) are defined ?
if
params
[
"time"
]
is
not
None
and
params
[
"start"
]
is
not
None
:
return
error_param
(
params
,
Error
.
MULTI_TIME
+
" starttime parameter."
)
if
params
[
"time"
]
is
not
None
and
params
[
"end"
]
is
not
None
:
return
error_param
(
params
,
Error
.
MULTI_TIME
+
" endtime parameter."
)
# time validations
if
params
[
"time"
]
is
not
None
:
if
not
is_valid_starttime
(
params
[
"time"
]):
return
error_param
(
params
,
Error
.
TIME
+
str
(
params
[
"time"
]))
if
is_valid_datetime
(
params
[
"time"
]):
params
[
"time"
]
=
is_valid_datetime
(
params
[
"time"
])
elif
params
[
"start"
]
is
None
and
params
[
"end"
]
is
None
:
params
[
"time"
]
=
currentutcday
()
# nodata parameter validation
if
not
is_valid_nodata
(
params
[
"nodata"
]):
return
error_param
(
params
,
Error
.
NODATA_CODE
+
str
(
params
[
"nodata"
]))
params
[
"nodata"
]
=
params
[
"nodata"
].
lower
()
# wildcards or list are allowed only with location and channel parameters
for
key
in
(
"network"
,
"station"
):
if
re
.
search
(
r
"[,*?]"
,
params
[
key
]):
return
error_param
(
params
,
Error
.
NO_WILDCARDS
+
key
)
return
(
params
,
{
"msg"
:
HTTP
.
_200_
,
"details"
:
Error
.
VALID_PARAM
,
"code"
:
200
})
apps/resp/README
_EN.md
→
docs/USAGE_RESP
_EN.md
View file @
37ee72a2
File moved
apps/resp/README
.md
→
docs/USAGE_RESP_FR
.md
View file @
37ee72a2
File moved
apps/sacpz/README
_EN.md
→
docs/USAGE_SACPZ
_EN.md
View file @
37ee72a2
File moved
apps/sacpz/README
.md
→
docs/USAGE_SACPZ_FR
.md
View file @
37ee72a2
File moved
requirements.txt
View file @
37ee72a2
Flask
==1.1.2
obspy
==1.2.
1
obspy
==1.2.
2
start_resp.py
View file @
37ee72a2
...
...
@@ -7,7 +7,6 @@ from apps.constants import VERSION
from
apps.root
import
output
app
=
Flask
(
__name__
)
app
.
config
[
"APPLICATION_ROOT"
]
=
"/resifws/resp"
fmt
=
"[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)d] [%(funcName)s] %(message)s"
loglevel
=
logging
.
INFO
if
os
.
environ
.
get
(
"RUNMODE"
)
==
"prodution"
else
logging
.
DEBUG
...
...
@@ -27,7 +26,7 @@ def resp_wadl():
return
response
@
app
.
route
(
"/version"
)
@
app
.
route
(
"/version"
,
strict_slashes
=
False
)
def
version
():
response
=
make_response
(
VERSION
)
response
.
headers
[
"Content-Type"
]
=
"text/plain"
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment