本文整理汇总了Python中mediagoblin.processing.FilenameBuilder类的典型用法代码示例。如果您正苦于以下问题:Python FilenameBuilder类的具体用法?Python FilenameBuilder怎么用?Python FilenameBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FilenameBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_pdf
def process_pdf(proc_state):
"""Code to process a pdf file. Will be run by celery.
A Workbench() represents a local tempory dir. It is automatically
cleaned up when this function exits.
"""
entry = proc_state.entry
workbench = proc_state.workbench
queued_filename = proc_state.get_queued_filename()
name_builder = FilenameBuilder(queued_filename)
# Copy our queued local workbench to its final destination
original_dest = name_builder.fill('{basename}{ext}')
proc_state.copy_original(original_dest)
# Create a pdf if this is a different doc, store pdf for viewer
ext = queued_filename.rsplit('.', 1)[-1].lower()
if ext == 'pdf':
pdf_filename = queued_filename
else:
pdf_filename = queued_filename.rsplit('.', 1)[0] + '.pdf'
unoconv = where('unoconv')
call(executable=unoconv,
args=[unoconv, '-v', '-f', 'pdf', queued_filename])
if not os.path.exists(pdf_filename):
_log.debug('unoconv failed to convert file to pdf')
raise BadMediaFail()
proc_state.store_public(keyname=u'pdf', local_file=pdf_filename)
pdf_info_dict = pdf_info(pdf_filename)
for name, width, height in [
(u'thumb', mgg.global_config['media:thumb']['max_width'],
mgg.global_config['media:thumb']['max_height']),
(u'medium', mgg.global_config['media:medium']['max_width'],
mgg.global_config['media:medium']['max_height']),
]:
filename = name_builder.fill('{basename}.%s.png' % name)
path = workbench.joinpath(filename)
create_pdf_thumb(pdf_filename, path, width, height)
assert(os.path.exists(path))
proc_state.store_public(keyname=name, local_file=path)
proc_state.delete_queue_file()
entry.media_data_init(**pdf_info_dict)
entry.save()
示例2: process_image
def process_image(proc_state):
"""Code to process an image. Will be run by celery.
A Workbench() represents a local tempory dir. It is automatically
cleaned up when this function exits.
"""
entry = proc_state.entry
workbench = proc_state.workbench
# Conversions subdirectory to avoid collisions
conversions_subdir = os.path.join(
workbench.dir, 'conversions')
os.mkdir(conversions_subdir)
queued_filename = proc_state.get_queued_filename()
name_builder = FilenameBuilder(queued_filename)
# EXIF extraction
exif_tags = extract_exif(queued_filename)
gps_data = get_gps_data(exif_tags)
# Always create a small thumbnail
resize_tool(proc_state, True, 'thumb',
name_builder.fill('{basename}.thumbnail{ext}'),
conversions_subdir, exif_tags)
# Possibly create a medium
resize_tool(proc_state, False, 'medium',
name_builder.fill('{basename}.medium{ext}'),
conversions_subdir, exif_tags)
# Copy our queued local workbench to its final destination
proc_state.copy_original(name_builder.fill('{basename}{ext}'))
# Remove queued media file from storage and database
proc_state.delete_queue_file()
# Insert exif data into database
exif_all = clean_exif(exif_tags)
if len(exif_all):
entry.media_data_init(exif_all=exif_all)
if len(gps_data):
for key in list(gps_data.keys()):
gps_data['gps_' + key] = gps_data.pop(key)
entry.media_data_init(**gps_data)
示例3: common_setup
def common_setup(self):
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self._set_ext()
self._set_model()
self._set_greatest()
示例4: common_setup
def common_setup(self):
"""
Set up common pdf processing steps
"""
# Pull down and set up the processing file
self.process_filename = get_process_filename(self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self._set_pdf_filename()
示例5: common_setup
def common_setup(self):
self.video_config = mgg \
.global_config['plugins'][MEDIA_TYPE]
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self.transcoder = transcoders.VideoTranscoder()
self.did_transcode = False
示例6: common_setup
def common_setup(self):
"""
Setup the workbench directory and pull down the original file, add
the audio_config, transcoder, thumbnailer and spectrogram_tmp path
"""
self.audio_config = mgg.global_config["plugins"]["mediagoblin.media_types.audio"]
# Pull down and set up the processing file
self.process_filename = get_process_filename(self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self.transcoder = AudioTranscoder()
self.thumbnailer = AudioThumbnailer()
示例7: common_setup
def common_setup(self):
"""
Set up the workbench directory and pull down the original file
"""
self.svg_config = mgg.global_config['plugins']['mediagoblin_svg']
# Conversions subdirectory to avoid collisions
self.conversions_subdir = os.path.join(
self.workbench.dir, 'conversions')
os.mkdir(self.conversions_subdir)
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
示例8: common_setup
def common_setup(self):
self.ascii_config = mgg.global_config['plugins'][
'mediagoblin.media_types.ascii']
# Conversions subdirectory to avoid collisions
self.conversions_subdir = os.path.join(
self.workbench.dir, 'conversions')
os.mkdir(self.conversions_subdir)
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self.charset = None
示例9: CommonImageProcessor
class CommonImageProcessor(MediaProcessor):
"""
Provides a base for various media processing steps
"""
# list of acceptable file keys in order of prefrence for reprocessing
acceptable_files = ['original', 'medium']
def common_setup(self):
"""
Set up the workbench directory and pull down the original file
"""
self.image_config = mgg.global_config[
'media_type:mediagoblin.media_types.image']
## @@: Should this be two functions?
# Conversions subdirectory to avoid collisions
self.conversions_subdir = os.path.join(
self.workbench.dir, 'convirsions')
os.mkdir(self.conversions_subdir)
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
# Exif extraction
self.exif_tags = extract_exif(self.process_filename)
def generate_medium_if_applicable(self, size=None, quality=None,
filter=None):
if not quality:
quality = self.image_config['quality']
if not filter:
filter = self.image_config['resize_filter']
resize_tool(self.entry, False, 'medium', self.process_filename,
self.name_builder.fill('{basename}.medium{ext}'),
self.conversions_subdir, self.exif_tags, quality,
filter, size)
def generate_thumb(self, size=None, quality=None, filter=None):
if not quality:
quality = self.image_config['quality']
if not filter:
filter = self.image_config['resize_filter']
resize_tool(self.entry, True, 'thumb', self.process_filename,
self.name_builder.fill('{basename}.thumbnail{ext}'),
self.conversions_subdir, self.exif_tags, quality,
filter, size)
def copy_original(self):
copy_original(
self.entry, self.process_filename,
self.name_builder.fill('{basename}{ext}'))
def extract_metadata(self):
# Is there any GPS data
gps_data = get_gps_data(self.exif_tags)
# Insert exif data into database
exif_all = clean_exif(self.exif_tags)
if len(exif_all):
self.entry.media_data_init(exif_all=exif_all)
if len(gps_data):
for key in list(gps_data.keys()):
gps_data['gps_' + key] = gps_data.pop(key)
self.entry.media_data_init(**gps_data)
示例10: process_audio
def process_audio(entry):
audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
workbench = mgg.workbench_manager.create_workbench()
queued_filepath = entry.queued_media_file
queued_filename = workbench.localized_file(
mgg.queue_store, queued_filepath,
'source')
name_builder = FilenameBuilder(queued_filename)
webm_audio_filepath = create_pub_filepath(
entry,
'{original}.webm'.format(
original=os.path.splitext(
queued_filepath[-1])[0]))
if audio_config['keep_original']:
with open(queued_filename, 'rb') as queued_file:
original_filepath = create_pub_filepath(
entry, name_builder.fill('{basename}{ext}'))
with mgg.public_store.get_file(original_filepath, 'wb') as \
original_file:
_log.debug('Saving original...')
original_file.write(queued_file.read())
entry.media_files['original'] = original_filepath
transcoder = AudioTranscoder()
with tempfile.NamedTemporaryFile() as webm_audio_tmp:
progress_callback = ProgressCallback(entry)
transcoder.transcode(
queued_filename,
webm_audio_tmp.name,
quality=audio_config['quality'],
progress_callback=progress_callback)
transcoder.discover(webm_audio_tmp.name)
_log.debug('Saving medium...')
mgg.public_store.get_file(webm_audio_filepath, 'wb').write(
webm_audio_tmp.read())
entry.media_files['webm_audio'] = webm_audio_filepath
# entry.media_data_init(length=int(data.audiolength))
if audio_config['create_spectrogram']:
spectrogram_filepath = create_pub_filepath(
entry,
'{original}-spectrogram.jpg'.format(
original=os.path.splitext(
queued_filepath[-1])[0]))
with tempfile.NamedTemporaryFile(suffix='.ogg') as wav_tmp:
_log.info('Creating OGG source for spectrogram')
transcoder.transcode(
queued_filename,
wav_tmp.name,
mux_string='vorbisenc quality={0} ! oggmux'.format(
audio_config['quality']))
thumbnailer = AudioThumbnailer()
with tempfile.NamedTemporaryFile(suffix='.jpg') as spectrogram_tmp:
thumbnailer.spectrogram(
wav_tmp.name,
spectrogram_tmp.name,
width=mgg.global_config['media:medium']['max_width'],
fft_size=audio_config['spectrogram_fft_size'])
_log.debug('Saving spectrogram...')
mgg.public_store.get_file(spectrogram_filepath, 'wb').write(
spectrogram_tmp.read())
entry.media_files['spectrogram'] = spectrogram_filepath
with tempfile.NamedTemporaryFile(suffix='.jpg') as thumb_tmp:
thumbnailer.thumbnail_spectrogram(
spectrogram_tmp.name,
thumb_tmp.name,
(mgg.global_config['media:thumb']['max_width'],
mgg.global_config['media:thumb']['max_height']))
thumb_filepath = create_pub_filepath(
entry,
'{original}-thumbnail.jpg'.format(
original=os.path.splitext(
queued_filepath[-1])[0]))
mgg.public_store.get_file(thumb_filepath, 'wb').write(
thumb_tmp.read())
entry.media_files['thumb'] = thumb_filepath
else:
entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg']
#.........这里部分代码省略.........
示例11: CommonSvgProcessor
class CommonSvgProcessor(MediaProcessor):
"""
Provides a base for various svg processing steps
"""
acceptable_files = ['original']
def common_setup(self):
"""
Set up the workbench directory and pull down the original file
"""
self.svg_config = mgg.global_config['plugins']['mediagoblin_svg']
# Conversions subdirectory to avoid collisions
self.conversions_subdir = os.path.join(
self.workbench.dir, 'conversions')
os.mkdir(self.conversions_subdir)
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
def generate_preview(self, size=None):
if not size:
size = (mgg.global_config['media:medium']['max_width'],
mgg.global_config['media:medium']['max_height'])
if self.svg_config['svg_previews']:
# delete existing thumbnail, if it doesn't match the original
if self.entry.media_files.has_key('preview') and \
self.entry.media_files['preview'] != self.entry.media_files['original']:
mgg.public_store.delete_file(self.entry.media_files['preview'])
self.entry.media_files['preview'] = self.entry.media_files.get('original')
else:
preview_filename = os.path.join(self.workbench.dir,
self.name_builder.fill('{basename}.preview.png'))
render_preview(self.process_filename, preview_filename, size)
store_public(self.entry, 'preview', preview_filename,
self.name_builder.fill('{basename}.preview.png'))
def generate_thumb(self, size=None):
if not size:
size = (mgg.global_config['media:thumb']['max_width'],
mgg.global_config['media:thumb']['max_height'])
if self.svg_config['svg_thumbnails']:
# delete existing thumbnail, if it doesn't match the original
if self.entry.media_files.has_key('thumb') and \
self.entry.media_files['thumb'] != self.entry.media_files['original']:
mgg.public_store.delete_file(self.entry.media_files['thumb'])
self.entry.media_files['thumb'] = self.entry.media_files.get('original')
else:
thumb_filename = os.path.join(self.workbench.dir,
self.name_builder.fill('{basename}.thumbnail.png'))
render_preview(self.process_filename, thumb_filename, size)
store_public(self.entry, 'thumb', thumb_filename)
def copy_original(self):
copy_original(
self.entry, self.process_filename,
self.name_builder.fill('{basename}{ext}'))
示例12: CommonPdfProcessor
class CommonPdfProcessor(MediaProcessor):
"""
Provides a base for various pdf processing steps
"""
acceptable_files = ['original', 'pdf']
def common_setup(self):
"""
Set up common pdf processing steps
"""
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self._set_pdf_filename()
def _set_pdf_filename(self):
if self.name_builder.ext == '.pdf':
self.pdf_filename = self.process_filename
elif self.entry.media_files.get('pdf'):
self.pdf_filename = self.workbench.localized_file(
mgg.public_store, self.entry.media_files['pdf'])
else:
self.pdf_filename = self._generate_pdf()
def _skip_processing(self, keyname, **kwargs):
file_metadata = self.entry.get_file_metadata(keyname)
skip = True
if not file_metadata:
return False
if keyname == 'thumb':
if kwargs.get('thumb_size') != file_metadata.get('thumb_size'):
skip = False
elif keyname == 'medium':
if kwargs.get('size') != file_metadata.get('size'):
skip = False
return skip
def copy_original(self):
copy_original(
self.entry, self.process_filename,
self.name_builder.fill('{basename}{ext}'))
def generate_thumb(self, thumb_size=None):
if not thumb_size:
thumb_size = (mgg.global_config['media:thumb']['max_width'],
mgg.global_config['media:thumb']['max_height'])
if self._skip_processing('thumb', thumb_size=thumb_size):
return
# Note: pdftocairo adds '.png', so don't include an ext
thumb_filename = os.path.join(self.workbench.dir,
self.name_builder.fill(
'{basename}.thumbnail'))
executable = where('pdftocairo')
args = [executable, '-scale-to', str(min(thumb_size)),
'-singlefile', '-png', self.pdf_filename, thumb_filename]
_log.debug('calling {0}'.format(repr(' '.join(args))))
Popen(executable=executable, args=args).wait()
# since pdftocairo added '.png', we need to include it with the
# filename
store_public(self.entry, 'thumb', thumb_filename + '.png',
self.name_builder.fill('{basename}.thumbnail.png'))
self.entry.set_file_metadata('thumb', thumb_size=thumb_size)
def _generate_pdf(self):
"""
Store the pdf. If the file is not a pdf, make it a pdf
"""
tmp_pdf = os.path.splitext(self.process_filename)[0] + '.pdf'
unoconv = where('unoconv')
args = [unoconv, '-v', '-f', 'pdf', self.process_filename]
_log.debug('calling %s' % repr(args))
Popen(executable=unoconv,
args=args).wait()
if not os.path.exists(tmp_pdf):
_log.debug('unoconv failed to convert file to pdf')
raise BadMediaFail()
store_public(self.entry, 'pdf', tmp_pdf,
self.name_builder.fill('{basename}.pdf'))
return self.workbench.localized_file(
mgg.public_store, self.entry.media_files['pdf'])
def extract_pdf_info(self):
pdf_info_dict = pdf_info(self.pdf_filename)
self.entry.media_data_init(**pdf_info_dict)
#.........这里部分代码省略.........
示例13: CommonAsciiProcessor
class CommonAsciiProcessor(MediaProcessor):
"""
Provides a base for various ascii processing steps
"""
acceptable_files = ['original', 'unicode']
def common_setup(self):
self.ascii_config = mgg.global_config['plugins'][
'mediagoblin.media_types.ascii']
# Conversions subdirectory to avoid collisions
self.conversions_subdir = os.path.join(
self.workbench.dir, 'conversions')
os.mkdir(self.conversions_subdir)
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self.charset = None
def copy_original(self):
copy_original(
self.entry, self.process_filename,
self.name_builder.fill('{basename}{ext}'))
def _detect_charset(self, orig_file):
d_charset = chardet.detect(orig_file.read())
# Only select a non-utf-8 charset if chardet is *really* sure
# Tested with "Feli\x0109an superjaron", which was detected
if d_charset['confidence'] < 0.9:
self.charset = 'utf-8'
else:
self.charset = d_charset['encoding']
_log.info('Charset detected: {0}\nWill interpret as: {1}'.format(
d_charset,
self.charset))
# Rewind the file
orig_file.seek(0)
def store_unicode_file(self):
with open(self.process_filename, 'rb') as orig_file:
self._detect_charset(orig_file)
unicode_filepath = create_pub_filepath(self.entry,
'ascii-portable.txt')
with mgg.public_store.get_file(unicode_filepath, 'wb') \
as unicode_file:
# Decode the original file from its detected charset (or UTF8)
# Encode the unicode instance to ASCII and replace any
# non-ASCII with an HTML entity (&#
unicode_file.write(
six.text_type(orig_file.read().decode(
self.charset)).encode(
'ascii',
'xmlcharrefreplace'))
self.entry.media_files['unicode'] = unicode_filepath
def generate_thumb(self, font=None, thumb_size=None):
with open(self.process_filename, 'rb') as orig_file:
# If no font kwarg, check config
if not font:
font = self.ascii_config.get('thumbnail_font', None)
if not thumb_size:
thumb_size = (mgg.global_config['media:thumb']['max_width'],
mgg.global_config['media:thumb']['max_height'])
if self._skip_resizing(font, thumb_size):
return
tmp_thumb = os.path.join(
self.conversions_subdir,
self.name_builder.fill('{basename}.thumbnail.png'))
ascii_converter_args = {}
# If there is a font from either the config or kwarg, update
# ascii_converter_args
if font:
ascii_converter_args.update(
{'font': self.ascii_config['thumbnail_font']})
converter = asciitoimage.AsciiToImage(
**ascii_converter_args)
thumb = converter._create_image(
orig_file.read())
with open(tmp_thumb, 'w') as thumb_file:
thumb.thumbnail(
thumb_size,
Image.ANTIALIAS)
thumb.save(thumb_file)
thumb_info = {'font': font,
#.........这里部分代码省略.........
示例14: CommonStlProcessor
class CommonStlProcessor(MediaProcessor):
"""
Provides a common base for various stl processing steps
"""
acceptable_files = ['original']
def common_setup(self):
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
self._set_ext()
self._set_model()
self._set_greatest()
def _set_ext(self):
ext = self.name_builder.ext[1:]
if not ext:
ext = None
self.ext = ext
def _set_model(self):
"""
Attempt to parse the model file and divine some useful
information about it.
"""
with open(self.process_filename, 'rb') as model_file:
self.model = model_loader.auto_detect(model_file, self.ext)
def _set_greatest(self):
greatest = [self.model.width, self.model.height, self.model.depth]
greatest.sort()
self.greatest = greatest[-1]
def copy_original(self):
copy_original(
self.entry, self.process_filename,
self.name_builder.fill('{basename}{ext}'))
def _snap(self, keyname, name, camera, size, project="ORTHO"):
filename = self.name_builder.fill(name)
workbench_path = self.workbench.joinpath(filename)
shot = {
"model_path": self.process_filename,
"model_ext": self.ext,
"camera_coord": camera,
"camera_focus": self.model.average,
"camera_clip": self.greatest*10,
"greatest": self.greatest,
"projection": project,
"width": size[0],
"height": size[1],
"out_file": workbench_path,
}
blender_render(shot)
# make sure the image rendered to the workbench path
assert os.path.exists(workbench_path)
# copy it up!
store_public(self.entry, keyname, workbench_path, filename)
def generate_thumb(self, thumb_size=None):
if not thumb_size:
thumb_size = (mgg.global_config['media:thumb']['max_width'],
mgg.global_config['media:thumb']['max_height'])
self._snap(
"thumb",
"{basename}.thumb.jpg",
[0, self.greatest*-1.5, self.greatest],
thumb_size,
project="PERSP")
def generate_perspective(self, size=None):
if not size:
size = (mgg.global_config['media:medium']['max_width'],
mgg.global_config['media:medium']['max_height'])
self._snap(
"perspective",
"{basename}.perspective.jpg",
[0, self.greatest*-1.5, self.greatest],
size,
project="PERSP")
def generate_topview(self, size=None):
if not size:
size = (mgg.global_config['media:medium']['max_width'],
mgg.global_config['media:medium']['max_height'])
self._snap(
"top",
"{basename}.top.jpg",
[self.model.average[0], self.model.average[1],
self.greatest*2],
size)
#.........这里部分代码省略.........
示例15: CommonImageProcessor
class CommonImageProcessor(MediaProcessor):
"""
Provides a base for various media processing steps
"""
# list of acceptable file keys in order of prefrence for reprocessing
acceptable_files = ['original', 'medium']
def common_setup(self):
"""
Set up the workbench directory and pull down the original file
"""
self.image_config = mgg.global_config['plugins'][
'mediagoblin.media_types.image']
## @@: Should this be two functions?
# Conversions subdirectory to avoid collisions
self.conversions_subdir = os.path.join(
self.workbench.dir, 'conversions')
os.mkdir(self.conversions_subdir)
# Pull down and set up the processing file
self.process_filename = get_process_filename(
self.entry, self.workbench, self.acceptable_files)
self.name_builder = FilenameBuilder(self.process_filename)
# Exif extraction
self.exif_tags = extract_exif(self.process_filename)
def generate_medium_if_applicable(self, size=None, quality=None,
filter=None):
if not quality:
quality = self.image_config['quality']
if not filter:
filter = self.image_config['resize_filter']
resize_tool(self.entry, False, 'medium', self.process_filename,
self.name_builder.fill('{basename}.medium{ext}'),
self.conversions_subdir, self.exif_tags, quality,
filter, size)
def generate_thumb(self, size=None, quality=None, filter=None):
if not quality:
quality = self.image_config['quality']
if not filter:
filter = self.image_config['resize_filter']
resize_tool(self.entry, True, 'thumb', self.process_filename,
self.name_builder.fill('{basename}.thumbnail{ext}'),
self.conversions_subdir, self.exif_tags, quality,
filter, size)
def copy_original(self):
copy_original(
self.entry, self.process_filename,
self.name_builder.fill('{basename}{ext}'))
def extract_metadata(self, file):
""" Extract all the metadata from the image and store """
# Extract GPS data and store in Location
gps_data = get_gps_data(self.exif_tags)
if len(gps_data):
Location.create({"position": gps_data}, self.entry)
# Insert exif data into database
exif_all = clean_exif(self.exif_tags)
if len(exif_all):
self.entry.media_data_init(exif_all=exif_all)
# Extract file metadata
try:
im = Image.open(self.process_filename)
except IOError:
raise BadMediaFail()
metadata = {
"width": im.size[0],
"height": im.size[1],
}
self.entry.set_file_metadata(file, **metadata)