-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
base.py
67 lines (50 loc) · 1.74 KB
/
base.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
import hashlib
from dataclasses import dataclass
from typing import List, Optional
from dbt.artifacts.resources.types import NodeType
from dbt_common.dataclass_schema import dbtClassMixin
@dataclass
class BaseResource(dbtClassMixin):
name: str
resource_type: NodeType
package_name: str
path: str
original_file_path: str
unique_id: str
@dataclass
class GraphResource(BaseResource):
fqn: List[str]
@dataclass
class FileHash(dbtClassMixin):
name: str # the hash type name
checksum: str # the hashlib.hash_type().hexdigest() of the file contents
@classmethod
def empty(cls):
return FileHash(name="none", checksum="")
@classmethod
def path(cls, path: str):
return FileHash(name="path", checksum=path)
def __eq__(self, other):
if not isinstance(other, FileHash):
return NotImplemented
if self.name == "none" or self.name != other.name:
return False
return self.checksum == other.checksum
def compare(self, contents: str) -> bool:
"""Compare the file contents with the given hash"""
if self.name == "none":
return False
return self.from_contents(contents, name=self.name) == self.checksum
@classmethod
def from_contents(cls, contents: str, name="sha256") -> "FileHash":
"""Create a file hash from the given file contents. The hash is always
the utf-8 encoding of the contents given, because dbt only reads files
as utf-8.
"""
data = contents.encode("utf-8")
checksum = hashlib.new(name, data).hexdigest()
return cls(name=name, checksum=checksum)
@dataclass
class Docs(dbtClassMixin):
show: bool = True
node_color: Optional[str] = None