kwalify 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +232 -0
- data/MIT-LICENSE +20 -0
- data/README.txt +16 -19
- data/bin/kwalify +3 -3
- data/contrib/inline-require +6 -4
- data/contrib/kwalify +3719 -2427
- data/doc-api/classes/CommandOptionError.html +17 -17
- data/doc-api/classes/CommandOptionParser.html +63 -63
- data/doc-api/classes/Kwalify.html +29 -7
- data/doc-api/classes/Kwalify/AssertionError.html +9 -9
- data/doc-api/classes/Kwalify/BaseError.html +72 -71
- data/doc-api/classes/Kwalify/BaseParser.html +461 -0
- data/doc-api/classes/Kwalify/CommandOptionError.html +11 -11
- data/doc-api/classes/Kwalify/ErrorHelper.html +51 -46
- data/doc-api/classes/Kwalify/HashInterface.html +13 -135
- data/doc-api/classes/Kwalify/Json.html +105 -0
- data/doc-api/classes/Kwalify/Main.html +129 -126
- data/doc-api/classes/Kwalify/MetaValidator.html +248 -232
- data/doc-api/classes/Kwalify/Parser.html +12 -12
- data/doc-api/classes/Kwalify/PlainYamlParser.html +166 -163
- data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +11 -11
- data/doc-api/classes/Kwalify/Rule.html +152 -130
- data/doc-api/classes/Kwalify/SchemaError.html +10 -10
- data/doc-api/classes/Kwalify/SyntaxError.html +185 -0
- data/doc-api/classes/Kwalify/Types.html +26 -25
- data/doc-api/classes/Kwalify/Util.html +389 -0
- data/doc-api/classes/Kwalify/Util/HashLike.html +246 -0
- data/doc-api/classes/Kwalify/Util/OrderedHash.html +330 -0
- data/doc-api/classes/Kwalify/ValidationError.html +10 -10
- data/doc-api/classes/Kwalify/Validator.html +153 -86
- data/doc-api/classes/Kwalify/Yaml.html +181 -0
- data/doc-api/classes/Kwalify/Yaml/Parser.html +1538 -0
- data/doc-api/classes/Kwalify/YamlParser.html +190 -183
- data/doc-api/classes/Kwalify/YamlSyntaxError.html +8 -57
- data/doc-api/created.rid +1 -1
- data/doc-api/files/__/README_txt.html +17 -22
- data/doc-api/files/kwalify/errors_rb.html +2 -2
- data/doc-api/files/kwalify/main_rb.html +4 -3
- data/doc-api/files/kwalify/messages_rb.html +2 -2
- data/doc-api/files/kwalify/meta-validator_rb.html +3 -3
- data/doc-api/files/kwalify/{util/yaml-helper_rb.html → parser/base_rb.html} +8 -6
- data/doc-api/files/kwalify/parser/yaml_rb.html +117 -0
- data/doc-api/files/kwalify/rule_rb.html +2 -2
- data/doc-api/files/kwalify/types_rb.html +2 -2
- data/doc-api/files/kwalify/util/assert-text-equal_rb.html +2 -2
- data/doc-api/files/kwalify/util/hash-interface_rb.html +9 -2
- data/doc-api/files/kwalify/util/hashlike_rb.html +107 -0
- data/doc-api/files/kwalify/util/option-parser_rb.html +2 -2
- data/doc-api/files/kwalify/util/ordered-hash_rb.html +107 -0
- data/doc-api/files/kwalify/util/testcase-helper_rb.html +2 -2
- data/doc-api/files/kwalify/util_rb.html +107 -0
- data/doc-api/files/kwalify/validator_rb.html +2 -2
- data/doc-api/files/kwalify/yaml-parser_rb.html +2 -2
- data/doc-api/files/kwalify_rb.html +3 -2
- data/doc-api/fr_class_index.html +8 -1
- data/doc-api/fr_file_index.html +5 -1
- data/doc-api/fr_method_index.html +128 -69
- data/doc/img/fig01.png +0 -0
- data/doc/users-guide.html +882 -717
- data/examples/address-book/address-book.schema.yaml +2 -2
- data/examples/data-binding/BABEL.data.yaml +63 -0
- data/examples/data-binding/BABEL.schema.yaml +31 -0
- data/examples/data-binding/Makefile +8 -0
- data/examples/data-binding/Rakefile +13 -0
- data/examples/data-binding/main.rb +27 -0
- data/examples/invoice/invoice.schema.yaml +3 -3
- data/examples/tapkit/tapkit.schema.yaml +2 -2
- data/lib/kwalify.rb +41 -4
- data/lib/kwalify/errors.rb +118 -96
- data/lib/kwalify/kwalify.schema.yaml +58 -0
- data/lib/kwalify/main.rb +384 -377
- data/lib/kwalify/messages.rb +41 -27
- data/lib/kwalify/meta-validator.rb +251 -331
- data/lib/kwalify/parser/base.rb +127 -0
- data/lib/kwalify/parser/yaml.rb +837 -0
- data/lib/kwalify/rule.rb +545 -487
- data/lib/kwalify/templates/genclass-java.eruby +189 -162
- data/lib/kwalify/templates/genclass-php.eruby +104 -0
- data/lib/kwalify/templates/genclass-ruby.eruby +95 -66
- data/lib/kwalify/types.rb +107 -106
- data/lib/kwalify/util.rb +157 -0
- data/lib/kwalify/util/assert-text-equal.rb +33 -31
- data/lib/kwalify/util/hash-interface.rb +11 -30
- data/lib/kwalify/util/hashlike.rb +51 -0
- data/lib/kwalify/util/option-parser.rb +144 -144
- data/lib/kwalify/util/ordered-hash.rb +57 -0
- data/lib/kwalify/util/testcase-helper.rb +3 -3
- data/lib/kwalify/validator.rb +267 -212
- data/lib/kwalify/yaml-parser.rb +822 -768
- data/setup.rb +861 -607
- data/test/Rookbook.yaml +10 -0
- data/test/{tmp.dir/Context.java → data/users-guide/AddressBook.java.expected} +11 -11
- data/test/data/users-guide/BABEL.data.yaml +24 -0
- data/test/data/users-guide/BABEL.schema.yaml +30 -0
- data/test/data/users-guide/ExampleAddressBook.java +47 -0
- data/test/{tmp.dir/Group.java → data/users-guide/Group.java.expected} +2 -11
- data/test/data/users-guide/Person.java.expected +44 -0
- data/test/data/users-guide/address_book.rb +52 -0
- data/test/data/users-guide/address_book.schema.yaml +28 -0
- data/test/data/users-guide/address_book.yaml +27 -0
- data/test/data/users-guide/answers-schema.yaml +12 -0
- data/test/data/users-guide/answers-validator.rb +52 -0
- data/test/data/users-guide/babel_genclass.result +26 -0
- data/test/data/users-guide/config.schema.yaml +7 -0
- data/test/data/users-guide/config.yaml +4 -0
- data/test/{tmp.dir/silent1.document → data/users-guide/document01a.yaml} +0 -0
- data/test/data/users-guide/document01b.yaml +3 -0
- data/test/data/users-guide/document02a.yaml +4 -0
- data/test/data/users-guide/document02b.yaml +4 -0
- data/test/data/users-guide/document03a.yaml +6 -0
- data/test/data/users-guide/document03b.yaml +6 -0
- data/test/data/users-guide/document04a.yaml +9 -0
- data/test/data/users-guide/document04b.yaml +9 -0
- data/test/data/users-guide/document05a.yaml +11 -0
- data/test/data/users-guide/document05b.yaml +12 -0
- data/test/data/users-guide/document06a.yaml +15 -0
- data/test/data/users-guide/document06b.yaml +16 -0
- data/test/data/users-guide/document07a.yaml +9 -0
- data/test/data/users-guide/document07b.yaml +7 -0
- data/test/data/users-guide/document12a.json +10 -0
- data/test/data/users-guide/document12b.json +6 -0
- data/test/data/users-guide/document13a.yaml +17 -0
- data/test/data/users-guide/document14a.yaml +3 -0
- data/test/data/users-guide/document14b.yaml +3 -0
- data/test/data/users-guide/document15a.yaml +6 -0
- data/test/data/users-guide/document15b.yaml +5 -0
- data/test/data/users-guide/example_address_book.rb +10 -0
- data/test/data/users-guide/example_address_book_java.result +32 -0
- data/test/data/users-guide/example_address_book_ruby.result +31 -0
- data/test/data/users-guide/genclass_java.result +4 -0
- data/test/data/users-guide/howto-validation-with-parsing.rb +28 -0
- data/test/data/users-guide/howto-validation.rb +25 -0
- data/test/data/users-guide/howto3.rb +6 -0
- data/test/data/users-guide/howto3.result +5 -0
- data/test/data/users-guide/howto3.yaml +8 -0
- data/test/data/users-guide/howto5_databinding.result +111 -0
- data/test/data/users-guide/invalid01.result +3 -0
- data/test/data/users-guide/invalid02.result +5 -0
- data/test/data/users-guide/invalid03.result +5 -0
- data/test/data/users-guide/invalid04.result +4 -0
- data/test/data/users-guide/invalid05.result +11 -0
- data/test/data/users-guide/invalid06.result +4 -0
- data/test/data/users-guide/invalid07.result +3 -0
- data/test/data/users-guide/invalid08.result +3 -0
- data/test/data/users-guide/invalid12.json +8 -0
- data/test/data/users-guide/invalid14.result +4 -0
- data/test/data/users-guide/invalid15.result +4 -0
- data/test/data/users-guide/loadbabel.rb +27 -0
- data/test/data/users-guide/loadconfig.rb +15 -0
- data/test/data/users-guide/loadconfig.result +2 -0
- data/test/data/users-guide/models.rb +22 -0
- data/test/data/users-guide/option_ha.result +6 -0
- data/test/data/users-guide/option_ha_genclass_java.result +7 -0
- data/test/{tmp.dir/meta1.schema → data/users-guide/schema01.yaml} +0 -0
- data/test/data/users-guide/schema02.yaml +12 -0
- data/test/data/users-guide/schema03.yaml +9 -0
- data/test/data/users-guide/schema04.yaml +20 -0
- data/test/data/users-guide/schema05.yaml +29 -0
- data/test/data/users-guide/schema06.yaml +11 -0
- data/test/data/users-guide/schema12.json +12 -0
- data/test/data/users-guide/schema13.yaml +13 -0
- data/test/data/users-guide/schema14.yaml +5 -0
- data/test/data/users-guide/schema15.yaml +21 -0
- data/test/data/users-guide/valid01.result +2 -0
- data/test/data/users-guide/valid02.result +2 -0
- data/test/data/users-guide/valid03.result +2 -0
- data/test/data/users-guide/valid04.result +2 -0
- data/test/data/users-guide/valid05.result +2 -0
- data/test/data/users-guide/valid06.result +2 -0
- data/test/data/users-guide/valid07.result +2 -0
- data/test/data/users-guide/valid08.result +2 -0
- data/test/data/users-guide/valid12.result +2 -0
- data/test/data/users-guide/valid13.result +2 -0
- data/test/data/users-guide/valid14.result +2 -0
- data/test/data/users-guide/valid15.result +2 -0
- data/test/data/users-guide/validate08.rb +37 -0
- data/test/test-action.rb +78 -0
- data/test/test-action.yaml +738 -0
- data/test/test-databinding.rb +80 -0
- data/test/test-databinding.yaml +301 -0
- data/test/test-main.rb +129 -150
- data/test/test-main.yaml +126 -321
- data/test/test-metavalidator.rb +47 -47
- data/test/test-metavalidator.yaml +77 -21
- data/test/test-parser-yaml.rb +57 -0
- data/test/test-parser-yaml.yaml +1749 -0
- data/test/test-rule.rb +14 -15
- data/test/test-rule.yaml +6 -3
- data/test/test-users-guide.rb +75 -0
- data/test/test-validator.rb +77 -52
- data/test/test-validator.yaml +168 -6
- data/test/test-yaml-parser.rb +47 -0
- data/test/{test-yamlparser.yaml → test-yaml-parser.yaml} +159 -52
- data/test/test.rb +37 -21
- metadata +136 -37
- data/COPYING +0 -340
- data/ChangeLog +0 -70
- data/doc-api/classes/YamlHelper.html +0 -259
- data/lib/kwalify/util/yaml-helper.rb +0 -82
- data/test/test-yamlparser.rb +0 -58
- data/test/tmp.dir/User.java +0 -43
- data/test/tmp.dir/action1.document +0 -18
- data/test/tmp.dir/action1.schema +0 -32
- data/test/tmp.dir/action2.document +0 -18
- data/test/tmp.dir/action2.schema +0 -32
- data/test/tmp.dir/emacs.document +0 -6
- data/test/tmp.dir/emacs.schema +0 -6
- data/test/tmp.dir/meta1.document +0 -0
- data/test/tmp.dir/meta2.document +0 -0
- data/test/tmp.dir/meta2.schema +0 -3
- data/test/tmp.dir/silent1.schema +0 -3
- data/test/tmp.dir/silent2.document +0 -7
- data/test/tmp.dir/silent2.schema +0 -3
- data/test/tmp.dir/stream.invalid +0 -8
- data/test/tmp.dir/stream.schema +0 -3
- data/test/tmp.dir/stream.valid +0 -8
- data/test/tmp.dir/untabify.document +0 -5
- data/test/tmp.dir/untabify.schema +0 -10
- data/todo.txt +0 -34
@@ -0,0 +1,58 @@
|
|
1
|
+
## $Rev: 95 $
|
2
|
+
## $Date: 2008-01-27 22:49:22 +0900 (Sun, 27 Jan 2008) $
|
3
|
+
## copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
4
|
+
|
5
|
+
name: MAIN
|
6
|
+
type: map
|
7
|
+
required: yes
|
8
|
+
mapping: &main-rule
|
9
|
+
"name": { type: str }
|
10
|
+
"desc": { type: str }
|
11
|
+
"class": { type: str }
|
12
|
+
"type":
|
13
|
+
type: str
|
14
|
+
#required: yes
|
15
|
+
enum: [ seq, map, str, int, float, number, bool, text,
|
16
|
+
date, time, timestamp, any, scalar ]
|
17
|
+
"required": { type: bool } # equivarent to 'not-null'
|
18
|
+
"enum":
|
19
|
+
type: seq
|
20
|
+
sequence:
|
21
|
+
- type: scalar
|
22
|
+
unique: yes
|
23
|
+
"pattern": { type: str }
|
24
|
+
"assert": { type: str, pattern: /\bval\b/ }
|
25
|
+
"range":
|
26
|
+
type: map
|
27
|
+
mapping:
|
28
|
+
"max": { type: scalar }
|
29
|
+
"min": { type: scalar }
|
30
|
+
"max-ex": { type: scalar }
|
31
|
+
"min-ex": { type: scalar }
|
32
|
+
"length":
|
33
|
+
type: map
|
34
|
+
mapping:
|
35
|
+
"max": { type: int }
|
36
|
+
"min": { type: int }
|
37
|
+
"max-ex": { type: int }
|
38
|
+
"min-ex": { type: int }
|
39
|
+
"ident": { type: bool } # equivarent to 'primary-key' (undocumented)
|
40
|
+
"unique": { type: bool }
|
41
|
+
"default": { type: scalar } # used only in action's template
|
42
|
+
"sequence":
|
43
|
+
name: SEQUENCE
|
44
|
+
type: seq
|
45
|
+
sequence:
|
46
|
+
- type: map
|
47
|
+
mapping: *main-rule
|
48
|
+
name: MAIN
|
49
|
+
#required: yes
|
50
|
+
"mapping":
|
51
|
+
name: MAPPING
|
52
|
+
type: map
|
53
|
+
mapping:
|
54
|
+
=:
|
55
|
+
type: map
|
56
|
+
mapping: *main-rule
|
57
|
+
name: MAIN
|
58
|
+
#required: yes
|
data/lib/kwalify/main.rb
CHANGED
@@ -1,435 +1,442 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
4
|
-
### copyright(c) 2005 kuwata-lab all rights reserved.
|
2
|
+
### $Rev: 95 $
|
3
|
+
### $Release: 0.7.0 $
|
4
|
+
### copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
7
7
|
require 'yaml'
|
8
8
|
require 'erb'
|
9
9
|
require 'kwalify'
|
10
|
-
require 'kwalify/util
|
11
|
-
|
10
|
+
require 'kwalify/util'
|
11
|
+
require 'kwalify/util/ordered-hash'
|
12
12
|
|
13
13
|
|
14
14
|
module Kwalify
|
15
15
|
|
16
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
|
-
end
|
17
|
+
class CommandOptionError < KwalifyError
|
18
|
+
def initialize(message, option, error_symbol)
|
19
|
+
super(message)
|
20
|
+
@option = option
|
21
|
+
@error_symbol = error_symbol
|
22
|
+
end
|
23
|
+
attr_reader :option, :error_symbol
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
##
|
28
|
+
## ex.
|
29
|
+
## command = File.basename($0)
|
30
|
+
## begin
|
31
|
+
## main = Kwalify::Main.new(command)
|
32
|
+
## s = main.execute
|
33
|
+
## print s if s
|
34
|
+
## rescue Kwalify::CommandOptionError => ex
|
35
|
+
## $stderr.puts "ERROR: #{ex.message}"
|
36
|
+
## exit 1
|
37
|
+
## rescue Kwalify::KwalifyError => ex
|
38
|
+
## $stderr.puts "ERROR: #{ex.message}"
|
39
|
+
## exit 1
|
40
|
+
## end
|
41
|
+
##
|
42
|
+
class Main
|
43
|
+
|
44
|
+
|
45
|
+
def initialize(command=nil)
|
46
|
+
@command = command || File.basename($0)
|
47
|
+
@options = {}
|
48
|
+
@properties = {}
|
49
|
+
@template_path = []
|
50
|
+
$:.each do |path|
|
51
|
+
tpath = "#{path}/kwalify/templates"
|
52
|
+
@template_path << tpath if test(?d, tpath)
|
54
53
|
end
|
54
|
+
end
|
55
55
|
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
def debug?
|
58
|
+
@options[:debug]
|
59
|
+
end
|
60
60
|
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
69
|
-
sb << "properties:\n"
|
70
|
-
@properties.keys.sort_by {|k| k.to_s}.each do |key|
|
71
|
-
sb << " - #{key}: #{@properties[key]}\n"
|
72
|
-
end
|
73
|
-
#sb << "template_path:\n"
|
74
|
-
#@template_path.each do |path|
|
75
|
-
# sb << " - #{path}\n"
|
76
|
-
#end
|
77
|
-
return sb.join
|
62
|
+
def _inspect()
|
63
|
+
sb = []
|
64
|
+
sb << "command: #{@command}\n"
|
65
|
+
sb << "options:\n"
|
66
|
+
@options.keys.sort {|k1,k2| k1.to_s<=>k2.to_s }.each do |key|
|
67
|
+
sb << " - #{key}: #{@options[key]}\n"
|
78
68
|
end
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
# parse command-line options
|
83
|
-
filenames = _parse_argv(argv)
|
84
|
-
|
85
|
-
# help or version
|
86
|
-
if @options[:help] || @options[:version]
|
87
|
-
action = @options[:action]
|
88
|
-
s = ''
|
89
|
-
s << _version() << "\n" if @options[:version]
|
90
|
-
s << _usage() if @options[:help] && !action
|
91
|
-
s << _describe_properties(action) if @options[:help] && action
|
92
|
-
return s
|
93
|
-
end
|
94
|
-
|
95
|
-
# validation
|
96
|
-
if @options[:meta2]
|
97
|
-
s = _quick_meta_validate(filenames)
|
98
|
-
elsif @options[:meta]
|
99
|
-
s = _meta_validate(filenames)
|
100
|
-
elsif @options[:action]
|
101
|
-
if !@options[:schema]
|
102
|
-
#* key=:command_option_actionnoschema msg="schema filename is not specified."
|
103
|
-
raise option_error(:command_option_actionnoschema, @options[:action])
|
104
|
-
end
|
105
|
-
s = _perform_action(@options[:action], @options[:schema])
|
106
|
-
elsif @options[:schema]
|
107
|
-
if @options[:debug]
|
108
|
-
s = _inspect_schema(@options[:schema])
|
109
|
-
else
|
110
|
-
s = _validate(filenames, @options[:schema])
|
111
|
-
end
|
112
|
-
else
|
113
|
-
#* key=:command_option_noaction msg="command-line option '-f' or '-m' required."
|
114
|
-
raise option_error(:command_option_noaction, @command)
|
115
|
-
end
|
116
|
-
return s # or return (s == nil || s.empty?) ? nil : s
|
69
|
+
sb << "properties:\n"
|
70
|
+
@properties.keys.sort_by {|k| k.to_s}.each do |key|
|
71
|
+
sb << " - #{key}: #{@properties[key]}\n"
|
117
72
|
end
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
73
|
+
#sb << "template_path:\n"
|
74
|
+
#@template_path.each do |path|
|
75
|
+
# sb << " - #{path}\n"
|
76
|
+
#end
|
77
|
+
return sb.join
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
def execute(argv=ARGV)
|
82
|
+
## parse command-line options
|
83
|
+
filenames = _parse_argv(argv)
|
84
|
+
|
85
|
+
## help or version
|
86
|
+
if @options[:help] || @options[:version]
|
87
|
+
action = @options[:action]
|
88
|
+
s = ''
|
89
|
+
s << _version() << "\n" if @options[:version]
|
90
|
+
s << _usage() if @options[:help] && !action
|
91
|
+
s << _describe_properties(action) if @options[:help] && action
|
92
|
+
puts s
|
93
|
+
return
|
139
94
|
end
|
140
95
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
96
|
+
# validation
|
97
|
+
if @options[:meta2]
|
98
|
+
validate_schemafiles2(filenames)
|
99
|
+
elsif @options[:meta]
|
100
|
+
validate_schemafiles(filenames)
|
101
|
+
elsif @options[:action]
|
102
|
+
unless @options[:schema]
|
103
|
+
#* key=:command_option_actionnoschema msg="schema filename is not specified."
|
104
|
+
raise option_error(:command_option_actionnoschema, @options[:action])
|
105
|
+
end
|
106
|
+
perform_action(@options[:action], @options[:schema])
|
107
|
+
elsif @options[:schema]
|
108
|
+
if @options[:debug]
|
109
|
+
inspect_schema(@options[:schema])
|
110
|
+
else
|
111
|
+
validate_files(filenames, @options[:schema])
|
112
|
+
end
|
113
|
+
else
|
114
|
+
#* key=:command_option_noaction msg="command-line option '-f' or '-m' required."
|
115
|
+
raise option_error(:command_option_noaction, @command)
|
148
116
|
end
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
117
|
+
return
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
def self.main(command, argv=ARGV)
|
122
|
+
begin
|
123
|
+
main = Kwalify::Main.new(command)
|
124
|
+
s = main.execute(argv)
|
125
|
+
print s if s
|
126
|
+
rescue Kwalify::CommandOptionError => ex
|
127
|
+
raise ex if main.debug?
|
128
|
+
$stderr.puts ex.message
|
129
|
+
exit 1
|
130
|
+
rescue Kwalify::KwalifyError => ex
|
131
|
+
raise ex if main.debug?
|
132
|
+
$stderr.puts "ERROR: #{ex.to_s}"
|
133
|
+
exit 1
|
134
|
+
#rescue => ex
|
135
|
+
# if main.debug?
|
136
|
+
# raise ex
|
137
|
+
# else
|
138
|
+
# $stderr.puts ex.message
|
139
|
+
# exit 1
|
140
|
+
# end
|
160
141
|
end
|
142
|
+
end
|
161
143
|
|
162
144
|
|
163
|
-
|
164
|
-
template = File.read(template_filename)
|
165
|
-
trim_mode = 1
|
166
|
-
erb = ERB.new(template, $SAFE, trim_mode)
|
167
|
-
context = Object.new
|
168
|
-
hash.each do |key, val|
|
169
|
-
context.instance_variable_set("@#{key}", val)
|
170
|
-
end
|
171
|
-
return context.instance_eval(erb.src, template_filename)
|
172
|
-
end
|
145
|
+
private
|
173
146
|
|
174
147
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
end
|
148
|
+
def option_error(error_symbol, arg)
|
149
|
+
msg = Kwalify.msg(error_symbol) % arg
|
150
|
+
return CommandOptionError.new(msg, arg, error_symbol)
|
151
|
+
end
|
180
152
|
|
181
153
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
154
|
+
def _find_template(action)
|
155
|
+
template_filename = action + '.eruby'
|
156
|
+
unless test(?f, template_filename)
|
157
|
+
pathlist = []
|
158
|
+
pathlist.concat(@options[:tpath].split(/,/)) if @options[:tpath]
|
159
|
+
pathlist.concat(@template_path)
|
160
|
+
tpath = pathlist.find {|path| test(?f, "#{path}/#{template_filename}") }
|
161
|
+
#* key=:command_option_notemplate msg="%s: invalid action (template not found).\n"
|
162
|
+
raise option_error(:command_option_notemplate, action) unless tpath
|
163
|
+
template_filename = "#{tpath}/#{action}.eruby"
|
189
164
|
end
|
165
|
+
return template_filename
|
166
|
+
end
|
190
167
|
|
191
168
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
s = validator._inspect()
|
200
|
-
s << "\n" unless s[-1] == ?\n
|
201
|
-
return s
|
169
|
+
def apply_template(template_filename, hash)
|
170
|
+
template = File.read(template_filename)
|
171
|
+
trim_mode = 1
|
172
|
+
erb = ERB.new(template, nil, trim_mode)
|
173
|
+
context = Object.new
|
174
|
+
hash.each do |key, val|
|
175
|
+
context.instance_variable_set("@#{key}", val)
|
202
176
|
end
|
177
|
+
s = context.instance_eval(erb.src, template_filename)
|
178
|
+
return s
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
def _describe_properties(action)
|
183
|
+
template_filename = _find_template(action)
|
184
|
+
s = apply_template(template_filename, :describe=>true)
|
185
|
+
return s
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
def perform_action(action, schema_filename, describe=false)
|
190
|
+
template_filename = _find_template(action)
|
191
|
+
schema = _load_schemafile(schema_filename, ordered=true)
|
192
|
+
validator = Kwalify::Validator.new(schema)
|
193
|
+
@properties[:schema_filename] = schema_filename
|
194
|
+
hash = { :validator=>validator, :schema=>schema, :properties=>@properties }
|
195
|
+
s = apply_template(template_filename, hash)
|
196
|
+
puts s if s && !s.empty?
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
def inspect_schema(schema_filename)
|
201
|
+
schema = _load_schemafile(schema_filename)
|
202
|
+
if schema.nil?
|
203
|
+
puts "nil"
|
204
|
+
else
|
205
|
+
validator = Kwalify::Validator.new(schema) # error raised when schema is wrong
|
206
|
+
puts validator._inspect()
|
207
|
+
end
|
208
|
+
end
|
203
209
|
|
204
210
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
# rescue Kwalify::SchemaError => ex
|
212
|
-
# errors << ex
|
213
|
-
# end
|
214
|
-
# return errors
|
215
|
-
# end
|
216
|
-
#end
|
217
|
-
#++
|
211
|
+
## -f schemafile datafile
|
212
|
+
def validate_files(filenames, schema_filename)
|
213
|
+
schema = _load_schemafile(schema_filename)
|
214
|
+
validator = Kwalify::Validator.new(schema)
|
215
|
+
_validate_files(validator, filenames)
|
216
|
+
end
|
218
217
|
|
219
218
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
rescue Kwalify::SchemaError => ex
|
227
|
-
errors << ex
|
228
|
-
end
|
229
|
-
return errors
|
230
|
-
end
|
231
|
-
s = _validate_files(meta_validator, filenames)
|
232
|
-
return s
|
219
|
+
def _load_schemafile(schema_filename, ordered=false)
|
220
|
+
str = File.read(schema_filename)
|
221
|
+
if str.empty?
|
222
|
+
#* key=:schema_empty msg="%s: empty schema.\n"
|
223
|
+
msg = Kwalify.msg(:schema_emtpy) % filename
|
224
|
+
raise CommandOptionError.new(msg)
|
233
225
|
end
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
226
|
+
str = Util.untabify(str) if @options[:untabify]
|
227
|
+
parser = Kwalify::Yaml::Parser.new()
|
228
|
+
parser.preceding_alias = true if @options[:preceding]
|
229
|
+
parser.mapping_class = Kwalify::Util::OrderedHash if ordered
|
230
|
+
schema = parser.parse(str, :filename=>schema_filename) # or YAML.load(str)
|
231
|
+
return schema
|
232
|
+
end
|
233
|
+
|
234
|
+
|
235
|
+
## -m schemafile
|
236
|
+
def validate_schemafiles(schema_filenames)
|
237
|
+
meta_validator = Kwalify::MetaValidator.instance()
|
238
|
+
_validate_files(meta_validator, schema_filenames)
|
239
|
+
end
|
240
|
+
|
241
|
+
|
242
|
+
## -M schemafile
|
243
|
+
def validate_schemafiles2(schema_filenames)
|
244
|
+
parser = Kwalify::Yaml::Parser.new()
|
245
|
+
parser.preceding_alias = true if @options[:preceding]
|
246
|
+
for schema_filename in schema_filenames
|
247
|
+
str = File.read(schema_filename)
|
248
|
+
str = Util.untabify(str) if @options[:untabify]
|
249
|
+
schema = parser.parse(str, :filename=>schema_filename)
|
250
|
+
Kwalify::Validator.new(schema) # exception raised when schema has errors
|
242
251
|
end
|
252
|
+
end
|
243
253
|
|
244
254
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
255
|
+
def _validate_files(validator, filenames)
|
256
|
+
## parser
|
257
|
+
if @options[:linenum] || @options[:preceding]
|
258
|
+
parser = Kwalify::Yaml::Parser.new(validator)
|
259
|
+
parser.preceding_alias = true if @options[:preceding]
|
260
|
+
else
|
261
|
+
parser = nil
|
249
262
|
end
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
schema = _load_schema_file(schema_filename)
|
254
|
-
if schema
|
255
|
-
validator = Kwalify::Validator.new(schema)
|
256
|
-
s = _validate_files(validator, filenames)
|
257
|
-
else
|
258
|
-
#* key=:schema_empty msg="%s: empty schema.\n"
|
259
|
-
s = Kwalify.msg(:schema_emtpy) % filename
|
260
|
-
end
|
261
|
-
return s
|
263
|
+
## filenames
|
264
|
+
if filenames.empty?
|
265
|
+
filenames = [ $stdin ]
|
262
266
|
end
|
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
|
-
|
267
|
+
for filename in filenames
|
268
|
+
## read input
|
269
|
+
if filename.is_a?(IO)
|
270
|
+
input = filename.read()
|
271
|
+
filename = '(stdin)'
|
272
|
+
else
|
273
|
+
input = File.read(filename)
|
274
|
+
end
|
275
|
+
if input.empty?
|
276
|
+
#* key=:validation_empty msg="%s#%d: empty."
|
277
|
+
puts kwalify.msg(:validation_empty) % [filename, i]
|
278
|
+
#puts "#{filename}##{i}: empty."
|
279
|
+
next
|
280
|
+
end
|
281
|
+
input = Util.untabify(input) if @options[:untabify]
|
282
|
+
## parse input
|
283
|
+
if parser
|
284
|
+
#i = 0
|
285
|
+
#ydoc = parser.parse(input, :filename=>filename)
|
286
|
+
#_show_errors(filename, i, ydoc, parser.errors)
|
287
|
+
#while parser.has_next?
|
288
|
+
# i += 1
|
289
|
+
# ydoc = parser.parse_next()
|
290
|
+
# _show_errors(filename, i, ydoc, parser.errors)
|
291
|
+
#end
|
292
|
+
i = 0
|
293
|
+
parser.parse_stream(input, :filename=>filename) do |ydoc|
|
294
|
+
_show_errors(filename, i, ydoc, parser.errors)
|
295
|
+
i += 1
|
296
|
+
end
|
297
|
+
else
|
298
|
+
i = 0
|
299
|
+
YAML.load_documents(input) do |ydoc|
|
300
|
+
errors = validator.validate(ydoc)
|
301
|
+
_show_errors(filename, i, ydoc, errors)
|
302
|
+
i += 1
|
303
|
+
end
|
304
|
+
end
|
294
305
|
end
|
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
|
-
raise unless @options[:linenum]
|
321
|
-
s << "#{filename}:#{error.linenum}: [#{error.path}] #{error.message}\n"
|
322
|
-
elsif @options[:linenum]
|
323
|
-
s << " - (line #{error.linenum}) [#{error.path}] #{error.message}\n"
|
324
|
-
else
|
325
|
-
s << " - [#{error.path}] #{error.message}\n"
|
326
|
-
end
|
327
|
-
end
|
328
|
-
end
|
329
|
-
return s
|
306
|
+
end
|
307
|
+
|
308
|
+
|
309
|
+
def _show_errors(filename, i, ydoc, errors, ok_label="valid.", ng_label="INVALID")
|
310
|
+
if errors && !errors.empty?
|
311
|
+
puts "#{filename}##{i}: #{ng_label}"
|
312
|
+
errors.sort!
|
313
|
+
for error in errors
|
314
|
+
e = error
|
315
|
+
if @options[:emacs]
|
316
|
+
raise unless @options[:linenum]
|
317
|
+
puts "#{filename}:#{e.linenum}:#{e.column} [#{e.path}] #{e.message}\n"
|
318
|
+
elsif @options[:linenum]
|
319
|
+
puts " - (line #{e.linenum}) [#{e.path}] #{e.message}\n"
|
320
|
+
else
|
321
|
+
puts " - [#{e.path}] #{e.message}\n"
|
322
|
+
end
|
323
|
+
end
|
324
|
+
elsif ydoc.nil?
|
325
|
+
#* key=:validation_empty msg="%s#%d: empty.\n"
|
326
|
+
puts kwalify.msg(:validation_empty) % [filename, i]
|
327
|
+
else
|
328
|
+
#* key=:validation_valid msg="%s#%d: valid."
|
329
|
+
puts Kwalify.msg(:validation_valid) % [filename, i] unless @options[:quiet]
|
330
|
+
#puts "#{filename}##{i} - #{ok_label}" unless @options[:quiet]
|
330
331
|
end
|
332
|
+
end
|
331
333
|
|
332
334
|
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
335
|
+
def _usage()
|
336
|
+
#msg = Kwalify.msg(:command_help) % [@command, @command, @command]
|
337
|
+
msg = Kwalify.msg(:command_help)
|
338
|
+
return msg
|
339
|
+
end
|
338
340
|
|
339
341
|
|
340
|
-
|
341
|
-
|
342
|
-
|
342
|
+
def _version()
|
343
|
+
return RELEASE
|
344
|
+
end
|
343
345
|
|
344
346
|
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
end
|
347
|
+
def _to_value(str)
|
348
|
+
case str
|
349
|
+
when nil, "null", "nil" ; return nil
|
350
|
+
when "true", "yes" ; return true
|
351
|
+
when "false", "no" ; return false
|
352
|
+
when /\A\d+\z/ ; return str.to_i
|
353
|
+
when /\A\d+\.\d+\z/ ; return str.to_f
|
354
|
+
when /\/(.*)\// ; return Regexp.new($1)
|
355
|
+
when /\A'.*'\z/, /\A".*"\z/ ; return eval(str)
|
356
|
+
else ; return str
|
356
357
|
end
|
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
|
-
|
358
|
+
end
|
359
|
+
|
360
|
+
|
361
|
+
def _parse_argv(argv)
|
362
|
+
option_table = {
|
363
|
+
?h => :help,
|
364
|
+
?v => :version,
|
365
|
+
?q => :quiet,
|
366
|
+
?s => :quiet,
|
367
|
+
?t => :untabify,
|
368
|
+
#?z => :meta,
|
369
|
+
?m => :meta,
|
370
|
+
?M => :meta2,
|
371
|
+
?E => :emacs,
|
372
|
+
?l => :linenum,
|
373
|
+
?f => :schema,
|
374
|
+
?D => :debug,
|
375
|
+
?a => :action,
|
376
|
+
?I => :tpath,
|
377
|
+
?P => :preceding,
|
378
|
+
}
|
379
|
+
|
380
|
+
errcode_table = {
|
381
|
+
#* key=:command_option_schema_required msg="-%s: schema filename required."
|
382
|
+
?f => :command_option_schema_required,
|
383
|
+
#* key=:command_option_action_required msg="-%s: action required."
|
384
|
+
?a => :command_option_action_required,
|
385
|
+
#* key=:command_option_tpath_required msg="-%s: template path required."
|
386
|
+
?I => :command_option_tpath_required,
|
387
|
+
}
|
388
|
+
|
389
|
+
while argv[0] && argv[0][0] == ?-
|
390
|
+
optstr = argv.shift
|
391
|
+
optstr = optstr[1, optstr.length-1]
|
392
|
+
## property
|
393
|
+
if optstr[0] == ?-
|
394
|
+
unless optstr =~ /\A\-([-\w]+)(?:=(.*))?\z/
|
395
|
+
#* key=:command_property_invalid msg="%s: invalid property."
|
396
|
+
raise option_error(:command_property_invalid, optstr)
|
397
|
+
end
|
398
|
+
prop_name = $1; prop_value = $2
|
399
|
+
key = prop_name.gsub(/-/, '_').intern
|
400
|
+
value = prop_value.nil? ? true : _to_value(prop_value)
|
401
|
+
@properties[key] = value
|
402
|
+
## option
|
403
|
+
else
|
404
|
+
while optstr && !optstr.empty?
|
405
|
+
optchar = optstr[0]
|
406
|
+
optstr[0,1] = ""
|
407
|
+
unless option_table.key?(optchar)
|
408
|
+
#* key=:command_option_invalid msg="-%s: invalid command option."
|
409
|
+
raise option_error(:command_option_invalid, optchar.chr)
|
410
|
+
end
|
411
|
+
optkey = option_table[optchar]
|
412
|
+
case optchar
|
413
|
+
when ?f, ?a, ?I
|
414
|
+
arg = optstr.empty? ? argv.shift : optstr
|
415
|
+
raise option_error(errcode_table[optchar], optchar.chr) unless arg
|
416
|
+
@options[optkey] = arg
|
417
|
+
optstr = ''
|
398
418
|
else
|
399
|
-
|
400
|
-
optchar = optstr[0]
|
401
|
-
optstr[0,1] = ""
|
402
|
-
unless option_table.key?(optchar)
|
403
|
-
#* key=:command_option_invalid msg="-%s: invalid command option."
|
404
|
-
raise option_error(:command_option_invalid, optchar.chr)
|
405
|
-
end
|
406
|
-
optkey = option_table[optchar]
|
407
|
-
case optchar
|
408
|
-
when ?f, ?a, ?I
|
409
|
-
arg = optstr.empty? ? argv.shift : optstr
|
410
|
-
raise option_error(errcode_table[optchar], optchar.chr) unless arg
|
411
|
-
@options[optkey] = arg
|
412
|
-
optstr = ''
|
413
|
-
else
|
414
|
-
@options[optkey] = true
|
415
|
-
end
|
416
|
-
end
|
419
|
+
@options[optkey] = true
|
417
420
|
end
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
421
|
+
end
|
422
|
+
end
|
423
|
+
end # end of while
|
424
|
+
#
|
425
|
+
@options[:linenum] = true if @options[:emacs]
|
426
|
+
@options[:help] = true if @properties[:help]
|
427
|
+
@options[:version] = true if @properties[:version]
|
428
|
+
filenames = argv
|
429
|
+
return filenames
|
430
|
+
end
|
426
431
|
|
427
432
|
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
433
|
+
def _domain_type?(doc)
|
434
|
+
klass = defined?(YAML::DomainType) ? YAML::DomainType : YAML::Syck::DomainType
|
435
|
+
return doc.is_a?(klass)
|
436
|
+
end
|
437
|
+
|
438
|
+
|
439
|
+
end
|
432
440
|
|
433
|
-
end
|
434
441
|
|
435
442
|
end
|