Skip to content

Commit 7e4c733

Browse files
committed
Cleanup unused code & small fixes
1 parent 5c00e80 commit 7e4c733

File tree

6 files changed

+27
-97
lines changed

6 files changed

+27
-97
lines changed

computercraft/errors.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
class CommandException(Exception):
2-
pass
3-
4-
51
class LuaException(Exception):
62
pass
73

computercraft/rproc.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
def coro(result):
55
assert isinstance(result, list)
6-
if len(result) < 2:
7-
result.append(None)
8-
assert len(result) == 2
9-
success, result = result
6+
assert len(result) >= 1
7+
success, *result = result
108
assert isinstance(success, bool)
119
if not success:
1210
raise LuaException(result)
11+
if result == []:
12+
return None
13+
if len(result) == 1:
14+
return result[0]
1315
return result
1416

1517

@@ -52,10 +54,13 @@ def any_list(result):
5254
return result
5355

5456

55-
def fact_tuple(*components):
57+
def fact_tuple(*components, tail_nils=0):
5658
def proc(result):
5759
result = any_list(result)
58-
assert len(components) == result
60+
assert len(components) + tail_nils >= len(result) >= len(components)
61+
while len(result) < len(components):
62+
result.append(None)
63+
assert len(components) == len(result)
5964
return tuple(comp(value) for comp, value in zip(components, result))
6065
return proc
6166

computercraft/subapis/base.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..errors import CommandException, ApiException
1+
from ..errors import ApiException
22
from typing import Union
33

44

@@ -63,64 +63,6 @@ def lua_args(*params, omit_nulls=True):
6363
return ', '.join(ps)
6464

6565

66-
def bool_success(v):
67-
if v == [True]:
68-
return
69-
if len(v) == 2 and v[0] is False:
70-
assert isinstance(v[1], str)
71-
raise CommandException(v[1])
72-
raise ApiException('Bad return value: {}'.format(v))
73-
74-
75-
def single_return(v):
76-
assert len(v) == 1
77-
return v[0]
78-
79-
80-
def make_optional(fn):
81-
def op_fn(v):
82-
if v == []:
83-
return None
84-
return fn(v)
85-
return op_fn
86-
87-
88-
def make_single_value_return(validator, converter=None):
89-
def fn(v):
90-
assert len(v) == 1
91-
if converter is not None:
92-
v[0] = converter(v[0])
93-
assert validator(v[0])
94-
return v[0]
95-
return fn
96-
97-
98-
bool_return = make_single_value_return(lambda v: isinstance(v, bool))
99-
int_return = make_single_value_return(lambda v: isinstance(v, int) and not isinstance(v, bool))
100-
number_return = make_single_value_return(lambda v: isinstance(v, (int, float)) and not isinstance(v, bool))
101-
str_return = make_single_value_return(lambda v: isinstance(v, str))
102-
str_bool_return = make_single_value_return(lambda v: isinstance(v, (str, bool)))
103-
list_return = make_single_value_return(
104-
lambda v: isinstance(v, list),
105-
lambda v: [] if v == {} else v,
106-
)
107-
dict_return = make_single_value_return(lambda v: isinstance(v, dict))
108-
109-
110-
@make_optional
111-
def nil_return(v):
112-
assert False
113-
114-
115-
opt_bool_return = make_optional(bool_return)
116-
opt_int_return = make_optional(int_return)
117-
opt_number_return = make_optional(number_return)
118-
opt_str_return = make_optional(str_return)
119-
opt_str_bool_return = make_optional(str_bool_return)
120-
opt_list_return = make_optional(list_return)
121-
opt_dict_return = make_optional(dict_return)
122-
123-
12466
class BaseSubAPI:
12567
_API = NotImplemented
12668

computercraft/subapis/commands.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
from typing import Tuple, List, Optional
22

33
from .base import BaseSubAPI
4-
from ..errors import CommandException
5-
from ..rproc import tuple3_integer, any_dict, any_list, array_string, integer
4+
from ..rproc import tuple3_integer, any_dict, any_list, array_string, integer, fact_tuple, boolean, option_integer
65

76

8-
class CommandsAPI(BaseSubAPI):
9-
_API = 'commands'
10-
11-
async def exec(self, command: str) -> Tuple[str, Optional[int]]:
12-
# TODO: use rproc
13-
r = await self._send('exec', command)
14-
assert len(r) == 3
15-
assert isinstance(r[0], bool)
7+
command_result = fact_tuple(boolean, array_string, option_integer, tail_nils=1)
168

17-
if r[1] == {}:
18-
r[1] = []
19-
assert isinstance(r[1], list)
20-
r[1] = '\n'.join(r[1])
219

22-
if r[2] is None:
23-
r[2] = 0
24-
assert isinstance(r[2], int)
25-
26-
if r[0] is False:
27-
raise CommandException(r[1])
10+
class CommandsAPI(BaseSubAPI):
11+
_API = 'commands'
2812

29-
return r[1], r[2]
13+
async def exec(self, command: str) -> Tuple[bool, List[str], Optional[int]]:
14+
return command_result(await self._send('exec', command))
3015

3116
async def execAsync(self, command: str) -> int:
3217
return integer(await self._send('execAsync', command))

computercraft/subapis/peripheral.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Optional, List, Tuple, Any
22

3-
from .base import BaseSubAPI, bool_success
3+
from .base import BaseSubAPI
44
from .mixins import TermMixin
55
from ..rproc import boolean, nil, integer, string, option_integer, option_string, tuple2_integer, array_string
66

testmod.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from time import monotonic
44
from types import FunctionType
55

6-
from computercraft.errors import LuaException, CommandException
6+
from computercraft.errors import LuaException
77

88

99
async def hello(api):
@@ -257,14 +257,16 @@ async def test_commands_api(api):
257257
for c in cmdlist:
258258
assert isinstance(c, str)
259259

260-
assert await api.commands.exec('say Hello!') == ('', AnyInstanceOf(int))
260+
assert await api.commands.exec('say Hello!') == (True, [], AnyInstanceOf(int))
261261

262-
with assert_raises(CommandException):
263-
await api.commands.exec('tp hajejndlasksdkelefsns fjeklaskslekffjslas')
262+
d = await api.commands.exec('tp hajejndlasksdkelefsns fjeklaskslekffjslas')
263+
assert d[0] is False
264264

265265
d = await api.commands.exec('difficulty')
266-
assert d[0].startswith('The difficulty is ')
267-
assert isinstance(d[1], int)
266+
assert d[0] is True
267+
assert len(d[1]) == 1
268+
assert d[1][0].startswith('The difficulty is ')
269+
assert isinstance(d[2], int)
268270

269271
await api.print('Test finished successfully')
270272

0 commit comments

Comments
 (0)