Remove media patch, put usnLim in server code
Since dae/anki@d6874de8, usnLim always returns -1 regardless of the
`server` attribute.
Reference: d6874de8c8
			
			
This commit is contained in:
		
							parent
							
								
									c4fad436c1
								
							
						
					
					
						commit
						a83e68412d
					
				@ -1,34 +1,5 @@
 | 
				
			|||||||
import functools
 | 
					 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def __mediapatch():
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    Monkey-patch Anki's MediaManager to ignore the "server" attribute.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    It's needed because MediaManager's __init__(), connect() and close() are
 | 
					 | 
				
			||||||
    close to no-ops when self.col.server is True. If self.col.server is False,
 | 
					 | 
				
			||||||
    Syncer.usnLim() doesn't match entities that are supposed to be sent to the
 | 
					 | 
				
			||||||
    client, thus breaking server→client deck sync.
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def noserver(f):
 | 
					 | 
				
			||||||
        @functools.wraps(f)
 | 
					 | 
				
			||||||
        def wrapped(self, *args, **kwargs):
 | 
					 | 
				
			||||||
            orig = self.col.server
 | 
					 | 
				
			||||||
            self.col.server = False
 | 
					 | 
				
			||||||
            ret = f(self, *args, **kwargs)
 | 
					 | 
				
			||||||
            self.col.server = orig
 | 
					 | 
				
			||||||
            return ret
 | 
					 | 
				
			||||||
        return wrapped
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    from anki.media import MediaManager
 | 
					 | 
				
			||||||
    orig_init = MediaManager.__init__
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    MediaManager.__init__ = functools.wraps(MediaManager.__init__)(lambda self, col, _: orig_init(self, col, False))
 | 
					 | 
				
			||||||
    MediaManager.connect = noserver(MediaManager.connect)
 | 
					 | 
				
			||||||
    MediaManager.close = noserver(MediaManager.close)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
sys.path.insert(0, "/usr/share/anki")
 | 
					sys.path.insert(0, "/usr/share/anki")
 | 
				
			||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), "anki-bundled"))
 | 
					sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), "anki-bundled"))
 | 
				
			||||||
__mediapatch()
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -49,7 +49,7 @@ class CollectionWrapper:
 | 
				
			|||||||
        # mkdir -p the path, because it might not exist
 | 
					        # mkdir -p the path, because it might not exist
 | 
				
			||||||
        os.makedirs(os.path.dirname(self.path), exist_ok=True)
 | 
					        os.makedirs(os.path.dirname(self.path), exist_ok=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        col = anki.storage.Collection(self.path, server=True)
 | 
					        col = anki.storage.Collection(self.path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Do any special setup
 | 
					        # Do any special setup
 | 
				
			||||||
        if self.setup_new_collection is not None:
 | 
					        if self.setup_new_collection is not None:
 | 
				
			||||||
@ -61,7 +61,7 @@ class CollectionWrapper:
 | 
				
			|||||||
        """Open the collection, or create it if it doesn't exist."""
 | 
					        """Open the collection, or create it if it doesn't exist."""
 | 
				
			||||||
        if self.__col is None:
 | 
					        if self.__col is None:
 | 
				
			||||||
            if os.path.exists(self.path):
 | 
					            if os.path.exists(self.path):
 | 
				
			||||||
                self.__col = anki.storage.Collection(self.path, server=True)
 | 
					                self.__col = anki.storage.Collection(self.path)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                self.__col = self.__create_collection()
 | 
					                self.__col = self.__create_collection()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -91,6 +91,9 @@ class SyncCollectionHandler(anki.sync.Syncer):
 | 
				
			|||||||
            'cont': True,
 | 
					            'cont': True,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def usnLim(self):
 | 
				
			||||||
 | 
					        return "usn >= %d" % self.minUsn
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SyncMediaHandler(anki.sync.MediaSyncer):
 | 
					class SyncMediaHandler(anki.sync.MediaSyncer):
 | 
				
			||||||
    operations = ['begin', 'mediaChanges', 'mediaSanity', 'uploadChanges', 'downloadFiles']
 | 
					    operations = ['begin', 'mediaChanges', 'mediaSanity', 'uploadChanges', 'downloadFiles']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user