def binary_filename(self, store_pregenerated_data, interested_in_old_reps):
     self.database.release_connection()
     # Copy the database to a temporary file.
     self.tmp_name = os.path.join(os.path.dirname(self.database._path),
                                  "__FORSTREAMING__.db")
     copy(self.database._path, self.tmp_name)
     # Delete old reps if needed.
     if not interested_in_old_reps:
         con = sqlite3.connect(self.tmp_name)
         con.execute("delete from log where event_type=?",
                     (EventTypes.REPETITION, ))
         con.commit()
         con.execute("vacuum")
         con.close()
     # Delete pregerated data if needed.
     if not store_pregenerated_data:
         con = sqlite3.connect(self.tmp_name)
         con.executescript("""
         begin;
         drop index i_cards;
         create table cards_new(
             _id integer primary key,
             id text,
             card_type_id text,
             _fact_id integer,
             fact_view_id text,
             grade integer,
             next_rep integer,
             last_rep integer,
             easiness real,
             acq_reps integer,
             ret_reps integer,
             lapses integer,
             acq_reps_since_lapse integer,
             ret_reps_since_lapse integer,
             creation_time integer,
             modification_time integer,
             extra_data text default "",
             scheduler_data integer default 0,
             active boolean default 1
         );
         insert into cards_new select _id, id, card_type_id, _fact_id,
             fact_view_id, grade, next_rep, last_rep, easiness, acq_reps,
             ret_reps, lapses, acq_reps_since_lapse, ret_reps_since_lapse,
             creation_time, modification_time, extra_data, scheduler_data,
             active from cards;
         drop table cards;
         alter table cards_new rename to cards;
         create index i_cards on cards (id);
         commit;
         vacuum;
         """)
         con.close()
     return self.tmp_name
示例#2
0
 def binary_filename(self, store_pregenerated_data, interested_in_old_reps):
     self.database.release_connection()
     # Copy the database to a temporary file.
     self.tmp_name = os.path.join(os.path.dirname(self.database._path), "__FORSTREAMING__.db")
     copy(self.database._path, self.tmp_name)
     # Delete old reps if needed.
     if not interested_in_old_reps:
         con = sqlite3.connect(self.tmp_name)
         con.execute("delete from log where event_type=?", (EventTypes.REPETITION,))
         con.execute("vacuum")
         con.close()
     # Delete pregerated data if needed.
     if not store_pregenerated_data:
         con = sqlite3.connect(self.tmp_name)
         con.executescript(
             """
         begin;
         drop index i_cards;
         create table cards_new(
             _id integer primary key,
             id text,
             card_type_id text,
             _fact_id integer,
             fact_view_id text,
             grade integer,
             next_rep integer,
             last_rep integer,
             easiness real,
             acq_reps integer,
             ret_reps integer,
             lapses integer,
             acq_reps_since_lapse integer,
             ret_reps_since_lapse integer,
             creation_time integer,
             modification_time integer,
             extra_data text default "",
             scheduler_data integer default 0,
             active boolean default 1
         );
         insert into cards_new select _id, id, card_type_id, _fact_id,
             fact_view_id, grade, next_rep, last_rep, easiness, acq_reps,
             ret_reps, lapses, acq_reps_since_lapse, ret_reps_since_lapse,
             creation_time, modification_time, extra_data, scheduler_data,
             active from cards;
         drop table cards;
         alter table cards_new rename to cards;
         create index i_cards on cards (id);
         commit;
         vacuum;
         """
         )
         con.close()
     return self.tmp_name
    def preprocess_media(self, fact_data, tag_names):
        missing_media = False
        media_dir = self.database().media_dir()
        # os.path.normpath does not convert Windows separators to Unix
        # separators, so we need to make sure we internally store Unix paths.
        for fact_key in fact_data:
            for match in re_src.finditer(fact_data[fact_key]):
                fact_data[fact_key] = \
                    fact_data[fact_key].replace(match.group(),
                    match.group().replace("\\", "/"))
        # Convert sound tags to audio tags.
        for fact_key in fact_data:
            for match in re_sound.finditer(fact_data[fact_key]):
                fact_data[fact_key] = fact_data[fact_key].replace(
                    match.group(),
                    match.group().replace("<sound src", "<audio src"))
        # Copy files to media directory, creating subdirectories as we go.
        # For missing media, we change the tag to scr_missing, which makes it
        # easier for the user to identify the problem if there is more than 1
        # media file missing for a card.
        for fact_key in fact_data:
            for match in re_src.finditer(fact_data[fact_key]):
                filename = match.group(1)
                if not os.path.exists(filename) \
                    and not os.path.exists(\
                        expand_path(filename, self.import_dir)) \
                    and not os.path.exists(\
                        expand_path(filename, self.database().media_dir())):
                    fact_data[fact_key] = \
                        fact_data[fact_key].replace(match.group(),
                        "src_missing=\"%s\"" % filename)
                    missing_media = True
                    continue
                if not os.path.isabs(filename) and not os.path.exists(\
                        expand_path(filename, self.database().media_dir())):
                    source = expand_path(filename, self.import_dir)
                    dest = expand_path(filename, media_dir)
                    directory = os.path.dirname(dest)
                    if not os.path.exists(directory):
                        os.makedirs(directory)
                    copy(source, dest)
        if missing_media:
            tag_names.append(_("MISSING_MEDIA"))
            if not self.warned_about_missing_media:
                self.main_widget().show_information(\
 _("Warning: media files were missing. These cards have been tagged as MISSING_MEDIA. You must also change 'src_missing' to 'src' in the text of these cards."))
                self.warned_about_missing_media = True
示例#4
0
    def preprocess_media(self, fact_data, tag_names):
        missing_media = False
        media_dir = self.database().media_dir()
        # os.path.normpath does not convert Windows separators to Unix
        # separators, so we need to make sure we internally store Unix paths.
        for fact_key in fact_data:
            for match in re_src.finditer(fact_data[fact_key]):
                fact_data[fact_key] = \
                    fact_data[fact_key].replace(match.group(),
                    match.group().replace("\\", "/"))
        # Convert sound tags to audio tags.
        for fact_key in fact_data:
            for match in re_sound.finditer(fact_data[fact_key]):
                fact_data[fact_key] = fact_data[fact_key].replace(match.group(),
                    match.group().replace("<sound src", "<audio src"))
        # Copy files to media directory, creating subdirectories as we go.
        # For missing media, we change the tag to scr_missing, which makes it
        # easier for the user to identify the problem if there is more than 1
        # media file missing for a card.
        for fact_key in fact_data:
            for match in re_src.finditer(fact_data[fact_key]):
                filename = match.group(1)
                if not os.path.exists(filename) \
                    and not os.path.exists(\
                        expand_path(filename, self.import_dir)) \
                    and not os.path.exists(\
                        expand_path(filename, self.database().media_dir())):
                    fact_data[fact_key] = \
                        fact_data[fact_key].replace(match.group(),
                        "src_missing=\"%s\"" % filename)
                    missing_media = True
                    continue
                if not os.path.isabs(filename) and not os.path.exists(\
                        expand_path(filename, self.database().media_dir())):
                    source = expand_path(filename, self.import_dir)
                    dest = expand_path(filename, media_dir)
                    directory = os.path.dirname(dest)
                    if not os.path.exists(directory):
                        os.makedirs(directory)
                    copy(source, dest)
        if missing_media:
            tag_names.append(_("MISSING_MEDIA"))
            if not self.warned_about_missing_media:
                self.main_widget().show_information(\
 _("Warning: media files were missing. These cards have been tagged as MISSING_MEDIA. You must also change 'src_missing' to 'src' in the text of these cards."))
                self.warned_about_missing_media = True
示例#5
0
    def create_latex_img_file(self, latex_command):
        """Creates png file from a latex command if needed. Returns path name
        relative to the media dir, to be stored in the media database (hence
        with the linux path name convention). Also returns a boolean saying
        whether the img file was newly created or still in the cache (needed
        to speed up syncing).

        """

        img_name = self.latex_img_filename(latex_command)
        latex_dir = os.path.join(self.database().media_dir(), "_latex")
        filename = os.path.join(latex_dir, img_name)
        rel_filename = "_latex" + "/" + img_name  # To be stored in database.
        if not os.path.exists(filename):
            if not os.path.exists(latex_dir):
                os.makedirs(latex_dir)
            previous_dir = os.getcwd()
            os.chdir(latex_dir)
            try:
                if os.path.exists("tmp1.png"):
                    os.remove("tmp1.png")
                if os.path.exists("tmp.dvi"):
                    os.remove("tmp.dvi")
                if os.path.exists("tmp.aux"):
                    os.remove("tmp.aux")
                f = open("tmp.tex", "w", encoding="utf-8")
                print(self.config()["latex_preamble"], file=f)
                print(latex_command, file=f)
                print(self.config()["latex_postamble"], file=f)
                f.close()
                in_file = "tmp.tex"
                self._call_cmd(self.config()["latex"] + [in_file],
                               "latex_out.txt", in_file)
                self._call_cmd(self.config()["dvipng"], "dvipng_out.txt")
                if not os.path.exists("tmp1.png"):
                    return None
                copy("tmp1.png", img_name)
                self.log().added_media_file(rel_filename)
            finally:
                os.chdir(previous_dir)
        return rel_filename
示例#6
0
 def run(self, text, card, fact_key, **render_args):
     if "no_side_effects" in render_args and \
         render_args["no_side_effects"] == True:
         return text
     if not re_audio.search(text):
         return text
     index = 0
     for match in re_audio.finditer(text):
         sound_file = match.group(1)
         # Workaround for lack of unicode support in Popen/mplayer.
         try:
             sound_file.decode("ascii")
         except UnicodeEncodeError:
             media_dir = self.database().media_dir()
             try:
                 media_dir.decode("ascii")
             except:
                 import tempfile
                 tmp_handle, tmp_path = tempfile.mkstemp()
                 tmp_file = os.fdopen(tmp_handle, "w")
                 media_dir = os.path.dirname(tmp_path)
                 tmp_file.close()
                 os.remove(tmp_path)
             new_name = unicode(os.path.join(media_dir,
                 "___" + str(index) + "___.mp3"))
             copy(sound_file.replace("file:///", ""), new_name)
             index += 1
             sound_file = new_name
         start, stop = 0, 999999
         if match.group(2):
             start_match = re_start.search(match.group(2))
             if start_match:
                 start = float(start_match.group(1))
             stop_match = re_stop.search(match.group(2))
             if stop_match:
                 stop = float(stop_match.group(1))
         text = text.replace(match.group(0), "")
         self.review_widget().play_media(sound_file, start, stop)
     return text
示例#7
0
 def run(self, text, card, fact_key, **render_args):
     if "no_side_effects" in render_args and \
         render_args["no_side_effects"] == True:
         return text
     if not re_audio.search(text):
         return text
     index = 0
     for match in re_audio.finditer(text):
         sound_file = match.group(1)
         # Workaround for lack of unicode support in Popen/mplayer.
         try:
             sound_file.decode("ascii")
         except UnicodeEncodeError:
             media_dir = self.database().media_dir()
             try:
                 media_dir.decode("ascii")
             except:
                 import tempfile
                 tmp_handle, tmp_path = tempfile.mkstemp()
                 tmp_file = os.fdopen(tmp_handle, "w")
                 media_dir = os.path.dirname(tmp_path)
                 tmp_file.close()
                 os.remove(tmp_path)
             new_name = unicode(
                 os.path.join(media_dir, "___" + str(index) + "___.mp3"))
             copy(sound_file.replace("file:///", ""), new_name)
             index += 1
             sound_file = new_name
         start, stop = 0, 999999
         if match.group(2):
             start_match = re_start.search(match.group(2))
             if start_match:
                 start = float(start_match.group(1))
             stop_match = re_stop.search(match.group(2))
             if stop_match:
                 stop = float(stop_match.group(1))
         text = text.replace(match.group(0), "")
         self.review_widget().play_media(sound_file, start, stop)
     return text
示例#8
0
文件: latex.py 项目: tbabej/mnemosyne
    def create_latex_img_file(self, latex_command):

        """Creates png file from a latex command if needed. Returns path name
        relative to the media dir, to be stored in the media database (hence
        with the linux path name convention). Also returns a boolean saying
        whether the img file was newly created or still in the cache (needed
        to speed up syncing).

        """

        img_name = self.latex_img_filename(latex_command)
        latex_dir = os.path.join(self.database().media_dir(), "_latex")
        filename = os.path.join(latex_dir, img_name)
        rel_filename = "_latex" + "/" + img_name  # To be stored in database.
        if not os.path.exists(filename):
            if not os.path.exists(latex_dir):
                os.mkdir(latex_dir)
            previous_dir = os.getcwd()
            os.chdir(latex_dir)
            if os.path.exists("tmp1.png"):
                os.remove("tmp1.png")
            if os.path.exists("tmp.dvi"):
                os.remove("tmp.dvi")
            if os.path.exists("tmp.aux"):
                os.remove("tmp.aux")
            f = file("tmp.tex", "w")
            print >> f, self.config()["latex_preamble"]
            print >> f, latex_command.encode("utf-8")
            print >> f, self.config()["latex_postamble"]
            f.close()
            os.system(self.config()["latex"] + " tmp.tex 2>&1 1>latex_out.txt")
            os.system(self.config()["dvipng"].rstrip())
            if not os.path.exists("tmp1.png"):
                return None
            copy("tmp1.png", img_name)
            self.log().added_media_file(rel_filename)
            os.chdir(previous_dir)
        return rel_filename
示例#9
0
    def create_latex_img_file(self, latex_command):
        """Creates png file from a latex command if needed. Returns path name
        relative to the media dir, to be stored in the media database (hence
        with the linux path name convention). Also returns a boolean saying
        whether the img file was newly created or still in the cache (needed
        to speed up syncing).

        """

        img_name = self.latex_img_filename(latex_command)
        latex_dir = os.path.join(self.database().media_dir(), "_latex")
        filename = os.path.join(latex_dir, img_name)
        rel_filename = "_latex" + "/" + img_name  # To be stored in database.
        if not os.path.exists(filename):
            if not os.path.exists(latex_dir):
                os.mkdir(latex_dir)
            previous_dir = os.getcwd()
            os.chdir(latex_dir)
            if os.path.exists("tmp1.png"):
                os.remove("tmp1.png")
            if os.path.exists("tmp.dvi"):
                os.remove("tmp.dvi")
            if os.path.exists("tmp.aux"):
                os.remove("tmp.aux")
            f = file("tmp.tex", "w")
            print >> f, self.config()["latex_preamble"]
            print >> f, latex_command.encode("utf-8")
            print >> f, self.config()["latex_postamble"]
            f.close()
            os.system(self.config()["latex"] + " tmp.tex 2>&1 1>latex_out.txt")
            os.system(self.config()["dvipng"].rstrip())
            if not os.path.exists("tmp1.png"):
                return None
            copy("tmp1.png", img_name)
            self.log().added_media_file(rel_filename)
            os.chdir(previous_dir)
        return rel_filename