Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rm/valid_langs_session #57

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions ovos_bus_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class UtteranceState(str, enum.Enum):
RESPONSE = "response"


def _get_valid_langs() -> List[str]:
return list(set([get_default_lang()] +
Configuration().get("secondary_langs", [])))


class IntentContextManagerFrame:
def __init__(self, entities: List[dict] = None, metadata: dict = None):
"""
Expand Down Expand Up @@ -285,13 +280,12 @@ def __init__(self, session_id: str = None, expiration_seconds: int = None,
@param utterance_states: dict of skill_id to UtteranceState
@param lang: language associated with this Session
@param context: IntentContextManager for this Session
@param valid_langs: list of configured valid languages
@param valid_langs: DEPRECATED
"""
self.session_id = session_id or str(uuid4())
self.lang = lang or get_default_lang()
self.site_id = site_id or "unknown" # indoors placement info

self.valid_languages = valid_langs or _get_valid_langs()
self.active_skills = active_skills or [] # [skill_id , timestamp]# (Message , timestamp)
self.utterance_states = utterance_states or {} # {skill_id: UtteranceState}

Expand All @@ -313,10 +307,12 @@ def __init__(self, session_id: str = None, expiration_seconds: int = None,

# deprecated - TODO remove 0.0.8
if history is not None or max_time is not None or max_messages is not None:
LOG.warning("history, max_time and max_messages have been deprecated")
LOG.warning("valid_langs , history, max_time and max_messages have been deprecated")
self.history = [] # (Message , timestamp)
self.max_time = 5 # minutes
self.max_messages = 5
self.valid_languages = list(set([get_default_lang()] +
Configuration().get("secondary_langs", [])))

@property
def active(self) -> bool:
Expand Down Expand Up @@ -409,7 +405,6 @@ def serialize(self) -> dict:
"utterance_states": self.utterance_states,
"session_id": self.session_id,
"lang": self.lang,
"valid_languages": self.valid_languages,
"context": self.context.serialize(),
"site_id": self.site_id,
"pipeline": self.pipeline
Expand All @@ -434,15 +429,13 @@ def deserialize(data: dict):
active = data.get("active_skills") or []
states = data.get("utterance_states") or {}
lang = data.get("lang")
valid_langs = data.get("valid_languages") or _get_valid_langs()
context = IntentContextManager.deserialize(data.get("context", {}))
site_id = data.get("site_id", "unknown")
pipeline = data.get("pipeline", [])
return Session(uid,
active_skills=active,
utterance_states=states,
lang=lang,
valid_langs=valid_langs,
context=context,
pipeline=pipeline,
site_id=site_id)
Expand Down Expand Up @@ -556,9 +549,7 @@ def get(message: Optional[Message] = None) -> Session:
if message:
msg_sess = Session.from_message(message)
if msg_sess:
if msg_sess.session_id == "default": # reserved namespace for ovos-core
LOG.debug(f"message is using default session")
else:
if msg_sess.session_id != "default": # reserved namespace for ovos-core
SessionManager.sessions[msg_sess.session_id] = msg_sess
return msg_sess
else:
Expand Down
33 changes: 0 additions & 33 deletions test/unittests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,6 @@ def test_utterance_state(self):
self.assertIsInstance(state, UtteranceState)
self.assertIsInstance(state, str)

@patch("ovos_bus_client.session.get_default_lang")
@patch("ovos_bus_client.session.Configuration")
def test_get_valid_langs(self, config, default_lang):
config.return_value = {
"secondary_langs": ["en-us", "es-mx", "fr-ca"]
}
default_lang.return_value = "en-us"
from ovos_bus_client.session import _get_valid_langs
# Test default in secondary
langs = _get_valid_langs()
self.assertIsInstance(langs, list)
self.assertEqual(len(langs), len(set(langs)))
self.assertEqual(set(langs), {"en-us", "es-mx", "fr-ca"})

# Test default not in secondary
default_lang.return_value = "pt-pt"
langs = _get_valid_langs()
self.assertIsInstance(langs, list)
self.assertEqual(len(langs), len(set(langs)))
self.assertEqual(set(langs), {"en-us", "es-mx", "fr-ca", "pt-pt"})

# Test no secondary
config.return_value = {}
langs = _get_valid_langs()
self.assertEqual(langs, [default_lang.return_value])

# Test invalid secondary lang config
config.return_value = {"secondary_langs": None}
with self.assertRaises(TypeError):
_get_valid_langs()


class TestIntentContextManagerFrame(unittest.TestCase):
def test_serialize_deserialize(self):
Expand Down Expand Up @@ -138,7 +107,6 @@ def test_init(self):
session = Session()
self.assertIsInstance(session.session_id, str)
self.assertIsInstance(session.lang, str)
self.assertIsInstance(session.valid_languages, list)
self.assertEqual(session.active_skills, list())
self.assertEqual(session.utterance_states, dict())
self.assertIsInstance(session.touch_time, int)
Expand Down Expand Up @@ -216,7 +184,6 @@ def test_serialize_deserialize(self):
self.assertIsInstance(test_session, Session)
self.assertIsInstance(test_session.session_id, str)
self.assertIsInstance(test_session.lang, str)
self.assertIsInstance(test_session.valid_languages, list)
self.assertIsInstance(test_session.active_skills, list)
self.assertIsInstance(test_session.utterance_states, dict)
self.assertIsInstance(test_session.touch_time, int)
Expand Down
Loading