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