# -*- coding: utf-8 -*-
#
# This file is part of Zenodo.
# Copyright (C) 2017 CERN.
#
# Zenodo is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Zenodo is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Zenodo; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Utilities for SIPStore archivers."""
from __future__ import absolute_import, print_function
from werkzeug.utils import secure_filename
[docs]def chunks(iterable, n):
"""Yield iterable split into chunks.
If 'n' is an integer, yield the iterable as n-sized chunks.
If 'n' is a list of integers, yield chunks of sizes: n[0],
n[1], ..., len(iterable) - sum(n)
>>> from invenio_sipstore.archivers.utils import chunks
>>> list(chunks('abcdefg', 3))
['abc', 'def', 'g']
>>> list(chunks('abcdefg', [1, ]))
['a', 'bcdefg']
>>> list(chunks('abcdefg', [1, 2, 3]))
['a', 'bc', 'def', 'g']
"""
if isinstance(n, int):
for i in range(0, len(iterable), n):
yield iterable[i:i + n]
elif isinstance(n, list):
acc = 0
if sum(n) < len(iterable):
n.append(len(iterable))
for i in n:
if acc < len(iterable):
yield iterable[acc: acc + i]
acc += i
[docs]def default_archive_directory_builder(sip):
"""Build a directory structure for the archived SIP.
Creates a structure that is based on the SIP's UUID.
'abcdefgh-1234-1234-1234-1234567890ab' ->
['ab', 'cd', 'efgh-1234-1234-1234-1234567890ab']
:param sip: SIP which is to be archived
:type SIP: invenio_sipstore.models.SIP
:returns: list of str
"""
return list(chunks(str(sip.id), [2, 2, ]))