ctioga 1.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/COPYING +340 -0
- data/ctioga/bin/ctable +28 -0
- data/ctioga/bin/ctioga +37 -0
- data/ctioga/doc/ctable.1 +156 -0
- data/ctioga/doc/ctioga.1 +2363 -0
- data/ctioga/examples/README +46 -0
- data/ctioga/examples/ctioga.gnuplot +4 -0
- data/ctioga/examples/ctioga_within_tioga.rb +53 -0
- data/ctioga/examples/ctiogarc.rb +24 -0
- data/ctioga/examples/include_1.rb +15 -0
- data/ctioga/examples/noise.dat +100 -0
- data/ctioga/examples/noise.rb +13 -0
- data/ctioga/examples/trig.csv +100 -0
- data/ctioga/examples/trig.dat +100 -0
- data/ctioga/examples/trig.rb +14 -0
- data/ctioga/examples/trigh.dat +100 -0
- data/ctioga/examples/trigh.rb +10 -0
- data/ctioga/examples/tutorial +763 -0
- data/ctioga/examples/tutorial.sh +269 -0
- data/ctioga/tests/README +14 -0
- data/ctioga/tests/axes.sh +40 -0
- data/ctioga/tests/basic.sh +11 -0
- data/ctioga/tests/draw.sh +24 -0
- data/ctioga/tests/histograms.sh +14 -0
- data/ctioga/tests/insets.sh +41 -0
- data/ctioga/tests/layouts.sh +29 -0
- data/ctioga/tests/legends.sh +113 -0
- data/ctioga/tests/styles.sh +43 -0
- data/ctioga/tests/test_style.sh +8 -0
- data/ctioga/tests/tests.sh +24 -0
- data/ctioga/tests/text_backend.sh +83 -0
- data/ctioga/tests/tioga_defaults.rb +18 -0
- data/lib/CTioga/axes.rb +904 -0
- data/lib/CTioga/backends.rb +88 -0
- data/lib/CTioga/boundaries.rb +224 -0
- data/lib/CTioga/ctable.rb +134 -0
- data/lib/CTioga/curve_style.rb +246 -0
- data/lib/CTioga/debug.rb +199 -0
- data/lib/CTioga/dimension.rb +133 -0
- data/lib/CTioga/elements.rb +17 -0
- data/lib/CTioga/elements/base.rb +84 -0
- data/lib/CTioga/elements/containers.rb +578 -0
- data/lib/CTioga/elements/curves.rb +368 -0
- data/lib/CTioga/elements/tioga_primitives.rb +440 -0
- data/lib/CTioga/layout.rb +595 -0
- data/lib/CTioga/legends.rb +29 -0
- data/lib/CTioga/legends/cmdline.rb +187 -0
- data/lib/CTioga/legends/item.rb +164 -0
- data/lib/CTioga/legends/style.rb +257 -0
- data/lib/CTioga/log.rb +73 -0
- data/lib/CTioga/movingarrays.rb +131 -0
- data/lib/CTioga/partition.rb +271 -0
- data/lib/CTioga/plot_style.rb +230 -0
- data/lib/CTioga/plotmaker.rb +1677 -0
- data/lib/CTioga/shortcuts.rb +69 -0
- data/lib/CTioga/structures.rb +82 -0
- data/lib/CTioga/styles.rb +140 -0
- data/lib/CTioga/themes.rb +581 -0
- data/lib/CTioga/themes/classical.rb +82 -0
- data/lib/CTioga/themes/demo.rb +63 -0
- data/lib/CTioga/themes/fits.rb +91 -0
- data/lib/CTioga/themes/mono.rb +33 -0
- data/lib/CTioga/tioga.rb +32 -0
- data/lib/CTioga/utils.rb +173 -0
- data/lib/MetaBuilder/Parameters/dates.rb +38 -0
- data/lib/MetaBuilder/Parameters/lists.rb +132 -0
- data/lib/MetaBuilder/Parameters/numbers.rb +69 -0
- data/lib/MetaBuilder/Parameters/strings.rb +86 -0
- data/lib/MetaBuilder/Parameters/styles.rb +75 -0
- data/lib/MetaBuilder/Qt4/Parameters/dates.rb +51 -0
- data/lib/MetaBuilder/Qt4/Parameters/numbers.rb +65 -0
- data/lib/MetaBuilder/Qt4/Parameters/strings.rb +106 -0
- data/lib/MetaBuilder/Qt4/parameter.rb +172 -0
- data/lib/MetaBuilder/Qt4/parameters.rb +9 -0
- data/lib/MetaBuilder/descriptions.rb +603 -0
- data/lib/MetaBuilder/factory.rb +101 -0
- data/lib/MetaBuilder/group.rb +57 -0
- data/lib/MetaBuilder/metabuilder.rb +10 -0
- data/lib/MetaBuilder/parameter.rb +374 -0
- data/lib/MetaBuilder/parameters.rb +11 -0
- data/lib/MetaBuilder/qt4.rb +8 -0
- data/lib/SciYAG/Backends/backend.rb +379 -0
- data/lib/SciYAG/Backends/binner.rb +168 -0
- data/lib/SciYAG/Backends/cache.rb +102 -0
- data/lib/SciYAG/Backends/dataset.rb +158 -0
- data/lib/SciYAG/Backends/descriptions.rb +469 -0
- data/lib/SciYAG/Backends/filters.rb +25 -0
- data/lib/SciYAG/Backends/filters/average.rb +134 -0
- data/lib/SciYAG/Backends/filters/cumulate.rb +37 -0
- data/lib/SciYAG/Backends/filters/filter.rb +70 -0
- data/lib/SciYAG/Backends/filters/norm.rb +39 -0
- data/lib/SciYAG/Backends/filters/smooth.rb +63 -0
- data/lib/SciYAG/Backends/filters/sort.rb +43 -0
- data/lib/SciYAG/Backends/filters/strip.rb +34 -0
- data/lib/SciYAG/Backends/filters/trim.rb +64 -0
- data/lib/SciYAG/Backends/gnuplot.rb +131 -0
- data/lib/SciYAG/Backends/math.rb +108 -0
- data/lib/SciYAG/Backends/mdb.rb +462 -0
- data/lib/SciYAG/Backends/multitext.rb +96 -0
- data/lib/SciYAG/Backends/source.rb +64 -0
- data/lib/SciYAG/Backends/text.rb +339 -0
- data/lib/SciYAG/backends.rb +16 -0
- metadata +191 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# factory.rb : few classes to handle factories
|
|
2
|
+
# Copyright (C) 2006 Vincent Fourmond
|
|
3
|
+
|
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program; if not, write to the Free Software
|
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17
|
+
|
|
18
|
+
# The MetaBuilder module contains the classes necessary to create classes
|
|
19
|
+
# with a simple meta-information system that lets you save/export the
|
|
20
|
+
# state of an object (under certain assumptions), query parameters in
|
|
21
|
+
# different ways (command-line, Qt widgets, and others if necessary).
|
|
22
|
+
module MetaBuilder
|
|
23
|
+
|
|
24
|
+
# The Factories module contains several classes useful for dealing
|
|
25
|
+
# with Description factories:
|
|
26
|
+
#
|
|
27
|
+
# * UniqueFactory: only one instance of each class described, and
|
|
28
|
+
# the user can choose which one is the current.
|
|
29
|
+
module Factories
|
|
30
|
+
|
|
31
|
+
# A factory for which there is only one instance of each
|
|
32
|
+
# class described, and only one selected instance at any moment.
|
|
33
|
+
# User can switch freely from one instance to another.
|
|
34
|
+
class UniqueFactory
|
|
35
|
+
|
|
36
|
+
attr_reader :current, :current_name
|
|
37
|
+
|
|
38
|
+
# Creates a UniqueFactory using the factory in _base_class_.
|
|
39
|
+
# The selected description is named by _default_, or is
|
|
40
|
+
# the first one in the list in case _default_ is missing.
|
|
41
|
+
def initialize(base_class, default = nil)
|
|
42
|
+
@base_class = base_class
|
|
43
|
+
@instances = {}
|
|
44
|
+
for name, desc in @base_class.factory_description_hash
|
|
45
|
+
@instances[name] = desc.instantiate
|
|
46
|
+
end
|
|
47
|
+
if default.nil?
|
|
48
|
+
default = @base_class.factory_description_list.first.name
|
|
49
|
+
end
|
|
50
|
+
self.current=default
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Sets the current class to the named class.
|
|
54
|
+
def current=(str)
|
|
55
|
+
if @instances.has_key?(str)
|
|
56
|
+
@current_name = str
|
|
57
|
+
@current = @instances[str]
|
|
58
|
+
else
|
|
59
|
+
raise "Incorrect name given: #{str}"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def instance_list
|
|
64
|
+
return @instances.values
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def instances
|
|
68
|
+
return @instances
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Returns the instance of the named class _name_, and sets
|
|
72
|
+
# the current class as a side effect.
|
|
73
|
+
def instance(name)
|
|
74
|
+
self.current = name
|
|
75
|
+
return @current
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Fills the Optionparser _parser_ with all the options necessary to
|
|
79
|
+
# select a class among others and set options to it. Note that
|
|
80
|
+
# options don't need to apply to the current class; they are set
|
|
81
|
+
# but the factory doesn't change the current class in this case.
|
|
82
|
+
#
|
|
83
|
+
# The options will be preceded by _banner_. If _uniquify_ is true, the
|
|
84
|
+
# names of the options will be made as unique as possible.
|
|
85
|
+
def option_parser_factory(parser, banner = "", uniquify = true)
|
|
86
|
+
parser.separator banner
|
|
87
|
+
# A for loop won't work here as name would be overwritten
|
|
88
|
+
# for every iteration.
|
|
89
|
+
@instances.each do |name, instance|
|
|
90
|
+
name
|
|
91
|
+
parser.on("--#{name}", instance.description.banner(instance)) do
|
|
92
|
+
self.current = name
|
|
93
|
+
end
|
|
94
|
+
instance.option_parser_options(parser, uniquify)
|
|
95
|
+
parser.separator ""
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# group.rb : A class to describe a group of parameters
|
|
2
|
+
# Copyright (C) 2006 Vincent Fourmond
|
|
3
|
+
|
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program; if not, write to the Free Software
|
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17
|
+
|
|
18
|
+
# The MetaBuilder module contains the classes necessary to create classes
|
|
19
|
+
# with a simple meta-information system that lets you save/export the
|
|
20
|
+
# state of an object (under certain assumptions), query paramters in
|
|
21
|
+
# different ways (command-line, Qt widgets, and others if necessary).
|
|
22
|
+
module MetaBuilder
|
|
23
|
+
|
|
24
|
+
# The ParameterGroup class provides an easy way to group parameters
|
|
25
|
+
# of a described class. The group should have a #name, a #long_name,
|
|
26
|
+
# more descriptive and a description.
|
|
27
|
+
|
|
28
|
+
class ParameterGroup
|
|
29
|
+
# The short name of the group
|
|
30
|
+
attr_accessor :name
|
|
31
|
+
|
|
32
|
+
# The long name of the group
|
|
33
|
+
attr_accessor :long_name
|
|
34
|
+
|
|
35
|
+
# The (text) description of the group
|
|
36
|
+
attr_accessor :description
|
|
37
|
+
|
|
38
|
+
# The list of parameters in that group, in the order they were
|
|
39
|
+
# declared.
|
|
40
|
+
|
|
41
|
+
attr_accessor :parameter_list
|
|
42
|
+
|
|
43
|
+
def initialize(name, long_name = name,
|
|
44
|
+
description = nil)
|
|
45
|
+
@name = name
|
|
46
|
+
@description = description
|
|
47
|
+
@long_name = long_name
|
|
48
|
+
@parameter_list = []
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def add_parameter(param)
|
|
52
|
+
@parameter_list << param
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# metabuilder.rb: The one include file for MetaBuilder. Doesn't include Qt
|
|
2
|
+
# stuff or other specific ones.
|
|
3
|
+
|
|
4
|
+
# This file is copyright 2006 by Vincent Fourmond, but you can do whatever
|
|
5
|
+
# you want with it.
|
|
6
|
+
|
|
7
|
+
require 'MetaBuilder/descriptions'
|
|
8
|
+
require 'MetaBuilder/group'
|
|
9
|
+
require 'MetaBuilder/parameters'
|
|
10
|
+
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
# parameter.rb : A class to describe a parameter
|
|
2
|
+
# Copyright (C) 2006 Vincent Fourmond
|
|
3
|
+
|
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program; if not, write to the Free Software
|
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17
|
+
|
|
18
|
+
require 'forwardable'
|
|
19
|
+
|
|
20
|
+
# The MetaBuilder module contains the classes necessary to create classes
|
|
21
|
+
# with a simple meta-information system that lets you save/export the
|
|
22
|
+
# state of an object (under certain assumptions), query parameters in
|
|
23
|
+
# different ways (command-line, Qt, Tk and Gtk widgets, and so on...).
|
|
24
|
+
module MetaBuilder
|
|
25
|
+
|
|
26
|
+
# A class that handles a parameter type. It has to be subclassed to
|
|
27
|
+
# actually provide a parameter. The subclasses must provide the
|
|
28
|
+
# following:
|
|
29
|
+
#
|
|
30
|
+
# * a #string_to_type function to convert from string to the type;
|
|
31
|
+
# * a #type_to_string to convert back from type to string
|
|
32
|
+
# * an instance #type_name that returns a really small description
|
|
33
|
+
# of the type, to be used for instance to name command-line parameters.
|
|
34
|
+
# * a #type_name statement that registers the current class to the
|
|
35
|
+
# ParameterType system.
|
|
36
|
+
#
|
|
37
|
+
# Moerover, it is a good idea to reimplement the
|
|
38
|
+
# #qt4_create_input_widget method; the default implementation works,
|
|
39
|
+
# but you probably wish it would look better.
|
|
40
|
+
#
|
|
41
|
+
# Types are implemented using hashes: this way, additionnal
|
|
42
|
+
# parameters can easily be added. The hash *must* have a :type key
|
|
43
|
+
# that will be interpreted by the children of ParameterType. Examples:
|
|
44
|
+
#
|
|
45
|
+
# { :type => :integer}
|
|
46
|
+
# { :type => :file, :filter => "Text Files (*.txt)}
|
|
47
|
+
#
|
|
48
|
+
# And so on. You definitely should document your type and it's
|
|
49
|
+
# attributes properly, if you ever want that someone uses it.
|
|
50
|
+
#
|
|
51
|
+
# The list of currently recognised types is here:
|
|
52
|
+
#
|
|
53
|
+
# <tt>:integer</tt> :: ParameterTypes::IntegerParameter
|
|
54
|
+
# <tt>:float</tt> :: ParameterTypes::FloatParameter
|
|
55
|
+
# <tt>:string</tt> :: ParameterTypes::StringParameter
|
|
56
|
+
# <tt>:file</tt> :: ParameterTypes::FileParameter
|
|
57
|
+
# <tt>:boolean</tt> :: ParameterTypes::BooleanParameter
|
|
58
|
+
# <tt>:list</tt> :: ParameterTypes::ListParameter
|
|
59
|
+
#
|
|
60
|
+
# Additionally to the parameters the given type is requiring, you can
|
|
61
|
+
# pass some other kind of information using this hash, such as option
|
|
62
|
+
# parser short argument, aliases, and so on. This has nothing to do
|
|
63
|
+
# with type conversion, but it is the best place where to put this kind
|
|
64
|
+
# of things, in my humble opinion. The currently recognized such additional
|
|
65
|
+
# parameters are:
|
|
66
|
+
# * :option_parser_short: a short option name for option_parser.
|
|
67
|
+
# * :namespace: a ruby module that will be searched by #string_to_type
|
|
68
|
+
# for a constant. If one of the given name is found, its value is
|
|
69
|
+
# returned.
|
|
70
|
+
# * :shortctus: a hash specifiying strings shortcuts for given values.
|
|
71
|
+
class ParameterType
|
|
72
|
+
|
|
73
|
+
# A hash that makes the :type value of the _type_ argument correspond
|
|
74
|
+
# to a ParameterType child
|
|
75
|
+
@@parameter_types = { }
|
|
76
|
+
|
|
77
|
+
# A default constructor. It should be safe to use it directly for
|
|
78
|
+
# children, unless something more specific is needed. Any descendent
|
|
79
|
+
# should *always* register _type_ as @type - or, even better, call
|
|
80
|
+
# super.
|
|
81
|
+
def initialize(type)
|
|
82
|
+
if type.is_a?(Symbol)
|
|
83
|
+
type = {:type => type}
|
|
84
|
+
end
|
|
85
|
+
@type = type
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# This class function actually registers the current type
|
|
89
|
+
# to the ParameterType ancestor. _name_ should be a symbol.
|
|
90
|
+
# Moreover, if the second argument is provided, it automatically
|
|
91
|
+
# creates a #type_name instance method returning this value.
|
|
92
|
+
def self.type_name(name, public_name = nil, default_value = nil)
|
|
93
|
+
if @@parameter_types.has_key?(name)
|
|
94
|
+
warn "Redefining type #{name} " +
|
|
95
|
+
"from #{@@parameter_types[name]} to #{self}"
|
|
96
|
+
end
|
|
97
|
+
@@parameter_types[name] = self
|
|
98
|
+
self.send(:define_method,:type_name) do
|
|
99
|
+
public_name
|
|
100
|
+
end
|
|
101
|
+
self.send(:define_method,:default_value) do
|
|
102
|
+
default_value
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# This function converts a 'description' of the type wanted
|
|
107
|
+
# into a ParameterType child. Used by the Parameter system.
|
|
108
|
+
# As a special treat, a lone symbol is converted into
|
|
109
|
+
# {:type => :symbol}
|
|
110
|
+
def self.get_param_type(type)
|
|
111
|
+
if type.is_a?(Symbol)
|
|
112
|
+
type = {:type => type}
|
|
113
|
+
end
|
|
114
|
+
raise "The type argument must be a Hash" unless type.is_a?(Hash)
|
|
115
|
+
begin
|
|
116
|
+
return @@parameter_types.fetch(type[:type])
|
|
117
|
+
rescue
|
|
118
|
+
raise "Type #{type[:type]} unknown to the parameter system"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Shortcut to convert directly a string to the given type specification.
|
|
123
|
+
# Handy shortcut.
|
|
124
|
+
def self.from_string(type, string)
|
|
125
|
+
return get_type(type).string_to_type(string)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Returns a ParameterType child instance suitable for conversion
|
|
129
|
+
# of the given type specification
|
|
130
|
+
def self.get_type(type)
|
|
131
|
+
return get_param_type(type).new(type)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
# Runs the string_to_type conversion hook
|
|
136
|
+
def stt_run_hook(val)
|
|
137
|
+
if @type.key?(:stt_hook)
|
|
138
|
+
return @type[:stt_hook].call(val)
|
|
139
|
+
else
|
|
140
|
+
val
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# This function converts the given string to the appropriate type. It is a
|
|
145
|
+
# wrapper around the #string_to_type_internal function
|
|
146
|
+
# that can take advantage of a few general features. It is recommanded
|
|
147
|
+
# to define a #string_to_type_internal function rather to redefine
|
|
148
|
+
# #string_to_type
|
|
149
|
+
def string_to_type(string)
|
|
150
|
+
# First, shortcuts:
|
|
151
|
+
if @type.key?(:shortcuts) and @type[:shortcuts].key? string
|
|
152
|
+
return stt_run_hook(@type[:shortcuts][string])
|
|
153
|
+
end
|
|
154
|
+
# Then, constants lookup.
|
|
155
|
+
if @type.key?(:namespace)
|
|
156
|
+
begin
|
|
157
|
+
return stt_run_hook(lookup_const(string))
|
|
158
|
+
rescue Exception
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
return stt_run_hook(string_to_type_internal(string))
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# This function does the exact opposite of the #string_to_type one.
|
|
165
|
+
# It defaults to using the to_s methods of the parameter. Be careful:
|
|
166
|
+
# it is absolutely important that for any valid type,
|
|
167
|
+
#
|
|
168
|
+
# string_to_type(type_to_string(type)) == type
|
|
169
|
+
def type_to_string(type)
|
|
170
|
+
return type_to_string_internal(type)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# The internal function for converting type to a string. Used by
|
|
174
|
+
# #type_to_string, children should only reimplement this function
|
|
175
|
+
# and leave #type_to_string
|
|
176
|
+
def type_to_string_internal(type)
|
|
177
|
+
return type.to_s
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Returns a default value for the given type. This is reimplemented
|
|
181
|
+
# systematically from children, with the ParameterType::type_name
|
|
182
|
+
# statement.
|
|
183
|
+
def default_value
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Looks for the value as a constant specified in the :namespace
|
|
187
|
+
# modules. Raises IncorrectInput if not found.
|
|
188
|
+
def lookup_const(str)
|
|
189
|
+
for mod in [@type[:namespace]].flatten
|
|
190
|
+
begin
|
|
191
|
+
return mod.const_get(str)
|
|
192
|
+
rescue
|
|
193
|
+
# Nothing, we still look up
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
raise IncorrectInput, "Constant #{str} not found"
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Returns a type name suitable for displaying, for instance, in
|
|
200
|
+
# an option parser, or inside a dialog box, and so on. Has to
|
|
201
|
+
# be one word (not to confuse the option parser, for instance);
|
|
202
|
+
# it is better if it is lowercase.
|
|
203
|
+
def type_name
|
|
204
|
+
return 'notype'
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# Creates on option for the OptionParser _parser_. The _args_
|
|
208
|
+
# will be fed directly to OptionParser#on. The _block_ is called with
|
|
209
|
+
# the value in the target type.
|
|
210
|
+
def option_parser_raw(parser, *args, &block)
|
|
211
|
+
b = block # For safe-keeping.
|
|
212
|
+
c = proc do |str|
|
|
213
|
+
b.call(string_to_type(str))
|
|
214
|
+
end
|
|
215
|
+
parser.on(*args, &c)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Creates an option for the OptionParser _parser_. The block is fed
|
|
219
|
+
# with the converted value. The default implementation should be
|
|
220
|
+
# fine for most classes, but this still leaves the room for
|
|
221
|
+
# reimplementation if necessary. The parameters are:
|
|
222
|
+
#
|
|
223
|
+
# * _parser_: the OptionParser;
|
|
224
|
+
# * _name_: the name of the option;
|
|
225
|
+
# * _desc_: it description,
|
|
226
|
+
# * _block_: the block used to set the data.
|
|
227
|
+
def option_parser_option(parser, name, desc, &block)
|
|
228
|
+
args = [option_parser_long_option(name), desc]
|
|
229
|
+
if @type.has_key?(:option_parser_short)
|
|
230
|
+
args.unshift(@type[:option_parser_short])
|
|
231
|
+
end
|
|
232
|
+
option_parser_raw(parser, *args, &block)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Returns a value to be fed to OptionParser#on as a 'long' option.
|
|
236
|
+
# It is separated from the rest to allow easy redefinition
|
|
237
|
+
# (in special cases). _name_ is the name of the option.
|
|
238
|
+
def option_parser_long_option(name)
|
|
239
|
+
return "--#{name} #{type_name.upcase}"
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# An exception that must be raised when ParamterType#string_to_type
|
|
243
|
+
# is given incorrect input.
|
|
244
|
+
class IncorrectInput < Exception
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# A parameter describes a way of storing some information into an
|
|
249
|
+
# instance of an object. No type checking is done on the target object
|
|
250
|
+
# when the actual reading/writing is done. However, the type checking
|
|
251
|
+
# is done upstream by the Description system.
|
|
252
|
+
#
|
|
253
|
+
# A Parameter consists of several things:
|
|
254
|
+
#
|
|
255
|
+
# * a #name, to identify it in a unique fashion;
|
|
256
|
+
# * a #type, used to convert to and from String and for user
|
|
257
|
+
# interaction in general;
|
|
258
|
+
# * some explanative text, used to inform the user: #long_name and
|
|
259
|
+
# #description
|
|
260
|
+
# * two symbols that are used to gain read and write access of the
|
|
261
|
+
# parameter on the target object.
|
|
262
|
+
#
|
|
263
|
+
# The Parameter class can be used to maintain a set of
|
|
264
|
+
# meta-informations about types in a given object.
|
|
265
|
+
class Parameter
|
|
266
|
+
|
|
267
|
+
# To forward string_to_type and type_to_string to the @param_type
|
|
268
|
+
extend Forwardable
|
|
269
|
+
|
|
270
|
+
# The short name of the parameter
|
|
271
|
+
attr_accessor :name
|
|
272
|
+
|
|
273
|
+
# The long name of the parameter, to be translated
|
|
274
|
+
attr_accessor :long_name
|
|
275
|
+
|
|
276
|
+
# The function names that should be used to set the symbol and
|
|
277
|
+
# retrieve it's current value. The corresponding functions should
|
|
278
|
+
# read or return a string, and writer(reader) should be a noop.
|
|
279
|
+
attr_accessor :reader_symbol, :writer_symbol
|
|
280
|
+
|
|
281
|
+
# The (text) description of the parameter
|
|
282
|
+
attr_accessor :description
|
|
283
|
+
|
|
284
|
+
# The way the type was specified to the system
|
|
285
|
+
attr_accessor :type_spec
|
|
286
|
+
|
|
287
|
+
# The actual ParameterType subclass instance in charge of handling
|
|
288
|
+
# the conversion from string.
|
|
289
|
+
attr_accessor :param_type
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
# (Vincent Fourmond 5/12/2006): this will be moved to ParameterType, as
|
|
293
|
+
# I think it has nothing to do here.
|
|
294
|
+
|
|
295
|
+
# The attributes hash is meant to hold various convenient
|
|
296
|
+
# information which are not directly linked to type conversion,
|
|
297
|
+
# such as an alternative for labels, shortcuts for command-line options
|
|
298
|
+
# and the like. Not compulsory. The currently understood values are:
|
|
299
|
+
# * :option_parser_short: a short option for option_parser.
|
|
300
|
+
|
|
301
|
+
# attr_accessor :attributes
|
|
302
|
+
|
|
303
|
+
# Creates a new Parameter with the given symbols. Remember that
|
|
304
|
+
# if you don't intend to use #get, #get_raw, #set and #set_raw,
|
|
305
|
+
# you don't need to pass meaningful values to _writer_symbol_ and
|
|
306
|
+
# _reader_symbol_.
|
|
307
|
+
def initialize(name, writer_symbol,
|
|
308
|
+
reader_symbol,
|
|
309
|
+
long_name,
|
|
310
|
+
type,
|
|
311
|
+
description, attributes = {})
|
|
312
|
+
@name = name
|
|
313
|
+
@writer_symbol = writer_symbol
|
|
314
|
+
@reader_symbol = reader_symbol
|
|
315
|
+
@description = description
|
|
316
|
+
@long_name = long_name
|
|
317
|
+
@type_spec = type
|
|
318
|
+
type_class = ParameterType.get_param_type(@type_spec)
|
|
319
|
+
raise "Parameter type not recognized: #{@type_spec}" unless type_class
|
|
320
|
+
|
|
321
|
+
@param_type = type_class.new(@type_spec)
|
|
322
|
+
@attributes = attributes
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
# Two methods forwarded to the ParameterType object.
|
|
327
|
+
def_delegators :@param_type, :string_to_type, :type_to_string
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
# Sets directly the target parameter, without type conversion
|
|
331
|
+
def set_raw(target, val)
|
|
332
|
+
target.send(@writer_symbol, val)
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# Uses the #writer_symbol of the _target_ to set the value of the
|
|
336
|
+
# parameter to the one converted from the String _str_
|
|
337
|
+
def set(target, str)
|
|
338
|
+
set_raw(target, string_to_type(str))
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
# Aquires the value from the backend, and returns it in the
|
|
343
|
+
# form of a string
|
|
344
|
+
def get(target)
|
|
345
|
+
return type_to_string(get_raw(target))
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# Aquires the value from the backend, and returns it.
|
|
349
|
+
def get_raw(target)
|
|
350
|
+
target.send(@reader_symbol)
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Creates an option in the OptionParser _parser_, with the name
|
|
354
|
+
# _name_, that will change the settings of the object _instance_
|
|
355
|
+
def option_parser_option(parser, name, instance)
|
|
356
|
+
obj = instance # copy needed to avoid problems with
|
|
357
|
+
# overwritten local variables
|
|
358
|
+
block = proc do |value|
|
|
359
|
+
set_raw(obj, value)
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# The use of #attributes is already deprecated. Don't use !!!
|
|
363
|
+
# if attributes.has_key?(:option_parser_short)
|
|
364
|
+
# @param_type.option_parser_raw(parser, attributes[:option_parser_short],
|
|
365
|
+
# @param_type.option_parser_long_option(name),
|
|
366
|
+
# description, &block)
|
|
367
|
+
# else
|
|
368
|
+
@param_type.option_parser_option(parser, name, description, &block)
|
|
369
|
+
# end
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
end
|