houdini/Houdini/Converters.py

222 lines
5.8 KiB
Python
Raw Normal View History

from abc import ABC
from abc import abstractmethod
2019-03-01 19:41:40 +00:00
import asyncio
2019-03-01 19:41:40 +00:00
class IConverter(ABC):
2019-03-01 19:41:40 +00:00
@property
@abstractmethod
def description(self):
"""A short description of the purpose of the converter"""
2019-03-01 19:41:40 +00:00
@abstractmethod
async def convert(self, ctx):
"""The actual converter implementation"""
2019-03-01 19:41:40 +00:00
class CredentialsConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Used for obtaining login credentials from XML login data"""
async def convert(self, ctx):
username = ctx.argument[0][0].text
password = ctx.argument[0][1].text
2019-03-01 19:41:40 +00:00
return username, password
class VersionChkConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Used for checking the verChk version number"""
async def convert(self, ctx):
return ctx.argument[0].get('v')
2019-03-01 19:41:40 +00:00
class ConnectedPenguinConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts a penguin ID into a live penguin instance
or none if the player is offline"""
async def convert(self, ctx):
penguin_id = int(ctx.argument)
if penguin_id in ctx.p.server.penguins_by_id:
return ctx.p.server.penguins_by_id[penguin_id]
2019-03-01 19:41:40 +00:00
return None
class ConnectedIglooConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts a penguin ID into a live igloo instance or
none if it's not available"""
async def convert(self, ctx):
igloo_id = int(ctx.argument)
if igloo_id in ctx.p.server.igloo_map:
return ctx.p.server.igloo_map[igloo_id]
2019-03-01 19:41:40 +00:00
return None
class RoomConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts a room ID into a Houdini.Data.Room instance"""
async def convert(self, ctx):
room_id = int(ctx.argument)
if room_id in ctx.p.server.rooms:
return ctx.p.server.rooms[room_id]
2019-03-01 19:41:40 +00:00
return None
class ItemConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts an item ID into a Houdini.Data.Item instance"""
async def convert(self, ctx):
item_id = int(ctx.argument)
if item_id in ctx.p.server.items:
return ctx.p.server.items[item_id]
2019-03-01 19:41:40 +00:00
return None
class IglooConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts an igloo ID into a Houdini.Data.Igloo instance"""
async def convert(self, ctx):
igloo_id = int(ctx.argument)
if igloo_id in ctx.p.server.igloos:
return ctx.p.server.igloos[igloo_id]
2019-03-01 19:41:40 +00:00
return None
class FurnitureConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts a furniture ID into a Houdini.Data.Furniture instance"""
async def convert(self, ctx):
furniture_id = int(ctx.argument)
if furniture_id in ctx.p.server.furniture:
return ctx.p.server.furniture[furniture_id]
2019-03-01 19:41:40 +00:00
return None
class FlooringConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts a flooring ID into a Houdini.Data.Flooring instance"""
async def convert(self, ctx):
flooring_id = int(ctx.argument)
if flooring_id in ctx.p.server.flooring:
return ctx.p.server.flooring[flooring_id]
2019-03-01 19:41:40 +00:00
return None
class StampConverter(IConverter):
2019-03-01 19:41:40 +00:00
description = """Converts a stamp ID into a Houdini.Data.Stamp instance"""
async def convert(self, ctx):
stamp_id = int(ctx.argument)
if stamp_id in ctx.p.server.stamps:
return ctx.p.server.stamps[stamp_id]
2019-03-01 19:41:40 +00:00
return None
class SeparatorConverter(IConverter):
__slots__ = ['separator', 'mapper']
description = """Converts strings separated by char into a list of type"""
def __init__(self, separator='|', mapper=int):
self.separator = separator
self.mapper = mapper
async def convert(self, ctx):
return map(self.mapper, ctx.argument.split(self.separator))
class UnionConverter(IConverter):
__slots__ = ['types']
description = """Converts union type into argument"""
def __init__(self, *types, skip_none=False):
self.types = types
self.skip_none = skip_none
async def convert(self, ctx):
for converter in self.types:
try:
result = await do_conversion(converter, ctx)
if not self.skip_none or result is not None:
return result
except ValueError:
continue
2019-03-01 19:41:40 +00:00
class GreedyConverter(IConverter):
2019-03-01 19:41:40 +00:00
__slots__ = ['target']
description = """Converts until it can't any longer"""
def __init__(self, target=int):
self.target = target
async def convert(self, ctx):
converted = []
try:
converted.append(await do_conversion(self.target, ctx))
for ctx.argument in ctx.arguments:
converted.append(await do_conversion(self.target, ctx))
except ValueError:
return converted
return converted
class OptionalConverter(IConverter):
__slots__ = ['target']
description = """Tries to convert but ignores if it can't"""
def __init__(self, target=int):
self.target = target
async def convert(self, ctx):
try:
return await do_conversion(self.target, ctx)
except ValueError:
return ctx.component.default
class _ConverterContext:
__slots__ = ['component', 'arguments', 'argument', 'p']
def __init__(self, component, arguments, argument, p):
self.component = component
self.arguments = arguments
self.argument = argument
self.p = p
2019-03-01 19:41:40 +00:00
def get_converter(component):
if component.annotation is component.empty:
return str
return component.annotation
2019-03-01 19:41:40 +00:00
async def do_conversion(converter, ctx):
if issubclass(type(converter), IConverter) and not isinstance(converter, IConverter):
converter = converter()
if isinstance(converter, IConverter):
if asyncio.iscoroutinefunction(converter.convert):
return await converter.convert(ctx)
return converter.convert(ctx)
return converter(ctx.argument)