HelperClasses 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e9d72b8fe43505cae6d07152f6bf45f9d94cec5
4
- data.tar.gz: c8ec6b3f48a225602628f578122ac32f5bb7388a
3
+ metadata.gz: 93e502dfc1275307448cbb1a400fb2e9f66cc9bb
4
+ data.tar.gz: 9a11df7ce26d9d05413b1fc26c56943adec234b7
5
5
  SHA512:
6
- metadata.gz: cdd44f845254cadfc4771dce2c205fdca28139550e0d1bf6d2393cb67fd0f03f12a3cca089ce8ff3e5f5f117cd82a16f2614cd4288504d621f1ead249ec733a0
7
- data.tar.gz: 3404bd78e68baa95a8c991714c80a0f9326c50dd8624e40ea907e76fa5c79315a0a12368cb44321826cc3b0dac0d1060c092d35e16cc9f78e0e6928dcf83b84c
6
+ metadata.gz: 8dc8b0468193eb16e9b776b17951f9cfd353b6f84624edb65817bb9ec0b1ae3f9a84a58d7de96f9ecb9e784983e592b49fe55928579e33bc6fb4a5b69594aa15
7
+ data.tar.gz: 93af48558a982873408929fd5a57ac57e633ee5180dcd22aa461d8b44db3d61723bdeb88f52e15e163240a92fd13542d634597d58d1a8ed8d97cf29b0fdb3dcf
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ /nbproject
3
+ .hg*
4
+ .idea
5
+ commit
6
+ .DS_Store
7
+ test/test_config
@@ -36,6 +36,28 @@ will NOT evaluate it! The debug-levels are arbitrarily chosen like this:
36
36
  4
37
37
  5 - Dumping lots of raw data
38
38
 
39
+ ==== Fine-grained debugging
40
+
41
+ If you have a function with lots of _dputs_ in it, and you'd like to output
42
+ all debugging messages just from that function, you simply add
43
+
44
+ ```
45
+ dputs_func
46
+ ```
47
+
48
+ at the beginning of your function.
49
+
50
+ If you want just one specific _dputs_ to be evaluated, just change its name to
51
+ _ddputs_:
52
+
53
+ ```
54
+ DEBUG_LVL = 0
55
+
56
+ ddputs(5){"String with lots of data#{huge_var.inspect}"}
57
+ ```
58
+
59
+ will be evaluated!
60
+
39
61
  === Arraysym
40
62
 
41
63
  to_sym and to_sym! - calls .to_sym on all elements. Usage:
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'HelperClasses'
3
+ s.version = '0.2.0'
4
+ s.date = '2015-04-06'
5
+ s.summary = 'Hash._accessor Array.to_sym and DPuts'
6
+ s.description = 'Added accessors to Hash, to_sym to Array and a nice debugging-interface called DPuts'
7
+ s.authors = ['Linus Gasser']
8
+ s.email = 'ineiti@linusetviviane.ch'
9
+
10
+ s.files = `git ls-files -z`.split("\x0")
11
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
12
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
13
+ s.require_paths = ['lib']
14
+
15
+ s.homepage =
16
+ 'https://github.com/ineiti/HelperClasses'
17
+ s.license = 'GPLv3'
18
+ end
data/lib/helperclasses.rb CHANGED
@@ -1,3 +1,7 @@
1
- require 'helperclasses/hashaccessor'
2
1
  require 'helperclasses/arraysym'
3
2
  require 'helperclasses/dputs'
3
+ require 'helperclasses/hashaccessor'
4
+ require 'helperclasses/readconfig'
5
+ require 'helperclasses/service'
6
+ require 'helperclasses/system'
7
+ require 'helperclasses/timing'
@@ -1,11 +1,11 @@
1
1
 
2
2
  module HelperClasses
3
3
  module ArraySym
4
- refine Array do
5
- # Comptaibility for Ruby < 1.9
6
- if ! Array.respond_to? :to_h
4
+ class Array
5
+ # Comptaibility for Ruby <= 2.0
6
+ if ![].respond_to? :to_h
7
7
  def to_h
8
- Hash[ *self ]
8
+ Hash[*self.flatten]
9
9
  end
10
10
  end
11
11
 
@@ -5,27 +5,27 @@ module HelperClasses
5
5
  module DPuts
6
6
  extend self
7
7
  attr_accessor :mutex, :silent, :show_time, :terminal_width, :log_file
8
-
8
+
9
9
  @mutex = Mutex.new
10
10
  @silent = false
11
11
  @show_time = 60
12
12
  @terminal_width = 160
13
13
  @log_file = false
14
-
15
- def dputs_out( n, s, call )
14
+
15
+ def dputs_out(n, s, call)
16
16
  return if DPuts.silent
17
17
  if precision = DPuts.show_time
18
18
  $dputs_time ||= Time.now - 120
19
19
  now = Time.now
20
20
  show = false
21
21
  case precision
22
- when /sec/
23
- show = now.to_i != $dputs_time.to_i
24
- when /min/
25
- show = ( now.to_i / 60 ).floor != ( $dputs_time.to_i / 60 ).floor
22
+ when /sec/
23
+ show = now.to_i != $dputs_time.to_i
24
+ when /min/
25
+ show = (now.to_i / 60).floor != ($dputs_time.to_i / 60).floor
26
26
  end
27
- show and puts "\n *** It is now: " +
28
- Time.now.strftime( "%Y-%m-%d %H:%M:%S" )
27
+ show and puts "\n *** It is now: " +
28
+ Time.now.strftime("%Y-%m-%d %H:%M:%S")
29
29
  $dputs_time = now
30
30
  end
31
31
  DPuts.mutex.synchronize do
@@ -33,54 +33,72 @@ module HelperClasses
33
33
  width -= 30.0
34
34
  file, func = call.split(" ")
35
35
  file = file[/^.*\/([^.]*)/, 1]
36
- who = ( ":" + n.to_s + ":" + file.to_s +
37
- func.to_s ).ljust(30, [ "X","x","*","-","."," "][n])
36
+ who = (":" + n.to_s + ":" + file.to_s +
37
+ func.to_s).ljust(30, ["X", "x", "*", "-", ".", " "][n])
38
38
  lines = []
39
39
  pos = 0
40
- while ( pos < s.length )
40
+ while (pos < s.length)
41
41
  len = width
42
42
  if s.length - pos > width
43
- len = s.rindex( /[, .;=&>]/, pos + width )
43
+ len = s.rindex(/[, .;=&>]/, pos + width)
44
44
  len and len = len - pos + 1
45
45
  if len < width / 2
46
46
  len = width
47
47
  end
48
48
  end
49
- lines.push s.slice( pos, len )
49
+ lines.push s.slice(pos, len)
50
50
  pos += len
51
51
  end
52
52
  puts who + " " + lines.shift.to_s
53
- lines.each{|l|
54
- puts " " * ( 32 ) + l
53
+ lines.each { |l|
54
+ puts " " * (32) + l
55
55
  }
56
56
  end
57
57
  end
58
-
58
+
59
+
60
+ def dputs_getcaller
61
+ caller(0)[2].sub(/:.*:in/, '').sub(/block .*in /, '')
62
+ end
63
+
64
+ def dputs_func
65
+ $DPUTS_FUNCS ||= []
66
+ $DPUTS_FUNCS.push(dputs_getcaller) unless $DPUTS_FUNCS.index(dputs_getcaller)
67
+ end
68
+
69
+ def dputs_unfunc
70
+ $DPUTS_FUNCS ||= []
71
+ $DPUTS_FUNCS.index(dputs_getcaller) and $DPUTS_FUNCS.delete(dputs_getcaller)
72
+ end
73
+
59
74
  def dputs(n, &s)
60
- if DEBUG_LVL >= n
75
+ n *= -1 if ($DPUTS_FUNCS and $DPUTS_FUNCS.index(dputs_getcaller))
76
+ if !self.class.const_defined?(:DEBUG_LVL) or
77
+ self.class.const_get(:DEBUG_LVL) >= n
61
78
  s = yield s
62
- dputs_out( n, s, caller(0)[1] )
79
+ dputs_out(n, s, caller(0)[1])
63
80
  end
64
81
  end
65
82
 
66
- def ddputs( n, &s )
83
+ def ddputs(n, &s)
67
84
  s = yield s
68
- dputs_out( -n, s, caller(0)[1] )
85
+ #dp caller(0)
86
+ dputs_out(-n, s, caller(0)[1])
69
87
  end
70
-
71
- def dp( s )
72
- dputs_out( 0, s.class == String ? s : s.inspect, caller(0)[1] )
88
+
89
+ def dp(s)
90
+ dputs_out(0, s.class == String ? s : s.inspect, caller(0)[1])
73
91
  s
74
92
  end
75
93
 
76
- def log_msg( mod, msg )
77
- dputs( 1 ){ "Info from #{mod}: #{msg}" }
94
+ def log_msg(mod, msg)
95
+ dputs(1) { "Info from #{mod}: #{msg}" }
78
96
  return if not DPuts.log_file
79
- File.open( DPuts.log_file, "a" ){ |f|
80
- str = Time.now.strftime( "%a %y.%m.%d-%H:%M:%S #{mod}: #{msg}" )
97
+ File.open(DPuts.log_file, "a") { |f|
98
+ str = Time.now.strftime("%a %y.%m.%d-%H:%M:%S #{mod}: #{msg}")
81
99
  f.puts str
82
100
  }
83
101
  end
84
102
 
85
103
  end
86
- end
104
+ end
@@ -1,41 +1,44 @@
1
1
  module HelperClasses
2
2
  module HashAccessor
3
3
  refine Hash do
4
- # Converts all keys of a hash to syms recursively
5
- def to_sym
6
- ret = {}
7
- each{|k,v|
8
- ret[ k.to_sym ] = v.class == Hash ? v.to_sym : v
9
- }
10
- ret
11
- end
4
+ end
5
+ end
6
+ end
7
+
8
+ class Hash
9
+ # Converts all keys of a hash to syms recursively
10
+ def to_sym
11
+ ret = {}
12
+ each { |k, v|
13
+ ret[k.to_sym] = v.class == Hash ? v.to_sym : v
14
+ }
15
+ ret
16
+ end
17
+
18
+ def to_sym!
19
+ self.replace(to_sym())
20
+ end
12
21
 
13
- def to_sym!
14
- self.replace( to_sym() )
15
- end
16
-
17
- def method_missing( s, *args )
18
- case s.to_s
19
- when "to_ary"
20
- super( s, args )
21
- when /^_.*[^=]$/
22
- key = s.to_s.sub(/^_{1,2}/, '').to_sym
23
- self.has_key? key and return self[key]
24
- self.has_key? key.to_s and return self[key.to_s]
25
- if s.to_s =~ /^__/
26
- return self[key] = {}
27
- else
28
- return nil
29
- end
30
- when /^_.*=$/
31
- key = /^_{1,2}(.*)=$/.match(s.to_s)[1].to_sym
32
- self.has_key? key and return self[key] = args[0]
33
- self.has_key? key.to_s and return self[key.to_s] = args[0]
34
- return self[key] = args[0]
22
+ def method_missing(s, *args)
23
+ case s.to_s
24
+ when "to_ary"
25
+ super(s, args)
26
+ when /^_.*[^=]$/
27
+ key = s.to_s.sub(/^_{1,2}/, '').to_sym
28
+ self.has_key? key and return self[key]
29
+ self.has_key? key.to_s and return self[key.to_s]
30
+ if s.to_s =~ /^__/
31
+ return self[key] = {}
35
32
  else
36
- super( s, args )
33
+ return nil
37
34
  end
38
- end
35
+ when /^_.*=$/
36
+ key = /^_{1,2}(.*)=$/.match(s.to_s)[1].to_sym
37
+ self.has_key? key and return self[key] = args[0]
38
+ self.has_key? key.to_s and return self[key.to_s] = args[0]
39
+ return self[key] = args[0]
40
+ else
41
+ super(s, args)
39
42
  end
40
43
  end
41
44
  end
@@ -0,0 +1,50 @@
1
+ module HelperClasses
2
+ module ReadConfig
3
+ extend self
4
+
5
+ # Searches in this order:
6
+ # ~/.config
7
+ # ~
8
+ # /etc
9
+ #
10
+ # Returns nil if nothing found
11
+ def file_name(file)
12
+ %w( ~/.config ~ /etc ).each { |d|
13
+ file_abs = File.expand_path("#{d}/#{file}")
14
+ File.exists?(file_abs) and return file_abs
15
+ }
16
+ nil
17
+ end
18
+
19
+ # Very simple bash-reader, doesn't do array or multi-line configurations
20
+ def bash(file, downcase = false)
21
+ return nil unless File.exists? file
22
+ IO.readlines(file).collect { |l|
23
+ if l =~ /^#/
24
+ nil
25
+ elsif l =~ /([^ ]+)=(.*)/
26
+ [(downcase ? $1.downcase : $1).to_sym, $2]
27
+ end
28
+ }.compact.to_h
29
+ end
30
+
31
+ # Ruby file-reader, returns created hash
32
+ # THIS IS ABSOLUTELY INSECURE AND WILL EAT YOUR KITTENS!
33
+ # It returns what the file returns at the end - so most probably you'll want
34
+ # something like
35
+ #
36
+ # { one: 1,
37
+ # two: 2 }
38
+ #
39
+ # in that config-file
40
+ def ruby(file)
41
+ return {} unless File.exists? file.to_s
42
+ return eval(IO.readlines(file).join)
43
+ end
44
+
45
+ def json(file)
46
+ p 'Not implemented yet'
47
+ exit
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,104 @@
1
+ require 'helperclasses/dputs'
2
+ require 'helperclasses/system'
3
+
4
+ module HelperClasses
5
+ module Service
6
+ attr_accessor :system, :services
7
+
8
+ extend self
9
+ extend HelperClasses::DPuts
10
+ @system = case System.run_str 'uname -a'
11
+ when /ARCH/
12
+ :ArchLinux
13
+ when /Ubuntu/
14
+ :Ubuntu
15
+ when /Darwin/
16
+ :MacOSX
17
+ else
18
+ nil
19
+ end
20
+
21
+ @services = {
22
+ samba: {ArchLinux: %w( smbd nmbd ), Ubuntu: %w(smbd nmbd)},
23
+ cups: {ArchLinux: 'org.cups.cupsd', Ubuntu: 'cupsd'}
24
+ }
25
+
26
+ def service_get(service)
27
+ begin
28
+ @services[service.to_sym][@system]
29
+ rescue NoMethodError => _e
30
+ service.to_s
31
+ end
32
+ end
33
+
34
+ def service_run(service, cmd)
35
+ return unless @system
36
+ if !cmd
37
+ log_msg :Services, "System #{@system} can't start services"
38
+ return false
39
+ end
40
+ service_name = service_get(service)
41
+ if !service_name
42
+ log_msg :Services, "System #{@system} doesn't have service #{service}"
43
+ return false
44
+ end
45
+ cmd_system = cmd[@system]
46
+ if !cmd_system
47
+ log_msg :Services, "System #{@system} doesn't know how to do #{cmd}"
48
+ return false
49
+ end
50
+ [service_name].flatten.each { |s|
51
+ c = cmd_system.sub(/##/, s)
52
+ if !System.run_bool(c)
53
+ log_msg :Services, "Command #{c} failed"
54
+ return false
55
+ end
56
+ }
57
+ end
58
+
59
+ def start(service)
60
+ service_run(service, {ArchLinux: 'systemctl start ##',
61
+ Ubuntu: '/etc/init.d/## start',
62
+ MacOSX: nil}
63
+ )
64
+ end
65
+
66
+ def stop(service)
67
+ service_run(service, {ArchLinux: 'systemctl stop ##',
68
+ Ubuntu: '/etc/init.d/## stop',
69
+ MacOSX: nil}
70
+ )
71
+ end
72
+
73
+ def restart(service)
74
+ service_run(service, {ArchLinux: 'systemctl restart ##',
75
+ Ubuntu: '/etc/init.d/## restart',
76
+ MacOSX: nil}
77
+ )
78
+ end
79
+
80
+ def enable(service)
81
+ service_run(service, {ArchLinux: 'systemctl enable ##',
82
+ Ubuntu: nil,
83
+ MacOSX: nil}
84
+ )
85
+ end
86
+
87
+ def disable(service)
88
+ service_run(service, {ArchLinux: 'systemctl disable ##',
89
+ Ubuntu: nil,
90
+ MacOSX: nil}
91
+ )
92
+ end
93
+
94
+ def enable_start(service)
95
+ enable(service)
96
+ start(service)
97
+ end
98
+
99
+ def stop_disable(service)
100
+ disable(service)
101
+ stop(service)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,36 @@
1
+ # System-interaction for different flavours of Unix
2
+ require 'helperclasses/dputs'
3
+
4
+ module HelperClasses
5
+ module System
6
+ extend self
7
+ extend HelperClasses::DPuts
8
+ include HelperClasses::DPuts
9
+
10
+ def run_str(cmd)
11
+ dputs(3) { "Running command --#{cmd}--" }
12
+ %x[ #{cmd} ]
13
+ end
14
+
15
+ def run_bool(cmd)
16
+ dputs(3) { "Running command --#{cmd}--" }
17
+ Kernel.system("#{cmd} > /dev/null 2>&1")
18
+ end
19
+
20
+ def exists?(cmd)
21
+ dputs(3) { "Exist command --#{cmd}--?" }
22
+ run_bool("which #{cmd} > /dev/null 2>&1")
23
+ end
24
+
25
+ def rescue_all(msg = nil)
26
+ begin
27
+ yield
28
+ rescue Exception => e
29
+ msg and dputs(0) { msg }
30
+ dputs(0) { "#{e.inspect}" }
31
+ dputs(0) { "#{e.to_s}" }
32
+ e.backtrace.each { |l| dputs(0) { l } }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ module HelperClasses
2
+ class Timing
3
+ def initialize(dbg = 0)
4
+ @dbg_lvl = dbg
5
+ @time = Time.now
6
+ end
7
+
8
+ def probe(msg = '')
9
+ t = sprintf('%6f', (Time.now - @time).to_f)
10
+ dputs(@dbg_lvl) { "#{msg}: #{t}" }
11
+ @time = Time.now
12
+ end
13
+
14
+ def self.measure(msg = '', dbg = 0)
15
+ t = Timing.new(dbg)
16
+ ret = yield
17
+ t.probe(msg)
18
+ ret
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ class TC_Service < Test::Unit::TestCase
5
+
6
+ def setup
7
+ end
8
+
9
+ def test_service_get
10
+ Service.system = :ArchLinux
11
+
12
+ assert_equal %w(smbd nmbd), Service.service_get(:samba)
13
+ assert_equal 'dnsmasq', Service.service_get(:dnsmasq)
14
+ end
15
+ end
data/test/test.rb ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.push '../lib', '.'
3
+
4
+ require 'test/unit'
5
+ require 'helperclasses'
6
+ include HelperClasses
7
+
8
+ tests = Dir.glob( 'hc_*.rb' )
9
+ #tests = %w( )
10
+
11
+ tests.each{|t|
12
+ begin
13
+ require "hc_#{t}"
14
+ rescue LoadError => e
15
+ require t
16
+ end
17
+ }
File without changes
data/test/test_dputs.rb CHANGED
File without changes
File without changes
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.push('../lib')
4
+ require 'helperclasses/readconfig'
5
+
6
+ include HelperClasses
7
+
8
+ testfile = 'test_config'
9
+ IO.write(testfile, "#!/bin/bash
10
+ # This is an example config
11
+ TEST=hi
12
+ DB=foo.db
13
+ # This shouldn't pass
14
+ TEST2 = hi
15
+ # And some unrelated stuff
16
+ if '$1'; then
17
+ fi
18
+ ")
19
+
20
+ test = ReadConfig.bash( testfile )
21
+ p test
22
+
23
+ def printit
24
+ p test
25
+ end
26
+
27
+ printit
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.push '../lib'
4
+
5
+ require 'helperclasses/system'
6
+ include HelperClasses::System
7
+
8
+ rescue_all do
9
+ puts 'Hello there'
10
+ end
11
+ puts
12
+
13
+ rescue_all do
14
+ puts "Some math: #{10/0}"
15
+ end
16
+ puts
17
+
18
+ rescue_all('math-error') do
19
+ puts "Some math: #{10/0}"
20
+ end
metadata CHANGED
@@ -1,32 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: HelperClasses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linus Gasser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-28 00:00:00.000000000 Z
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Array.to_sym, Hash-accessors and debug-outputs with lazy evaluation
13
+ description: Added accessors to Hash, to_sym to Array and a nice debugging-interface
14
+ called DPuts
14
15
  email: ineiti@linusetviviane.ch
15
16
  executables: []
16
17
  extensions: []
17
18
  extra_rdoc_files: []
18
19
  files:
19
- - "./LICENSE"
20
- - "./README"
21
- - "./lib/helperclasses.rb"
22
- - "./lib/helperclasses/arraysym.rb"
23
- - "./lib/helperclasses/dputs.rb"
24
- - "./lib/helperclasses/hashaccessor.rb"
25
- - "./test/test_arraysym.rb"
26
- - "./test/test_dputs.rb"
27
- - "./test/test_hashaccessor.rb"
20
+ - ".gitignore"
28
21
  - LICENSE
29
- - README
22
+ - README.md
23
+ - helperclasses.gemspec
24
+ - lib/helperclasses.rb
25
+ - lib/helperclasses/arraysym.rb
26
+ - lib/helperclasses/dputs.rb
27
+ - lib/helperclasses/hashaccessor.rb
28
+ - lib/helperclasses/readconfig.rb
29
+ - lib/helperclasses/service.rb
30
+ - lib/helperclasses/system.rb
31
+ - lib/helperclasses/timing.rb
32
+ - test/hc_service.rb
33
+ - test/test.rb
34
+ - test/test_arraysym.rb
35
+ - test/test_dputs.rb
36
+ - test/test_hashaccessor.rb
37
+ - test/test_readconfig.rb
38
+ - test/test_system.rb
30
39
  homepage: https://github.com/ineiti/HelperClasses
31
40
  licenses:
32
41
  - GPLv3
@@ -47,8 +56,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
56
  version: '0'
48
57
  requirements: []
49
58
  rubyforge_project:
50
- rubygems_version: 2.2.0
59
+ rubygems_version: 2.2.2
51
60
  signing_key:
52
61
  specification_version: 4
53
- summary: Helpers for Array, Hash and debug-output
54
- test_files: []
62
+ summary: Hash._accessor Array.to_sym and DPuts
63
+ test_files:
64
+ - test/hc_service.rb
65
+ - test/test.rb
66
+ - test/test_arraysym.rb
67
+ - test/test_dputs.rb
68
+ - test/test_hashaccessor.rb
69
+ - test/test_readconfig.rb
70
+ - test/test_system.rb