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
b8993f7d
Commit
b8993f7d
authored
Nov 05, 2015
by
J. Fernando Sánchez
Browse files
Added shelve mixin
parent
bd2e0f0d
Changes
3
Hide whitespace changes
Inline
Side-by-side
senpy/models.py
View file @
b8993f7d
...
...
@@ -71,7 +71,10 @@ class Leaf(dict):
return
id
def
__delattr__
(
self
,
key
):
return
super
(
Leaf
,
self
).
__delitem__
(
self
.
_get_key
(
key
))
if
key
in
self
.
__dict__
:
del
self
.
__dict__
[
key
]
else
:
super
(
Leaf
,
self
).
__delitem__
(
self
.
_get_key
(
key
))
def
_get_key
(
self
,
key
):
if
key
[
0
]
==
"_"
:
...
...
senpy/plugins.py
View file @
b8993f7d
import
inspect
import
os.path
import
shelve
import
logging
import
ConfigParser
from
.models
import
Response
,
Leaf
...
...
@@ -85,6 +88,9 @@ class SenpyPlugin(Leaf):
self
.
is_activated
=
False
self
.
_info
=
info
def
get_folder
(
self
):
return
os
.
path
.
dirname
(
inspect
.
getfile
(
self
.
__class__
))
def
analyse
(
self
,
*
args
,
**
kwargs
):
logger
.
debug
(
"Analysing with: {} {}"
.
format
(
self
.
name
,
self
.
version
))
pass
...
...
@@ -121,3 +127,29 @@ class EmotionPlugin(SenpyPlugin):
resp
=
super
(
EmotionPlugin
,
self
).
__init__
(
info
,
*
args
,
**
kwargs
)
self
.
minEmotionValue
=
float
(
info
.
get
(
"minEmotionValue"
,
0
))
self
.
maxEmotionValue
=
float
(
info
.
get
(
"maxEmotionValue"
,
0
))
class
ShelfMixin
(
object
):
@
property
def
sh
(
self
):
if
not
hasattr
(
self
,
'_sh'
)
or
not
self
.
_sh
:
self
.
_sh
=
shelve
.
open
(
self
.
shelf_file
,
writeback
=
True
)
return
self
.
_sh
@
sh
.
deleter
def
sh
(
self
):
if
hasattr
(
self
,
'_sh'
):
del
(
self
.
_sh
)
@
property
def
shelf_file
(
self
):
if
not
hasattr
(
self
,
'_shelf_file'
)
or
not
self
.
_shelf_file
:
if
hasattr
(
self
,
'_info'
)
and
'shelf_file'
in
self
.
_info
:
self
.
_shelf_file
=
self
.
_info
[
'shelf_file'
]
else
:
self
.
_shelf_file
=
os
.
path
.
join
(
self
.
get_folder
(),
self
.
name
+
'.db'
)
return
self
.
_shelf_file
def
close
(
self
):
del
(
self
.
_sh
)
tests/plugins_test/__init__.py
0 → 100644
View file @
b8993f7d
import
os
import
logging
import
shelve
try
:
import
unittest.mock
as
mock
except
ImportError
:
import
mock
import
json
import
os
from
unittest
import
TestCase
from
senpy.models
import
Response
,
Entry
from
senpy.plugins
import
SenpyPlugin
,
ShelfMixin
class
ModelsTest
(
TestCase
):
# def test_shelf(self):
# class ShelfTest(ShelfMixin):
# pass
# a = ShelfTest()
# print(type(a.sh))
# assert(False)
def
test_shelf
(
self
):
class
ShelfTest
(
ShelfMixin
,
SenpyPlugin
):
pass
a
=
ShelfTest
({
'name'
:
'shelve'
,
'version'
:
'test'
})
print
(
type
(
a
.
sh
))
a
.
context
=
"ohno"
del
a
.
context
print
(
a
)
a
.
sh
[
'classifier'
]
=
{
'name'
:
'ohno'
}
assert
a
.
name
==
'shelve'
assert
isinstance
(
a
.
sh
,
shelve
.
Shelf
)
a
.
close
()
b
=
ShelfTest
({
'name'
:
'shelve'
,
'version'
:
'test'
})
assert
b
.
name
==
'shelve'
assert
b
.
sh
[
'classifier'
]
assert
b
.
sh
[
'classifier'
][
'name'
]
==
'ohno'
assert
isinstance
(
b
.
sh
,
shelve
.
Shelf
)
Write
Preview
Markdown
is supported
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