Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
senpy
senpy
Commits
e0b4c762
Commit
e0b4c762
authored
Apr 07, 2017
by
J. Fernando Sánchez
Browse files
Add plugin method to client
Closes
#28
parent
14c86ec3
Changes
5
Hide whitespace changes
Inline
Side-by-side
senpy/client.py
View file @
e0b4c762
import
requests
import
logging
from
.
import
models
from
.plugins
import
default_plugin_type
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -12,6 +13,10 @@ class Client(object):
def
analyse
(
self
,
input
,
method
=
'GET'
,
**
kwargs
):
return
self
.
request
(
'/'
,
method
=
method
,
input
=
input
,
**
kwargs
)
def
plugins
(
self
,
ptype
=
default_plugin_type
):
resp
=
self
.
request
(
path
=
'/plugins'
,
plugin_type
=
ptype
).
plugins
return
{
p
.
name
:
p
for
p
in
resp
}
def
request
(
self
,
path
=
None
,
method
=
'GET'
,
**
params
):
url
=
'{}{}'
.
format
(
self
.
endpoint
,
path
)
response
=
requests
.
request
(
method
=
method
,
url
=
url
,
params
=
params
)
...
...
senpy/extensions.py
View file @
e0b4c762
...
...
@@ -183,7 +183,7 @@ class Senpy(object):
return
resp
def
_conversion_candidates
(
self
,
fromModel
,
toModel
):
candidates
=
self
.
filter_plugins
(
**
{
'@
type
'
:
'emotionConversionPlugin'
}
)
candidates
=
self
.
filter_plugins
(
plugin_
type
=
'emotionConversionPlugin'
)
for
name
,
candidate
in
candidates
.
items
():
for
pair
in
candidate
.
onyx__doesConversion
:
logging
.
debug
(
pair
)
...
...
@@ -417,33 +417,7 @@ class Senpy(object):
return
self
.
_plugin_list
def
filter_plugins
(
self
,
**
kwargs
):
""" Filter plugins by different criteria """
ptype
=
kwargs
.
pop
(
'plugin_type'
,
None
)
logger
.
debug
(
'#'
*
100
)
logger
.
debug
(
'ptype {}'
.
format
(
ptype
))
if
ptype
:
try
:
ptype
=
ptype
[
0
].
upper
()
+
ptype
[
1
:]
pclass
=
getattr
(
plugins
,
ptype
)
logger
.
debug
(
'Class: {}'
.
format
(
pclass
))
candidates
=
filter
(
lambda
x
:
isinstance
(
x
,
pclass
),
self
.
plugins
.
values
())
except
AttributeError
:
raise
Error
(
'{} is not a valid type'
.
format
(
ptype
))
else
:
candidates
=
self
.
plugins
.
values
()
logger
.
debug
(
candidates
)
def
matches
(
plug
):
res
=
all
(
getattr
(
plug
,
k
,
None
)
==
v
for
(
k
,
v
)
in
kwargs
.
items
())
logger
.
debug
(
"matching {} with {}: {}"
.
format
(
plug
.
name
,
kwargs
,
res
))
return
res
if
kwargs
:
candidates
=
filter
(
matches
,
candidates
)
return
{
p
.
name
:
p
for
p
in
candidates
}
return
plugins
.
pfilter
(
self
.
plugins
,
**
kwargs
)
@
property
def
analysis_plugins
(
self
):
...
...
senpy/plugins/__init__.py
View file @
e0b4c762
...
...
@@ -9,6 +9,7 @@ import logging
import
tempfile
import
copy
from
..
import
models
from
..api
import
API_PARAMS
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -117,3 +118,40 @@ class ShelfMixin(object):
if
hasattr
(
self
,
'_sh'
)
and
self
.
_sh
is
not
None
:
with
open
(
self
.
shelf_file
,
'wb'
)
as
f
:
pickle
.
dump
(
self
.
_sh
,
f
)
default_plugin_type
=
API_PARAMS
[
'plugin_type'
][
'default'
]
def
pfilter
(
plugins
,
**
kwargs
):
""" Filter plugins by different criteria """
if
isinstance
(
plugins
,
models
.
Plugins
):
plugins
=
plugins
.
plugins
elif
isinstance
(
plugins
,
dict
):
plugins
=
plugins
.
values
()
ptype
=
kwargs
.
pop
(
'plugin_type'
,
default_plugin_type
)
logger
.
debug
(
'#'
*
100
)
logger
.
debug
(
'ptype {}'
.
format
(
ptype
))
if
ptype
:
try
:
ptype
=
ptype
[
0
].
upper
()
+
ptype
[
1
:]
pclass
=
globals
()[
ptype
]
logger
.
debug
(
'Class: {}'
.
format
(
pclass
))
candidates
=
filter
(
lambda
x
:
isinstance
(
x
,
pclass
),
plugins
)
except
KeyError
:
raise
models
.
Error
(
'{} is not a valid type'
.
format
(
ptype
))
else
:
candidates
=
plugins
logger
.
debug
(
candidates
)
def
matches
(
plug
):
res
=
all
(
getattr
(
plug
,
k
,
None
)
==
v
for
(
k
,
v
)
in
kwargs
.
items
())
logger
.
debug
(
"matching {} with {}: {}"
.
format
(
plug
.
name
,
kwargs
,
res
))
return
res
if
kwargs
:
candidates
=
filter
(
matches
,
candidates
)
return
{
p
.
name
:
p
for
p
in
candidates
}
tests/test_client.py
View file @
e0b4c762
...
...
@@ -4,18 +4,21 @@ try:
except
ImportError
:
from
mock
import
patch
import
json
from
senpy.client
import
Client
from
senpy.models
import
Results
,
Error
from
senpy.models
import
Results
,
Plugins
,
Error
from
senpy.plugins
import
AnalysisPlugin
,
default_plugin_type
class
Call
(
dict
):
def
__init__
(
self
,
obj
):
self
.
obj
=
obj
.
jsonld
()
self
.
obj
=
obj
.
serialize
()
self
.
status_code
=
200
self
.
content
=
self
.
json
()
def
json
(
self
):
return
self
.
obj
return
json
.
loads
(
self
.
obj
)
class
ModelsTest
(
TestCase
):
...
...
@@ -44,3 +47,19 @@ class ModelsTest(TestCase):
method
=
'GET'
,
params
=
{
'input'
:
'hello'
,
'algorithm'
:
'NONEXISTENT'
})
def
test_plugins
(
self
):
endpoint
=
'http://dummy/'
client
=
Client
(
endpoint
)
plugins
=
Plugins
()
p1
=
AnalysisPlugin
({
'name'
:
'AnalysisP1'
,
'version'
:
0
,
'description'
:
'No'
})
plugins
.
plugins
=
[
p1
,
]
success
=
Call
(
plugins
)
with
patch
(
'requests.request'
,
return_value
=
success
)
as
patched
:
response
=
client
.
plugins
()
assert
isinstance
(
response
,
dict
)
assert
len
(
response
)
==
1
assert
"AnalysisP1"
in
response
patched
.
assert_called_with
(
url
=
endpoint
+
'/plugins'
,
method
=
'GET'
,
params
=
{
'plugin_type'
:
default_plugin_type
})
tests/test_models.py
View file @
e0b4c762
...
...
@@ -170,7 +170,7 @@ class ModelsTest(TestCase):
def
test_single_plugin
(
self
):
"""A response with a single plugin should still return a list"""
plugs
=
Plugins
()
p
=
Plugin
({
'id'
:
str
(
0
),
p
=
Plugin
({
'id'
:
str
(
1
),
'version'
:
0
,
'description'
:
'dummy'
})
plugs
.
plugins
.
append
(
p
)
...
...
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