[73] | 1 | import json
|
---|
| 2 |
|
---|
| 3 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 4 |
|
---|
| 5 | from geniusweb.party.Party import Party
|
---|
| 6 | from geniusweb.partystdio.StdInOutConnectionEnd import StdInOutConnectionEnd
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | class PartyStdIo:
|
---|
| 10 | '''
|
---|
| 11 | A class that makes a connection between a Party and the system stdio and stderr channels.
|
---|
| 12 | This allows communication with a party through the stdio channels,
|
---|
| 13 | which in fact is a system pipe.
|
---|
| 14 | '''
|
---|
| 15 | def __init__(self, partyclas):
|
---|
| 16 | '''
|
---|
| 17 | partyclas the class of the party.
|
---|
| 18 | Parties should implement a getter for this in the function
|
---|
| 19 | party.party()
|
---|
| 20 | '''
|
---|
| 21 | if not issubclass(partyclas, Party):
|
---|
| 22 | raise ValueError("party must be subclass of Party but got "+str(partyclas))
|
---|
| 23 | self._partyclas= partyclas
|
---|
| 24 | self._jackson = ObjectMapper()
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | def capabilities(self)->None:
|
---|
| 28 | '''
|
---|
| 29 | This assumes a party object
|
---|
| 30 | at the global scope, containing the party() call that returns
|
---|
| 31 | a class extending Party
|
---|
| 32 | '''
|
---|
| 33 | self.out(self._partyclas.getCapabilities(None))
|
---|
| 34 |
|
---|
| 35 | def description(self)->None:
|
---|
| 36 | '''
|
---|
| 37 | This assumes a party object
|
---|
| 38 | at the global scope, containing the party() call that returns
|
---|
| 39 | a class extending Party
|
---|
| 40 | '''
|
---|
| 41 | self.out(self._partyclas.getDescription(None))
|
---|
| 42 |
|
---|
| 43 | def out(self, obj)->None:
|
---|
| 44 | '''
|
---|
| 45 | prints object serialized with ObjectMapper to stdout, without newline
|
---|
| 46 | '''
|
---|
| 47 | print(json.dumps(self._jackson.toJson(obj)),end="")
|
---|
| 48 |
|
---|
| 49 | def run(self, connend=StdInOutConnectionEnd()):
|
---|
| 50 | '''
|
---|
| 51 | "runs" the party. Create instance, connect,
|
---|
| 52 | pipe actions and inform through stdin/stdout, terminate.
|
---|
| 53 | '''
|
---|
| 54 | party=self._partyclas()
|
---|
| 55 |
|
---|
| 56 | party.connect(connend)
|
---|
| 57 | connend.run(party)
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | |
---|