本文整理汇总了Python中picard.script.ScriptParser类的典型用法代码示例。如果您正苦于以下问题:Python ScriptParser类的具体用法?Python ScriptParser怎么用?Python ScriptParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ScriptParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _example_to_filename
def _example_to_filename(self, file):
settings = {
'windows_compatibility': self.ui.windows_compatibility.isChecked(),
'ascii_filenames': self.ui.ascii_filenames.isChecked(),
'rename_files': self.ui.rename_files.isChecked(),
'move_files': self.ui.move_files.isChecked(),
'use_va_format': False, # TODO remove
'file_naming_format': unicode(self.ui.file_naming_format.toPlainText()),
'move_files_to': os.path.normpath(unicode(self.ui.move_files_to.text()))
}
try:
if config.setting["enable_tagger_script"]:
script = config.setting["tagger_script"]
parser = ScriptParser()
parser.eval(script, file.metadata)
filename = file._make_filename(file.filename, file.metadata, settings)
if not settings["move_files"]:
return os.path.basename(filename)
return filename
except SyntaxError:
return ""
except ScriptError:
return ""
except TypeError:
return ""
except UnknownFunction:
return ""
示例2: _example_to_filename
def _example_to_filename(self, file):
settings = {
'windows_compatibility': self.ui.windows_compatibility.isChecked(),
'ascii_filenames': self.ui.ascii_filenames.isChecked(),
'rename_files': self.ui.rename_files.isChecked(),
'move_files': self.ui.move_files.isChecked(),
'use_va_format': False, # TODO remove
'file_naming_format': self.ui.file_naming_format.toPlainText(),
'move_files_to': os.path.normpath(self.ui.move_files_to.text())
}
try:
if config.setting["enable_tagger_scripts"]:
for s_pos, s_name, s_enabled, s_text in config.setting["list_of_scripts"]:
if s_enabled and s_text:
parser = ScriptParser()
parser.eval(s_text, file.metadata)
filename = file._make_filename(file.filename, file.metadata, settings)
if not settings["move_files"]:
return os.path.basename(filename)
return filename
except SyntaxError:
return ""
except ScriptError:
return ""
except TypeError:
return ""
示例3: _script_to_filename
def _script_to_filename(self, format, file_metadata, settings=None):
if settings is None:
settings = config.setting
metadata = Metadata()
if config.setting["clear_existing_tags"]:
metadata.copy(file_metadata)
else:
metadata.copy(self.orig_metadata)
metadata.update(file_metadata)
# make sure every metadata can safely be used in a path name
for name in metadata.keys():
if isinstance(metadata[name], basestring):
metadata[name] = sanitize_filename(metadata[name])
format = format.replace("\t", "").replace("\n", "")
filename = ScriptParser().eval(format, metadata, self)
if settings["ascii_filenames"]:
if isinstance(filename, unicode):
filename = unaccent(filename)
filename = replace_non_ascii(filename)
# replace incompatible characters
if settings["windows_compatibility"] or sys.platform == "win32":
filename = replace_win32_incompat(filename)
# remove null characters
filename = filename.replace("\x00", "")
return filename
示例4: _finalize_loading
def _finalize_loading(self, error):
if error:
self.metadata.clear()
self.metadata['album'] = _("[could not load album %s]") % self.id
del self._new_metadata
del self._new_tracks
self.update()
return
# Run metadata plugins
while self._metadata_plugins:
try:
self._metadata_plugins.popleft()()
except:
self.log.error(traceback.format_exc())
if not self._requests:
# Prepare parser for user's script
script = None
if self.config.setting["enable_tagger_script"]:
script = self.config.setting["tagger_script"]
parser = ScriptParser()
for track in self._new_tracks:
# Update the track with new album metadata, in case it
# was modified by plugins.
for key, values in self._new_metadata.rawitems():
track.metadata[key] = values[:]
# Run tagger script for each track
if script:
try:
parser.eval(script, track.metadata)
except:
self.log.error(traceback.format_exc())
# Strip leading/trailing whitespace
track.metadata.strip_whitespace()
# Run tagger script for the album itself
if script:
try:
parser.eval(script, self._new_metadata)
except:
self.log.error(traceback.format_exc())
self._new_metadata.strip_whitespace()
for track in self.tracks:
for file in list(track.linked_files):
file.move(self.unmatched_files)
self.metadata = self._new_metadata
self.tracks = self._new_tracks
del self._new_metadata
del self._new_tracks
self.loaded = True
self.match_files(self.unmatched_files.files)
self.update()
self.tagger.window.set_statusbar_message('Album %s loaded', self.id, timeout=3000)
while self._after_load_callbacks.qsize() > 0:
func = self._after_load_callbacks.get()
func()
示例5: check_format
def check_format(self):
parser = ScriptParser()
try:
parser.eval(unicode(self.ui.file_naming_format.toPlainText()))
except Exception as e:
raise OptionsCheckError("", str(e))
if self.ui.rename_files.isChecked():
if not unicode(self.ui.file_naming_format.toPlainText()).strip():
raise OptionsCheckError("", _("The file naming format must not be empty."))
示例6: process
def process(self, script):
files = self.files
for file in files:
m = file.metadata
parser = ScriptParser()
try:
parser.eval(script, m)
except:
log.error(traceback.format_exc())
m.strip_whitespace()
示例7: _make_image_filename
def _make_image_filename(self, filename, dirname, metadata):
filename = ScriptParser().eval(filename, metadata)
if config.setting["ascii_filenames"]:
if isinstance(filename, unicode):
filename = unaccent(filename)
filename = replace_non_ascii(filename)
if not filename:
filename = "cover"
if not os.path.isabs(filename):
filename = os.path.join(dirname, filename)
# replace incompatible characters
if config.setting["windows_compatibility"] or sys.platform == "win32":
filename = replace_win32_incompat(filename)
# remove null characters
filename = filename.replace("\x00", "")
return encode_filename(filename)
示例8: script_to_filename
def script_to_filename(naming_format, metadata, file=None, settings=None):
if settings is None:
settings = config.setting
# make sure every metadata can safely be used in a path name
for name in metadata:
values = [sanitize_filename(str(v)) for v in metadata.getall(name)]
metadata.set(name, values)
naming_format = naming_format.replace("\t", "").replace("\n", "")
filename = ScriptParser().eval(naming_format, metadata, file)
if settings["ascii_filenames"]:
filename = replace_non_ascii(filename, pathsave=True)
# replace incompatible characters
if settings["windows_compatibility"] or sys.platform == "win32":
filename = replace_win32_incompat(filename)
# remove null characters
filename = filename.replace("\x00", "")
return filename
示例9: _run_script
def _run_script(self, script):
s_name = script[1]
s_text = script[3]
parser = ScriptParser()
for obj in self._get_unique_metadata_objects():
try:
parser.eval(s_text, obj.metadata)
obj.update()
except ScriptError as e:
log.exception('Error running tagger script "%s" on object %r', s_name, obj)
msg = N_('Script error in "%(script)s": %(message)s')
mparms = {
'script': s_name,
'message': str(e),
}
self.tagger.window.set_statusbar_message(msg, mparms)
示例10: setUp
def setUp(self):
config.setting = {
'enabled_plugins': '',
}
self.parser = ScriptParser()
def func_noargstest(parser):
return ""
register_script_function(func_noargstest, "noargstest")
示例11: _parse_recording
def _parse_recording(self, recording):
recording_to_metadata(recording, self, self.config)
self._customize_metadata()
m = self.metadata
run_track_metadata_processors(self.album, m, None, recording)
if self.config.setting["enable_tagger_script"]:
script = self.config.setting["tagger_script"]
if script:
parser = ScriptParser()
try:
parser.eval(script, m)
except:
self.log.error(traceback.format_exc())
m.strip_whitespace()
self.loaded = True
if self.callback:
self.callback()
self.tagger.nats.update(True)
示例12: _parse_recording
def _parse_recording(self, recording):
m = self.metadata
recording_to_metadata(recording, m, self)
self.orig_metadata.copy(m)
self._customize_metadata()
run_track_metadata_processors(self.album, m, None, recording)
for s_name, s_text in enabled_tagger_scripts_texts():
parser = ScriptParser()
try:
parser.eval(s_text, m)
except ScriptError:
log.exception("Failed to run tagger script %s on track", s_name)
m.strip_whitespace()
self.loaded = True
self.status = None
if self.callback:
self.callback()
self.callback = None
self.album.update(True)
示例13: _parse_recording
def _parse_recording(self, recording):
m = self.metadata
recording_to_metadata(recording, m, self)
self._customize_metadata()
run_track_metadata_processors(self.album, m, None, recording)
if config.setting["enable_tagger_scripts"]:
for s_pos, s_name, s_enabled, s_text in config.setting["list_of_scripts"]:
if s_enabled and s_text:
parser = ScriptParser()
try:
parser.eval(s_text, m)
except:
log.error(traceback.format_exc())
m.strip_whitespace()
self.loaded = True
if self.callback:
self.callback()
self.callback = None
self.tagger.nats.update(True)
示例14: _example_to_filename
def _example_to_filename(self, file):
settings = {
'windows_compatible_filenames': self.ui.windows_compatible_filenames.isChecked(),
'ascii_filenames': self.ui.ascii_filenames.isChecked(),
'rename_files': self.ui.rename_files.isChecked(),
'move_files': self.config.setting["move_files"],
'use_va_format': self.ui.use_va_format.isChecked(),
'file_naming_format': unicode(self.ui.file_naming_format.toPlainText()),
'va_file_naming_format': unicode(self.ui.va_file_naming_format.toPlainText()),
'move_files_to': os.path.normpath(unicode(self.config.setting["move_files_to"])),
}
try:
if self.config.setting["enable_tagger_script"]:
script = self.config.setting["tagger_script"]
parser = ScriptParser()
parser.eval(script, file.metadata)
filename = file._make_filename(file.filename, file.metadata, settings)
return filename
except SyntaxError, e: return ""
except TypeError, e: return ""
except UnknownFunction, e: return ""
示例15: eval
def eval(self, script, context=None, file=None):
result = ScriptParser.eval(self, script, context, file)
return result.split(STOP_MARKER)[0]