コード例 #1
0
class Map(CardType):

    id = "4"
    name = _("Map")

    # List and name the keys.
    fact_keys_and_names = [("loc", _("Location")),
                           ("blank", _("Blank map")),
                           ("marked", _("Marked map"))]

    # Recognition.
    v1 = FactView(_("Recognition"), "4.1")
    v1.q_fact_keys = ["_", "marked"]
    v1.a_fact_keys = ["loc", "marked"]
    v1.a_on_top_of_q = True

    # Production.
    v2 = FactView(_("Production"), "4.2")
    v2.q_fact_keys = ["loc", "blank"]
    v2.a_fact_keys = ["loc", "marked"]
    v2.a_on_top_of_q = True

    fact_views = [v1, v2]
    unique_fact_keys = ["loc"]
    required_fact_keys = ["loc", "blank", "marked"]

    def fact_data(self, card):
        _fact_data = copy.copy(card.fact.data)
        _fact_data["_"] = " "  # Insert a blank line to improve layout.
        return _fact_data

    def fact_key_format_proxies(self):
        return {"loc": "loc", "blank": "blank",
                "marked": "marked", "_": "loc"}
コード例 #2
0
ファイル: map.py プロジェクト: wojas/pomni
    def __init__(self):
        CardType.__init__(self)
        self.id = "4"
        self.name = _("Map")
        self.description = _("A card type for learning locations on a map")

        # List and name the keys.
        self.fields.append(("loc", _("Location")))
        self.fields.append(("blank", _("Blank map")))
        self.fields.append(("marked", _("Marked map")))
        
        # Recognition.
        v = FactView(1, _("Recognition"))
        v.q_fields = ["marked"]
        v.a_fields = ["loc"]
        v.required_fields = ["marked", "loc"]
        self.fact_views.append(v)

        # Production.
        v = FactView(2, _("Production"))
        v.q_fields = ["loc","blank"]
        v.a_fields = ["marked"]
        v.required_fields = ["loc", "blank", "marked"]
        v.a_on_top_of_q = True
        self.fact_views.append(v)
    
        # The following field needs to be unique.
        self.unique_fields = ["loc"]
コード例 #3
0
ファイル: both_ways.py プロジェクト: wojas/pomni
 def __init__(self, language_name=""):
     CardType.__init__(self)
     if not language_name:
         self.id = "2"
         self.name = _("Front-to-back and back-to-front")
         self.is_language = False
     else:
         self.id = "2_" + language_name
         self.name = language_name
         self.is_language = True
         
     # List and name the keys.
     self.fields.append(("q", _("Question")))
     self.fields.append(("a", _("Answer")))
     
     # Front-to-back.
     v = FactView(1, _("Front-to-back"))
     v.q_fields = ["q"]
     v.a_fields = ["a"]
     v.required_fields = ["q"]
     self.fact_views.append(v)
     
     # Back-to-front.
     v = FactView(2, _("Back-to-front"))
     v.q_fields = ["a"]
     v.a_fields = ["q"]
     v.required_fields = ["a"]
     self.fact_views.append(v)
     
     # The question field needs to be unique. As for duplicates is the answer
     # field, these are better handled through a synonym detection plugin.
     self.unique_fields = ["q"]
コード例 #4
0
ファイル: three_sided.py プロジェクト: wojas/pomni
    def __init__(self, language_name=""):
        CardType.__init__(self)
        if not language_name:
            self.id = "3"
            self.name = _("Foreign word with pronunciation")
            self.is_language = False
        else:
            self.id = "3_" + language_name
            self.name = language_name
            self.is_language = True

        # List and name the keys.
        self.fields.append(("f", _("Foreign word")))
        self.fields.append(("p", _("Pronunciation")))
        self.fields.append(("t", _("Translation")))

        # Recognition.
        v = FactView(1, _("Recognition"))
        v.q_fields = ["f"]
        v.a_fields = ["p", "t"]
        v.required_fields = ["f"]
        self.fact_views.append(v)

        # Production.
        v = FactView(2, _("Production"))
        v.q_fields = ["t"]
        v.a_fields = ["f", "p"]
        v.required_fields = ["t"]
        self.fact_views.append(v)
    
        # The foreign word field needs to be unique. As for duplicates is the
        # answer field, these are better handled through a synonym detection 
        # plugin.
        self.unique_fields = ["f"]
コード例 #5
0
ファイル: SQLite.py プロジェクト: bartosh/pomni
 def get_fact_view(self, id, id_is_internal):
     if id_is_internal:
         sql_res = self.con.execute("select * from fact_views where _id=?",
             (id, )).fetchone()
     else:
         sql_res = self.con.execute("select * from fact_views where id=?",
              (id, )).fetchone()            
     fact_view = FactView(sql_res["name"], sql_res["id"])
     fact_view._id = sql_res["_id"]
     for attr in ("q_fields", "a_fields"):
         setattr(fact_view, attr, eval(sql_res[attr]))
     for attr in ["a_on_top_of_q", "type_answer"]:
         setattr(fact_view, attr, bool(sql_res[attr]))
     self._get_extra_data(sql_res, fact_view)
     return fact_view
コード例 #6
0
ファイル: front_to_back.py プロジェクト: wojas/pomni
    def __init__(self):
        CardType.__init__(self)
        self.id = "1"
        self.name = _("Front-to-back only")

        # List and name the keys.
        self.fields.append(("q", _("Question")))
        self.fields.append(("a", _("Answer")))

        # Front-to-back.
        v = FactView(1, _("Front-to-back"))
        v.q_fields = ["q"]
        v.a_fields = ["a"]
        v.required_fields = ["q"]
        self.fact_views.append(v)
    
        # The following field needs to be unique.
        self.unique_fields = ["q"]
コード例 #7
0
ファイル: sqlite.py プロジェクト: wojas/pomni
    def get_view(self, view_id):
        """Get view object by id."""

        res = self.conn.execute("select name from views where id=?",
            (view_id, )).fetchone()

        fact_view = FactView(view_id, res["name"])

        # Find fact view in registered card_types and copy
        # *fields attributes from it
        for card_type in card_types():
            for view in card_type.fact_views:
                if view.name == res["name"]:
                    fact_view.q_fields = view.q_fields
                    fact_view.a_fields = view.a_fields
                    fact_view.required_fields = view.required_fields
                    return fact_view

        raise RuntimeError("Wrong view(id=%d) found in the database" % view_id)
コード例 #8
0
 def fact_view_from_log_entry(self, log_entry):
     # Get fact view object to be deleted now.
     if log_entry["type"] == EventTypes.DELETED_FACT_VIEW:
         return self.fact_view(log_entry["o_id"], is_id_internal=False)
     # Create an empty shell of fact view object that will be deleted later
     # during this sync.
     if "name" not in log_entry:
         return FactView("irrelevant", log_entry["o_id"])
     # Create fact view object.
     fact_view = FactView(log_entry["name"], log_entry["o_id"])
     fact_view.q_fact_keys = eval(log_entry["q_fact_keys"])
     fact_view.a_fact_keys = eval(log_entry["a_fact_keys"])
     fact_view.q_fact_key_decorators = eval(log_entry["q_fact_key_decorators"])
     fact_view.a_fact_key_decorators = eval(log_entry["a_fact_key_decorators"])
     fact_view.a_on_top_of_q = bool(eval(log_entry["a_on_top_of_q"]))
     fact_view.type_answer = bool(eval(log_entry["type_answer"]))
     if "extra" in log_entry:
         fact_view.extra_data = eval(log_entry["extra"])
     return fact_view
コード例 #9
0
class DecoratedVocabulary(Vocabulary):

    id = "3_decorated"
    name = _("Vocabulary (decorated)")

    # The keys we inherit from Vocabulary, we just override the FactViews.

    # Recognition.
    v1 = FactView(_("Recognition"), "3::1")
    v1.q_fact_keys = ["f"]
    v1.a_fact_keys = ["p_1", "m_1", "n"]
    v1.q_fact_key_decorators = {"f": "What is the translation of ${f}?"}

    # Production.
    v2 = FactView(_("Production"), "3::2")
    v2.q_fact_keys = ["m_1"]
    v2.a_fact_keys = ["f", "p_1", "n"]
    v2.q_fact_key_decorators = {"m_1": "How do you say ${m_1}?"}

    fact_views = [v1, v2]
コード例 #10
0
class Vocabulary(CardType):

    id = "3"
    name = _("Vocabulary")

    # List and name the keys.
    fact_keys_and_names = [("f", _("Foreign word or phrase")),
                           ("p_1", _("Pronunciation")), ("m_1", _("Meaning")),
                           ("n", _("Notes"))]

    # Recognition.
    v1 = FactView(_("Recognition"), "3.1")
    v1.q_fact_keys = ["f"]
    v1.a_fact_keys = ["p_1", "m_1", "n"]

    # Production.
    v2 = FactView(_("Production"), "3.2")
    v2.q_fact_keys = ["m_1"]
    v2.a_fact_keys = ["f", "p_1", "n"]

    fact_views = [v1, v2]
    unique_fact_keys = ["f"]
    required_fact_keys = ["f", "m_1"]
コード例 #11
0
class DecoratedThreeSided(CardType):

    id = "3_decorated"
    name = "Foreign word with pronunciation (decorated)"

    # List and name the keys.
    fact_keys_and_names = [("f", "Foreign word"), ("p_1", "Pronunciation"),
                           ("m_1", "Translation")]

    # Recognition.
    v1 = FactView("Recognition", "3.1")
    v1.q_fact_keys = ["f"]
    v1.a_fact_keys = ["p_1", "m_1"]
    v1.q_fact_key_decorators = {"f": "What is the translation of ${f}?"}

    # Production.
    v2 = FactView("Production", "3.2")
    v2.q_fact_keys = ["m_1"]
    v2.a_fact_keys = ["f", "p_1"]
    v2.q_fact_key_decorators = {"m_1": "How do you say ${t}?"}

    fact_views = [v1, v2]
    unique_fact_keys = ["f"]
    required_fact_keys = ["f", "m_1"]
コード例 #12
0
class BothWays(CardType):

    id = "2"
    name = _("Front-to-back and back-to-front")

    # List and name the keys.
    fact_keys_and_names = [("f", _("Front")), ("b", _("Back"))]

    # Front-to-back.
    v1 = FactView(_("Front-to-back"), "2.1")
    v1.q_fact_keys = ["f"]
    v1.a_fact_keys = ["b"]

    # Back-to-front.
    v2 = FactView(
        _("Back-to-front"),
        "2.2",
    )
    v2.q_fact_keys = ["b"]
    v2.a_fact_keys = ["f"]

    fact_views = [v1, v2]
    required_fact_keys = ["f", "b"]
    unique_fact_keys = ["f"]
コード例 #13
0
class FrontToBack(CardType):

    id = "1"
    name = _("Front-to-back only")

    # List and name the keys.
    fact_keys_and_names = [("f", _("Front")), ("b", _("Back"))]

    # Front-to-back.
    v = FactView(_("Front-to-back"), "1.1")
    v.q_fact_keys = ["f"]
    v.a_fact_keys = ["b"]

    fact_views = [v]
    unique_fact_keys = ["f"]
    required_fact_keys = ["f"]
コード例 #14
0
class Cloze(CardType):
    """CardType to do cloze deletion on a string, e.g. "The political parties in
    the US are the [democrats] and the [republicans]." would give the following
    cards:

    Q:The political parties in the US are the [...] and the republicans.
    A:democrats

    Q:The political parties in the US are the democrats and the [...].
    A:republicans

    Illustration of a CardType which does not use the traditional FactView
    mechanism.

    This is implemented by creating cards which contain extra_data entries
    "cloze" and "index", containing e.g. "democrats" and 0. Storing both the
    cloze and its index allows us to have enough data to support all possible
    editing operations.

    """

    id = "5"
    name = _("Cloze deletion")

    fact_keys_and_names = [("text", _("Text"))]
    unique_fact_keys = ["text"]
    required_fact_keys = ["text"]

    v = FactView(_("Cloze"), "5.1")
    v.q_fact_keys = ["f"]  # Generated on the fly.
    v.a_fact_keys = ["b"]  # Generated on the fly.
    fact_views = [v]

    def fact_key_format_proxies(self):
        return {"text": "text", "f": "text", "b": "text"}

    def is_fact_data_valid(self, fact_data):
        text = fact_data["text"]
        for f in self.component_manager.all("hook", "preprocess_cloze"):
            text = f.run(text)
        return bool(cloze_re.search(text))

    def q_a_from_cloze(self, text, index):
        """Auxiliary function used by other card types to return question
        and answer for the cloze with a given index in a text which can have
        the following form:

        La [casa:house] es [grande:big]

        Use 'index=-1' to get the cloze text without brackets and without
        hints.

        """

        for f in self.component_manager.all("hook", "preprocess_cloze"):
            text = f.run(text)
        cursor = 0
        current_index = 0
        question = text
        answer = None
        while True:
            cursor = text.find("[", cursor)
            if cursor == -1:
                break
            cloze = text[cursor + 1:text.find("]", cursor)]
            if ":" in cloze:
                cloze_without_hint, hint = cloze.split(":", 1)
            else:
                cloze_without_hint, hint = cloze, "..."
            if current_index == index:
                question = question.replace(\
                    "[" + cloze + "]", "[" + hint + "]", 1)
                answer = cloze_without_hint
            else:
                question = question.replace(\
                    "[" + cloze + "]", cloze_without_hint, 1)
            cursor += 1
            current_index += 1
        for f in self.component_manager.all("hook", "postprocess_q_a_cloze"):
            question, answer = f.run(question, answer)
        return question, answer

    def fact_data(self, card):
        question, answer = self.q_a_from_cloze\
            (card.fact["text"], card.extra_data["index"])
        return {"f": question, "b": answer}

    def create_sister_cards(self, fact):
        cards = []
        text = fact["text"]
        for f in self.component_manager.all("hook", "preprocess_cloze"):
            text = f.run(text)
        for match in cloze_re.finditer(text):
            card = Card(self, fact, self.fact_views[0])
            card.extra_data["cloze"] = match.group(1)
            card.extra_data["index"] = len(cards)
            cards.append(card)
        return cards

    def _edit_clozes(self, fact, new_fact_data, cloze_fact_key,
                     cloze_fact_view):
        """Auxiliary function used by other card types to when editing clozes.
        Should take into account that not all fact views are cloze-based.

        """

        new_cards, edited_cards, deleted_cards = [], [], []
        old_clozes = cloze_re.findall(fact[cloze_fact_key])
        new_clozes = cloze_re.findall(new_fact_data[cloze_fact_key])
        # If the number of clozes is equal, just edit the existing cards.
        if len(old_clozes) == len(new_clozes):
            for card in self.database().cards_from_fact(fact):
                if "cloze" in card.extra_data:
                    index = card.extra_data["index"]
                    card.extra_data["cloze"] = new_clozes[index]
                edited_cards.append(card)
        # If not, things are a little more complicated.
        else:
            new_clozes_processed = set()
            for card in self.database().cards_from_fact(fact):
                if "cloze" in card.extra_data:
                    old_cloze = card.extra_data["cloze"]
                    index = card.extra_data["index"]
                    if old_cloze in new_clozes:
                        new_index = new_clozes.index(old_cloze)
                        card.extra_data["cloze"] = new_clozes[new_index]
                        card.extra_data["index"] = new_index
                        new_clozes_processed.add(new_clozes[new_index])
                        edited_cards.append(card)
                    else:
                        deleted_cards.append(card)
                else:
                    edited_cards.append(card)
            for new_cloze in set(new_clozes).difference(new_clozes_processed):
                new_index = new_clozes.index(new_cloze)
                card = Card(self, fact, cloze_fact_view)
                card.extra_data["cloze"] = new_cloze
                card.extra_data["index"] = new_index
                new_cards.append(card)
        return new_cards, edited_cards, deleted_cards

    def edit_fact(self, fact, new_fact_data):
        return self._edit_clozes(fact, new_fact_data, "text",
                                 self.fact_views[0])
コード例 #15
0
    def do_import(self, filename, extra_tag_names=""):
        self.main_widget().show_information(_(\
"Note that while you can edit imported cards, adding new cards to Anki's card types is currently not supported.\n\nAlso, in case you run into problems, don't hesitate to contact the developers."))
        FileFormat.do_import(self, filename, extra_tag_names)
        w = self.main_widget()
        db = self.database()
        # Preprocess apkg files.
        tmp_dir = None
        if filename.endswith(".apkg"):
            tmp_dir = self.extract_apkg(filename)
            filename = os.path.join(tmp_dir, "collection.anki2")
        # Set up tag cache.
        tag_with_name = TagCache(self.component_manager)
        # Open database.
        con = sqlite3.connect(filename)
        # Copy media directory.
        w.set_progress_text(_("Copying media files..."))
        src = filename.replace(".anki2", ".media")
        dst = db.media_dir()
        number_of_files = len(os.listdir(src))
        w.set_progress_range(number_of_files)
        w.set_progress_update_interval(number_of_files / 50)
        for item in os.listdir(src):
            shutil.copy(os.path.join(src, item), os.path.join(dst, item))
            w.increase_progress(1)
        # Import collection table.
        w.set_progress_text(_("Importing card types..."))
        # Too few in number to warrant counted progress bar.
        card_type_for_mid = {}  # mid: model id
        deck_name_for_did = {}  # did: deck id
        for id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, \
            dconf, tags in con.execute("""select id, crt, mod, scm, ver, dty,
            usn, ls, conf, models, decks, dconf, tags from col"""):
            # mod: modification time, ignore.
            # scm: schema modification time, ignore.
            # ver: schema version, ignore.
            # dty: no longer used according to Anki source.
            # usn: syncing related, ignore.
            # ls: last sync, ignore.
            # conf: configuration, ignore.
            # dconf: deck configuration, ignore.
            # tags: list of tags, but they turn up later in the notes, ignore.
            collection_creation_time = crt
            decks = json.loads(decks)
            # Decks will be converted to Tags when creating cards.
            for did in decks:
                deck_name_for_did[int(did)] = decks[did]["name"]
            # Models will be converted to CardTypes
            models = json.loads(models)
            for mid in models:  # mid: model id
                card_type_id = "7::" + mid
                card_type_already_imported = \
                    db.has_card_type_with_id(card_type_id)
                if card_type_already_imported:
                    card_type = self.component_manager.card_type_with_id[\
                        card_type_id]
                else:
                    card_type = MSided(self.component_manager)
                card_type.name = models[mid]["name"]
                card_type.id = card_type_id
                card_type.hidden_from_UI = False
                card_type_for_mid[int(mid)] = card_type
                vers = models[mid]["vers"]  # Version, ignore.
                tags = models[mid]["tags"]  # Seems empty, ignore.
                did = models[mid]["did"]  # Deck id, ignore.
                usn = models[mid]["usn"]  # Syncing related, ignore.
                if "req" in models[mid]:
                    required = models[mid]["req"]
                    # Cache for a calculation to determine which fields are
                    # required. "req": [[0, "all", [0]]]
                    # Not yet implemented.
                else:
                    required = []
                flds = models[mid]["flds"]
                flds.sort(key=lambda x: x["ord"])
                card_type.fact_keys_and_names = []
                for field in flds:
                    card_type.fact_keys_and_names.append(\
                        (str(field["ord"]), field["name"]))
                    media = field["media"]  # Reserved for future use, ignore.
                    sticky = field["sticky"]  # Sticky field, ignore.
                    rtl = field["rtl"]  # Text direction, ignore.
                    font_string = field["font"] + "," + str(field["size"]) + \
                        ",-1,5,50,0,0,0,0,0,Regular"
                    self.config().set_card_type_property(
                        "font", font_string, card_type, str(field["ord"]))
                sortf = models[mid]["sortf"]  # Sorting field, ignore.
                tmpls = models[mid]["tmpls"]
                tmpls.sort(key=lambda x: x["ord"])
                # Fact views.
                card_type.fact_views = []
                for template in tmpls:
                    fact_view_id = card_type.id + "." + str(template["ord"])
                    fact_view_already_imported = \
                        db.has_fact_view_with_id(fact_view_id)
                    if fact_view_already_imported:
                        fact_view = db.fact_view(\
                            fact_view_id, is_id_internal=False)
                        fact_view.name = template["name"]
                    else:
                        fact_view = FactView(template["name"], fact_view_id)
                    fact_view.extra_data["qfmt"] = template["qfmt"]
                    fact_view.extra_data["afmt"] = template["afmt"]
                    fact_view.extra_data["bqfmt"] = template["bqfmt"]
                    fact_view.extra_data["bafmt"] = template["bafmt"]
                    fact_view.extra_data["ord"] = template["ord"]
                    did = template["did"]  # Deck id, ignore.
                    card_type.fact_views.append(fact_view)
                    if fact_view_already_imported:
                        db.update_fact_view(fact_view)
                    else:
                        db.add_fact_view(fact_view)
                mod = models[mid]["mod"]  # Modification time, ignore.
                type_ = models[mid]["type"]  # 0: standard, 1 cloze
                id = models[mid]["id"]
                css = models[mid]["css"]
                latex_preamble = models[mid]["latexPre"]  # Ignore.
                latex_postamble = models[mid]["latexPost"]  # Ignore.
                # Save to database.
                card_type.extra_data = {"css": css, "id": id, "type": type_}
                if card_type_already_imported:
                    db.update_card_type(card_type)
                else:
                    db.add_card_type(card_type)
        # nid are Anki-internal indices for notes, so we need to temporarily
        # store some information.
        tag_names_for_nid = {}
        card_type_for_nid = {}
        # Import facts and tags.
        w.set_progress_text(_("Importing notes..."))
        number_of_notes = con.execute(
            "select count() from notes").fetchone()[0]
        w.set_progress_range(number_of_notes)
        w.set_progress_update_interval(number_of_notes / 20)
        fact_for_nid = {}
        modification_time_for_nid = {}
        for id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data in \
            con.execute("""select id, guid, mid, mod, usn, tags, flds, sfld,
            csum, flags, data from notes"""):
            # usn: syncing related, ignore.
            # sfld: sorting field, ignore.
            # csum: checksum, ignore.
            # flags: seems empty, ignore.
            # data: seems empty, ignore.
            # Make compatible with openSM2sync:
            guid = guid.replace("`", "ap").replace("\"", "qu")
            guid = guid.replace("&", "am").replace("<",
                                                   "lt").replace(">", "gt")
            modification_time_for_nid[id] = mod
            card_type = card_type_for_mid[int(mid)]
            card_type_for_nid[id] = card_type
            fields = flds.split("\x1f")
            assert (len(fields) == len(card_type.fact_keys_and_names))
            fact_data = {}
            for i in range(len(fields)):
                fact_key = card_type.fact_keys_and_names[i][0]
                data = fields[i]
                # Deal with sound tags.
                for match in sound_re.finditer(data):
                    fname = match.group("fname")
                    data = data.replace(\
                        match.group(0), "<audio src=\"" + fname + "\">")
                # Deal with latex tags.
                data = data.replace("[latex]", "<latex>")
                data = data.replace("[/latex]", "</latex>")
                data = data.replace("[$]", "<$>")
                data = data.replace("[/$]", "</$>")
                data = data.replace("[$$]", "<$$>")
                data = data.replace("[/$$]", "</$$>")
                fact_data[fact_key] = data
            if db.has_fact_with_id(guid):
                fact = db.fact(guid, is_id_internal=False)
                fact.data = fact_data
                db.update_fact(fact)
            else:
                fact = Fact(fact_data, id=guid)
                db.add_fact(fact)
            fact_for_nid[id] = fact
            tag_names_for_nid[id] = tags
            w.increase_progress(1)
        # Import logs. This needs to happen before creating the cards,
        # otherwise, the sync protocol will use the scheduling data from the
        # latest repetition log, instead of the correct current one.
        w.set_progress_text(_("Importing logs..."))
        number_of_logs = con.execute(
            "select count() from revlog").fetchone()[0]
        w.set_progress_range(number_of_logs)
        w.set_progress_update_interval(number_of_logs / 20)
        for id, cid, usn, ease, ivl, lastIvl, factor, time, type_ in \
            con.execute("""select id, cid, usn, ease, ivl, lastIvl, factor,
            time, type from revlog"""):
            # usn: syncing related, ignore.
            if type_ == 0:  # Acquisition phase.
                grade = 0
            else:  # Retention phase.
                grade = ease + 1  # Anki ease is from 1 to 4.
            timestamp = int(id / 1000)
            scheduled_interval = lastIvl * 86400 if lastIvl > 0 else 0
            new_interval = ivl * 86400 if ivl > 0 else 0
            next_rep = timestamp + new_interval
            easiness = factor / 1000 if factor else 2.5
            db.log_repetition(timestamp=timestamp,
                              card_id=cid,
                              grade=grade,
                              easiness=easiness,
                              acq_reps=0,
                              ret_reps=0,
                              lapses=0,
                              acq_reps_since_lapse=0,
                              ret_reps_since_lapse=0,
                              scheduled_interval=scheduled_interval,
                              actual_interval=scheduled_interval,
                              thinking_time=int(time / 1000),
                              next_rep=next_rep,
                              scheduler_data=0)
            w.increase_progress(1)
        # Import cards.
        w.set_progress_text(_("Importing cards..."))
        number_of_cards = con.execute(
            "select count() from cards").fetchone()[0]
        w.set_progress_range(number_of_cards)
        w.set_progress_update_interval(number_of_cards / 20)
        for id, nid, did, ord, mod, usn, type_, queue, due, ivl, factor, reps, \
            lapses, left, odue, odid, flags, data in con.execute("""select id,
            nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps,
            lapses, left, odue, odid, flags, data from cards"""):
            # type: 0=new, 1=learning, 2=due
            # queue: same as above, and -1=suspended,
            #        -2=user buried, -3=sched buried
            # due is used differently for different queues.
            # - new queue: note id or random int
            # - rev queue: integer day
            # - lrn queue: integer timestamp
            # In Mnemosyne, type=2 / rev queue corresponds to grades >= 2.
            # mod: modification time, but gets updated on each answer.
            # usn: syncing related, ignore.
            # left: repetitions left to graduation, ignore.
            # odue: original due, related to filtered decks, ignore.
            # odid: original deck id, related to filtered decks, ignore.
            # flags: seems empty, ignore.
            # data: seems empty, ignore
            fact = fact_for_nid[nid]
            card_type = card_type_for_nid[nid]
            creation_time = int(nid / 1000)
            if card_type.extra_data["type"] == 0:
                fact_view = card_type.fact_views[ord]
            else:  # Cloze.
                fact_view = card_type.fact_views[0]
            already_imported = db.has_card_with_id(id)
            if already_imported:
                card = db.card(id, is_id_internal=False)
                card.card_type = card_type
                card.fact = fact
                card.fact_view = fact_view
                card.creation_time = creation_time
            else:
                card = Card(card_type,
                            fact,
                            fact_view,
                            creation_time=creation_time)
            card.id = id
            card.extra_data["ord"] = ord  # Needed separately for clozes.
            tag_names = [tag_name.strip() for \
                             tag_name in extra_tag_names.split(",")]
            tag_names += [tag_name.strip() for \
                             tag_name in tag_names_for_nid[nid].split(" ")]
            tag_names += [deck_name_for_did[did].strip().replace(",", ";")]
            for tag_name in tag_names:
                if tag_name:
                    card.tags.add(tag_with_name[tag_name])
            card.next_rep = collection_creation_time + due * 86400
            card.last_rep = card.next_rep - ivl * 86400
            card.easiness = factor / 1000 if factor else 2.5
            card.acq_reps = 1  # No information.
            card.ret_reps = reps
            card.lapses = lapses
            card.acq_reps_since_lapse = card.acq_reps  # No information.
            card.ret_reps_since_lapse = card.ret_reps  # No information.
            card.modification_time = modification_time_for_nid[nid]
            self.active = (queue >= 0)
            if type_ == 0:  # 'new', unseen.
                card.reset_learning_data()
            elif type_ == 1:  # 'learning', acquisition phase.
                card.grade = 0
                card.last_rep = mod
                card.next_rep = mod
            else:  # 'due', retention phase.
                card.grade = 4  # No information.
            if card.grade >= 2:
                assert card.ret_reps_since_lapse != 0  # Issue #93 on github.
            if already_imported:
                db.update_card(card)
            else:
                db.add_card(card)
            w.increase_progress(1)
        # Create criteria for 'database' tags.
        for deck_name in deck_name_for_did.values():
            deck_name = deck_name.strip().replace(",", ";")
            if deck_name in [criterion.name for criterion in db.criteria()]:
                continue
            tag = tag_with_name[deck_name]
            criterion = DefaultCriterion(\
                component_manager=self.component_manager)
            criterion.name = deck_name
            criterion._tag_ids_active.add(tag._id)
            criterion._tag_ids_forbidden = set()
            db.add_criterion(criterion)
        # Clean up.
        con.close()
        if tmp_dir:
            shutil.rmtree(tmp_dir)
        w.close_progress()
        self.warned_about_missing_media = False
コード例 #16
0
ファイル: sentence.py プロジェクト: vdsukhov/mnemosyne
class Sentence(Cloze):

    """A card type using sentences to study foreign languages.

    Apart from simple recognition of the sentence, you can also add production
    cards using close deletion. E.g. if in the sentence field you write
    "La [casa:house] es [grande:big]", you'll get cards with questions like
    "La [house] es grande".

    """

    id = "6"
    name = _("Sentence")

    # List and name the keys.
    fact_keys_and_names = [("f", _("Sentence")),
                           ("p_1", _("Pronunciation")),
                           ("m_1", _("Meaning")),
                           ("n", _("Notes"))]

    # Recognition.
    v1 = FactView(_("Recognition"), "6.1")
    v1.q_fact_keys = ["f"]
    v1.a_fact_keys = ["p_1", "m_1", "n"]

    # Production.
    v2 = FactView(_("Production"), "6.2")
    v2.q_fact_keys = ["f"]  # Generated on the fly.
    v2.a_fact_keys = ["a", "p_1", "m_1", "n"] # Generated on the fly.

    fact_views = [v1, v2]
    unique_fact_keys = ["f"]
    required_fact_keys = ["f"]

    def fact_key_format_proxies(self):
        proxies = CardType.fact_key_format_proxies(self)
        proxies["a"] = "f"
        return proxies

    def is_fact_data_valid(self, fact_data):
        return CardType.is_fact_data_valid(self, fact_data)

    def fact_data(self, card):
        data = copy.copy(card.fact.data)
        # Recognition card.
        if card.fact_view == self.fact_views[0]:
            question, answer = self.q_a_from_cloze\
                (card.fact["f"], -1)
        # Production card.
        else:
            question, answer = self.q_a_from_cloze\
                (card.fact["f"], card.extra_data["index"])
            # Entire sentence clozed.
            if question.strip() == "[...]" and "m_1" in data:
                question = data["m_1"]
                data["m_1"] = ""
        data["f"] = question
        data["a"] = answer
        return data

    def create_sister_cards(self, fact):
        cards = [Card(self, fact, self.fact_views[0])]
        for match in cloze_re.finditer(fact["f"]):
            card = Card(self, fact, self.fact_views[1])
            card.extra_data["cloze"] = match.group(1)
            card.extra_data["index"] = len(cards) - 1
            cards.append(card)
        return cards

    def edit_fact(self, fact, new_fact_data):
        return self._edit_clozes(fact, new_fact_data,
            "f", self.fact_views[1])