added loop for winnowing result set and a method to remove unselected records from...
[zcc.git] / bin / zcc-new
blob0c07b39fb345b76bfa461a2d3af38be40e0cc1f6
1 #!/usr/bin/ruby -w
2 ## Jason Ronallo
3 # July, 2007
4 #zcc-new
7 require 'rubygems'
8 require 'zoom'
9 require 'marc'
10 require 'stringio'
11 require 'yaml'
13 $KCODE = 'u'
14 require 'jcode'
15 require 'unicode'
16 require 'highline/import'
17 require 'zcc'
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 zserver_objects = []
50 zservers.each do |server|
51 zserver_objects << Zserver.new(server[0], server[1], server[2], server[3])
52 end
53 puts zserver_objects.inspect
54 #STDIN.gets
57 labels = yamlfile['labels']
58 scripting = yamlfile['scripting']
59 $procs = yamlfile['procs']
61 TO_SHOW = yamlfile['to_show']
62 HOW_TO_SAVE = yamlfile['save_filename']
63 TIMESTAMP = Time.now.strftime("%Y%m%d%H%M%S")
64 SCRIPTING = yamlfile['scripting_on']
65 EDITING = yamlfile['subfield_editing']
66 CSV = yamlfile['csv_on']
67 LINTER = yamlfile['linter_on']
68 FORMAT_TO_SAVE = yamlfile['save_record_syntax']
69 #ZEBRA_ON = yamlfile['zebra']
70 #ZEBRA_CONF = yamlfile['zebraconf']
72 print $clear_code
73 #printed once when starting the program.
74 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")
75 say("Introductory help. For now see: http://zcc.rubyforge.org/zcc.html")
78 groups = zserver_objects.collect do |server|
79 server.group
80 end
82 groups.uniq!
83 groups.sort!
85 loop {
87 search_term = ask("\n<%= color('Enter search. [ISBN, title, LCCN (with dash), or :q to quit]', :bolder) %>\nsearch> ", String){ |q| q.readline = true }
88 exit if search_term == ':q'
91 taken = []
92 groups.each do |group|
93 puts group
94 servers = zserver_objects.find_all{|server| server.group == group }
95 puts servers
96 puts "gets here"
97 query = Query.new(search_term, servers)
98 results = query.search(25)
99 return_value = zcc_select_good_marc(results, 'multi')
101 #taken << results.get_selected
102 taken = results.dup
103 break if return_value == 'done'
106 puts taken.to_s
107 taken.remove_unselected!
108 puts taken.to_s
109 continue = true
110 while continue
111 puts "we will winnow until you choose done/d"
112 sleep 2
113 return_value = zcc_select_good_marc(taken, 'multi')
114 continue = false if return_value == 'done'
115 taken.remove_unselected!
121 puts "Here are the records you've selected: "
122 #puts taken.inspect
123 taken.each do |record|
124 puts record.title
127 continue = true
128 while continue
130 continue = false
134 taken.each do |final_taken|
136 #SCRIPTING and EDITING
137 if SCRIPTING
138 print $clear_code
139 print "\a"
140 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
141 say("<%= color('SCRIPTING...', :headline) %>")
142 final_taken.local_script(scripting)
144 if EDITING
145 print $clear_code
146 print "\a"
147 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
148 say("Would you like to <%= color('edit', :headline) %> this record [yes or ANYTHING]? ")
149 edit = STDIN.gets.chomp
150 if edit == ('y' or 'yes')
151 print $clear_code
152 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
153 final_taken.edit
157 print $clear_code
158 say("<%= color('#{'-'*20}FINAL RECORD#{'-'*20}', :headline) %>")
159 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
160 say("<%= color('#{'-'*20}FINAL RECORD#{'-'*20}\a', :headline) %>")
161 print "Would you like to keep this FINAL RECORD? (ANYTHING or n)"
163 to_save_or_not_to_save = STDIN.gets.chomp
165 if to_save_or_not_to_save == 'n'
166 next #if continue? == 'again'
169 if CSV
170 print $clear_code
171 print "\a"
172 say("#{ZCC.zcc_marc_str_bold(final_taken.to_s, 'record')}")
173 say("<%= color('Label making time!', :headline) %>")
174 csv = final_taken.marc_to_csv(labels)
175 print "Final csv:\n#{csv}\n"
178 print $clear_code
179 clear_code_do = false
182 #saves in the directory in which zcc was run
183 if HOW_TO_SAVE == 'search_term'
184 filename = search_term.gsub(' ','_')
185 elsif HOW_TO_SAVE == 'timestamp'
186 filename = TIMESTAMP
189 if FORMAT_TO_SAVE == 'marc'
190 aFile = File.new("#{filename}\.mrc","a+")
191 aFile.write(final_taken.marc.to_marc)
192 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.mrc"
193 aFile.close
194 elsif FORMAT_TO_SAVE == 'marcxml'
195 puts final_taken.marc.to_xml
196 aFile = File.new("#{filename}\.xml","a+")
197 aFile.write(final_taken.marc.to_xml)
198 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.xml"
199 aFile.close
200 elsif FORMAT_TO_SAVE == 'zebra'
201 #puts final_taken.to_xml
202 File.open("#{File.expand_path("~")}/.zcc/zebra/records/#{Time.now.strftime("%Y%m%d%H%M%S")}\.mrc", "w") do |f|
203 f.write final_taken.marc.to_marc
205 Dir.chdir("#{File.expand_path("~")}/.zcc/zebra") do
206 record_update = `zebraidx update records`
207 puts record_update
209 aFile = File.new("#{filename}\.mrc","a+")
210 aFile.write(final_taken.marc.to_marc)
211 puts "#{final_taken.marc['245']['a']} saved in #{filename}\.mrc"
212 aFile.close
215 if CSV
216 csvFile = File.new("csv-#{filename}\.txt","a+")
217 csvFile.write(csv)
218 puts "csv for #{final_taken.marc['245']['a']} saved in csv-#{filename}\.txt"
219 csvFile.close