File: args.py

package info (click to toggle)
gamera 1:3.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,912 kB
  • sloc: xml: 122,324; cpp: 50,730; python: 35,044; ansic: 258; makefile: 114; sh: 101
file content (500 lines) | stat: -rw-r--r-- 15,818 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# -*- mode: python; indent-tabs-mode: nil; tab-width: 3 -*-
# vim: set tabstop=3 shiftwidth=3 expandtab:
#
# Copyright (C) 2001-2010 Ichiro Fujinaga, Michael Droettboom,
#                         Karl MacMillan, Christoph Dalitz
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from gamera.gui import has_gui
from new import instancemethod
import sys, os.path   # Python standard library
from types import *
import util, paths            # Gamera specific

# the maximum default number (int, float) in argument boxes
# necessary because sys.maxint is not accepted by some wxPython widgets
DEFAULT_MAX_ARG_NUMBER = 1048576    # 2^20

######################################################################

# This is a "self-generating" dialog box

class NoGUIArgs:
   def setup(self, *args, **kwargs):
      raise Exception("No GUI environment available.  Cannot display dialog.")
   
   def show(self, *args, **kwargs):
      raise Exception("No GUI environment available.  Cannot display dialog.")

class Args(NoGUIArgs):
   # list is a list of "Arg s"
   def __init__(self, list=[], name="Arguments", function=None, title=None):
      self.list = util.make_sequence(list)
      self.valid = 1
      self.name = name
      self.function = function
      self.title = title

   def __repr__(self):
      return "<" + self.__class__.__name__ + ">"

   def __getitem__(self, i):
      return self.list[i]
   index = __getitem__

   def __len__(self, i):
      return len(self.list)

######################################################################

# ARGUMENT TYPES

# for allowing None as default argument
class CNoneDefault:
   def __str__(self):
      return "None"
NoneDefault = CNoneDefault()

class Arg:
   default = 0
   length = 1

   def __init__(self, name):
      self.name = name
      if name is not None and not util.is_string_or_unicode(name):
         raise TypeError("'name' must be a string")
      self.has_default = False

   def __repr__(self):
      return "<" + self.__class__.__name__ + ">"

   def rest_repr(self, name=False):
      result = "``%s``" % self.__class__.__name__
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %s" % str(self.default)
      return result

   def register(self, plug, func):
      pass
   
class Int(Arg):
   def __init__(self, name=None, range=(-DEFAULT_MAX_ARG_NUMBER, DEFAULT_MAX_ARG_NUMBER), default=None):
      Arg.__init__(self, name)
      if not (util.is_sequence(range) and len(range) == 2 and
              type(range[0]) in (int, float) and type(range[1]) in (int, float)):
         raise TypeError("'range' must be a 2-tuple of numbers")
      self.rng = range
      if default is None:
         self.has_default = False
         self.default = 0
      else:
         self.has_default = True
         self.default = default
      if not isinstance(self.default,CNoneDefault) and type(self.default) != int:
         raise TypeError("'default' must be an int")

   def rest_repr(self, name=False):
      result = "int"
      if self.rng != (-DEFAULT_MAX_ARG_NUMBER, DEFAULT_MAX_ARG_NUMBER):
         result += "(%d, %d)" % tuple([int(x) for x in self.rng])
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            if isinstance(self.default,CNoneDefault) or isinstance(self.default, NoneType):
                result += " = %s" % str(self.default)
            else:
                result += " = %d" % self.default
      return result

class Real(Arg):
   def __init__(self, name=None,
                range=None,
                default=None):
      Arg.__init__(self, name)
      if range is None:
         range = (-DEFAULT_MAX_ARG_NUMBER, DEFAULT_MAX_ARG_NUMBER)
      if not (util.is_sequence(range) and len(range) == 2 and
              type(range[0]) in (int, float) and type(range[1]) in (int, float)):
         raise TypeError("'range' must be a 2-tuple of numbers")
      self.rng = range
      if default is None:
         self.has_default = False
         self.default = 0.0
      else:
         self.has_default = True
         self.default = default
      if type(self.default) != float:
         raise TypeError("'default' must be a float")

   def rest_repr(self, name=False):
      result = "float"
      if self.rng != (-DEFAULT_MAX_ARG_NUMBER, DEFAULT_MAX_ARG_NUMBER):
         result += "(%.02f, %.02f)" % tuple([float(x) for x in self.rng])
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %.02f" % self.default
      return result

Float = Real

class Complex(Arg):
   def __init__(self, name=None, default=None):
      Arg.__init__(self, name)
      if default is None:
         self.has_default = False
         self.default = 0j
      else:
         self.has_default = True
         self.default = default

   def rest_repr(self, name=False):
      result = "complex"
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %s" % self.default
      return result

class String(Arg):
   def __init__(self, name=None, default=None):
      Arg.__init__(self, name)
      if default is None:
         self.has_default = False
         self.default = ''
      else:
         self.has_default = True
         self.default = default
      if not util.is_string_or_unicode(self.default):
         raise TypeError("'default' must be an int")

   def rest_repr(self, name=False):
      result = "str"
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %s" % repr(self.default)
      return result

class Class(Arg):
   def __init__(self, name=None, klass=None, list_of=False, default=None):
      Arg.__init__(self, name)
      self.klass = klass
      self.list_of = bool(list_of)
      if default is None:
         self.has_default = False
         self.default = ''
      else:
         self.has_default = True
         self.default = default

   def rest_repr(self, name=False):
      if self.klass is None:
         result = "object"
      else:
         result = "%s" % self.klass.__name__
      if name:
         result += " *%s*" % self.name
      if self.list_of:
         result = "[%s]" % result
      if self.has_default:
         result += " = %s" % str(self.default)
      return result

class ImageType(Arg):
   def __init__(self, pixel_types, name=None, list_of=False, default=None):
      import core
      Arg.__init__(self, name)
      if not util.is_sequence(pixel_types):
         pixel_types = (pixel_types,)
      if not util.is_homogenous_list(pixel_types, (int,)):
         raise TypeError("'pixel_types' must be a list of integers.")
      if not core is None:
         self.klass = core.ImageBase
      else:
         self.klass = None
      self.pixel_types = pixel_types
      self.list_of = bool(list_of)
      if default is None:
         self.has_default = False
         self.default = None
      else:
         self.has_default = True
         self.default = default
      if self.has_default and not isinstance(self.default,CNoneDefault):
         raise TypeError("'default' can only be NoneDefault")

   def rest_repr(self, name=False):
      result = '``Image`` [%s]' % '|'.join([util.get_pixel_type_name(x) for x in self.pixel_types])
      if name:
         result += " *%s*" % self.name
      if self.list_of:
         result = "[%s]" % result
      if self.has_default:
          result += " = %s" % str(self.default)
      return result

   def register(self, plug, func):
      from gamera import core
      func = instancemethod(func, None, core.gameracore.Image)
      setattr(core.ImageBase, plug.__name__, func)

class Rect(Arg):
   def __init__(self, name=None, list_of=False):
      import core
      Arg.__init__(self, name)
      if not core is None:
         self.klass = core.Rect
      self.list_of = bool(list_of)
   
   def rest_repr(self, name=False):
      result = Arg.rest_repr(self, name)
      if self.list_of:
         return "[%s]" % result
      return result
      
class Choice(Arg):
   def __init__(self, name=None, choices=[], default=None):
      Arg.__init__(self, name)
      if not util.is_string_or_unicode_list(choices):
         raise TypeError("'choices' must be a list of strings.")
      self.choices = choices
      if default is None:
         self.has_default = False
         self.default = 0
      else:
         self.has_default = True
         self.default = default
      if not isinstance(self.default,CNoneDefault) and type(self.default) != int:
         raise TypeError("'default' must be an int")

   def rest_repr(self, name = False):
      result = '``Choice`` [%s]' % ('|'.join(self.choices))
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            if isinstance(self.default,CNoneDefault):
               result += " = %s" % str(self.default)
            else:
               result += " = %s" % self.choices[self.default]
      return result

class ChoiceString(Arg):
   def __init__(self, name=None, choices=[], default=None, strict=True):
      Arg.__init__(self, name)
      if not util.is_string_or_unicode_list(choices):
         raise TypeError("'choices' must be a list of strings.")
      self.choices = choices
      if default is None:
         self.has_default = False
         self.default = choices[0]
      else:
         if type(default) != str:
            raise TypeError("'default' must be a string")
         if default not in choices:
            raise ValueError("Given 'default' must be in available 'choices'")
         self.has_default = True
         self.default = default
      self.strict = strict

   def rest_repr(self, name = False):
      if self.strict:
         strict = "(strict)"
      else:
         strict = ""
      result = '``ChoiceString%s`` [%s]' % (strict, '|'.join(self.choices))
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %s" % self.default
      return result

class _Filename(Arg):
   def __init__(self, name=None, default="", extension="*.*"):
      Arg.__init__(self, name)
      if not util.is_string_or_unicode(default):
         raise TypeError("'default' must be a string")
      self.default = default
##       if not util.is_string_or_unicode(extension):
##          raise TypeError("'extension' must be a string")
      self.extension = extension

class FileOpen(_Filename):
   pass

class FileSave(_Filename):
   pass

class Directory(_Filename):
   pass

class Radio(Arg):
   def __init__(self, name=None, radio_button=''):
      Arg.__init__(self, name)
      self.radio_button = radio_button

class Check(Arg):
   def __init__(self, name=None, check_box='', default=None, enabled=True):
      Arg.__init__(self, name)
      if not util.is_string_or_unicode(check_box):
         raise TypeError("'check_box' must be a string")
      self.check_box = check_box
      if default is None:
         self.has_default = False
         self.default = False
      else:
         self.has_default = True
         self.default = default
      self.default = bool(self.default)
      self.enabled = bool(enabled)

   def rest_repr(self, name=False):
      result = '``bool``'
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %s" % str(self.default)
      return result

Bool = Check

class Region(Class):
   def __init__(self, name=None):
      Class.__init__(self, name, None)

class RegionMap(Class):
   def __init__(self, name=None):
      Class.__init__(self, name, None)

class ImageInfo(Class):
   def __init__(self, name=None):
      Class.__init__(self, name, None)

class Point(Arg):
   def __init__(self, name=None, default=None):
      Arg.__init__(self, name)
      if default is None:
         self.has_default = False
         self.default = (0, 0)
      else:
         self.has_default = True
         self.default = default

class FloatPoint(Arg):
   def __init__(self, name=None, default=None):
      Arg.__init__(self, name)
      if default is None:
         self.has_default = False
         self.default = (0.0, 0.0)
      else:
         self.has_default = True
         self.default = default

class Dim(Point):
   pass

class _Vector(Class):
   def __init__(self, name=None, default=None, length=-1):
      Class.__init__(self, name, self.klass)
      if type(length) != int:
         raise TypeError("'length' must be an int")
      self.length = length
      if default is None:
         self.default = []
         self.has_default = False
      else:
         self.default = default
         self.has_default = True

   def rest_repr(self, name=False):
      result = "``%s``" % self.__class__.__name__
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %s" % str(self.default)
      return result

class FloatVector(_Vector):
   klass = float
   typecode = "d"

class IntVector(_Vector):
   klass = int
   typecode = "i"

class ComplexVector(_Vector):
   klass = complex
   typecode = None

class ImageList(Class):
   def __init__(self, name=None, default=None):
      Class.__init__(self, name=name, klass=None, list_of=True, default=default)

class Pixel(Class):
   """Uses these to pass pixel values into a plugin.  The
PixelType must always match the type of the "self" image."""
   def __init__(self, name=None, default=None):
      Class.__init__(self, name)
      if default is None:
         self.has_default = False
      else:
         self.has_default = True
      self.default = default

   def rest_repr(self, name=False):
      result = "Pixel"
      if name:
         result += " *%s*" % self.name
         if self.has_default:
            result += " = %s" % str(self.default)
      return result

class Info(Arg):
   pass

class PointVector(Class):
   def __init__(self, name=None):
      Class.__init__(self, name, None, True)

class Wizard:
   def show(self, dialog):
      dialog_history = ['start', dialog]
      next_dialog = dialog
      while next_dialog != None:
         if next_dialog == 'start':
            return
         result = next_dialog.show(self.parent, self.locals, wizard=1)
         if not result is None:
            next_dialog = getattr(self, next_dialog.function)(*next_dialog.get_args())
            if next_dialog != dialog_history[-1]:
               dialog_history.append(next_dialog)
         else:
            next_dialog = dialog_history[-2]
            dialog_history = dialog_history[0:-1]
      self.done()

__all__ = 'Args Int Real Float Complex String Class ImageType Rect Choice FileOpen FileSave Directory Radio Check Region RegionMap ImageInfo FloatVector IntVector ComplexVector ImageList Info Wizard Pixel PointVector _Vector ChoiceString Point FloatPoint Dim Bool'.split()

___mixin_locals = locals()
def mixin(module, name):
   for cls_name in __all__ + ["Arg"]:
      cls = ___mixin_locals[cls_name]
      if module.has_key(cls_name):
         cls.__bases__ = tuple([module[cls_name]] + list(cls.__bases__))
   sys.stdout.write("\n")