Make poezio.core.struct more Cython-friendly.

Status and Command are now slotted classes instead of namedtuples,
which led to a few changes to access them with their named parameters
instead of as a tuple.

“short” being a C type, I renamed Command.short into
Command.short_desc, which is more explicit anyway.

I also renamed possible_show into POSSIBLE_SHOW, as it is a
module-level constant dict.
This commit is contained in:
Emmanuel Gil Peyrot 2015-11-26 01:02:07 +00:00
parent 721756c5c1
commit bfb02d64a8
6 changed files with 46 additions and 32 deletions

View file

@ -1,8 +1,7 @@
"""
Core class, splitted into smaller chunks
Core class, split into smaller chunks
"""
from . core import Core
from . structs import Command, Status, possible_show, DEPRECATED_ERRORS, \
ERROR_AND_STATUS_CODES
from . structs import Command, Status

View file

@ -27,7 +27,7 @@ from roster import roster
from theming import dump_tuple, get_theme
from decorators import command_args_parser
from . structs import Command, possible_show
from . structs import Command, POSSIBLE_SHOW
@command_args_parser.quoted(0, 1)
@ -44,7 +44,7 @@ def command_help(self, args):
acc.append(' \x19%s}%s\x19o - %s' % (
color,
command,
self.commands[command].short))
self.commands[command].short_desc))
else:
acc.append(' \x19%s}%s\x19o' % (color, command))
acc = sorted(acc)
@ -57,7 +57,7 @@ def command_help(self, args):
acc.append(' \x19%s}%s\x19o - %s' % (
color,
command,
commands[command].short))
commands[command].short_desc))
else:
acc.append(' \x19%s}%s\x19o' % (color, command))
acc = sorted(acc)
@ -111,10 +111,10 @@ def command_status(self, args):
if args is None:
return self.command_help('status')
if not args[0] in possible_show.keys():
if not args[0] in POSSIBLE_SHOW.keys():
return self.command_help('status')
show = possible_show[args[0]]
show = POSSIBLE_SHOW[args[0]]
msg = args[1]
pres = self.xmpp.make_presence()

View file

@ -15,7 +15,7 @@ from common import safeJID
from config import config
from roster import roster
from . structs import possible_show
from . structs import POSSIBLE_SHOW
def completion_help(self, the_input):
@ -29,7 +29,7 @@ def completion_status(self, the_input):
Completion of /status
"""
if the_input.get_argument_position() == 1:
return the_input.new_completion([status for status in possible_show], 1, ' ', quotify=False)
return the_input.new_completion([status for status in POSSIBLE_SHOW], 1, ' ', quotify=False)
def completion_presence(self, the_input):
@ -40,7 +40,7 @@ def completion_presence(self, the_input):
if arg == 1:
return the_input.auto_completion([jid for jid in roster.jids()], '', quotify=True)
elif arg == 2:
return the_input.auto_completion([status for status in possible_show], '', quotify=True)
return the_input.auto_completion([status for status in POSSIBLE_SHOW], '', quotify=True)
def completion_theme(self, the_input):

View file

@ -45,7 +45,7 @@ import keyboard
from . import completions
from . import commands
from . import handlers
from . structs import possible_show, DEPRECATED_ERRORS, \
from . structs import POSSIBLE_SHOW, DEPRECATED_ERRORS, \
ERROR_AND_STATUS_CODES, Command, Status
@ -61,7 +61,7 @@ class Core(object):
self.connection_time = time.time()
self.stdscr = None
status = config.get('status')
status = possible_show.get(status, None)
status = POSSIBLE_SHOW.get(status, None)
self.status = Status(show=status,
message=config.get('status_message'))
self.running = True
@ -716,11 +716,11 @@ class Core(object):
if line == "":
return
if line.startswith('/'):
command = line.strip()[:].split()[0][1:]
command = line.strip().split()[0][1:]
arg = line[2+len(command):] # jump the '/' and the ' '
# example. on "/link 0 open", command = "link" and arg = "0 open"
if command in self.commands:
func = self.commands[command][0]
func = self.commands[command].func
func(arg)
return
else:

View file

@ -1,7 +1,9 @@
"""
Module defining structures useful to the core class and related methods
"""
import collections
__all__ = ['ERROR_AND_STATUS_CODES', 'DEPRECATED_ERRORS', 'POSSIBLE_SHOW',
'Status', 'Command']
# http://xmpp.org/extensions/xep-0045.html#errorstatus
ERROR_AND_STATUS_CODES = {
@ -36,14 +38,27 @@ DEPRECATED_ERRORS = {
'510': 'Disconnected',
}
possible_show = {'available':None,
'chat':'chat',
'away':'away',
'afk':'away',
'dnd':'dnd',
'busy':'dnd',
'xa':'xa'
}
POSSIBLE_SHOW = {
'available': None,
'chat': 'chat',
'away': 'away',
'afk': 'away',
'dnd': 'dnd',
'busy': 'dnd',
'xa': 'xa'
}
Status = collections.namedtuple('Status', 'show message')
Command = collections.namedtuple('Command', 'func desc comp short usage')
class Status:
__slots__ = ('show', 'message')
def __init__(self, show, message):
self.show = show
self.message = message
class Command:
__slots__ = ('func', 'desc', 'comp', 'short_desc', 'usage')
def __init__(self, func, desc, comp, short_desc, usage):
self.func = func
self.desc = desc
self.comp = comp
self.short_desc = short_desc
self.usage = usage

View file

@ -252,10 +252,10 @@ class Tab(object):
command = self.core.commands[command_name]
else: # Unknown command, cannot complete
return False
if command[2] is None:
if command.comp is None:
return False # There's no completion function
else:
return command[2](the_input)
return command.comp(the_input)
return False
def execute_command(self, provided_text):
@ -270,15 +270,15 @@ class Tab(object):
arg = txt[2+len(command):] # jump the '/' and the ' '
func = None
if command in self.commands: # check tab-specific commands
func = self.commands[command][0]
func = self.commands[command].func
elif command in self.core.commands: # check global commands
func = self.core.commands[command][0]
func = self.core.commands[command].func
else:
low = command.lower()
if low in self.commands:
func = self.commands[low][0]
func = self.commands[low].func
elif low in self.core.commands:
func = self.core.commands[low][0]
func = self.core.commands[low].func
else:
if self.missing_command_callback is not None:
error_handled = self.missing_command_callback(low)