Class: SportDb::Pathspec

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

Constant Summary collapse

SEASON_RE =
%r{ (?:
        (?<season>\d{4}-\d{2})
      | (?<season>\d{4})
  )
}x
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?

note - support case-insensitive flag  (e.g. 2025-26/namur/2_Prov_A.txt)
%r{
    ## "classic" variant i)  with season folder
    ##     e.g.  /1930/cup.txt
    (?:
      (?: ^|/ )    # beginning (^) or beginning of path (/)

       #{SEASON_RE}
          (?:  --[a-z0-9_-]+
          )?
         /

        ### note - allow optional directories
        (?:
            [a-z0-9][a-z0-9_-]*
              /
        )*

       [a-z0-9][a-z0-9._-]* \.txt   ## e.g /1-premierleague.txt
         $
    )

      |
    ## "compact" variant ii) with season in filename
    ##         e.g. 1930.txt or 2024_br.txt etc.
    (?:
       (?: ^|/ )      # beginning (^) or beginning of path (/)
         #{SEASON_RE}
         (?:   _   ## allow more than one underscore - why? why not?
            [a-z0-9][a-z0-9._-]*
          )?
            \.txt
            $
    )
}xi

Class Method Summary collapse

Class Method Details

.find(path, seasons: nil) ⇒ Object

Raises:

  • (Errno::ENOENT)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fbtok/pathspec.rb', line 99

def self.find( path, seasons: nil )
    ##
    ## note -  only if seasons filter is turn on
    ##                     MATCH_RE gets used!!!
    ##            otherwise generic **/*.txt
    ##
    ## note -   the ignore/exlude filter always gets used/applied for now


    ## check - rename dir
    ##          use root_dir or work_dir or cd or such - why? why not?

    ## note: normalize path - use File.expand_path ??
    ##    change all backslash to slash for now
    fullpath =  File.expand_path( path )

    ####
    ## note - make sure path exists; raise error if not
    ##   ENOENT => Error No Entity
    raise Errno::ENOENT, "No such directory - #{path})"  unless Dir.exist?( fullpath )


    if seasons && seasons.size > 0
       ## norm seasons (string, integer => Season obj)
       seasons =  seasons.map {|season| Season(season) }
    end


    ## check all txt files
    ## note: incl. files starting with dot (.)) as candidates
    ##     (normally excluded with just *)
    ##  was:  Dir.glob( "#{path}/**/{*,.*}.txt" )

    candidates = Dir.glob( "#{fullpath}/**/*.txt" )
    ## pp candidates


    datafiles = []
    candidates.each do |candidate|

         ## (i) check for (optional) seasons filter
         if seasons && seasons.size > 0
            if m=MATCH_RE.match( candidate )
               next   unless seasons.include?( Season.parse( m[:season] ))
            else
              next    ## note - no season found in filename; skip too
            end
         end


         ## (ii) check for (default/built-in) ignore/excludes
          basename = File.basename( candidate, File.extname( candidate ))
          dirname  = File.dirname( candidate )

          ### exclude basenames with:
          ##   - squad
          ##   - .v2 or .v2603
          ##
          ##    - worldcup/more/1930_squads.txt   =>   squads
          ##    - 2014--brazil/cup.v2.txt
          ##    - 2014--brazil/cup.v260318_164934.txt

          next   if /squad/i.match?( basename )
          next   if /\.v[0-9][0-9_]*/i.match?( basename )

          #####
          ### exclude dirs  with:
          ##    - squad or wiki
          next   if /squad|wiki/i.match?( dirname )


          datafiles << candidate
    end

    ## pp datafiles
    datafiles
end

.path(default = []) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/fbtok/pathspec.rb', line 178

def self.path( default=[])
 ## check for  FBPATH
 ##         or FBTXT_PATH
      path = ENV['FBPATH'] || ENV['FBTXT_PATH']
      if path
          path.split( /[:;]/)
      else
         default
      end
end