helpema 0.2.0 → 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
- SHA1:
3
- metadata.gz: 97aef7e26445e9b17fb514f34fba1d286dd6cda7
4
- data.tar.gz: c8149b74cb8d35ce7e0a42dfc6236c4a210f35a8
2
+ SHA256:
3
+ metadata.gz: 0f73de852bf1c5e430ed5b5a0887e0b16ad37f3f4156843b40738065400b40e9
4
+ data.tar.gz: 111bed98f5e6f8c7cba8c1f2353ea8a1d94e24ac84cda4351380d54283bc779a
5
5
  SHA512:
6
- metadata.gz: 9d37cc78e671535927fdfe18c36374409729a6b8490a85e5b91394f566755dbde70f459db38452bff697e6f36d42dab5f62c008f3dc0ff6e22f42f1d38a8e08e
7
- data.tar.gz: ecbc2b1ff09517c00d7fe9a430345c9c6331b62f8bdfd17259d81e6a3017c7428ff89f8cb9f53c68e918b56c71f6a410fe691d36bd744661bcbce7417aa31387
6
+ metadata.gz: 6b39435438e44edef870ed22e4b0a5acb6f2c2439cbe99906606fa62d6b224cbb1ce2fcc453f91c3b64bee6b70367d97605343c4a892db1e89eea0fac2a7fb39
7
+ data.tar.gz: 567e0d4eb2be6863b3f6fb9d183b77b3c549755dee855a629e282f544514dcf51b00cbc4ba9a4a5fdedb67e113ef963efd6a406a06c978182ea03f2cbaa7ee91
@@ -0,0 +1,132 @@
1
+ # Helpema
2
+
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
+
7
+ ## DESCRIPTION:
8
+
9
+ Meant to be an eclectic collection of useful single functions and wrappers.
10
+
11
+ Featured method: `requires "good ~>3.0", "bad ~>2.7", "ugly ~>1.8"`
12
+
13
+ ## INSTALL:
14
+
15
+ ```shell
16
+ $ gem install helpema
17
+ ```
18
+
19
+ ## SYNOPSIS:
20
+
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.
108
+
109
+ ## LICENSE:
110
+
111
+ (The MIT License)
112
+
113
+ Copyright (c) 2021 carlosjhr64
114
+
115
+ Permission is hereby granted, free of charge, to any person obtaining
116
+ a copy of this software and associated documentation files (the
117
+ 'Software'), to deal in the Software without restriction, including
118
+ without limitation the rights to use, copy, modify, merge, publish,
119
+ distribute, sublicense, and/or sell copies of the Software, and to
120
+ permit persons to whom the Software is furnished to do so, subject to
121
+ the following conditions:
122
+
123
+ The above copyright notice and this permission notice shall be
124
+ included in all copies or substantial portions of the Software.
125
+
126
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
127
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
128
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
129
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
130
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
131
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
132
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,16 +1,18 @@
1
- # Standard Libraries
2
- require 'open3'
3
- require 'timeout'
4
- require 'json'
1
+ module Helpema
2
+ VERSION = '3.0.210108'
5
3
 
6
- # This Gem
7
- require 'helpema/version.rb'
8
- require 'helpema/ssss.rb'
9
- require 'helpema/zbar.rb'
10
- require 'helpema/youtubedl.rb'
4
+ require_relative 'helpema/helpema'
5
+
6
+ autoload :SSSS, 'helpema/ssss.rb'
7
+ autoload :YouTubeDL, 'helpema/youtubedl.rb'
8
+ autoload :ZBar, 'helpema/zbar.rb'
9
+ end
11
10
 
12
11
  # Requires:
13
12
  #`ruby`
13
+ #`youtube-dl`
14
14
  #`ssss-split`
15
15
  #`ssss-combine`
16
16
  #`zbarcam`
17
+ #`zbarimg`
18
+ #`gnome-screenshot`
@@ -0,0 +1,112 @@
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
+
87
+ def requires(*list)
88
+ loaded = []
89
+ list.each do |gems|
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
105
+ require gemname and loaded.push gemname
106
+ end
107
+ end
108
+ return loaded
109
+ end
110
+
111
+ extend self
112
+ end
@@ -1,25 +1,36 @@
1
1
  module Helpema
2
2
  module SSSS
3
+ class << self
4
+ attr_accessor :version
5
+ SSSS.version = 'Version: 0\.[567]\b' # version as of this writing is 0.5
3
6
 
4
- def self.split(pwd, t, n)
5
- pwds = nil
6
- Open3.popen3("ssss-split -Q -t #{t} -n #{n}") do |stdin, stdout, stderr|
7
- stdin.puts pwd
8
- stdin.close
9
- pwds = stdout.read.strip.split
10
- end
11
- return pwds
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])
12
18
  end
13
19
 
14
- def self.combine(*pwds)
15
- pwd = ''
16
- Open3.popen3("ssss-combine -Q -t #{pwds.length}") do |stdin, stdout, stderr|
17
- pwds.each{|p| stdin.puts p}
18
- stdin.close
19
- pwd = stderr.read.strip.split.last
20
- end
21
- 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)
22
23
  end
23
-
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
24
35
  end
25
36
  end
@@ -1,19 +1,25 @@
1
+ require 'json'
2
+
1
3
  module Helpema
2
4
  module YouTubeDL
5
+ class << self
6
+ attr_accessor :version
7
+ YouTubeDL.version = '^202\d\.[01]\d\.[0123]\d$'
3
8
 
4
- def self.json(url, pwd=nil)
5
- Open3.popen2e("youtube-dl -j '#{url}'") do |i, o|
6
- i.puts pwd if pwd
7
- i.close
8
- o.each do |line|
9
- begin
10
- yield JSON.parse line
11
- rescue JSON::ParserError
12
- yield line
13
- end
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
14
20
  end
15
21
  end
16
22
  end
17
-
23
+ end
18
24
  end
19
25
  end
@@ -1,27 +1,42 @@
1
+ require 'tmpdir'
2
+
1
3
  module Helpema
2
4
  module ZBar
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']
3
9
 
4
- def self.qrcode(timeout=3)
5
- qrcode = ''
6
- IO.popen('zbarcam --nodisplay --raw --prescale=800x800', 'r') do |io|
7
- begin
8
- Timeout.timeout(timeout) do
9
- qrcode << io.gets
10
- while q = io.gets
11
- break if q=="\n"
12
- qrcode << q
13
- end
14
- qrcode.strip!
15
- end
16
- rescue Timeout::Error
17
- qrcode = nil
18
- $stderr.puts $!
19
- ensure
20
- Process.kill('INT', io.pid)
21
- end
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)
22
20
  end
23
- qrcode
24
21
  end
25
22
 
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
28
+ raw = nil
29
+ Dir.mktmpdir do |tmpdir|
30
+ _ = File.join(tmpdir, "#{$$}.#{Time.now.to_f}.png")
31
+ raw = _ if snapshot(_) and not (_=ZBar.img(_).chomp).empty?
32
+ end
33
+ raw
34
+ end
35
+
36
+ private
37
+ def snapshot(filename)
38
+ system(*ZBar.screenshot, filename)
39
+ end
40
+ end
26
41
  end
27
42
  end
metadata CHANGED
@@ -1,39 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helpema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
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: 2017-06-14 00:00:00.000000000 Z
11
+ date: 2021-01-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
- Some wrappers on the following linux commands: ssss-split, ssss-combine, zbarcam, youtube-dl -j.
14
+ Meant to be an eclectic collection of useful single functions and wrappers.
15
15
 
16
- More later.
16
+ Featured method: `requires "good ~>3.0", "bad ~>2.7", "ugly ~>1.8"`
17
17
  email: carlosjhr64@gmail.com
18
18
  executables: []
19
19
  extensions: []
20
- extra_rdoc_files:
21
- - README.rdoc
20
+ extra_rdoc_files: []
22
21
  files:
23
- - README.rdoc
22
+ - README.md
24
23
  - lib/helpema.rb
24
+ - lib/helpema/helpema.rb
25
25
  - lib/helpema/ssss.rb
26
- - lib/helpema/version.rb
27
26
  - lib/helpema/youtubedl.rb
28
27
  - lib/helpema/zbar.rb
29
28
  homepage: https://github.com/carlosjhr64/helpema
30
29
  licenses:
31
30
  - MIT
32
31
  metadata: {}
33
- post_install_message:
34
- rdoc_options:
35
- - "--main"
36
- - README.rdoc
32
+ post_install_message:
33
+ rdoc_options: []
37
34
  require_paths:
38
35
  - lib
39
36
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -47,14 +44,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
44
  - !ruby/object:Gem::Version
48
45
  version: '0'
49
46
  requirements:
50
- - 'ruby: ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]'
47
+ - 'ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]'
48
+ - 'youtube-dl: 2020.12.31'
51
49
  - 'ssss-split: 0.5'
52
50
  - 'ssss-combine: 0.5'
53
- - 'zbarcam: 0.10'
54
- rubyforge_project:
55
- rubygems_version: 2.5.2
56
- signing_key:
51
+ - 'zbarcam: 0.23'
52
+ - 'zbarimg: 0.23'
53
+ - 'gnome-screenshot: gnome-screenshot 3.38.0'
54
+ rubygems_version: 3.2.3
55
+ signing_key:
57
56
  specification_version: 4
58
- summary: 'Some wrappers on the following linux commands: ssss-split, ssss-combine,
59
- zbarcam, youtube-dl -j.'
57
+ summary: Meant to be an eclectic collection of useful single functions and wrappers.
60
58
  test_files: []
@@ -1,48 +0,0 @@
1
- = helpema
2
-
3
- {<img src="https://badge.fury.io/rb/helpema.svg" alt="Gem Version" />}[http://badge.fury.io/rb/helpema]
4
-
5
- == DESCRIPTION:
6
-
7
- Some wrappers on the following linux commands: ssss-split, ssss-combine, zbarcam, youtube-dl -j.
8
-
9
- More later.
10
-
11
- == SYNOPSIS:
12
-
13
- === Helpema::SSSS.split( secret, threshold, shares)
14
-
15
- === Helpema::SSSS.combine( *shared_secrets )
16
-
17
- === Helpema::ZBar.qrcode( timeout=3 )
18
-
19
- === Helpema::YouTubeDL.json( url ){|obj| ... }
20
-
21
- == INSTALL:
22
-
23
- $ sudo gem install helpema
24
-
25
- == LICENSE:
26
-
27
- (The MIT License)
28
-
29
- Copyright (c) 2014 carlosjhr64
30
-
31
- Permission is hereby granted, free of charge, to any person obtaining
32
- a copy of this software and associated documentation files (the
33
- 'Software'), to deal in the Software without restriction, including
34
- without limitation the rights to use, copy, modify, merge, publish,
35
- distribute, sublicense, and/or sell copies of the Software, and to
36
- permit persons to whom the Software is furnished to do so, subject to
37
- the following conditions:
38
-
39
- The above copyright notice and this permission notice shall be
40
- included in all copies or substantial portions of the Software.
41
-
42
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,3 +0,0 @@
1
- module Helpema
2
- VERSION = '0.2.0'
3
- end