state_mate 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c226a8faea22d2b1e92ba9ec2b6a7e4bd05567d
4
- data.tar.gz: 5a3aa7df4eaa5174c2da39cda349871f87d45d57
3
+ metadata.gz: 2a4c8ab3b912af529324511e027680f26fef9266
4
+ data.tar.gz: 19900ae7fbd3aca5d28530833af4417b3018f5a6
5
5
  SHA512:
6
- metadata.gz: 9a5cd92203f92b5fecb9aaf63198c16d64936e151bbc5f7b050161121e6a6f7700045b9cb62cbe39deac7ceb1021e3852721829a459ba66c5f6c02409dffcac8
7
- data.tar.gz: 9ba7d11e17927dd298e769d29692ee9257299fdde999b849549c1e7f739672cd9f8235fc3361a0decbeae6aa4f7440d7d49605c2068f9b1984c8f4217d3f3d02
6
+ metadata.gz: 725018a48c59f60532de69717380def4d25a7c1d93a14a3496f539877cf52c2dc30eb4885325f2e87fc373256afbe61f224219c3221d9661d81749f8138b069b
7
+ data.tar.gz: 9ebc8e8dfaf989dfaf951881bfbc608b1d2a35e7451764643fa5f1d8a219f12592c27856961c434299e3f158633e71592021eebba9fcec1a2bc36001e46a63e6
data/bin/console CHANGED
@@ -1,7 +1,31 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'pathname'
4
+ require 'fileutils'
5
+
3
6
  require "bundler/setup"
4
7
  require "state_mate"
8
+ require 'nrser'
9
+ require 'nrser/extras'
10
+
11
+ using NRSER
12
+
13
+ ROOT = NRSER.git_root __FILE__
14
+
15
+ Pathname.glob(ROOT + 'lib' + 'state_mate' + 'adapters' + '*.rb').each do |pn|
16
+ name = "state_mate/adapters/#{ pn.basename '.rb' }"
17
+ begin
18
+ require name
19
+ rescue Exception => e
20
+ puts NRSER.dedent <<-END
21
+ failed to load adapter '#{ name }':
22
+
23
+ #{ e.format }
24
+
25
+ END
26
+ end
27
+
28
+ end
5
29
 
6
30
  # You can add fixtures and/or initialization code here to make experimenting
7
31
  # with your gem easier. You can also use a different console, if you like.
@@ -1,5 +1,5 @@
1
+ require 'cmds'
1
2
  require 'nrser'
2
- require 'nrser/exec'
3
3
 
4
4
  using NRSER
5
5
 
@@ -0,0 +1,336 @@
1
+ require 'cmds'
2
+ require 'nrser'
3
+ require 'pp'
4
+
5
+ using NRSER
6
+
7
+ module StateMate; end
8
+ module StateMate::Adapters; end
9
+
10
+ module StateMate::Adapters::PMSet
11
+ # whitelist of modes we handle mapped to their `pmset` flag
12
+ #
13
+ # there is also a UPS mode, but i don't know what it looks like
14
+ #
15
+ MODES = {
16
+ 'Battery Power' => 'b',
17
+ 'AC Power' => 'c',
18
+ }
19
+
20
+ # a whitelist of settings to parse, since the output of `pmset -g custom`
21
+ # can include lines like
22
+ #
23
+ # Sleep On Power Button 1
24
+ #
25
+ # which makes it hard to parse in general (spaces in key name and between
26
+ # key name and value).
27
+ #
28
+ # from
29
+ #
30
+ # - `man pmset
31
+ # - <https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/pmset.1.html>
32
+ #
33
+ SETTINGS = [
34
+ # display sleep timer; replaces 'dim' argument in 10.4 (value in minutes,
35
+ # or 0 to disable)
36
+ 'displaysleep',
37
+
38
+ # disk spindown timer; replaces 'spindown' argument in 10.4 (value in
39
+ # minutes, or 0 to dis-able)
40
+ 'disksleep',
41
+
42
+ # system sleep timer (value in minutes, or 0 to disable)
43
+ 'sleep',
44
+
45
+ # wake on ethernet magic packet (value = 0/1)
46
+ 'womp',
47
+
48
+ # wake on modem ring (value = 0/1)
49
+ 'ring',
50
+
51
+ # automatic restart on power loss (value = 0/1)
52
+ 'autorestart',
53
+
54
+ # wake the machine when the laptop lid (or clamshell) is opened
55
+ # (value = 0/1)
56
+ 'lidwake',
57
+
58
+ # wake the machine when power source (AC/battery) is changed (value = 0/1)
59
+ 'acwake',
60
+
61
+ # slightly turn down display brightness when switching to this power source
62
+ # (value = 0/1)
63
+ 'lessbright',
64
+
65
+ # display sleep will use an intermediate half-brightness state between full
66
+ # brightness and fully off (value = 0/1)
67
+ 'halfdim',
68
+
69
+ # use Sudden Motion Sensor to park disk heads on sudden changes in G force
70
+ # (value = 0/1)
71
+ 'sms',
72
+
73
+ # change hibernation mode. Please use caution. (value = integer)
74
+ 'hibernatemode',
75
+
76
+ # change hibernation image file location. Image may only be located on the
77
+ # root volume. Please use caution. (value = path)
78
+ 'hibernatefile',
79
+
80
+ # prevent idle system sleep when any tty (e.g. remote login session) is
81
+ # 'active'. A tty is 'inactive' only when its idle time exceeds the system
82
+ # sleep timer. (value = 0/1)
83
+ 'ttyskeepawake',
84
+
85
+ # this setting affects how OS X networking presents shared network services
86
+ # during system sleep. This setting is not used by all platforms; changing
87
+ # its value is unsupported.
88
+ #
89
+ # ...so we won't support it
90
+ # 'networkoversleep',
91
+
92
+ # Destroy File Vault Key when going to standby mode. By default File vault
93
+ # keys are retained even when system goes to standby. If the keys are
94
+ # destroyed, user will be prompted to enter the password while coming out
95
+ # of standby mode.(value: 1 - Destroy, 0 - Retain)
96
+ 'destroyfvkeyonstandby',
97
+
98
+ # Where supported, enabled per default as an implementation of Lot 6 to the
99
+ # European Energy-related Products Directive. After sleeping for
100
+ # <autopoweroffdelay> minutes, the system will write a hibernation image
101
+ # and go into a lower power chipset sleep. Wakeups from this state will take
102
+ # longer than wakeups from regular sleep. The system will not auto power
103
+ # off if any external devices are connected, if the system is on battery
104
+ # power, or if the system is bound to a network and wake for net-work
105
+ # network work access is enabled.
106
+ 'autopoweroff',
107
+
108
+ # delay before entering autopoweroff mode. (Value = integer, in minutes)
109
+ 'autopoweroffdelay',
110
+
111
+ # STANDBY ARGUMENTS
112
+
113
+ # standby causes kernel power management to automatically hibernate a
114
+ # machine after it has slept for a specified time period. This saves power
115
+ # while asleep. This setting defaults to ON for supported hardware.
116
+ # The setting standby will be visible in pmset -g if the feature is
117
+ # supported on this machine.
118
+ #
119
+ # only works if hibernation is turned on to hibernatemode 3 or 25.
120
+ 'standby',
121
+
122
+ # specifies the delay, in seconds, before writing the hibernation image
123
+ # to disk and powering off memory for Standby.
124
+ 'standbydelay',
125
+
126
+ # UNDOCUMENTED ARGUMENTS
127
+
128
+ # the "Power Nap" feature..?
129
+ #
130
+ # http://apple.stackexchange.com/questions/116348/how-can-i-enable-and-or-disable-os-xs-power-nap-feature-from-within-terminal
131
+ #
132
+ 'darkwakes',
133
+
134
+ # unknown
135
+ # 'gpuswitch',
136
+
137
+ # shows up on my imac. not even sure you can set it with pmset, so ignoring
138
+ # for now
139
+ # 'Sleep On Power Button',
140
+ ]
141
+
142
+ # regexp to pick the mode headers out of `pmset -g custom` output
143
+ MODE_RE = /^(#{ MODES.keys.map {|_| Regexp.escape _ }.join('|') })\:$/
144
+
145
+ # regexp to pick the settings and values out of other lines of
146
+ # `pmset -g custom`
147
+ SETTING_RE = /^\s(#{ SETTINGS.map {|_| Regexp.escape _ }.join '|' })\s+(.*)$/
148
+
149
+ # @api util
150
+ # *pure*
151
+ #
152
+ # parse the output of `pmset -g custom`.
153
+ #
154
+ # since keys can apparently have spaces in them
155
+ # (like "Sleep On Power Button") and are seperated by spaces, it uses
156
+ # the {.SETTINGS} whitelist.
157
+ #
158
+ # settings are saved at
159
+ #
160
+ # /Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist
161
+ #
162
+ # which might be a possible site for reading and writing, but seems safer
163
+ # to use `pmset`, and should satify needs for now.
164
+ #
165
+ # @param input [String] output of `pmset -g custom`.
166
+ # on my mbp it looks like
167
+ #
168
+ # Battery Power:
169
+ # lidwake 1
170
+ # autopoweroff 1
171
+ # autopoweroffdelay 14400
172
+ # standbydelay 10800
173
+ # standby 1
174
+ # ttyskeepawake 1
175
+ # hibernatemode 3
176
+ # darkwakes 0
177
+ # gpuswitch 2
178
+ # hibernatefile /var/vm/sleepimage
179
+ # displaysleep 5
180
+ # sleep 5
181
+ # acwake 0
182
+ # halfdim 1
183
+ # lessbright 0
184
+ # disksleep 10
185
+ # AC Power:
186
+ # lidwake 1
187
+ # autopoweroff 0
188
+ # autopoweroffdelay 0
189
+ # standbydelay 0
190
+ # standby 0
191
+ # ttyskeepawake 1
192
+ # hibernatemode 3
193
+ # darkwakes 1
194
+ # gpuswitch 0
195
+ # hibernatefile /var/vm/sleepimage
196
+ # womp 0
197
+ # displaysleep 5
198
+ # networkoversleep 0
199
+ # sleep 10
200
+ # acwake 0
201
+ # halfdim 1
202
+ # disksleep 10
203
+ #
204
+ # @return [Hash<String, Hash<String, String>>] hash of section titles
205
+ # (like "Battery Power") to hashes of string keys to *sting* values
206
+ # (does not turn numeric strings into integers).
207
+ def self.parse input
208
+ sections = {}
209
+ section = {}
210
+
211
+ input.lines.each do |line|
212
+ if m = line.match(MODE_RE)
213
+ section = {}
214
+ sections[m[1]] = section
215
+ else
216
+ if m = line.match(SETTING_RE)
217
+ section[m[1]] = m[2]
218
+ end
219
+ end
220
+ end
221
+ sections
222
+ end
223
+
224
+ # @api adapter
225
+ #
226
+ # reads pm settings.
227
+ #
228
+ # @param key [Array<String>] key path to read:
229
+ # - `[]` gets everything, returning a hash
230
+ # `{<mode> => {<setting> => <value>}}`.
231
+ #
232
+ # `PMSet.read []` looks something like:
233
+ #
234
+ # {"Battery Power"=>
235
+ # {"lidwake"=>"1",
236
+ # "autopoweroff"=>"1",
237
+ # ...},
238
+ # "AC Power"=>
239
+ # {"lidwake"=>"1",
240
+ # "autopoweroff"=>"1",
241
+ # ...}}
242
+ #
243
+ #
244
+ # - `[<mode>]` gets a hash of `{<setting> => <value>}` for that mode.
245
+ #
246
+ # `PMSet.read ["AC Power"]` looks something like:
247
+ #
248
+ # {"lidwake"=>"1",
249
+ # "autopoweroff"=>"1",
250
+ # ...}
251
+ #
252
+ # - `[<mode>, <setting>]` gets a string value.
253
+ #
254
+ # `PMSet.read ["AC Power", "lidwake"]` looks something like `"1"`
255
+ #
256
+ # in addition
257
+ # - `<mode>` must be in the keys of {.MODES}
258
+ # - `<setting>` must be in {.SETTINGS}
259
+ #
260
+ # @return [Hash<String, Hash<String, String>>] hash of everything when
261
+ # `key` is `[]`
262
+ # @return [Hash<String, String>] hash of values for mode when `key` is
263
+ # `[<mode>]`
264
+ # @return [String] value when `key` is `[<mode>, <setting>]`
265
+ #
266
+ # @raise [ArgumentError] if the key is not found.
267
+ #
268
+ def self.read key, options = {}
269
+ # read all the settings.
270
+ settings = parse Cmds.out!('pmset -g custom')
271
+
272
+ value = settings
273
+ key.each do |seg|
274
+ unless value.key? seg
275
+ raise ArgumentError.new binding.erb <<-END
276
+ bad segment #{ seg.inspect } in key #{ key }.
277
+
278
+ pm settings:
279
+
280
+ <%= settings.pretty_inspect %>
281
+ END
282
+ end
283
+ value = value[seg]
284
+ end
285
+
286
+ value
287
+ end
288
+
289
+ # @api adapter
290
+ #
291
+ # writes pm settings.
292
+ #
293
+ # @param key [Array<String>] must be a two-element array of strings where
294
+ # the first element is a key of {.MODES} and the second is in {.SETTINGS}.
295
+ # @param value [String] value to write.
296
+ #
297
+ # @return nil
298
+ #
299
+ # @raise [ArgumentError] if `key` is bad.
300
+ #
301
+ def self.write key, value, options = {}
302
+ unless key.is_a?(Array) && key.length == 2
303
+ raise ArgumentError.new binding.erb <<-END
304
+ key must be a pair [mode, setting], not <%= key.inspect %>.
305
+ END
306
+ end
307
+
308
+ mode, setting = key
309
+
310
+ if MODES[mode].nil?
311
+ raise ArgumentError.new binding.erb <<-END
312
+ first key element must be one of
313
+
314
+ <%= MODES.keys.pretty_inspect %>
315
+
316
+ found <%= mode.inspect %>
317
+ END
318
+ end
319
+
320
+ unless SETTINGS.include? setting
321
+ raise ArgumentError.new binding.erb <<-END
322
+ second key element must be one of
323
+
324
+ <%= SETTINGS.pretty_inspect %>
325
+
326
+ found <%= setting.inspect %>
327
+ END
328
+ end
329
+
330
+ Cmds! "sudo pmset -#{ MODES[mode] } %{setting} %{value}",
331
+ setting: setting,
332
+ value: value
333
+
334
+ nil
335
+ end
336
+ end # PMSet
@@ -1,3 +1,3 @@
1
1
  module StateMate
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/state_mate.gemspec CHANGED
@@ -26,6 +26,7 @@ END
26
26
  spec.add_development_dependency "rspec"
27
27
  spec.add_development_dependency "yard"
28
28
  spec.add_development_dependency "redcarpet"
29
+ spec.add_development_dependency "nrser-extras"
29
30
 
30
31
  spec.add_dependency 'nrser', '~> 0.0', '>= 0.0.13'
31
32
  spec.add_dependency 'CFPropertyList', '~> 2.3'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_mate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-18 00:00:00.000000000 Z
11
+ date: 2015-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: nrser-extras
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: nrser
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -158,6 +172,7 @@ files:
158
172
  - lib/state_mate/adapters/json.rb
159
173
  - lib/state_mate/adapters/launchd.rb
160
174
  - lib/state_mate/adapters/nvram.rb
175
+ - lib/state_mate/adapters/pmset.rb
161
176
  - lib/state_mate/adapters/scutil.rb
162
177
  - lib/state_mate/adapters/time_machine.rb
163
178
  - lib/state_mate/version.rb