8000 Textutils & pocket · neumond/python-computer-craft@92df874 · GitHub
[go: up one dir, main page]

Sk 8000 ip to content

Commit 92df874

Browse files
committed
Textutils & pocket
1 parent 48551da commit 92df874

File tree

6 files changed

+126
-20
lines changed

6 files changed

+126
-20
lines changed

computercraft/rproc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ def proc(result):
120120
array_string = fact_array(string)
121121
option_integer = fact_option(integer)
122122
option_string = fact_option(string)
123+
_try_result = fact_tuple(boolean, option_string, tail_nils=1)
124+
125+
126+
def try_result(result):
127+
success, error_msg = _try_result(result)
128+
if success:
129+
assert error_msg is None
130+
return None
131+
else:
132+
raise LuaException(error_msg)
133+
134+
135+
def flat_try_result(result):
136+
if result is True:
137+
return None
138+
return try_result(result)
123139

124140

125141
option_string_bool = fact_option(fact_union(

computercraft/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .subapis.multishell import MultishellAPI
2121
from .subapis.os import OSAPI
2222
from .subapis.peripheral import PeripheralAPI
23+
from .subapis.pocket import PocketAPI
2324
from .subapis.rednet import RednetAPI
2425
from .subapis.redstone import RedstoneAPI
2526
from .subapis.settings import SettingsAPI
@@ -64,6 +65,7 @@ def __init__(self, nid, program, cleanup_callback):
6465
self.multishell = MultishellAPI(self)
6566
self.os = OSAPI(self)
6667
self.peripheral = PeripheralAPI(self)
68+
self.pocket = PocketAPI(self)
6769
self.rednet = RednetAPI(self)
6870
self.redstone = RedstoneAPI(self)
6971
self.settings = SettingsAPI(self)

computercraft/subapis/peripheral.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
from .base import BaseSubAPI, LuaNum
66
from .mixins import TermMixin
7-
from ..errors import LuaException
87
from ..rproc import (
98
boolean, nil, integer, string, option_integer, option_string,
10-
tuple2_integer, array_string, option_string_bool, fact_tuple,
9+
tuple2_integer, array_string, option_string_bool, try_result,
1110
)
1211

1312

@@ -217,9 +216,6 @@ async def playSound(self, sound: str, volume: int = 1, pitch: int = 1):
217216
return boolean(await self._send('playSound', sound, volume, pitch))
218217

219218

220-
run_result = fact_tuple(boolean, option_string, tail_nils=1)
221-
222-
223219
class CCCommandBlock(CCPeripheral):
224220
async def getCommand(self) -> str:
225221
return string(await self._send('getCommand'))
@@ -228,11 +224,7 @@ async def setCommand(self, command: str):
228224
return nil(await self._send('setCommand', command))
229225

230226
async def runCommand(self):
231-
success, error_msg = run_result(await self._send('runCommand'))
232-
if not success:
233-
raise LuaException(error_msg)
234-
else:
235-
assert error_msg is None
227+
return try_result(await self._send('runCommand'))
236228

237229

238230
TYPE_MAP = {

computercraft/subapis/pocket.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .base import BaseSubAPI
2+
from ..rproc import flat_try_result
3+
4+
5+
class PocketAPI(BaseSubAPI):
6+
_API = 'pocket'
7+
8+
async def equipBack(self):
9+
return flat_try_result(await self._send('equipBack'))
10+
11+
async def unequipBack(self) -> bool:
12+
return flat_try_result(await self._send('unequipBack'))

computercraft/subapis/textutils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
from typing import List, Union
22

33
from .base import BaseSubAPI, LuaNum
4-
from ..rproc import nil, string, integer, array_string
4+
from ..rproc import nil, string, integer
55

66

77
class TextutilsAPI(BaseSubAPI):
88
_API = 'textutils'
99

10-
async def slowWrite(self, text: str, rate: LuaNum):
10+
async def slowWrite(self, text: str, rate: LuaNum = None):
1111
return nil(await self._send('slowWrite', text, rate))
1212

13-
async def slowPrint(self, text: str, rate: LuaNum):
13+
async def slowPrint(self, text: str, rate: LuaNum = None):
1414
return nil(await self._send('slowPrint', text, rate))
1515

16-
async def formatTime(self, time: LuaNum, twentyFourHour: bool) -> str:
16+
async def formatTime(self, time: LuaNum, twentyFourHour: bool = None) -> str:
1717
return string(await self._send('formatTime', time, twentyFourHour))
1818

1919
async def tabulate(self, *rows_and_colors: Union[list, int]):
@@ -25,13 +25,16 @@ async def pagedTabulate(self, *rows_and_colors: Union[list, int]):
2525
async def pagedPrint(self, text: str, freeLines: int = None) -> int:
2626
return integer(await self._send('pagedPrint', text, freeLines))
2727

28-
async def complete(self, partialName: str, environment: dict = None) -> List[str]:
29-
return array_string(await self._send('complete', partialName, environment))
28+
def complete(self, partial: str, possible: List[str]) -> List[str]:
29+
return [p[len(partial):] for p in possible if p.startswith(partial)]
3030

3131
# Questionable to implement
3232
# serialize
3333
# unserialize
3434

3535
# Will not implement, use pythonic equivalents
3636
# serializeJSON
37+
# unserializeJSON
3738
# urlEncode
39+
# json_null
40+
# empty_json_array

testmod.py

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,10 +1417,6 @@ async def test_commandblock_peripheral(api):
14171417
await api.print('Test finished successfully')
14181418

14191419

1420-
async def test_turtle_peripheral(api):
1421-
raise NotImplementedError
1422-
1423-
14241420
async def test_modem_wrap(api):
14251421
side = 'back'
14261422

@@ -1460,3 +1456,88 @@ async def test_modem_wrap(api):
14601456

14611457
await api.print('You must have heard levelup sound')
14621458
await api.print('Test finished successfully')
1459+
1460+
1461+
async def test_turtle_peripheral(api):
1462+
raise NotImplementedError
1463+
1464+
1465+
async def test_textutils(api):
1466+
assert await api.textutils.slowWrite('write ') is None
1467+
assert await api.textutils.slowWrite('write ', 5) is None
1468+
assert await api.textutils.slowPrint('print') is None
1469+
assert await api.textutils.slowPrint('print', 5) is None
1470+
1471+
assert await api.textutils.formatTime(0) == '0:00 AM'
1472+
assert await api.textutils.formatTime(0, True) == '0:00'
1473+
1474+
table = [
1475+
api.colors.red,
1476+
['Planet', 'Distance', 'Mass'],
1477+
api.colors.gray,
1478+
['Mercury', '0.387', '0.055'],
1479+
api.colors.lightGray,
1480+
['Venus', '0.723', '0.815'],
1481+
api.colors.green,
1482+
['Earth', '1.000', '1.000'],
1483+
api.colors.red,
1484+
['Mars', '1.524', '0.107'],
1485+
api.colors.orange,
1486+
['Jupiter', '5.203', '318'],
1487+
api.colors.yellow,
1488+
['Saturn', '9.537', '95'],
1489+
api.colors.cyan,
1490+
['Uranus', '19.191', '14.5'],
1491+
api.colors.blue,
1492+
['Neptune', '30.069', '17'],
1493+
api.colors.white,
1494+
]
1495+
1496+
assert await api.textutils.tabulate(*table) is None
1497+
1498+
lines = await api.textutils.pagedPrint('''
1499+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
1500+
Suspendisse feugiat diam et velit aliquam, nec porttitor eros facilisis.
1501+
Nulla facilisi.
1502+
Sed eget dui vel tellus aliquam fermentum.
1503+
Aliquam sed lorem congue, dignissim nulla in, porta diam.
1504+
Aliquam erat volutpat.
1505+
'''.strip())
1506+
assert isinstance(lines, int)
1507+
assert lines > 0
1508+
1509+
assert await api.textutils.pagedTabulate(*table[:-1], *table[2:-1], *table[2:]) is None
1510+
1511+
assert api.textutils.complete('co', ['command', 'row', 'column']) == [
1512+
'mmand', 'lumn']
1513+
1514+
await api.print('Test finished successfully')
1515+
1516+
1517+
async def test_pocket(api):
1518+
assert await api.peripheral.isPresent('back') is False
1519+
1520+
from computercraft.subapis.pocket import PocketAPI
1521+
tbl = await get_object_table(api, 'pocket')
1522+
assert get_class_table(PocketAPI) == tbl
1523+
1524+
await step(api, 'Clean inventory from any pocket upgrades')
1525+
1526+
with assert_raises(LuaException):
1527+
await api.pocket.equipBack()
1528+
with assert_raises(LuaException):
1529+
await api.pocket.unequipBack()
1530+
assert await api.peripheral.isPresent('back') is False
1531+
1532+
await step(api, 'Put any pocket upgrade to inventory')
1533+
1534+
assert await api.pocket.equipBack() is None
1535+
assert await api.peripheral.isPresent('back') is True
1536+
1537+
assert await api.pocket.unequipBack() is None
1538+
assert await api.peripheral.isPresent('back') is False
1539+
1540+
await api.print('Test finished successfully')
1541+
1542+
1543+
# vector won't be implemented, use python equivalent

0 commit comments

Comments
 (0)
0