nrser 0.3.8 → 0.3.9

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
2
  SHA1:
3
- metadata.gz: 20a9f5822833e3797ebf2ec2ba0b6e430ad7346e
4
- data.tar.gz: 0dc5e6c87d30f0d9ed99692b33da84a4f2584db9
3
+ metadata.gz: 8924f7337f324ced68cc42837909ecd01e31cbed
4
+ data.tar.gz: 9967780c03770b6f18214d6800107046db3ba3ae
5
5
  SHA512:
6
- metadata.gz: 374129784c79fe2897c078beed6e4d7c168e9b2e79027bf293df15403eac73a505176db6238f2c9b14ba8d2ecbda399e61c5f2a57cce4b8ec79a0c7a12ec031e
7
- data.tar.gz: ae5a9bb34f77b6d35976fbed02fa85b8267a34b9cfb0a0ec0b4db6b39bae1d38ed08c3c994f27439f437580ebc9023c6d23c19ef827b58691a5aca0969f4e17a
6
+ metadata.gz: e8bc8cfb3460f2c1bba1aeec16f5e9f7f56d431a5caddd01fea73422ddafb1dbe11503664a91103be82be48c51e7620794e11a052f414656baff4726bd271f3e
7
+ data.tar.gz: 5c48ccefb961b1308c8adf7b11aa225ce7a932ddc663c99d62e425ae7dacda93f572187fdacc86795b990950e7caff23b8e208a10b5b5484b3de4b03abcb7624
@@ -1,6 +1,7 @@
1
1
  require 'pathname'
2
2
  require 'active_support/core_ext/string/filters'
3
3
  require 'active_support/core_ext/string/inflections'
4
+ require 'nrser/sys/env'
4
5
 
5
6
  # Extension methods for {String}
6
7
  #
@@ -133,5 +134,21 @@ class String
133
134
  end
134
135
 
135
136
  # @!endgroup Unicode Stylization
137
+
138
+
139
+ # @!group Inflection Instance Methods
140
+ # ==========================================================================
141
+
142
+ # Attempt to convert `self` into an ENV var name.
143
+ #
144
+ # @see NRSER::Sys::Env.varize
145
+ #
146
+ # @return (see NRSER::Sys::Env.varize)
147
+ #
148
+ def env_varize
149
+ NRSER::Sys::Env.varize self
150
+ end
151
+
152
+ # @!endgroup Inflection Instance Methods # *********************************
136
153
 
137
154
  end # class String
@@ -0,0 +1,104 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ class Consumer
5
+ def initialize
6
+ @consumed = []
7
+ end
8
+ end
9
+
10
+
11
+ class ArgConsumer < Consumer
12
+
13
+ def can_consume? tokens
14
+ return 0 unless consumed.empty?
15
+
16
+ if tokens[0][0] == '-'
17
+ 0
18
+ else
19
+ 1
20
+ end
21
+ end
22
+
23
+ end # class ArgConsumer
24
+
25
+
26
+ class OptionalArgConsumer
27
+
28
+ end
29
+
30
+
31
+ class OptConsumer < Consumer
32
+
33
+ def initialize name:, aliases: []
34
+ @name = name
35
+ @aliases = aliases.dup
36
+ end
37
+
38
+ def names
39
+ @names ||= [name, *aliases]
40
+ end
41
+
42
+ def long_names
43
+ @long_names ||= @names.select { |name| name.length > 1 }
44
+ end
45
+
46
+ def can_consume? tokens
47
+ return 0 unless consumed.empty?
48
+
49
+ first = tokens[0]
50
+
51
+ return 1 if long_names.any? { |name|
52
+ /\-\-#{ name }\=.*/ =~ first
53
+ }
54
+
55
+ if tokens.length > 1 &&
56
+ names.any? { |name|
57
+ if name.length == 1
58
+ first == "-#{ name }"
59
+ else
60
+ first == "--#{ name }"
61
+ end
62
+ }
63
+ return 2
64
+ end
65
+
66
+ return 0
67
+ end
68
+
69
+ def satisfied?
70
+ if consumed.empty?
71
+ type.test?( nil )
72
+ else
73
+ true
74
+ end
75
+ end
76
+
77
+ end # class OptConsumer
78
+
79
+
80
+ class BoolOptConsumer < Consumer
81
+
82
+ def switch_tokens
83
+ @switches ||= names.each_with_object( Set.new ) do |name, set|
84
+ if name.length == 1
85
+ set << "-#{ name }"
86
+ else
87
+ set << "--#{ name }"
88
+ set << "--no-#{ name }"
89
+ end
90
+ end
91
+ end
92
+
93
+ def can_consume? tokens
94
+ return 0 unless consumed.empty?
95
+
96
+ first = tokens[0]
97
+
98
+ return 1 if self.switch_tokens.include?( tokens[0] )
99
+
100
+ return 0
101
+ end
102
+
103
+ end # class BoolOptConsumer
104
+
@@ -0,0 +1,10 @@
1
+
2
+
3
+ class Parser
4
+ def initialize consumers:, tokens:
5
+ @consumers = consumers
6
+ @tokens = tokens
7
+ end
8
+
9
+
10
+ end
data/lib/nrser/log.rb CHANGED
@@ -101,11 +101,9 @@ module NRSER::Log
101
101
  singleton_class.send :alias_method, :[], :logger_for
102
102
 
103
103
 
104
-
105
104
  # @!group Utility Class Methods
106
105
  # ------------------------------------------------------------------------
107
106
 
108
-
109
107
  # @todo Document logger_name_and_type method.
110
108
  #
111
109
  # @param [type] arg_name
@@ -244,9 +242,82 @@ module NRSER::Log
244
242
  def self.level= level
245
243
  SemanticLogger.default_level = level_sym_for level
246
244
  end
245
+
246
+
247
+ # Get the current ENV var prefix to use looking for config (when values
248
+ # are not explictly provided).
249
+ #
250
+ # @return [nil]
251
+ # A prefix has not been sucessfully set, and we couldn't figure one out
252
+ # from {.application}.
253
+ #
254
+ # @return [false]
255
+ # Looking for config in ENV has been explicitly disabled.
256
+ #
257
+ # @return [String]
258
+ # The ENV var name prefix we'll use looking for logging config.
259
+ #
260
+ def self.env_var_prefix
261
+ return @env_var_prefix unless @env_var_prefix.nil?
262
+
263
+ return application.to_s.env_varize unless application.nil?
264
+
265
+ return nil
266
+ end
267
+
268
+
269
+ # Set the {.env_var_prefix}.
270
+ #
271
+ # @param [String | false] prefix
272
+ # Value to set the prefix to. `false` disables looking up config in the
273
+ # ENV.
274
+ #
275
+ # If anything other than `false` or a {String} that satisfies
276
+ # {NRSER::Sys::Env.var_name?} is passed a warning will be logged,
277
+ # the value will be ignored, and `nil` will be returned.
278
+ #
279
+ # @return [nil]
280
+ # `prefix` was a bad value and the internal variable was not set. A
281
+ # warning was also logged.
282
+ #
283
+ # @return [String | false]
284
+ # The value was set to `prefix`.
285
+ #
286
+ def self.env_var_prefix= prefix
287
+ unless prefix == false || NRSER::Sys::Env.var_name?( prefix )
288
+ logger.warn \
289
+ "Bad NRSER::Log.env_var_prefix; must `false` or a String matching" +
290
+ "#{ NRSER::Sys::Env::VAR_NAME_RE }. Ignoring...",
291
+ value: prefix
292
+
293
+ return nil
294
+ end
295
+
296
+ @env_var_prefix = prefix
297
+ end
247
298
 
248
299
 
249
- def self.level_from_ENV prefix:
300
+ # Try to find a log level in the ENV.
301
+ #
302
+ # @param [String | false | nil] prefix:
303
+ # The prefix to look under.
304
+ #
305
+ # @return [nil]
306
+ # We either didn't look or we didn't find.
307
+ #
308
+ # @return [Symbol]
309
+ # One of {SemanticLogger::LEVELS}, returned because we found a
310
+ # {NRSER.truthy?} `<prefix>_DEBUG` or `<prefix>_TRACE`.
311
+ #
312
+ # @return [String]
313
+ # Value found at `<prefix>_LOG_LEVEL`. **Note that this may not be
314
+ # a valid level - this method does NOT check!**.
315
+ #
316
+ def self.level_from_env prefix: self.env_var_prefix
317
+ # Bail with a `nil` if we can't figure out a prefix (it's `nil`) or
318
+ # looking in the ENV has been disabled (it's `false`).
319
+ return nil unless prefix
320
+
250
321
  if NRSER.truthy? ENV["#{ prefix }_TRACE"]
251
322
  return :trace
252
323
  elsif NRSER.truthy? ENV["#{ prefix }_DEBUG"]
@@ -270,8 +341,14 @@ module NRSER::Log
270
341
 
271
342
  # Setup logging.
272
343
  #
273
- # @param [type] arg_name
274
- # @todo Add name param description.
344
+ # @param [String | false | nil] env_var_prefix:
345
+ # Prefix to ENV var names to look for logging setup config under,
346
+ # like `<prefix>_LOG_LEVEL`, `<prefix>_DEBUG` and `<prefix>_TRACE`.
347
+ #
348
+ # If `nil` (the default), we'll try to use `application` to guess a
349
+ # prefix.
350
+ #
351
+ # You can disable any ENV lookups by passing `false`.
275
352
  #
276
353
  # @return [nil]
277
354
  #
@@ -295,27 +372,28 @@ module NRSER::Log
295
372
 
296
373
  # Wrap around everything to make sure we release the mutex
297
374
  begin
298
- # Setup main appender if needed
375
+ # Setup main appender if needed so that any logging *in here* hopefully
376
+ # has somewhere to go
299
377
  setup_appender! dest
378
+
379
+ # Set the application, which we may use for the ENV var prefix
380
+ self.application = application unless application.nil?
381
+
382
+ # Set the ENV var prefix, if we received one
383
+ self.env_var_prefix = env_var_prefix unless env_var_prefix.nil?
300
384
 
301
385
  # Set sync/async processor state
302
386
  setup_sync! sync
303
387
 
304
388
  # If we didn't receive a level, check the ENV
305
389
  if level.nil?
306
- if env_var_prefix.nil? && !application.nil?
307
- env_var_prefix = application.gsub( /[^a-zA-Z0-0_]+/, '_' ).upcase
308
- end
309
-
310
- level = level_from_ENV prefix: env_var_prefix
390
+ level = level_from_env
311
391
  end
312
392
 
313
393
  # If we ended up with a level, try to set it (will only log a warning
314
394
  # if it fails, not raise, which could crash things on boot)
315
395
  setup_level! level unless level.nil?
316
396
 
317
- self.application = application unless application.nil?
318
-
319
397
  # Setup formatter header and body tokens, if needed
320
398
  setup_formatter_tokens! :header, header
321
399
  setup_formatter_tokens! :body, body
@@ -370,18 +448,38 @@ module NRSER::Log
370
448
  # Call {.setup!} with some default keywords that are nice for interactive
371
449
  # session (console/REPL) usage.
372
450
  #
373
- # @param (see .setup!)
451
+ # @param [Boolean] add_main_logger:
452
+ # Define a `logger` method at the top level (global) that gets a logger
453
+ # for the main object.
454
+ #
455
+ # @param application: (see .setup!)
456
+ # @param dest: (see .setup!)
457
+ # @param header: (see .setup!)
458
+ # @param sync: (see .setup!)
459
+ #
374
460
  # @return (see .setup!)
375
461
  #
376
- def self.setup_for_console! dest: $stderr,
377
- sync: true,
462
+ def self.setup_for_console! add_main_logger: true,
463
+ application: $0,
464
+ dest: $stderr,
378
465
  header: { delete: :process_info },
466
+ sync: true,
379
467
  **kwds
380
468
  setup! \
381
469
  dest: dest,
382
470
  sync: sync,
383
471
  header: header,
472
+ application: application,
384
473
  **kwds
474
+
475
+ if add_main_logger
476
+ TOPLEVEL_BINDING.eval <<~END
477
+ def logger
478
+ NRSER::Log[self]
479
+ end
480
+ END
481
+ end
482
+
385
483
  end # .setup_for_console!
386
484
 
387
485
 
@@ -697,18 +795,32 @@ module NRSER::Log
697
795
  when false
698
796
  # Used to remove the appender and set {.appender} to `nil`.
699
797
  nil
700
- when SemanticLogger::Subscriber, Hash
798
+
799
+ when SemanticLogger::Subscriber
701
800
  # Can be handled directly
702
801
  SemanticLogger.add_appender dest
802
+
803
+ when Hash
804
+ # Override color formatter with our own implementation
805
+ if dest[:formatter] == :color
806
+ dest = dest.merge formatter: NRSER::Log::Formatters::Color.new
807
+ end
808
+
809
+ SemanticLogger.add_appender dest
703
810
 
704
811
  when String, Pathname
705
812
  # Assume these are file paths
706
813
  SemanticLogger.add_appender file_name: dest.to_s
707
814
 
708
- else
815
+ when IO
709
816
  SemanticLogger.add_appender \
710
817
  io: dest,
711
818
  formatter: NRSER::Log::Formatters::Color.new
819
+
820
+ else
821
+ # I guess just try and add it...?
822
+ SemanticLogger.add_appender dest
823
+
712
824
  end
713
825
 
714
826
  # Remove the old appender (if there was one). This is done after adding
@@ -14,6 +14,8 @@ module NRSER::RSpex::ExampleGroup
14
14
  metadata: metadata,
15
15
  &body
16
16
  end # #describe_case
17
+
18
+ alias_method :use_case, :describe_case
17
19
 
18
20
  # Older name
19
21
  alias_method :describe_use_case, :describe_case
@@ -25,8 +25,11 @@ module NRSER::RSpex::ExampleGroup
25
25
  end
26
26
 
27
27
  # Old name (used to be different method)
28
-
29
28
  alias_method :context_where, :describe_when
29
+
30
+ # Short names (need `_` pre 'cause of `when` Ruby keyword, and suffix fucks
31
+ # up auto-indent in Atom/VSCode)
32
+ alias_method :_when, :describe_when
30
33
 
31
34
 
32
35
  end # module NRSER::RSpex::ExampleGroup
data/lib/nrser/sys/env.rb CHANGED
@@ -1,13 +1,205 @@
1
+ # encoding: UTF-8
1
2
  # frozen_string_literal: true
2
3
 
3
- # Declarations
4
+
5
+ # Requirements
6
+ # ========================================================================
7
+
8
+ # Stdlib
9
+ # ------------------------------------------------------------------------
10
+
11
+ # Need {Set}
12
+ require 'set'
13
+
14
+ # Deps
15
+ # ----------------------------------------------------------------------------
16
+
17
+ # Need {String#underscore}
18
+ require 'active_support/core_ext/string/inflections'
19
+
20
+
21
+ # Namespace
22
+ # =======================================================================
23
+
24
+ module NRSER
25
+ module Sys
26
+
27
+
28
+ # Definitions
4
29
  # =======================================================================
5
30
 
6
- module NRSER::Sys; end
7
- module NRSER::Sys::Env; end
31
+ # Tools for dealing with the system (POSIX-like) environment.
32
+ module Env
33
+
34
+ # Constants
35
+ # ========================================================================
36
+
37
+ # Common and/or important ENV var names that we don't really want to end
38
+ # up auto-generating.
39
+ #
40
+ # @see http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html
41
+ # @see https://archive.is/fmBRH
42
+ #
43
+ # @return [Set<String>]
44
+ #
45
+ COMMON_EXPORT_NAMES = Set[
46
+ "ARFLAGS",
47
+ "CC",
48
+ "CDPATH",
49
+ "CFLAGS",
50
+ "CHARSET",
51
+ "COLUMNS",
52
+ "DATEMSK",
53
+ "DEAD",
54
+ "EDITOR",
55
+ "ENV",
56
+ "EXINIT",
57
+ "FC",
58
+ "FCEDIT",
59
+ "FFLAGS",
60
+ "GET",
61
+ "GFLAGS",
62
+ "HISTFILE",
63
+ "HISTORY",
64
+ "HISTSIZE",
65
+ "HOME",
66
+ "IFS",
67
+ "LANG",
68
+ "LC_ALL",
69
+ "LC_COLLATE",
70
+ "LC_CTYPE",
71
+ "LC_MESSAGES",
72
+ "LC_MONETARY",
73
+ "LC_NUMERIC",
74
+ "LC_TIME",
75
+ "LDFLAGS",
76
+ "LEX",
77
+ "LFLAGS",
78
+ "LINENO",
79
+ "LINES",
80
+ "LISTER",
81
+ "LOGNAME",
82
+ "LPDEST",
83
+ "MAIL",
84
+ "MAILCHECK",
85
+ "MAILER",
86
+ "MAILPATH",
87
+ "MAILRC",
88
+ "MAKEFLAGS",
89
+ "MAKESHELL",
90
+ "MANPATH",
91
+ "MBOX",
92
+ "MORE",
93
+ "MSGVERB",
94
+ "NLSPATH",
95
+ "NPROC",
96
+ "OLDPWD",
97
+ "OPTARG",
98
+ "OPTERR",
99
+ "OPTIND",
100
+ "PAGER",
101
+ "PATH",
102
+ "PPID",
103
+ "PRINTER",
104
+ "PROCLANG",
105
+ "PROJECTDIR",
106
+ "PS1",
107
+ "PS2",
108
+ "PS3",
109
+ "PS4",
110
+ "PWD",
111
+ "RANDOM",
112
+ "SECONDS",
113
+ "SHELL",
114
+ "TERM",
115
+ "TERMCAP",
116
+ "TERMINFO",
117
+ "TMPDIR",
118
+ "TZ",
119
+ "USER",
120
+ "VISUAL",
121
+ "YACC",
122
+ "YFLAGS",
123
+ ].freeze
124
+
125
+
126
+ # Regular expression mathcing strict and portable ENV var name rules:
127
+ # only `A-Z`, `0-9` and `_`; can not start with digit.
128
+ #
129
+ # @return [RegExp]
130
+ #
131
+ VAR_NAME_RE = /\A[A-Z_][A-Z0-9_]+\z/
132
+
133
+
134
+ # Class Methods
135
+ # ========================================================================
136
+
137
+ # Is ENV var name?
138
+ #
139
+ # Must be {String} and match {VAR_NAME_RE}.
140
+ #
141
+ # @param [Object] name
142
+ # The name. No chance of `true` unless it's a {String}.
143
+ #
144
+ # @return [Boolean]
145
+ # `true` if it passes our mustard.
146
+ #
147
+ def self.var_name? name
148
+ String === name && VAR_NAME_RE =~ name
149
+ end # .var_name?
150
+
151
+
152
+ # Attempt to munge any string into a decent-looking and legal ENV var name.
153
+ #
154
+ # We follow a strict, very portable (should be find in `sh`) guideline:
155
+ #
156
+ # > Environment variable names [...] consist solely of uppercase letters,
157
+ # > digits, and the '_' (underscore) [...] and do not begin with a digit.
158
+ #
159
+ # @see http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html
160
+ # @see https://archive.is/fmBRH
161
+ #
162
+ # @param [String] string
163
+ # Take a guess.
164
+ #
165
+ # @return [nil]
166
+ # If we didn't end up with a legal ENV var name.
167
+ #
168
+ # @return [String]
169
+ # If we were able to munge a legal ENV var name.
170
+ #
171
+ def self.varize string, prohibit_common_exports: false
172
+ candidate = string.
173
+ # 1. Use {ActiveSupport}'s {String#underscore} to produce a nicely
174
+ # underscore-separated name for common strings like class names.
175
+ underscore.
176
+ # 2. Convert lower-case letters to upper case.
177
+ upcase.
178
+ # 3. Smush all contiguous runs of anything not `A-Z0-9` into a `_`.
179
+ gsub( /[^A-Z0-9]+/, '_' ).
180
+ # 4. Bite off any leading digits.
181
+ sub( /\A\d+/, '' ).
182
+ # 5. Chomp off any trailing `_`. Just for good looks :)
183
+ chomp( '_' )
184
+
185
+ # Return `nil` if we didn't get a legal ENV var name
186
+ return nil unless var_name?( candidate )
187
+
188
+ # If `prohibit_common_exports` is `true` and the name we made is in
189
+ # that list then return `nil`.
190
+ if prohibit_common_exports && COMMON_EXPORT_NAMES.include?( candidate )
191
+ return nil
192
+ end
193
+
194
+ # If we got here, we're good!
195
+ return candidate
196
+ end # .varize
197
+
198
+ end # module Env
8
199
 
9
200
 
10
- # Post-Processing
201
+ # /Namespace
11
202
  # =======================================================================
12
203
 
13
- require_relative './env/path'
204
+ end # module Sys
205
+ end # module NRSER
data/lib/nrser/version.rb CHANGED
@@ -18,7 +18,7 @@ module NRSER
18
18
  #
19
19
  # @return [String]
20
20
  #
21
- VERSION = '0.3.8'
21
+ VERSION = '0.3.9'
22
22
 
23
23
 
24
24
  module Version
@@ -1,4 +1,4 @@
1
- require 'nrser/sys/env'
1
+ require 'nrser/sys/env/path'
2
2
 
3
3
  describe_spec_file(
4
4
  spec_path: __FILE__,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nrser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-06 00:00:00.000000000 Z
11
+ date: 2018-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hamster
@@ -328,6 +328,8 @@ files:
328
328
  - lib/nrser/labs/globlin.rb
329
329
  - lib/nrser/labs/i8.rb
330
330
  - lib/nrser/labs/index.rb
331
+ - lib/nrser/labs/lots/consumer.rb
332
+ - lib/nrser/labs/lots/parser.rb
331
333
  - lib/nrser/labs/stash.rb
332
334
  - lib/nrser/log.rb
333
335
  - lib/nrser/log/appender.rb