1 #!/usr/bin/env python 2 3 """ 4 Copying utilities. 5 6 Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from shutil import copy 23 24 def copy_attachments(parser, input, output, all=False): 25 26 """ 27 For attachments referenced by the page processed by 'parser', copy them from 28 'input' to 'output'. If 'all' is set to a true value, all stored attachments 29 are copied; otherwise, only those explicitly linked in the page are copied. 30 """ 31 32 pagename = parser.metadata.get("pagename") 33 34 if not pagename: 35 return 36 37 # Obtain attachments. 38 39 if not all: 40 filenames = [] 41 for link_target in parser.link_targets: 42 if link_target.get_type() == "attachment": 43 filenames.append(link_target.get_identifier()) 44 else: 45 input = parser.metadata.get_input() 46 filenames = input.get_attachments(pagename) 47 48 # Copy attachments. 49 50 for filename in filenames: 51 52 # Obtain the source location and the destination. 53 54 input_filename = input.get_attachment_filename(pagename, filename) 55 output_filename = output.get_attachment_filename(pagename, filename) 56 57 # Copy the file if possible. 58 59 if input_filename and output_filename: 60 output.ensure_attachments(pagename) 61 copy(input_filename, output_filename) 62 63 # vim: tabstop=4 expandtab shiftwidth=4