-
Notifications
You must be signed in to change notification settings - Fork 0
/
archivesspace.add_objects.rb
354 lines (316 loc) · 15.2 KB
/
archivesspace.add_objects.rb
1
2
3
4
5
6
7
8
9
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
117
118
119
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
149
150
151
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
=begin
Abbreviations, AO = archival object (Everything's an AO, but there's also uri "archive_objects". It's confusing...)
AS = ArchivesSpace
IT = instance type
TC = top container
SC = Sub-container
_H = Hash
_J = Json string
_A = Array
_O = Object
_Q = Query
_S = Structure
_0R = Zero Relative
Usage: this_program.rb --res-num n [other options] FILE
this_program.rb --help
To attach the records in FILE to the "Resource Record", only the --res-num option is needed.
To attach the records to a specific AO, enter the --res-num option along with:
1) the --ao-num of the child AO. The --ao-num is the number at the end of the AO's uri.
2) OR use the 'new_parent' record level along with desired title in the input FILE (see below)
The input FILE has the following three formats( JSONized ):
1 = {
K.fmtr_record =>
{
K.level => '' # the AO level (eg. 'file', 'series', 'recordgrp', ...)
# -OR- a value of K.fmtr_new_parent (see below).
K.title => '', # the AO title field.
-optional- K.dates => [ ], # An array of AO date hashes, single or inclusive.
-optional- K.notes => [ ], # An array of AO note hashes, singlepart or miltipart.
-optional- K.fmtr_container => # References to TC of "type => indicator", creates the TC if needed.
{
K.fmtr_tc_type => 'VALUE' # eg. 'box'
K.fmtr_tc_indicator => 'n' # Box number (must be a number)
K.fmtr_sc_type => 'VALUE' # eg. 'folder'
K.fmtr_sc_indicator => 'STRING' # Anything identifing the folder
}
}
}
2 = {
K.fmtr_indent => [ K.fmtr_right, 'Any text' ] # Text only used for debugging
}
3 = {
K.fmtr_indent => [ K.fmtr_left, 'Any text' ] # Text only used for debugging
}
Record format 1 is the data record. As-of 3/18/2020 only the "series", "subseries", "recordgrp", and "file" AO-level types
were tested. If the record level (K.level) is equal to 'K.fmtr_new_parent' this causes the program to find an existing AO record
equal to the 'Title' value. This AO then becomes the parent record for all subsequent records in FILE.
Record format 2 causes all the records following the "indent => right" record to be attached to the record PRIOR to
the "indent => right" record.
Record format 3 causes all the records following the "indent => left" record to be attached to the record PRIOR to
the previous "indent => right" record. At the end of the program run, an "indent counter" is displayed, which should
be 0 IF the number of right-dents equals the number left-dents. Sometimes, there's no final indent-left tho.
=end
require 'json'
require 'optparse'
require 'class.Array.extend.rb'
require 'class.String.extend.rb'
require 'module.SE.rb'
require 'class.Archivesspace.rb'
require 'class.ArchivesSpace.http_calls.rb'
require 'class.Archivesspace.ArchivalObject.rb'
require 'class.Archivesspace.Repository.rb'
require 'class.Archivesspace.TopContainer.rb'
require 'class.Archivesspace.Resource.rb'
class Res_Q
def initialize( res_O )
@record_H_A = [ ]
Resource_Query.new( res_O ).record_H_A.each do | record_H |
if ( record_H[ K.level ] != K.file ) then
# SE.puts "%-11s" % record_H[ K.level ] + " #{record_H[ K.uri ]} '#{record_H[ K.title ]}'"
@record_H_A << record_H
end
end
return self
end
def uri_of( title ) # Find the URI with the matching title
a1 = [ ]
@record_H_A.each do | record_H |
if ( title.downcase == record_H[ K.title ].downcase ) then
a1 << record_H[ K.uri ]
# SE.puts "New parent: #{record_H[ K.uri ]} '#{record_H[ K.title ]}'"
end
end
if ( a1.maxindex < 0 ) then
SE.puts "#{SE.lineno}: Couldn't find a non-file AO with a title of '#{title}'"
raise
end
if ( a1.maxindex > 1 ) then
SE.puts "#{SE.lineno}: Found more than 1 AO with a title of '#{title}'.",
"Use the uri number and the '--ao-num' option."
raise
end
return a1[ 0 ]
end
end
BEGIN {}
END {}
binding.pry if ( respond_to? :pry )
myself_name = File.basename( $0 )
api_uri_base = "http://localhost:8089"
cmdln_option = { :rep_num => 2 ,
:res_num => nil ,
:ao_num => nil ,
:initial_parent_title => nil ,
:delete_TC_only => false ,
:update => false ,
:last_record_num => nil ,
}
OptionParser.new do |option|
option.banner = "Usage: #{myself_name} [options] FILE"
option.on( "--rep-num n", OptionParser::DecimalInteger, "Repository number ( default = 2 )" ) do |opt_arg|
cmdln_option[ :rep_num ] = opt_arg
end
option.on( "--res-num n", OptionParser::DecimalInteger, "Resource number ( required )" ) do |opt_arg|
cmdln_option[ :res_num ] = opt_arg
end
option.on( "--ao-num n", OptionParser::DecimalInteger, "Initial parent AO URI number ( optional, but must be member of suppled Resource number )" ) do |opt_arg|
cmdln_option[ :ao_num ] = opt_arg
end
option.on( "--initial-parent-title x", "Initial parent AO Title ( optional, but must be member of suppled Resource number )" ) do |opt_arg|
cmdln_option[ :initial_parent_title ] = opt_arg
end
option.on( "--delete-tc-only", "Delete TC records, then stop" ) do |opt_arg|
cmdln_option[ :delete_TC_only ] = true
end
option.on( "--update", "Do updates" ) do |opt_arg|
cmdln_option[ :update ] = true
end
option.on( "--last-record-num n", OptionParser::DecimalInteger, "Stop after record N" ) do |opt_arg|
cmdln_option[ :last_record_num ] = opt_arg
end
option.on( "-h","--help" ) do
SE.puts option
exit
end
end.parse! # Bang because ARGV is altered
#p cmdln_option
#p ARGV
if ( ! cmdln_option[ :rep_num ] ) then
SE.puts "The --rep-num option is required."
raise
end
if ( ! cmdln_option[ :res_num ] ) then
SE.puts "The --res-num option is required."
raise
end
aspace_O = ASpace.new
aspace_O.allow_updates=cmdln_option[ :update ]
aspace_O.api_uri_base = api_uri_base
aspace_O.login( "admin", "admin" )
#SE.pom(aspace_O)
#SE.pov(aspace_O)
rep_O = Repository.new( aspace_O, cmdln_option[ :rep_num ] )
#SE.pom(rep_O)
#SE.pov(rep_O)
res_O = Resource.new( rep_O, cmdln_option[ :res_num ] )
res_buf_O = res_O.new_buffer.read
#SE.pom(res_buf_O)
#SE.pov(res_buf_O)
parent_ref_stack_A = [ ]
if ( cmdln_option[ :ao_num ] ) then
if ( cmdln_option[ :initial_parent_title ] ) then
SE.puts "#{SE.lineno}: The '--ao-num' and 'initial-parent-title' options are mutually exclusive"
raise
end
parent_ref_stack_A << Archival_Object.new( res_buf_O,cmdln_option[ :ao_num ] ).new_buffer.read.record_H[ K.uri ]
SE.puts "#{SE.lineno}: initial parent uri = #{parent_ref_stack_A[ 0 ]} (From the cmd_line)"
else
res_Q_O = Res_Q.new( res_O )
if ( cmdln_option[ :initial_parent_title ] ) then
parent_ref_stack_A << res_Q_O.uri_of( cmdln_option[ :initial_parent_title ] )
SE.puts "#{SE.lineno}: initial parent AO uri = #{parent_ref_stack_A[ 0 ]} (From the cmd_line)"
else
parent_ref_stack_A << res_buf_O.record_H[ K.uri ]
SE.puts "#{SE.lineno}: initial parent AO_uri = #{parent_ref_stack_A[ 0 ]} (The Resource)"
end
end
SE.puts "Finding Top_Containers (which takes some time) ..."
time_begin = Time.now
all_TC_S = TC_Query.new( rep_O ).get_all_TC_S
elapsed_seconds = Time.now - time_begin
SE.puts "Elapsed seconds = #{elapsed_seconds}"
tc_uri_H__by_type_and_indicator = {}
all_TC_S.for_R( res_O ).each do | record_H |
if ( record_H.key?( K.type ) and record_H.key?( K.indicator )) then
stringer=record_H[ K.type ] + record_H[ K.indicator ]
SE.puts "Reusing TC: #{record_H[ K.uri ].sub( /^.*\//, '' )}, type=#{record_H[ K.type ]}, indicator='#{record_H[ K.indicator ]}'"
if ( tc_uri_H__by_type_and_indicator.key?( stringer ) ) then
SE.puts "#{SE.lineno}: Duplicate record_H 'type+indicator' #{stringer}, K.uri=#{record_H[ K.uri ]}"
next
end
tc_uri_H__by_type_and_indicator[ stringer ] = record_H[ K.uri ]
end
end
all_TC_S.unused.each do | record_H |
SE.puts "#{SE.lineno}: Delete top_container: #{record_H[ K.uri ]}"
Top_Container.new( res_O, record_H[ K.uri ] ).new_buffer.delete
end
if ( cmdln_option[ :delete_TC_only ] ) then
exit
end
if ( ARGV.maxindex < 0 ) then
SE.puts "No input file provided."
exit
end
#SE.pp "tc_uri_H__by_type_and_indicator:", tc_uri_H__by_type_and_indicator
net_indent_cnt = 0
record_level_cnt = Hash.new( 0 ) # h.default works too...
last_AO_uri_created = ""
if ( parent_ref_stack_A.maxindex != 0 ) then
SE.puts "#{SE.lineno}: Was expecting parent_ref_stack_A.maxindex to be 0"
SE.pp "parent_ref_stack_A=", parent_ref_stack_A
raise
end
for argv in ARGV do
File.foreach( argv ) do |input_record_J|
if (cmdln_option[ :last_record_num ] != nil and $. >cmdln_option[ :last_record_num ] ) then
break
end
input_record_J.chomp!
if ( input_record_J.match?( /^\s*$/ ) ) then
next
end
input_record_H = JSON.parse( input_record_J )
if ( input_record_H.key?( K.fmtr_indent ) ) then
SE.puts "#{SE.lineno}: '#{input_record_J}'"
record_level_cnt[ input_record_H[ K.fmtr_indent ][ 0 ] ] += 1
case input_record_H[ K.fmtr_indent ][ 0 ]
when K.fmtr_right then
net_indent_cnt += 1
parent_ref_stack_A.push( last_AO_uri_created )
SE.puts "#{SE.lineno}: New parent: #{last_AO_uri_created}"
next
when K.fmtr_left then
net_indent_cnt -= 1
parent_ref_stack_A.pop( 1 )
last_AO_uri_created = parent_ref_stack_A[ parent_ref_stack_A.maxindex ]
SE.puts "#{SE.lineno}: New parent: #{last_AO_uri_created}"
next
else
SE.puts "#{SE.lineno}: Invalid indent direction '#{input_record_H[ K.fmtr_indent ][ 0 ]}'"
raise
end
end
if ( input_record_H.key?( K.fmtr_record ) ) then
SE.puts "#{SE.lineno}: '#{input_record_J}'"
record_level = input_record_H[ K.fmtr_record ][ K.level ]
record_level_cnt[ record_level ] += 1
if ( record_level == K.fmtr_new_parent ) then
if (cmdln_option[ :ao_num ] or cmdln_option[ :initial_parent_title ] ) then
SE.puts "#{SE.lineno}: Hit 'new_parent' record, but"
SE.puts "the --ao-num and --initial-parent-title options aren't allowed for this record type."
raise
end
if ( parent_ref_stack_A.maxindex != 0 ) then
SE.puts "#{SE.lineno}: Hit 'new_parent' record, but parent_ref_stack_A.maxindex != 0"
SE.puts "The formatter should insure the indent level is at 0 for a 'new_parent' record."
SE.pp "parent_ref_stack_A:", parent_ref_stack_A
SE.pp "#{$.}: input_record_H:", input_record_H
raise
end
parent_ref_stack_A[ 0 ] = res_Q_O.uri_of( input_record_H[ K.fmtr_record ][ K.title ] )
SE.puts "#{SE.lineno}: New parent: #{parent_ref_stack_A[ 0 ]}"
next
end
ao_buf_O = Archival_Object.new( res_buf_O ).new_buffer.create( record_level )
# SE.pp ao_buf_O.record_H
if ( ao_buf_O.record_H[ K.resource ][ K.ref ] == parent_ref_stack_A[ parent_ref_stack_A.maxindex ] ) then
ao_buf_O.record_H = { K.parent => '' }
else
ao_buf_O.record_H = { K.parent => { K.ref => parent_ref_stack_A[ parent_ref_stack_A.maxindex ] }}
end
if ( input_record_H[ K.fmtr_record ].key?( K.component_id )) then
ao_buf_O.record_H = { K.component_id => input_record_H[ K.fmtr_record ][ K.component_id ] }
end
ao_buf_O.record_H = { K.title => input_record_H[ K.fmtr_record ][ K.title ] }
if ( input_record_H[ K.fmtr_record ].key?( K.dates ) and
! input_record_H[ K.fmtr_record ][ K.dates ].empty? ) then
ao_buf_O.record_H = { K.dates => input_record_H[ K.fmtr_record ][ K.dates ] }
end
if ( input_record_H[ K.fmtr_record ].key?( K.notes ) and
! input_record_H[ K.fmtr_record ][ K.notes ].empty? ) then
ao_buf_O.record_H = { K.notes => input_record_H[ K.fmtr_record ][ K.notes ] }
end
if ( input_record_H[ K.fmtr_record ].key?( K.fmtr_container ) and
! input_record_H[ K.fmtr_record ][ K.fmtr_container ].empty? ) then
type = "#{input_record_H[ K.fmtr_record ][ K.fmtr_container ][ K.fmtr_tc_type ]}"
indicator = "#{input_record_H[ K.fmtr_record ][ K.fmtr_container ][ K.fmtr_tc_indicator ]}"
unique_TC_key = "#{type}#{indicator}"
if ( ! tc_uri_H__by_type_and_indicator.key?( unique_TC_key ) ) then
tc_buf_O = Top_Container.new( res_buf_O ).new_buffer.create
tc_buf_O.record_H = { K.type => type }
tc_buf_O.record_H = { K.indicator => indicator }
tc_buf_O.store
tc_uri_H__by_type_and_indicator[ unique_TC_key ] = tc_buf_O.uri
# SE.pp "#{SE.lineno}: tc_uri_H__by_type_and_indicator:", tc_uri_H__by_type_and_indicator
end
it_frag_O = Record_Format.new( :instance_type )
it_frag_O.record_H = { K.instance_type => K.mixed_materials}
it_frag_O.record_H = { K.sub_container => { K.top_container => { K.ref => tc_uri_H__by_type_and_indicator[ unique_TC_key ] }}}
it_frag_O.record_H = { K.sub_container => { K.type_2 => input_record_H[ K.fmtr_record ][ K.fmtr_container ][ K.fmtr_sc_type ] }}
it_frag_O.record_H = { K.sub_container => { K.indicator_2 => input_record_H[ K.fmtr_record ][ K.fmtr_container ][ K.fmtr_sc_indicator ] }}
ao_buf_O.record_H = { K.instances => [ it_frag_O.record_H ] }
end
# SE.pp ao_buf_O.record_H
ao_buf_O.store
last_AO_uri_created = ao_buf_O.uri
next
end
SE.puts "#{SE.lineno}: I should't be here!"
SE.pp "#{$.}: input_record_H:", input_record_H
raise
end
end
#SE.pp "tc_uri_H__by_type_and_indicator:", tc_uri_H__by_type_and_indicator
SE.puts "#{SE.lineno}: net indent count = #{net_indent_cnt}"
SE.puts "record counts:", record_level_cnt.ai