fide_xml_parser 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/fide_xml_parser.gemspec +2 -3
- data/lib/fide_xml_parser/fide_processor.rb +23 -0
- data/lib/fide_xml_parser/json_writer.rb +4 -2
- data/lib/fide_xml_parser/processor.rb +41 -24
- data/lib/fide_xml_parser/rating_combiner.rb +9 -0
- data/lib/fide_xml_parser/version.rb +1 -1
- metadata +9 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16cf18729c304b19bc521574841ad100b5aed0b9bd95dedb93e7e6474b83d251
|
4
|
+
data.tar.gz: 10b3e2bce46890eaee461199ccda229a0cec85e11b03a1d373f361c37b47fe24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66f43b7384936aff168df5275af752bf050a3ab9205c1dec927e410df187f904cb6e58efdb0a2456cb5f9ad27b1b765084b252e3644c7b8a7ea92a9db49e9524
|
7
|
+
data.tar.gz: '08ceb66194d7b95ee64cc6cc54759f7662aac2555ff6c00742c37142c91fccc0e54abc87c37c4c8e1d2eacf0e189436bd005c5bc1c19322fc610c62e95b64cc8'
|
data/CHANGELOG.md
CHANGED
@@ -4,4 +4,10 @@ First release.
|
|
4
4
|
|
5
5
|
### 0.2.0
|
6
6
|
|
7
|
-
* API change: change last 2 write method arguments to keyword arguments.
|
7
|
+
* API change: change last 2 write method arguments to keyword arguments.
|
8
|
+
|
9
|
+
### 0.3.0
|
10
|
+
|
11
|
+
* Parser can now be configured with array name, record name, and numeric field list.
|
12
|
+
* Interface has changed, must instantiate FideParser for FIDE files.
|
13
|
+
* Field name renames (renaming) can now be done.
|
data/fide_xml_parser.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
|
18
18
|
spec.metadata["homepage_uri"] = spec.homepage
|
19
19
|
spec.metadata["source_code_uri"] = spec.homepage
|
20
|
-
|
20
|
+
spec.metadata["changelog_uri"] = 'https://github.com/keithrbennett/fide-xml-parser/blob/master/CHANGELOG.md'
|
21
21
|
|
22
22
|
# Specify which files should be added to the gem when it is released.
|
23
23
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -29,11 +29,10 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.require_paths = ["lib"]
|
30
30
|
|
31
31
|
spec.add_dependency "nokogiri", "~>1.10"
|
32
|
-
spec.add_dependency "tty-cursor", "~> 0.7"
|
33
32
|
spec.add_dependency "pry", "~> 0.12"
|
34
33
|
spec.add_dependency "awesome_print", "~> 1.8"
|
35
34
|
|
36
35
|
spec.add_development_dependency "bundler", "~> 2.0"
|
37
|
-
spec.add_development_dependency "rake", "
|
36
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
38
37
|
spec.add_development_dependency "rspec", "~> 3.0"
|
39
38
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'processor'
|
2
|
+
|
3
|
+
module FideXmlParser
|
4
|
+
|
5
|
+
class FideProcessor < Processor
|
6
|
+
|
7
|
+
NUMERIC_FIELDS = %w[
|
8
|
+
k
|
9
|
+
blitz_k
|
10
|
+
rapid_k
|
11
|
+
rating
|
12
|
+
blitz_rating
|
13
|
+
rapid_rating
|
14
|
+
games
|
15
|
+
blitz_games
|
16
|
+
rapid_games
|
17
|
+
].map(&:freeze)
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
super('playerslist', 'player', NUMERIC_FIELDS)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -5,12 +5,13 @@ module FideXmlParser
|
|
5
5
|
class JsonWriter
|
6
6
|
|
7
7
|
attr_reader :parser
|
8
|
-
attr_accessor :key_filter, :record_filter
|
8
|
+
attr_accessor :key_filter, :record_filter, :field_name_renames
|
9
9
|
|
10
10
|
|
11
11
|
def initialize
|
12
12
|
@key_filter = nil
|
13
13
|
@record_filter = nil
|
14
|
+
@field_name_renames = nil
|
14
15
|
end
|
15
16
|
|
16
17
|
|
@@ -57,9 +58,10 @@ class JsonWriter
|
|
57
58
|
# Default json_filespec will be constructed from the input file, just replacing 'xml' with 'json'.
|
58
59
|
private
|
59
60
|
def write_private(input_filespec, json_mode: :pretty, json_filespec: nil)
|
60
|
-
@parser = FideXmlParser::
|
61
|
+
@parser = FideXmlParser::FideProcessor.new
|
61
62
|
parser.key_filter = key_filter
|
62
63
|
parser.record_filter = record_filter
|
64
|
+
parser.field_name_renames = field_name_renames
|
63
65
|
records = parser.parse(File.new(input_filespec))
|
64
66
|
json_text = (json_mode == :pretty) ? JSON.pretty_generate(records) : records.to_json
|
65
67
|
json_filespec ||= input_filespec.sub(/\.xml$/, '.json')
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'awesome_print'
|
2
2
|
require 'nokogiri'
|
3
|
-
require 'tty-cursor'
|
4
3
|
|
5
4
|
module FideXmlParser
|
6
5
|
|
@@ -18,27 +17,38 @@ module FideXmlParser
|
|
18
17
|
# and returns true to include it or false to exclude it,
|
19
18
|
# e.g. to include only records with a "title", do this:
|
20
19
|
# processor.record_filter = ->(rec) { rec.title }
|
20
|
+
# If a field name has been changed via the field_name_renames hash, the new name should be used in the filter.
|
21
|
+
|
22
|
+
# A field_name_renames hash can be provided.
|
23
|
+
# Keys are the field names in the XML input, values are the names in the output JSON, e.g.:
|
24
|
+
# {
|
25
|
+
# 'rating' => 'standard_rating',
|
26
|
+
# 'games' => 'standard_games'
|
27
|
+
# }
|
28
|
+
|
29
|
+
|
21
30
|
class Processor < Nokogiri::XML::SAX::Document
|
22
31
|
|
23
32
|
attr_reader :start_time
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
33
|
+
|
34
|
+
# Constructor parameters:
|
35
|
+
attr_accessor :numeric_fields, :array_name, :record_name
|
36
|
+
|
37
|
+
# User-provided callbacks:
|
38
|
+
attr_accessor :key_filter, :record_filter, :field_name_renames
|
39
|
+
|
40
|
+
# For internal use:
|
41
|
+
attr_accessor :current_property_name, :record, :records, :input_record_count, :output_record_count
|
42
|
+
|
43
|
+
ANSI_GO_TO_LINE_START = "\033[1G"
|
44
|
+
|
45
|
+
def initialize(array_name, record_name, numeric_fields)
|
46
|
+
@array_name = array_name
|
47
|
+
@record_name = record_name
|
48
|
+
@numeric_fields = numeric_fields
|
40
49
|
@key_filter = nil
|
41
50
|
@record_filter = nil
|
51
|
+
@field_name_renames = nil
|
42
52
|
@current_property_name = nil
|
43
53
|
@record = {}
|
44
54
|
@records = []
|
@@ -62,7 +72,7 @@ class Processor < Nokogiri::XML::SAX::Document
|
|
62
72
|
|
63
73
|
|
64
74
|
def output_status
|
65
|
-
print
|
75
|
+
print ANSI_GO_TO_LINE_START
|
66
76
|
print "Records processed: %9d kept: %9d Seconds elapsed: %11.2f" % [
|
67
77
|
input_record_count,
|
68
78
|
output_record_count,
|
@@ -73,9 +83,9 @@ class Processor < Nokogiri::XML::SAX::Document
|
|
73
83
|
|
74
84
|
def start_element(name, _attrs)
|
75
85
|
case name
|
76
|
-
when
|
86
|
+
when array_name
|
77
87
|
# ignore
|
78
|
-
when
|
88
|
+
when record_name
|
79
89
|
self.input_record_count += 1
|
80
90
|
output_status if input_record_count % 1000 == 0
|
81
91
|
else # this is a field in the players record; process it as such
|
@@ -86,9 +96,9 @@ class Processor < Nokogiri::XML::SAX::Document
|
|
86
96
|
|
87
97
|
def end_element(name)
|
88
98
|
case name
|
89
|
-
when
|
99
|
+
when array_name # end of data, write JSON file
|
90
100
|
finish
|
91
|
-
when
|
101
|
+
when record_name
|
92
102
|
if record_filter.nil? || record_filter.(record)
|
93
103
|
self.output_record_count += 1
|
94
104
|
records << record
|
@@ -103,8 +113,15 @@ class Processor < Nokogiri::XML::SAX::Document
|
|
103
113
|
def characters(string)
|
104
114
|
if current_property_name
|
105
115
|
if key_filter.nil? || key_filter.(current_property_name)
|
106
|
-
value =
|
107
|
-
|
116
|
+
value = numeric_fields.include?(current_property_name) ? Integer(string) : string
|
117
|
+
key = current_property_name
|
118
|
+
if field_name_renames
|
119
|
+
new_field_name = field_name_renames[key]
|
120
|
+
if new_field_name
|
121
|
+
key = new_field_name
|
122
|
+
end
|
123
|
+
end
|
124
|
+
record[key] = value
|
108
125
|
end
|
109
126
|
end
|
110
127
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fide_xml_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keith Bennett
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.10'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: tty-cursor
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0.7'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0.7'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: pry
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -84,16 +70,16 @@ dependencies:
|
|
84
70
|
name: rake
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
|
-
- - "
|
73
|
+
- - ">="
|
88
74
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
75
|
+
version: 12.3.3
|
90
76
|
type: :development
|
91
77
|
prerelease: false
|
92
78
|
version_requirements: !ruby/object:Gem::Requirement
|
93
79
|
requirements:
|
94
|
-
- - "
|
80
|
+
- - ">="
|
95
81
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
82
|
+
version: 12.3.3
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: rspec
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,8 +112,10 @@ files:
|
|
126
112
|
- Rakefile
|
127
113
|
- fide_xml_parser.gemspec
|
128
114
|
- lib/fide_xml_parser.rb
|
115
|
+
- lib/fide_xml_parser/fide_processor.rb
|
129
116
|
- lib/fide_xml_parser/json_writer.rb
|
130
117
|
- lib/fide_xml_parser/processor.rb
|
118
|
+
- lib/fide_xml_parser/rating_combiner.rb
|
131
119
|
- lib/fide_xml_parser/version.rb
|
132
120
|
homepage: https://github.com/keithrbennett/fide-xml-parser
|
133
121
|
licenses:
|
@@ -136,6 +124,7 @@ metadata:
|
|
136
124
|
allowed_push_host: https://rubygems.org
|
137
125
|
homepage_uri: https://github.com/keithrbennett/fide-xml-parser
|
138
126
|
source_code_uri: https://github.com/keithrbennett/fide-xml-parser
|
127
|
+
changelog_uri: https://github.com/keithrbennett/fide-xml-parser/blob/master/CHANGELOG.md
|
139
128
|
post_install_message:
|
140
129
|
rdoc_options: []
|
141
130
|
require_paths:
|