Skip to content
Permalink
Browse files

core: media: use the same meta data keys like the content model

This patch also fixes possible recursions when using repr

Signed-off-by: Florian Scherf <f.scherf@pengutronix.de>
  • Loading branch information
fscherf committed Aug 20, 2019
1 parent 0e727c3 commit 1b8225962942bf7133f5877ec420665af15375a4
@@ -141,12 +141,23 @@ def __init__(self, **data):
self.data = data

def __repr__(self):
repr_string = []

for k, v in self.data.items():
if k in ('content_body', ):
continue

if isinstance(v, Content):
repr_string.append('{}=Content(...)'.format(k))

elif isinstance(v, ContentSet):
repr_string.append('{}=ContentSet(...)'.format(k))

else:
repr_string.append('{}={}'.format(k, repr(v)))

return '<Content({})>'.format(
shorten(', '.join(
['{}={}'.format(k, repr(v)) for k, v in self.data.items()
if k != 'content_body']
), width=CONTENT_REPR_MAX_LEN)
)
shorten(', '.join(repr_string), width=CONTENT_REPR_MAX_LEN))

def __getitem__(self, key):
if key in self.data:
@@ -4,45 +4,34 @@


def add_media(context, content, name):
# gen source
# path
if name.startswith('/'):
source = os.path.join(context.settings.CONTENT_ROOT, name[1:])
path = name[1:]

else:
source = os.path.join(
os.path.dirname(
os.path.join(context.settings.CONTENT_ROOT, content['path'])
),
name,
)
path = os.path.join(os.path.dirname(content['path']), name)

source = os.path.normpath(source)
path = os.path.normpath(path)

# gen destination
# output
if name.startswith('/'):
destination = os.path.join(
context.settings.MEDIA_ROOT,
name[1:],
)
output = os.path.join(context.settings.MEDIA_ROOT, name[1:])

else:
destination = os.path.join(
output = os.path.join(
context.settings.MEDIA_ROOT,
os.path.dirname(content['path']),
os.path.basename(name),
)

# gen link
link = os.path.join(
'/media',
os.path.relpath(destination, context.settings.MEDIA_ROOT),
)
# url
url = '/' + output

# content['media']
if not content['media']:
content['media'] = ContentSet()

media_content = Content(source=source, destination=destination, link=link)
media_content = Content(path=path, output=output, url=url)

content['media'].add(media_content)

@@ -54,13 +43,21 @@ def add_media(context, content, name):

class Media:
def post_build(self, context):
if context.settings.CONTENT_PATHS:
if context.settings.SKIP_FILE_OPERATIONS:
return

for content in context.contents:
if not content['media']:
continue

for media in content['media']:
context.cp(source=media['source'],
destination=media['destination'])
context.cp(
source=os.path.join(
context.settings.CONTENT_ROOT,
media['path'],
),
destination=os.path.join(
context.settings.OUTPUT_ROOT,
media['output'],
),
)
@@ -59,7 +59,7 @@

# output
OUTPUT_ROOT = 'output'
MEDIA_ROOT = 'output/media'
MEDIA_ROOT = 'media'
STATIC_ROOT = 'output/static'

# layers
@@ -41,9 +41,6 @@ def __init__(self, path):
self.settings.OUTPUT_ROOT = os.path.join(
path, self.settings.OUTPUT_ROOT)

self.settings.MEDIA_ROOT = os.path.join(
path, self.settings.MEDIA_ROOT)

self.settings.STATIC_ROOT = os.path.join(
path, self.settings.STATIC_ROOT)

@@ -88,22 +88,23 @@ def _resolve_paths(paths):
return path

# media files
media_link = '/' + request_path
media_url = '/' + request_path

contents = self.context.contents.filter(
media__passes=lambda m: m and m.filter(link=media_link).exists())
media__passes=lambda m: m and m.filter(url=media_url).exists())

if contents.exists():
media_contents = contents.last()['media']
media_content = media_contents.filter(link=media_link).last()
media_content = media_contents.filter(url=media_url).last()

self.context.plugins.run_plugin_hook('render_media_content',
media_content)

self.logger.debug("handled as media file: '%s'",
media_content['source'])
media_content['path'])

return media_content['source']
return os.path.join(self.context.settings.CONTENT_ROOT,
media_content['path'])

# content
contents = self.context.contents.filter(output=request_path)
@@ -6,17 +6,17 @@ def test_add_media(flamingo_dummy_context, flamingo_env):
content = Content(path='foo/bar/index.rst')
media_content = add_media(flamingo_dummy_context, content, 'image.jpg')

assert media_content['source'] == 'content/foo/bar/image.jpg'
assert media_content['destination'] == 'output/media/foo/bar/image.jpg'
assert media_content['link'] == '/media/foo/bar/image.jpg'
assert media_content['path'] == 'foo/bar/image.jpg'
assert media_content['output'] == 'media/foo/bar/image.jpg'
assert media_content['url'] == '/media/foo/bar/image.jpg'

# absolute path
content = Content(path='foo/bar/index.rst')
media_content = add_media(flamingo_dummy_context, content, '/image.jpg')

assert media_content['source'] == 'content/image.jpg'
assert media_content['destination'] == 'output/media/image.jpg'
assert media_content['link'] == '/media/image.jpg'
assert media_content['path'] == 'image.jpg'
assert media_content['output'] == 'media/image.jpg'
assert media_content['url'] == '/media/image.jpg'

# real build tests
flamingo_env.settings.PLUGINS.append('flamingo.core.plugins.Media')
@@ -8,7 +8,6 @@ def test_already_existing_directories(flamingo_env):

os.mkdir(flamingo_env.settings.CONTENT_ROOT)
os.mkdir(flamingo_env.settings.OUTPUT_ROOT)
os.mkdir(flamingo_env.settings.MEDIA_ROOT)
os.mkdir(flamingo_env.settings.STATIC_ROOT)

flamingo_env.build()

0 comments on commit 1b82259

Please sign in to comment.
You can’t perform that action at this time.