documente_shared 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of documente_shared might be problematic. Click here for more details.

File without changes
@@ -0,0 +1,54 @@
1
+ from enum import Enum
2
+ from typing import Union, Optional
3
+
4
+
5
+ class BaseEnum(Enum):
6
+ """Provides the common functionalties to multiple model choices."""
7
+
8
+ @classmethod
9
+ def get_members(cls):
10
+ return [tag for tag in cls if type(tag.value) in [int, str, float]]
11
+
12
+ @classmethod
13
+ def choices(cls):
14
+ """Generate choice options for models."""
15
+ return [
16
+ (option.value, option.value)
17
+ for option in cls
18
+ if type(option.value) in [int, str, float]
19
+ ]
20
+
21
+ @classmethod
22
+ def values(cls):
23
+ """Returns values from choices."""
24
+ return [option.value for option in cls]
25
+
26
+ def __str__(self): # noqa: D105
27
+ return str(self.value)
28
+
29
+ def __repr__(self):
30
+ return self.__str__()
31
+
32
+ def __hash__(self):
33
+ return hash(self.value)
34
+
35
+ @classmethod
36
+ def as_list(cls):
37
+ """Returns properties as a list."""
38
+ return [
39
+ value
40
+ for key, value in cls.__dict__.items()
41
+ if isinstance(value, str) and not key.startswith('__')
42
+ ]
43
+
44
+ @classmethod
45
+ def from_value(
46
+ cls,
47
+ value: Union[str, int],
48
+ ) -> Optional['BaseEnum']:
49
+ for tag in cls:
50
+ if isinstance(tag.value, str) and str(tag.value).upper() == str(value).upper():
51
+ return tag
52
+ elif not isinstance(tag.value, str) and tag.value == value:
53
+ return tag
54
+ return None
@@ -0,0 +1,114 @@
1
+ from dataclasses import dataclass
2
+ from datetime import datetime
3
+ from decimal import Decimal
4
+ from typing import Optional
5
+
6
+ from documente_shared.domain.enums import DocumentProcessStatus
7
+
8
+
9
+ @dataclass
10
+ class DocumentProcess(object):
11
+ digest: str
12
+ status: DocumentProcessStatus
13
+ file_path: str
14
+ processed_csv_path: str
15
+ processed_xlsx_path: str
16
+ processing_time: Optional[Decimal] = None
17
+ enqueued_at: Optional[datetime] = None
18
+ started_at: Optional[datetime] = None
19
+ failed_at: Optional[datetime] = None
20
+ processed_at: Optional[datetime] = None
21
+
22
+ @property
23
+ def is_pending(self) -> bool:
24
+ return self.status == DocumentProcessStatus.PENDING
25
+
26
+ @property
27
+ def is_enqueued(self) -> bool:
28
+ return self.status == DocumentProcessStatus.ENQUEUED
29
+
30
+ @property
31
+ def is_processing(self) -> bool:
32
+ return self.status == DocumentProcessStatus.PROCESSING
33
+
34
+ @property
35
+ def is_completed(self) -> bool:
36
+ return self.status == DocumentProcessStatus.COMPLETED
37
+
38
+ @property
39
+ def is_failed(self) -> bool:
40
+ return self.status == DocumentProcessStatus.FAILED
41
+
42
+ @property
43
+ def is_valid(self) -> bool:
44
+ return all([
45
+ self.digest,
46
+ self.status,
47
+ self.file_path,
48
+ ])
49
+
50
+ def enqueue(self):
51
+ self.status = DocumentProcessStatus.ENQUEUED
52
+ self.enqueued_at = datetime.now()
53
+
54
+ def processing(self):
55
+ self.status = DocumentProcessStatus.PROCESSING
56
+ self.started_at = datetime.now()
57
+
58
+ def failed(self):
59
+ self.status = DocumentProcessStatus.FAILED
60
+ self.failed_at = datetime.now()
61
+
62
+ def completed(self):
63
+ self.status = DocumentProcessStatus.COMPLETED
64
+ self.processed_at = datetime.now()
65
+
66
+
67
+
68
+ @property
69
+ def to_dict(self) -> dict:
70
+ return {
71
+ 'digest': self.digest,
72
+ 'status': self.status.value,
73
+ 'file_path': self.file_path,
74
+ 'processed_csv_path': self.processed_csv_path,
75
+ 'processed_xlsx_path': self.processed_xlsx_path,
76
+ 'processing_time': (
77
+ str(self.processing_time)
78
+ if self.processing_time else None
79
+ ),
80
+ 'enqueued_at': self.enqueued_at.isoformat() if self.enqueued_at else None,
81
+ 'started_at': self.started_at.isoformat() if self.started_at else None,
82
+ 'failed_at': self.failed_at.isoformat() if self.failed_at else None,
83
+ 'processed_at': self.processed_at.isoformat() if self.processed_at else None,
84
+ }
85
+
86
+ @classmethod
87
+ def from_dict(cls, data: dict) -> 'DocumentProcess':
88
+ return cls(
89
+ digest=data.get('digest'),
90
+ status=DocumentProcessStatus.from_value(data.get('status')),
91
+ file_path=data.get('file_path'),
92
+ processed_csv_path=data.get('processed_csv_path'),
93
+ processed_xlsx_path=data.get('processed_xlsx_path'),
94
+ processing_time=(
95
+ Decimal(data.get('processing_time'))
96
+ if data.get('processing_time') else None
97
+ ),
98
+ enqueued_at=(
99
+ datetime.fromisoformat(data.get('enqueued_at'))
100
+ if data.get('enqueued_at') else None
101
+ ),
102
+ started_at=(
103
+ datetime.fromisoformat(data.get('started_at'))
104
+ if data.get('started_at') else None
105
+ ),
106
+ failed_at=(
107
+ datetime.fromisoformat(data.get('failed_at'))
108
+ if data.get('failed_at') else None
109
+ ),
110
+ processed_at=(
111
+ datetime.fromisoformat(data.get('processed_at'))
112
+ if data.get('processed_at') else None
113
+ ),
114
+ )
@@ -1,4 +1,4 @@
1
- from domain.base_enum import BaseEnum
1
+ from documente_shared.domain.base_enum import BaseEnum
2
2
 
3
3
 
4
4
  class DocumentProcessStatus(BaseEnum):
@@ -0,0 +1,14 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from documente_shared.domain.entities import DocumentProcess
4
+
5
+
6
+ class DocumentProcessRepository(ABC):
7
+
8
+ @abstractmethod
9
+ def persist(self, instance: DocumentProcess) -> DocumentProcess:
10
+ raise NotImplementedError
11
+
12
+ @abstractmethod
13
+ def remove(self, instance: DocumentProcess):
14
+ raise NotImplementedError
File without changes
@@ -0,0 +1,17 @@
1
+ from documente_shared.domain.entities import DocumentProcess
2
+ from documente_shared.domain.repositories import DocumentProcessRepository
3
+ from documente_shared.infrastructure.dynamo_table import DynamoDBTable
4
+
5
+
6
+ class DynamoDocumentProcessRepository(
7
+ DynamoDBTable,
8
+ DocumentProcessRepository,
9
+ ):
10
+
11
+ def persist(self, instance: DocumentProcess) -> DocumentProcess:
12
+ self.put(instance.to_dict)
13
+ return instance
14
+
15
+
16
+ def remove(self, instance: DocumentProcess):
17
+ self.delete(key=instance.digest)
@@ -0,0 +1,75 @@
1
+ from dataclasses import dataclass
2
+
3
+ import boto3
4
+ from boto3.dynamodb.conditions import Key
5
+
6
+
7
+ RETURN_VALUES = 'UPDATED_NEW'
8
+
9
+ @dataclass
10
+ class DynamoDBTable(object):
11
+ table_name: str
12
+
13
+ def __post_init__(self):
14
+ self._table = boto3.resource('dynamodb').Table(self.table_name)
15
+
16
+ def get(self, key: str):
17
+ return self._table.get_item(Key=key).get('Item')
18
+
19
+ def get_all(self):
20
+ return self._table.scan().get('Items')
21
+
22
+ def upsert(self, key, attributes):
23
+ return self.put({**key, **attributes})
24
+
25
+
26
+ def filter_by(self, attribute, target_value):
27
+ return self._table(
28
+ FilterExpression=Key(attribute).eq(target_value),
29
+ ).get('Items')
30
+
31
+ def put(self, attributes: dict, condition: dict = None):
32
+ extra_args = {}
33
+ if condition:
34
+ extra_args['ConditionExpression'] = condition
35
+ return self._table.put_item(Item=attributes, **extra_args)
36
+
37
+
38
+ def update(self, key: str, attributes: dict):
39
+ return self._table.update_item(
40
+ Key=key,
41
+ UpdateExpression=self._update_expression(attributes),
42
+ ExpressionAttributeNames=self._expression_attribute_names(attributes),
43
+ ExpressionAttributeValues=self._expression_attribute_values(attributes),
44
+ ReturnValues=RETURN_VALUES,
45
+ )
46
+
47
+ def delete(self, key: str):
48
+ return self._table.delete_item(Key=key)
49
+
50
+ def count(self) -> int:
51
+ return self._table.item_count
52
+
53
+
54
+ @classmethod
55
+ def _update_expression(cls, attributes):
56
+ return 'SET {param}'.format(
57
+ param=','.join(
58
+ '#{key}=:{key}'.format(
59
+ key=key,
60
+ )
61
+ for key in attributes
62
+ ),
63
+ )
64
+
65
+ @classmethod
66
+ def _expression_attribute_names(cls, attributes):
67
+ return {
68
+ '#{key}'.format(key=key): key for key in attributes
69
+ }
70
+
71
+ @classmethod
72
+ def _expression_attribute_values(cls, attributes):
73
+ return {
74
+ ':{key}'.format(key=key): attr for key, attr in attributes.items()
75
+ }
@@ -0,0 +1,38 @@
1
+ from dataclasses import dataclass
2
+ from typing import Optional
3
+
4
+ from python_layer.python import boto3
5
+
6
+ def remove_none_values(data: dict) -> dict: # noqa: WPS110
7
+ return {key: value for key, value in data.items() if value is not None} # noqa: WPS110
8
+
9
+
10
+ @dataclass
11
+ class S3Bucket(object):
12
+ bucket_url: str
13
+
14
+ def __post_init__(self):
15
+ self._resource = boto3.resource('s3')
16
+
17
+
18
+ def get(self, file_name: str):
19
+ return self._resource.Object(self.bucket_url, file_name).get()
20
+
21
+ def upload(self, file_name, file_content, content_type: Optional[str] = None):
22
+ optional_params = {'ContentType': content_type}
23
+ return self._resource.Object(self.bucket_url, file_name).put(
24
+ Body=file_content,
25
+ **remove_none_values(optional_params),
26
+ )
27
+
28
+ def delete(self, file_name):
29
+ return self._resource.Object(self.bucket_url, file_name).delete()
30
+
31
+ def get_url(self, file_name):
32
+ return 'https://{bucket_url}.s3.amazonaws.com/{file_name}'.format(
33
+ bucket_url=self.bucket_url,
34
+ file_name=file_name,
35
+ )
36
+
37
+ def read(self, file_name):
38
+ return self.get(file_name)['Body'].read()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: documente_shared
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: Shared utilities for Documente AI projects
5
5
  License: MIT
6
6
  Author: Tech
@@ -0,0 +1,13 @@
1
+ documente_shared/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ documente_shared/domain/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ documente_shared/domain/base_enum.py,sha256=DojAfn-zQdtjtImeHUpBzE6TBTm07XrbMOdW3h8RVd8,1449
4
+ documente_shared/domain/entities.py,sha256=0Flg2yWYdVM34hAwZmarn8CUKGZ9LchtC6Imeikbthk,3726
5
+ documente_shared/domain/enums.py,sha256=LF-gY3EyucQSmGBxXNy8z4Cb4Ry672muPtz5Xt7e9oo,227
6
+ documente_shared/domain/repositories.py,sha256=h_nArptP5Xh7Jrhden-Ii9rZQqzgLFHhuzj1p78LgmU,365
7
+ documente_shared/infrastructure/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ documente_shared/infrastructure/dynamo_repositories.py,sha256=RrJ3T1GsH5NNbaTqBwZ4_h34nWnoQ9mhcd5VDRiiv48,521
9
+ documente_shared/infrastructure/dynamo_table.py,sha256=H4X_SlK3DzOAt4wPQ8u4wkURt00bsKr5mfeSQTTA1O8,2094
10
+ documente_shared/infrastructure/s3_bucket.py,sha256=nJe6OCHhycPcl21gwazC_xxqVQ6y1cRS4T3LH14Hfjk,1180
11
+ documente_shared-0.1.3.dist-info/METADATA,sha256=1sL4e7TIZ29d2o1EEKaOkMLGdWtNMnfKicSX09H5zh0,639
12
+ documente_shared-0.1.3.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
13
+ documente_shared-0.1.3.dist-info/RECORD,,
@@ -1,5 +0,0 @@
1
- documente_shared/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- documente_shared/enums.py,sha256=CABjh0FRrBjoD2Q2-CwxTupnVclXi-YWYfurjnbg1kQ,210
3
- documente_shared-0.1.1.dist-info/METADATA,sha256=8iurH4PWXg_QYTByZwVIysyb16E4mw7bKr8s7jioRcQ,639
4
- documente_shared-0.1.1.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
5
- documente_shared-0.1.1.dist-info/RECORD,,