Ran formatting on code

This commit is contained in:
Griatch 2022-10-31 20:43:27 +01:00
parent 83154de19e
commit 353e4c0aa7
20 changed files with 296 additions and 327 deletions

View file

@ -20,6 +20,7 @@ from evennia.utils.utils import callables_from_module, class_from_module
SCRIPTDB = None
class Container:
"""
Base container class. A container is simply a storage object whose
@ -203,7 +204,9 @@ class GlobalScriptContainer(Container):
self.typeclass_storage = {}
for key, data in list(self.loaded_data.items()):
typeclass = data.get("typeclass", settings.BASE_SCRIPT_TYPECLASS)
self.typeclass_storage[key] = class_from_module(typeclass, fallback=settings.BASE_SCRIPT_TYPECLASS)
self.typeclass_storage[key] = class_from_module(
typeclass, fallback=settings.BASE_SCRIPT_TYPECLASS
)
def get(self, key, default=None):
"""

View file

@ -6,5 +6,6 @@ dependencies.
from evennia import nonexistent_module, DefaultScript
class BrokenScript(DefaultScript):
pass

View file

@ -8,14 +8,16 @@ from evennia import DefaultScript
_BASE_TYPECLASS = class_from_module(settings.BASE_SCRIPT_TYPECLASS)
class GoodScript(DefaultScript):
pass
class InvalidScript:
pass
class TestGlobalScriptContainer(unittest.TestCase):
class TestGlobalScriptContainer(unittest.TestCase):
def test_init_with_no_scripts(self):
gsc = containers.GlobalScriptContainer()
@ -29,7 +31,7 @@ class TestGlobalScriptContainer(unittest.TestCase):
self.assertEqual(len(gsc.typeclass_storage), 0)
@override_settings(GLOBAL_SCRIPTS={'script_name': {}})
@override_settings(GLOBAL_SCRIPTS={"script_name": {}})
def test_start_with_typeclassless_script(self):
"""No specified typeclass should fallback to base"""
gsc = containers.GlobalScriptContainer()
@ -37,10 +39,14 @@ class TestGlobalScriptContainer(unittest.TestCase):
gsc.start()
self.assertEqual(len(gsc.typeclass_storage), 1)
self.assertIn('script_name', gsc.typeclass_storage)
self.assertEqual(gsc.typeclass_storage['script_name'], _BASE_TYPECLASS)
self.assertIn("script_name", gsc.typeclass_storage)
self.assertEqual(gsc.typeclass_storage["script_name"], _BASE_TYPECLASS)
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.NoScript'}})
@override_settings(
GLOBAL_SCRIPTS={
"script_name": {"typeclass": "evennia.utils.tests.test_containers.NoScript"}
}
)
def test_start_with_nonexistent_script(self):
"""Missing script class should fall back to base"""
gsc = containers.GlobalScriptContainer()
@ -48,35 +54,53 @@ class TestGlobalScriptContainer(unittest.TestCase):
gsc.start()
self.assertEqual(len(gsc.typeclass_storage), 1)
self.assertIn('script_name', gsc.typeclass_storage)
self.assertEqual(gsc.typeclass_storage['script_name'], _BASE_TYPECLASS)
self.assertIn("script_name", gsc.typeclass_storage)
self.assertEqual(gsc.typeclass_storage["script_name"], _BASE_TYPECLASS)
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.GoodScript'}})
@override_settings(
GLOBAL_SCRIPTS={
"script_name": {"typeclass": "evennia.utils.tests.test_containers.GoodScript"}
}
)
def test_start_with_valid_script(self):
gsc = containers.GlobalScriptContainer()
gsc.start()
self.assertEqual(len(gsc.typeclass_storage), 1)
self.assertIn('script_name', gsc.typeclass_storage)
self.assertEqual(gsc.typeclass_storage['script_name'], GoodScript)
self.assertIn("script_name", gsc.typeclass_storage)
self.assertEqual(gsc.typeclass_storage["script_name"], GoodScript)
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.InvalidScript'}})
@override_settings(
GLOBAL_SCRIPTS={
"script_name": {"typeclass": "evennia.utils.tests.test_containers.InvalidScript"}
}
)
def test_start_with_invalid_script(self):
"""Script class doesn't implement required methods methods"""
gsc = containers.GlobalScriptContainer()
with self.assertRaises(AttributeError) as err:
with self.assertRaises(AttributeError) as err:
gsc.start()
# check for general attribute failure on the invalid class to preserve against future code-rder changes
self.assertTrue(str(err.exception).startswith("type object 'InvalidScript' has no attribute"), err.exception)
self.assertTrue(
str(err.exception).startswith("type object 'InvalidScript' has no attribute"),
err.exception,
)
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.data.broken_script.BrokenScript'}})
@override_settings(
GLOBAL_SCRIPTS={
"script_name": {"typeclass": "evennia.utils.tests.data.broken_script.BrokenScript"}
}
)
def test_start_with_broken_script(self):
"""Un-importable script should traceback"""
gsc = containers.GlobalScriptContainer()
with self.assertRaises(Exception) as err:
with self.assertRaises(Exception) as err:
gsc.start()
# exception raised by imported module
self.assertTrue(str(err.exception).startswith("cannot import name 'nonexistent_module' from 'evennia'"), err.exception)
self.assertTrue(
str(err.exception).startswith("cannot import name 'nonexistent_module' from 'evennia'"),
err.exception,
)

View file

@ -353,12 +353,9 @@ class TestTextToHTMLparser(TestCase):
def test_non_url_with_www(self):
self.assertEqual(
self.parser.convert_urls('Awwww.this should not be highlighted'),
'Awwww.this should not be highlighted'
self.parser.convert_urls("Awwww.this should not be highlighted"),
"Awwww.this should not be highlighted",
)
def test_invalid_www_url(self):
self.assertEqual(
self.parser.convert_urls('www.t'),
'www.t'
)
self.assertEqual(self.parser.convert_urls("www.t"), "www.t")

View file

@ -721,10 +721,10 @@ class TestIntConversions(TestCase):
# basic mapped numbers
self.assertEqual(3, utils.str2int("three"))
self.assertEqual(20, utils.str2int("twenty"))
# multi-place numbers
self.assertEqual(2345, utils.str2int("two thousand, three hundred and forty-five"))
# ordinal numbers
self.assertEqual(1, utils.str2int("1st"))
self.assertEqual(1, utils.str2int("first"))
@ -734,4 +734,4 @@ class TestIntConversions(TestCase):
self.assertEqual(20, utils.str2int("twentieth"))
with self.assertRaises(ValueError):
utils.str2int("not a number")
utils.str2int("not a number")

View file

@ -90,8 +90,10 @@ class TextToHTMLparser(object):
re_url = re.compile(
r'(?<!=")(\b(?:ftp|www|https?)\W+(?:(?!\.(?:\s|$)|&\w+;)[^"\',;$*^\\(){}<>\[\]\s])+)(\.(?:\s|$)|&\w+;|)'
)
re_protocol = re.compile(r'^(?:ftp|https?)://')
re_valid_no_protocol = re.compile(r'^(?:www|ftp)\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9@:%_\+.~#?&//=]*')
re_protocol = re.compile(r"^(?:ftp|https?)://")
re_valid_no_protocol = re.compile(
r"^(?:www|ftp)\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9@:%_\+.~#?&//=]*"
)
re_mxplink = re.compile(r"\|lc(.*?)\|lt(.*?)\|le", re.DOTALL)
re_mxpurl = re.compile(r"\|lu(.*?)\|lt(.*?)\|le", re.DOTALL)
@ -151,20 +153,24 @@ class TextToHTMLparser(object):
"""
m = self.re_url.search(text)
if m:
href = m.group(1)
label = href
# if there is no protocol (i.e. starts with www or ftp)
# prefix with http:// so the link isn't treated as relative
if not self.re_protocol.match(href):
if not self.re_valid_no_protocol.match(href):
return text
href = "http://" + href
rest = m.group(2)
# -> added target to output prevent the web browser from attempting to
# change pages (and losing our webclient session).
return text[:m.start()] + f'<a href="{href}" target="_blank">{label}</a>{rest}' + text[m.end():]
href = m.group(1)
label = href
# if there is no protocol (i.e. starts with www or ftp)
# prefix with http:// so the link isn't treated as relative
if not self.re_protocol.match(href):
if not self.re_valid_no_protocol.match(href):
return text
href = "http://" + href
rest = m.group(2)
# -> added target to output prevent the web browser from attempting to
# change pages (and losing our webclient session).
return (
text[: m.start()]
+ f'<a href="{href}" target="_blank">{label}</a>{rest}'
+ text[m.end() :]
)
else:
return text
return text
def sub_mxp_links(self, match):
"""

View file

@ -59,10 +59,7 @@ PRONOUN_MAPPING = {
"neutral": "mine",
"plural": "ours",
},
"reflexive pronoun": {
"neutral": "myself",
"plural": "ourselves"
}
"reflexive pronoun": {"neutral": "myself", "plural": "ourselves"},
},
"2nd person": {
"subject pronoun": {
@ -80,26 +77,16 @@ PRONOUN_MAPPING = {
"reflexive pronoun": {
"neutral": "yourself",
"plural": "yourselves",
}
},
},
"3rd person": {
"subject pronoun": {
"male": "he",
"female": "she",
"neutral": "it",
"plural": "they"
},
"object pronoun": {
"male": "him",
"female": "her",
"neutral": "it",
"plural": "them"
},
"subject pronoun": {"male": "he", "female": "she", "neutral": "it", "plural": "they"},
"object pronoun": {"male": "him", "female": "her", "neutral": "it", "plural": "them"},
"possessive adjective": {
"male": "his",
"female": "her",
"neutral": "its",
"plural": "their"
"plural": "their",
},
"possessive pronoun": {
"male": "his",
@ -113,166 +100,61 @@ PRONOUN_MAPPING = {
"neutral": "itself",
"plural": "themselves",
},
}
},
}
PRONOUN_TABLE = {
"I": (
"1st person",
("neutral", "male", "female"),
"subject pronoun"
),
"me": (
"1st person",
("neutral", "male", "female"),
"object pronoun"
),
"my": (
"1st person",
("neutral", "male", "female"),
"possessive adjective"
),
"mine": (
"1st person",
("neutral", "male", "female"),
"possessive pronoun"
),
"myself": (
"1st person",
("neutral", "male", "female"),
"reflexive pronoun"
),
"we": (
"1st person",
"plural",
"subject pronoun"
),
"us": (
"1st person",
"plural",
"object pronoun"
),
"our": (
"1st person",
"plural",
"possessive adjective"
),
"ours": (
"1st person",
"plural",
"possessive pronoun"
),
"ourselves": (
"1st person",
"plural",
"reflexive pronoun"
),
"I": ("1st person", ("neutral", "male", "female"), "subject pronoun"),
"me": ("1st person", ("neutral", "male", "female"), "object pronoun"),
"my": ("1st person", ("neutral", "male", "female"), "possessive adjective"),
"mine": ("1st person", ("neutral", "male", "female"), "possessive pronoun"),
"myself": ("1st person", ("neutral", "male", "female"), "reflexive pronoun"),
"we": ("1st person", "plural", "subject pronoun"),
"us": ("1st person", "plural", "object pronoun"),
"our": ("1st person", "plural", "possessive adjective"),
"ours": ("1st person", "plural", "possessive pronoun"),
"ourselves": ("1st person", "plural", "reflexive pronoun"),
"you": (
"2nd person",
("neutral", "male", "female", "plural"),
("subject pronoun", "object pronoun")
),
"your": (
"2nd person",
("neutral", "male", "female", "plural"),
"possessive adjective"
),
"yours": (
"2nd person",
("neutral", "male", "female", "plural"),
"possessive pronoun"
),
"yourself": (
"2nd person",
("neutral", "male", "female"),
"reflexive pronoun"
),
"yourselves": (
"2nd person",
"plural",
"reflexive pronoun"
),
"he": (
"3rd person",
"male",
"subject pronoun"
),
"him": (
"3rd person",
"male",
"object pronoun"
),
"his":(
"3rd person",
"male",
("possessive pronoun","possessive adjective"),
),
"himself": (
"3rd person",
"male",
"reflexive pronoun"
),
"she": (
"3rd person",
"female",
"subject pronoun"
),
"2nd person",
("neutral", "male", "female", "plural"),
("subject pronoun", "object pronoun"),
),
"your": ("2nd person", ("neutral", "male", "female", "plural"), "possessive adjective"),
"yours": ("2nd person", ("neutral", "male", "female", "plural"), "possessive pronoun"),
"yourself": ("2nd person", ("neutral", "male", "female"), "reflexive pronoun"),
"yourselves": ("2nd person", "plural", "reflexive pronoun"),
"he": ("3rd person", "male", "subject pronoun"),
"him": ("3rd person", "male", "object pronoun"),
"his": (
"3rd person",
"male",
("possessive pronoun", "possessive adjective"),
),
"himself": ("3rd person", "male", "reflexive pronoun"),
"she": ("3rd person", "female", "subject pronoun"),
"her": (
"3rd person",
"female",
("object pronoun", "possessive adjective"),
),
"hers": (
"3rd person",
"female",
"possessive pronoun"
),
"herself": (
"3rd person",
"female",
"reflexive pronoun"
),
"3rd person",
"female",
("object pronoun", "possessive adjective"),
),
"hers": ("3rd person", "female", "possessive pronoun"),
"herself": ("3rd person", "female", "reflexive pronoun"),
"it": (
"3rd person",
"neutral",
("subject pronoun", "object pronoun"),
),
"3rd person",
"neutral",
("subject pronoun", "object pronoun"),
),
"its": (
"3rd person",
"neutral",
("possessive pronoun", "possessive adjective"),
),
"itself": (
"3rd person",
"neutral",
"reflexive pronoun"
),
"they": (
"3rd person",
"plural",
"subject pronoun"
),
"them": (
"3rd person",
"plural",
"object pronoun"
),
"their": (
"3rd person",
"plural",
"possessive adjective"
),
"theirs": (
"3rd person",
"plural",
"possessive pronoun"
),
"themselves": (
"3rd person",
"plural",
"reflexive pronoun"
),
"3rd person",
"neutral",
("possessive pronoun", "possessive adjective"),
),
"itself": ("3rd person", "neutral", "reflexive pronoun"),
"they": ("3rd person", "plural", "subject pronoun"),
"them": ("3rd person", "plural", "object pronoun"),
"their": ("3rd person", "plural", "possessive adjective"),
"theirs": ("3rd person", "plural", "possessive pronoun"),
"themselves": ("3rd person", "plural", "reflexive pronoun"),
}
# define the default viewpoint conversions
@ -304,7 +186,11 @@ ALIASES = {
def pronoun_to_viewpoints(
pronoun, options=None, pronoun_type=DEFAULT_PRONOUN_TYPE, gender=DEFAULT_GENDER, viewpoint=DEFAULT_VIEWPOINT
pronoun,
options=None,
pronoun_type=DEFAULT_PRONOUN_TYPE,
gender=DEFAULT_GENDER,
viewpoint=DEFAULT_VIEWPOINT,
):
"""
Access function for determining the forms of a pronount from different viewpoints.
@ -365,7 +251,7 @@ def pronoun_to_viewpoints(
viewpoint = DEFAULT_VIEWPOINT
if gender not in GENDERS:
gender = DEFAULT_GENDER
if options:
# option string/list will override the kwargs differentiators given
if isinstance(options, str):
@ -395,9 +281,9 @@ def pronoun_to_viewpoints(
# special handling for the royal "we"
if is_iter(source_gender):
gender_opts = list(source_gender)
gender_opts = list(source_gender)
else:
gender_opts = [source_gender]
gender_opts = [source_gender]
if viewpoint == "1st person":
# make sure plural is always an option when converting to 1st person
# it doesn't matter if it's in the list twice, so don't bother checking
@ -409,7 +295,7 @@ def pronoun_to_viewpoints(
viewpoint_map = PRONOUN_MAPPING[viewpoint]
pronouns = viewpoint_map.get(pronoun_type, viewpoint_map[DEFAULT_PRONOUN_TYPE])
mapped_pronoun = pronouns.get(gender, pronouns[DEFAULT_GENDER])
# keep the same capitalization as the original
if pronoun != "I":
# don't remap I, since this is always capitalized.

View file

@ -279,7 +279,7 @@ class TestPronounMapping(TestCase):
("you", "m", "you", "he"),
("you", "f op", "you", "her"),
("I", "", "I", "it"),
("I", "p", "I", "it"), # plural is invalid
("I", "p", "I", "it"), # plural is invalid
("I", "m", "I", "he"),
("Me", "n", "Me", "It"),
("your", "p", "your", "their"),