def run(self, name): """Import movies, function called in a loop over source files""" from add import validate_details, edit_movie from gutils import find_next_available from sqlalchemy import Select, func import gtk if not self.set_source(name): self.debug.show("Can't read data from file %s" % name) return False self.widgets['pwindow'].show() while gtk.events_pending(): # give GTK some time for updates gtk.main_iteration() # progressbar update_on = [] count = self.count_movies() if count > 0: for i in range(0,100): update_on.append(int(float(i)/100*count)) statement = Select( [ func.max(self.db.Movie.c.number) ] ) number = statement.execute().fetchone()[0] if number is None: number = 1 else: number += 1 statement = Select([self.db.Movie.c.number]) processed = 0 while self._abort is False: details = self.get_movie_details() if details is None: break processed += 1 if processed in update_on: self.widgets['progressbar'].set_fraction(float(processed)/float(count)) gtk.main_iteration() self.widgets['progressbar'].set_text("%s (%s/%s)" % (str(self.imported), str(processed), str(count))) gtk.main_iteration() gtk.main_iteration() # extra iteration for abort button if (details.has_key('o_title') and details['o_title']) or (details.has_key('title') and details['title']): if details.has_key('o_title') and details['o_title']: statement.whereclause = func.lower(self.db.Movie.c.o_title)==details['o_title'].lower() if details.has_key('title') and details['title']: statement.append_whereclause( func.lower(self.db.Movie.c.title)==details['title'].lower() ) tmp = statement.execute().fetchone() if tmp is not None: self.debug.show("movie already exists (number=%s, o_title=%s)" % (tmp.number, details['o_title'])) continue elif details.has_key('title') and details['title']: statement.whereclause = func.lower(self.db.Movie.c.title)==details['title'].lower() tmp = statement.execute().fetchone() if tmp is not None: self.debug.show("movie already exists (number=%s, title=%s)" % (tmp.number, details['title'])) continue validate_details(details, self.fields_to_import) if self.edit is True: response = edit_movie(self.parent, details) # FIXME: wait until save or cancel button pressed if response == 1: self.imported += 1 else: if not details.has_key('number') or (details.has_key('number') and details['number'] is None): #details['number'] = find_next_available(self.db) details['number'] = number number += 1 #movie = self.db.Movie() #movie.add_to_db(details) try: self.db.Movie.mapper.mapped_table.insert().execute(details) self.imported += 1 except Exception, e: self.debug.show("movie details are not unique, skipping: %s" % str(e)) else: self.debug.show('skipping movie without title or original title')