|
1 | 1 | # Testing code |
2 | | - |
3 | 2 | import sys |
| 3 | +import os |
| 4 | +import time |
4 | 5 | import uuid |
| 6 | +import random |
5 | 7 | import datetime |
6 | 8 | from io import BytesIO, StringIO |
7 | | -import time |
8 | 9 |
|
9 | 10 | from python_ms_core import Core |
10 | | -from python_ms_core.core.queue.providers import azure_queue_config |
11 | 11 | from python_ms_core.core.queue.models.queue_message import QueueMessage |
12 | 12 |
|
13 | | -Core.initialize() |
| 13 | +core = Core(config='Local') |
14 | 14 | print('Hello') |
15 | 15 |
|
16 | | -topic = 'gtfs-flex-upload' |
| 16 | +topic = 'gtfspathways' |
17 | 17 | subscription = 'upload-validation-processor-test' |
18 | 18 | some_other_sub = 'usdufs' |
19 | 19 |
|
20 | | -topic_config = azure_queue_config.AzureQueueConfig() |
21 | 20 |
|
22 | | -azure_client = Core.get_storage_client() |
| 21 | +def publish_messages(topic_name): |
| 22 | + topic_object = core.get_topic(topic_name=topic_name) |
| 23 | + queue_message = QueueMessage.data_from({ |
| 24 | + 'message': str(uuid.uuid4().hex), |
| 25 | + 'data': {'a': random.randint(0, 1000)} |
| 26 | + }) |
| 27 | + topic_object.publish(data=queue_message) |
| 28 | + print('Message Published') |
| 29 | + |
| 30 | + |
| 31 | +def subscribe(topic_name, subscription_name): |
| 32 | + def process(message): |
| 33 | + print(f'Message Received: {message}') |
| 34 | + |
| 35 | + topic_object = core.get_topic(topic_name=topic_name) |
| 36 | + try: |
| 37 | + topic_object.subscribe(subscription=subscription_name, callback=process) |
| 38 | + except Exception as e: |
| 39 | + print(e) |
23 | 40 |
|
24 | | -container = azure_client.get_container(container_name='tdei-storage-test') |
| 41 | + |
| 42 | +subscribe(topic, subscription) |
| 43 | + |
| 44 | +azure_client = core.get_storage_client() |
| 45 | + |
| 46 | +container = azure_client.get_container(container_name='gtfspathways') |
25 | 47 |
|
26 | 48 | list_of_files = container.list_files() |
27 | | -for single in list_of_files: |
28 | | - print(single.name) |
29 | | -firstFile = list_of_files[2] |
30 | | -# print(firstFile.name+'<><>') |
31 | | -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() |
32 | 55 |
|
33 | 56 | # Creating a text stream |
34 | 57 | txt = 'foo\nbar\nbaz' |
35 | 58 | file_like_io = StringIO(txt) |
36 | 59 | basename = 'sample-file' |
37 | 60 | suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S") |
38 | 61 | filename = '_'.join([basename, suffix]) |
39 | | -test_file = container.create_file(f'{filename}.txt', 'text/plain') |
| 62 | +try: |
| 63 | + test_file = container.create_file(f'{filename}.txt') |
| 64 | +except Exception as e: |
| 65 | + print(e) |
40 | 66 | print('Start uploading...') |
41 | 67 | test_file.upload(file_like_io.read()) |
| 68 | +print(test_file.get_remote_url()) |
42 | 69 | print('Uploaded Successfully') |
43 | 70 |
|
44 | | -logger = Core.get_logger() |
45 | | -logger.record_metric(name='test', value='test') |
46 | | - |
47 | | -logger = Core.get_logger(provider='Local') |
| 71 | +logger = core.get_logger() |
48 | 72 | logger.record_metric(name='test', value='test') |
49 | 73 |
|
| 74 | +publish_messages(topic) |
| 75 | +time.sleep(2) |
50 | 76 |
|
51 | | -def on_connect_callback(instance): |
52 | | - print('Connected with result code {}'.format(instance.get_messages())) |
53 | | - time.sleep(30) |
54 | | - sys.exit() |
55 | | - |
56 | | - |
57 | | -topicObject1 = Core.get_topic(topic_name=topic, callback=on_connect_callback) |
58 | | -topicObject2 = Core.get_topic(topic_name='tipic') |
59 | | -try: |
60 | | - topicObject2.subscribe(subscription=subscription) |
61 | | -except Exception as e: |
62 | | - print(e) |
63 | | - |
64 | | -queue_message = QueueMessage.data_from({ |
65 | | - 'message': str(uuid.uuid4().hex), |
66 | | - 'data': {'a': 1} |
67 | | -}) |
68 | | - |
69 | | -topicObject1.publish(data=queue_message) |
70 | | - |
71 | | -topicObject1.subscribe(subscription=subscription) |
| 77 | +# logger = core.get_logger() |
| 78 | +# logger.record_metric(name='test', value='test') |
| 79 | +os._exit(os.EX_OK) |
0 commit comments