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:
type_flag = ConfigFlags.NONE.value
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)
captions = "|".join(names)
possible_values = ""
for s in ServiceManager.services:
if s._custom_needed:
possible_values += '1'
possible_values += '|'
groups = self.session.services.buildgroups(ServiceManager.services)
# sort groups by name and map services to groups
groups = set()
group_map = {}
for service in ServiceManager.services.itervalues():
group = service._group
groups.add(group)
group_map.setdefault(group, []).append(service)
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
else:
if not node_id:

View file

@ -22,7 +22,7 @@ class ServiceManager(object):
"""
Manages services available for CORE nodes to use.
"""
services = []
services = {}
@classmethod
def add(cls, service):
@ -32,14 +32,11 @@ class ServiceManager(object):
:param CoreService service: service to add
: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__)
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
def get(cls, name):
@ -50,10 +47,7 @@ class ServiceManager(object):
:return: service if it exists, None otherwise
:rtype: CoreService
"""
for service in cls.services:
if service._name == name:
return service
return None
return cls.services.get(name)
@classmethod
def add_services(cls, path):
@ -89,7 +83,6 @@ class CoreServices(object):
Creates a CoreServices instance.
:param core.session.Session session: session this manager is tied to
:return: nothing
"""
# ConfigurableManager.__init__(self)
self.session = session
@ -450,39 +443,6 @@ class CoreServices(object):
services.append(s)
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):
"""
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
_name = ""
# group string allows grouping services together
_group = ""
# list name(s) of services that this service depends upon
_depends = ()
keys = ["dirs", "files", "startidx", "cmdup", "cmddown", "cmdval", "meta", "starttime"]
# private, per-node directories required by this service
_dirs = ()
# config files written by this service
_configs = ()
# configs = []
# index used to determine start order with other services
_startindex = 0
# time in seconds after runtime to run startup commands
_starttime = ""
# list of startup commands
_startup = ()
# startup = []
# list of shutdown commands
_shutdown = ()
# list of validate commands
_validate = ()
# metadata associated with this service
_meta = ""
# custom configuration text
_configtxt = ()
_custom = False
@ -701,13 +674,13 @@ class CoreService(object):
pass
@classmethod
def getconfigfilenames(cls, nodenum, services):
def getconfigfilenames(cls, node_id, services):
"""
Return the tuple of configuration file filenames. This default method
returns the cls._configs tuple, but this method may be overriden to
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
:return: class configuration files
:rtype: tuple