cocos 0.4.1 → 0.4.3
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 +4 -4
- data/CHANGELOG.md +1 -2
- data/Manifest.txt +1 -0
- data/README.md +4 -5
- data/Rakefile +1 -1
- data/lib/cocos/env.rb +4 -4
- data/lib/cocos/find_file.rb +46 -8
- data/lib/cocos/version.rb +1 -1
- data/lib/cocos/words.rb +86 -0
- data/lib/cocos.rb +69 -18
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8bef9f661e35af9a5b936f7e37a27e73d0554e361e4d5f3a722305db037b6dc9
|
|
4
|
+
data.tar.gz: c347765ba3ce339a52541c801bb10fb6ce39800facd619773a6072bddffd16c2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8f6d70adfcfd0eb010daf8b7a23e8ae402a6186f7242d097e8a1fb99ad6187105567727077c033760b724c9863b6da64efe4a960ac8dd6c9ff961a6b485a9b89
|
|
7
|
+
data.tar.gz: d0b49d70ac8a369ed7d1f1d51b793edcffd468575592a1cb229c09494d16f7ff63b5479e385858d621190981c92e14c307cb7ebab66c62669fc12e94b69ce09b
|
data/CHANGELOG.md
CHANGED
data/Manifest.txt
CHANGED
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
|
|
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
|
|
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(
|
|
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 =
|
|
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
|
-
|
|
44
|
+
}x
|
|
45
45
|
|
|
46
46
|
## use a parser class - why? why not?
|
|
47
47
|
def self.parse( text )
|
data/lib/cocos/find_file.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
module Cocos
|
|
2
|
+
module Find
|
|
1
3
|
|
|
2
4
|
|
|
3
5
|
##
|
|
@@ -13,11 +15,9 @@
|
|
|
13
15
|
## always return absolute (expanded) path - why? why not?
|
|
14
16
|
|
|
15
17
|
|
|
16
|
-
module Kernel
|
|
17
|
-
|
|
18
18
|
def find_file!( name, path: )
|
|
19
19
|
filepath = find_file( name, path: path )
|
|
20
|
-
raise
|
|
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
|
-
|
|
37
|
+
filepath = File.expand_path( name )
|
|
38
|
+
return filepath if File.file?( filepath )
|
|
35
39
|
|
|
36
|
-
|
|
37
|
-
|
|
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?
|
|
44
|
+
|
|
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
|
|
|
45
|
-
|
|
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
|
+
end ## module Find
|
|
83
|
+
end ## module Cocos
|
data/lib/cocos/version.rb
CHANGED
data/lib/cocos/words.rb
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
|
|
2
|
+
###
|
|
3
|
+
##
|
|
4
|
+
## try new style with Cocos::FileUtils or Cocos::IO module
|
|
5
|
+
## and include module into Kernel (instead of directly)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
module Cocos
|
|
9
|
+
module FileUtils
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## "words" or "word array.
|
|
14
|
+
##
|
|
15
|
+
## follows %w[] rules
|
|
16
|
+
## splits on whitespace (space or newline)
|
|
17
|
+
## BUT include support for inline comments!!!!
|
|
18
|
+
##
|
|
19
|
+
## %w[apple banana cherry]
|
|
20
|
+
## => ["apple", "banana", "cherry"]
|
|
21
|
+
## note - allows more than one word on a line!!!!
|
|
22
|
+
##
|
|
23
|
+
## %w[
|
|
24
|
+
## ruby
|
|
25
|
+
## python
|
|
26
|
+
## javascript
|
|
27
|
+
## go
|
|
28
|
+
## ]
|
|
29
|
+
## => ["ruby", "python", "javascript", "go"]
|
|
30
|
+
##
|
|
31
|
+
## %w[] is a shorthand syntax (known as a percent literal)
|
|
32
|
+
## used to create an array of strings.
|
|
33
|
+
## It allows you to write an array of words without typing
|
|
34
|
+
## repetitive quotation marks and commas.
|
|
35
|
+
## Instead, you separate each item using whitespace (spaces or newlines).
|
|
36
|
+
## note - You CANNOT use standard # comments inside %w[].
|
|
37
|
+
## Ruby treats the # character as part of a literal string, which will ruin your array.
|
|
38
|
+
##
|
|
39
|
+
##
|
|
40
|
+
## todo/check:
|
|
41
|
+
## Option 1: The "Strict Space" Rule (Simplest & Safest)
|
|
42
|
+
## In most text/config files, a comment is separated by a space
|
|
43
|
+
## (e.g., key = value # comment).
|
|
44
|
+
## If a # is glued to a word without a space, it is usually part of the data
|
|
45
|
+
## (like a hex color #ffffff or a tag #important).
|
|
46
|
+
## You can modify your code to only remove # if it is preceded by whitespace
|
|
47
|
+
## color#ff0000 # red
|
|
48
|
+
## color = #ff0000 # red
|
|
49
|
+
## check - if this is how # is handled in yaml?
|
|
50
|
+
## - Inline Comments: A # can be placed at the end of a line of data,
|
|
51
|
+
## but it must be preceded by a space.
|
|
52
|
+
## e.g. port: 8080# This will break or fail to parse correctly
|
|
53
|
+
##
|
|
54
|
+
## e.g. use
|
|
55
|
+
## line.sub( ??, '' ).strip
|
|
56
|
+
|
|
57
|
+
def parse_words( txt )
|
|
58
|
+
words = [] ## array of strings
|
|
59
|
+
txt.each_line( chomp: true ) do |line|
|
|
60
|
+
|
|
61
|
+
line = line.strip
|
|
62
|
+
|
|
63
|
+
next if line.empty? || line.start_with?('#')
|
|
64
|
+
|
|
65
|
+
## strip (inline) end-of-line comments (from line) too - keep why? why not?
|
|
66
|
+
## ## (eat-up) REQUIRED leading (preceding) space(s) too - why? why not?
|
|
67
|
+
line = line.sub( /[ \t]+#.*/, '' )
|
|
68
|
+
|
|
69
|
+
## support __END__ marker for inline comments
|
|
70
|
+
break if line == '__END__'
|
|
71
|
+
|
|
72
|
+
words += line.split( /[ \t]+/ )
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
words
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
alias_method :parse_wordarray, :parse_words
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
## todo - add (missing) read_words/wordarray too - why? why not?
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
end # module FileUtils
|
|
86
|
+
end # module Cocos
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
206
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -397,6 +435,19 @@ end # module Kernel
|
|
|
397
435
|
|
|
398
436
|
|
|
399
437
|
|
|
438
|
+
require_relative 'cocos/words' ## pulls in parse_words/wordarray
|
|
439
|
+
|
|
440
|
+
###
|
|
441
|
+
## try add kernel global methods via module
|
|
442
|
+
module Kernel
|
|
443
|
+
include Cocos::FileUtils
|
|
444
|
+
include Cocos::Find # e.g. find_file, find_dir, etc.
|
|
445
|
+
end ## module Kernel
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
400
451
|
|
|
401
452
|
####
|
|
402
453
|
# convenience alias (use plural or singual)
|
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.
|
|
4
|
+
version: 0.4.3
|
|
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-
|
|
11
|
+
date: 2026-09-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: csvreader
|
|
@@ -119,6 +119,7 @@ files:
|
|
|
119
119
|
- lib/cocos/env.rb
|
|
120
120
|
- lib/cocos/find_file.rb
|
|
121
121
|
- lib/cocos/version.rb
|
|
122
|
+
- lib/cocos/words.rb
|
|
122
123
|
homepage: https://github.com/rubycocos/cocos
|
|
123
124
|
licenses:
|
|
124
125
|
- Public Domain
|