本文整理汇总了Python中mnemosyne.libmnemosyne.utils.expand_path函数的典型用法代码示例。如果您正苦于以下问题:Python expand_path函数的具体用法?Python expand_path怎么用?Python expand_path使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了expand_path函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: file_open
def file_open(self):
self.stopwatch().pause()
db = self.database()
basedir = self.config().basedir
old_path = expand_path(self.config()["path"], basedir)
filename = self.main_widget().open_file_dialog(path=old_path,
filter=_("Mnemosyne databases") + " (*%s)" % db.suffix)
if not filename:
self.stopwatch().unpause()
return
if filename.startswith(os.path.join(basedir, "backups")):
result = self.main_widget().question_box(\
_("Do you want to restore from this backup?"),
_("Yes"), _("No"), "")
if result == 0: # Yes
db.abandon()
db_path = expand_path(self.config()["path"], basedir)
import shutil
shutil.copy(filename, db_path)
db.load(db_path)
self.review_controller().reset()
self.update_title()
self.stopwatch().unpause()
return
try:
self.log().saved_database()
db.backup()
db.unload()
except RuntimeError, error:
self.main_widget().error_box(unicode(error))
self.stopwatch().unpause()
return
示例2: _preprocess_media
def _preprocess_media(self, fact_data):
mediadir = self.database().mediadir()
# os.path.normpath does not convert Windows separators to Unix
# separators, so we need to make sure we internally store Unix paths.
for key in fact_data:
for match in re_src.finditer(fact_data[key]):
fact_data[key] = fact_data[key].replace(match.group(),
match.group().replace("\\", "/"))
# Convert sound tags to audio tags.
for key in fact_data:
for match in re_sound.finditer(fact_data[key]):
fact_data[key] = fact_data[key].replace(match.group(),
match.group().replace("sound", "audio"))
# Copy files to media directory, creating subdirectories as we go.
for key in fact_data:
for match in re_src.finditer(fact_data[key]):
filename = match.group(1)
if not os.path.isabs(filename):
subdir = os.path.dirname(filename)
subdirs = []
while subdir:
subdirs.insert(0, os.path.join(mediadir, subdir))
subdir = os.path.dirname(subdir)
for subdir in subdirs:
if not os.path.exists(subdir):
os.mkdir(subdir)
source = expand_path(filename, self.importdir)
dest = expand_path(filename, mediadir)
if not os.path.exists(source):
self.main_widget().information_box(\
_("Missing media file") + " %s" % source)
fact_data[key] = fact_data[key].replace(match.group(),
"src_missing=\"%s\"" % match.group(1))
else:
shutil.copy(source, dest)
示例3: show_insert_img_dialog
def show_insert_img_dialog(self, filter):
"""Show a file dialog filtered on the supported filetypes, get a
filename, massage it, and return it to the widget to be inserted.
There is more media file logic inside the database code too, as the
user could also just type in the html tags as opposed to passing
through the file selector here. The reason we don't do all the
operations in the database code, is that we want to display a nice
short relative path back in the edit field.
"""
from mnemosyne.libmnemosyne.utils import copy_file_to_dir
data_dir, media_dir = \
self.config().data_dir, self.database().media_dir()
path = expand_path(self.config()["import_img_dir"], data_dir)
filter = _("Image files") + " " + filter
filename = self.main_widget().get_filename_to_open(\
path, filter, _("Insert image"))
if not filename:
return ""
else:
self.config()["import_img_dir"] = contract_path(\
os.path.dirname(filename), data_dir)
filename = copy_file_to_dir(filename, media_dir)
return contract_path(filename, media_dir)
示例4: load
def load(self, path):
path = expand_path(path, config().basedir)
if self.is_loaded():
unload_database()
if not os.path.exists(path):
self.load_failed = True
raise LoadError
try:
infile = file(path, 'rb')
db = cPickle.load(infile)
self.start_date = db[0]
self.categories = db[1]
self.facts = db[2]
self.fact_views = db[3]
self.cards = db[4]
infile.close()
self.load_failed = False
except:
self.load_failed = True
raise InvalidFormatError(stack_trace=True)
# Work around a sip bug: don't store card types, but their ids.
for f in self.facts:
f.card_type = card_type_by_id(f.card_type)
# TODO: This was to remove database inconsistencies. Still needed?
#for c in self.categories:
# self.remove_category_if_unused(c)
config()["path"] = contract_path(path, config().basedir)
log().loaded_database()
for f in component_manager.get_all("function_hook", "after_load"):
f.run()
示例5: save
def save(self, path=None):
# Don't erase a database which failed to load.
if self.load_failed == True:
return -1
if not path:
path = self.config()["path"]
path = expand_path(path, self.config().basedir)
# Update version.
self.global_variables["version"] = self.version
# Work around a sip bug: don't store card types, but their ids.
for f in self.facts:
f.card_type = f.card_type.id
try:
# Write to a backup file first, as shutting down Windows can
# interrupt the dump command and corrupt the database.
outfile = file(path + "~", 'wb')
db = [self.tags, self.facts, self.cards,
self.global_variables]
cPickle.dump(db, outfile)
outfile.close()
shutil.move(path + "~", path) # Should be atomic.
except:
raise RuntimeError, _("Unable to save file.") \
+ "\n" + traceback_string()
self.config()["path"] = contract_path(path, self.config().basedir)
# Work around sip bug again.
for f in self.facts:
f.card_type = self.card_type_by_id(f.card_type)
示例6: load_database
def load_database(self, filename):
if not filename:
filename = self.config()["last_database"]
path = expand_path(filename, self.config().data_dir)
try:
if not os.path.exists(path):
try:
self.database().new(path)
except:
from mnemosyne.libmnemosyne.translator import _
raise RuntimeError(_("Previous drive letter no longer available."))
else:
self.database().load(path)
self.controller().update_title()
except RuntimeError, e:
from mnemosyne.libmnemosyne.translator import _
self.main_widget().show_error(unicode(e))
self.main_widget().show_information(\
_("If you are using a USB key, refer to the instructions on the website so as not to be affected by drive letter changes."))
success = False
while not success:
try:
self.database().abandon()
self.controller().show_open_file_dialog()
success = True
except RuntimeError, e:
self.main_widget().show_error(unicode(e))
示例7: new
def new(self, path):
if self.is_loaded():
self.unload()
self._path = expand_path(path, self.config().basedir)
if os.path.exists(self._path):
os.remove(self._path)
self.load_failed = False
# Create tables.
self.con.executescript(SCHEMA)
self.con.execute("insert into global_variables(key, value) values(?,?)",
("version", self.version))
self.con.execute("""insert into partnerships(partner, _last_log_id)
values(?,?)""", ("log.txt", 0))
self.con.commit()
self.config()["path"] = contract_path(self._path, self.config().basedir)
# Create default criterion.
from mnemosyne.libmnemosyne.activity_criteria.default_criterion import \
DefaultCriterion
self._current_criterion = DefaultCriterion(self.component_manager)
self.add_activity_criterion(self._current_criterion)
# Create media directory.
mediadir = self.mediadir()
if not os.path.exists(mediadir):
os.mkdir(mediadir)
os.mkdir(os.path.join(mediadir, "latex"))
示例8: show_save_file_as_dialog
def show_save_file_as_dialog(self):
self.stopwatch().pause()
if self.config()["single_database_help_shown"] == False:
self.main_widget().show_information(_(self.single_database_help))
self.config()["single_database_help_shown"] = True
self.flush_sync_server()
suffix = self.database().suffix
old_path = expand_path(self.config()["last_database"], self.config().data_dir)
old_media_dir = self.database().media_dir()
filename = self.main_widget().get_filename_to_save(path=old_path,
filter=_("Mnemosyne databases") + " (*%s)" % suffix)
if not filename:
self.stopwatch().unpause()
return
if filename.endswith("config.db"):
self.main_widget().show_information(\
_("The configuration database cannot be used to store cards."))
self.stopwatch().unpause()
return
if not filename.endswith(suffix):
filename += suffix
try:
self.database().save(filename)
new_media_dir = self.database().media_dir()
if old_media_dir == new_media_dir:
return
if os.path.exists(new_media_dir):
shutil.rmtree(new_media_dir)
shutil.copytree(old_media_dir, new_media_dir)
self.log().saved_database()
except RuntimeError, error:
self.main_widget().show_error(unicode(error.message))
self.stopwatch().unpause()
return
示例9: expand_tag
def expand_tag(self, tag, text):
# Add "=" to make sure we don't match "Application Data".
i = text.lower().find(tag + "=")
while i != -1:
start = text.find("\"", i)
end = text.find("\"", start + 1)
if start == -1 or end == -1:
break
if len(text[i:start].replace(" ", "")) > len(tag) + 1:
break
old_path = text[start+1:end]
if not old_path.startswith("http:"):
new_path = expand_path(old_path, self.database().media_dir())
if sys.platform == "win32":
new_path = "/" + new_path.replace("\\", "/")
new_path = new_path.replace("#", "%23")
text = text[:start+1] + "file://" + new_path + text[end:]
if sys.platform == "win32" and tag == "data":
text = text.replace("file:///", "")
# Since text is always longer now, we can start searching
# from the previous end tag.
i = text.lower().find(tag, end + 1)
# Replace code word 'db_media:///' by the absolute path for use e.g.
# in javascript.
if "db_media:///" in text:
text = text.replace("db_media:///",
self.database().media_dir().replace("\\", "/") + "/")
if not text.startswith("/"):
text = "/" + text
return text
示例10: _process_media
def _process_media(self, fact):
"""Copy the media files to the media directory and edit the media
table. We don't keep track of which facts use which media and delete
a media file if it's no longer in use. The reason for this is that some
people use the media directory as their only location to store their
media files, and also use these files for other purposes.
Note that not all 'added_media_file' events originated here, they are
also generated by the latex subsystem, or by checking for files which
were modified outside of Mnemosyne.
"""
for match in re_src.finditer("".join(fact.data.values())):
filename = match.group(2)
if filename.startswith("http:"):
continue
if len(filename) > 200:
self.main_widget().show_information(
_("Media filename rather long. This could cause problems using this file on a different OS.")
)
if "#" in filename:
self.main_widget().show_information(
_("Filename contains '#', which could cause problems on some operating systems.")
)
if not path_exists(filename) and not path_exists(expand_path(filename, self.media_dir())):
self.main_widget().show_error(_("Missing media file!") + "\n\n" + filename)
for fact_key, value in fact.data.iteritems():
fact.data[fact_key] = fact.data[fact_key].replace(
match.group(), 'src_missing="%s"' % match.group(2)
)
continue
# If needed, copy file to the media dir. Normally this happens when
# the user clicks 'Add image' e.g., but he could have typed in the
# full path directly.
if os.path.isabs(filename):
filename = copy_file_to_dir(filename, self.media_dir())
else: # We always store Unix paths internally.
filename = filename.replace("\\", "/")
for fact_key, value in fact.data.iteritems():
fact.data[fact_key] = value.replace(match.group(2), filename)
self.con.execute(
"""update data_for_fact set value=? where
_fact_id=? and key=?""",
(fact.data[fact_key], fact._id, fact_key),
)
if self.con.execute("select count() from media where filename=?", (filename,)).fetchone()[0] == 0:
self.con.execute(
"""insert into media(filename, _hash)
values(?,?)""",
(filename, self._media_hash(filename)),
)
# When we are applying log entries during sync or import, the
# side effects of e.g. ADDED_FACT events should not generate
# additional ADDED_MEDIA_FILE events at the remote partner, so
# we disable the logging of these side effects in that case.
if not self.syncing and not self.importing:
self.log().added_media_file(filename)
示例11: new
def new(self, path):
if self.is_loaded():
self.unload()
path = expand_path(path, self.config().basedir)
self.load_failed = False
self.save(contract_path(path, self.config().basedir))
self.config()["path"] = contract_path(path, self.config().basedir)
self.log().new_database()
示例12: preprocess_media
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
示例13: do_import
def do_import(self, filename, extra_tag_names=None):
db = self.database()
if filename.endswith("config.db"):
self.main_widget().show_error(\
_("The configuration database is not used to store cards."))
return
data_dir = self.config().data_dir
receiving_database_filename = \
expand_path(self.config()["last_database"], data_dir)
db.dump_to_science_log()
# Heuristic to check if we haven't imported this database before.
current_tag_ids = set([tag.id for tag in db.tags()])
db.load(filename)
tag_ids_to_import = set([tag.id for tag in db.tags()])
if len(tag_ids_to_import.intersection(current_tag_ids)) >= 2:
answer = self.main_widget().show_question(\
_("It looks like you've imported this database before. Importing it twice will generate duplicate log entries, which will skew your statistics. Do you want to continue?"),
_("Abort"), _("Continue"), "")
if answer == 0:
db.load(receiving_database_filename)
return
# Export to temporary *.cards file.
cards_format = Mnemosyne2Cards(self.component_manager)
tmp_cards_filename = os.path.join(data_dir, "TMP.cards")
cards_format.do_export(tmp_cards_filename, used_for_merging_dbs=True)
old_deactivated_card_type_fact_view_ids = \
db.current_criterion().deactivated_card_type_fact_view_ids
user_card_types = [card_type for card_type in db.card_types_in_use() \
if db.is_user_card_type(card_type)]
# Get config info to be merged.
old_config_dir = self.config().config_dir
self.config().config_dir = os.path.dirname(filename)
self.log().active = False
self.config().load()
old_config = self.config().copy()
self.config().config_dir = old_config_dir
self.config().load()
self.log().active = True
# Import the *.cards file into the receiving database.
db.load(receiving_database_filename)
log_index_before_import = db.current_log_index()
db.importing_with_learning_data = True
cards_format.do_import(\
tmp_cards_filename, extra_tag_names, show_metadata=False)
db.importing_with_learning_data = False
db.merge_logs_from_other_database(filename, log_index_before_import)
os.remove(tmp_cards_filename)
db.current_criterion().deactivated_card_type_fact_view_ids.update(\
old_deactivated_card_type_fact_view_ids)
db.set_current_criterion(db.current_criterion())
for property_name in ["background_colour", "font", "font_colour",
"alignment", "hide_pronunciation_field"]:
self.log().edited_setting(property_name)
for card_type in user_card_types:
if card_type.id in old_config[property_name]:
self.config()[property_name][card_type.id] = \
old_config[property_name][card_type.id]
db.skip_science_log()
示例14: insert_sound
def insert_sound(self):
path = expand_path(config()["import_sound_dir"])
fname = unicode(QFileDialog.getOpenFileName(self, _("Insert sound"),
path, _("Sound files") + \
" (*.wav *.mp3 *.ogg *.WAV *.MP3 *.OGG)"))
if fname:
self.insertPlainText("<sound src=\""+contract_path(fname)+"\">")
config()["import_sound_dir"] = \
contract_path(os.path.dirname(fname))
示例15: all_media_filenames
def all_media_filenames(self):
filenames = []
for filename in [cursor[0] for cursor in self.con.execute(\
"""select object_id from log where event_type=? or
event_type=?""", (EventTypes.ADDED_MEDIA,
EventTypes.UPDATED_MEDIA))]:
if os.path.exists(expand_path(filename, self.mediadir())):
filenames.append(filename)
return filenames