turn zserver array into hash by group. should make lookups faster instead of running...
[zcc.git] / bin / zcc-new
blob62f8f45494cc2e032b1f2dbe3f22ffbabd624d54
1 #!/usr/bin/ruby -w
2 ## Jason Ronallo
3 # July, 2007
4 #zcc-new
6 require 'rubygems'
7 require 'zoom'
8 require 'marc'
9 require 'stringio'
10 require 'yaml'
12 $KCODE = 'u'
13 require 'jcode'
14 require 'unicode'
15 require 'highline/import'
16 require 'zcc'
17 require 'tempfile'
18 include ZCC
20 termsize = HighLine::SystemExtensions.terminal_size
21 $term_width = termsize[0]
22 $term_height = termsize[1]
23 $clear_code = %x{clear}
26 ft = HighLine::ColorScheme.new do |cs|
27 cs[:headline] = [ :bold, :white, :on_black ]
28 cs[:horizontal_line] = [ :bold, :white, :on_blue]
29 cs[:even_row] = [ :green ]
30 cs[:odd_row] = [ :magenta ]
31 cs[:marc_tag] = [:bold]
32 cs[:bolder] = [:bold]
33 cs[:field_hilite] = [:bold, :blue]
34 cs[:index] = [:bold, :red]
35 end
36 HighLine.color_scheme = ft
39 ##Setting up testing parameter
40 $testing = ARGV[0]
41 $testing = nil unless $testing == 'v' or $testing == 't'
42 file_mode = true if ARGV[0] == 'file'
43 puts "We're testing " if $testing
44 STDIN.gets if $testing
46 #reading in config from yaml file
47 yamlfile = YAML.load_file( "#{File.expand_path("~")}/.zcc/zcc.yaml" )
48 zservers = yamlfile['zservers']
49 #STDIN.gets
50 labels = yamlfile['labels']
51 scripting = yamlfile['scripting']
52 $procs = yamlfile['procs']
54 TO_SHOW = yamlfile['to_show']
55 HOW_TO_SAVE = yamlfile['save_filename']
56 TIMESTAMP = Time.now.strftime("%Y%m%d%H%M%S")
57 SCRIPTING = yamlfile['scripting_on']
58 EDITING = yamlfile['subfield_editing']
59 CSV = yamlfile['csv_on']
60 LINTER = yamlfile['linter_on']
61 FORMAT_TO_SAVE = yamlfile['save_record_syntax']
64 print $clear_code
65 #printed once when starting the program.
66 say("Zcc, Copyright (C) 2007 Jason Ronallo\nZcc comes with ABSOLUTELY NO WARRANTY.\nZcc is released under the terms of the GPL v.2 or later.\nSee LICENSE for full information.\n")
67 say("Introductory help. For now see: http://zcc.rubyforge.org/zcc.html")
69 zserver_objects = []
70 zservers.each do |server|
71 zserver_objects << Zserver.new(server[0], server[1], server[2], server[3])
72 end
73 #puts zserver_objects.inspect
75 groups = zserver_objects.collect do |server|
76 server.group
77 end
78 groups.uniq!
79 groups.sort!
80 server_h = {}
81 groups.each do |group|
82 server_h[group] = Array.new(zserver_objects.find_all{|z| z.group == group})
83 end
85 #puts server_h
86 #STDIN.gets
87 #puts
88 #puts server_h.inspect
89 #STDIN.gets
91 loop {
93 search_term = ask("\n<%= color('Enter search. [ISBN, title, LCCN (with dash), or :q to quit]', :bolder) %>\nsearch> ", String){ |q| q.readline = true }
94 exit if search_term == ':q'
97 taken = ResultSet.new
98 groups.each do |group|
99 print $clear_code
100 #puts group
101 servers = server_h[group] #zserver_objects.find_all{|server| server.group == group }
102 #puts "searching the following servers: #{servers.join("...\n")}"
103 #puts "gets here"
104 query = Query.new(search_term, servers)
105 results = query.search(TO_SHOW)
106 #print "results.class" ; puts results.class
107 if results.records.empty?
108 puts "There are no results from group #{group} servers!\n\n\n"
109 say("Do you want to continue searcing the next group of servers? [y], anything")
110 another_group = STDIN.gets.chomp
111 if another_group == 'y' || another_group == ''
112 next
113 else
114 print $clear_code
115 break
118 return_value = zcc_select_good_marc(results, 'multi')
119 taken << results
120 break if return_value == 'done'
123 #puts taken.to_s
124 taken.remove_unselected!
125 next if taken.empty?
126 continue = true
127 say("\a<%= color('This is your combined result set. We will winnow until you choose done or you unselect all records.', :headline) %>")
128 sleep 3
129 while continue
130 return_value = zcc_select_good_marc(taken, 'multi')
131 continue = false if return_value == 'done'
132 #taken.remove_unselected!
133 #continue = false if taken.length == 0
134 continue = false if taken.selected_length == 0
136 taken.remove_unselected!
137 unless taken.length == 0
138 puts "Here are the records you've selected for processing:"
139 taken.each do |record|
140 puts record.title
142 sleep 2
145 taken.records.each do |final_taken|
147 #SCRIPTING and EDITING
148 if SCRIPTING
149 print $clear_code
150 print "\a"
151 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
152 say("<%= color('SCRIPTING...', :headline) %>")
153 final_taken.local_script(scripting)
156 if EDITING
157 print $clear_code
158 print "\a"
159 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
160 say("Would you like to <%= color('edit', :headline) %> this record [yes or ANYTHING]? ")
161 edit = STDIN.gets.chomp
162 if edit == ('y' or 'yes')
163 print $clear_code
164 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
165 final_taken.edit
169 print $clear_code
170 say("<%= color('#{'-'*20}FINAL RECORD#{'-'*20}', :headline) %>")
171 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
172 say("<%= color('#{'-'*20}FINAL RECORD#{'-'*20}\a', :headline) %>")
173 print "Would you like to keep this FINAL RECORD? (ANYTHING or n)"
175 to_save_or_not_to_save = STDIN.gets.chomp
177 if to_save_or_not_to_save == 'n'
178 next #if continue? == 'again'
181 if CSV
182 print $clear_code
183 print "\a"
184 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
185 say("<%= color('Label making time!', :headline) %>")
186 csv = final_taken.marc_to_csv(labels)
187 print "Final csv:\n#{csv}\n"
190 print $clear_code
191 clear_code_do = false
194 #saves in the directory in which zcc was run
195 if HOW_TO_SAVE == 'search_term'
196 filename = search_term.gsub(' ','_')
197 elsif HOW_TO_SAVE == 'timestamp'
198 filename = TIMESTAMP
201 FORMAT_TO_SAVE.each do |format|
202 if format == 'marc'
203 aFile = File.new("#{filename}\.mrc","a+")
204 aFile.write(final_taken.marc.to_marc)
205 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.mrc"
206 aFile.close
207 elsif format == 'xml'
208 #puts final_taken.marc.to_xml
209 aFile = File.new("#{filename}\.xml","a+")
210 aFile.write(final_taken.marc.to_xml)
211 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.xml"
212 aFile.close
213 elsif format == 'zebra'
214 #puts final_taken.to_xml
215 File.open("#{File.expand_path("~")}/.zcc/zebra/records/#{Time.now.strftime("%Y%m%d%H%M%S")}\.mrc", "w") do |f|
216 f.write final_taken.marc.to_marc
218 Dir.chdir("#{File.expand_path("~")}/.zcc/zebra") do
219 record_update = `zebraidx update records`
220 puts record_update
222 #saving to koha2 is untested. Please let me know if it doesn't work--or even if it does.
223 elsif format == 'koha2'
224 out = Tempfile.new("koha2-")
225 out << final_taken.marc.to_marc
226 out.close
227 koha_save = `perl /usr/local/koha/intranet/scripts/misc/migration_tools/bulkmarcimport.pl -v 6 -file #{out.path}`
228 puts koha_save
232 if CSV
233 csvFile = File.new("csv-#{filename}\.txt","a+")
234 csvFile.write(csv)
235 puts "csv for #{final_taken.marc['245']['a']} saved in csv-#{filename}\.txt"
236 csvFile.close