-
Notifications
You must be signed in to change notification settings - Fork 1
/
DocumentMover.py
78 lines (59 loc) · 2.28 KB
/
DocumentMover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from src.ConfigValidator import ConfigValidator
from src.ConfigReader import ConfigReader
from src.Worker import Worker
import argparse
import os
from os.path import join
import sys
from configparser import ConfigParser, DuplicateSectionError
VERSION='v0.2.0'
def main() -> int:
parser = argparse.ArgumentParser(description='DocumentMover is a command line tool that automatically moves files in directories. Rules determine where the files get moved.')
parser.add_argument('-val', dest='validate', help='only validate the config file without executing it', action='store_true')
parser.add_argument('--version', dest='version', help='show the version of the program', action='store_true')
parser.add_argument('-p', dest='path', type=str, help='path to the folder with config file', default='.')
args = parser.parse_args()
if args.version:
showVersion()
return 0
if not os.path.isdir(args.path):
print('Error: "' + args.path + '" is not a valid path')
return -1
configParser = ConfigParser()
if not os.path.isfile(join(args.path, '.documentMover')):
print('Error: No configuration file ".documentMover" found in the given path')
return -1
try:
configParser.read(join(args.path, '.documentMover'))
except DuplicateSectionError as e:
print('Error:', e.message)
return -1
if not validate(configParser, args.validate):
return -1
if args.validate:
return 0
configReader = ConfigReader()
config = configReader.read(configParser)
worker = Worker(config)
worker.run()
return 0
def validate(configParser: ConfigParser, validateOnly: bool) -> bool:
configValidator = ConfigValidator(validateOnly)
isValid = configValidator.validate(configParser)
if validateOnly and isValid and len(configValidator.getErrors()) > 0:
for error in configValidator.getErrors():
print(error)
if isValid:
if validateOnly:
print('The config is valid!')
return True
print('The config is invalid!')
for error in configValidator.getErrors():
print(error)
return False
def showVersion() -> None:
print('DocumentMover version', VERSION)
if __name__ == '__main__':
sys.exit(main())