Fix typo, run black

This commit is contained in:
Griatch 2019-09-30 19:05:09 +02:00
parent 2cf49882de
commit 040bf50703
2 changed files with 183 additions and 112 deletions

View file

@ -53,7 +53,8 @@ __all__ = (
# used by set # used by set
from ast import literal_eval as _LITERAL_EVAL from ast import literal_eval as _LITERAL_EVAL
LIST_APPEND_CHAR = '+'
LIST_APPEND_CHAR = "+"
# used by find # used by find
CHAR_TYPECLASS = settings.BASE_CHARACTER_TYPECLASS CHAR_TYPECLASS = settings.BASE_CHARACTER_TYPECLASS
@ -1594,8 +1595,8 @@ class CmdSetAttribute(ObjManipCommand):
by you starting your value with one of |c'|n, |c"|n, |c(|n, |c[|n by you starting your value with one of |c'|n, |c"|n, |c(|n, |c[|n
or |c{ |n. or |c{ |n.
Once you have stored a Python primative as noted above, you can include Once you have stored a Python primitive as noted above, you can include
|c[<key>]|n in <attr> to reference nested values. |c[<key>]|n in <attr> to reference nested values in e.g. a list or dict.
Remember that if you use Python primitives like this, you must Remember that if you use Python primitives like this, you must
write proper Python syntax too - notably you must include quotes write proper Python syntax too - notably you must include quotes
@ -1606,7 +1607,7 @@ class CmdSetAttribute(ObjManipCommand):
key = "set" key = "set"
locks = "cmd:perm(set) or perm(Builder)" locks = "cmd:perm(set) or perm(Builder)"
help_category = "Building" help_category = "Building"
nested_re = re.compile(r'\[.*?\]') nested_re = re.compile(r"\[.*?\]")
not_found = object() not_found = object()
def check_obj(self, obj): def check_obj(self, obj):
@ -1645,10 +1646,10 @@ class CmdSetAttribute(ObjManipCommand):
("nested['asdf'][0]", []), ("nested['asdf'][0]", []),
] ]
""" """
quotes = '"\'' quotes = "\"'"
def clean_key(val): def clean_key(val):
val = val.strip('[]') val = val.strip("[]")
if val[0] in quotes: if val[0] in quotes:
return val.strip(quotes) return val.strip(quotes)
if val[0] == LIST_APPEND_CHAR: if val[0] == LIST_APPEND_CHAR:
@ -1661,9 +1662,9 @@ class CmdSetAttribute(ObjManipCommand):
parts = self.nested_re.findall(attr) parts = self.nested_re.findall(attr)
base_attr = '' base_attr = ""
if parts: if parts:
base_attr = attr[:attr.find(parts[0])] base_attr = attr[: attr.find(parts[0])]
for index, part in enumerate(parts): for index, part in enumerate(parts):
yield (base_attr, [clean_key(p) for p in parts[index:]]) yield (base_attr, [clean_key(p) for p in parts[index:]])
base_attr += part base_attr += part
@ -1692,7 +1693,7 @@ class CmdSetAttribute(ObjManipCommand):
return "\nAttribute %s/%s = %s" % (obj.name, attr, val) return "\nAttribute %s/%s = %s" % (obj.name, attr, val)
error = "\n%s has no attribute '%s'." % (obj.name, attr) error = "\n%s has no attribute '%s'." % (obj.name, attr)
if nested: if nested:
error += ' (Nested lookups attempted)' error += " (Nested lookups attempted)"
return error return error
def rm_attr(self, obj, attr): def rm_attr(self, obj, attr):
@ -1719,7 +1720,7 @@ class CmdSetAttribute(ObjManipCommand):
return "\nDeleted attribute '%s' (= %s) from %s." % (attr, exists, obj.name) return "\nDeleted attribute '%s' (= %s) from %s." % (attr, exists, obj.name)
error = "\n%s has no attribute '%s'." % (obj.name, attr) error = "\n%s has no attribute '%s'." % (obj.name, attr)
if nested: if nested:
error += ' (Nested lookups attempted)' error += " (Nested lookups attempted)"
return error return error
def set_attr(self, obj, attr, value): def set_attr(self, obj, attr, value):

View file

@ -537,138 +537,208 @@ class TestBuilding(CommandTest):
def test_nested_attribute_commands(self): def test_nested_attribute_commands(self):
# list - adding white space proves real parsing # list - adding white space proves real parsing
self.call(building.CmdSetAttribute(), "Obj/test1=[1,2]", "Created attribute Obj/test1 = [1, 2]") self.call(
building.CmdSetAttribute(), "Obj/test1=[1,2]", "Created attribute Obj/test1 = [1, 2]"
)
self.call(building.CmdSetAttribute(), "Obj/test1", "Attribute Obj/test1 = [1, 2]") self.call(building.CmdSetAttribute(), "Obj/test1", "Attribute Obj/test1 = [1, 2]")
self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 1") self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 1")
self.call(building.CmdSetAttribute(), "Obj/test1[1]", "Attribute Obj/test1[1] = 2") self.call(building.CmdSetAttribute(), "Obj/test1[1]", "Attribute Obj/test1[1] = 2")
self.call(building.CmdSetAttribute(), "Obj/test1[0] = 99", "Modified attribute Obj/test1 = [99, 2]") self.call(
building.CmdSetAttribute(),
"Obj/test1[0] = 99",
"Modified attribute Obj/test1 = [99, 2]",
)
self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 99") self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 99")
# list delete # list delete
self.call(building.CmdSetAttribute(), self.call(
"Obj/test1[0] =", building.CmdSetAttribute(),
"Deleted attribute 'test1[0]' (= nested) from Obj.") "Obj/test1[0] =",
"Deleted attribute 'test1[0]' (= nested) from Obj.",
)
self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 2") self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 2")
self.call(building.CmdSetAttribute(), self.call(
"Obj/test1[1]", building.CmdSetAttribute(),
"Obj has no attribute 'test1[1]'. (Nested lookups attempted)") "Obj/test1[1]",
"Obj has no attribute 'test1[1]'. (Nested lookups attempted)",
)
# Delete non-existent # Delete non-existent
self.call(building.CmdSetAttribute(), self.call(
"Obj/test1[5] =", building.CmdSetAttribute(),
"Obj has no attribute 'test1[5]'. (Nested lookups attempted)") "Obj/test1[5] =",
"Obj has no attribute 'test1[5]'. (Nested lookups attempted)",
)
# Append # Append
self.call(building.CmdSetAttribute(), self.call(
"Obj/test1[+] = 42", building.CmdSetAttribute(),
"Modified attribute Obj/test1 = [2, 42]") "Obj/test1[+] = 42",
self.call(building.CmdSetAttribute(), "Modified attribute Obj/test1 = [2, 42]",
"Obj/test1[+0] = -1", )
"Modified attribute Obj/test1 = [-1, 2, 42]") self.call(
building.CmdSetAttribute(),
"Obj/test1[+0] = -1",
"Modified attribute Obj/test1 = [-1, 2, 42]",
)
# dict - removing white space proves real parsing # dict - removing white space proves real parsing
self.call(building.CmdSetAttribute(), self.call(
"Obj/test2={ 'one': 1, 'two': 2 }", building.CmdSetAttribute(),
"Created attribute Obj/test2 = {'one': 1, 'two': 2}") "Obj/test2={ 'one': 1, 'two': 2 }",
self.call(building.CmdSetAttribute(), "Obj/test2", "Attribute Obj/test2 = {'one': 1, 'two': 2}") "Created attribute Obj/test2 = {'one': 1, 'two': 2}",
)
self.call(
building.CmdSetAttribute(), "Obj/test2", "Attribute Obj/test2 = {'one': 1, 'two': 2}"
)
self.call(building.CmdSetAttribute(), "Obj/test2['one']", "Attribute Obj/test2['one'] = 1") self.call(building.CmdSetAttribute(), "Obj/test2['one']", "Attribute Obj/test2['one'] = 1")
self.call(building.CmdSetAttribute(), "Obj/test2['one]", "Attribute Obj/test2['one] = 1") self.call(building.CmdSetAttribute(), "Obj/test2['one]", "Attribute Obj/test2['one] = 1")
self.call(building.CmdSetAttribute(), self.call(
"Obj/test2['one']=99", building.CmdSetAttribute(),
"Modified attribute Obj/test2 = {'one': 99, 'two': 2}") "Obj/test2['one']=99",
"Modified attribute Obj/test2 = {'one': 99, 'two': 2}",
)
self.call(building.CmdSetAttribute(), "Obj/test2['one']", "Attribute Obj/test2['one'] = 99") self.call(building.CmdSetAttribute(), "Obj/test2['one']", "Attribute Obj/test2['one'] = 99")
self.call(building.CmdSetAttribute(), "Obj/test2['two']", "Attribute Obj/test2['two'] = 2") self.call(building.CmdSetAttribute(), "Obj/test2['two']", "Attribute Obj/test2['two'] = 2")
self.call(building.CmdSetAttribute(), self.call(
"Obj/test2[+'three']", building.CmdSetAttribute(),
"Obj has no attribute 'test2[+'three']'. (Nested lookups attempted)") "Obj/test2[+'three']",
self.call(building.CmdSetAttribute(), "Obj has no attribute 'test2[+'three']'. (Nested lookups attempted)",
"Obj/test2[+'three'] = 3", )
"Modified attribute Obj/test2 = {'one': 99, 'two': 2, \"+'three'\": 3}") self.call(
self.call(building.CmdSetAttribute(), building.CmdSetAttribute(),
"Obj/test2[+'three'] =", "Obj/test2[+'three'] = 3",
"Deleted attribute 'test2[+'three']' (= nested) from Obj.") "Modified attribute Obj/test2 = {'one': 99, 'two': 2, \"+'three'\": 3}",
self.call(building.CmdSetAttribute(), )
"Obj/test2['three']=3", self.call(
"Modified attribute Obj/test2 = {'one': 99, 'two': 2, 'three': 3}") building.CmdSetAttribute(),
"Obj/test2[+'three'] =",
"Deleted attribute 'test2[+'three']' (= nested) from Obj.",
)
self.call(
building.CmdSetAttribute(),
"Obj/test2['three']=3",
"Modified attribute Obj/test2 = {'one': 99, 'two': 2, 'three': 3}",
)
# Dict delete # Dict delete
self.call(building.CmdSetAttribute(), self.call(
"Obj/test2['two'] =", building.CmdSetAttribute(),
"Deleted attribute 'test2['two']' (= nested) from Obj.") "Obj/test2['two'] =",
self.call(building.CmdSetAttribute(), "Deleted attribute 'test2['two']' (= nested) from Obj.",
"Obj/test2['two']", )
"Obj has no attribute 'test2['two']'. (Nested lookups attempted)") self.call(
self.call(building.CmdSetAttribute(), "Obj/test2", "Attribute Obj/test2 = {'one': 99, 'three': 3}") building.CmdSetAttribute(),
self.call(building.CmdSetAttribute(), "Obj/test2['two']",
"Obj/test2[0]", "Obj has no attribute 'test2['two']'. (Nested lookups attempted)",
"Obj has no attribute 'test2[0]'. (Nested lookups attempted)") )
self.call(building.CmdSetAttribute(), self.call(
"Obj/test2['five'] =", building.CmdSetAttribute(), "Obj/test2", "Attribute Obj/test2 = {'one': 99, 'three': 3}"
"Obj has no attribute 'test2['five']'. (Nested lookups attempted)") )
self.call(building.CmdSetAttribute(), self.call(
"Obj/test2[+]=42", building.CmdSetAttribute(),
"Modified attribute Obj/test2 = {'one': 99, 'three': 3, '+': 42}") "Obj/test2[0]",
self.call(building.CmdSetAttribute(), "Obj has no attribute 'test2[0]'. (Nested lookups attempted)",
"Obj/test2[+1]=33", )
"Modified attribute Obj/test2 = {'one': 99, 'three': 3, '+': 42, '+1': 33}") self.call(
building.CmdSetAttribute(),
"Obj/test2['five'] =",
"Obj has no attribute 'test2['five']'. (Nested lookups attempted)",
)
self.call(
building.CmdSetAttribute(),
"Obj/test2[+]=42",
"Modified attribute Obj/test2 = {'one': 99, 'three': 3, '+': 42}",
)
self.call(
building.CmdSetAttribute(),
"Obj/test2[+1]=33",
"Modified attribute Obj/test2 = {'one': 99, 'three': 3, '+': 42, '+1': 33}",
)
# tuple # tuple
self.call(building.CmdSetAttribute(), "Obj/tup = (1,2)", "Created attribute Obj/tup = (1, 2)") self.call(
self.call(building.CmdSetAttribute(), building.CmdSetAttribute(), "Obj/tup = (1,2)", "Created attribute Obj/tup = (1, 2)"
"Obj/tup[1] = 99", )
"'tuple' object does not support item assignment - (1, 2)") self.call(
self.call(building.CmdSetAttribute(), building.CmdSetAttribute(),
"Obj/tup[+] = 99", "Obj/tup[1] = 99",
"'tuple' object does not support item assignment - (1, 2)") "'tuple' object does not support item assignment - (1, 2)",
self.call(building.CmdSetAttribute(), )
"Obj/tup[+1] = 99", self.call(
"'tuple' object does not support item assignment - (1, 2)") building.CmdSetAttribute(),
self.call(building.CmdSetAttribute(), "Obj/tup[+] = 99",
# Special case for tuple, could have a better message "'tuple' object does not support item assignment - (1, 2)",
"Obj/tup[1] = ", )
"Obj has no attribute 'tup[1]'. (Nested lookups attempted)") self.call(
building.CmdSetAttribute(),
"Obj/tup[+1] = 99",
"'tuple' object does not support item assignment - (1, 2)",
)
self.call(
building.CmdSetAttribute(),
# Special case for tuple, could have a better message
"Obj/tup[1] = ",
"Obj has no attribute 'tup[1]'. (Nested lookups attempted)",
)
# Deaper nesting # Deaper nesting
self.call(building.CmdSetAttribute(), self.call(
"Obj/test3=[{'one': 1}]", building.CmdSetAttribute(),
"Created attribute Obj/test3 = [{'one': 1}]") "Obj/test3=[{'one': 1}]",
self.call(building.CmdSetAttribute(), "Obj/test3[0]['one']", "Attribute Obj/test3[0]['one'] = 1") "Created attribute Obj/test3 = [{'one': 1}]",
)
self.call(
building.CmdSetAttribute(), "Obj/test3[0]['one']", "Attribute Obj/test3[0]['one'] = 1"
)
self.call(building.CmdSetAttribute(), "Obj/test3[0]", "Attribute Obj/test3[0] = {'one': 1}") self.call(building.CmdSetAttribute(), "Obj/test3[0]", "Attribute Obj/test3[0] = {'one': 1}")
self.call(building.CmdSetAttribute(), self.call(
"Obj/test3[0]['one'] =", building.CmdSetAttribute(),
"Deleted attribute 'test3[0]['one']' (= nested) from Obj.") "Obj/test3[0]['one'] =",
"Deleted attribute 'test3[0]['one']' (= nested) from Obj.",
)
self.call(building.CmdSetAttribute(), "Obj/test3[0]", "Attribute Obj/test3[0] = {}") self.call(building.CmdSetAttribute(), "Obj/test3[0]", "Attribute Obj/test3[0] = {}")
self.call(building.CmdSetAttribute(), "Obj/test3", "Attribute Obj/test3 = [{}]") self.call(building.CmdSetAttribute(), "Obj/test3", "Attribute Obj/test3 = [{}]")
# Naughty keys # Naughty keys
self.call(building.CmdSetAttribute(), self.call(
"Obj/test4[0]='foo'", building.CmdSetAttribute(),
"Created attribute Obj/test4[0] = 'foo'") "Obj/test4[0]='foo'",
"Created attribute Obj/test4[0] = 'foo'",
)
self.call(building.CmdSetAttribute(), "Obj/test4[0]", "Attribute Obj/test4[0] = foo") self.call(building.CmdSetAttribute(), "Obj/test4[0]", "Attribute Obj/test4[0] = foo")
self.call(building.CmdSetAttribute(), self.call(
"Obj/test4=[{'one': 1}]", building.CmdSetAttribute(),
"Created attribute Obj/test4 = [{'one': 1}]") "Obj/test4=[{'one': 1}]",
self.call(building.CmdSetAttribute(), "Obj/test4[0]['one']", "Attribute Obj/test4[0]['one'] = 1") "Created attribute Obj/test4 = [{'one': 1}]",
)
self.call(
building.CmdSetAttribute(), "Obj/test4[0]['one']", "Attribute Obj/test4[0]['one'] = 1"
)
# Prefer nested items # Prefer nested items
self.call(building.CmdSetAttribute(), "Obj/test4[0]", "Attribute Obj/test4[0] = {'one': 1}") self.call(building.CmdSetAttribute(), "Obj/test4[0]", "Attribute Obj/test4[0] = {'one': 1}")
self.call(building.CmdSetAttribute(), "Obj/test4[0]['one']", "Attribute Obj/test4[0]['one'] = 1") self.call(
building.CmdSetAttribute(), "Obj/test4[0]['one']", "Attribute Obj/test4[0]['one'] = 1"
)
# Restored access # Restored access
self.call(building.CmdWipe(), "Obj/test4", "Wiped attributes test4 on Obj.") self.call(building.CmdWipe(), "Obj/test4", "Wiped attributes test4 on Obj.")
self.call(building.CmdSetAttribute(), "Obj/test4[0]", "Attribute Obj/test4[0] = foo") self.call(building.CmdSetAttribute(), "Obj/test4[0]", "Attribute Obj/test4[0] = foo")
self.call(building.CmdSetAttribute(), self.call(
"Obj/test4[0]['one']", building.CmdSetAttribute(),
"Obj has no attribute 'test4[0]['one']'.") "Obj/test4[0]['one']",
"Obj has no attribute 'test4[0]['one']'.",
)
def test_split_nested_attr(self): def test_split_nested_attr(self):
split_nested_attr = building.CmdSetAttribute().split_nested_attr split_nested_attr = building.CmdSetAttribute().split_nested_attr
test_cases = { test_cases = {
'test1': [('test1', [])], "test1": [("test1", [])],
'test2["dict"]': [('test2', ['dict']), ('test2["dict"]', [])], 'test2["dict"]': [("test2", ["dict"]), ('test2["dict"]', [])],
# Quotes not actually required # Quotes not actually required
'test3[dict]': [('test3', ['dict']), ('test3[dict]', [])], "test3[dict]": [("test3", ["dict"]), ("test3[dict]", [])],
'test4["dict]': [('test4', ['dict']), ('test4["dict]', [])], 'test4["dict]': [("test4", ["dict"]), ('test4["dict]', [])],
# duplicate keys don't cause issues # duplicate keys don't cause issues
'test5[0][0]': [('test5', [0, 0]), ('test5[0]', [0]), ('test5[0][0]', [])], "test5[0][0]": [("test5", [0, 0]), ("test5[0]", [0]), ("test5[0][0]", [])],
# String ints preserved # String ints preserved
'test6["0"][0]': [('test6', ['0', 0]), ('test6["0"]', [0]), ('test6["0"][0]', [])], 'test6["0"][0]': [("test6", ["0", 0]), ('test6["0"]', [0]), ('test6["0"][0]', [])],
# Unmatched [] # Unmatched []
'test7[dict': [('test7[dict', [])], "test7[dict": [("test7[dict", [])],
} }
for attr, result in test_cases.items(): for attr, result in test_cases.items():
@ -684,19 +754,19 @@ class TestBuilding(CommandTest):
def do_test_multi(value, keys, result): def do_test_multi(value, keys, result):
self.assertEqual(do_nested_lookup(value, *keys), result) self.assertEqual(do_nested_lookup(value, *keys), result)
do_test_single([], 'test1', not_found) do_test_single([], "test1", not_found)
do_test_single([1], 'test2', not_found) do_test_single([1], "test2", not_found)
do_test_single([], 0, not_found) do_test_single([], 0, not_found)
do_test_single([], '0', not_found) do_test_single([], "0", not_found)
do_test_single([1], 2, not_found) do_test_single([1], 2, not_found)
do_test_single([1], 0, 1) do_test_single([1], 0, 1)
do_test_single([1], '0', not_found) # str key is str not int do_test_single([1], "0", not_found) # str key is str not int
do_test_single({}, 'test3', not_found) do_test_single({}, "test3", not_found)
do_test_single({}, 0, not_found) do_test_single({}, 0, not_found)
do_test_single({'foo': 'bar'}, 'foo', 'bar') do_test_single({"foo": "bar"}, "foo", "bar")
do_test_multi({'one': [1, 2, 3]}, ('one', 0), 1) do_test_multi({"one": [1, 2, 3]}, ("one", 0), 1)
do_test_multi([{}, {'two': 2}, 3], (1, 'two'), 2) do_test_multi([{}, {"two": 2}, 3], (1, "two"), 2)
def test_name(self): def test_name(self):
self.call(building.CmdName(), "", "Usage: ") self.call(building.CmdName(), "", "Usage: ")