data_gov_au 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +15 -0
- data/README.md +111 -6
- data/TODO +3 -4
- data/data_gov_au.gemspec +3 -0
- data/exe/data_gov_au +53 -3
- data/lib/DataGovAu/ABR/Error.rb +4 -1
- data/lib/DataGovAu/ABR/Extract.rb +48 -0
- data/lib/DataGovAu/ABR/Fetch.rb +5 -4
- data/lib/DataGovAu/ABR/Parser.rb +181 -0
- data/lib/DataGovAu/ABR/Release.rb +7 -0
- data/lib/DataGovAu/ABR.rb +2 -0
- data/lib/DataGovAu/VERSION.rb +1 -1
- data/lib/Thoran/Array/NaturalSort/natural_sort.rb +27 -0
- data/lib/Thoran/String/NaturalComparison/natural_comparison.rb +37 -0
- data/test/DataGovAu/ABR/Extract_test.rb +90 -0
- data/test/DataGovAu/ABR/Fetch_test.rb +10 -0
- data/test/DataGovAu/ABR/Parser_test.rb +194 -0
- data/test/Thoran/Array/NaturalSort/natural_sort_test.rb +21 -0
- data/test/Thoran/String/NaturalComparison/natural_comparison_test.rb +35 -0
- data/test/exe/data_gov_au_test.rb +94 -0
- data/test/fixtures/20261020Public01.xml +225 -0
- data/test/helper.rb +20 -0
- metadata +167 -118
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f723a194bf01ee073e8eab9719e0f884af8681c108cb57dc12a2cc5e7291d85c
|
|
4
|
+
data.tar.gz: 98be987f09d6cd2a5ff734c0e42e9dd2527ede14844b2041d261fbb79a0074b8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ccb74c80f91d5c5a1fcaa4309237b479e5ab858758107ac687fc496b8ccbe7794c937819403056a809379c3ccf4d728a8b322d9cbad5aa0ce04dc78a988db16f
|
|
7
|
+
data.tar.gz: 1eff4edd9fdd31f54f66faabc9161df739dd735c7bb86dfebe2d2d6156dcee78a6627979911ecdb28ee3a0f69abb6b582946a25512ca9c90b6ca57da24b0ebda
|
data/CHANGELOG
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
## 20261103
|
|
4
4
|
|
|
5
|
+
0.2.0: + DataGovAu::ABR::Parser, DataGovAu::ABR::Extract: Records are read directly from a downloaded release's zips.
|
|
6
|
+
|
|
7
|
+
1. + lib/DataGovAu/ABR/Parser.rb: One XML file read as a stream and given up a record at a time. Ox rather than Nokogiri: Over a whole release, building every record, Ox's SAX parser takes 7 m 34 s where Nokogiri's takes 12 m 32 s. An element present and empty is kept apart from an element absent, the extract having both: A blank State comes back as the blank it is where an absent ASICNumber comes back as nil. An entity's own endorsement and each named fund it runs are kept apart, the extract putting both in DGR elements of the same name, the first carrying a status and no name and the second a name and no status. An individual's name is given up in its parts, joining being a decision for whoever asks. + error, + error_free?(), from the error attribute the schema requires of each file. + whole?(), which sets what a file gave up against the RecordCount it carries and wants its error to be none, so that a truncated file, or one reporting an error, is not taken for a smaller week.
|
|
8
|
+
2. + lib/DataGovAu/ABR/Extract.rb: A member is inflated by rubyzip as it is read and Ox is handed that stream, so neither a 477 MB zip nor one of the 605 MB XML files inside it is ever held whole. + members(), which orders the files by the numbers their names carry rather than by the names of the zips: The records are one stream sorted by ABN and the zips cut across it, so public_split_11_20 sorts first while holding the later half. The numbers are compared as numbers, since unpadded their characters would put file 2 after file 19, and the records would stop being one ascending run without anything in them saying so.
|
|
9
|
+
3. + lib/Thoran/String/NaturalComparison/natural_comparison.rb, lib/Thoran/Array/NaturalSort/natural_sort.rb: Taken from String#natural_comparison 0.0.1 and Array#natural_sort 0.0.1, and namespaced under Thoran. Required relatively rather than by name, so that another copy on the load path is not what the suite tests.
|
|
10
|
+
4. ~ lib/DataGovAu/ABR/Release.rb: + zip_paths(), read from the directory rather than from the catalogue, so that a release on disk can be parsed without going back to data.gov.au, and natural_sorted.
|
|
11
|
+
5. ~ lib/DataGovAu/ABR/Fetch.rb: Its extract_resources() orders by the numbers in the catalogue's own names, a tenth part otherwise coming between the first and the second. A release is named from the first of them. The comments on supporting_resources() and resources() count the list of resources among what they hold.
|
|
12
|
+
6. ~ lib/DataGovAu/ABR/Error.rb: + NothingToParse.
|
|
13
|
+
7. ~ exe/data_gov_au: + -p/--parse, which parses the newest release held and reports each file against the count it carries and any error it reports or fails to name, going nowhere near the network. + reported_duration(). Its output is written unbuffered, so that each file is reported as it is finished.
|
|
14
|
+
8. ~ data_gov_au(.rb).gemspec: + ox, + rubyzip, + test/fixtures.
|
|
15
|
+
9. + test/fixtures/20261020Public01.xml: Nine records cut from the published schema, validating against it: a sole trader with no business name and one with, a private company with no OtherEntity, an interstate record, a trading trust, a cancelled ABN, a superannuation fund, a record carrying three OtherEntity names and a DGR, and one carrying a blank State and Postcode, a family name standing alone and GST under the status NON. The DGR entries carry a status or a name rather than both together, as the extract does. GST under CAN and NON as well as ACT. An encoded ampersand and an apostrophe in two names. Its error is none, as a release's files report it.
|
|
16
|
+
10. + test/DataGovAu/ABR/Parser_test.rb, Extract_test.rb, test/Thoran/String/NaturalComparison/natural_comparison_test.rb, test/Thoran/Array/NaturalSort/natural_sort_test.rb. ~ test/DataGovAu/ABR/Fetch_test.rb: The halves are ordered by the numbers in their names. ~ test/exe/data_gov_au_test.rb: + Parsing. Where the settings put the data directory is tested through -p. ~ test/helper.rb: + release_holding(), fixture_records(), fixture_record(): 130 runs.
|
|
17
|
+
11. ~ README.md: + Parsing, + What a record comes out as, + On the parsing.
|
|
18
|
+
12. ~ TODO: - The parser.
|
|
19
|
+
|
|
5
20
|
0.1.0: Download the current weekly ABR release.
|
|
6
21
|
|
|
7
22
|
1. + lib/DataGovAu/ABR.rb: + DataGovAu::ABR::ATTRIBUTION, required of any output which leaves the machine, the extract being published under CC BY 3.0 AU.
|
data/README.md
CHANGED
|
@@ -71,7 +71,7 @@ Everything raised is a `DataGovAu::Error`: `CatalogueUnavailable` where the cata
|
|
|
71
71
|
|
|
72
72
|
## The ABN Bulk Extract
|
|
73
73
|
|
|
74
|
-
`DataGovAu::ABR`, built on the catalogue, holds the Australian Business Register's weekly bulk extract: a snapshot of the public ABN register, two zip files of around 477 MB each, holding twenty XML files and some 12 GB uncompressed. It fetches each release and
|
|
74
|
+
`DataGovAu::ABR`, built on the catalogue, holds the Australian Business Register's weekly bulk extract: a snapshot of the public ABN register, two zip files of around 477 MB each, holding twenty XML files and some 12 GB uncompressed. It fetches each release, keeps it, and reads its records.
|
|
75
75
|
|
|
76
76
|
```ruby
|
|
77
77
|
require 'DataGovAu/ABR'
|
|
@@ -83,6 +83,7 @@ Its parts, in the order a release passes through them:
|
|
|
83
83
|
- `Fetch`: the current release found through the catalogue, and whatever of it is not held retrieved.
|
|
84
84
|
- `ExtractDate`: the date the extract carries, read from the first bytes of a zip.
|
|
85
85
|
- `Release`: a release on disk, its directory named for that date.
|
|
86
|
+
- `Extract` and `Parser`: a release's records, read out of its zips.
|
|
86
87
|
|
|
87
88
|
### Why a local copy
|
|
88
89
|
|
|
@@ -110,9 +111,28 @@ held: 0 of 5 files
|
|
|
110
111
|
|
|
111
112
|
Two dates are reported because they differ. A release is named for the date the extract carries itself, read from the names of the files inside its zips; the catalogue's own date is when data.gov.au last touched its record, which moves whether or not the data did. Reading the first is cheap: the name of the first file inside a zip sits in the first few hundred bytes, so it is asked for by range before anything large is retrieved.
|
|
112
113
|
|
|
114
|
+
#### Parsing
|
|
115
|
+
|
|
116
|
+
```shell
|
|
117
|
+
$ data_gov_au abr --parse
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Reads every record of the newest release held, touching nothing on the network. Each file is set against the count it carries itself, so a file which gave up fewer records than it promised is reported rather than passing for a smaller week. So is a file which reports an error, in the `error` attribute the schema requires of each, which is `none` in a file written out whole.
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
release: 20261028
|
|
124
|
+
20261028_Public01.xml 1032000 of 1032000
|
|
125
|
+
...
|
|
126
|
+
20261028_Public20.xml 1023294 of 1023294
|
|
127
|
+
total: 20631294 records in 5 m 48 s
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### Options
|
|
131
|
+
|
|
113
132
|
```
|
|
114
133
|
-d, --data-directory DIR Where every dataset is kept (default: $XDG_DATA_HOME/data_gov_au)
|
|
115
134
|
-n, --dry-run Report what would be retrieved without retrieving it
|
|
135
|
+
-p, --parse Parse the newest release held, retrieving nothing
|
|
116
136
|
-q, --quiet Report nothing but errors
|
|
117
137
|
-h, --help Show this help
|
|
118
138
|
```
|
|
@@ -123,7 +143,7 @@ The command reads `~/.config/data_gov_au/config.yaml`, where present, and its sw
|
|
|
123
143
|
|
|
124
144
|
```yaml
|
|
125
145
|
data_directory: /srv/data_gov_au
|
|
126
|
-
user_agent: data_gov_au/0.
|
|
146
|
+
user_agent: data_gov_au/0.2.0
|
|
127
147
|
```
|
|
128
148
|
|
|
129
149
|
The library reads no file. `DataGovAu::ABR::Config` takes `data_directory:`, `api_url:` and `user_agent:` as keywords or through its setters, and yields itself to a block. The API's URL and the user agent default as the catalogue's do. The data directory has no default, and is named as missing where it is first wanted.
|
|
@@ -152,18 +172,27 @@ Each release directory is self-describing: the data, the schema and readme neede
|
|
|
152
172
|
|
|
153
173
|
### From Ruby
|
|
154
174
|
|
|
155
|
-
What the command does, the library does: fetching the current release.
|
|
175
|
+
What the command does, the library does: fetching the current release, and reading the records of one held.
|
|
156
176
|
|
|
157
177
|
```ruby
|
|
158
178
|
config = DataGovAu::ABR::Config.new(data_directory: '/srv/data_gov_au')
|
|
159
179
|
release = DataGovAu::ABR::Fetch.new(config).perform{|resource| puts resource.filename}
|
|
160
|
-
|
|
180
|
+
|
|
181
|
+
DataGovAu::ABR::Extract.new(release).each_record do |record|
|
|
182
|
+
record[:abn] # => "11000047950"
|
|
183
|
+
end
|
|
161
184
|
```
|
|
162
185
|
|
|
163
|
-
|
|
186
|
+
A release already held is had by its date, without going to the catalogue, and each file can be set against the count it carries:
|
|
164
187
|
|
|
165
188
|
```ruby
|
|
166
|
-
DataGovAu::ABR::Release.dates_held(config).
|
|
189
|
+
date = DataGovAu::ABR::Release.dates_held(config).first
|
|
190
|
+
release = DataGovAu::ABR::Release.new(config, date)
|
|
191
|
+
|
|
192
|
+
DataGovAu::ABR::Extract.new(release).each_file do |name, parser|
|
|
193
|
+
parser.each_record{|record| record[:names]}
|
|
194
|
+
parser.whole? # => true, where the file reports no error and gave up as many records as it promised
|
|
195
|
+
end
|
|
167
196
|
```
|
|
168
197
|
|
|
169
198
|
### What the extract holds
|
|
@@ -176,6 +205,82 @@ The twenty XML files are one stream sorted ascending by ABN, within each file an
|
|
|
176
205
|
|
|
177
206
|
Two caveats worth knowing. `ABNStatusFromDate` is the date the *current* status took effect, not necessarily the date of first registration, so a reactivated ABN looks new on that field alone. And the extract carries current information only, with no history: history is what accumulates here, one release at a time.
|
|
178
207
|
|
|
208
|
+
### What a record comes out as
|
|
209
|
+
|
|
210
|
+
Nothing is filtered, derived or discarded on the way in. Every element and attribute the schema allows for is carried through, dates as the strings the extract gives them, so that what is asked of the data later is not settled at ingest.
|
|
211
|
+
|
|
212
|
+
```ruby
|
|
213
|
+
{
|
|
214
|
+
abn: '11000047950', abn_status: 'ACT', abn_status_from_date: '20000224',
|
|
215
|
+
record_last_updated_date: '20260211', replaced: 'N',
|
|
216
|
+
entity_type_code: 'PUB', entity_type_text: 'Australian Public Company',
|
|
217
|
+
state: 'NSW', postcode: '2132',
|
|
218
|
+
asic_number: '000047950', asic_number_type: 'undetermined',
|
|
219
|
+
gst_status: 'ACT', gst_status_from_date: '20000701',
|
|
220
|
+
names: [{type: 'MN', text: 'SYDNEY MISSIONARY & BIBLE COLLEGE'}, {type: 'BN', text: "SMBC's The Bridge"}],
|
|
221
|
+
dgr: [{from_date: '20060301', status: 'ACT', name: nil}],
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
An individual's name comes as its parts rather than joined, joining being a decision for whoever asks: `{type: 'LGL', title: 'MR', given_names: ['WILLIAM', 'RICHARD'], family_name: 'LITT'}`.
|
|
226
|
+
|
|
227
|
+
Three things in that shape are there because the extract makes them matter.
|
|
228
|
+
|
|
229
|
+
An element present and empty is not an element absent. A blank `State` comes back as `''` while an absent `ASICNumber` comes back as `nil`, and the two are not run together.
|
|
230
|
+
|
|
231
|
+
Every record carries a `GST` element, those not registered among them, under the status `NON`. That status is in neither the schema nor the readme: the schema declares `GST/@status` as a plain `xsd:string` where it enumerates `ABN/@status` as `CAN|ACT`, and the readme says only that a record has "zero or one GST status". `NON` is known from counting the data, where it is the commonest of the three. A record with no `GST` element at all is a fourth case, so `nil` is kept distinct from `'NON'`.
|
|
232
|
+
|
|
233
|
+
An entity's own gift-deductible endorsement and each named fund it runs arrive as `DGR` elements of the same name. The first carries a status and no name:
|
|
234
|
+
|
|
235
|
+
```xml
|
|
236
|
+
<DGR status="ACT" DGRStatusFromDate="20060301" />
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
The second carries a name and no status:
|
|
240
|
+
|
|
241
|
+
```xml
|
|
242
|
+
<DGR DGRStatusFromDate="20000701">
|
|
243
|
+
<NonIndividualName type="DGR">
|
|
244
|
+
<NonIndividualNameText>SYDNEY MISSIONARY & BIBLE COLLEGE BUILDING & MAINTENANCE FUND</NonIndividualNameText>
|
|
245
|
+
</NonIndividualName>
|
|
246
|
+
</DGR>
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Both of those are from one record, ABN 11000047950, which has its own endorsement and runs one named fund. They mean different things, so they are kept apart rather than collapsed into one flag and one name.
|
|
250
|
+
|
|
251
|
+
A company's own name is a name of type `MN` and sits on `MainEntity`, while business and trading names are types `BN` and `TRD` and sit on `OtherEntity`. Asking whether a record has a business name is therefore not the same question as asking whether it has a name.
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
### On the parsing
|
|
255
|
+
|
|
256
|
+
Ox, over the stream rubyzip inflates as it is read, so neither a 477 MB zip nor one of the 605 MB XML files inside it is ever held whole.
|
|
257
|
+
|
|
258
|
+
Three parsers were first ranked on identical work — every record counted and four fields taken from each — over a 150 MB cut of one real file:
|
|
259
|
+
|
|
260
|
+
```
|
|
261
|
+
nokogiri sax 5.5-8.1 s 18-26 MB/s
|
|
262
|
+
nokogiri reader 2.4-3.8 s 37-59 MB/s
|
|
263
|
+
ox sax 1.4-2.0 s 73-100 MB/s
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
A rate over a cut file is not a runtime, though. Run it end to end instead — out of the zip, building the whole record rather than four fields of it — over a whole release, the same work either way:
|
|
267
|
+
|
|
268
|
+
```
|
|
269
|
+
ox sax 7 m 34 s 26.7 MB/s
|
|
270
|
+
nokogiri sax 12 m 32 s 16.1 MB/s
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Both gave up 20,631,294 records. Inflating the zip and building the records are in that path too, so the gap closes from three to five times down to about twice: 1.7 times here, 1.9 on a single file measured back to back. Twice is worth having weekly; five times was never on offer.
|
|
274
|
+
|
|
275
|
+
Those are both slower than the figure the tool itself reports, which is the same Ox work at 5 m 48 s. The spread between 5 m 48 s and 7 m 34 s is run to run, these being runs bound by reading a gigabyte off disk and by the garbage collector rather than by the parser. So: six to eight minutes for a release, and about half what Nokogiri would take.
|
|
276
|
+
|
|
277
|
+
The cost of Ox is memory, which it spends to go faster: over one file it finished holding 195 MB against Nokogiri's 115 MB.
|
|
278
|
+
|
|
279
|
+
What does not grow is what is held of the data. No record outlives the block it is given to, and no file or zip is read whole, so 12 GB goes past in 605 MB pieces which are themselves never assembled. The process does settle higher over a release than over a file — one file peaks at 373 MB resident and twenty at 563 MB — but that is Ruby's heap finding its size, not records accumulating.
|
|
280
|
+
|
|
281
|
+
The order the files are read in matters more than it looks. The records are one run sorted ascending by ABN, within each file and across all twenty, which is what lets one release be diffed against another in step rather than held in memory. That ordering is the names' to give, and the names are the ABR's to change: they were `yyyymmddPublicNN.xml` when this was written against the brief and are `yyyymmdd_PublicNN.xml` in the data. Sorted by their characters the order holds only while the numbers stay padded — unpadded, file 2 falls after file 19 and the run stops ascending without anything in the data saying so. So they are sorted by the numbers in them, through a vendored `natural_comparison`.
|
|
282
|
+
|
|
283
|
+
|
|
179
284
|
### Attribution
|
|
180
285
|
|
|
181
286
|
Incorporates or develops data sourced from the Australian Business Register, used under [CC BY 3.0 AU](https://creativecommons.org/licenses/by/3.0/au/). The ABR does not endorse this tool or its output.
|
data/TODO
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# TODO
|
|
2
2
|
|
|
3
|
-
1. DataGovAu::ABR:
|
|
4
|
-
2. DataGovAu::ABR:
|
|
5
|
-
3. DataGovAu::ABR:
|
|
6
|
-
4. DataGovAu::ABR: output, CSV through dsv, carrying the ABR's attribution.
|
|
3
|
+
1. DataGovAu::ABR: storage, every record and every name stored with the dates over which it held, nothing filtered and nothing derived at ingest.
|
|
4
|
+
2. DataGovAu::ABR: querying, every criterion a WHERE clause, none of them applied at ingest.
|
|
5
|
+
3. DataGovAu::ABR: output, CSV through dsv, carrying the ABR's attribution.
|
data/data_gov_au.gemspec
CHANGED
|
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
|
|
|
34
34
|
Dir['exe/*'],
|
|
35
35
|
Dir['lib/**/*.rb'],
|
|
36
36
|
Dir['test/**/*.rb'],
|
|
37
|
+
Dir['test/fixtures/*'],
|
|
37
38
|
'CHANGELOG',
|
|
38
39
|
'Gemfile',
|
|
39
40
|
'LICENSE',
|
|
@@ -44,6 +45,8 @@ Gem::Specification.new do |spec|
|
|
|
44
45
|
|
|
45
46
|
spec.runtime_dependencies = [
|
|
46
47
|
'http.rb',
|
|
48
|
+
'ox',
|
|
49
|
+
'rubyzip',
|
|
47
50
|
'switches.rb',
|
|
48
51
|
]
|
|
49
52
|
|
data/exe/data_gov_au
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# data_gov_au
|
|
3
3
|
|
|
4
4
|
# 20261103
|
|
5
|
-
# 0.
|
|
5
|
+
# 0.2.0
|
|
6
6
|
|
|
7
7
|
require 'switches.rb'
|
|
8
8
|
require 'yaml'
|
|
@@ -14,6 +14,7 @@ BYTES_PER_KILOBYTE = 1024
|
|
|
14
14
|
BYTES_PER_MEGABYTE = 1_048_576
|
|
15
15
|
CONFIG_PATH = '~/.config/data_gov_au/config.yaml'
|
|
16
16
|
DATASETS = ['abr']
|
|
17
|
+
SECONDS_PER_MINUTE = 60
|
|
17
18
|
SETTINGS = [:api_url, :data_directory, :user_agent]
|
|
18
19
|
|
|
19
20
|
def usage
|
|
@@ -27,6 +28,7 @@ def usage
|
|
|
27
28
|
puts "Options:"
|
|
28
29
|
puts " -d, --data-directory DIR Where every dataset is kept (default: $XDG_DATA_HOME/data_gov_au)"
|
|
29
30
|
puts " -n, --dry-run Report what would be retrieved without retrieving it"
|
|
31
|
+
puts " -p, --parse Parse the newest release held, retrieving nothing"
|
|
30
32
|
puts " -q, --quiet Report nothing but errors"
|
|
31
33
|
puts " -h, --help Show this help"
|
|
32
34
|
end
|
|
@@ -35,6 +37,7 @@ def switches
|
|
|
35
37
|
@switches ||= Switches.new do |s|
|
|
36
38
|
s.optional(:d, :data_directory){'Where every dataset is kept (default: $XDG_DATA_HOME/data_gov_au)'}
|
|
37
39
|
s.boolean(:n, :dry_run){'Report what would be retrieved without retrieving it'}
|
|
40
|
+
s.boolean(:p, :parse){'Parse the newest release held, retrieving nothing'}
|
|
38
41
|
s.boolean(:q, :quiet){'Report nothing but errors'}
|
|
39
42
|
s.perform(:h, :help?) do
|
|
40
43
|
usage
|
|
@@ -85,6 +88,15 @@ def fetch
|
|
|
85
88
|
@fetch ||= DataGovAu::ABR::Fetch.new(config)
|
|
86
89
|
end
|
|
87
90
|
|
|
91
|
+
# The newest release on disk, which is read from the directory names and so costs nothing: parsing goes nowhere near the network.
|
|
92
|
+
def held_release
|
|
93
|
+
@held_release ||= (
|
|
94
|
+
date = DataGovAu::ABR::Release.dates_held(config).first
|
|
95
|
+
raise DataGovAu::ABR::NothingToParse, "no release is held in #{config.releases_directory}" unless date
|
|
96
|
+
DataGovAu::ABR::Release.new(config, date)
|
|
97
|
+
)
|
|
98
|
+
end
|
|
99
|
+
|
|
88
100
|
def report(message)
|
|
89
101
|
puts message unless switches.quiet?
|
|
90
102
|
end
|
|
@@ -95,6 +107,13 @@ def reported_size(size)
|
|
|
95
107
|
"#{(size.to_f / BYTES_PER_KILOBYTE).round} kB"
|
|
96
108
|
end
|
|
97
109
|
|
|
110
|
+
# Rounded before it is divided, so that no duration reads as 60 seconds.
|
|
111
|
+
def reported_duration(seconds)
|
|
112
|
+
whole_seconds = seconds.round
|
|
113
|
+
return "#{whole_seconds} s" if whole_seconds < SECONDS_PER_MINUTE
|
|
114
|
+
"#{whole_seconds / SECONDS_PER_MINUTE} m #{whole_seconds % SECONDS_PER_MINUTE} s"
|
|
115
|
+
end
|
|
116
|
+
|
|
98
117
|
def describe(resource)
|
|
99
118
|
" #{resource.filename} (#{reported_size(resource.size)})"
|
|
100
119
|
end
|
|
@@ -112,6 +131,37 @@ def retrieve_release
|
|
|
112
131
|
fetch.perform{|resource| report(" #{resource.filename}")}
|
|
113
132
|
end
|
|
114
133
|
|
|
134
|
+
def fetch_release
|
|
135
|
+
report_release
|
|
136
|
+
retrieve_release unless switches.dry_run?
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Each file is set against the count it carries itself, so that a file which gave up fewer records than it promised, or which reports an error, is reported rather than passing as a smaller week.
|
|
140
|
+
def parse_release
|
|
141
|
+
report("release: #{held_release}")
|
|
142
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
143
|
+
records = 0
|
|
144
|
+
not_whole = []
|
|
145
|
+
DataGovAu::ABR::Extract.new(held_release).each_file do |member, parser|
|
|
146
|
+
parser.each_record{}
|
|
147
|
+
records += parser.records_parsed
|
|
148
|
+
report(file_report(member, parser))
|
|
149
|
+
not_whole << member unless parser.whole?
|
|
150
|
+
end
|
|
151
|
+
report("total: #{records} records in #{reported_duration(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started)}")
|
|
152
|
+
report_not_whole(not_whole) unless not_whole.empty?
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def file_report(member, parser)
|
|
156
|
+
line = " #{member} #{parser.records_parsed} of #{parser.record_count}"
|
|
157
|
+
return line if parser.error_free?
|
|
158
|
+
"#{line} error: #{parser.error || 'not given'}"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def report_not_whole(members)
|
|
162
|
+
report_failure("fewer records than promised, or an error reported, in #{members.join(', ')}")
|
|
163
|
+
end
|
|
164
|
+
|
|
115
165
|
def report_failure(message)
|
|
116
166
|
$stderr.puts "data_gov_au: #{message}"
|
|
117
167
|
exit 1
|
|
@@ -123,10 +173,10 @@ def check_dataset
|
|
|
123
173
|
end
|
|
124
174
|
|
|
125
175
|
def main
|
|
176
|
+
$stdout.sync = true
|
|
126
177
|
switches
|
|
127
178
|
check_dataset
|
|
128
|
-
|
|
129
|
-
retrieve_release unless switches.dry_run?
|
|
179
|
+
switches.parse? ? parse_release : fetch_release
|
|
130
180
|
rescue DataGovAu::Error => error
|
|
131
181
|
report_failure(error.message)
|
|
132
182
|
end
|
data/lib/DataGovAu/ABR/Error.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# DataGovAu/ABR/Error.rb
|
|
2
|
-
# DataGovAu::ABR::ExtractUndated
|
|
2
|
+
# DataGovAu::ABR::ExtractUndated, NothingToParse
|
|
3
3
|
|
|
4
4
|
require_relative '../Error'
|
|
5
5
|
|
|
@@ -7,5 +7,8 @@ module DataGovAu
|
|
|
7
7
|
module ABR
|
|
8
8
|
# A zip did not name the date of the extract it holds, so the release cannot be named.
|
|
9
9
|
class ExtractUndated < Error; end
|
|
10
|
+
|
|
11
|
+
# No release is held, or the one held has no zip files, so there is nothing to parse.
|
|
12
|
+
class NothingToParse < Error; end
|
|
10
13
|
end
|
|
11
14
|
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# DataGovAu/ABR/Extract.rb
|
|
2
|
+
# DataGovAu::ABR::Extract
|
|
3
|
+
|
|
4
|
+
require 'zip'
|
|
5
|
+
|
|
6
|
+
require_relative './Error'
|
|
7
|
+
require_relative './Parser'
|
|
8
|
+
require_relative '../../Thoran/String/NaturalComparison/natural_comparison'
|
|
9
|
+
|
|
10
|
+
module DataGovAu
|
|
11
|
+
module ABR
|
|
12
|
+
# A release's records, read out of the zips as they lie. rubyzip inflates a member as it is read and Ox is handed that stream, so neither a 477 MB zip nor one of the 605 MB XML files inside it is ever held whole.
|
|
13
|
+
class Extract
|
|
14
|
+
# Each XML file of the release in turn, as its own parser, so that a caller can report its progress and set what each file gave up against what it promised.
|
|
15
|
+
def each_file
|
|
16
|
+
return enum_for(:each_file) unless block_given?
|
|
17
|
+
raise NothingToParse, "#{@release.directory} holds no zip files" if members.empty?
|
|
18
|
+
members.each do |path, name|
|
|
19
|
+
Zip::File.open(path) do |zip|
|
|
20
|
+
zip.get_entry(name).get_input_stream{|io| yield name, Parser.new(io)}
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def each_record(&block)
|
|
26
|
+
return enum_for(:each_record) unless block
|
|
27
|
+
each_file{|_name, parser| parser.each_record(&block)}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Every XML file of the release in the order the extract was written, which is the order of the sequence numbers its names carry. Sorted by those numbers rather than by the characters of the names, so that the order does not rest on the ABR going on padding them: unpadded, the characters would put file 2 after file 19, and the records would stop being one ascending run without anything in them saying so.
|
|
31
|
+
def members
|
|
32
|
+
@members ||= paths.flat_map{|path|
|
|
33
|
+
Zip::File.open(path){|zip| zip.entries.collect{|entry| [path, entry.name]}}
|
|
34
|
+
}.sort{|(_, a), (_, b)| a.natural_comparison(b)}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def paths
|
|
38
|
+
@paths ||= @release.zip_paths
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def initialize(release)
|
|
44
|
+
@release = release
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/DataGovAu/ABR/Fetch.rb
CHANGED
|
@@ -5,6 +5,7 @@ require_relative '../Catalogue'
|
|
|
5
5
|
require_relative './Error'
|
|
6
6
|
require_relative './ExtractDate'
|
|
7
7
|
require_relative './Release'
|
|
8
|
+
require_relative '../../Thoran/String/NaturalComparison/natural_comparison'
|
|
8
9
|
|
|
9
10
|
module DataGovAu
|
|
10
11
|
module ABR
|
|
@@ -20,17 +21,17 @@ module DataGovAu
|
|
|
20
21
|
@dataset ||= catalogue.dataset(DATASET)
|
|
21
22
|
end
|
|
22
23
|
|
|
23
|
-
# The zipped halves of the extract. Sorted by
|
|
24
|
+
# The zipped halves of the extract, in the order the catalogue's own names put them: the first is the one a release is named from. Sorted by the numbers in those names rather than by their characters, so that a tenth part would not come between the first and the second.
|
|
24
25
|
def extract_resources
|
|
25
|
-
@extract_resources ||= dataset.resources.select{|resource| resource.format == EXTRACT_FORMAT}.
|
|
26
|
+
@extract_resources ||= dataset.resources.select{|resource| resource.format == EXTRACT_FORMAT}.sort{|a, b| a.name.to_s.natural_comparison(b.name.to_s)}
|
|
26
27
|
end
|
|
27
28
|
|
|
28
|
-
#
|
|
29
|
+
# Every other resource of the dataset, the schema, the readme and the list of resources among them, kept alongside each release so that a release on disk carries what is needed to read it.
|
|
29
30
|
def supporting_resources
|
|
30
31
|
@supporting_resources ||= dataset.resources.reject{|resource| resource.format == EXTRACT_FORMAT}
|
|
31
32
|
end
|
|
32
33
|
|
|
33
|
-
# The extract's own halves and the
|
|
34
|
+
# The extract's own halves and the resources alongside them, so that a release on disk carries what is needed to read it without going back to the catalogue.
|
|
34
35
|
def resources
|
|
35
36
|
@resources ||= extract_resources + supporting_resources
|
|
36
37
|
end
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# DataGovAu/ABR/Parser.rb
|
|
2
|
+
# DataGovAu::ABR::Parser
|
|
3
|
+
|
|
4
|
+
require 'ox'
|
|
5
|
+
|
|
6
|
+
require_relative './Error'
|
|
7
|
+
|
|
8
|
+
module DataGovAu
|
|
9
|
+
module ABR
|
|
10
|
+
# One XML file of an extract, read as a stream and given up one record at a time. Nothing is filtered, nothing is derived and nothing is discarded: every element and attribute the schema allows for is carried into the record, dates as the strings the extract gives them, so that what is asked of the data later is not settled here.
|
|
11
|
+
#
|
|
12
|
+
# A record comes out shaped so:
|
|
13
|
+
#
|
|
14
|
+
# {
|
|
15
|
+
# abn: '11000047950', abn_status: 'ACT', abn_status_from_date: '20000224',
|
|
16
|
+
# record_last_updated_date: '20260211', replaced: 'N',
|
|
17
|
+
# entity_type_code: 'PUB', entity_type_text: 'Australian Public Company',
|
|
18
|
+
# state: 'NSW', postcode: '2132',
|
|
19
|
+
# asic_number: '000047950', asic_number_type: 'undetermined',
|
|
20
|
+
# gst_status: 'ACT', gst_status_from_date: '20000701',
|
|
21
|
+
# names: [{type: 'MN', text: 'SYDNEY MISSIONARY & BIBLE COLLEGE'}, ...],
|
|
22
|
+
# dgr: [{from_date: '20060301', status: 'ACT', name: nil}, ...],
|
|
23
|
+
# }
|
|
24
|
+
#
|
|
25
|
+
# An individual's name comes as its parts rather than joined, joining being a decision for whoever asks:
|
|
26
|
+
#
|
|
27
|
+
# {type: 'LGL', title: 'MR', given_names: ['WILLIAM', 'RICHARD'], family_name: 'LITT'}
|
|
28
|
+
class Parser
|
|
29
|
+
# What a file reports where it was written out whole.
|
|
30
|
+
NO_ERROR = 'none'
|
|
31
|
+
|
|
32
|
+
# What the file says of itself, which is read before any record and lets what was parsed be set against what was promised.
|
|
33
|
+
attr_reader :error, :extract_time, :file_sequence_number, :record_count, :records_parsed
|
|
34
|
+
|
|
35
|
+
def each_record(&block)
|
|
36
|
+
return enum_for(:each_record) unless block
|
|
37
|
+
@block = block
|
|
38
|
+
Ox.sax_parse(self, @io, convert_special: true, skip: :skip_none)
|
|
39
|
+
self
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The file reports no error of its own, in the error attribute the schema requires of it.
|
|
43
|
+
def error_free?
|
|
44
|
+
@error == NO_ERROR
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Every record was given up where the file reports no error and holds as many as its own TransferInfo promised.
|
|
48
|
+
def whole?
|
|
49
|
+
error_free? && !record_count.nil? && records_parsed == record_count
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Ox drives these four as it reads. They are public because Ox calls them, and are no part of what this class is for.
|
|
53
|
+
|
|
54
|
+
def start_element(name)
|
|
55
|
+
@element = name
|
|
56
|
+
@text.clear
|
|
57
|
+
case name
|
|
58
|
+
when :ABR then start_record
|
|
59
|
+
when :DGR then @record[:dgr] << {from_date: nil, status: nil, name: nil}
|
|
60
|
+
when :NonIndividualName, :IndividualName then @name = {type: nil}
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def attr(name, value)
|
|
65
|
+
case @element
|
|
66
|
+
when :Transfer then @error = value if name == :error
|
|
67
|
+
when :ABR then record_attribute(name, value)
|
|
68
|
+
when :ABN then abn_attribute(name, value)
|
|
69
|
+
when :GST then gst_attribute(name, value)
|
|
70
|
+
when :DGR then dgr_attribute(name, value)
|
|
71
|
+
when :ASICNumber then @record[:asic_number_type] = value if name == :ASICNumberType
|
|
72
|
+
when :NonIndividualName, :IndividualName then @name[:type] = value if name == :type
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def text(value)
|
|
77
|
+
@text << value
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def end_element(name)
|
|
81
|
+
case name
|
|
82
|
+
when :ABR then finish_record
|
|
83
|
+
when :ABN then @record[:abn] = taken
|
|
84
|
+
when :EntityTypeInd then @record[:entity_type_code] = taken
|
|
85
|
+
when :EntityTypeText then @record[:entity_type_text] = taken
|
|
86
|
+
when :ASICNumber then @record[:asic_number] = taken
|
|
87
|
+
when :State then @record[:state] = taken
|
|
88
|
+
when :Postcode then @record[:postcode] = taken
|
|
89
|
+
when :NameTitle then @name[:title] = taken
|
|
90
|
+
when :GivenName then (@name[:given_names] ||= []) << taken
|
|
91
|
+
when :FamilyName then @name[:family_name] = taken
|
|
92
|
+
when :NonIndividualNameText then @name[:text] = taken
|
|
93
|
+
when :NonIndividualName, :IndividualName then finish_name
|
|
94
|
+
when :FileSequenceNumber then @file_sequence_number = taken.to_i
|
|
95
|
+
when :RecordCount then @record_count = taken.to_i
|
|
96
|
+
when :ExtractTime then @extract_time = taken
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def initialize(io)
|
|
103
|
+
@io = io
|
|
104
|
+
@records_parsed = 0
|
|
105
|
+
@text = +''
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# The buffer is cleared at each element rather than made anew, there being some hundreds of millions of them in a release.
|
|
109
|
+
def taken
|
|
110
|
+
@text.dup
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def start_record
|
|
114
|
+
@record = {
|
|
115
|
+
abn: nil,
|
|
116
|
+
abn_status: nil,
|
|
117
|
+
abn_status_from_date: nil,
|
|
118
|
+
record_last_updated_date: nil,
|
|
119
|
+
replaced: nil,
|
|
120
|
+
entity_type_code: nil,
|
|
121
|
+
entity_type_text: nil,
|
|
122
|
+
state: nil,
|
|
123
|
+
postcode: nil,
|
|
124
|
+
asic_number: nil,
|
|
125
|
+
asic_number_type: nil,
|
|
126
|
+
gst_status: nil,
|
|
127
|
+
gst_status_from_date: nil,
|
|
128
|
+
names: [],
|
|
129
|
+
dgr: [],
|
|
130
|
+
}
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def finish_record
|
|
134
|
+
@records_parsed += 1
|
|
135
|
+
@block.call(@record)
|
|
136
|
+
@record = nil
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# A name of type DGR belongs to the DGR element enclosing it, that being the only place the schema puts one; every other name belongs to the record.
|
|
140
|
+
def finish_name
|
|
141
|
+
if @name[:type] == 'DGR'
|
|
142
|
+
@record[:dgr].last[:name] = @name[:text] if @record[:dgr].last
|
|
143
|
+
else
|
|
144
|
+
@record[:names] << @name
|
|
145
|
+
end
|
|
146
|
+
@name = nil
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def record_attribute(name, value)
|
|
150
|
+
case name
|
|
151
|
+
when :recordLastUpdatedDate then @record[:record_last_updated_date] = value
|
|
152
|
+
when :replaced then @record[:replaced] = value
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def abn_attribute(name, value)
|
|
157
|
+
case name
|
|
158
|
+
when :status then @record[:abn_status] = value
|
|
159
|
+
when :ABNStatusFromDate then @record[:abn_status_from_date] = value
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def gst_attribute(name, value)
|
|
164
|
+
case name
|
|
165
|
+
when :status then @record[:gst_status] = value
|
|
166
|
+
when :GSTStatusFromDate then @record[:gst_status_from_date] = value
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# The extract puts an entity's own endorsement and each named fund it runs in DGR elements of the same name, the first carrying a status and no name and the second a name and no status. Both are kept as they come.
|
|
171
|
+
def dgr_attribute(name, value)
|
|
172
|
+
entry = @record[:dgr].last
|
|
173
|
+
return unless entry
|
|
174
|
+
case name
|
|
175
|
+
when :status then entry[:status] = value
|
|
176
|
+
when :DGRStatusFromDate then entry[:from_date] = value
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
@@ -5,6 +5,8 @@ require 'date'
|
|
|
5
5
|
require 'fileutils'
|
|
6
6
|
require 'json'
|
|
7
7
|
|
|
8
|
+
require_relative '../../Thoran/Array/NaturalSort/natural_sort'
|
|
9
|
+
|
|
8
10
|
module DataGovAu
|
|
9
11
|
module ABR
|
|
10
12
|
class Release
|
|
@@ -31,6 +33,11 @@ module DataGovAu
|
|
|
31
33
|
resource.held_at?(path_for(resource))
|
|
32
34
|
end
|
|
33
35
|
|
|
36
|
+
# The zips held, read from the directory rather than from the catalogue, so that a release on disk can be parsed without going back to data.gov.au. Sorted by the numbers in their names rather than by their characters, the halves being named public_split_1_10 and public_split_11_20, of which the second sorts first.
|
|
37
|
+
def zip_paths
|
|
38
|
+
@zip_paths ||= Dir.glob(File.join(directory, '*.zip')).natural_sort
|
|
39
|
+
end
|
|
40
|
+
|
|
34
41
|
def missing
|
|
35
42
|
@resources.reject{|resource| held?(resource)}
|
|
36
43
|
end
|
data/lib/DataGovAu/ABR.rb
CHANGED
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
|
|
6
6
|
require_relative './ABR/Config'
|
|
7
7
|
require_relative './ABR/Error'
|
|
8
|
+
require_relative './ABR/Extract'
|
|
8
9
|
require_relative './ABR/ExtractDate'
|
|
9
10
|
require_relative './ABR/Fetch'
|
|
11
|
+
require_relative './ABR/Parser'
|
|
10
12
|
require_relative './ABR/Release'
|
|
11
13
|
|
|
12
14
|
module DataGovAu
|