1# -*- coding: utf-8 -*-
2from __future__ import absolute_import, print_function
3
4import errno
5import os
6import sys
7
8import six
9from vistir.compat import FileNotFoundError
10
11if six.PY2:
12
13    class FileExistsError(OSError):
14        def __init__(self, *args, **kwargs):
15            self.errno = errno.EEXIST
16            super(FileExistsError, self).__init__(*args, **kwargs)
17
18
19else:
20    from six.moves.builtins import FileExistsError
21
22
23class RequirementError(Exception):
24    pass
25
26
27class MissingParameter(Exception):
28    def __init__(self, param):
29        self.message = self.get_message(param)
30        super(MissingParameter, self).__init__(self.message)
31
32    @classmethod
33    def get_message(cls, param):
34        return "Missing Parameter: %s" % param
35
36    def show(self, param):
37        print(self.message, file=sys.stderr, flush=True)
38
39
40class FileCorruptException(OSError):
41    def __init__(self, path, *args, **kwargs):
42        path = path
43        backup_path = kwargs.pop("backup_path", None)
44        if not backup_path and args:
45            args = reversed(args)
46            backup_path = args.pop()
47            if not isinstance(backup_path, six.string_types) or not os.path.exists(
48                os.path.abspath(os.path.dirname(backup_path))
49            ):
50                args.append(backup_path)
51                backup_path = None
52            if args:
53                args = reversed(args)
54        self.message = self.get_message(path, backup_path=backup_path)
55        super(FileCorruptException, self).__init__(self.message)
56
57    def get_message(self, path, backup_path=None):
58        message = "ERROR: Failed to load file at %s" % path
59        if backup_path:
60            msg = "it will be backed up to %s and removed" % backup_path
61        else:
62            msg = "it will be removed and replaced on the next lock."
63        message = "{0}\nYour lockfile is corrupt, {1}".format(message, msg)
64        return message
65
66    def show(self):
67        print(self.message, file=sys.stderr, flush=True)
68
69
70class LockfileCorruptException(FileCorruptException):
71    def __init__(self, path, backup_path=None):
72        self.message = self.get_message(path, backup_path=backup_path)
73        super(LockfileCorruptException, self).__init__(self.message)
74
75    def get_message(self, path, backup_path=None):
76        message = "ERROR: Failed to load lockfile at %s" % path
77        if backup_path:
78            msg = "it will be backed up to %s and removed" % backup_path
79        else:
80            msg = "it will be removed and replaced on the next lock."
81        message = "{0}\nYour lockfile is corrupt, {1}".format(message, msg)
82        return message
83
84    def show(self, path, backup_path=None):
85        print(self.message, file=sys.stderr, flush=True)
86
87
88class PipfileCorruptException(FileCorruptException):
89    def __init__(self, path, backup_path=None):
90        self.message = self.get_message(path, backup_path=backup_path)
91        super(PipfileCorruptException, self).__init__(self.message)
92
93    def get_message(self, path, backup_path=None):
94        message = "ERROR: Failed to load Pipfile at %s" % path
95        if backup_path:
96            msg = "it will be backed up to %s and removed" % backup_path
97        else:
98            msg = "it will be removed and replaced on the next lock."
99        message = "{0}\nYour Pipfile is corrupt, {1}".format(message, msg)
100        return message
101
102    def show(self, path, backup_path=None):
103        print(self.message, file=sys.stderr, flush=True)
104
105
106class PipfileNotFound(FileNotFoundError):
107    def __init__(self, path, *args, **kwargs):
108        self.errno = errno.ENOENT
109        self.filename = path
110        super(PipfileNotFound, self).__init__(self.filename)
111