Skip to content

Commit 4746661

Browse files
committed
Refactor LuaExpr passing & BasePeripheral
1 parent 823c14e commit 4746661

File tree

23 files changed

+94
-122
lines changed

23 files changed

+94
-122
lines changed

computercraft/lua.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ def lua_value(v):
5757
raise ValueError('Can\'t convert a value to lua {}'.format(v))
5858

5959

60-
def lua_args(*params, omit_nulls=True):
61-
if omit_nulls:
62-
for idx in range(len(params) - 1, -1, -1):
63-
if params[idx] is not None:
64-
break
65-
else:
66-
idx = -1
67-
params = params[:idx + 1]
60+
def lua_args(*params):
61+
for idx in range(len(params) - 1, -1, -1):
62+
if params[idx] is not None:
63+
break
64+
else:
65+
idx = -1
66+
params = params[:idx + 1]
6867
return ', '.join(lua_value(p) for p in params)

computercraft/server.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,25 @@ def __init__(self, nid, program, cleanup_callback):
5555
self._event_to_tids = {}
5656
self._tid_to_event = {}
5757

58-
self.colors = ColorsAPI(self)
59-
self.commands = CommandsAPI(self)
60-
self.disk = DiskAPI(self)
61-
self.fs = FSAPI(self)
62-
self.gps = GpsAPI(self)
63-
self.help = HelpAPI(self)
64-
self.keys = KeysAPI(self)
65-
self.multishell = MultishellAPI(self)
66-
self.os = OSAPI(self)
67-
self.peripheral = PeripheralAPI(self)
68-
self.pocket = PocketAPI(self)
69-
self.rednet = RednetAPI(self)
70-
self.redstone = RedstoneAPI(self)
71-
self.settings = SettingsAPI(self)
72-
self.shell = ShellAPI(self) # TODO: autocomplete functions
73-
self.term = TermAPI(self) # TODO: window redirections
74-
self.textutils = TextutilsAPI(self)
75-
self.turtle = TurtleAPI(self)
76-
self.window = WindowAPI(self) # TODO: unimplemented
58+
self.colors = ColorsAPI(self, 'colors')
59+
self.commands = CommandsAPI(self, 'commands')
60+
self.disk = DiskAPI(self, 'disk')
61+
self.fs = FSAPI(self, 'fs')
62+
self.gps = GpsAPI(self, 'gps')
63+
self.help = HelpAPI(self, 'help')
64+
self.keys = KeysAPI(self, 'keys')
65+
self.multishell = MultishellAPI(self, 'multishell')
66+
self.os = OSAPI(self, 'os')
67+
self.peripheral = PeripheralAPI(self, 'peripheral')
68+
self.pocket = PocketAPI(self, 'pocket')
69+
self.rednet = RednetAPI(self, 'rednet')
70+
self.redstone = RedstoneAPI(self, 'redstone')
71+
self.settings = SettingsAPI(self, 'settings')
72+
self.shell = ShellAPI(self, 'shell') # TODO: autocomplete functions
73+
self.term = TermAPI(self, 'term') # TODO: window redirections
74+
self.textutils = TextutilsAPI(self, 'textutils')
75+
self.turtle = TurtleAPI(self, 'turtle')
76+
self.window = WindowAPI(self, 'window') # TODO: unimplemented
7777

7878
async def prog_wrap():
7979
err = None

computercraft/subapis/base.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
from ..lua import lua_args
1+
from ..lua import LuaExpr, lua_args
22

33

4-
class BaseSubAPI:
5-
_API = NotImplemented
6-
7-
def __init__(self, cc):
4+
class BaseSubAPI(LuaExpr):
5+
def __init__(self, cc, lua_expr):
86
self._cc = cc
7+
self._lua_expr = lua_expr
8+
9+
def get_expr_code(self):
10+
return self._lua_expr
11+
12+
async def _send(self, method, *params):
13+
return await self._method(method, *params)
914

10-
async def _send(self, method, *params, omit_nulls=True):
15+
async def _method(self, name, *params):
1116
return await self._cc.eval_coro('return {}.{}({})'.format(
12-
self._API, method, lua_args(*params, omit_nulls=omit_nulls)
17+
self._lua_expr, name, lua_args(*params),
1318
))

computercraft/subapis/colors.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66

77
class ColorsAPI(BaseSubAPI):
8-
_API = 'colors'
9-
108
white = 0x1
119
orange = 0x2
1210
magenta = 0x4

computercraft/subapis/commands.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99

1010
class CommandsAPI(BaseSubAPI):
11-
_API = 'commands'
12-
1311
async def exec(self, command: str) -> Tuple[bool, List[str], Optional[int]]:
1412
return command_result(await self._send('exec', command))
1513

computercraft/subapis/disk.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66

77
class DiskAPI(BaseSubAPI):
8-
_API = 'disk'
9-
108
async def isPresent(self, side: str) -> bool:
119
return boolean(await self._send('isPresent', side))
1210

computercraft/subapis/fs.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional, List
33

44
from .base import BaseSubAPI
5-
from ..lua import LuaExpr, lua_string
5+
from ..lua import lua_string
66
from ..rproc import boolean, string, integer, nil, array_string, option_string, fact_scheme_dict
77

88

@@ -14,16 +14,7 @@
1414
}, {})
1515

1616

17-
class BaseHandle(BaseSubAPI, LuaExpr):
18-
def __init__(self, cc, var):
19-
super().__init__(cc)
20-
self._API = var
21-
22-
def get_expr_code(self):
23-
return self._API
24-
25-
26-
class ReadHandle(BaseHandle):
17+
class ReadHandle(BaseSubAPI):
2718
async def read(self, count: int) -> Optional[str]:
2819
return option_string(await self._send('read', count))
2920

@@ -43,7 +34,7 @@ async def __anext__(self):
4334
return line
4435

4536

46-
class WriteHandle(BaseHandle):
37+
class WriteHandle(BaseSubAPI):
4738
async def write(self, text: str):
4839
return nil(await self._send('write', text))
4940

@@ -55,8 +46,6 @@ async def flush(self):
5546

5647

5748
class FSAPI(BaseSubAPI):
58-
_API = 'fs'
59-
6049
async def list(self, path: str) -> List[str]:
6150
return array_string(await self._send('list', path))
6251

@@ -111,7 +100,7 @@ async def open(self, path: str, mode: str):
111100
fid = self._cc._new_task_id()
112101
var = 'temp[{}]'.format(lua_string(fid))
113102
await self._cc.eval_coro('{} = fs.open({}, {})'.format(
114-
var, *map(lua_string, [path, mode])))
103+
var, lua_string(path), lua_string(mode)))
115104
try:
116105
yield (ReadHandle if 'r' in mode else WriteHandle)(self._cc, var)
117106
finally:
@@ -133,7 +122,7 @@ async def complete(
133122
self, partialName: str, path: str, includeFiles: bool = None, includeDirs: bool = None,
134123
) -> List[str]:
135124
return array_string(await self._send(
136-
'complete', partialName, path, includeFiles, includeDirs, omit_nulls=False))
125+
'complete', partialName, path, includeFiles, includeDirs))
137126

138127
async def attributes(self, path: str) -> dict:
139128
return attribute(await self._send('attributes', path))

computercraft/subapis/gps.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010

1111
class GpsAPI(BaseSubAPI):
12-
_API = 'gps'
13-
1412
CHANNEL_GPS = 65534
1513

1614
async def locate(self, timeout: LuaNum = None, debug: bool = None) -> Optional[Tuple[LuaNum, LuaNum, LuaNum]]:
17-
return option_tuple3_number(await self._send('locate', timeout, debug, omit_nulls=False))
15+
return option_tuple3_number(await self._send('locate', timeout, debug))

computercraft/subapis/help.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66

77
class HelpAPI(BaseSubAPI):
8-
_API = 'help'
9-
108
async def path(self) -> str:
119
return string(await self._send('path'))
1210

computercraft/subapis/keys.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66

77

88
class KeysAPI(BaseSubAPI):
9-
_API = 'keys'
10-
119
async def getCode(self, name: str) -> Optional[int]:
1210
# replaces properties
1311
# keys.space → await api.keys.getCode('space')
1412
return option_integer(await self._cc.eval_coro('''
15-
if type({api}[{key}]) == 'number' then
16-
return {api}[{key}]
13+
if type({module}[{key}]) == 'number' then
14+
return {module}[{key}]
1715
end
18-
return nil'''.format(api=self._API, key=lua_string(name))))
16+
return nil'''.format(module=self.get_expr_code(), key=lua_string(name))))
1917

2018
async def getName(self, code: int) -> Optional[str]:
2119
return option_string(await self._send('getName', code))

0 commit comments

Comments
 (0)