Page MenuHomec4science

copy_svg_files.py
No OneTemporary

File Metadata

Created
Fri, Sep 27, 01:04

copy_svg_files.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This program can be used to copy a svg file(s) to a target directory, and also updating its image-path.
"""
# Copyright (C) University of Basel 2019 {{{1
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <https://www.gnu.org/licenses/> 1}}}
import getopt
from progress.bar import Bar
import os
from os import listdir, sep, path, setpgrp, devnull, makedirs
from os.path import basename, commonpath, dirname, exists, isfile, isdir, join, realpath, splitext
import sys
if dirname(__file__) not in sys.path:
sys.path.append(dirname(__file__))
from util import copy_faksimile_update_image_location
from create_task import Task
__author__ = "Christian Steiner"
__maintainer__ = __author__
__copyright__ = 'University of Basel'
__email__ = "christian.steiner@unibas.ch"
__status__ = "Development"
__license__ = "GPL v3"
__version__ = "0.0.1"
UNITTESTING = False
def usage():
"""prints information on how to use the script
"""
if __package__ is not None:
print(main.__doc__.format(package=__package__, file=basename(__file__)))
else:
print(main.__doc__.format(package=dirname(__file__), file=basename(__file__)))
def main(argv):
"""This program can be used to copy a svg file(s) to a target directory, and also updating its image-path.
{package}/{file} [OPTIONS] <faksimile_dir|faksimile_svg_file> <target_dir>
<faksimile_dir> a directory containing faksimile_svg_files.
<faksimile_svg_file> a svg file containing information about the word positions on the faksimile.
<target_dir> the target directory.
OPTIONS:
-h|--help: show help
:return: exit code (int)
"""
try:
opts, args = getopt.getopt(argv, "h", ["help" ])
except getopt.GetoptError:
usage()
return 2
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
return 0
if len(args) < 2:
usage()
return 2
exit_status = 0
messages = []
if exists(args[0]) and exists(args[1]):
svg_file_list = []
if (isdir(args[0]) and not isdir(args[1])) or (isdir(args[1]) and not isdir(args[0])):
svg_file = args[0] if isfile(args[0]) else args[1]
target_dir = args[1] if isdir(args[1]) else args[0]
svg_file_list.append(svg_file)
elif isdir(args[0]) and isdir(args[1]):
source_dir = args[0]
target_dir = args[1]
svg_file_list = [ join(source_dir, svg_file) for svg_file in listdir(source_dir) if svg_file.endswith('.svg') ]
if not UNITTESTING:
bar = Bar('copy files to directory {}'.format(target_dir), max=len(svg_file_list))
for svg_file in svg_file_list:
not bool(UNITTESTING) and bar.next()
target_file = join(target_dir, basename(svg_file))
finished_target_file = join(join(target_dir, Task.finish_dir), basename(svg_file))
if not isfile(target_file) and not isfile(finished_target_file):
copy_faksimile_update_image_location(faksimile_source_file=svg_file, target_file=target_file)
else:
existing_file = target_file if isfile(target_file) else finished_target_file
messages.append('File {0} skipped: Directory {1} already contains a file {2}.'.format(svg_file, target_dir, existing_file))
if not bool(UNITTESTING):
bar.finish()
for msg in messages:
print(msg)
else:
file_a = args[0] if not exists(args[0]) else args[1]
raise FileNotFoundError('File {} does not exist!'.format(file_a))
return exit_status
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))

Event Timeline