dohutil 0.1.0
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/MIT-LICENSE +20 -0
- data/lib/doh/config.rb +21 -0
- data/lib/doh/findup.rb +12 -0
- data/lib/doh/options.rb +108 -0
- data/lib/doh/root.rb +41 -0
- metadata +55 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007-2012 Makani Mason, Kem Mason
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/doh/config.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'doh/root'
|
2
|
+
|
3
|
+
module Doh
|
4
|
+
|
5
|
+
def self.load_config_file(name)
|
6
|
+
path = File.join(Doh::root, 'config', name) + '.rb'
|
7
|
+
return false unless File.exist?(path)
|
8
|
+
require(path)
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.config
|
13
|
+
@config ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.get_required_config_value(value, desc)
|
17
|
+
raise "Attempt to get configuration value: #{value.inspect}, but none exists. #{desc}" if !config.key?(value)
|
18
|
+
config[value]
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/lib/doh/findup.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Doh
|
2
|
+
def self.findup(start_directory, filename, max_tries = 20)
|
3
|
+
curr_directory = start_directory
|
4
|
+
max_tries.times do
|
5
|
+
path = File.expand_path(File.join(curr_directory, filename))
|
6
|
+
return path if File.exist?(path)
|
7
|
+
return nil if (path == '/')
|
8
|
+
curr_directory = File.join(curr_directory, '..')
|
9
|
+
end
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
data/lib/doh/options.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'optparse'
|
3
|
+
|
4
|
+
module Doh
|
5
|
+
|
6
|
+
class Options
|
7
|
+
# this class is for command line parsing
|
8
|
+
# the hash is of the form: {'variable_name' => option_list}
|
9
|
+
# you can also pass an array of hashes [{'var1' => optlist1}, {'var2' => optlist2}]
|
10
|
+
# if you use an array of hashes, the help will be shown in the order specified (otherwise it's random)
|
11
|
+
|
12
|
+
# option_list is an array, the first element of which is the default value of the option
|
13
|
+
# the others are of the form with examples shown on
|
14
|
+
# http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html
|
15
|
+
# returns any non-options parameters via method varargs
|
16
|
+
# set allow_unknown_options to true to allow for filename specification, etc..
|
17
|
+
# if the default value is :required for a field, then if that field isn't
|
18
|
+
# specified, an exception will be raised
|
19
|
+
def initialize(param_definition_hash_or_array, allow_unknown_options = false, explanation_text = '', unknown_options_name = 'file', allow_no_prefix = false)
|
20
|
+
@vals = OpenStruct.new
|
21
|
+
|
22
|
+
@opts = OptionParser.new
|
23
|
+
@opts.banner = "Usage: #{$0} [options]"
|
24
|
+
if explanation_text != ''
|
25
|
+
@opts.separator ""
|
26
|
+
@opts.separator explanation_text
|
27
|
+
end
|
28
|
+
@opts.separator ""
|
29
|
+
@opts.separator "Specific options:"
|
30
|
+
@opts.separator ""
|
31
|
+
|
32
|
+
if param_definition_hash_or_array.class == Array
|
33
|
+
param_definition_array = param_definition_hash_or_array
|
34
|
+
param_definition_hash = param_definition_hash_or_array.inject({}) {|sum, elem| sum.merge(elem)}
|
35
|
+
else
|
36
|
+
param_definition_array = param_definition_hash_or_array.collect {|elem1, elem2| {elem1 => elem2}}
|
37
|
+
param_definition_hash = param_definition_hash_or_array
|
38
|
+
end
|
39
|
+
|
40
|
+
param_definition_array.each do |elem|
|
41
|
+
keyname = elem.to_a[0][1][2]
|
42
|
+
if keyname =~ /^--no-/
|
43
|
+
raise "key name: #{keyname} will not behave as desired if meant to be a boolean, rename it, or pass in allow_no_prefix flag as 5th param"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
param_definition_array.each do |elem|
|
48
|
+
key = elem.to_a[0][0]
|
49
|
+
val = elem.to_a[0][1]
|
50
|
+
default = val.shift
|
51
|
+
@vals.send("#{key}=", default)
|
52
|
+
val.last.insert(0, '(required) - ') if val.last.is_a?(String) && default == :required
|
53
|
+
@opts.on(*val) do |optval|
|
54
|
+
@vals.send("#{key}=", optval)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
@opts.separator ""
|
59
|
+
@opts.separator "Common options:"
|
60
|
+
@opts.separator ""
|
61
|
+
|
62
|
+
@opts.on("-h", "-?", "--help", "Show this message") do
|
63
|
+
puts @opts
|
64
|
+
exit
|
65
|
+
end
|
66
|
+
|
67
|
+
varargs = []
|
68
|
+
@opts.order(ARGV) do |arg|
|
69
|
+
varargs.push(arg)
|
70
|
+
end
|
71
|
+
@vals.varargs = varargs
|
72
|
+
|
73
|
+
unset_vars = []
|
74
|
+
param_definition_hash.each do |key,val|
|
75
|
+
if @vals.send(key) == :required
|
76
|
+
unset_vars.push(key)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
exception = ''
|
81
|
+
exception << "Required options not specified: #{unset_vars.inspect}\n" unless unset_vars.size == 0
|
82
|
+
exception << "Unknown options specified: #{varargs.inspect}\n" unless allow_unknown_options || (varargs.size == 0)
|
83
|
+
exception << "You must specify #{allow_unknown_options} #{unknown_options_name}#{allow_unknown_options > 1 ? 's' : ''}" if allow_unknown_options.class == Fixnum && varargs.size != allow_unknown_options
|
84
|
+
if allow_unknown_options.class == Range
|
85
|
+
if (allow_unknown_options.min == allow_unknown_options.max)
|
86
|
+
if (varargs.size < allow_unknown_options.min)
|
87
|
+
exception << "You must specify at least #{allow_unknown_options.min} #{unknown_options_name}#{allow_unknown_options.min > 1 ? 's' : ''}"
|
88
|
+
end
|
89
|
+
elsif !allow_unknown_options.include?(varargs.size)
|
90
|
+
exception << "You must specify between #{allow_unknown_options.min} and #{allow_unknown_options.max} #{unknown_options_name}s"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
if exception != ''
|
94
|
+
puts @opts
|
95
|
+
raise exception
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def method_missing(sym, *args)
|
100
|
+
@vals.send(sym, *args)
|
101
|
+
end
|
102
|
+
|
103
|
+
def to_s
|
104
|
+
@opts.to_s
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/lib/doh/root.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'doh/findup'
|
2
|
+
|
3
|
+
module Doh
|
4
|
+
def self.root
|
5
|
+
@root
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.root=(directory)
|
9
|
+
@root = directory
|
10
|
+
# having a root lib/ directory in your gem or application tree is a common standard now
|
11
|
+
libdir = File.join(@root, 'lib')
|
12
|
+
$LOAD_PATH.push(libdir) if libdir
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.find_root(start_directory, filename = 'dohroot', max_tries = 20)
|
16
|
+
rootfile = Doh::findup(start_directory, filename, max_tries)
|
17
|
+
if rootfile
|
18
|
+
Doh::root = File.dirname(rootfile)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.find_root_from_file(filepath = nil)
|
23
|
+
Doh::find_root(File.dirname(filepath || caller[0]))
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.find_root_from_path(path)
|
27
|
+
if File.directory?(path)
|
28
|
+
Doh::find_root(path)
|
29
|
+
else
|
30
|
+
Doh::find_root(File.dirname(path))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.find_root_from_prog
|
35
|
+
Doh::find_root(File.dirname($PROGRAM_NAME))
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.find_root_from_pwd
|
39
|
+
Doh::find_root(Dir.pwd)
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dohutil
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Makani Mason
|
9
|
+
- Kem Mason
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-01-27 00:00:00.000000000Z
|
14
|
+
dependencies: []
|
15
|
+
description: This is a collection of tiny standalone utilities, built primarily to
|
16
|
+
support other doh* gems (note the gems that are dependent on dohutil), but designed
|
17
|
+
in a way that hopefully makes them more generally useful.
|
18
|
+
email:
|
19
|
+
- gitauthor@pqmland.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files:
|
23
|
+
- MIT-LICENSE
|
24
|
+
files:
|
25
|
+
- lib/doh/config.rb
|
26
|
+
- lib/doh/findup.rb
|
27
|
+
- lib/doh/options.rb
|
28
|
+
- lib/doh/root.rb
|
29
|
+
- MIT-LICENSE
|
30
|
+
homepage: https://github.com/pquimo/dohutil
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 1.9.2
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.10
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Tiny standalone utilities, packaged together for convenience.
|
55
|
+
test_files: []
|