Example #1
0
 def previewCards(self, note, type=0):
     if type == 0:
         cms = self.findTemplates(note)
     elif type == 1:
         cms = [c.template() for c in note.cards()]
     else:
         cms = note.model()['tmpls']
     if not cms:
         return []
     cards = []
     for template in cms:
         cards.append(self._newCard(note, template, 1, flush=False))
     return cards
Example #2
0
 def previewCards(self, note, type=0):
     if type == 0:
         cms = self.findTemplates(note)
     elif type == 1:
         cms = [c.template() for c in note.cards()]
     else:
         cms = note.model()['tmpls']
     if not cms:
         return []
     cards = []
     for template in cms:
         cards.append(self._newCard(note, template, 1, flush=False))
     return cards
Example #3
0
 def previewCards(self, note: Note, type: int = 0, did: None = None) -> List:
     if type == 0:
         cms = self.findTemplates(note)
     elif type == 1:
         cms = [c.template() for c in note.cards()]
     else:
         cms = note.model()["tmpls"]
     if not cms:
         return []
     cards = []
     for template in cms:
         cards.append(self._newCard(note, template, 1, flush=False, did=did))
     return cards
Example #4
0
File: deck.py Project: ChYi/libanki
 def previewCards(self, fact, type=0):
     "Return uncommited cards for preview."
     if type == 0:
         cms = self.findTemplates(fact, checkActive=True)
     elif type == 1:
         cms = [c.template() for c in fact.cards()]
     else:
         cms = fact.model().templates
     if not cms:
         return []
     cards = []
     for template in cms:
         cards.append(self._newCard(fact, template, 1, flush=False))
     return cards
Example #5
0
    def previewCards(self, note, type=0):
        """Returns a list of new cards, one by template. Those cards are not flushed, and their due is always 1.

        type 0 - when previewing in add dialog, only non-empty. Seems to be used only in tests.
        type 1 - when previewing edit, only existing. Seems to be used only in tests.
        type 2 - when previewing in models dialog (i.e. note type modifier), return the list of cards for every single template of the model.
        """
        #cms is the list of templates to consider
        if type == 0:
            cms = self.findTemplates(note)
        elif type == 1:
            cms = [c.template() for c in note.cards()]
        else:
            cms = note.model()['tmpls']
        if not cms:
            return []
        cards = []
        for template in cms:
            cards.append(self._newCard(note, template, 1, flush=False))
        return cards
Example #6
0
File: deck.py Project: ChYi/libanki
 def genCards(self, fact, templates):
     "Generate cards for templates if cards not empty. Return cards."
     cards = []
     # if random mode, determine insertion point
     if self.randomNew():
         # if this fact has existing new cards, use their due time
         due = self.db.scalar(
             "select due from cards where fid = ? and queue = 0", fact.id)
         due = due or random.randrange(1, self.conf['nextFid'])
     else:
         due = fact.id
     for template in self.findTemplates(fact, checkActive=False):
         if template not in templates:
             continue
         # if it doesn't already exist
         if not self.db.scalar(
             "select 1 from cards where fid = ? and ord = ?",
             fact.id, template['ord']):
             # create
             cards.append(self._newCard(fact, template, due))
     return cards
Example #7
0
def Sync(c):  
    if not mw.deck:
        c.close()
    else:        
        l = struct.pack("I", len(mw.deck.name()))
        c.sendall(l)
        c.sendall(mw.deck.name())                  
        d = c.recv(4)# maybee we should loop there too because we could get less than 4 bytes... it's quite unlikely though (besides, the ds buffers it's sends in a 8k stack)
        l = struct.unpack("i", d)[0]    
        if l >0:
            r = 0
            data = ''
            while r < l:
                d = c.recv(l-r)
                r += len(d)
                data += d                  
            data = data.split('\n')# we could remove the trailing \n before splitting too....
            for i in data:
                    if i.count(':')==2:
                        id, ease, reps = i.split(':')
                        ScoreCard(int(id),int(ease),int(reps))
                        
        # we prepare a list of cards to export, with the same function call used by AnkiMini and web Anki.  
        
        Old_getCardTables = mw.deck._getCardTables# overiding the _getCardTables function to get rid of the hardcoded limits and to add one column to the table 
        def New_getCardTables():
                mw.deck.checkDue()
                sel = """select id, factId, modified, question, answer, cardModelId, type, due, interval, factor, priority, reps from """
                new = mw.deck.newCardTable()
                rev = mw.deck.revCardTable()
                d = {}
                d['fail'] = Shuffle(mw.deck.s.all(sel + """cards where type = 0 and isDue = 1 and combinedDue <= :now limit %d""" % Limit, now = time.time() + DaysAhead * 24 * 3600))
                d['rev'] = Shuffle(mw.deck.s.all(sel + rev + " limit %d" % Limit))
                if mw.deck.newCountToday:
                        d['acq'] = Shuffle(mw.deck.s.all(sel + """%s where factId in (select distinct factId from cards where factId in (select factId from %s limit %d))""" % (new, new, Limit)))
                else:
                        d['acq'] = []
                if (not d['fail'] and not d['rev'] and not d['acq']):
                        d['fail'] = Shuffle(mw.deck.s.all(sel + "failedCards limit %d" % Limit))
                return d
        mw.deck._getCardTables = New_getCardTables      
        Export = mw.deck.getCards(lambda x:"%d\t%d\t%d\t%s\t%s" % (int(x[0]),int(x[7]),int(x[11]),striphtml(x[3]),striphtml(x[4])))
        mw.deck._getCardTables = Old_getCardTables
        
        cards = []
        if Export['status'] == 'cardsAvailable':
            # we make a card list out of the failed/rev/new cards
            n = min(Limit, len(Export['fail']))
            cards = Export['fail'][0:n-1]
            while n<=Limit:
                # add review/new cards
                if Export['acq'] and Export['newCardModulus'] and (n % Export['newCardModulus'] == 0):
                        cards.append(Export['acq'].pop(0))
                elif Export['rev']:
                        cards.append(Export['rev'].pop(0))
                elif Export['acq'] and Export['newCardModulus']:
                        cards.append(Export['acq'].pop(0))
                else:
                        break
                n += 1                                   
            
        srs = '\n'.join(cards)
        l = struct.pack("I", len(srs))
        c.sendall(l) 
        c.sendall(srs) 
        
        c.close()
        mw.loadDeck(mw.deck.path, sync=False)