helpema 2.1.200129 → 3.0.210108

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d18fa86fd912cfcc45ebb9efbdae0f9ae2a207399660fca0b3f0e3ce3b2bad64
4
- data.tar.gz: c18e1447a10e90e878644d982e1578965309264eae84c7badc3676c31e1c018b
3
+ metadata.gz: 0f73de852bf1c5e430ed5b5a0887e0b16ad37f3f4156843b40738065400b40e9
4
+ data.tar.gz: 111bed98f5e6f8c7cba8c1f2353ea8a1d94e24ac84cda4351380d54283bc779a
5
5
  SHA512:
6
- metadata.gz: e7b98dfb0588da2070870131947cff69d9777d586fe72a7d513e3156c3f78b405e9d513d195ae18a952d099894965c44d0ba06fddd605825c6f06fe86297f604
7
- data.tar.gz: f9cedadc0afe83aa718c637fe017555f50931023343ebc24519742b8cc17f89fe87367f4de4eafbfdcf30305b66ce4e0d23c8355ad344e62efa89e3827d42239
6
+ metadata.gz: 6b39435438e44edef870ed22e4b0a5acb6f2c2439cbe99906606fa62d6b224cbb1ce2fcc453f91c3b64bee6b70367d97605343c4a892db1e89eea0fac2a7fb39
7
+ data.tar.gz: 567e0d4eb2be6863b3f6fb9d183b77b3c549755dee855a629e282f544514dcf51b00cbc4ba9a4a5fdedb67e113ef963efd6a406a06c978182ea03f2cbaa7ee91
data/README.md CHANGED
@@ -1,90 +1,116 @@
1
- # helpema
1
+ # Helpema
2
2
 
3
- * [VERSION 2.1.200129](https://github.com/carlosjhr64/helpema/releases)
4
- * [github](https://github/carlosjhr64/helpema)
5
- * [rubygems](https://rubygems/gems/helpema)
3
+ * [VERSION 3.0.210108](https://github.com/carlosjhr64/helpema/releases)
4
+ * [github](https://github.com/carlosjhr64/helpema)
5
+ * [rubygems](https://rubygems.org/gems/helpema)
6
6
 
7
7
  ## DESCRIPTION:
8
8
 
9
9
  Meant to be an eclectic collection of useful single functions and wrappers.
10
10
 
11
- Wrappers:
12
-
13
- * ssss-split
14
- * ssss-combine
15
- * zbarcam
16
- * youtube-dl -j
17
-
18
- Functions:
19
-
20
- * requires("gemname version",...)
21
- * params(arg,...){|h Hash[Class,Obj]|...}
22
-
23
- More later.
24
-
25
- ## SYNOPSIS:
26
-
27
- require 'helpema'
28
- include HELPEMA
29
-
30
- Helpema.requires <<-GEMS
31
- awesome_print ~>1.8
32
- base_convert ~>4.0
33
- entropia ~>0.1
34
- GEMS
35
- #=> ["awesome_print", "base_convert", "entropia"]
36
- # Returns the list of loaded gems.
37
- # For those quick little scripts one writes in one's bin
38
- # that annoyingly keep falling out of maintainance... right?
39
-
40
- s0 = s1 = i = f0 = f1 = r = nil
41
- Helpema.params(1, 2.0, 3, "Five", /Six/, "Seven", /Eight/) do |params|
42
- s0 = params[String][0]
43
- s1 = params[String][1]
44
- i = params[Integer][0]
45
- f0 = params[Float][0]
46
- f1 = params[Float][1]
47
- r = params[Regexp][0]
48
- end
49
- s0 #=> "Five"
50
- s1 #=> "Seven"
51
- i #=> 1
52
- f0 #=> 2.0
53
- f1 #=> nil
54
- r #=> /Six/
55
-
56
- SSSS.split("Top Secret!", threshold: 2, shares: 3)
57
- #~> ^\["1-\h+", "2-\h+", "3-\h+"\]$
58
- # Note that the split has random outputs on the same inputs.
59
-
60
- SSSS.combine("3-055562917c41e68c6ab2c8", "1-27bf3cbfe8d2c25c7e8928")
61
- #=> Top Secret!
62
- # Pregenerated splits combine to reproduce the secret.
63
-
64
- list = []
65
- url = 'https://www.youtube.com/watch?v=u4oK3ZSccZI'
66
- YouTubeDL.json(url){|json| list.push json}
67
- # The url was for just one video
68
- list.length #=> 1
69
- json = list[0]
70
- json['title'] #=> "Fortnite Easy Last Ten"
71
-
72
- ZBar.cam( timeout=3 ) # Reads qrcodes on camera.
73
- ZBar.screen # Reads qrcodes on screen.
11
+ Featured method: `requires "good ~>3.0", "bad ~>2.7", "ugly ~>1.8"`
74
12
 
75
13
  ## INSTALL:
76
14
 
77
- $ sudo gem install helpema
15
+ ```shell
16
+ $ gem install helpema
17
+ ```
78
18
 
79
- ## FEATURES:
19
+ ## SYNOPSIS:
80
20
 
81
- * Autoloaded: no need to pick and choose which library component to require.
21
+ ```ruby
22
+ require 'helpema'
23
+ include Helpema
24
+ using Helpema
25
+
26
+ ### requires ###
27
+ # Ensure ruby's and helpema's version.
28
+ # Returns the list of loaded gems.
29
+ # For those quick little scripts one writes in one's bin
30
+ # that annoyingly keep falling out of maintainance... right?
31
+ requires'
32
+ ruby ~>3.0
33
+ helpema ~>3.0
34
+ base_convert ~>4.0
35
+ entropia ~>0.1'
36
+ #=> ["base_convert", "entropia"]
37
+
38
+ ### String#satisfies? ###
39
+ # Uses Gem::Requirement and Gem::Version to check version strings.
40
+ '1.2.3'.satisfies? '~>1.1' #=> true
41
+ '1.2.3'.satisfies? '~>1.3' #=> false
42
+
43
+ ### run_command ###
44
+ # Automates pipe creation to a system command.
45
+ # See the code for all available features.
46
+ run_command('date',{d: 'Dec 31, 2020'}) #=> "Thu Dec 31 12:00:00 AM PST 2020\n"
47
+
48
+ ### define_command ###
49
+ # Creates a method out of a system command.
50
+ # See the code for all available features.
51
+ define_command(:date, cmd: 'date', usage: {d: nil}, synonyms: {string: :d})
52
+ date(string: 'Dec 31, 2020') #=> "Thu Dec 31 12:00:00 AM PST 2020\n"
53
+
54
+ ### to_arg ###
55
+ # A helper function to do system command calls.
56
+ to_arg :q, true #=> "-q"
57
+ to_arg :quiet, true #=> "--quiet"
58
+ to_arg :verbose, false #=> nil
59
+ to_arg :f, '/path-to/file' #=> ["-f", "/path-to/file"]
60
+ to_arg :geo=, '10x20' #=> "--geo=10x20"
61
+ to_arg :arg0, 'Hello World' #=> "Hello World"
62
+
63
+ ### Hash#to_args ###
64
+ {q: true, quiet: true, verbose: false, f: '/path-to/file', :geo= => '10x20', arg0: 'Hello World'}.to_args
65
+ #=> ["-q", "--quiet", "-f", "/path-to/file", "--geo=10x20", "Hello World"]
66
+
67
+ ### Array#classify ###
68
+ # Groups items in Array by class.
69
+ [1, 2.0, :Three, 'Four', /Five/, :Six, 'Seven'].classify
70
+ #=> {Integer=>[1], Float=>[2.0], Symbol=>[:Three, :Six], String=>["Four", "Seven"], Regexp=>[/Five/]}
71
+
72
+ ### SSSS.split ####
73
+ SSSS.split(secret: "Top Secret!", threshold: 2, shares: 3)
74
+ #~> ^\["1-\h+", "2-\h+", "3-\h+"\]$
75
+ # Note that the split has random outputs on the same inputs.
76
+
77
+ #### SSSS.combine ###
78
+ # Pregenerated splits combine to reproduce the secret.
79
+ SSSS.combine(secrets: ["3-055562917c41e68c6ab2c8", "1-27bf3cbfe8d2c25c7e8928"], threshold: 2)
80
+ #=> "Top Secret!"
81
+
82
+ ### YouTubeDL.json ###
83
+ list = []
84
+ url = 'https://www.youtube.com/watch?v=u4oK3ZSccZI'
85
+ YouTubeDL.json(url){|json| list.push json}
86
+ # The url was for just one video
87
+ list.length #=> 1
88
+ json = list[0]
89
+ json['title'] #=> "Fortnite Easy Last Ten"
90
+
91
+ ### ZBar.screen ###
92
+ # Reads qrcodes on screen.
93
+ string_or_nil = ZBar.screen
94
+
95
+ ### ZBar.cam ###
96
+ # Reads qrcodes from camera.
97
+ # You may want to wrap this one in a Timeout block.
98
+ string = ZBar.cam
99
+ ```
100
+
101
+ ## TROUBLESHOOTING:
102
+
103
+ Command version mismatch
104
+ : set `Helpema::WRAPPER.version = "your.version"` or just nil it.
105
+
106
+ More documentation
107
+ : see [sig/helpema.rb](sig/helpema.rbs) for the expected method signatures.
82
108
 
83
109
  ## LICENSE:
84
110
 
85
111
  (The MIT License)
86
112
 
87
- Copyright (c) 2020 carlosjhr64
113
+ Copyright (c) 2021 carlosjhr64
88
114
 
89
115
  Permission is hereby granted, free of charge, to any person obtaining
90
116
  a copy of this software and associated documentation files (the
@@ -1,14 +1,18 @@
1
- module HELPEMA
2
- VERSION = '2.1.200129'
1
+ module Helpema
2
+ VERSION = '3.0.210108'
3
+
4
+ require_relative 'helpema/helpema'
3
5
 
4
- autoload :Helpema, 'helpema/helpema.rb'
5
6
  autoload :SSSS, 'helpema/ssss.rb'
6
7
  autoload :YouTubeDL, 'helpema/youtubedl.rb'
7
8
  autoload :ZBar, 'helpema/zbar.rb'
8
9
  end
10
+
9
11
  # Requires:
10
12
  #`ruby`
13
+ #`youtube-dl`
11
14
  #`ssss-split`
12
15
  #`ssss-combine`
13
16
  #`zbarcam`
17
+ #`zbarimg`
14
18
  #`gnome-screenshot`
@@ -1,21 +1,112 @@
1
- module HELPEMA
2
1
  module Helpema
2
+ refine ::String do
3
+ def satisfies?(*reqs)
4
+ Gem::Requirement.new(*reqs).satisfied_by? Gem::Version.new(self)
5
+ end
6
+ end
7
+ refine ::Hash do
8
+ def to_args(usage:nil, synonyms:nil)
9
+ # create separate args from self with the translated synonyms
10
+ args = self.transform_keys(synonyms.to_h)
11
+ # pad usage's defaults to args
12
+ usage&.each{|key,default| args[key]=default unless args.has_key? key}
13
+ # order might be important so enforce usage
14
+ args = usage&.map{|k,v|[k,args[k]]} || args.to_a
15
+ # convert key,value tuples to final list of args
16
+ args.map!(&:to_arg)
17
+ # get rid of nil
18
+ args.select!{_1}
19
+ # ...and finally flatten!
20
+ args.flatten!
21
+ return args
22
+ end
23
+ end
24
+ refine ::Array do
25
+ def classify(hash: Hash.new{|h,k|h[k]=[]})
26
+ self.each{|v| hash[v.class] << v}
27
+ return hash
28
+ end
29
+ def to_arg
30
+ Helpema.to_arg(*self)
31
+ end
32
+ end
33
+ refine ::Symbol do
34
+ def to_flag
35
+ return nil if self[-1].match?(/\d/) # like :arg0
36
+ (self.length > 1)? "--#{self}": "-#{self}" # like --verbose or -V
37
+ end
38
+ end
39
+ using Helpema
40
+
41
+ def to_arg(key,value)
42
+ # keep only keys with value(no false or nil)
43
+ return nil unless value
44
+ # assume nil0/--long/-short
45
+ key = key.to_flag
46
+ if key
47
+ return key if value==true # a flag
48
+ return "#{key}#{value}" if key[-1]=='=' # joined key=value
49
+ return [key,value.to_s]
50
+ end
51
+ # It's a Nth arg, like arg0...
52
+ return value.to_s
53
+ end
54
+
55
+ def run_command(cmd, options={},
56
+ usage:nil, synonyms:nil, mode:'r',
57
+ exception:nil, forward_pass:nil, **popt, &blk)
58
+ args,ret = options.to_args(usage:usage,synonyms:synonyms),nil
59
+ $stderr.puts "#{cmd} #{args.join(' ')}" if $DEBUG
60
+ IO.popen([cmd, *args], mode, **popt) do |pipe|
61
+ ret = (forward_pass)? forward_pass.call(pipe, options, blk): (blk)? blk.call(pipe): pipe.read
62
+ end
63
+ (exception.nil? or $?.exitstatus==0)? ret : raise(exception)
64
+ end
65
+
66
+ def define_command(name,
67
+ cmd: name.to_s, version: nil, v: nil,
68
+ usage: nil, synonyms: nil,
69
+ mode: 'r',exception: nil,
70
+ **popt, &forward_pass)
71
+
72
+ # which version? --version or -v
73
+ if version and not `#{cmd} --version`.strip.match?(version)
74
+ raise "`#{cmd} --version` !~ #{version}"
75
+ end
76
+ if v and not `#{cmd} -v`.strip.match?(v)
77
+ raise "`#{cmd} -v` !~ #{v}"
78
+ end
79
+
80
+ define_method(name) do |**options, &blk|
81
+ run_command(cmd, options,
82
+ usage:usage, synonyms:synonyms, mode:mode,
83
+ exception:exception, forward_pass:forward_pass, **popt, &blk)
84
+ end
85
+ end
86
+
3
87
  def requires(*list)
4
88
  loaded = []
5
89
  list.each do |gems|
6
- gems.lines.each do |gemname_version|
7
- gemname, *version = gemname_version.split
8
- gem gemname, *version unless version.empty?
90
+ gems.lines.each do |gemname_reqs|
91
+ gemname, *reqs = gemname_reqs.split
92
+ next unless gemname
93
+ unless reqs.empty?
94
+ case gemname
95
+ when 'helpema'
96
+ raise "helpema #{VERSION} not #{reqs.join(', ')}" unless VERSION.satisfies?(*reqs)
97
+ next
98
+ when 'ruby'
99
+ raise "ruby #{RUBY_VERSION} not #{reqs.join(', ')}" unless RUBY_VERSION.satisfies?(*reqs)
100
+ next
101
+ else
102
+ gem gemname, *reqs
103
+ end
104
+ end
9
105
  require gemname and loaded.push gemname
10
106
  end
11
107
  end
12
- loaded
13
- end
14
-
15
- def params(*args)
16
- yield args.inject(Hash.new{|h,k|h[k]=[]}){|h,v|h[v.class].push(v) && h}
108
+ return loaded
17
109
  end
18
110
 
19
111
  extend self
20
112
  end
21
- end
@@ -1,34 +1,36 @@
1
- require 'open3'
2
-
3
- module HELPEMA
1
+ module Helpema
4
2
  module SSSS
5
- # Note how the function mirrors the command line options.
6
- def self.split(secret, t0=2, n0=3,
7
- t: t0, n: n0,
8
- threshold: t, shares: n,
9
- w: nil, s: nil, x: false,
10
- token: w, level: s, hexmode: x)
11
- pwds = nil
12
- command = "ssss-split -Q -t #{threshold} -n #{shares}"
13
- command << " -w #{token}" if token
14
- command << " -s #{level}" if level
15
- command << " -x" if hexmode
16
- Open3.popen3(command) do |stdin, stdout, stderr|
17
- stdin.puts secret
18
- stdin.close
19
- pwds = stdout.read.split("\n")
20
- end
21
- return pwds
3
+ class << self
4
+ attr_accessor :version
5
+ SSSS.version = 'Version: 0\.[567]\b' # version as of this writing is 0.5
6
+
7
+ def split(secret:, threshold:, shares:, token:nil, level:nil, hexmode:false) =
8
+ _split(secret:secret, threshold:threshold, shares:shares, token:token, level:level, hexmode:hexmode)
9
+ define_command(:_split,
10
+ cmd: 'ssss-split', v: SSSS.version,
11
+ usage: {Q:true,t:2,n:3,w:nil,s:nil,x:false},
12
+ synonyms: {threshold: :t, shares: :n, token: :w, level: :s, hexmode: :x},
13
+ mode: 'w+', exception: 'ssss-split failed.',
14
+ err: [:child, :out]
15
+ ) do |pipe, options, blk|
16
+ pipe.puts options.fetch(:secret)
17
+ pipe.read.split.last(options[:shares])
22
18
  end
23
19
 
24
- def self.combine(*pwds)
25
- pwd = ''
26
- Open3.popen3("ssss-combine -q -t #{pwds.length}") do |stdin, stdout, stderr|
27
- pwds.each{|p| stdin.puts p}
28
- stdin.close
29
- pwd = stderr.read.split("\n").last
30
- end
31
- return pwd
20
+ def combine(secrets:, threshold:, hexmode:false)
21
+ raise 'Need threshold number of secrets.' unless secrets.size >= threshold
22
+ _combine(secrets:secrets, threshold:threshold, hexmode:hexmode)
32
23
  end
24
+ define_command(:_combine,
25
+ cmd: 'ssss-combine', v: SSSS.version,
26
+ usage: {Q:true,t:2,x:false}, synonyms: {threshold: :t, hexmode: :x},
27
+ mode: 'w+', exception: 'ssss-combine failed.',
28
+ err: [:child, :out]
29
+ ) do |pipe, options, blk|
30
+ secrets,n = options.fetch_values(:secrets,:threshold)
31
+ secrets.first(n).each{pipe.puts _1}
32
+ pipe.read.lines.last.chomp
33
+ end
34
+ end
33
35
  end
34
36
  end
@@ -1,20 +1,25 @@
1
1
  require 'json'
2
- require 'open3'
3
2
 
4
- module HELPEMA
3
+ module Helpema
5
4
  module YouTubeDL
6
- def self.json(url, pwd=nil)
7
- Open3.popen2e("youtube-dl -j '#{url}'") do |i, o|
8
- i.puts pwd if pwd
9
- i.close
10
- o.each do |line|
11
- begin
12
- yield JSON.parse line
13
- rescue JSON::ParserError
14
- yield line
15
- end
5
+ class << self
6
+ attr_accessor :version
7
+ YouTubeDL.version = '^202\d\.[01]\d\.[0123]\d$'
8
+
9
+ def json(url, &blk) = _json(url:url, &blk)
10
+ define_command(:_json,
11
+ cmd: 'youtube-dl', version: YouTubeDL.version,
12
+ usage: {j:true, arg0:nil}, synonyms: {url: :arg0},
13
+ err: '/dev/null'
14
+ ) do |pipe, options, blk|
15
+ pipe.each do |data|
16
+ begin
17
+ data = JSON.parse data
18
+ ensure
19
+ blk.call data
16
20
  end
17
21
  end
18
22
  end
19
23
  end
20
24
  end
25
+ end
@@ -1,38 +1,42 @@
1
- require 'timeout'
2
1
  require 'tmpdir'
3
2
 
4
- module HELPEMA
3
+ module Helpema
5
4
  module ZBar
6
- def self.cam(timeout=3)
7
- raw = ''
8
- IO.popen('zbarcam --nodisplay --raw --prescale=800x800', 'r') do |io|
9
- begin
10
- Timeout.timeout(timeout) do
11
- raw << io.gets
12
- while q = io.gets
13
- break if q=="\n"
14
- raw << q
15
- end
16
- raw.strip!
17
- end
18
- rescue Timeout::Error
19
- raw = nil
20
- $stderr.puts $!
21
- ensure
22
- Process.kill('INT', io.pid)
23
- end
5
+ class << self
6
+ attr_accessor :version, :screenshot
7
+ ZBar.version = '^0\.2[345]\b' # version as of this writing is 0.23
8
+ ZBar.screenshot = ['gnome-screenshot', '-f']
9
+
10
+ def cam() = _cam()
11
+ define_command(:_cam,
12
+ cmd: 'zbarcam', version: ZBar.version,
13
+ usage: {raw:true,quiet:true,nodisplay:true, :prescale= => '800x800'},
14
+ synonyms: {prescale: :prescale=}
15
+ ) do |pipe, kw, blk|
16
+ begin
17
+ pipe.gets.chomp # This is the return value!
18
+ ensure
19
+ Process.kill('TERM', pipe.pid)
24
20
  end
25
- raw
26
21
  end
27
22
 
28
- def self.screen
23
+ def img(filename) = _img(filename:filename)
24
+ define_command(:_img, cmd: 'zbarimg', version: ZBar.version,
25
+ usage: {q:true,raw:true,arg0:nil}, synonyms: {filename: :arg0})
26
+
27
+ def screen
29
28
  raw = nil
30
29
  Dir.mktmpdir do |tmpdir|
31
- screenshot = File.join(tmpdir, "#{$$}.#{Time.now.to_f}.png")
32
- system "gnome-screenshot -f #{screenshot}"
33
- raw = `zbarimg -q --raw #{screenshot}`.strip
30
+ _ = File.join(tmpdir, "#{$$}.#{Time.now.to_f}.png")
31
+ raw = _ if snapshot(_) and not (_=ZBar.img(_).chomp).empty?
34
32
  end
35
33
  raw
36
34
  end
35
+
36
+ private
37
+ def snapshot(filename)
38
+ system(*ZBar.screenshot, filename)
39
+ end
40
+ end
37
41
  end
38
42
  end
metadata CHANGED
@@ -1,21 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helpema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.200129
4
+ version: 3.0.210108
5
5
  platform: ruby
6
6
  authors:
7
7
  - carlosjhr64
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-29 00:00:00.000000000 Z
11
+ date: 2021-01-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Meant to be an eclectic collection of useful single functions and wrappers.
15
- Wrappers: ssss-split, ssss-combine, zbarcam, youtube-dl -j.
16
- Funtions: requires("gemname version",...).
17
15
 
18
- More later.
16
+ Featured method: `requires "good ~>3.0", "bad ~>2.7", "ugly ~>1.8"`
19
17
  email: carlosjhr64@gmail.com
20
18
  executables: []
21
19
  extensions: []
@@ -31,7 +29,7 @@ homepage: https://github.com/carlosjhr64/helpema
31
29
  licenses:
32
30
  - MIT
33
31
  metadata: {}
34
- post_install_message:
32
+ post_install_message:
35
33
  rdoc_options: []
36
34
  require_paths:
37
35
  - lib
@@ -46,15 +44,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
44
  - !ruby/object:Gem::Version
47
45
  version: '0'
48
46
  requirements:
49
- - 'ruby: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]'
47
+ - 'ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]'
48
+ - 'youtube-dl: 2020.12.31'
50
49
  - 'ssss-split: 0.5'
51
50
  - 'ssss-combine: 0.5'
52
51
  - 'zbarcam: 0.23'
53
- - 'gnome-screenshot: gnome-screenshot 3.34.0'
54
- rubygems_version: 3.1.2
55
- signing_key:
52
+ - 'zbarimg: 0.23'
53
+ - 'gnome-screenshot: gnome-screenshot 3.38.0'
54
+ rubygems_version: 3.2.3
55
+ signing_key:
56
56
  specification_version: 4
57
- summary: 'Meant to be an eclectic collection of useful single functions and wrappers.
58
- Wrappers: ssss-split, ssss-combine, zbarcam, youtube-dl -j. Funtions: requires("gemname
59
- version",...).'
57
+ summary: Meant to be an eclectic collection of useful single functions and wrappers.
60
58
  test_files: []