<commit_before>import os
import pika
import time

from flask import Flask
from flask import request
from flask_sqlalchemy import SQLAlchemy

db_path = 'sqlite:///' + os.path.join(os.path.dirname(__file__), 'flask_db.db')

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = db_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

pika_params = pika.URLParameters(os.environ.get('FLASK_AMQP_URL'))
conn_attempts = 30
while conn_attempts > 0:
    conn_attempts -= 1
    try:
        pika_conn = pika.BlockingConnection(pika_params)
    except pika.exceptions.AMQPConnectionError as e:
        print('AMQPConnectionError. Attempts remaining: ' + str(conn_attempts))
        time.sleep(5)

channel = pika_conn.channel()
channel.queue_declare(queue='add_note')
channel.queue_declare(queue='delete_note')

this_is_prod = True
flask_env = os.environ.get('FLASK_ENV')
if flask_env and flask_env == 'development':
    this_is_prod = False
else:
    channel.basic_consume(queue='add_note', auto_ack=True, on_message_callback=callback_add_note)
    channel.basic_consume(queue='delete_note', auto_ack=True, on_message_callback=callback_delete_note)

class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String(255), unique=True, nullable=False)

    def show(self):
        return self.text

def callback_add_note(ch, method, properties, body):
    note_text = body
    new_note = Note(text=note_text)
    db.session.add(new_note)
    db.session.commit()
    return

def callback_delete_note(ch, method, properties, body):
    note_text = body
    notes_deleted = []
    for note_to_del in Note.query.filter_by(text=note_text):
        notes_deleted.append(note_to_del.show())
        db.session.delete(note_to_del)
    db.session.commit()

@app.route('/')
def hello_world():
    show_api = ['initDb', 'showAll', 'addNote text', 'deleteNotes text']    
    return '<p>API:<br/>' + '<br/>'.join(show_api) + '</p>'

@app.route('/initDb/')
def init_db():
    db.create_all()
    return '<p>Init Completed!</p>'

@app.route('/addNote/')
def add_note():
    note_text = request.args.get('text', default='Note', type=str)
    new_note = Note(text=note_text)
    db.session.add(new_note)
    db.session.commit()
    if this_is_prod:
        channel.basic_publish(exchange='', routing_key='add_note', body=note_text)
    return '<p>' + note_text + ' - Added</p>'

@app.route('/deleteNotes/')
def delete_note():
    note_text = request.args.get('text', type=str)
    notes_deleted = []
    for note_to_del in Note.query.filter_by(text=note_text):
        notes_deleted.append(note_to_del.show())
        db.session.delete(note_to_del)
    db.session.commit()
    if this_is_prod:
        channel.basic_publish(exchange='', routing_key='delete_note', body=note_text)
    return '<p>' + '<br/>'.join(notes_deleted) + ' - Deleted</p>'

@app.route('/showAll/')
def say_i_run_on_flask():
    existing_notes = Note.query.all()
    notes_text = []
    for n in existing_notes:
        notes_text.append(n.show())
    return '<p>' + '<br/>'.join(notes_text) + '</p>'

if __name__ == "__main__":
    flask_env = os.environ.get('FLASK_ENV')
    if flask_env and flask_env == 'development':
        app.run(debug=True, host='0.0.0.0')

<commit_msg>added amqp conn url output<commit_after>import os
import pika
import time

from flask import Flask
from flask import request
from flask_sqlalchemy import SQLAlchemy

db_path = 'sqlite:///' + os.path.join(os.path.dirname(__file__), 'flask_db.db')

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = db_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

amqp_conn_url = os.environ.get('FLASK_AMQP_URL')
pika_params = pika.URLParameters(amqp_conn_url)
conn_attempts = 300
while conn_attempts > 0:
    conn_attempts -= 1
    try:
        pika_conn = pika.BlockingConnection(pika_params)
    except pika.exceptions.AMQPConnectionError as e:
        print(amqp_conn_url)
        print('AMQPConnectionError. Attempts remaining: ' + str(conn_attempts))
        time.sleep(10)

channel = pika_conn.channel()
channel.queue_declare(queue='add_note')
channel.queue_declare(queue='delete_note')

this_is_prod = True
flask_env = os.environ.get('FLASK_ENV')
if flask_env and flask_env == 'development':
    this_is_prod = False
else:
    channel.basic_consume(queue='add_note', auto_ack=True, on_message_callback=callback_add_note)
    channel.basic_consume(queue='delete_note', auto_ack=True, on_message_callback=callback_delete_note)

class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.String(255), unique=True, nullable=False)

    def show(self):
        return self.text

def callback_add_note(ch, method, properties, body):
    note_text = body
    new_note = Note(text=note_text)
    db.session.add(new_note)
    db.session.commit()
    return

def callback_delete_note(ch, method, properties, body):
    note_text = body
    notes_deleted = []
    for note_to_del in Note.query.filter_by(text=note_text):
        notes_deleted.append(note_to_del.show())
        db.session.delete(note_to_del)
    db.session.commit()

@app.route('/')
def hello_world():
    show_api = ['initDb', 'showAll', 'addNote text', 'deleteNotes text']    
    return '<p>API:<br/>' + '<br/>'.join(show_api) + '</p>'

@app.route('/initDb/')
def init_db():
    db.create_all()
    return '<p>Init Completed!</p>'

@app.route('/addNote/')
def add_note():
    note_text = request.args.get('text', default='Note', type=str)
    new_note = Note(text=note_text)
    db.session.add(new_note)
    db.session.commit()
    if this_is_prod:
        channel.basic_publish(exchange='', routing_key='add_note', body=note_text)
    return '<p>' + note_text + ' - Added</p>'

@app.route('/deleteNotes/')
def delete_note():
    note_text = request.args.get('text', type=str)
    notes_deleted = []
    for note_to_del in Note.query.filter_by(text=note_text):
        notes_deleted.append(note_to_del.show())
        db.session.delete(note_to_del)
    db.session.commit()
    if this_is_prod:
        channel.basic_publish(exchange='', routing_key='delete_note', body=note_text)
    return '<p>' + '<br/>'.join(notes_deleted) + ' - Deleted</p>'

@app.route('/showAll/')
def say_i_run_on_flask():
    existing_notes = Note.query.all()
    notes_text = []
    for n in existing_notes:
        notes_text.append(n.show())
    return '<p>' + '<br/>'.join(notes_text) + '</p>'

if __name__ == "__main__":
    flask_env = os.environ.get('FLASK_ENV')
    if flask_env and flask_env == 'development':
        app.run(debug=True, host='0.0.0.0')


{'fraction_non_alphanumeric': 0.14034322280395578, 'fraction_numerical': 0.0037812681791739382, 'mean_word_length': 3.618535930154466, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3826223', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2413, 'n_words': 415}