moto/moto/polly/models.py

124 lines
3.7 KiB
Python
Raw Normal View History

2017-09-26 15:46:18 +00:00
from __future__ import unicode_literals
from xml.etree import ElementTree as ET
import datetime
2019-12-26 16:12:22 +00:00
from boto3 import Session
2017-09-26 15:46:18 +00:00
from moto.core import BaseBackend, BaseModel
from .resources import VOICE_DATA
from .utils import make_arn_for_lexicon
from moto.core import ACCOUNT_ID as DEFAULT_ACCOUNT_ID
2017-09-26 15:46:18 +00:00
class Lexicon(BaseModel):
def __init__(self, name, content, region_name):
self.name = name
self.content = content
self.size = 0
self.alphabet = None
self.last_modified = None
self.language_code = None
self.lexemes_count = 0
self.arn = make_arn_for_lexicon(DEFAULT_ACCOUNT_ID, name, region_name)
self.update()
def update(self, content=None):
if content is not None:
self.content = content
# Probably a very naive approach, but it'll do for now.
try:
root = ET.fromstring(self.content)
self.size = len(self.content)
2019-10-31 15:44:26 +00:00
self.last_modified = int(
(
datetime.datetime.now() - datetime.datetime(1970, 1, 1)
).total_seconds()
)
self.lexemes_count = len(root.findall("."))
2017-09-26 15:46:18 +00:00
for key, value in root.attrib.items():
2019-10-31 15:44:26 +00:00
if key.endswith("alphabet"):
2017-09-26 15:46:18 +00:00
self.alphabet = value
2019-10-31 15:44:26 +00:00
elif key.endswith("lang"):
2017-09-26 15:46:18 +00:00
self.language_code = value
except Exception as err:
2019-10-31 15:44:26 +00:00
raise ValueError("Failure parsing XML: {0}".format(err))
2017-09-26 15:46:18 +00:00
def to_dict(self):
return {
2019-10-31 15:44:26 +00:00
"Attributes": {
"Alphabet": self.alphabet,
"LanguageCode": self.language_code,
"LastModified": self.last_modified,
"LexemesCount": self.lexemes_count,
"LexiconArn": self.arn,
"Size": self.size,
2017-09-26 15:46:18 +00:00
}
}
def __repr__(self):
2019-10-31 15:44:26 +00:00
return "<Lexicon {0}>".format(self.name)
2017-09-26 15:46:18 +00:00
class PollyBackend(BaseBackend):
def __init__(self, region_name=None):
super(PollyBackend, self).__init__()
self.region_name = region_name
self._lexicons = {}
def reset(self):
region_name = self.region_name
self.__dict__ = {}
self.__init__(region_name)
def describe_voices(self, language_code, next_token):
if language_code is None:
return VOICE_DATA
2019-10-31 15:44:26 +00:00
return [item for item in VOICE_DATA if item["LanguageCode"] == language_code]
2017-09-26 15:46:18 +00:00
def delete_lexicon(self, name):
# implement here
del self._lexicons[name]
def get_lexicon(self, name):
# Raises KeyError
return self._lexicons[name]
def list_lexicons(self, next_token):
result = []
for name, lexicon in self._lexicons.items():
lexicon_dict = lexicon.to_dict()
2019-10-31 15:44:26 +00:00
lexicon_dict["Name"] = name
2017-09-26 15:46:18 +00:00
result.append(lexicon_dict)
return result
def put_lexicon(self, name, content):
# If lexicon content is bad, it will raise ValueError
if name in self._lexicons:
# Regenerated all the stats from the XML
# but keeps the ARN
self._lexicons.update(content)
else:
lexicon = Lexicon(name, content, region_name=self.region_name)
self._lexicons[name] = lexicon
2019-12-26 16:12:22 +00:00
polly_backends = {}
for region in Session().get_available_regions("polly"):
polly_backends[region] = PollyBackend(region)
for region in Session().get_available_regions("polly", partition_name="aws-us-gov"):
polly_backends[region] = PollyBackend(region)
for region in Session().get_available_regions("polly", partition_name="aws-cn"):
polly_backends[region] = PollyBackend(region)