ruby-magic 0.0.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEIDCCAoigAwIBAgIBATANBgkqhkiG9w0BAQsFADAdMRswGQYDVQQDDBJrdy9E
3
+ Qz1saW51eC9EQz1jb20wHhcNMjEwMTAxMDAwOTU0WhcNMjYwMTAxMDAwOTU0WjAd
4
+ MRswGQYDVQQDDBJrdy9EQz1saW51eC9EQz1jb20wggGiMA0GCSqGSIb3DQEBAQUA
5
+ A4IBjwAwggGKAoIBgQCv02+pOrAMVUipLY7qjNLx8OMfMjnVCBH21SsWn8gyIHE9
6
+ BtQ+tlsJU8nWNjlr++7SKIV5vQmyYsC2UY0cdcFASdoV0A3Lc2Z0makfvPgcIlpL
7
+ 5THqQtP11tJfhjQ3MSFJYRG2D6m6GNqA+WTTsvJbPIxy5PHv5xb+79LLzcVxB9Op
8
+ C0ervTOF8W6Ye4EDvhQ5tppOr/d26UQV+8zRuxjxx5uZSPfUrE9u/gIIdeROoB3T
9
+ cnjAfzvjIzampLQppvhUnT4Vf5Rum2LohZ5GosKLy7tM65vr1eLyI2ndvS1MXWrU
10
+ 05sFKm5CSqORD+TVkR1jd3z5HdPgO1KwadccxFixoI8kEIt/iixWTzbka2T2iJoq
11
+ 5DeNMEMYYAZKuAGBzBcgvgPccCeAXFVF9AHjfaGRUMmYNHKK2HD6AdgNdhcE18V+
12
+ CJxFtggCJFYc1ygFOsHZPTbKo9+3ybSQhAswtDosWt26utUT1JOc2Bkj1sgQ0Yrk
13
+ orANsdYz2ee3JFkzx08CAwEAAaNrMGkwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAw
14
+ HQYDVR0OBBYEFME41mMBb/XAHgjhlX4kE8xnXxewMBcGA1UdEQQQMA6BDGt3QGxp
15
+ bnV4LmNvbTAXBgNVHRIEEDAOgQxrd0BsaW51eC5jb20wDQYJKoZIhvcNAQELBQAD
16
+ ggGBAF/2ryrSgTpf0+wYG46Oi6V3p9A3uMxxbGsh0ZmlLQT1Z1ozclR+xzxzyIFC
17
+ i7YWYyR085brDBnYNXgnUT9Nm4iAYx58MaPD28IFrcX0XA3DJE6Wkx4MmMV0chiJ
18
+ kf5ASikfGtFtf9qDmpLQB4louB6HpKNq1MwsQo8zwY93t77HCiV5f0g43rcZfGTn
19
+ wfBhpT7oOVWgfTLeBD811aS52VUCWclmnz+f01GdTeMwqsgc6oJaPSvg4JCMN94O
20
+ wcBlxus69rhM8PeTJNhwLxzRCNnZ69t9k2cU+4lTmQQIB8gnEFJxY9gZdgQ6pej0
21
+ HXUIut+6pp5BRYF2mTZ3fnxhWyIZaeB7lla2gI6rV2GXDbp4kVplGEuQ975YklJw
22
+ i4y7bz2Es4Hkly2Nc/J2L0+uMoBhMWMY3Xl0XDdkZYtBdnSB5g7NacChaAsukEkd
23
+ Ozt6vfXxSzW/KfodB+0ELjlHWiFbI8w7c8Kt7/ceZGVsXTxD/f4tVWy8MmxFy3pY
24
+ Cpe8Kg==
25
+ -----END CERTIFICATE-----
data/lib/magic.rb CHANGED
@@ -1,31 +1,9 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- # :stopdoc:
4
-
5
- #
6
- # magic.rb
7
- #
8
- # Copyright 2013-2014 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- require 'magic/magic'
24
- require 'magic/version'
25
- require 'magic/core/file'
26
- require 'magic/core/string'
27
-
28
- # :startdoc:
3
+ require_relative 'magic/magic'
4
+ require_relative 'magic/version'
5
+ require_relative 'magic/core/file'
6
+ require_relative 'magic/core/string'
29
7
 
30
8
  #
31
9
  # File _Magic_ in Ruby.
@@ -37,12 +15,12 @@ class Magic
37
15
  # call-seq:
38
16
  # magic.inspect -> string
39
17
  #
40
- # Returns
41
- #
42
18
  # Example:
43
19
  #
44
- # magic = Magic.new #=> #<Magic:0x007fd5258a1108>
45
- # magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @path=[\"/etc/magic\", \"/usr/share/misc/magic\"]>"
20
+ # magic = Magic.new
21
+ # magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"]>"
22
+ # magic.close
23
+ # magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"] (closed)>"
46
24
  #
47
25
  def inspect
48
26
  super.insert(-2, self.closed? ? ' (closed)' : '')
@@ -50,25 +28,17 @@ class Magic
50
28
 
51
29
  #
52
30
  # call-seq:
53
- # magic.flags_to_a( names ) -> array
54
- #
55
- # Returns an +array+
56
- #
57
- # Example:
58
- #
59
- # Will raise <i>Magic::LibraryError</i> exception if, or
31
+ # magic.flags_list( boolean ) -> array
60
32
  #
61
- # See also: Magic#flags
33
+ # See also: Magic#flags and Magic#flags_names
62
34
  #
63
- def flags_to_a(names = false)
64
- raise LibraryError, "Magic library is not open" if closed?
35
+ def flags_list(names = false)
36
+ raise LibraryError, 'Magic library is not open' if closed?
37
+ return [names ? 'NONE' : 0] if @flags.zero?
65
38
 
66
39
  n, i = 0, @flags
67
-
68
40
  flags = []
69
41
 
70
- return [names ? 'NONE' : 0] if @flags.zero?
71
-
72
42
  @@flags_map ||= flags_as_map if names
73
43
 
74
44
  while i > 0
@@ -80,15 +50,22 @@ class Magic
80
50
  flags
81
51
  end
82
52
 
53
+ alias_method :flags_to_a, :flags_list
54
+
55
+ # call-seq:
56
+ # magic.flags_names -> array
57
+ #
58
+ # See also: Magic#flags and Magic#flags_list
59
+ #
60
+ def flags_names
61
+ flags_list(true)
62
+ end
63
+
83
64
  class << self
84
65
  #
85
66
  # call-seq:
86
- # Magic.open( flags ) -> self
87
- # Magic.open( flags ) {|magic| block } -> string or array
88
- #
89
- # Returns
90
- #
91
- # Example:
67
+ # Magic.open( integer ) -> self
68
+ # Magic.open( integer ) {|magic| block } -> string or array
92
69
  #
93
70
  # See also: Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check
94
71
  #
@@ -112,10 +89,6 @@ class Magic
112
89
  # Magic.mime -> self
113
90
  # Magic.mime {|magic| block } -> string or array
114
91
  #
115
- # Returns
116
- #
117
- # Example:
118
- #
119
92
  # See also: Magic::open, Magic::type, Magic::encoding, Magic::compile and Magic::check
120
93
  #
121
94
  def mime(&block)
@@ -127,10 +100,6 @@ class Magic
127
100
  # Magic.type -> self
128
101
  # Magic.type {|magic| block } -> string or array
129
102
  #
130
- # Returns
131
- #
132
- # Example:
133
- #
134
103
  # See also: Magic::open, Magic::mime, Magic::encoding, Magic::compile and Magic::check
135
104
  #
136
105
  def type(&block)
@@ -142,10 +111,6 @@ class Magic
142
111
  # Magic.encoding -> self
143
112
  # Magic.encoding {|magic| block } -> string or array
144
113
  #
145
- # Returns
146
- #
147
- # Example:
148
- #
149
114
  # See also: Magic::open, Magic::mime, Magic::type, Magic::compile and Magic::check
150
115
  #
151
116
  def encoding(&block)
@@ -154,51 +119,85 @@ class Magic
154
119
 
155
120
  #
156
121
  # call-seq:
157
- # Magic.compile( path, ... ) -> true
158
- # Magic.compile( array ) -> true
122
+ # Magic.compile( string, ... ) -> true
123
+ # Magic.compile( array ) -> true
124
+ #
125
+ # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check
159
126
  #
160
- # Returns
127
+ def compile(*paths)
128
+ open {|m| m.compile(paths) }
129
+ end
130
+
161
131
  #
162
- # Example:
132
+ # call-seq:
133
+ # Magic.check( string, ... ) -> true or false
134
+ # Magic.check( array ) -> true or false
163
135
  #
164
- # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check
136
+ # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile
165
137
  #
166
- def compile(path)
167
- open {|m| m.compile(path) }
138
+ def check(*paths)
139
+ open {|m| m.check(paths) }
168
140
  end
169
141
 
170
142
  #
171
143
  # call-seq:
172
- # Magic.check( path, ... ) -> true or false
173
- # Magic.check( array ) -> true or false
144
+ # Magic.file( object ) -> string or array
145
+ # Magic.file( string ) -> string or array
146
+ # Magic.file( string, integer ) -> string or array
174
147
  #
175
- # Returns
148
+ # See also:
176
149
  #
177
- # Example:
150
+ def file(path, flags = Magic::MIME)
151
+ open(flags).file(path)
152
+ end
153
+
178
154
  #
179
- # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile
155
+ # call-seq:
156
+ # Magic.buffer( string ) -> string or array
157
+ # Magic.buffer( string, integer ) -> string or array
180
158
  #
181
- def check(path)
182
- open {|m| m.check(path) }
159
+ # See also:
160
+ #
161
+ def buffer(buffer, flags = Magic::MIME)
162
+ open(flags).buffer(buffer)
163
+ end
164
+
165
+ #
166
+ # call-seq:
167
+ # Magic.descriptor( object ) -> string or array
168
+ # Magic.descriptor( integer ) -> string or array
169
+ # Magic.descriptor( integer, integer ) -> string or array
170
+ #
171
+ # See also:
172
+ #
173
+ def descriptor(fd, flags = Magic::MIME)
174
+ open(flags).descriptor(fd)
183
175
  end
184
- end
185
176
 
186
- alias_method :flags_array, :flags_to_a
177
+ alias_method :fd, :descriptor
178
+ end
187
179
 
188
180
  private
189
181
 
182
+ def power_of_two?(number)
183
+ number > 0 && Math.log2(number) % 1 == 0
184
+ end
185
+
190
186
  def flags_as_map
191
- self.class.constants.inject({}) do |flags,constant|
192
- value = self.class.const_get(constant)
193
- flags[value] = constant.to_s if value.is_a?(Fixnum)
194
- flags
187
+ klass = self.class
188
+
189
+ klass.constants.each_with_object({}) do |constant, flags|
190
+ constant = constant.to_s
191
+
192
+ next if constant.start_with?('PARAM_')
193
+
194
+ value = klass.const_get(constant)
195
+
196
+ if value.is_a?(Integer) && power_of_two?(value)
197
+ flags[value] = constant
198
+ end
195
199
  end
196
200
  end
197
201
  end
198
202
 
199
- # :enddoc:
200
-
201
203
  FileMagic = Magic
202
-
203
- # vim: set ts=2 sw=2 sts=2 et :
204
- # encoding: utf-8
@@ -1,58 +1,21 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- # :stopdoc:
4
-
5
- #
6
- # core/file.rb
7
- #
8
- # Copyright 2013-2014 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- # :startdoc:
24
-
25
- #
26
- #
27
- #
28
3
  class File
29
4
  class << self
30
5
  #
31
6
  # call-seq:
32
- # File.magic( path ) -> string, array or nil
33
- #
34
- # Returns
35
- #
36
- # Example:
7
+ # File.magic( object ) -> string or array
8
+ # File.magic( string ) -> string or array
37
9
  #
38
10
  # See also: File::mime and File::type
39
11
  #
40
12
  def magic(path, flags = Magic::NONE)
41
- path = path.path if path.respond_to?(:path)
42
- path ||= path.to_path if path.respond_to?(:to_path)
43
- path ||= path.to_s
44
-
45
13
  Magic.open(flags) {|mgc| mgc.file(path) }
46
- rescue Magic::Error
47
14
  end
48
15
 
49
16
  #
50
17
  # call-seq:
51
- # File.mime( path ) -> string, array or nil
52
- #
53
- # Returns
54
- #
55
- # Example:
18
+ # File.mime( string ) -> string or array
56
19
  #
57
20
  # See also: File::magic and File::type
58
21
  #
@@ -62,11 +25,7 @@ class File
62
25
 
63
26
  #
64
27
  # call-seq:
65
- # File.type( path ) -> string, array or nil
66
- #
67
- # Returns
68
- #
69
- # Example:
28
+ # File.type( string ) -> string or array
70
29
  #
71
30
  # See also: File::magic and File::mime
72
31
  #
@@ -77,11 +36,7 @@ class File
77
36
 
78
37
  #
79
38
  # call-seq:
80
- # File.magic( path ) -> string, array or nil
81
- #
82
- # Returns
83
- #
84
- # Example:
39
+ # File.magic -> string or array
85
40
  #
86
41
  # See also: File#mime and File#type
87
42
  #
@@ -91,11 +46,7 @@ class File
91
46
 
92
47
  #
93
48
  # call-seq:
94
- # File.mime( path ) -> string, array or nil
95
- #
96
- # Returns
97
- #
98
- # Example:
49
+ # File.mime -> string or array
99
50
  #
100
51
  # See also: File#magic and File#type
101
52
  #
@@ -105,11 +56,7 @@ class File
105
56
 
106
57
  #
107
58
  # call-seq:
108
- # File.type( path ) -> string, array or nil
109
- #
110
- # Returns
111
- #
112
- # Example:
59
+ # File.type -> string or array
113
60
  #
114
61
  # See also: File#magic and File#mime
115
62
  #
@@ -117,8 +64,3 @@ class File
117
64
  self.class.type(self)
118
65
  end
119
66
  end
120
-
121
- # :enddoc:
122
-
123
- # vim: set ts=2 sw=2 sts=2 et :
124
- # encoding: utf-8
@@ -1,53 +1,19 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- # :stopdoc:
4
-
5
- #
6
- # core/string.rb
7
- #
8
- # Copyright 2013-2014 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- # :startdoc:
24
-
25
- #
26
- #
27
- #
28
3
  class String
29
4
  #
30
5
  # call-seq:
31
- # string.magic -> string, array or nil
32
- #
33
- # Returns
34
- #
35
- # Example:
6
+ # string.magic -> string or array
36
7
  #
37
8
  # See also: String#mime and String#type
38
9
  #
39
10
  def magic(flags = Magic::NONE)
40
11
  Magic.open(flags) {|mgc| mgc.buffer(self) }
41
- rescue Magic::Error
42
12
  end
43
13
 
44
14
  #
45
15
  # call-seq:
46
- # string.mime -> string, array or nil
47
- #
48
- # Returns
49
- #
50
- # Example:
16
+ # string.mime -> string or array
51
17
  #
52
18
  # See also: String#magic and String#type
53
19
  #
@@ -57,11 +23,7 @@ class String
57
23
 
58
24
  #
59
25
  # call-seq:
60
- # string.type -> string, array or nil
61
- #
62
- # Returns
63
- #
64
- # Example:
26
+ # string.type -> string or array
65
27
  #
66
28
  # See also: String#magic and String#mime
67
29
  #
@@ -69,8 +31,3 @@ class String
69
31
  magic(Magic::MIME_TYPE)
70
32
  end
71
33
  end
72
-
73
- # :enddoc:
74
-
75
- # vim: set ts=2 sw=2 sts=2 et :
76
- # encoding: utf-8