ca4006-ca2/university.py

36 lines
1.0 KiB
Python

import pika
import os
from functions import log
if not os.path.exists("approved_projects"):
os.makedirs("approved_projects")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
channel = connection.channel()
channel.queue_declare(queue='approved_projects')
def process_approved_project(ch, method, properties, body):
proposal = body.decode()
acronym, title, description, amount = proposal.split(',')
filename = os.path.join(os.getcwd(), 'approved_projects', f'{acronym}.txt')
with open(filename, 'w') as f:
f.write(f'Acronym: {acronym}\n')
f.write(f'Researcher: {description}\n')
f.write(f'Budget: {amount}\n')
log(f'Approved project received: {body}')
channel.basic_consume(queue='approved_projects', on_message_callback=process_approved_project, auto_ack=True)
try:
log('University waiting for approved projects...')
channel.start_consuming()
except KeyboardInterrupt:
channel.stop_consuming()
connection.close()