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?
%r{
    ## "classic" variant i)  with season folder
    ##     e.g.  /1930/cup.txt
    (?:
      (?: ^|/ )    # beginning (^) or beginning of path (/)

       #{SEASON_RE}
          (?:  --[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
            $
    )
}x

Class Method Summary collapse

Class Method Details

._find(path, seasons: nil) ⇒ Object

todo/check - rename to glob or expand or such - why? why not?

Raises:

  • (Errno::ENOENT)


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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fbtok/pathspec.rb', line 61

def self._find( path, seasons: nil )
    ## 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
    ## path = path.gsub( "\\", '/' )
    path =  File.expand_path( path )

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


    if seasons && seasons.size > 0
       seasons = _norm_seasons( seasons )    ## norm seasons (string, integer => Season obj)
    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( "#{path}/**/*.txt" )
    ## pp candidates


    datafiles = []
    candidates.each do |candidate|
       if m = MATCH_RE.match( candidate )

          ## check for seasons filter
          next   if seasons && seasons.size > 0 &&
                    !seasons.include?( Season.parse( m[:season] ))

          ### exclude squad
          ##   and .v2 or .v2603
          ##
          ##    - worldcup/more/1930_squads.txt   =>   squads
          ##    - 2014--brazil/cup.v2.txt",
          ##    - 2014--brazil/cup.v260318_164934.txt",

          basename = File.basename( candidate, File.extname( candidate ))

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


          datafiles << candidate
       end
    end

    ## pp datafiles
    datafiles
end

._norm_seasons(seasons) ⇒ Object



55
56
57
# File 'lib/fbtok/pathspec.rb', line 55

def self._norm_seasons( seasons )
    seasons.map {|season| Season(season) }
end

.debug=(value) ⇒ Object



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

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

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


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

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

.read(src) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fbtok/pathspec.rb', line 123

def self.read( src )
    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 )
        datafiles =   _find( fullpath )

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

    recs
end