kaki-utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa8adc8d24d05a18c36681dafbc80dff35eed800
4
+ data.tar.gz: 0fb3a03864ed75010bea5573724fdb55334fb61b
5
+ SHA512:
6
+ metadata.gz: 2c41cac77aa4e932d292dac9586f54d252c629fd74bdb602b7169eb6cfbd381b0dca13b98cc139b7c16f420647c07a52d871fbcf3b780eac7b3c4d6dda3766bd
7
+ data.tar.gz: b9e9ae7aa96f02ce9569b3c3ac97e81634d1feb114636e631d6b1cb051bbf90fef5f51397d438d4996e65d982ff3c2b0473e82c12e6c5d2deef1ca20e57a4076
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ ## modele Utils
2
+
3
+ ```ruby
4
+ require "kaki/utils"
5
+ ```
6
+
7
+ methods
8
+
9
+ * Utils.imgexist?(url)
10
+ * Utils.getfile(url, filename, max=0)
11
+ * Utils.key_wait
12
+ * Utils.progress_bar
13
+ * Utils.delete\_non\_img(file_name)
14
+ * Utils.delete\_empty\_folder(folder_name = './')
15
+ * Utils.bell    (only Ubuntu)
16
+
17
+ ## Recurring decimal
18
+
19
+ ```ruby
20
+ require "kaki/utils/rec_decimal"
21
+ ```
22
+
23
+ methods
24
+
25
+ * Rational#to\_rec_decimal
26
+ * String#to_r
27
+
28
+ ## image suffix
29
+
30
+ ```ruby
31
+ require "kaki/utils/imgsuffix"
32
+ ```
33
+
34
+ method
35
+
36
+ * String#imgsuffix
37
+
38
+ ## n times retry
39
+
40
+ ```ruby
41
+ require "kaki/utils/retry"
42
+ ```
43
+ method
44
+
45
+ * Integer#times_retry(message: true, wait: 0)
46
+
@@ -0,0 +1,8 @@
1
+ class String
2
+ def imgsuffix
3
+ [".jpg", ".gif", ".png", ".jpeg", ".bmp", ".JPG", ".GIF", ".PNG", ".JPEG", ".BMP"].each do |sf|
4
+ return sf if include?(sf)
5
+ end
6
+ ""
7
+ end
8
+ end
@@ -0,0 +1,41 @@
1
+ class Rational
2
+ #分数を循環小数に直す
3
+ def to_rec_decimal
4
+ f, num = (self < 0) ? ["-", -self] : ["", self]
5
+
6
+ i = num.to_i
7
+ result = f + i.to_s
8
+ ra = num - i
9
+ return result if ra.zero?
10
+ result += "."
11
+
12
+ remainder = ra.numerator
13
+ deno = ra.denominator
14
+ place = []
15
+ rems = []
16
+ begin
17
+ rems << remainder
18
+ place << remainder * 10 / deno
19
+ remainder = remainder * 10 % deno
20
+ return result + place.join if remainder.zero?
21
+ end while not (idx = rems.find_index(remainder))
22
+ place.insert(idx, "(")
23
+ result + place.join + ")"
24
+ end
25
+ end
26
+
27
+ class String
28
+ #循環小数を分数に直す
29
+ alias :__to_r__ :to_r
30
+ def to_r
31
+ st = delete(" ")
32
+ return st.__to_r__ unless (m = /^([^\d]?)(\d+)\.(\d*)\((\d+)\)$/.match(st))
33
+ f = (m[1] == "-") ? -1 : 1
34
+
35
+ result = (m[2] + "." + m[3]).__to_r__
36
+ l = m[4].length
37
+ result += Rational(m[4].to_i, 10 ** l - 1) * Rational(1, 10) ** m[3].length
38
+ result * f
39
+ end
40
+ end
41
+
@@ -0,0 +1,18 @@
1
+ class Integer
2
+ def times_retry(message: true, wait: 0)
3
+ n = 1
4
+ begin
5
+ yield(n)
6
+ rescue => e
7
+ if n <= self
8
+ puts "Error: retry #{n}" if message
9
+ puts e.backtrace if message
10
+ n += 1
11
+ sleep(wait)
12
+ retry
13
+ end
14
+ puts "Error: stop" if message
15
+ raise e
16
+ end
17
+ end
18
+ end
data/lib/kaki/utils.rb ADDED
@@ -0,0 +1,93 @@
1
+ require 'open-uri'
2
+ require 'fileutils'
3
+ require 'fastimage'
4
+ require "io/console"
5
+ require 'gtk2'
6
+
7
+ module Utils
8
+ #画像か?
9
+ def imgexist?(url)
10
+ FastImage.size(url)
11
+ end
12
+
13
+
14
+ #Web上のバイナリファイルの保存
15
+ def getfile(url, filename, max=0)
16
+ count = 0
17
+ begin
18
+ open(filename, 'wb') do |file|
19
+ open(url) {|data| file.write(data.read)}
20
+ end
21
+ true
22
+ rescue
23
+ puts "ファイル入出力エラー: " + $!.message.encode("UTF-8")
24
+ count += 1
25
+ return false if count > max #max回までリトライする
26
+ puts "リトライ: #{count}"
27
+ sleep(1)
28
+ retry
29
+ end
30
+ end
31
+ module_function :imgexist?, :getfile
32
+
33
+
34
+ #一文字入力
35
+ def key_wait
36
+ c = nil
37
+ loop {break if (c = STDIN.getch)}
38
+ STDIN.cooked!
39
+ c
40
+ end
41
+ module_function :key_wait
42
+
43
+
44
+ #プログレスバーの表示
45
+ def progress_bar
46
+ w = Gtk::Window.new
47
+ w.signal_connect("destroy") {Gtk.main_quit}
48
+ w.set_size_request(300, 50)
49
+ w.border_width = 10
50
+ w.title = "Progress"
51
+
52
+ bar = Gtk::ProgressBar.new
53
+ w.add(bar)
54
+
55
+ Thread.new(bar) do |bar|
56
+ yield(bar)
57
+ end
58
+
59
+ w.show_all
60
+ Gtk.main
61
+ end
62
+ module_function :progress_bar
63
+
64
+
65
+ #画像ファイルでなければ削除
66
+ def delete_non_img(fname)
67
+ if Utils.imgexist?(fname)
68
+ false
69
+ else
70
+ FileUtils.rm(fname)
71
+ true
72
+ end
73
+ end
74
+ module_function :delete_non_img
75
+
76
+
77
+ #指定したディレクトリの空フォルダを削除
78
+ def delete_empty_folder(fname = './')
79
+ Dir.chdir(fname)
80
+ Dir.glob("*").each do |name|
81
+ next unless File.directory?(name)
82
+ Dir.rmdir(p name) if Dir.glob("./#{name}/*").empty? rescue next
83
+ end
84
+ end
85
+ module_function :delete_empty_folder
86
+
87
+
88
+ #ベルを鳴らす(Ubuntuのみ)
89
+ def bell
90
+ `paplay /usr/share/sounds/freedesktop/stereo/complete.oga`
91
+ end
92
+ module_function :bell
93
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kaki-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - obelisk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: My trivial utilities.
14
+ email: obelisk_1968@mail.goo.ne.jp
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/kaki/utils.rb
21
+ - lib/kaki/utils/imgsuffix.rb
22
+ - lib/kaki/utils/rec_decimal.rb
23
+ - lib/kaki/utils/retry.rb
24
+ homepage:
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.5.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: My trivial utilities.
48
+ test_files: []