Griffith - media collection manager
May 23, 2012, 08:47:27 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please check ask.griffith.cc and wiki.griffith.cc before asking questions.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Feature. Merge databases  (Read 1218 times)
coffelius
Newbie
*
Offline Offline

Posts: 1


View Profile
« on: April 21, 2006, 07:22:15 PM »

hi all people

I have to mix two griffith databases because I have 2 computers where I append the movies.

I added a new menuitem "Merge"  below of menuitem "Restore" in the griffith menu and the code to do the job.

The patch here:

Index: griffith
===================================================================
--- griffith   (revision 314)
+++ griffith   (working copy)
@@ -413,7 +413,18 @@
          backup.restore(self)
       else:
          pass
+   def merge(self, *args):
+      response = gutils.question(self, \
+         self._("""Are you sure you want to mix?
+Your current movie collection will be mixed with a backup.
+You can't undo this operation."""), \
+         1, self.main_window)
+      if response == gtk.RESPONSE_YES:
+         backup.merge(self)
+      else:
+         pass
          
+      
    # cover
          
    def print_cover_simple_show(self, *args):
Index: lib/backup.py
===================================================================
--- lib/backup.py   (revision 314)
+++ lib/backup.py   (working copy)
@@ -76,3 +76,57 @@
       self.treeview_clicked()
       self.count_statusbar()
 
+def merge(self):
+   """merge a griffith compressed backup"""
+   filename = gutils.file_chooser(_("Restore Griffith backup"), \
+      action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons= \
+      (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, \
+      gtk.STOCK_OPEN, gtk.RESPONSE_OK))
+   if filename[0]:
+      from tempfile import mkdtemp
+      tmp_dir=mkdtemp()
+
+
+      response_unzip = gutils.merge(filename[0], tmp_dir, self.griffith_dir)
+      if not response_unzip:
+         gutils.error(self, _("Can't read backup file"), self.main_window)
+         return
+      
+      ndb=sql.GriffithSQL(self.config, self.debug, tmp_dir)
+      
+      for r in ndb.get_all_data():
+         #sqlstring="""INSERT into 'movies' ('id', 'original_title', 'title', 'director', 'plot', 'image', 'year',
+                        #'runtime', 'actors', 'country', 'genre', 'media', 'classification', 'studio', 'site', 'color',
+                        #'region', 'layers', 'condition', 'imdb', 'trailer', 'obs', 'num_media', 'loaned', 'rating', 'seen',
+                        #'number', 'volume_id', 'collection_id') VALUES (NULL"""
+         sqlstring="""INSERT into 'movies' VALUES (NULL"""
+         
+         next=gutils.find_next_available(self)
+
+         for c in range(len(r)-1):
+            if(c==5):
+               sqlstring+=", '"+str(next)+"'";
+            else:
+               sqlstring+=", '"+gutils.gescape(str(r[c+1]))+"'";
+         
+         sqlstring+=")";
+         #print sqlstring+"\n"
+
+         self.db.cursor.execute(sqlstring);
+      ndb.con.close();
+      
+      from shutil import rmtree
+      rmtree(tmp_dir)
+
+      self.db.con.close()
+      self.db = sql.GriffithSQL(self.config, self.debug, self.griffith_dir)
+      from initialize   import dictionaries
+      dictionaries(self)
+      gutils.info(self, _("Backup mixed"), self.main_window)
+      # let's refresh the treeview
+      self.populate_treeview(self.db.get_all_data())
+      self.total = self.db.count_records("movies")
+      self.select_last_row(self.total)
+      self.treeview_clicked()
+      self.count_statusbar()
+
Index: lib/widgets.py
===================================================================
--- lib/widgets.py   (revision 314)
+++ lib/widgets.py   (working copy)
@@ -274,6 +274,7 @@
       "on_save_preferences_clicked"           : self.save_preferences,
       "on_backup_activate"                    : self.backup,
       "on_restore_activate"                   : self.restore,
+      "on_merge_activate"         : self.merge,
       "on_cover_simple_activate"              : self.print_cover_simple_show,
       "on_cancel_print_cover_simple_clicked"  : self.print_cover_simple_hide,
       "on_b_print_cover_simple_clicked"       : self.print_cover_simple_process,
Index: lib/gutils.py
===================================================================
--- lib/gutils.py   (revision 314)
+++ lib/gutils.py   (working copy)
@@ -245,7 +245,38 @@
    except:
       return 0
    zip.close()
-         
+
+def merge(file, dir, target):
+   try:
+      zip = zipfile.ZipFile(file, 'r')
+      for each in zip.namelist():
+         file_to_restore = os.path.split(each)
+         if os.path.isdir(file_to_restore[1]):
+            pass
+         if file_to_restore[1].endswith('.gri'):
+            myfile = os.path.join(dir,file_to_restore[1])
+            outfile = open(myfile, 'wb')
+            outfile.write(zip.read(each))
+            outfile.flush()
+            outfile.close()
+         elif file_to_restore[1].endswith('config'):
+            myfile = os.path.join(dir,file_to_restore[1])
+            outfile = open(myfile, 'wb')
+            outfile.write(zip.read(each))
+            outfile.flush()
+            outfile.close()
+         elif file_to_restore[1].endswith('.jpg'):
+            mypath = os.path.join(target,'posters')
+            myfile = os.path.join(mypath,file_to_restore[1])
+            outfile = open(myfile, 'wb')
+            outfile.write(zip.read(each))
+            outfile.flush()
+            outfile.close()   
+      return 1
+   except:
+      return 0
+   zip.close()
+      
 # Messages
 
 def error(self, msg, parent=None):
Index: glade/griffith.glade
===================================================================
--- glade/griffith.glade   (revision 314)
+++ glade/griffith.glade   (working copy)
@@ -93,7 +93,27 @@
             </child>
           </widget>
         </child>
+        <child>
+          <widget class="GtkImageMenuItem" id="merge">
+            <property name="visible">True</property>
+            <property name="label" translatable="yes">Merge</property>
+            <property name="use_underline">True</property>
+            <signal name="activate" handler="on_merge_activate" last_modification_time="Tue, 19 Apr 2005 11:58:23 GMT"/>
 
+            <child internal-child="image">
+         <widget class="GtkImage" id="image377">
+           <property name="visible">True</property>
+           <property name="stock">gtk-open</property>
+           <property name="icon_size">1</property>
+           <property name="xalign">0.5</property>
+           <property name="yalign">0.5</property>
+           <property name="xpad">0</property>
+           <property name="ypad">0</property>
+         </widget>
+            </child>
+          </widget>
+        </child>
+
         <child>
           <widget class="GtkSeparatorMenuItem" id="separatormenuitem1">
             <property name="visible">True</property>
Logged
POX
Administrator
Hero Member
*****
Offline Offline

Posts: 512



View Profile WWW
« Reply #1 on: April 21, 2006, 07:33:13 PM »

Thanks!

We're in middle of ADOdb transition and after we'll finish, you will be able to use external databases like MySQL or PostgreSQL (no more problems with synchronization between remote computers). SQLite3 will be also used as default database engine (no more problems with old pysqlite1.x and sharing databases between Linux and Windows)

BTW: If its not a problem, use this site to submit patches

PS I will commit changes after I finish this ADOdb transition...

UPDATE: I've rewritten your patch (only new movies are merged now) and committed it. Thanks again!
« Last Edit: April 23, 2006, 08:58:05 PM by POX » Logged

Please, feel free to correct my English.
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2006-2007, Simple Machines Valid XHTML 1.0! Valid CSS!