ktcommon 0.0.7

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 81c64e7c3478570a0841f5e7972ff0e81113c62d
4
+ data.tar.gz: 6b4ee02f4af83f0e427fd5ef711fe36fb24d2fd1
5
+ SHA512:
6
+ metadata.gz: dad9d0a0eb629658e29dd094e63e1c0b9462d86b5c97f909a8e96bc91ac1ad7e748237c39f334d7f18241fb3e7533782f86bd5ca77d561564a90101a07f5a7f8
7
+ data.tar.gz: e8177af062a618dd562e780a65ea5fc7c8773b3f22970315b272a775ecd0289c2b446b73ee62735abc1af4c8148f24e694a4ca11e18ecc9dce5e242be7c67bc4
@@ -0,0 +1,14 @@
1
+ # Gitignore file
2
+ #
3
+ ##############################################################################
4
+
5
+
6
+ # SVN Ignore
7
+ #########################
8
+ .svn
9
+
10
+ # Project specific files
11
+ #########################
12
+ pkg/
13
+ install.cmd
14
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in test.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jeff McAffee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,55 @@
1
+ # KtCommon Library
2
+
3
+
4
+ ## Summary
5
+
6
+ KtCommon is a library of utility classes.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your Gemfile:
11
+
12
+ gem 'ktcommon'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ or install it yourself as:
19
+
20
+ $ gem install ktcommon
21
+
22
+ ## Usage
23
+
24
+ Require and instantiate classes as needed.
25
+
26
+ ## Testing
27
+
28
+ KtCommon uses RSpec for testing.
29
+
30
+ To run all existing tests:
31
+
32
+ $ rake spec
33
+
34
+ or directly:
35
+
36
+ $ bundle exec rspec
37
+
38
+ ## TODO
39
+
40
+ * Rename methods to follow ruby naming conventions (ie. `snake_case` rather than `camelCase`)
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://bitbucket.org/ktechsystems/ktcommon/fork )
45
+ 1. Clone it (`git clone git@bitbucket.org:[my-bitbucket-username]/ktcommon.git`)
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Create tests for your feature branch
48
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 5. Push to the branch (`git push origin my-new-feature`)
50
+ 6. Create a new Pull Request
51
+
52
+ ## License
53
+
54
+ This gem is licensed under the MIT license. See LICENSE file for details.
55
+
@@ -0,0 +1,14 @@
1
+ ========================================================================
2
+ = File: README.rdoc
3
+ = Purpose: KtCommon utilities
4
+ =
5
+ = Generated: 09/17/2010
6
+ = Copyright: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
7
+ = Website: http://ktechsystems.com
8
+ ========================================================================
9
+
10
+ OVERVIEW:
11
+
12
+ This project contains a number of utility class objects and mixins
13
+ used by other projects.
14
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ktcommon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ktcommon"
8
+ spec.version = KtCommon::VERSION
9
+ spec.authors = ["Jeff McAffee"]
10
+ spec.email = ["jeff@ktechsystems.com"]
11
+ spec.summary = %q{kTech common helper class library}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://bitbucket.org/ktechsystems/ktcommon"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "git_store"
25
+ end
@@ -0,0 +1,24 @@
1
+ ######################################################################################
2
+ #
3
+ # ktcommon.rb
4
+ #
5
+ # Jeff McAffee 6/27/09
6
+ #
7
+ # Purpose: kTech Common utility/helper functions and classes
8
+ #
9
+ ######################################################################################
10
+
11
+ module KtCommon
12
+
13
+ end # module
14
+
15
+ # Require source files
16
+
17
+ #require 'find'
18
+
19
+ #class_files = File.join( File.dirname(__FILE__), 'ktcommon', '*.rb')
20
+ #$: << File.join( File.dirname(__FILE__), 'ktcommon') # Add directory to the include file array
21
+ #Dir.glob(class_files) do | class_file |
22
+ # require class_file[/\w+\.rb$/]
23
+ #end
24
+
@@ -0,0 +1,75 @@
1
+ ##############################################################################
2
+ # File:: ktconsoleio.rb
3
+ # Purpose:: KtConsole I/O object gets input from the console.
4
+ #
5
+ # Author:: Jeff McAffee 09/26/2010
6
+ # Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
7
+ # Website:: http://ktechsystems.com
8
+ ##############################################################################
9
+
10
+
11
+ ##############################################################################
12
+ #
13
+ module KtCommon
14
+
15
+ require 'ktcommon/ktcmdline'
16
+
17
+ ###
18
+ # The ConsoleIO class provides methods for reading from and writing to
19
+ # the console.
20
+ #
21
+ class ConsoleIO
22
+ include KtCmdLine # Mixin the getInput() method
23
+
24
+ attr_reader :answers
25
+
26
+
27
+ ###
28
+ # ConsoleIO class constructor
29
+ #
30
+ def initialize()
31
+ $LOG.debug "ConsoleIO::new"
32
+ @answers = {}
33
+ end
34
+
35
+
36
+ ###
37
+ # Ask questions and store the answers.
38
+ # Questions will be an array of hashes. Each element in the array
39
+ # is a question. Each question hash can have the following keys:
40
+ # - :param - this will be used as the key to the answer hash
41
+ # - :q - The question to be displayed. No punctuation will be added so include it if needed.
42
+ # - :desc - Description text to be displayed before showing the question (optional)
43
+ #
44
+ def ask(questions)
45
+ $LOG.debug "ConsoleIO::ask"
46
+ raise ArgumentError.new("Expecting Array of Hashes. [questions] not an Array.") if questions.class != Array
47
+ raise ArgumentError.new("Expecting Array of Hashes. [questions] is empty.") if questions.empty?
48
+
49
+ qs = []
50
+ questions.each do |ques|
51
+ raise ArgumentError.new("Expecting Hash.") if ques.class != Hash
52
+ raise ArgumentError.new("Missing key :param") if !ques.has_key? :param
53
+ raise ArgumentError.new("Missing key :q") if !ques.has_key? :q
54
+
55
+ qs.clear
56
+ if(!ques[:desc].nil?) # If there is a :desc key,
57
+ if(ques[:desc].class == Array) # If it is an array,
58
+ ques[:desc].each {|d| qs << d} # add each element to the text to display.
59
+ else
60
+ qs << ques[:desc] # otherwise add :desc to text to display.
61
+ end
62
+ end
63
+ qs << ques[:q] # Add the actual question to text to display.
64
+
65
+ @answers[ques[:param]] = getInput(qs) # Ask the question and store the answer.
66
+ end
67
+
68
+ @answers # Return the answers hash for easy usage.
69
+ end
70
+
71
+
72
+ end # class ConsoleIO
73
+
74
+
75
+ end # module KtCommon
@@ -0,0 +1,162 @@
1
+ ##############################################################################
2
+ # File:: ktcsvloader.rb
3
+ # Purpose:: CSV Loader object for loading and parsing CSV files
4
+ #
5
+ # Author:: Jeff McAffee 09/25/2010
6
+ # Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
7
+ # Website:: http://ktechsystems.com
8
+ #
9
+ # This class is based on an original implementation of
10
+ # ruby/gdl/ppmgenerator/lib/ppmgenerator/csvloader.rb
11
+ #
12
+ ##############################################################################
13
+
14
+ require 'csv'
15
+
16
+ ##############################################################################
17
+ #
18
+ module KtCommon
19
+
20
+ ###
21
+ # CsvLoader is used to parse CSV data. It generates symbols to use as
22
+ # hash keys based on the header row of data. This means that a header
23
+ # row is REQUIRED.
24
+ #
25
+ # - All loaded data can be retrieved from the .rows attribute.
26
+ # - .rows is an array of Hashes, each hash being data for one row,
27
+ # indexed by the symbols mentioned above.
28
+ #
29
+ # Example Rows Data Layout:
30
+ #
31
+ # rows[0] = Hash{:colHeaderItem1 => "row1Col1 Data", :colHeaderItem2 => "row1Col2 Data"}
32
+ # rows[1] = Hash{:colHeaderItem1 => "row2Col1 Data", :colHeaderItem2 => "row2Col2 Data"}
33
+ # rows[2] = Hash{:colHeaderItem1 => "row3Col1 Data", :colHeaderItem2 => "row3Col2 Data"}
34
+ #
35
+ class CsvLoader
36
+
37
+ attr_reader :verbose
38
+ attr_reader :rows
39
+
40
+
41
+ ###
42
+ # CsvLoader constructor
43
+ #
44
+ def initialize()
45
+ $LOG.debug "KtCommon::CsvLoader::initialize"
46
+ @verbose = false
47
+ @rows = Array.new
48
+ end
49
+
50
+
51
+ ###
52
+ # Turn on verbose messaging.
53
+ #
54
+ # arg:: true to turn on verbose messaging
55
+ #
56
+ def verbose(arg)
57
+ $LOG.debug "KtCommon::CsvLoader::verbose( #{arg} )"
58
+ puts "Verbose mode: #{arg.to_s}" if @verbose
59
+ @verbose = arg
60
+ end
61
+
62
+
63
+ ###
64
+ # Create a symbol to be used for hash keys (column) indexes for
65
+ # each row. The symbol is created by removing all whitespace.
66
+ # All capitalization will be kept as-is.
67
+ #
68
+ # hdr:: Header data row from loaded CSV file
69
+ #
70
+ def createSymbolsFromHeader(hdr)
71
+ i = 0
72
+ @header = Array.new
73
+ hdr.each do |colHdr|
74
+ #puts "colHdr (before): " + colHdr
75
+ val = colHdr.gsub(/\s/, "").to_s
76
+ #puts "colHdr (after): " + val
77
+ raise "Unrecognized header value: #{colHdr} in #{__FILE__}:#{__LINE__}" if !colHdr.nil? && val.nil?
78
+ unless(colHdr.nil? || colHdr.empty?)
79
+ #@header[i] = colHdr.gsub!(/s/, "").to_s.intern if(!colHdr.nil? && !colHdr.empty?)
80
+ @header[i] = val.intern
81
+ end
82
+ i += 1
83
+ end # each colHdr
84
+
85
+ #puts "header[#{i.to_s}]: #{@header[i]}"
86
+
87
+ return true
88
+ end
89
+
90
+
91
+ ###
92
+ # Process a row of data. Used internally.
93
+ #
94
+ # row:: a row of data read from a CSV file
95
+ #
96
+ def process(row)
97
+ $LOG.debug "KtCommon::CsvLoader::process"
98
+ rowData = Hash.new
99
+ row.each_index do |i|
100
+ rowData[@header[i]] = row[i] if(!row[i].nil? && !row[i].empty?)
101
+ end
102
+ @rows << rowData
103
+ end
104
+ private :process
105
+
106
+
107
+ ###
108
+ # Load a CSV file and process it into an array of hashes.
109
+ #
110
+ # filepath:: path to file to process
111
+ #
112
+ def load(filepath)
113
+ $LOG.debug "KtCommon::CsvLoader::load( #{filepath} )"
114
+
115
+ if(!File.exist?( filepath ))
116
+ raise IOError.new("File not found: #{filepath}")
117
+ return false
118
+ end
119
+
120
+ reader = CSV.open(filepath, "r")
121
+ header = reader.shift
122
+
123
+ if(@verbose)
124
+ puts "CSV Column Headers:"
125
+ header.each {|h| puts "\t#{h}" }
126
+ puts
127
+ end
128
+
129
+ if(!createSymbolsFromHeader( header ))
130
+ return false
131
+ end
132
+
133
+ reader.each do |row|
134
+ process(row)
135
+ end
136
+
137
+ return true
138
+ end
139
+
140
+
141
+ ###
142
+ # Generate an example CSV file.
143
+ # filepath:: path and name of file to generate
144
+ # hdr:: array of text to create headers from
145
+ # rows:: an array of arrays of row data ( [ ["row1Col1", "row1Col2", "row1Col3"], ["row2Col1", "row2Col2", "row2Col3"] ] )
146
+ #
147
+ def generateTemplate(filepath, hdr, rows=nil)
148
+ $LOG.debug "KtCommon::CsvLoader::generateTemplate( #{filepath}, hdr )"
149
+
150
+ writer = CSV.open(filepath, "w")
151
+ writer << hdr
152
+ unless(rows.nil?)
153
+ rows.each {|r| writer << r}
154
+ end
155
+ writer.close
156
+ end
157
+
158
+
159
+ end # class CsvLoader
160
+
161
+
162
+ end # module KtCommon
@@ -0,0 +1,59 @@
1
+ ##############################################################################
2
+ # File:: date.rb
3
+ # Purpose:: KtCommon::Date module. Helper functions for date and time.
4
+ #
5
+ # Author:: Jeff McAffee 10/06/2010
6
+ # Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
7
+ # Website:: http://ktechsystems.com
8
+ ##############################################################################
9
+
10
+
11
+ ##############################################################################
12
+ #
13
+ module KtCommon
14
+
15
+
16
+ module Date
17
+
18
+ ###
19
+ # Return a timestamp string including both date and time.
20
+ #
21
+ # returns:: timestamp string
22
+ #
23
+ def timestamp()
24
+ now = DateTime::now()
25
+ now.strftime("%m/%d/%Y %H:%M:%S")
26
+ end # def
27
+
28
+
29
+ ###
30
+ # Return a datestamp string including only the date.
31
+ #
32
+ # returns:: datestamp string
33
+ #
34
+ def datestamp()
35
+ now = Date::today()
36
+ now.strftime("%m/%d/%Y")
37
+ end # def
38
+
39
+
40
+ ###
41
+ # Alias for #timestamp
42
+ #
43
+ def current_datetime()
44
+ return timestamp()
45
+ end # def
46
+
47
+
48
+ ###
49
+ # Alias for #datestamp
50
+ #
51
+ def current_date()
52
+ return datestamp()
53
+ end # def
54
+
55
+
56
+ end # module Date
57
+
58
+
59
+ end # module KtCommon
@@ -0,0 +1,118 @@
1
+ ##############################################################################
2
+ # File:: gitdatastore.rb
3
+ # Purpose:: GitDataStore object encapsulates a Git repository for programatic
4
+ # access.
5
+ #
6
+ # Author:: Jeff McAffee 09/17/2010
7
+ # Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
8
+ # Website:: http://ktechsystems.com
9
+ ##############################################################################
10
+
11
+ require 'stringio'
12
+ require 'git_store'
13
+
14
+ module KtCommon
15
+
16
+ ###########################################################################
17
+ # GitDataStore encapsulates a Git repository
18
+ class GitDataStore
19
+
20
+
21
+ def initialize(location)
22
+ $LOG.debug "GitDataStore::new"
23
+ @location = location
24
+ @store = nil
25
+ end
26
+
27
+
28
+ def ymlPath(path)
29
+ #puts "IN: #{path}"
30
+ raise ArgumentError.new("Empty path") if (path.nil? || path.empty?)
31
+
32
+ path += ".yml" unless !File.extname(path).empty?
33
+ #puts "OUT: #{path}"
34
+ return path
35
+ end
36
+
37
+
38
+ def store()
39
+ return @store unless @store.nil?
40
+ raise ArgumentError.new("No repo location provided before store requested.") unless (!@location.nil? && !@location.empty?)
41
+ begin
42
+ @store = GitStore.new(@location)
43
+ rescue ArgumentError => e
44
+ if(e.message.include?("must be a valid Git repo"))
45
+ raise RuntimeError.new("Cannot find a valid Git repo. Are you sure you've initialized the dir at #{@location}?")
46
+ end
47
+ raise $:
48
+ end
49
+ end
50
+
51
+
52
+ def location(location)
53
+ raise ArgumentError.new("Repo already opened when new location submitted: #{location}") unless @store.nil?
54
+ @location = location
55
+ end
56
+
57
+
58
+ def create(path, data)
59
+ $LOG.debug "GitDataStore::create"
60
+
61
+ store[ymlPath(path)] = data
62
+ commitMsg = "Added to data store: #{ymlPath(path)}"
63
+ store.commit commitMsg
64
+ end
65
+
66
+
67
+ ###
68
+ # Read a specific file from the store based on a <em>path</em>.
69
+ # path:: path to retrieve data from
70
+ # returns:: <em>one(1)</em> data object
71
+ def read(path)
72
+ $LOG.debug "GitDataStore::read(#{ymlPath(path)})"
73
+
74
+ return store[ymlPath(path)]
75
+ end
76
+
77
+
78
+ ###
79
+ # Read data from the store based on a <em>path</em>.
80
+ # path:: path to retrieve data from
81
+ # returns:: a Hash of data
82
+ def read_path(path)
83
+ $LOG.debug "GitDataStore::read_path(#{path})"
84
+
85
+ return store[path]
86
+ end
87
+
88
+
89
+ def update(path, data)
90
+ $LOG.debug "GitDataStore::update"
91
+
92
+ store[ymlPath(path)] = data
93
+ commitMsg = "Updated data store: #{ymlPath(path)}"
94
+ store.commit commitMsg
95
+ end
96
+
97
+
98
+ def delete(path)
99
+ $LOG.debug "GitDataStore::delete"
100
+
101
+
102
+ result = store.delete ymlPath(path)
103
+ commitMsg = "Deleted from data store: #{ymlPath(path)}"
104
+ store.commit commitMsg
105
+
106
+ return result
107
+ end
108
+
109
+
110
+ end # class GitDataStore
111
+
112
+ =begin ======================================================================
113
+ # ======================================================================
114
+ =end
115
+
116
+
117
+
118
+ end # module KtCommon
@@ -0,0 +1,84 @@
1
+ ######################################################################################
2
+ #
3
+ # ktCfg.rb
4
+ #
5
+ # Jeff McAffee 10/24/09
6
+ #
7
+ # Purpose: YAML based config file related functions
8
+ #
9
+ ######################################################################################
10
+
11
+ module KtCfg
12
+
13
+ require 'yaml'
14
+ require 'fileutils'
15
+ require 'ktcommon/ktpath'
16
+
17
+ class CfgFile
18
+
19
+ attr_reader :rootDir
20
+
21
+
22
+ def initialize(rootDir = nil)
23
+ $LOG.debug "CfgFile::initialize"
24
+ @rootDir = rootDir
25
+
26
+ if( rootDir )
27
+ @rootDir = File.rubypath(@rootDir)
28
+ if( !File.exists?(@rootDir))
29
+ $LOG.debug "Creating directory: #{@rootDir}"
30
+ FileUtils.mkdir(@rootDir)
31
+ end
32
+ end
33
+ end # initialize
34
+
35
+
36
+ def rootDir=(dir)
37
+ @rootDir = File.rubypath(dir)
38
+ end
39
+
40
+
41
+ def cfgFilePath(filename)
42
+ $LOG.debug "CfgFile::cfgFilePath( #{filename} )"
43
+ filepath = filename
44
+ if( @rootDir )
45
+ filepath = File.join(@rootDir, filename)
46
+ end
47
+ $LOG.debug "Filepath: #{filepath}"
48
+ filepath
49
+ end
50
+
51
+
52
+ def write(filename, cfg)
53
+ $LOG.debug "CfgFile::write"
54
+ $LOG.debug "Config File Contents: #{cfg.inspect} "
55
+
56
+ filepath = cfgFilePath(filename)
57
+
58
+ open(filepath, 'w') { |f| YAML.dump(cfg, f) }
59
+
60
+ end #write
61
+
62
+
63
+ def read(filename)
64
+ $LOG.debug "CfgFile::read"
65
+
66
+ filepath = cfgFilePath(filename)
67
+
68
+ cfg = {}
69
+
70
+ if(File.exists?(filepath))
71
+ open(filepath) { |f| cfg = YAML.load(f) }
72
+ end
73
+ cfg
74
+ end
75
+
76
+
77
+ end # class CfgFile
78
+
79
+
80
+
81
+ end # module KtPath
82
+
83
+
84
+
@@ -0,0 +1,42 @@
1
+ ######################################################################################
2
+ #
3
+ # ktCmdLine.rb
4
+ #
5
+ # Jeff McAffee 10/26/09
6
+ #
7
+ # Purpose: Command Line helper methods
8
+ #
9
+ ######################################################################################
10
+
11
+ module KtCmdLine
12
+
13
+ # require 'yaml'
14
+ # require 'fileutils'
15
+ # require 'ktcommon/ktpath'
16
+
17
+ ###
18
+ # Display a short message and collect input from the keyboard.
19
+ # Accepts either an array of strings or a single string.
20
+ # When the last array element is printed the cursor will
21
+ # stay on the same line waiting for user input.
22
+ #
23
+ # text:: string or array[string]: text to display to user
24
+ #
25
+ def getInput(text)
26
+ lines = text
27
+ *lines = text if text.class != Array
28
+ text = lines.pop
29
+ lines.each {|l| puts l }
30
+ $stdout.write "#{text} "
31
+ i = $stdin.readline
32
+ i.chomp!.strip!
33
+ return i
34
+ end # def getInput
35
+
36
+
37
+
38
+
39
+ end # module KtCmdLine
40
+
41
+
42
+
@@ -0,0 +1,63 @@
1
+ ##############################################################################
2
+ # File:: ktLogger.rb
3
+ # Purpose: Set up logging for ruby applications
4
+ #
5
+ # Author:: Jeff McAffee 10/30/2009
6
+ # Copyright:: Copyright (c) 2009 kTech Systems LLC. All rights reserved.
7
+ # Website:: http://ktechsystems.com
8
+ ##############################################################################
9
+
10
+
11
+ require 'logger'
12
+
13
+ # Available release levels.
14
+ releaseLevels = [:debug, :alpha, :beta, :production]
15
+
16
+ $DEBUG ||= false
17
+ $LOGGING ||= false
18
+ $RELEASE_LEVEL ||= :production
19
+
20
+ PROJNAME = 'ktLogger' unless defined?(PROJNAME)
21
+
22
+ if($LOGGING == true && PROJNAME.empty?)
23
+ raise("ktLogger: PROJNAME needs to be defined for logging purposes.")
24
+ end
25
+
26
+ if(!releaseLevels.include? $RELEASE_LEVEL)
27
+ raise("ktLogger: $RELEASE_LEVEL needs to be defined for logging purposes.")
28
+ end
29
+
30
+ debugfileindicator = "debug.txt"
31
+ if(File.exists?("/#{debugfileindicator}"))
32
+ $DEBUG = true
33
+ $LOGGING = true
34
+ $RELEASE_LEVEL = :debug
35
+ end
36
+
37
+
38
+ if($LOGGING)
39
+ $LOG = Logger.new( "#{PROJNAME.downcase}.log", 3, 100 * 1024 ) # Rotate 3 logs, each no larger than 100k
40
+ $LOG.datetime_format = "%Y-%m-%d %H:%M:%S"
41
+ $LOG.level = Logger::WARN
42
+ else
43
+ $LOG = Logger.new(STDERR)
44
+ $LOG.datetime_format = "%Y-%m-%d %H:%M:%S"
45
+ $LOG.level = Logger::FATAL
46
+ end
47
+
48
+ if($RELEASE_LEVEL == :debug)
49
+ $LOG.level = Logger::DEBUG
50
+ end
51
+
52
+ if($RELEASE_LEVEL == :alpha)
53
+ $LOG.level = Logger::INFO
54
+ end
55
+
56
+ if($RELEASE_LEVEL == :beta)
57
+ $LOG.level = Logger::WARN
58
+ end
59
+
60
+ if($RELEASE_LEVEL == :production)
61
+ $LOG.level = Logger::ERROR
62
+ end
63
+
@@ -0,0 +1,31 @@
1
+ ######################################################################################
2
+ #
3
+ # ktPath.rb
4
+ #
5
+ # Jeff McAffee 6/27/09
6
+ #
7
+ # Purpose: File path related functions
8
+ #
9
+ ######################################################################################
10
+
11
+
12
+ # Add path conversion functionality to File class
13
+ class File
14
+ end
15
+
16
+ class << File
17
+ # Convert windows file path seperators to forward slashes.
18
+ # path:: path to convert
19
+ def rubypath path
20
+ return path.gsub(/\\/, "/")
21
+ end
22
+
23
+ # Convert forward slashes to windows file path seperators (back slashes).
24
+ # path:: path to convert
25
+ def winpath path
26
+ return path.gsub(/\//, "\\")
27
+ end
28
+ end
29
+
30
+
31
+
@@ -0,0 +1,118 @@
1
+ ##############################################################################
2
+ # File:: ktRegistry.rb
3
+ # Purpose: Class to read windows registry.
4
+ #
5
+ # Author:: Jeff McAffee 10/04/09
6
+ # Copyright:: Copyright (c) 2009 kTech Systems LLC. All rights reserved.
7
+ # Website:: http://ktechsystems.com
8
+ ##############################################################################
9
+
10
+
11
+ require 'win32/registry'
12
+
13
+ class KtRegistry
14
+
15
+ attr_accessor :coName
16
+ attr_accessor :appName
17
+
18
+ def initialize(companyName="kTechSystems")
19
+ $LOG.debug "KtRegistry::initialize()"
20
+
21
+ @coName = companyName
22
+ @appName = "Unknown"
23
+ end
24
+
25
+
26
+ def softwarePath()
27
+ path = 'Software' + "\\" + @coName + "\\" + @appName
28
+ end
29
+
30
+
31
+ def readValue(valueName, defaultValue)
32
+ $LOG.debug "KtRegistry::readValue( #{valueName}, #{defaultValue} )"
33
+ result = ""
34
+ regType = nil
35
+ regValue = nil
36
+
37
+ begin
38
+ Win32::Registry::HKEY_LOCAL_MACHINE.open(softwarePath()) do |reg|
39
+ #default_reg_typ, reg_defaultVal = reg.read('') # Throws error if value doesn't exist.
40
+ regType, regValue = reg.read(valueName)
41
+ if(defaultValue.class == TrueClass || defaultValue.class == FalseClass) # Handle returning bools.
42
+ if(regValue != 0)
43
+ regValue = true
44
+ else
45
+ regValue = false
46
+ end
47
+ end
48
+ end
49
+ rescue Win32::Registry::Error => e
50
+ $LOG.error "Error ocurred while reading path: #{softwarePath()}, #{valueName}"
51
+ $LOG.error "#{e.class} : #{e.to_s}"
52
+ $LOG.debug "Returning user supplied default value."
53
+ regValue = defaultValue
54
+ end
55
+
56
+ regValue
57
+ end
58
+
59
+
60
+ def writeValue(valueName, value)
61
+ $LOG.debug "KtRegistry::setValue( #{valueName}, #{value} )"
62
+ $LOG.debug "Value class: #{value.class}"
63
+ regType = nil
64
+ regValue = nil
65
+
66
+ begin
67
+ Win32::Registry::HKEY_LOCAL_MACHINE.open(softwarePath(), Win32::Registry::Constants::KEY_WRITE) do |reg|
68
+ case value.class.to_s
69
+ when 'Fixnum'
70
+ # Write as a dword
71
+ $LOG.debug "Writing integer to registry"
72
+ reg.write_i(valueName, value)
73
+
74
+ when 'TrueClass'
75
+ # Write as a dword
76
+ $LOG.debug "Writing TrueClass to registry"
77
+ reg.write_i(valueName, 1)
78
+
79
+ when 'FalseClass'
80
+ # Write as a dword
81
+ $LOG.debug "Writing FalseClass to registry"
82
+ reg.write_i(valueName, 0)
83
+
84
+ else
85
+ # Write as a string
86
+ $LOG.debug "Writing String to registry"
87
+ reg.write_s(valueName, value)
88
+
89
+ end
90
+ end
91
+ rescue Win32::Registry::Error => e
92
+ $LOG.error "Error ocurred while writing path: #{softwarePath()}, #{valueName}"
93
+ $LOG.error "#{e.class} : #{e.to_s}"
94
+ return false
95
+ end
96
+
97
+ return true
98
+ end
99
+
100
+
101
+ def deleteValue(valueName)
102
+ $LOG.debug "KtRegistry::deleteValue( #{valueName} )"
103
+ begin
104
+ Win32::Registry::HKEY_LOCAL_MACHINE.open(softwarePath(), Win32::Registry::Constants::KEY_WRITE) do |reg|
105
+ reg.delete_value(valueName)
106
+ end
107
+ rescue Win32::Registry::Error => e
108
+ $LOG.error "Error ocurred while deleting value (#{valueName}) from path: #{softwarePath()}"
109
+ $LOG.error "#{e.class} : #{e.to_s}"
110
+ return false
111
+ end
112
+
113
+ return true
114
+ end
115
+
116
+ end # class KtRegistry
117
+
118
+
@@ -0,0 +1,5 @@
1
+ module KtCommon
2
+ VERSION = "0.0.7" unless constants.include?("VERSION")
3
+ APPNAME = "KtCommon" unless constants.include?("APPNAME")
4
+ COPYRIGHT = "Copyright (c) 2013, kTech Systems LLC. All rights reserved" unless constants.include?("COPYRIGHT")
5
+ end
@@ -0,0 +1,47 @@
1
+ ######################################################################################
2
+ # File:: rakefile
3
+ # Purpose:: Build tasks for ktCommon library
4
+ #
5
+ # Author:: Jeff McAffee 09/17/2010
6
+ # Copyright:: Copyright (c) 2010, kTech Systems LLC. All rights reserved.
7
+ # Website:: http://ktechsystems.com
8
+ ######################################################################################
9
+
10
+ require 'bundler/gem_tasks'
11
+ require 'psych'
12
+ gem 'rdoc', '>= 3.9.4'
13
+
14
+ require 'rake'
15
+ require 'rake/clean'
16
+ require 'rdoc/task'
17
+ require 'rspec/core/rake_task'
18
+
19
+ # Setup common directory structure
20
+
21
+
22
+ PROJNAME = "KtCommon"
23
+
24
+ # Setup common clean and clobber targets
25
+
26
+ CLEAN.include("pkg")
27
+ CLOBBER.include("pkg")
28
+
29
+
30
+ #############################################################################
31
+ RDoc::Task.new(:rdoc) do |rdoc|
32
+ files = ['docs/**/*.rdoc', 'lib/**/*.rb', 'app/**/*.rb']
33
+ rdoc.rdoc_files.add( files )
34
+ rdoc.main = "docs/README.rdoc" # Page to start on
35
+ #puts "PWD: #{FileUtils.pwd}"
36
+ rdoc.title = "#{PROJNAME} Documentation"
37
+ rdoc.rdoc_dir = 'doc' # rdoc output folder
38
+ rdoc.options << '--line-numbers' << '--all'
39
+ end
40
+
41
+
42
+ #############################################################################
43
+ desc "Run all specs"
44
+ RSpec::Core::RakeTask.new do |t|
45
+ #t.rcov = true
46
+ end
47
+
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ktcommon require file' do
4
+ it 'does not crash programs when required' do
5
+ require_relative '../lib/ktcommon'
6
+ end
7
+
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ktcommon/ktlogger require file' do
4
+ it 'does not crash programs when required' do
5
+ require_relative '../lib/ktcommon/ktlogger'
6
+ end
7
+
8
+
9
+ end
@@ -0,0 +1,16 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+
11
+ # Run specs in random order to surface order dependencies. If you find an
12
+ # order dependency and want to debug it, you can fix the order by providing
13
+ # the seed, which is printed after each run.
14
+ # --seed 1234
15
+ config.order = 'random'
16
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ktcommon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Jeff McAffee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: git_store
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: kTech common helper class library
56
+ email:
57
+ - jeff@ktechsystems.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - docs/README.rdoc
68
+ - ktcommon.gemspec
69
+ - lib/ktcommon.rb
70
+ - lib/ktcommon/consoleio.rb
71
+ - lib/ktcommon/csvloader.rb
72
+ - lib/ktcommon/date.rb
73
+ - lib/ktcommon/gitdatastore.rb
74
+ - lib/ktcommon/ktcfg.rb
75
+ - lib/ktcommon/ktcmdline.rb
76
+ - lib/ktcommon/ktlogger.rb
77
+ - lib/ktcommon/ktpath.rb
78
+ - lib/ktcommon/ktregistry.rb
79
+ - lib/ktcommon/version.rb
80
+ - rakefile.rb
81
+ - spec/ktcommon_spec.rb
82
+ - spec/ktlogger_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: https://bitbucket.org/ktechsystems/ktcommon
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.3.0
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: kTech common helper class library
108
+ test_files:
109
+ - spec/ktcommon_spec.rb
110
+ - spec/ktlogger_spec.rb
111
+ - spec/spec_helper.rb
112
+ has_rdoc: