sportdb-readers 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d2044ec2c7a67046c429f4f52fa78a02d64f42b
4
- data.tar.gz: 4e4e0102a368e5f9b53d8d40681470017fbc8579
3
+ metadata.gz: 4299b02180313d31b3706a9248032471ffff4b23
4
+ data.tar.gz: b11257f793ff6292ad4bb201f3326e719c791360
5
5
  SHA512:
6
- metadata.gz: bf9db195f63e40f376a5bf9a63a7c73c79a6fcaf8dc42bbcc2fd6e18813ec275ccdab4ba5209ff52adba42b98ca7c211e2b172ac844fe2c721c349ceaf3685d7
7
- data.tar.gz: afe05f74d798eb1a0ea04d0d2ed207f858f1d3135a7bfd27eeefadbc55dc8afd8d7eb290dd2652f01394f4a9af335bbc83a7b7f715467e681b2618a86b8c3fca
6
+ metadata.gz: 1e73305335ddcc20ffcfbd064770edb6469b4b3ab62c3c574e76fbdcb3522f42256f3f337e9a07bef5ffb7ed15786f96fd9c04c5267281aa2cba34283e6284fb
7
+ data.tar.gz: e0b4d66842c4e66c7bb818496abc26cf75e2932617c19446bc9a804cdde01437866a1d7e61f5364940c569d3f196aa056592fc6fe349afb45b184914afffb627
data/Manifest.txt CHANGED
@@ -13,5 +13,6 @@ lib/sportdb/readers/match_reader.rb
13
13
  lib/sportdb/readers/version.rb
14
14
  test/helper.rb
15
15
  test/test_match_parser.rb
16
+ test/test_package.rb
16
17
  test/test_read.rb
17
18
  test/test_reader.rb
@@ -25,20 +25,57 @@ class PackageBase
25
25
  def each_conf( &blk ) each( pattern: CONF_RE, &blk ); end
26
26
  def each_match( &blk ) each( pattern: MATCH_RE, &blk ); end
27
27
 
28
- def read_conf( season:, sync: )
29
- read( pattern: CONF_RE ) do |name,txt|
30
- SportDb.parse_conf( season: season, sync: sync )
28
+
29
+ def read_conf( *names,
30
+ season: nil, sync: true )
31
+ if names.empty? ## no (entry) names passed in; read in all
32
+ each_read( pattern: CONF_RE ) do |name, txt|
33
+ SportDb.parse_conf( txt, season: season, sync: sync )
34
+ end
35
+ else
36
+ names.each do |name|
37
+ txt = read_entry( name )
38
+ SportDb.parse_conf( txt, season: season, sync: sync )
39
+ end
31
40
  end
32
41
  end
33
- def read_match( season:, sync: )
34
- read( pattern: MATCH_RE ) do |name, txt|
35
- SportDb.parse_match( season: season, sync: sync )
42
+
43
+ def read_match( *names,
44
+ season: nil, sync: true )
45
+ if names.empty? ## no (entry) names passed in; read in all
46
+ each_read( pattern: MATCH_RE ) do |name, txt|
47
+ SportDb.parse_match( txt, season: season, sync: sync )
48
+ end
49
+ else
50
+ names.each do |name|
51
+ txt = read_entry( name )
52
+ SportDb.parse_match( txt, season: season, sync: sync )
53
+ end
54
+ end
55
+ end
56
+
57
+
58
+ def read( *names,
59
+ season: nil, sync: true )
60
+ if names.empty? ## read all datafiles
61
+ read_conf( season: season, sync: sync )
62
+ read_match( season: season, sync: sync )
63
+ else
64
+ names.each do |name|
65
+ txt = read_entry( name )
66
+ if Datafile.match_conf( name ) ## check if datafile matches conf(iguration) naming (e.g. .conf.txt)
67
+ SportDb.parse_conf( txt, season: season, sync: sync )
68
+ else ## assume "regular" match datafile
69
+ SportDb.parse_match( txt, season: season, sync: sync )
70
+ end
71
+ end
36
72
  end
37
73
  end
38
74
  end # class PackageBase
39
75
 
40
76
 
41
77
 
78
+
42
79
  class DirPackage < PackageBase ## todo/check: find a better name e.g. UnzippedPackage, FilesystemPackage, etc. - why? why not?
43
80
 
44
81
  def initialize( path )
@@ -51,7 +88,7 @@ class DirPackage < PackageBase ## todo/check: find a better name e.g. Unzippe
51
88
  Dir.glob( "#{@path}/**/{*,.*}.txt" ).each do |path|
52
89
  ## todo/fix: (auto) skip and check for directories
53
90
  if pattern.match( path )
54
- yield( path)
91
+ yield( path )
55
92
  else
56
93
  ## puts " skipping >#{path}<"
57
94
  end
@@ -68,7 +105,12 @@ class DirPackage < PackageBase ## todo/check: find a better name e.g. Unzippe
68
105
  end
69
106
  end
70
107
 
71
- def read( pattern: )
108
+ def read_entry( name )
109
+ txt = File.open( "#{@path}/#{name}", 'r:utf-8').read
110
+ txt
111
+ end
112
+
113
+ def each_read( pattern: )
72
114
  each_file( pattern: pattern ) do |path|
73
115
  txt = File.open( path, 'r:utf-8').read
74
116
  yield( path, txt ) ## only pass along txt - why? why not? or pass along entry and not just entry.name?
@@ -97,14 +139,58 @@ class ZipPackage < PackageBase
97
139
  ## puts " skipping >#{entry.name}<"
98
140
  end
99
141
  else
100
- puts "** !! ERROR !! #{entry.name} is unknown zip file type, sorry"
142
+ puts "** !! ERROR !! #{entry.name} is unknown zip file type in >#{@path}<, sorry"
101
143
  exit 1
102
144
  end
103
145
  end
104
146
  end
105
147
  end
106
148
 
107
- def read( pattern: )
149
+
150
+ def match_entry( name )
151
+ ## todo/fix: use Zip::File.glob or find_entry or ? why? why not?
152
+
153
+ pattern = %r{ #{Regexp.escape( name )} ## match string if ends with name
154
+ $
155
+ }x
156
+
157
+ entries = []
158
+ Zip::File.open( @path ) do |zipfile|
159
+ zipfile.each do |entry|
160
+ if entry.directory?
161
+ next ## skip
162
+ elsif entry.file?
163
+ if pattern.match( entry.name )
164
+ entries << entry
165
+ end
166
+ else
167
+ puts "** !! ERROR !! #{entry.name} is unknown zip file type in >#{@path}<, sorry"
168
+ exit 1
169
+ end
170
+ end
171
+ end
172
+ entries
173
+ end
174
+
175
+ def read_entry( name )
176
+ entries = match_entry( name )
177
+ if entries.empty?
178
+ puts "** !!! ERROR !!! zip entry >#{name}< not found in >#{@path}<; sorry"
179
+ exit 1
180
+ elsif entries.size > 1
181
+ puts "** !!! ERROR !!! ambigious zip entry >#{name}<; found #{entries.size} entries in >#{@path}<:"
182
+ pp entries
183
+ exit 1
184
+ else
185
+ entry = entries[0]
186
+ txt = entry.get_input_stream.read
187
+ ## puts "** encoding: #{txt.encoding}" #=> encoding: ASCII-8BIT
188
+ txt = txt.force_encoding( Encoding::UTF_8 )
189
+ end
190
+ end
191
+
192
+
193
+ def each_read( pattern: )
108
194
  each( pattern: pattern ) do |entry|
109
195
  txt = entry.get_input_stream.read
110
196
  ## puts "** encoding: #{txt.encoding}" #=> encoding: ASCII-8BIT
@@ -6,7 +6,7 @@ module Readers
6
6
 
7
7
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
8
8
  MINOR = 3
9
- PATCH = 0
9
+ PATCH = 1
10
10
  VERSION = [MAJOR,MINOR,PATCH].join('.')
11
11
 
12
12
  def self.version
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_package.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+ class TestPackage < MiniTest::Test
12
+
13
+ def test_read
14
+ eng = Datafile::DirPackage.new( '../../../openfootball/england' )
15
+ ## eng = Datafile::ZipPackage.new( 'tmp/england-master.zip' )
16
+ assert eng.read_entry( '2015-16/.conf.txt' ).start_with?( '= English Premier League 2015/16' )
17
+ assert eng.read_entry( '2017-18/.conf.txt' ).start_with?( '= English Premier League 2017/18' )
18
+ assert eng.read_entry( '2015-16/1-premierleague-i.txt' ).start_with?( '= English Premier League 2015/16' )
19
+
20
+ at = Datafile::DirPackage.new( '../../../openfootball/austria' )
21
+ ## at = Datafile::ZipPackage.new( 'tmp/austria-master.zip' )
22
+ assert at.read_entry( '2018-19/.conf.txt' ).start_with?( '= Österr. Bundesliga 2018/19' )
23
+ end # method test_read
24
+
25
+ end # class TestPackage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sportdb-readers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-16 00:00:00.000000000 Z
11
+ date: 2019-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sportdb-config
@@ -105,6 +105,7 @@ files:
105
105
  - lib/sportdb/readers/version.rb
106
106
  - test/helper.rb
107
107
  - test/test_match_parser.rb
108
+ - test/test_package.rb
108
109
  - test/test_read.rb
109
110
  - test/test_reader.rb
110
111
  homepage: https://github.com/sportdb/sport.db