-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
65 lines (51 loc) · 2.02 KB
/
config.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
# config.py
import os
import sys
def is_path_writable(path):
if os.path.isdir(path) and os.access(path, os.W_OK):
sys.stdout.write("Output will be directed to: {} \n".format(path))
sys.stdout.flush()
else:
sys.exit("The directory: {}, either does not exist or lacks write permissions".format(path))
return False
def default_dir_fallback():
system.stdout.write("Unable to make required folder in home directory, falling back to root")
file_dest = DEFAULT_DIR
if DEFAULT_DIR == "~" or DEFAULT_DIR is None:
sys.exit("Unable to use root or default directory, exiting...")
if not os.path.isdir(file_dest+"/output"):
try:
os.mkdir(file_dest)
except OSError:
sys.exit("Unable to create output folder in root directory, exiting...")
# path variables
DEFAULT_DIR = os.path.normpath(os.path.expanduser('~'))
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_LOCATION = os.path.normpath(os.path.join(ROOT_DIR, "data/cedict_ts.u8"))
# Assign CLI arguments to values here.
# It should go something like this:
# python main.py www.sampleurl.com [optional destination]
# If sys.argv[x] doesn't exist it should throw an IndexError
c_url = file_dest = None
try:
c_url = sys.argv[1]
file_dest = sys.argv[2]
except:
if (c_url is None):
sys.exit("Please provide a uri for the second argument \n")
elif (file_dest is None):
sys.stdout.write("Using default directory for resultant file location. \n")
# Check to see if the path is writable, exiting via sys.exit() if unable.
if not file_dest is None and os.path.isabs(file_dest):
is_path_writable(file_dest)
elif file_dest is None:
file_dest = ROOT_DIR+"/output"
if not os.path.isdir(file_dest):
# if os.mkdir fails, it should raise an OSError
try:
os.mkdir(file_dest)
except OSError:
default_dir_fallback()
elif not os.path.isabs(file_dest):
file_dest = os.path.join(ROOT_DIR, file_dest)
is_path_writable(file_dest)