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@223 | 24 | def copy_attachments(parser, input, output): |
paul@223 | 25 | |
paul@223 | 26 | "Copy attachments referenced by 'parser' from 'input' to 'output'." |
paul@223 | 27 | |
paul@223 | 28 | pagename = parser.metadata.get("pagename") |
paul@223 | 29 | |
paul@223 | 30 | if not pagename: |
paul@223 | 31 | return |
paul@223 | 32 | |
paul@223 | 33 | for link_target in parser.link_targets: |
paul@223 | 34 | |
paul@223 | 35 | # Obtain attachments. |
paul@223 | 36 | |
paul@223 | 37 | if link_target.get_type() == "attachment": |
paul@223 | 38 | |
paul@223 | 39 | # Obtain the attachment filename, the source location and the |
paul@223 | 40 | # destination. |
paul@223 | 41 | |
paul@223 | 42 | filename = link_target.get_identifier() |
paul@223 | 43 | input_filename = input.get_attachment_filename(pagename, filename) |
paul@223 | 44 | output_filename = output.get_attachment_filename(pagename, filename) |
paul@223 | 45 | |
paul@223 | 46 | # Copy the file if possible. |
paul@223 | 47 | |
paul@223 | 48 | if input_filename and output_filename: |
paul@223 | 49 | output.ensure_attachments(pagename) |
paul@223 | 50 | copy(input_filename, output_filename) |
paul@223 | 51 | |
paul@223 | 52 | # vim: tabstop=4 expandtab shiftwidth=4 |