updated service manager to use a dict and throw and error on duplicate service names

This commit is contained in:
Blake J. Harnden 2018-06-14 12:50:48 -07:00
parent 82c3d57dd3
commit 0bf9c99910
2 changed files with 57 additions and 57 deletions

View file

@ -1068,15 +1068,42 @@ class CoreHandler(SocketServer.BaseRequestHandler):
if opaque is None: if opaque is None:
type_flag = ConfigFlags.NONE.value type_flag = ConfigFlags.NONE.value
data_types = tuple(repeat(ConfigDataTypes.BOOL.value, len(ServiceManager.services))) data_types = tuple(repeat(ConfigDataTypes.BOOL.value, len(ServiceManager.services)))
values = "|".join(repeat('0', len(ServiceManager.services)))
names = map(lambda x: x._name, ServiceManager.services) # sort groups by name and map services to groups
captions = "|".join(names) groups = set()
possible_values = "" group_map = {}
for s in ServiceManager.services: for service in ServiceManager.services.itervalues():
if s._custom_needed: group = service._group
possible_values += '1' groups.add(group)
possible_values += '|' group_map.setdefault(group, []).append(service)
groups = self.session.services.buildgroups(ServiceManager.services) groups = sorted(groups, key=lambda x: x.lower())
# define tlv values in proper order
captions = []
possible_values = []
values = []
group_strings = []
start_index = 1
logger.info("sorted groups: %s", groups)
for group in groups:
services = sorted(group_map[group], key=lambda x: x._name.lower())
logger.info("sorted services for group(%s): %s", group, services)
end_index = start_index + len(services) - 1
group_strings.append("%s:%s-%s" % (group, start_index, end_index))
start_index = start_index + len(services)
for service in services:
captions.append(service._name)
values.append("0")
if service._custom_needed:
possible_values.append("1")
else:
possible_values.append("")
# format for tlv
captions = "|".join(captions)
possible_values = "|".join(possible_values)
values = "|".join(values)
groups = "|".join(group_strings)
# send back the properties for this service # send back the properties for this service
else: else:
if not node_id: if not node_id:

View file

@ -22,7 +22,7 @@ class ServiceManager(object):
""" """
Manages services available for CORE nodes to use. Manages services available for CORE nodes to use.
""" """
services = [] services = {}
@classmethod @classmethod
def add(cls, service): def add(cls, service):
@ -32,14 +32,11 @@ class ServiceManager(object):
:param CoreService service: service to add :param CoreService service: service to add
:return: nothing :return: nothing
""" """
insert = 0
for index, known_service in enumerate(cls.services):
if known_service._group == service._group:
insert = index + 1
break
logger.info("loading service: %s", service.__name__) logger.info("loading service: %s", service.__name__)
cls.services.insert(insert, service) name = service._name
if name in cls.services:
raise ValueError("duplicate service being added: %s" % name)
cls.services[name] = service
@classmethod @classmethod
def get(cls, name): def get(cls, name):
@ -50,10 +47,7 @@ class ServiceManager(object):
:return: service if it exists, None otherwise :return: service if it exists, None otherwise
:rtype: CoreService :rtype: CoreService
""" """
for service in cls.services: return cls.services.get(name)
if service._name == name:
return service
return None
@classmethod @classmethod
def add_services(cls, path): def add_services(cls, path):
@ -89,7 +83,6 @@ class CoreServices(object):
Creates a CoreServices instance. Creates a CoreServices instance.
:param core.session.Session session: session this manager is tied to :param core.session.Session session: session this manager is tied to
:return: nothing
""" """
# ConfigurableManager.__init__(self) # ConfigurableManager.__init__(self)
self.session = session self.session = session
@ -450,39 +443,6 @@ class CoreServices(object):
services.append(s) services.append(s)
return services, unknown return services, unknown
def buildgroups(self, servicelist):
"""
Build a string of groups for use in a configuration message given
a list of services. The group list string has the format
"title1:1-5|title2:6-9|10-12", where title is an optional group title
and i-j is a numeric range of value indices; groups are
separated by commas.
:param list servicelist: service list to build group string from
:return: groups string
:rtype: str
"""
i = 0
r = ""
lastgroup = "<undefined>"
for service in servicelist:
i += 1
group = service._group
if group != lastgroup:
lastgroup = group
# finish previous group
if i > 1:
r += "-%d|" % (i - 1)
# optionally include group title
if group == "":
r += "%d" % i
else:
r += "%s:%d" % (group, i)
# finish the last group list
if i > 0:
r += "-%d" % i
return r
def getservicefile(self, services, node, filename): def getservicefile(self, services, node, filename):
""" """
Send a File Message when the GUI has requested a service file. Send a File Message when the GUI has requested a service file.
@ -662,27 +622,40 @@ class CoreService(object):
""" """
# service name should not include spaces # service name should not include spaces
_name = "" _name = ""
# group string allows grouping services together # group string allows grouping services together
_group = "" _group = ""
# list name(s) of services that this service depends upon # list name(s) of services that this service depends upon
_depends = () _depends = ()
keys = ["dirs", "files", "startidx", "cmdup", "cmddown", "cmdval", "meta", "starttime"] keys = ["dirs", "files", "startidx", "cmdup", "cmddown", "cmdval", "meta", "starttime"]
# private, per-node directories required by this service # private, per-node directories required by this service
_dirs = () _dirs = ()
# config files written by this service # config files written by this service
_configs = () _configs = ()
# configs = []
# index used to determine start order with other services # index used to determine start order with other services
_startindex = 0 _startindex = 0
# time in seconds after runtime to run startup commands # time in seconds after runtime to run startup commands
_starttime = "" _starttime = ""
# list of startup commands # list of startup commands
_startup = () _startup = ()
# startup = []
# list of shutdown commands # list of shutdown commands
_shutdown = () _shutdown = ()
# list of validate commands # list of validate commands
_validate = () _validate = ()
# metadata associated with this service # metadata associated with this service
_meta = "" _meta = ""
# custom configuration text # custom configuration text
_configtxt = () _configtxt = ()
_custom = False _custom = False
@ -701,13 +674,13 @@ class CoreService(object):
pass pass
@classmethod @classmethod
def getconfigfilenames(cls, nodenum, services): def getconfigfilenames(cls, node_id, services):
""" """
Return the tuple of configuration file filenames. This default method Return the tuple of configuration file filenames. This default method
returns the cls._configs tuple, but this method may be overriden to returns the cls._configs tuple, but this method may be overriden to
provide node-specific filenames that may be based on other services. provide node-specific filenames that may be based on other services.
:param int nodenum: node id to get config file names for :param int node_id: node id to get config file names for
:param list services: node services :param list services: node services
:return: class configuration files :return: class configuration files
:rtype: tuple :rtype: tuple