Skip to content

Commit ae74d5f

Browse files
authored
Merge pull request #3 from TaskarCenterAtUW/fix_configuration
Fix Multiple Issue
2 parents 148bca2 + ecc9d15 commit ae74d5f

66 files changed

Lines changed: 520 additions & 283 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Change log
22

3+
### 0.0.7
4+
- Fixed configuration issues
5+
- Fixed all the review comments
6+
- Refactor code
7+
- Removed unused code
8+
- Added local dev env using TDEI-local-server Package
9+
- Added local env support to -
10+
- Logger
11+
- Queue
12+
- Topic
13+
- Storage
14+
315
### 0.0.6
416
- Added get_remote_url function in FileEntity class which will return the uploaded file url
517

README.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ All the cloud connections are initialized with `initialize` function of core whi
2424
Eg.
2525
```python
2626
from python_ms_core import Core
27-
Core.initialize()
27+
core = Core() or Core(config='Local')
2828
```
2929
The method analyzes the `.env` variables and does a health check on what components are available
3030

@@ -56,12 +56,13 @@ This file will have to be generated or shared offline as per the developer requi
5656
Offers helper classes to help log the information. It is also used to record the audit messages
5757
as well as the analytics information required.
5858

59-
Use `Core.get_logger()`
59+
Use `core.get_logger()`
6060
Eg.
6161
```python
6262
from python_ms_core import Core
6363

64-
logger = Core.get_logger()
64+
core = Core()
65+
logger = core.get_logger()
6566

6667
# Record a metric
6768
logger.record_metric(name='test', value='test') # Metric and value
@@ -74,7 +75,8 @@ Eg.
7475
```python
7576
from python_ms_core import Core
7677

77-
logger = Core.get_logger()
78+
core = Core()
79+
logger = core.get_logger()
7880

7981
# Record a metric
8082
logger.debug('Debug Message')
@@ -102,15 +104,12 @@ The configuration required by Queue and Topic is similar and will be handled via
102104

103105
Topic can be accessed by the core method `get_topic`. This method takes two parameters
104106
1. topic name (required)
105-
2. callback function where messages will be received
106107

107108
```python
108109
from python_ms_core import Core
109110

110-
def callback(instance):
111-
print(instance.get_messages())
112-
113-
topic = Core.get_topic(topic_name='topicName', callback=callback)
111+
core = Core()
112+
topic = core.get_topic(topic_name='topicName')
114113

115114
```
116115

@@ -121,7 +120,8 @@ Once the topic object is got, use `publish` method to publish the message to top
121120
from python_ms_core import Core
122121
from python_ms_core.core.queue.models.queue_message import QueueMessage
123122

124-
topic = Core.get_topic(topic_name='topicName')
123+
core = Core()
124+
topic = core.get_topic(topic_name='topicName')
125125
topic.publish(QueueMessage.data_from(
126126
{
127127
'message': 'Test message'
@@ -139,18 +139,26 @@ It takes one parameter
139139
```python
140140
from python_ms_core import Core
141141

142-
topic = Core.get_topic(topic_name='topicName')
143-
msg = topic.subscribe(subscription='subscriptionName')
144-
print(msg)
142+
core = Core()
143+
topic_object = topic = Core.get_topic(topic_name='topicName')
144+
145+
def process(message):
146+
print(f'Message Received: {message}')
147+
148+
try:
149+
topic_object.subscribe(subscription='subscriptionName')
150+
except Exception as e:
151+
print(e)
145152
```
146153

147154
### Storage
148155
For all the azure blobs and other storages, storage components will offer simple ways to upload/download and read the existing data.
149156
```python
150157
from python_ms_core import Core
151158

159+
core = Core()
152160
# Create storage client
153-
azure_client = Core.get_storage_client()
161+
azure_client = core.get_storage_client()
154162

155163
# Get a container in the storage client
156164
container = azure_client.get_container(container_name='tdei-storage-test')
@@ -168,7 +176,8 @@ File upload is done only through read stream.
168176
from python_ms_core import Core
169177
from io import BytesIO, StringIO
170178

171-
azure_client = Core.get_storage_client()
179+
core = Core()
180+
azure_client = core.get_storage_client()
172181
container = azure_client.get_container(container_name='tdei-storage-test')
173182
# Create an instance of `AzureFileEntity` with name and mime-type
174183
txt = 'foo\nbar\nbaz'

freeze_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
build_date = date.today().strftime('%Y-%m-%d')
1313

14-
version = '0.0.6'
14+
version = '0.0.7'
1515

1616
with open(version_file_path, 'w+') as version_file:
1717
version_file.write("version = '{}'\n".format(version))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ six==1.16.0
1818
typing_extensions==4.4.0
1919
uamqp==1.6.3
2020
urllib3==1.26.14
21+
pika==1.3.1

src/example.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,57 @@
11
# Testing code
2-
32
import sys
3+
import os
4+
import time
45
import uuid
6+
import random
57
import datetime
68
from io import BytesIO, StringIO
7-
import time
89

910
from python_ms_core import Core
10-
from python_ms_core.core.queue.providers import azure_queue_config
1111
from python_ms_core.core.queue.models.queue_message import QueueMessage
1212

13-
Core.initialize()
13+
core = Core(config='Local')
1414
print('Hello')
1515

16-
topic = 'gtfs-flex-upload'
16+
topic = 'gtfspathways'
1717
subscription = 'upload-validation-processor-test'
1818
some_other_sub = 'usdufs'
1919

20-
topic_config = azure_queue_config.AzureQueueConfig()
21-
2220

2321
def publish_messages(topic_name):
24-
topic_object = Core.get_topic(topic_name=topic_name)
22+
topic_object = core.get_topic(topic_name=topic_name)
2523
queue_message = QueueMessage.data_from({
2624
'message': str(uuid.uuid4().hex),
27-
'data': {'a': 1}
25+
'data': {'a': random.randint(0, 1000)}
2826
})
2927
topic_object.publish(data=queue_message)
28+
print('Message Published')
3029

3130

3231
def subscribe(topic_name, subscription_name):
3332
def process(message):
34-
print(message)
33+
print(f'Message Received: {message}')
3534

36-
topic_object = Core.get_topic(topic_name=topic_name)
35+
topic_object = core.get_topic(topic_name=topic_name)
3736
try:
3837
topic_object.subscribe(subscription=subscription_name, callback=process)
3938
except Exception as e:
4039
print(e)
4140

4241

43-
publish_messages(topic)
4442
subscribe(topic, subscription)
4543

46-
azure_client = Core.get_storage_client()
44+
azure_client = core.get_storage_client()
4745

48-
container = azure_client.get_container(container_name='tdei-storage-test')
46+
container = azure_client.get_container(container_name='gtfspathways')
4947

5048
list_of_files = container.list_files()
51-
for single in list_of_files:
52-
print(single.name)
53-
firstFile = list_of_files[2]
54-
# print(firstFile.name+'<><>')
55-
file_content = firstFile.get_body_text()
49+
if len(list_of_files) > 0:
50+
for single in list_of_files:
51+
print(single.path)
52+
firstFile = list_of_files[0]
53+
# print(firstFile.name+'<><>')
54+
file_content = firstFile.get_body_text()
5655

5756
# Creating a text stream
5857
txt = 'foo\nbar\nbaz'
@@ -69,8 +68,12 @@ def process(message):
6968
print(test_file.get_remote_url())
7069
print('Uploaded Successfully')
7170

72-
logger = Core.get_logger()
71+
logger = core.get_logger()
7372
logger.record_metric(name='test', value='test')
7473

75-
logger = Core.get_logger(provider='Local')
76-
logger.record_metric(name='test', value='test')
74+
publish_messages(topic)
75+
time.sleep(2)
76+
77+
# logger = core.get_logger()
78+
# logger.record_metric(name='test', value='test')
79+
os._exit(os.EX_OK)

src/python_ms_core/__init__.py

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,50 @@
11
import logging
2-
import os
3-
from dotenv import load_dotenv
4-
from .models.config import CoreConfig
52
from .core.logger.logger import Logger
63
from .core.logger.local_logger import LocalLogger
7-
from .core.queue.topic import Topic
8-
from .core.storage.providers.azure import azure_storage_client, azure_file_entity
9-
from .core.storage.providers.azure.azure_storage_config import AzureStorageConfig
4+
from .core.topic.topic import Topic
5+
from .core.topic.local_topic import LocalTopic
6+
from .core.storage.providers.azure.azure_storage_client import AzureStorageClient
7+
from .core.storage.providers.local.local_storage_client import LocalStorageClient
8+
from .core.config.config import CoreConfig, LocalConfig
109

11-
load_dotenv()
10+
LOCAL_ENV = 'LOCAL'
11+
AZURE_ENV = 'AZURE'
1212

1313

1414
class Core:
15-
16-
# FIXME: Fix the configuration
1715
def __init__(self, config=None):
18-
19-
if config is not None:
20-
self.config = config
16+
if config is not None and config.upper() == LOCAL_ENV:
17+
self.config = LocalConfig()
2118
else:
22-
self.config = CoreConfig.default()
23-
24-
# FIXME: remove local from here and accept only Azure for now
25-
if self.config.provider == 'Local':
26-
logging.error(f'Unimplemented initialization for core {self.config.provider}')
27-
28-
@staticmethod
29-
def initialize():
30-
return Core().__check_health()
31-
32-
@staticmethod
33-
def get_logger(provider=None):
34-
if provider and provider.lower() == 'local':
35-
return LocalLogger(provider_config=provider)
19+
self.config = CoreConfig()
20+
self.__check_health()
21+
22+
def get_logger(self):
23+
logger_config = self.config.logger()
24+
if logger_config.provider.upper() == LOCAL_ENV:
25+
return LocalLogger(config=logger_config)
26+
elif logger_config.provider.upper() == AZURE_ENV:
27+
return Logger(config=logger_config)
3628
else:
37-
return Logger()
38-
39-
# @staticmethod
40-
# def get_custom_queue():
41-
42-
@staticmethod
43-
def get_topic(topic_name: str):
44-
if topic_name is not None:
45-
return Topic(config=CoreConfig.default(), topic_name=topic_name)
29+
logging.error(f'Unimplemented initialization for core {logger_config.provider}')
30+
31+
def get_topic(self, topic_name: str):
32+
topic_config = self.config.topic()
33+
if topic_config.provider.upper() == LOCAL_ENV:
34+
return LocalTopic(config=topic_config, topic_name=topic_name)
35+
elif topic_config.provider.upper() == AZURE_ENV:
36+
return Topic(config=topic_config, topic_name=topic_name)
4637
else:
47-
logging.error(f'Error: Topic name is required as function parameter')
48-
return
49-
50-
@staticmethod
51-
def get_storage_client():
52-
return azure_storage_client.AzureStorageClient(AzureStorageConfig())
38+
logging.error(f'Unimplemented initialization for core {topic_config.provider}')
39+
40+
def get_storage_client(self):
41+
storage_config = self.config.storage()
42+
if storage_config.provider.upper() == LOCAL_ENV:
43+
return LocalStorageClient(storage_config)
44+
elif storage_config.provider.upper() == AZURE_ENV:
45+
return AzureStorageClient(storage_config)
46+
else:
47+
logging.error(f'Unimplemented initialization for core {storage_config.provider}')
5348

5449
def __check_health(self):
5550
print('\x1b[32m ------------------------- \x1b[0m')
@@ -63,34 +58,34 @@ def __check_health(self):
6358

6459
print(f'Configured for \x1b[32m {self.config.provider} \x1b[0m \n')
6560

66-
if self.config.provider == 'Azure':
67-
logger_queue_name = os.environ.get('LOGGERQUEUE', None)
68-
queue_connection = os.environ.get('QUEUECONNECTION', None)
69-
storage_connection = os.environ.get('STORAGECONNECTION', None)
70-
71-
print('\x1b[31m > Checking Queue Connections\x1b[0m')
72-
if queue_connection is None:
73-
print('\x1b[33m Queue connection not available by default \x1b[0m')
74-
print('\x1b[33m Please configure QUEUECONNECTION in .env file to ensure queue communication \x1b[0m')
75-
print('\x1b[33m Note: All the logger functionality will be restricted to console \x1b[0m')
76-
else:
77-
print('\x1b[32m\x1b[40m Connected to Queues \x1b[0m')
61+
logger_config = self.config.logger()
62+
queue_config = self.config.queue()
63+
storage_config = self.config.storage()
64+
logger_queue_name = logger_config.queue_name
65+
queue_connection = queue_config.connection_string
66+
storage_connection = storage_config.connection_string
67+
print('\x1b[31m > Checking Queue Connections\x1b[0m')
68+
if queue_connection is None:
69+
print('\x1b[33m Queue connection not available by default \x1b[0m')
70+
print('\x1b[33m Please configure QUEUECONNECTION in .env file to ensure queue communication \x1b[0m')
71+
print('\x1b[33m Note: All the logger functionality will be restricted to console \x1b[0m')
72+
else:
73+
print('\x1b[32m\x1b[40m Connected to Queues \x1b[0m')
7874

79-
print('\x1b[31m\n > Checking Storage Connections\x1b[0m')
75+
print('\x1b[31m\n > Checking Storage Connections\x1b[0m')
8076

81-
if storage_connection is None:
82-
print('\x1b[31m Storage connection not available \x1b[0m')
83-
print('\x1b[31m Storage related functionalities will be unavailable \x1b[0m')
84-
print('\x1b[31m Please configure STORAGECONNECTION in .env for storage functions \x1b[0m')
85-
else:
86-
print('\x1b[32m\x1b[40m Connected to Storage \x1b[0m')
77+
if storage_connection is None:
78+
print('\x1b[31m Storage connection not available \x1b[0m')
79+
print('\x1b[31m Storage related functionalities will be unavailable \x1b[0m')
80+
print('\x1b[31m Please configure STORAGECONNECTION in .env for storage functions \x1b[0m')
81+
else:
82+
print('\x1b[32m\x1b[40m Connected to Storage \x1b[0m')
8783

88-
print('\x1b[31m\n > Checking Logger Queue \x1b[0m')
89-
if logger_queue_name is None:
90-
print('\x1b[33m Logger queue is not configured. App will write to \x1b[0m')
91-
print('\x1b[32m tdei-ms-log queue \x1b[0m')
92-
else:
93-
print('\x1b[32m Logger configured \x1b[0m')
94-
print('\x1b[32m ------------------------- \x1b[0m')
95-
return True
84+
print('\x1b[31m\n > Checking Logger Queue \x1b[0m')
85+
if logger_queue_name is None:
86+
print('\x1b[33m Logger queue is not configured. App will write to \x1b[0m')
87+
print('\x1b[32m tdei-ms-log queue \x1b[0m')
88+
else:
89+
print('\x1b[32m Logger configured \x1b[0m')
90+
print('\x1b[32m ------------------------- \x1b[0m')
9691
return True

0 commit comments

Comments
 (0)