147 lines
4.6 KiB
Python
Executable file
147 lines
4.6 KiB
Python
Executable file
#! /usr/bin/python3
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
"""Creates a directory with all documents for a business trip."""
|
|
|
|
import os
|
|
import argparse
|
|
import shutil
|
|
import sys
|
|
import shutil
|
|
from constants import CONFIG_FORMDIR, CONFIGFILE, CONFIG_SEPARATOR
|
|
from install import SCRIPTS_TO_INSTALL
|
|
|
|
def parse():
|
|
"""Create the argument parser (including the help) and parse the arguments.
|
|
|
|
Options/ Arguments:
|
|
directory
|
|
|
|
"""
|
|
parser = argparse.ArgumentParser(
|
|
description="Create a directory for a new trip.",
|
|
# formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog=("To start go to the new directory and run " +
|
|
"'fillform Antrag.pdf'"))
|
|
parser.add_argument("directory",
|
|
help=("The new directory to be created." +
|
|
"Relative or absolute path. The " +
|
|
"parent directory should exist."))
|
|
return vars(parser.parse_args())
|
|
|
|
|
|
def readFormFileDirectoriesFromConfig():
|
|
"""Read the config file and find form file dirs.
|
|
|
|
Returns:
|
|
the last config dir in the config file.
|
|
None if none is specified or the config file does not exist.
|
|
|
|
"""
|
|
formdir = None
|
|
try:
|
|
with open(CONFIGFILE, "r") as configfile:
|
|
for line in [l.strip("\n") for l in configfile.readlines() if
|
|
l.startswith(CONFIG_FORMDIR + CONFIG_SEPARATOR)]:
|
|
try:
|
|
formdir = line.split(CONFIG_SEPARATOR,
|
|
maxsplit=1 # max 2 parts
|
|
)[1]
|
|
except IndexError:
|
|
print("Debug: impossible index error " +
|
|
"in config file reading.")
|
|
return formdir
|
|
except FileNotFoundError:
|
|
print("No config file found.")
|
|
return None
|
|
|
|
|
|
def filesToCopy(where):
|
|
"""Find all files that are needed for one trip.
|
|
|
|
where: the directory where all the files are (in subdirs).
|
|
|
|
"""
|
|
copyfiles = []
|
|
for dirpath, _, files in os.walk(where):
|
|
for f in files:
|
|
if f.endswith(".form"):
|
|
copyfiles.append(os.path.join(dirpath, f))
|
|
possiblepdfFile = os.path.join(
|
|
dirpath, os.path.splitext(f)[0] + ".pdf")
|
|
if os.path.exists(possiblepdfFile):
|
|
copyfiles.append(possiblepdfFile)
|
|
return copyfiles
|
|
|
|
|
|
def checkIfInstalled():
|
|
"""Check if the software suite fillpdf is installed.
|
|
|
|
Remind the user to install if not happenend yet.
|
|
|
|
"""
|
|
# check if program is installed
|
|
if not all([shutil.which(script) for script in SCRIPTS_TO_INSTALL]):
|
|
# if which returns something that is not none or "" it is
|
|
# considered True
|
|
print("Not all scripts that are part of this software suite" +
|
|
" are installed. Consider executing the install.py script "+
|
|
"in the downloaded repository.")
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
|
|
def copyFile(copyfile, where):
|
|
"""Try to copy the file copyfile to the directory where."""
|
|
try:
|
|
shutil.copy(copy, directory)
|
|
except FileNotFoundError as fnfe:
|
|
print("The file", copy, "is not there anymore.")
|
|
raise fnfe
|
|
except PermissionError as pe:
|
|
print("I am not allowed to copy the file",
|
|
copy + ". Use another place.")
|
|
raise pe
|
|
else:
|
|
pass
|
|
# print("Debug: copied the file " + copy)
|
|
|
|
|
|
def mkdir(where):
|
|
"""Try to create the directory directory.
|
|
|
|
Raises:
|
|
PermissionsError: if not allowed.
|
|
|
|
"""
|
|
# print("Debug: directory:", where)
|
|
try:
|
|
os.makedirs(where)
|
|
except FileExistsError:
|
|
print("Warning: the files in", where, "that have",
|
|
"the same names as the copied files are overwritten.")
|
|
except PermissionError as pe:
|
|
print("I am not allowed to create this directory. Use another place.")
|
|
raise pe
|
|
|
|
|
|
if __name__ == "__main__":
|
|
arguments = parse()
|
|
directory = arguments["directory"]
|
|
formdirectory = readFormFileDirectoriesFromConfig()
|
|
if formdirectory is None:
|
|
print("I could not find the information where the form files" +
|
|
" are. Maybe you should execute the install script or " +
|
|
"add a line \n" +
|
|
CONFIG_FORMDIR + CONFIG_SEPARATOR +
|
|
"<directory with form files>\n" +
|
|
"to the file " + CONFIGFILE + ".")
|
|
else:
|
|
tocopy = filesToCopy(formdirectory)
|
|
# print("Debug:", tocopy)
|
|
mkdir(directory)
|
|
for copy in tocopy:
|
|
copyFile(copy, directory)
|
|
|
|
checkIfInstalled()
|