Use namedtuples for internal meta info

This commit is contained in:
Griatch 2018-03-03 15:19:18 +01:00
parent 3e7dae6a8c
commit a7eed91d90

View file

@ -14,6 +14,7 @@ prototype, override its name with an empty dict.
""" """
from collections import namedtuple
from django.conf import settings from django.conf import settings
from evennia.scripts.scripts import DefaultScript from evennia.scripts.scripts import DefaultScript
from evennia.utils.create import create_script from evennia.utils.create import create_script
@ -25,19 +26,22 @@ from evennia.utils.evtable import EvTable
_READONLY_PROTOTYPES = {} _READONLY_PROTOTYPES = {}
_READONLY_PROTOTYPE_MODULES = {} _READONLY_PROTOTYPE_MODULES = {}
# storage of meta info about the prototype
MetaProto = namedtuple('MetaProto', ['key', 'desc', 'locks', 'tags', 'prototype'])
for mod in settings.PROTOTYPE_MODULES: for mod in settings.PROTOTYPE_MODULES:
# to remove a default prototype, override it with an empty dict. # to remove a default prototype, override it with an empty dict.
# internally we store as (key, desc, locks, tags, prototype_dict) # internally we store as (key, desc, locks, tags, prototype_dict)
prots = [(key, prot) for key, prot in all_from_module(mod).items() prots = [(key, prot) for key, prot in all_from_module(mod).items()
if prot and isinstance(prot, dict)] if prot and isinstance(prot, dict)]
_READONLY_PROTOTYPES.update( _READONLY_PROTOTYPES.update(
{key.lower(): {key.lower(): MetaProto(
(key.lower(), key.lower(),
prot['prototype_desc'] if 'prototype_desc' in prot else mod, prot['prototype_desc'] if 'prototype_desc' in prot else mod,
prot['prototype_lock'] if 'prototype_lock' in prot else "use:all()", prot['prototype_lock'] if 'prototype_lock' in prot else "use:all()",
set(make_iter( set(make_iter(
prot['prototype_tags']) if 'prototype_tags' in prot else ["base-prototype"]), prot['prototype_tags']) if 'prototype_tags' in prot else ["base-prototype"]),
prot) prot)
for key, prot in prots}) for key, prot in prots})
_READONLY_PROTOTYPE_MODULES.update({tup[0]: mod for tup in prots}) _READONLY_PROTOTYPE_MODULES.update({tup[0]: mod for tup in prots})
@ -151,17 +155,16 @@ def search_readonly_prototype(key=None, tags=None):
tags (str or list): Tag key to query for. tags (str or list): Tag key to query for.
Return: Return:
matches (list): List of prototype tuples that includes matches (list): List of MetaProto tuples that includes
prototype metadata, on the form prototype metadata,
`(key, desc, lockstring, taglist, prototypedict)`
""" """
matches = [] matches = []
if tags: if tags:
# use tags to limit selection # use tags to limit selection
tagset = set(tags) tagset = set(tags)
matches = {key: tup for key, tup in _READONLY_PROTOTYPES.items() matches = {key: metaproto for key, metaproto in _READONLY_PROTOTYPES.items()
if tagset.intersection(tup[3])} if tagset.intersection(metaproto.tags)}
else: else:
matches = _READONLY_PROTOTYPES matches = _READONLY_PROTOTYPES
@ -171,7 +174,7 @@ def search_readonly_prototype(key=None, tags=None):
return matches[key] return matches[key]
else: else:
# fuzzy matching # fuzzy matching
return [tup for pkey, tup in matches.items() if key in pkey] return [metaproto for pkey, metaproto in matches.items() if key in pkey]
return matches return matches
@ -227,11 +230,11 @@ def get_prototype_list(caller, key=None, tags=None, show_non_use=False, show_non
# get use-permissions of readonly attributes (edit is always False) # get use-permissions of readonly attributes (edit is always False)
readonly_prototypes = [ readonly_prototypes = [
(tup[0], (tup.key,
tup[1], tup.desc,
("{}/N".format('Y' ("{}/N".format('Y'
if caller.locks.check_lockstring(caller, tup[2], access_type='use') else 'N')), if caller.locks.check_lockstring(caller, tup.locks, access_type='use') else 'N')),
",".join(tup[3])) for tup in readonly_prototypes] ",".join(tup.tags)) for tup in readonly_prototypes]
# next, handle db-stored prototypes # next, handle db-stored prototypes
prototypes = search_persistent_prototype(key, tags) prototypes = search_persistent_prototype(key, tags)