Class: SportDb::Parser::Opts

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

Overview

note - Opts Helpers for now nested inside Parser - keep here? why? why not?

Defined Under Namespace

Classes: PathspecNode

Constant Summary collapse

SEASON_RE =
%r{ (?:
     (?<season>\d{4}-\d{2})
      | 
     (?<season>\d{4}) 
       (?: --[a-z0-9_-]+ )?   ## todo/fix - allow "extension" to 2024-25 too - why?
  )
}x
SEASON =

“inline” helper for embedding in other regexes - keep? why? why not?

SEASON_RE.source
MATCH_RE =

note: if pattern includes directory add here

  (otherwise move to more "generic" datafile) - why? why not?
update - note include/allow dot (.) too
  BUT NOT as first character!!! (e.g. exclude .confg.txt !!!)
            e.g. 2024-25/at.1.txt
                     change to at_1 or uefa_cl or such - why? why not?
%r{ (?:   ## "classic"  variant i)  with season folder
      (?: ^|/ )      # beginning (^) or beginning of path (/)
       #{SEASON}
         /
       [a-z0-9][a-z0-9_.-]*\.txt$  ## txt e.g /1-premierleague.txt
    )
     |
    (?:  ## "compact" variant ii) with season in filename
       (?: ^|/ )      # beginning (^) or beginning of path (/)
        (?:  (?<season>\d{4}-\d{2}) 
                | 
             (?<season>\d{4})
         )
         _  ## allow more than one underscore - why? why not?
        [a-z0-9][a-z0-9_.-]*\.txt$           
    )
}x

Class Method Summary collapse

Class Method Details

._expand(arg) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fbtok/opts.rb', line 90

def self._expand( arg )
    ## check if directory
    if Dir.exist?( arg )
          datafiles = _find( arg )
          if debug?
            puts
            puts "  found #{datafiles.size} match txt datafiles in #{arg}"
            pp datafiles
          end
          datafiles
    else ## assume it's a file
        ## make sure path exists; raise error if not 
        if File.exist?( arg )
          [arg]   ## note - always return an array - why? why not?
        else    
          raise Errno::ENOENT, "No such file or directory - #{arg}" 
        end
    end
end

._find(path, seasons: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fbtok/opts.rb', line 52

def self._find( path, seasons: nil )
    ## check - rename dir
    ##          use root_dir or work_dir or cd or such - why? why not?

    datafiles = []

    ## note: normalize path - use File.expand_path ??
    ##    change all backslash to slash for now
    ## path = path.gsub( "\\", '/' )
    path =  File.expand_path( path )
          
    if seasons && seasons.size > 0
       ## norm seasons
       seasons = seasons.map {|season| Season(season) }
    end


    ## check all txt files
    ## note: incl. files starting with dot (.)) as candidates
    ##     (normally excluded with just *)
    candidates = Dir.glob( "#{path}/**/{*,.*}.txt" )
    ## pp candidates
    candidates.each do |candidate|
       if m=MATCH_RE.match( candidate )
          if seasons && seasons.size > 0   ## check for seasons filter 
            season = Season.parse(m[:season])
            datafiles << candidate   if seasons.include?( season )   
          else
            datafiles << candidate 
          end  
       end
    end

    ## pp datafiles
    datafiles
end

.build_pathspec(paths:) ⇒ Object



128
129
130
# File 'lib/fbtok/opts.rb', line 128

def self.build_pathspec( paths: )
    PathspecNode.new( paths: paths, rec: {} )
end

.debug=(value) ⇒ Object



10
# File 'lib/fbtok/opts.rb', line 10

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

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


11
# File 'lib/fbtok/opts.rb', line 11

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

.expand_args(args) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/fbtok/opts.rb', line 111

def self.expand_args( args )
  paths = []
  args.each do |arg|
    paths += _expand( arg )
  end
  paths 
end

.read_pathspecs(src) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fbtok/opts.rb', line 132

def self.read_pathspecs( src )
    specs = []

    recs = read_csv( src )
    pp recs     if debug?
  
    ##  note - make pathspecs relative to passed in file arg!!!
    basedir = File.dirname( src )
    recs.each do |rec|
        path = rec['path']
        fullpath = File.expand_path( path, basedir ) 
        ## make sure path exists; raise error if not
        paths =  if Dir.exist?( fullpath )
                    _find( fullpath )
                 else
                    raise Errno::ENOENT, "No such directory - #{fullpath})" 
                 end
        
        specs << PathspecNode.new( paths: paths, rec: rec )
    end
    specs
end