Module: Fbtxt2json
- Defined in:
- lib/fbtxt2json/fbtxt2json.rb
Class Method Summary collapse
- .do_dirs(paths, outdir: nil, seasons: [], write_summary: false) ⇒ Object
- .do_files(paths, outpath: nil) ⇒ Object
- .main(args = ARGV) ⇒ Object
-
.parse(txt) ⇒ Object
check - name parse_txt or txt_to_json or such - why? why not?.
Class Method Details
.do_dirs(paths, outdir: nil, seasons: [], write_summary: false) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/fbtxt2json/fbtxt2json.rb', line 152 def self.do_dirs( paths, outdir: nil, seasons: [], write_summary: false ) ## use a html pre(formatted) text summary = String.new summary << "<pre>\n" summary << "run on #{Time.now.to_s}\n\n" ## add version and such - why? why not? paths.each_with_index do |path,i| puts "==> reading dir [#{i+1}/#{paths.size}] >#{path}<..." summary << "==> [#{i+1}/#{paths.size}] dir #{path}\n" datafiles = SportDb::Pathspec._find( path, seasons: seasons ) pp datafiles puts " #{datafiles.size} datafile(s)" summary << " #{datafiles.size} datafile(s)\n\n" datafiles.each do |datafile| txt = read_text( datafile ) data = parse( txt ) ## add stats to summary page summary << "- #{data['name']} | #{data['matches'].size} match(es) - #{datafile}\n" if outdir ### norm - File.expand_path !!! ## note - use '.' to use (relative to) local directory !!! reldir = File.(File.dirname( path )) ## keep last dir (in relative name) relpath = datafile.sub( reldir+'/', '' ) dir = File.dirname( relpath ) ## puts " reldir = #{reldir}, datafile = #{datafile}" ## puts " relpath = #{relpath}, dir = #{dir}" basename = File.basename( relpath, File.extname(relpath)) outpath = "#{outdir}/#{dir}/#{basename}.json" else dir = File.dirname( datafile ) basename = File.basename( datafile, File.extname(datafile) ) outpath = "#{dir}/#{basename}.json" end puts " writing matches to #{outpath}" write_json( outpath, data ) end end summary << "</pre>" puts summary ## note - do NOT write-out summary if seasons filter is used!!! if outdir && write_summary && seasons.empty? write_text( "#{outdir}/index.html", summary ) end end |
.do_files(paths, outpath: nil) ⇒ Object
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 |
# File 'lib/fbtxt2json/fbtxt2json.rb', line 120 def self.do_files( paths, outpath: nil ) data = nil paths.each_with_index do |path,i| puts "==> reading file [#{i+1}/#{paths.size}] >#{path}<..." txt = read_text( path ) more_data = parse( txt ) if data.nil? data = more_data else ## concat matches ## check if name match up!!! if data['name'] != more_data['name'] raise ArgumentError, "cannot merge matchfiles - league names do NOT match: #{data['name']} != #{more_data['name']}" else data['matches'] += more_data['matches'] end end end pp data puts puts " #{data['matches'].size} match(es)" if outpath puts "==> writing matches to #{outpath}" write_json( outpath, data ) end end |
.main(args = ARGV) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 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 |
# File 'lib/fbtxt2json/fbtxt2json.rb', line 10 def self.main( args=ARGV ) opts = { debug: false, output: nil, summary: false, seasons: [], } parser = OptionParser.new do |parser| parser. = "Usage: #{$PROGRAM_NAME} [options] DATAFILES or DIRS" ## ## check if git has a offline option?? (use same) ## check for other tools - why? why not? # parser.on( "-q", "--quiet", # "less debug output/messages - default is (#{!opts[:debug]})" ) do |debug| # opts[:debug] = false # end parser.on( "--verbose", "--debug", "turn on verbose / debug output (default: #{opts[:debug]})" ) do |debug| opts[:debug] = true end parser.on( "-o PATH", "--output PATH", "output to file / dir" ) do |output| opts[:output] = output end parser.on( "--summary", "(auto-)generate summary (index.html) page (default: #{opts[:summary]})" ) do |summary| opts[:summary] = summary end parser.on( "--seasons SEASONS", "turn on processing only seasons (default: #{!opts[:seasons].empty?})" ) do |seasons| pp seasons seasons = seasons.split( /[, ]/ ) seasons = seasons.map {|season| Season.parse(season) } opts[:seasons] = seasons end end parser.parse!( args ) puts "OPTS:" p opts puts "ARGV:" p args paths = if args.empty? ['/sports/openfootball/euro/2021--europe/euro.txt'] else args end if opts[:debug] SportDb::QuickMatchReader.debug = true SportDb::MatchParser.debug = true else SportDb::QuickMatchReader.debug = false SportDb::MatchParser.debug = false LogUtils::Logger.root.level = :info end ### ## two modes - process directories or concat(enate)d files ## ## check if args is a directory ## dirs = 0 files = 0 paths.each do |path| if Dir.exist?( path ) dirs += 1 elsif File.file?( path ) ## note - strictly File.exist? also checks for dirs (use File.file?)!! files += 1 else ## not a file or dir repprt errr raise ArgumentError, "file/dir does NOT exist - #{path}" end end if dirs > 0 && files > 0 raise ArgumentError, "#{files} file(s), #{dirs} dir(s) - can only process dirs or files but NOT both; sorry" end if files > 0 do_files( paths, outpath: opts[:output] ) elsif dirs > 0 do_dirs( paths, outdir: opts[:output], seasons: opts[:seasons], write_summary: opts[:summary] ) else ## do nothing; no args end puts "bye" end |
.parse(txt) ⇒ Object
check - name parse_txt or txt_to_json or such - why? why not?
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/fbtxt2json/fbtxt2json.rb', line 210 def self.parse( txt ) ### check - name parse_txt or txt_to_json or such - why? why not? quick = SportDb::QuickMatchReader.new( txt ) matches = quick.parse name = quick.league_name ## quick hack - get league+season via league_name data = { 'name' => name, 'matches' => matches.map {|match| match.as_json }} if quick.errors? puts "!! #{quick.errors.size} parse error(s):" pp quick.errors exit 1 end data end |