Class: SportDb::Pathspecs

Inherits:
Object
  • Object
show all
Defined in:
lib/fbtok/pathspec.rb

Overview

change to a module - why? why not?

Class Method Summary collapse

Class Method Details

._expand_pathspecs(names) ⇒ Object

quick (internal) helper to

expand names (directories)  ending with /*
             with Pathspec.find


230
231
232
233
234
235
236
237
238
239
240
# File 'lib/fbtok/pathspec.rb', line 230

def self._expand_pathspecs( names )
   datafiles = []
   names.each do |name|
      if name.end_with?('/*')
         datafiles += Pathspec.find( name[0..-3] )
      else
         datafiles << name
      end
   end
   datafiles
end

.build(args, path: [], filepack: nil) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/fbtok/pathspec.rb', line 242

def self.build( args, path: [],
                      filepack: nil )
  recs = []

  ## check fo no args case  (and filepack present with default)
  if args.empty?
    if filepack && filepack.has_key?('default')
               recs << { 'path'      => '<default>',
                         'datafiles' => _expand_pathspecs(filepack['default']) }
    end
  else

    ## note - collect all "loose/standalone" files (NOT directories)
    ##             in single default pathspec node
    more = []

    args.each do |arg|
       ## note - ALWAYS give priority to existing directory matches (over filepack)
       ##             e.g. euro or such -  why? why not?
       ##
       ##  todo/check  if euro/ works too?
       ## check if directory
       ##     note - make find_dir/path lookup an env variable
       ##               e.g. FBTXT_ROOTDIR, FBTXT_SOURCE, FBSOURCE,
       ##                    FBTXT_REPOS/PACKS/etc. or such!!!
       ##     add dir_path/dirs or such to build - why? why not?
       if dir=find_dir( arg, path: ['/sports/openfootball']  )
          recs << { 'path'       => arg,
                    'datafiles'  => Pathspec.find( dir ) }
       elsif filepack && filepack.has_key?( arg.downcase )
           recs << { 'path'      => "<#{arg.downcase}>",
                     'datafiles' => _expand_pathspecs(filepack[arg.downcase]) }
       else   ## assume it's a file
           file = find_file( arg, path: path )
            ## check if file (exists) in any path lookup
            if file
              ## todo/fix:
              ##   add File.expand_path upstream in find_file !!!
              ##           and remove later here
              ##       also fix upstream Errorno::ENOENT to Errno::ENOENT !!!
              ##
              ## make sure path exists; raise error if not
               ## (auto-)expand path to normalize - yes why? why not?
              more << File.expand_path(file)
            else
              raise Errno::ENOENT, "No such file or directory - #{arg}"
            end
       end
    end

    if more.size > 0
      recs << { 'path'      => '<input>',
                'datafiles' =>  more }
    end
  end

  recs
end

.debug=(value) ⇒ Object



197
# File 'lib/fbtok/pathspec.rb', line 197

def self.debug=(value) @@debug = value; end

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


198
# File 'lib/fbtok/pathspec.rb', line 198

def self.debug?()      @@debug ||= false; end

.read(src) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/fbtok/pathspec.rb', line 201

def self.read( src )
    ## note: normalize src - use File.expand_path ??
    ##    change all backslash to slash for now (\ to /)
    fullsrc =  File.expand_path( src )

    recs = read_csv( fullsrc )
    pp recs     if debug?

    ##  note - make pathspecs relative to passed in file arg!!!
    basedir = File.dirname( fullsrc )

    recs.each do |rec|
        ##
        ## todo/check - check for season/seasons column - why? why not?
        path = rec['path']
        fullpath = File.expand_path( path, basedir )
        datafiles =   Pathspec.find( fullpath )

        ## auto-add (new) datafiles column (from expanded pathspec)
        rec['datafiles'] = datafiles
    end

    recs
end