From: W. Trevor King Date: Wed, 12 May 2010 17:36:27 +0000 (-0400) Subject: Moved Interaction related classes from hooke.command -> hooke.interaction. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=897394fc3e0df066b0ef6d1e934ae2a851296927;p=hooke.git Moved Interaction related classes from hooke.command -> hooke.interaction. Breaking them out makes hooke.command clearer for new readers. Also moved remaining libhooke functionality -> hooke.ui.gui.point_request, although it will need reworking (like the rest of the stuff in hooke.ui.gui) once I finish the architecture transition. --- diff --git a/hooke/command.py b/hooke/command.py index 0a6abc3..82ab53f 100644 --- a/hooke/command.py +++ b/hooke/command.py @@ -1,5 +1,10 @@ """The `command` module provides :class:`Command`\s and :class:`Argument`\s for defining commands. + +It also provides :class:`CommandExit` and subclasses for communicating +command completion information between +:class:`hooke.engine.CommandEngine`\s and +:class:`hooke.ui.UserInterface`\s. """ import Queue as queue @@ -57,100 +62,6 @@ class UncaughtException (Failure): setattr(self, key, value) self.args = (self.traceback + str(self.exception),) -class InList (list): - """:class:`Request` validator class. - - Examples - -------- - - >>> i = InList(['abc', 'def', 5, True]) - >>> i('abc') - >>> i(5) - >>> i(False) - Traceback (most recent call last): - ... - ValueError: False - """ - def __init__(self, *args, **kwargs): - list.__init__(self, *args, **kwargs) - - def __call__(self, value): - """Raises ValueError if a given `value` is not in our internal - list. - """ - if value not in self: - raise ValueError(value) - -class Interaction (object): - """Mid-command inter-process interaction. - - Stores :attr:`type`, a string representing the interaction type - ('boolean', 'string', ...). - """ - def __init__(self, type): - self.type = type - -class Request (Interaction): - """Command engine requests for information from the UI. - """ - def __init__(self, type, response_class, - msg, default=None, validator=None): - super(Request, self).__init__(type) - self.response_class = response_class - self.msg = msg - self.default = default - self.validator = validator - - def response(self, value): - if self.validator != None: - self.validator(value) - return self.response_class(value) - -class Response (Interaction): - """UI response to a :class:`Request`. - """ - def __init__(self, type, value): - super(Response, self).__init__(type) - self.value = value - -class BooleanRequest (Request): - def __init__(self, msg, default=None): - super(BooleanRequest, self).__init__( - 'boolean', BooleanResponse, msg, default, - validator = InList([True, False, default])) - -class BooleanResponse (Response): - def __init__(self, value): - super(BooleanResponse, self).__init__('boolean', value) - -class StringRequest (Request): - def __init__(self, msg, default=None): - super(StringRequest, self).__init__( - 'string', StringResponse, msg, default) - -class StringResponse (Response): - def __init__(self, value): - super(StringResponse, self).__init__('string', value) - -class FloatRequest (Request): - def __init__(self, msg, default=None): - super(FloatRequest, self).__init__( - 'float', FloatResponse, msg, default) - -class FloatResponse (Response): - def __init__(self, value): - super(FloatResponse, self).__init__('float', value) - -class SelectionRequest (Request): - def __init__(self, msg, default=None, options=[]): - super(SelectionRequest, self).__init__( - 'selection', SelectionResponse, msg, default) - self.options = options - -class SelectionResponse (Response): - def __init__(self, value): - super(SelectionResponse, self).__init__('selection', value) - class Command (object): """One-line command description here. diff --git a/hooke/interaction.py b/hooke/interaction.py new file mode 100644 index 0000000..6904f7d --- /dev/null +++ b/hooke/interaction.py @@ -0,0 +1,111 @@ +"""The `interaction` module provides :class:`Request`, +:class:`Response`, an related classes for handling user interaction +through the +:class:`hooke.engine.CommandEngine`/:class:`hooke.ui.UserInterface` +connection. +""" + + +class InList (list): + """:class:`Request` validator class. + + Examples + -------- + + >>> i = InList(['abc', 'def', 5, True]) + >>> i('abc') + >>> i(5) + >>> i(False) + Traceback (most recent call last): + ... + ValueError: False + """ + def __init__(self, *args, **kwargs): + list.__init__(self, *args, **kwargs) + + def __call__(self, value): + """Raises ValueError if a given `value` is not in our internal + list. + """ + if value not in self: + raise ValueError(value) + +class Interaction (object): + """Mid-command inter-process interaction. + + Stores :attr:`type`, a string representing the interaction type + ('boolean', 'string', ...). + """ + def __init__(self, type): + self.type = type + +class Request (Interaction): + """Command engine requests for information from the UI. + """ + def __init__(self, type, response_class, + msg, default=None, validator=None): + super(Request, self).__init__(type) + self.response_class = response_class + self.msg = msg + self.default = default + self.validator = validator + + def response(self, value): + if self.validator != None: + self.validator(value) + return self.response_class(value) + +class Response (Interaction): + """UI response to a :class:`Request`. + """ + def __init__(self, type, value): + super(Response, self).__init__(type) + self.value = value + +class BooleanRequest (Request): + def __init__(self, msg, default=None): + super(BooleanRequest, self).__init__( + 'boolean', BooleanResponse, msg, default, + validator = InList([True, False, default])) + +class BooleanResponse (Response): + def __init__(self, value): + super(BooleanResponse, self).__init__('boolean', value) + +class StringRequest (Request): + def __init__(self, msg, default=None): + super(StringRequest, self).__init__( + 'string', StringResponse, msg, default) + +class StringResponse (Response): + def __init__(self, value): + super(StringResponse, self).__init__('string', value) + +class FloatRequest (Request): + def __init__(self, msg, default=None): + super(FloatRequest, self).__init__( + 'float', FloatResponse, msg, default) + +class FloatResponse (Response): + def __init__(self, value): + super(FloatResponse, self).__init__('float', value) + +class SelectionRequest (Request): + def __init__(self, msg, default=None, options=[]): + super(SelectionRequest, self).__init__( + 'selection', SelectionResponse, msg, default) + self.options = options + +class SelectionResponse (Response): + def __init__(self, value): + super(SelectionResponse, self).__init__('selection', value) + +class PointRequest (Request): + def __init__(self, msg, curve, block=0, default=None): + super(PointRequest, self).__init__( + 'point', PointResponse, msg, default) + self.options = options + +class PointResponse (Response): + def __init__(self, value): + super(PointResponse, self).__init__('point', value) diff --git a/hooke/libhooke.py b/hooke/ui/gui/point_request.py similarity index 50% rename from hooke/libhooke.py rename to hooke/ui/gui/point_request.py index a1d623f..f0813c7 100644 --- a/hooke/libhooke.py +++ b/hooke/ui/gui/point_request.py @@ -1,40 +1,3 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -''' -libhooke.py - -General library of internal objects and utilities for Hooke. - -Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy). -With algorithms contributed by Francesco Musiani (University of Bologna, Italy) - -This program is released under the GNU General Public License version 2. -''' - -import scipy -import numpy -import xml.dom.minidom -import os -import os.path -import string -import csv - - -HOOKE_VERSION=['0.9.0_devel', 'Kenzo', '2009-09-xx'] -WX_GOOD=['2.6','2.8'] -hookeDir='' - - -def get_file_path(filename, folders = []): - if os.path.dirname(filename) == '' or os.path.isabs(filename) == False: - path = '' - for folder in folders: - path = os.path.join(path, folder) - filename = os.path.join(hookeDir, path, filename) - - return filename - class ClickedPoint(object): ''' this class defines what a clicked point on the curve plot is @@ -82,49 +45,3 @@ class ClickedPoint(object): self.index=dists.index(min(dists)) self.graph_coords=(xvector[self.index],yvector[self.index]) -#----------------------------------------- -#CSV-HELPING FUNCTIONS - -def transposed2(lists, defval=0): - ''' - transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing - elements - (by Zoran Isailovski on the Python Cookbook online) - ''' - if not lists: return [] - return map(lambda *row: [elem or defval for elem in row], *lists) - -def csv_write_dictionary(f, data, sorting='COLUMNS'): - ''' - Writes a CSV file from a dictionary, with keys as first column or row - Keys are in "random" order. - - Keys should be strings - Values should be lists or other iterables - ''' - keys=data.keys() - values=data.values() - t_values=transposed2(values) - writer=csv.writer(f) - - if sorting=='COLUMNS': - writer.writerow(keys) - for item in t_values: - writer.writerow(item) - - if sorting=='ROWS': - print 'Not implemented!' #FIXME: implement it. - - -#----------------------------------------- - -def debug(): - ''' - debug stuff from latest rewrite of hooke_playlist.py - should be removed sooner or later (or substituted with new debug code!) - ''' - confo=HookeConfig() - print confo.load_config('hooke.conf') - -if __name__ == '__main__': - debug()