linux_stat 1.0.1 → 1.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.
@@ -14,13 +14,13 @@ module LinuxStat
14
14
  #
15
15
  # It can have information like:
16
16
  #
17
- # id, vendor id, product id, manufacturer, serial, bus number, dev number,
17
+ # path, id, vendor id, product id, manufacturer, serial, bus number, dev number,
18
18
  # b_max_power, b_max_packet_size, etc.
19
19
  #
20
20
  # An example of the returned sample from a test machine is:
21
21
  # LinuxStat::USB.devices_stat
22
22
  #
23
- # [{:path=>"/sys/bus/usb/devices/1-1.2/", :id=>"04d9:1203", :vendor_id=>"04d9", :product_id=>"1203", :bus_num=>1, :dev_num=>4, :hwdata=>{:vendor=>"Holtek Semiconductor, Inc.", :product=>"Keyboard"}, :authorized=>true, :b_max_power=>"100mA", :b_max_packet_size0=>8}
23
+ # => [{:path=>"/sys/bus/usb/devices/1-1.2/", :id=>"04d9:1203", :vendor_id=>"04d9", :product_id=>"1203", :bus_num=>1, :dev_num=>4, :hwdata=>{:vendor=>"Holtek Semiconductor, Inc.", :product=>"Keyboard"}, :authorized=>true, :b_max_power=>"100mA", :b_max_packet_size0=>8}
24
24
  #
25
25
  # Right, it's an array of Hashes.
26
26
  #
@@ -114,6 +114,8 @@ module LinuxStat
114
114
 
115
115
  ret.merge!(b_max_power: b_max_power) unless b_max_power.empty?
116
116
  ret.merge!(b_max_packet_size0: b_max_packet_size0) if b_max_packet_size0
117
+
118
+ ret
117
119
  }.tap(&:compact!)
118
120
  end
119
121
 
@@ -141,11 +143,63 @@ module LinuxStat
141
143
  }
142
144
  end
143
145
 
146
+ ##
147
+ # hwdata_file = file
148
+ #
149
+ # Lets you set the hwdata_file about usb.ids.
150
+ #
151
+ # The hwdata file about usb.ids contains vendor name and product name information about
152
+ # devices. This is then mapped by the other methods that utilizes hwdata/usb.ids.
153
+ #
154
+ # Do note that this method is intended to run only once, at the beginning.
155
+ # If you use any other method that utilizes hwdata/usb.ids, before
156
+ # calling this method, this method will not work.
157
+ def hwdata_file=(file)
158
+ @@hwdata_file ||= file.freeze
159
+ end
160
+
161
+ ##
162
+ # Checks if hwdata_file is already initialized or not.
163
+ # Once it's initialized, calling hwdata_file = 'something/usb.ids' is futile.
164
+ def hwdata_file_set?
165
+ @@hwdata_file ||= nil
166
+ !!@@hwdata_file
167
+ end
168
+
169
+ ##
170
+ # Returns the hwdata_file as string.
171
+ #
172
+ # If hwdata_file isn't set, it will return an empty frozen string.
173
+ #
174
+ # Once it's set, it can't be changed.
175
+ def hwdata_file
176
+ @@hwdata_file ||= nil
177
+ @@hwdata_file ? @@hwdata_file : ''.freeze
178
+ end
179
+
180
+ ##
181
+ # Initializes hwdata
182
+ #
183
+ # hwdata can take upto 0.1 to 0.2 seconds to get initialized.
184
+ #
185
+ # Calling this method will load hwdata for future use.
186
+ #
187
+ # Once it's initialized, hwdata_file can't be changed.
188
+ #
189
+ # If this method initializes hwdata, it will return true
190
+ # Othewise this method will return false.
191
+ def initialize_hwdata
192
+ @@hwdata ||= nil
193
+ init = !@@hwdata
194
+ hwdata
195
+ init
196
+ end
197
+
144
198
  alias count_devices count
145
199
 
146
200
  private
147
201
  def hwdata
148
- @@hwdata_file ||= "/usr/share/hwdata/usb.ids"
202
+ @@hwdata_file ||= "/usr/share/hwdata/usb.ids".freeze
149
203
 
150
204
  @@hwdata ||= if File.readable?(@@hwdata_file)
151
205
  file_data = IO.readlines(@@hwdata_file, encoding: 'ASCII-8BIT')
@@ -158,17 +212,19 @@ module LinuxStat
158
212
  x = file_data[i]
159
213
 
160
214
  _lstripped = x.lstrip
161
- next if _lstripped == ?#.freeze || _lstripped.empty?
215
+ next if _lstripped[0] == ?#.freeze || _lstripped.empty?
216
+
217
+ if x[0] == ?\t.freeze
218
+ next unless vendor_id
162
219
 
163
- if x.start_with?(?\t.freeze)
164
- data = x.tap(&:strip!)
165
- device_id = data[/\A.*?\s/].to_s.strip
166
- device = data[device_id.length..-1].to_s.strip
220
+ x.strip!
221
+ device_id = x[/\A.*?\s/].to_s.strip
222
+ device = x[device_id.length..-1].to_s.strip
167
223
  ret[vendor_id][1][device_id] = device
168
224
  else
169
- data = x
170
- vendor_id = data[/\A.*?\s/].to_s.strip
171
- vendor = data[vendor_id.length..-1].to_s.strip
225
+ x.strip!
226
+ vendor_id = x[/\A.*?\s/].to_s.strip
227
+ vendor = x[vendor_id.length..-1].to_s.strip
172
228
  ret[vendor_id] = [vendor, {}]
173
229
  end
174
230
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.0.1"
2
+ VERSION ||= "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-24 00:00:00.000000000 Z
11
+ date: 2020-12-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -18,9 +18,9 @@ executables:
18
18
  - linuxstat.rb
19
19
  extensions:
20
20
  - ext/fs_stat/extconf.rb
21
+ - ext/nproc/extconf.rb
21
22
  - ext/sysconf/extconf.rb
22
23
  - ext/utsname/extconf.rb
23
- - ext/nproc/extconf.rb
24
24
  extra_rdoc_files:
25
25
  - README.md
26
26
  files:
@@ -47,6 +47,7 @@ files:
47
47
  - lib/linux_stat/mounts.rb
48
48
  - lib/linux_stat/net.rb
49
49
  - lib/linux_stat/os.rb
50
+ - lib/linux_stat/pci.rb
50
51
  - lib/linux_stat/prettify_bytes.rb
51
52
  - lib/linux_stat/process.rb
52
53
  - lib/linux_stat/process_info.rb