cocos 0.4.1 → 0.4.2

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
  SHA256:
3
- metadata.gz: 51832aaf3ccd7164304a1c4652108b89d7651d2d7358e80c8f0d24fa46f59f69
4
- data.tar.gz: 3236f78ad3ced02d1ac42a047a0ce11eaf60017063b61c2c1480786e3513ddc0
3
+ metadata.gz: e276117f95b89fcfc61f23559b725c3905aa8668eb6b0b7e7175c4fe57eb1a86
4
+ data.tar.gz: 1b3043bd0de213189a8e531d4fea92ccf7b1262636067a05c914261a66d4338c
5
5
  SHA512:
6
- metadata.gz: 11206d11694da8a98bf33b2c444975ec3e195e4c29348f761945fba044fe582d5ab885bb8a5d3057f3ce5589ba288bc14b9d2b456e0003fa74537ac5efa3d0a1
7
- data.tar.gz: 8f9e09293599531c4fd4482295fc75e0d7aeea95ffe5de62f7c2bc4d2e84a4e8b643109682accdb98b3295e30e89265f0aa6b36dbcaf70e25adfd8488ea6ba8c
6
+ metadata.gz: ff35b353cde05f6f76e6b6069442abda8769488768edaba6dfd2b411b06ec24aac97d0a10c25bcac08ba5617451e2a9c800861a5bd535e4d347b8e68b17eb747
7
+ data.tar.gz: '08c86a7e5950818e53c38668b68259e4248be7e0ef5201a52a570349e7dfe803dd6a534af73ca45ca7745fadc9a19022c15d08a38a121c5153fbc7705035e153'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,4 @@
1
- ### 0.4.1
2
- ### 0.4.0
1
+ ### 0.4.2
3
2
  ### 0.0.1 / 2022-08-01
4
3
 
5
4
  * Everything is new. First release.
data/README.md CHANGED
@@ -50,7 +50,7 @@ txt = File.read( "history.txt" )
50
50
  # (e.g. on microsoft windows it is ISO Code Page (CP-1252
51
51
  # or something - depending on your locale/culture/language)
52
52
 
53
- txt = File.open( "history.txt", "r:utf-8" ) do |f|
53
+ txt = File.open( "history.txt", "r:bom|utf-8" ) do |f|
54
54
  f.read
55
55
  end
56
56
  ```
@@ -61,7 +61,7 @@ and always repeating the same open / read and code block dance
61
61
  again and again e.g.:
62
62
 
63
63
  ``` ruby
64
- txt = File.open( "history.json", "r:utf-8" ) do |f|
64
+ txt = File.open( "history.json", "r:bom|utf-8" ) do |f|
65
65
  f.read
66
66
  end
67
67
  data = JSON.parse( txt )
@@ -94,7 +94,7 @@ _Read / parse convenience short-cut helpers_
94
94
  also known as `read_txt`
95
95
 
96
96
 
97
- `read_lines( path )`
97
+ `read_lines( path, chomp: true|false )`
98
98
 
99
99
 
100
100
  `read_json( path )` / `parse_json( str )`
@@ -138,5 +138,4 @@ That's it for now.
138
138
  ## License
139
139
 
140
140
  The `cocos` scripts are dedicated to the public domain.
141
- Use it as you please with no restrictions whatsoever.
142
-
141
+ Use as you please with no restrictions whatsoever.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ Hoe.spec 'cocos' do
12
12
  self.urls = { home: 'https://github.com/rubycocos/cocos' }
13
13
 
14
14
  self.author = 'Gerald Bauer'
15
- self.email = 'gerald.bauer@gmail.com'
15
+ self.email = 'gerald.bauer@gmail.com'
16
16
 
17
17
  # switch extension to .markdown for gihub formatting
18
18
  self.readme_file = 'README.md'
data/lib/cocos/env.rb CHANGED
@@ -13,9 +13,9 @@ module EnvParser
13
13
  # returns a hash
14
14
  # (compatible structure - works like YAML.load_file)
15
15
  #
16
- # change to .read(path) and parse( text) - why? why not?
16
+ # change to .read(path) and parse(text) - why? why not?
17
17
  def self.load_file( path )
18
- text = File.open( path, 'r:utf-8' ) { |f| f.read }
18
+ text = File.open( path, 'r:bom|utf-8', newline: :lf) { |f| f.read }
19
19
  parse( text )
20
20
  end
21
21
  def self.load( text ) parse( text ); end
@@ -32,7 +32,7 @@ module EnvParser
32
32
  ## todo/ addd support for quoted values - why? why not?
33
33
  ## add support for "inline" end of line comments - why? why not?
34
34
  ## add support for escapes and multi-line values - why? why not?
35
- LINE_RE = /\A
35
+ LINE_RE = %r{\A
36
36
  [ ]*
37
37
  (?<key> [A-Za-z][A-Za-z0-9_-]*)
38
38
  [ ]*
@@ -41,7 +41,7 @@ LINE_RE = /\A
41
41
  (?<value>.+?) ## non-greedy
42
42
  [ ]*
43
43
  \z
44
- /x
44
+ }x
45
45
 
46
46
  ## use a parser class - why? why not?
47
47
  def self.parse( text )
@@ -17,7 +17,7 @@ module Kernel
17
17
 
18
18
  def find_file!( name, path: )
19
19
  filepath = find_file( name, path: path )
20
- raise Errorno::ENOENT, "file <#{name}> not found; looking in path #{path.inspect}" if filepath.nil?
20
+ raise Errno::ENOENT, "file #{name.inspect} not found; looking in path #{path.inspect}" if filepath.nil?
21
21
  filepath
22
22
  end
23
23
 
@@ -25,16 +25,25 @@ end
25
25
  ## note - find_file will NOT find directories!!!
26
26
  ## File.file? will only check if a file (not directory) exits!!
27
27
 
28
-
29
28
  ##
30
29
  ## todo/check - expand path and use File.realpath too?
31
30
  ## or keep "simple" File.join ?
32
31
 
32
+ ##
33
+ ## note - always expand path for now and return absolute path!
34
+ ##
35
+ ## note - do NOT search path if name passed in is absolute!!!
33
36
  def find_file( name, path: )
34
- return name if File.file?( name )
37
+ filepath = File.expand_path( name )
38
+ return filepath if File.file?( filepath )
39
+
40
+ ## note - if name starts with root / or c:\
41
+ ## assume it's absolute - handle by Pathname lib for now
42
+ ## do NOT search!!!
43
+ return nil if Pathname.new(name).absolute?
35
44
 
36
- path.each do |dir|
37
- filepath = File.join( dir, name )
45
+ path.each do |basedir|
46
+ filepath = File.expand_path( name, basedir )
38
47
  return filepath if File.file?( filepath )
39
48
  end
40
49
 
@@ -42,4 +51,33 @@ def find_file( name, path: )
42
51
  end
43
52
 
44
53
 
54
+
55
+ def find_dir( name, path: [] )
56
+ dirpath = File.expand_path( name )
57
+ return dirpath if Dir.exist?( dirpath )
58
+
59
+ ## note - if name starts with / or \ assume it's absolute!!
60
+ ## do NOT search!!!
61
+ ## note - search still works for
62
+ ## ./austria or ../austria or such
63
+ ##
64
+ ## todo/check/fix-fix-fix
65
+ ## is there a File.absolute? or such method for reuse??
66
+ ##
67
+ ## todo/fix-fix-fix add absolute check upstream to find_file too!!!
68
+ ## return nil if name.start_with?( %r{[/\\]} )
69
+ ## note - Pathname#absolute? is basically !Pathname#relative? !!!
70
+ return nil if Pathname.new(name).absolute?
71
+
72
+ path.each do |basedir|
73
+ ## todo/check - always make sure basedir is an absolute/expanded path - why? why not?
74
+ dirpath = File.expand_path( name, basedir )
75
+ return dirpath if Dir.exist?( dirpath )
76
+ end
77
+
78
+ nil ## return nil if not found
79
+ end
80
+
81
+
82
+
45
83
  end # module Kernel
data/lib/cocos/version.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  module Cocos
3
3
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
4
4
  MINOR = 4
5
- PATCH = 1
5
+ PATCH = 2
6
6
  VERSION = [MAJOR,MINOR,PATCH].join('.')
7
7
 
8
8
  def self.version
data/lib/cocos.rb CHANGED
@@ -8,6 +8,8 @@ require 'json'
8
8
  require 'yaml'
9
9
  require 'base64' ## e.g. Base64.decode64,Base64.encode64,...
10
10
  require 'fileutils'
11
+ require 'pathname' ## used by find_file/find_dir to check for absolute? etc.
12
+
11
13
 
12
14
  require 'uri'
13
15
  require 'net/http'
@@ -40,9 +42,8 @@ require_relative 'cocos/find_file'
40
42
  ## read/parse convenience/helper shortcuts
41
43
 
42
44
 
43
- module Kernel
44
-
45
45
 
46
+ module Kernel
46
47
 
47
48
  ################
48
49
  # private helpers - keep along here - why? why not?
@@ -51,6 +52,20 @@ module Kernel
51
52
  ## todo: add symbolize options a la read_json? - why? why not?
52
53
  ## add sep options
53
54
 
55
+ ##
56
+ ## todo - add upstream
57
+ ## to CsvHash.read
58
+ ## newline option on read/write and
59
+ ## bom option on read
60
+ ## note:
61
+ ## (i) :newline => :lf option
62
+ # Reads text natively; normalizes all incoming newlines to strict LF (\n)
63
+ ## (ii) 'bom|' flag
64
+ ## The 'bom|' flag instructs Ruby to drop the BOM bytes if they exist
65
+ ## File.open( path, 'r:bom|utf-8', newline: :lf) do |f|
66
+
67
+
68
+
54
69
  def read_csv( path, sep: nil )
55
70
  opts = {}
56
71
  opts[:sep] = sep if sep
@@ -108,6 +123,10 @@ def download_tab( url )
108
123
  end
109
124
 
110
125
 
126
+ ## fix-fix-fix
127
+ ## for json add new strict: true|false option (default is false!)
128
+ ## add option for allowing trailing_commas and comments!!!
129
+ ## see JSON.parse for more
111
130
 
112
131
  ## todo: add symbolize options ???
113
132
  def read_json( path )
@@ -160,14 +179,13 @@ alias_method :download_conf, :download_ini
160
179
 
161
180
 
162
181
 
163
-
164
182
  def read_text( path )
165
- ## todo/check: add universal newline mode or such?
166
- ## e.g. will always convert all
167
- ## newline variants (\n|\r|\n\r) to "universal" \n only
168
- ##
169
- ## add r:bom - why? why not?
170
- File.open( path, 'r:utf-8' ) do |f|
183
+ ## note:
184
+ ## (i) :newline => :lf option
185
+ # Reads text natively; normalizes all incoming newlines to strict LF (\n)
186
+ ## (ii) 'bom|' flag
187
+ ## The 'bom|' flag instructs Ruby to drop the BOM bytes if they exist
188
+ File.open( path, 'r:bom|utf-8', newline: :lf) do |f|
171
189
  f.read
172
190
  end
173
191
  end
@@ -202,20 +220,25 @@ end
202
220
  ## add/offer chomp: true/false option or such - why? why not?
203
221
  ## see String.lines in rdoc
204
222
  ##
205
- def read_lines( path )
206
- read_text( path ).lines
223
+ ##
224
+ ## yes, add chomp: true|false option
225
+ ## note - default is chomp: false (keeping the trailing newline)
226
+
227
+ def read_lines( path, chomp: false )
228
+ read_text( path ).lines( chomp: chomp )
207
229
  end
208
230
 
209
- def parse_lines( str )
210
- str.lines
231
+ def parse_lines( str, chomp: false )
232
+ str.lines( chomp: chomp )
211
233
  end
212
234
 
213
- def download_lines( url )
214
- parse_lines( download_text( url ))
235
+ def download_lines( url, chomp: false )
236
+ parse_lines( download_text( url ), chomp: chomp )
215
237
  end
216
238
 
217
239
 
218
240
 
241
+
219
242
  def read_env( path )
220
243
  parse_env( read_text( path ))
221
244
  end
@@ -250,6 +273,7 @@ end
250
273
 
251
274
 
252
275
 
276
+
253
277
  ######
254
278
  # add writers
255
279
 
@@ -262,7 +286,9 @@ def write_json( path, data )
262
286
  FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
263
287
 
264
288
  ## note: pretty print/reformat json
265
- File.open( path, 'w:utf-8' ) do |f|
289
+ ## note: on windows ruby translates \n to \r\n
290
+ ## use newline option to use \n everywhere!!
291
+ File.open( path, 'w:utf-8', newline: :lf) do |f|
266
292
  f.write( JSON.pretty_generate( data ))
267
293
  end
268
294
  end
@@ -292,7 +318,9 @@ def write_text( path, text )
292
318
  dirname = File.dirname( path )
293
319
  FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
294
320
 
295
- File.open( path, 'w:utf-8' ) do |f|
321
+ ## note: on windows ruby translates \n to \r\n
322
+ ## use newline option to use \n everywhere!!
323
+ File.open( path, 'w:utf-8', newline: :lf) do |f|
296
324
  f.write( text )
297
325
  end
298
326
  end
@@ -309,7 +337,9 @@ def write_csv( path, recs, headers: nil )
309
337
  dirname = File.dirname( path )
310
338
  FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
311
339
 
312
- File.open( path, 'w:utf-8' ) do |f|
340
+ ## note: on windows ruby translates \n to \r\n
341
+ ## use newline option to use \n everywhere!!
342
+ File.open( path, 'w:utf-8', newline: :lf) do |f|
313
343
  if headers ## e.g. Date,Team 1,FT,HT,Team 2
314
344
  f.write( headers.map do |header|
315
345
  _escape_csv( header )
@@ -341,8 +371,10 @@ end
341
371
  ## then wrap the field in quotes
342
372
  ## Inside quoted fields, double every double quote (" → "")
343
373
 
374
+
344
375
  def _escape_csv(value)
345
376
  ## auto-convert to string or let code fail on nil or such?
377
+ ## or use value.to_str - why? why not?
346
378
  value = value.to_s
347
379
 
348
380
  ## note - double double quotes (") for now only
@@ -377,6 +409,12 @@ end
377
409
  ######
378
410
  # world wide web (www) support
379
411
 
412
+ ##
413
+ ## keep wget/wget! support - why? why not?
414
+ ## check if used outside anywhere
415
+ ## add deprecated warning - why? why not?
416
+
417
+
380
418
  def wget( url, **opts )
381
419
  Webclient.get( url, **opts )
382
420
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-05-09 00:00:00.000000000 Z
11
+ date: 2026-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: csvreader