added a way to include a longer list of zservers from another file so as not to clutt...
[zcc.git] / bin / zcc
bloba834e6f570890b4cf9cadf92d8fc98446a39a84c
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 'highline/import'
10 require 'term/ansicolor'
11 #include Term::ANSIColor
13 require 'stringio'
14 require 'yaml'
15 require 'tempfile'
17 $KCODE = 'u'
18 require 'jcode'
19 require 'unicode'
21 require 'zcc'
22 include ZCC
24 termsize = HighLine::SystemExtensions.terminal_size
25 $term_width = termsize[0]
26 $term_height = termsize[1]
27 $clear_code = %x{clear}
28 HighLine.track_eof = false
30 ##Setting up testing parameter
31 $testing = ARGV[0]
32 $testing = nil unless $testing == 'v' or $testing == 't'
33 file_mode = true if ARGV[0] == 'file'
34 puts "We're testing " if $testing
35 STDIN.gets if $testing
37 #reading in config from yaml file
38 yamlfile = YAML.load_file( "#{File.expand_path("~")}/.zcc/zcc.yaml" )
39 zservers = yamlfile['zservers']
40 #puts zservers[0][0]
41 #STDIN.gets
42 #puts zservers[0][0].class
43 #STDIN.gets
44 #puts zservers[0][0] =~ /Include/
45 #STDIN.gets
46 if zservers[0][0] =~ /Include/
47 #puts "MATCH"
48 filename = zservers[0][0][8, 99]
49 contents = YAML.load_file("#{File.expand_path("~")}/.zcc/#{filename}")
50 #puts contents.class
51 #STDIN.gets
52 contents.each{|c| zservers.push(c)}
53 #zservers << contents
54 end
55 #puts zservers.inspect
56 #STDIN.gets
57 zservers[0] = nil
58 zservers.compact!
60 labels = yamlfile['labels']
61 scripting = yamlfile['scripting']
62 $procs = yamlfile['procs']
64 TO_SHOW = yamlfile['to_show']
65 HOW_TO_SAVE = yamlfile['save_filename']
66 TIMESTAMP = Time.now.strftime("%Y%m%d%H%M%S")
67 SCRIPTING = yamlfile['scripting_on']
68 EDITING = yamlfile['subfield_editing']
69 CSV = yamlfile['csv_on']
70 LINTER = yamlfile['linter_on']
71 FORMAT_TO_SAVE = yamlfile['save_record_syntax']
73 print $clear_code
74 #printed once when starting the program.
75 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".bold)
76 say("Introductory help. For now see: http://zcc.rubyforge.org/zcc.html")
78 zserver_objects = []
79 zservers.each do |server|
80 zserver_objects << Zserver.new(server[0], server[1], server[2], server[3])
81 end
83 server_h = Hash.new {|hash, key| hash[key] = [] }
84 zserver_objects.each {|z| server_h[z.group] << z}
86 groups = []
87 server_h.each_key{|k| groups << k}
88 groups.uniq!
89 groups.sort!
91 loop {
92 search_term = ask("\nEnter search. [ISBN, title, LCCN (with dash), or :q to quit]\nsearch> ".boldz, String){ |q| q.readline = true }
93 exit if search_term == ':q'
95 taken = ResultSet.new
96 groups.each do |group|
97 print $clear_code
98 #puts group
99 servers = server_h[group] #zserver_objects.find_all{|server| server.group == group }
100 #puts "searching the following servers: #{servers.join("...\n")}"
101 #puts "gets here"
102 query = Query.new(search_term, servers)
103 results = query.search(TO_SHOW)
104 #print "results.class" ; puts results.class
105 if results.records.empty?
106 puts "There are no results from group #{group} servers!\n\n\n"
107 another_group = ask("Do you want to continue searcing the next group of servers? [y], anything ".boldz)
108 if another_group == 'y' || another_group == ''
109 next
110 else
111 print $clear_code
112 break
115 return_value = zcc_select_good_marc(results, 'multi')
116 taken << results
117 break if return_value == 'done'
120 #puts taken.to_s
121 taken.remove_unselected!
122 next if taken.empty?
123 continue = true
124 print $clear_code
125 say("\aThis is your combined result set.\nWe will winnow until you choose ".white.bold.on_black + "'done'".red.bold + "\nor you unselect all records.".white.bold.on_black)
126 sleep 3
127 while continue
128 return_value = zcc_select_good_marc(taken, 'multi')
129 #taken.remove_unselected!
130 #continue = false if taken.length == 0
131 continue = false if taken.selected_length == 0
132 continue = false if return_value == 'done'
134 taken.remove_unselected!
135 unless taken.length == 0
136 puts "Here are the records you've selected for processing:"
137 taken.each do |record|
138 puts record.title
140 sleep 2
143 taken.records.each do |final_taken|
145 #SCRIPTING and EDITING
146 if SCRIPTING
147 print $clear_code
148 print "\a"
149 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
150 say("SCRIPTING...".bold.white.on_black)
151 final_taken.local_script(scripting)
154 if EDITING
155 print $clear_code
156 print "\a"
157 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
158 edit = ask("Would you like to " + "edit".headline + " this record [yes or ANYTHING]? ")
159 if edit == ('y' or 'yes')
160 print $clear_code
161 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
162 final_taken.edit
166 print $clear_code
167 say("#{'-'*20}FINAL RECORD#{'-'*20}".headline)
168 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
169 say("#{'-'*20}FINAL RECORD#{'-'*20}".headline)
170 to_save_or_not_to_save = ask("Would you like to keep this FINAL RECORD? (ANYTHING or n)")
172 if to_save_or_not_to_save == 'n'
173 next #if continue? == 'again'
176 if CSV
177 print $clear_code
178 print "\a"
179 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
180 say("Label making time!".headline)
181 csv = final_taken.marc_to_csv(labels)
182 print "Final csv:\n#{csv}\n"
185 print $clear_code
186 clear_code_do = false
189 #saves in the directory in which zcc was run
190 if HOW_TO_SAVE == 'search_term'
191 filename = search_term.gsub(' ','_')
192 elsif HOW_TO_SAVE == 'timestamp'
193 filename = TIMESTAMP
196 FORMAT_TO_SAVE.each do |format|
197 if format == 'marc'
198 puts
199 aFile = File.new("#{filename}\.mrc","a+")
200 aFile.write(final_taken.marc.to_marc)
201 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.mrc"
202 aFile.close
203 puts
204 elsif format == 'xml'
205 #puts final_taken.marc.to_xml
206 aFile = File.new("#{filename}\.xml","a+")
207 aFile.write(final_taken.marc.to_xml)
208 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.xml"
209 aFile.close
210 elsif format == 'zebra'
211 #puts final_taken.to_xml
212 File.open("#{File.expand_path("~")}/.zcc/zebra/records/#{Time.now.strftime("%Y%m%d%H%M%S")}\.mrc", "w") do |f|
213 f.write final_taken.marc.to_marc
215 Dir.chdir("#{File.expand_path("~")}/.zcc/zebra") do
216 record_update = `zebraidx update records`
217 puts record_update
219 #saving to koha2 is untested. Please let me know if it doesn't work--or even if it does.
220 elsif format == 'koha2'
221 puts
222 out = Tempfile.new("koha2-")
223 out << final_taken.marc.to_marc
224 out.close
225 koha_save = `perl /usr/local/koha/intranet/scripts/misc/migration_tools/bulkmarcimport.pl -v 6 -file #{out.path}`
226 puts koha_save
227 puts
231 if CSV
232 puts
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
237 puts