lol
This commit is contained in:
240
app.py
240
app.py
@@ -5,14 +5,10 @@ from datetime import datetime
|
|||||||
from werkzeug.security import generate_password_hash, check_password_hash
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
import os
|
import os
|
||||||
|
from config import Config
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SECRET_KEY'] = os.urandom(24)
|
app.config.from_object(Config)
|
||||||
# Use DATABASE_URL from environment if available, else default to SQLite
|
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///site.db')
|
|
||||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
||||||
app.config['UPLOAD_FOLDER'] = 'static/uploads'
|
|
||||||
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 # 2MB max file size
|
|
||||||
|
|
||||||
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
||||||
|
|
||||||
@@ -23,7 +19,7 @@ migrate = Migrate(app, db)
|
|||||||
class User(db.Model):
|
class User(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
password_hash = db.Column(db.String(128), nullable=False)
|
password_hash = db.Column(db.String(256), nullable=False)
|
||||||
|
|
||||||
def set_password(self, password):
|
def set_password(self, password):
|
||||||
self.password_hash = generate_password_hash(password)
|
self.password_hash = generate_password_hash(password)
|
||||||
@@ -270,6 +266,14 @@ def manage_environments():
|
|||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
icon_file = request.files.get('icon')
|
icon_file = request.files.get('icon')
|
||||||
icon_filename = None
|
icon_filename = None
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('manage_environments'))
|
||||||
|
if Environment.query.filter_by(name=name).first():
|
||||||
|
flash('Environment with this name already exists!', 'danger')
|
||||||
|
print('Duplicate environment name')
|
||||||
|
return redirect(url_for('manage_environments'))
|
||||||
if icon_file and icon_file.filename:
|
if icon_file and icon_file.filename:
|
||||||
icon_filename = secure_filename(icon_file.filename)
|
icon_filename = secure_filename(icon_file.filename)
|
||||||
icon_path = os.path.join('static/icons', icon_filename)
|
icon_path = os.path.join('static/icons', icon_filename)
|
||||||
@@ -278,11 +282,18 @@ def manage_environments():
|
|||||||
icon_file.save(icon_path)
|
icon_file.save(icon_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error saving icon:', e)
|
print('Error saving icon:', e)
|
||||||
|
flash(f'Error saving icon: {e}', 'danger')
|
||||||
|
try:
|
||||||
environment = Environment(name=name, description=description, icon=icon_filename)
|
environment = Environment(name=name, description=description, icon=icon_filename)
|
||||||
db.session.add(environment)
|
db.session.add(environment)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Environment added successfully!', 'success')
|
flash('Environment added successfully!', 'success')
|
||||||
return redirect(url_for('manage_environments'))
|
print('Environment added:', name)
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding environment:', e)
|
||||||
|
flash(f'Error adding environment: {e}', 'danger')
|
||||||
|
return redirect(url_for('manage_environments'))
|
||||||
environments = Environment.query.all()
|
environments = Environment.query.all()
|
||||||
return render_template('manage_environments.html',
|
return render_template('manage_environments.html',
|
||||||
environments=environments,
|
environments=environments,
|
||||||
@@ -339,6 +350,14 @@ def manage_climates():
|
|||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
icon_file = request.files.get('icon')
|
icon_file = request.files.get('icon')
|
||||||
icon_filename = None
|
icon_filename = None
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('manage_climates'))
|
||||||
|
if Climate.query.filter_by(name=name).first():
|
||||||
|
flash('Climate with this name already exists!', 'danger')
|
||||||
|
print('Duplicate climate name')
|
||||||
|
return redirect(url_for('manage_climates'))
|
||||||
if icon_file and icon_file.filename:
|
if icon_file and icon_file.filename:
|
||||||
icon_filename = secure_filename(icon_file.filename)
|
icon_filename = secure_filename(icon_file.filename)
|
||||||
icon_path = os.path.join('static/icons', icon_filename)
|
icon_path = os.path.join('static/icons', icon_filename)
|
||||||
@@ -347,11 +366,18 @@ def manage_climates():
|
|||||||
icon_file.save(icon_path)
|
icon_file.save(icon_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error saving icon:', e)
|
print('Error saving icon:', e)
|
||||||
|
flash(f'Error saving icon: {e}', 'danger')
|
||||||
|
try:
|
||||||
climate = Climate(name=name, description=description, icon=icon_filename)
|
climate = Climate(name=name, description=description, icon=icon_filename)
|
||||||
db.session.add(climate)
|
db.session.add(climate)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Climate added successfully!', 'success')
|
flash('Climate added successfully!', 'success')
|
||||||
return redirect(url_for('manage_climates'))
|
print('Climate added:', name)
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding climate:', e)
|
||||||
|
flash(f'Error adding climate: {e}', 'danger')
|
||||||
|
return redirect(url_for('manage_climates'))
|
||||||
climates = Climate.query.all()
|
climates = Climate.query.all()
|
||||||
return render_template('manage_climates.html',
|
return render_template('manage_climates.html',
|
||||||
climates=climates,
|
climates=climates,
|
||||||
@@ -470,30 +496,59 @@ def new_plant():
|
|||||||
picture_file = request.files.get('picture')
|
picture_file = request.files.get('picture')
|
||||||
picture_filename = None
|
picture_filename = None
|
||||||
|
|
||||||
|
# Required field checks
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('new_plant'))
|
||||||
|
if not climate_id:
|
||||||
|
flash('Climate is required!', 'danger')
|
||||||
|
print('No climate provided')
|
||||||
|
return redirect(url_for('new_plant'))
|
||||||
|
if not environment_id:
|
||||||
|
flash('Environment is required!', 'danger')
|
||||||
|
print('No environment provided')
|
||||||
|
return redirect(url_for('new_plant'))
|
||||||
|
if Plant.query.filter_by(name=name).first():
|
||||||
|
flash('A plant with this name already exists!', 'danger')
|
||||||
|
print('Duplicate plant name')
|
||||||
|
return redirect(url_for('new_plant'))
|
||||||
|
|
||||||
if picture_file and picture_file.filename:
|
if picture_file and picture_file.filename:
|
||||||
filename = secure_filename(picture_file.filename)
|
filename = secure_filename(picture_file.filename)
|
||||||
picture_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
picture_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||||||
picture_file.save(picture_path)
|
try:
|
||||||
picture_filename = filename
|
picture_file.save(picture_path)
|
||||||
|
picture_filename = filename
|
||||||
|
except Exception as e:
|
||||||
|
print('Error saving plant picture:', e)
|
||||||
|
flash(f'Error saving plant picture: {e}', 'danger')
|
||||||
|
|
||||||
plant = Plant(
|
try:
|
||||||
name=name,
|
plant = Plant(
|
||||||
picture=picture_filename,
|
name=name,
|
||||||
climate_id=climate_id,
|
picture=picture_filename,
|
||||||
environment_id=environment_id,
|
climate_id=climate_id,
|
||||||
light_id=light_id,
|
environment_id=environment_id,
|
||||||
toxicity_id=toxicity_id,
|
light_id=light_id,
|
||||||
size_id=size_id,
|
toxicity_id=toxicity_id,
|
||||||
care_difficulty_id=care_difficulty_id,
|
size_id=size_id,
|
||||||
growth_rate_id=growth_rate_id,
|
care_difficulty_id=care_difficulty_id,
|
||||||
products=','.join(product_ids),
|
growth_rate_id=growth_rate_id,
|
||||||
description=description,
|
products=','.join(product_ids),
|
||||||
care_guide=care_guide
|
description=description,
|
||||||
)
|
care_guide=care_guide
|
||||||
db.session.add(plant)
|
)
|
||||||
db.session.commit()
|
db.session.add(plant)
|
||||||
flash('Your plant has been added!', 'success')
|
db.session.commit()
|
||||||
return redirect(url_for('home'))
|
flash('Your plant has been added!', 'success')
|
||||||
|
print('Plant added:', name)
|
||||||
|
return redirect(url_for('manage_plants', add=1))
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding plant:', e)
|
||||||
|
flash(f'Error adding plant: {e}', 'danger')
|
||||||
|
return redirect(url_for('new_plant'))
|
||||||
|
|
||||||
climates = Climate.query.all()
|
climates = Climate.query.all()
|
||||||
environments = Environment.query.all()
|
environments = Environment.query.all()
|
||||||
@@ -591,20 +646,37 @@ def manage_plants():
|
|||||||
climates = Climate.query.all()
|
climates = Climate.query.all()
|
||||||
environments = Environment.query.all()
|
environments = Environment.query.all()
|
||||||
products = Product.query.all()
|
products = Product.query.all()
|
||||||
|
lights = Light.query.order_by(Light.name).all()
|
||||||
|
toxicities = Toxicity.query.order_by(Toxicity.name).all()
|
||||||
|
sizes = Size.query.order_by(Size.name).all()
|
||||||
|
difficulties = CareDifficulty.query.order_by(CareDifficulty.name).all()
|
||||||
|
growth_rates = GrowthRate.query.order_by(GrowthRate.name).all()
|
||||||
|
|
||||||
|
# Create dictionaries for easy lookup
|
||||||
|
climates_dict = {climate.id: climate.name for climate in climates}
|
||||||
|
environments_dict = {env.id: env.name for env in environments}
|
||||||
|
|
||||||
return render_template('manage_plants.html',
|
return render_template('manage_plants.html',
|
||||||
plants=plants,
|
plants=plants,
|
||||||
climates=climates,
|
climates=climates, # list for form
|
||||||
environments=environments,
|
environments=environments, # list for form
|
||||||
|
climates_dict=climates_dict, # dict for table
|
||||||
|
environments_dict=environments_dict, # dict for table
|
||||||
products=products,
|
products=products,
|
||||||
|
lights=lights,
|
||||||
|
toxicities=toxicities,
|
||||||
|
sizes=sizes,
|
||||||
|
difficulties=difficulties,
|
||||||
|
growth_rates=growth_rates,
|
||||||
plant_count=len(plants),
|
plant_count=len(plants),
|
||||||
climate_count=len(climates),
|
climate_count=len(climates),
|
||||||
environment_count=len(environments),
|
environment_count=len(environments),
|
||||||
product_count=len(products),
|
product_count=len(products),
|
||||||
light_count=len(Light.query.all()),
|
light_count=len(lights),
|
||||||
toxicity_count=len(Toxicity.query.all()),
|
toxicity_count=len(toxicities),
|
||||||
size_count=len(Size.query.all()),
|
size_count=len(sizes),
|
||||||
difficulty_count=len(CareDifficulty.query.all()),
|
difficulty_count=len(difficulties),
|
||||||
rate_count=len(GrowthRate.query.all()))
|
rate_count=len(growth_rates))
|
||||||
|
|
||||||
@app.route('/admin/attributes')
|
@app.route('/admin/attributes')
|
||||||
def manage_attributes():
|
def manage_attributes():
|
||||||
@@ -634,6 +706,14 @@ def manage_lights():
|
|||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
icon_file = request.files.get('icon')
|
icon_file = request.files.get('icon')
|
||||||
icon_filename = None
|
icon_filename = None
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('manage_lights'))
|
||||||
|
if Light.query.filter_by(name=name).first():
|
||||||
|
flash('Light requirement with this name already exists!', 'danger')
|
||||||
|
print('Duplicate light name')
|
||||||
|
return redirect(url_for('manage_lights'))
|
||||||
if icon_file and icon_file.filename:
|
if icon_file and icon_file.filename:
|
||||||
icon_filename = secure_filename(icon_file.filename)
|
icon_filename = secure_filename(icon_file.filename)
|
||||||
icon_path = os.path.join('static/icons', icon_filename)
|
icon_path = os.path.join('static/icons', icon_filename)
|
||||||
@@ -642,11 +722,18 @@ def manage_lights():
|
|||||||
icon_file.save(icon_path)
|
icon_file.save(icon_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error saving icon:', e)
|
print('Error saving icon:', e)
|
||||||
|
flash(f'Error saving icon: {e}', 'danger')
|
||||||
|
try:
|
||||||
light = Light(name=name, description=description, icon=icon_filename)
|
light = Light(name=name, description=description, icon=icon_filename)
|
||||||
db.session.add(light)
|
db.session.add(light)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Light requirement added successfully!', 'success')
|
flash('Light requirement added successfully!', 'success')
|
||||||
return redirect(url_for('manage_lights'))
|
print('Light added:', name)
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding light:', e)
|
||||||
|
flash(f'Error adding light: {e}', 'danger')
|
||||||
|
return redirect(url_for('manage_lights'))
|
||||||
lights = Light.query.all()
|
lights = Light.query.all()
|
||||||
return render_template('manage_lights.html',
|
return render_template('manage_lights.html',
|
||||||
lights=lights,
|
lights=lights,
|
||||||
@@ -703,6 +790,14 @@ def manage_toxicities():
|
|||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
icon_file = request.files.get('icon')
|
icon_file = request.files.get('icon')
|
||||||
icon_filename = None
|
icon_filename = None
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('manage_toxicities'))
|
||||||
|
if Toxicity.query.filter_by(name=name).first():
|
||||||
|
flash('Toxicity with this name already exists!', 'danger')
|
||||||
|
print('Duplicate toxicity name')
|
||||||
|
return redirect(url_for('manage_toxicities'))
|
||||||
if icon_file and icon_file.filename:
|
if icon_file and icon_file.filename:
|
||||||
icon_filename = secure_filename(icon_file.filename)
|
icon_filename = secure_filename(icon_file.filename)
|
||||||
icon_path = os.path.join('static/icons', icon_filename)
|
icon_path = os.path.join('static/icons', icon_filename)
|
||||||
@@ -711,11 +806,18 @@ def manage_toxicities():
|
|||||||
icon_file.save(icon_path)
|
icon_file.save(icon_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error saving icon:', e)
|
print('Error saving icon:', e)
|
||||||
|
flash(f'Error saving icon: {e}', 'danger')
|
||||||
|
try:
|
||||||
toxicity = Toxicity(name=name, description=description, icon=icon_filename)
|
toxicity = Toxicity(name=name, description=description, icon=icon_filename)
|
||||||
db.session.add(toxicity)
|
db.session.add(toxicity)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Toxicity level added successfully!', 'success')
|
flash('Toxicity level added successfully!', 'success')
|
||||||
return redirect(url_for('manage_toxicities'))
|
print('Toxicity added:', name)
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding toxicity:', e)
|
||||||
|
flash(f'Error adding toxicity: {e}', 'danger')
|
||||||
|
return redirect(url_for('manage_toxicities'))
|
||||||
toxicities = Toxicity.query.all()
|
toxicities = Toxicity.query.all()
|
||||||
return render_template('manage_toxicities.html',
|
return render_template('manage_toxicities.html',
|
||||||
toxicities=toxicities,
|
toxicities=toxicities,
|
||||||
@@ -772,6 +874,14 @@ def manage_sizes():
|
|||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
icon_file = request.files.get('icon')
|
icon_file = request.files.get('icon')
|
||||||
icon_filename = None
|
icon_filename = None
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('manage_sizes'))
|
||||||
|
if Size.query.filter_by(name=name).first():
|
||||||
|
flash('Size with this name already exists!', 'danger')
|
||||||
|
print('Duplicate size name')
|
||||||
|
return redirect(url_for('manage_sizes'))
|
||||||
if icon_file and icon_file.filename:
|
if icon_file and icon_file.filename:
|
||||||
icon_filename = secure_filename(icon_file.filename)
|
icon_filename = secure_filename(icon_file.filename)
|
||||||
icon_path = os.path.join('static/icons', icon_filename)
|
icon_path = os.path.join('static/icons', icon_filename)
|
||||||
@@ -780,11 +890,18 @@ def manage_sizes():
|
|||||||
icon_file.save(icon_path)
|
icon_file.save(icon_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error saving icon:', e)
|
print('Error saving icon:', e)
|
||||||
|
flash(f'Error saving icon: {e}', 'danger')
|
||||||
|
try:
|
||||||
size = Size(name=name, description=description, icon=icon_filename)
|
size = Size(name=name, description=description, icon=icon_filename)
|
||||||
db.session.add(size)
|
db.session.add(size)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Size category added successfully!', 'success')
|
flash('Size category added successfully!', 'success')
|
||||||
return redirect(url_for('manage_sizes'))
|
print('Size added:', name)
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding size:', e)
|
||||||
|
flash(f'Error adding size: {e}', 'danger')
|
||||||
|
return redirect(url_for('manage_sizes'))
|
||||||
sizes = Size.query.all()
|
sizes = Size.query.all()
|
||||||
return render_template('manage_sizes.html',
|
return render_template('manage_sizes.html',
|
||||||
sizes=sizes,
|
sizes=sizes,
|
||||||
@@ -841,6 +958,14 @@ def manage_care_difficulties():
|
|||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
icon_file = request.files.get('icon')
|
icon_file = request.files.get('icon')
|
||||||
icon_filename = None
|
icon_filename = None
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('manage_care_difficulties'))
|
||||||
|
if CareDifficulty.query.filter_by(name=name).first():
|
||||||
|
flash('Care difficulty with this name already exists!', 'danger')
|
||||||
|
print('Duplicate care difficulty name')
|
||||||
|
return redirect(url_for('manage_care_difficulties'))
|
||||||
if icon_file and icon_file.filename:
|
if icon_file and icon_file.filename:
|
||||||
icon_filename = secure_filename(icon_file.filename)
|
icon_filename = secure_filename(icon_file.filename)
|
||||||
icon_path = os.path.join('static/icons', icon_filename)
|
icon_path = os.path.join('static/icons', icon_filename)
|
||||||
@@ -849,11 +974,18 @@ def manage_care_difficulties():
|
|||||||
icon_file.save(icon_path)
|
icon_file.save(icon_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error saving icon:', e)
|
print('Error saving icon:', e)
|
||||||
|
flash(f'Error saving icon: {e}', 'danger')
|
||||||
|
try:
|
||||||
difficulty = CareDifficulty(name=name, description=description, icon=icon_filename)
|
difficulty = CareDifficulty(name=name, description=description, icon=icon_filename)
|
||||||
db.session.add(difficulty)
|
db.session.add(difficulty)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Care difficulty level added successfully!', 'success')
|
flash('Care difficulty level added successfully!', 'success')
|
||||||
return redirect(url_for('manage_care_difficulties'))
|
print('Care difficulty added:', name)
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding care difficulty:', e)
|
||||||
|
flash(f'Error adding care difficulty: {e}', 'danger')
|
||||||
|
return redirect(url_for('manage_care_difficulties'))
|
||||||
difficulties = CareDifficulty.query.all()
|
difficulties = CareDifficulty.query.all()
|
||||||
return render_template('manage_care_difficulties.html',
|
return render_template('manage_care_difficulties.html',
|
||||||
difficulties=difficulties,
|
difficulties=difficulties,
|
||||||
@@ -910,6 +1042,14 @@ def manage_growth_rates():
|
|||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
icon_file = request.files.get('icon')
|
icon_file = request.files.get('icon')
|
||||||
icon_filename = None
|
icon_filename = None
|
||||||
|
if not name:
|
||||||
|
flash('Name is required!', 'danger')
|
||||||
|
print('No name provided')
|
||||||
|
return redirect(url_for('manage_growth_rates'))
|
||||||
|
if GrowthRate.query.filter_by(name=name).first():
|
||||||
|
flash('Growth rate with this name already exists!', 'danger')
|
||||||
|
print('Duplicate growth rate name')
|
||||||
|
return redirect(url_for('manage_growth_rates'))
|
||||||
if icon_file and icon_file.filename:
|
if icon_file and icon_file.filename:
|
||||||
icon_filename = secure_filename(icon_file.filename)
|
icon_filename = secure_filename(icon_file.filename)
|
||||||
icon_path = os.path.join('static/icons', icon_filename)
|
icon_path = os.path.join('static/icons', icon_filename)
|
||||||
@@ -918,11 +1058,18 @@ def manage_growth_rates():
|
|||||||
icon_file.save(icon_path)
|
icon_file.save(icon_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error saving icon:', e)
|
print('Error saving icon:', e)
|
||||||
|
flash(f'Error saving icon: {e}', 'danger')
|
||||||
|
try:
|
||||||
rate = GrowthRate(name=name, description=description, icon=icon_filename)
|
rate = GrowthRate(name=name, description=description, icon=icon_filename)
|
||||||
db.session.add(rate)
|
db.session.add(rate)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('Growth rate category added successfully!', 'success')
|
flash('Growth rate category added successfully!', 'success')
|
||||||
return redirect(url_for('manage_growth_rates'))
|
print('Growth rate added:', name)
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
print('Error adding growth rate:', e)
|
||||||
|
flash(f'Error adding growth rate: {e}', 'danger')
|
||||||
|
return redirect(url_for('manage_growth_rates'))
|
||||||
rates = GrowthRate.query.all()
|
rates = GrowthRate.query.all()
|
||||||
return render_template('manage_growth_rates.html',
|
return render_template('manage_growth_rates.html',
|
||||||
rates=rates,
|
rates=rates,
|
||||||
@@ -1147,4 +1294,15 @@ def seed_db():
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
with app.app_context():
|
||||||
|
db.create_all() # Create all tables
|
||||||
|
# Create admin user if it doesn't exist
|
||||||
|
admin = User.query.filter_by(username='admin').first()
|
||||||
|
if not admin:
|
||||||
|
admin = User(username='admin')
|
||||||
|
admin.set_password('admin123') # Set a default password
|
||||||
|
db.session.add(admin)
|
||||||
|
db.session.commit()
|
||||||
|
print("Admin user created with username: admin and password: admin123")
|
||||||
|
seed_db() # Seed initial data
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
8
config.py
Normal file
8
config.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
SECRET_KEY = os.environ.get('SECRET_KEY') or os.urandom(24)
|
||||||
|
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'postgresql://postgres:1253@localhost:5432/verpotjelot'
|
||||||
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
UPLOAD_FOLDER = 'static/uploads'
|
||||||
|
MAX_CONTENT_LENGTH = 2 * 1024 * 1024 # 2MB max file size
|
||||||
130
migrations/versions/dd9a310269e3_initial_migration.py
Normal file
130
migrations/versions/dd9a310269e3_initial_migration.py
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
"""Initial migration
|
||||||
|
|
||||||
|
Revision ID: dd9a310269e3
|
||||||
|
Revises:
|
||||||
|
Create Date: 2025-05-22 20:59:13.076749
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'dd9a310269e3'
|
||||||
|
down_revision = None
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('care_difficulty',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=50), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('icon', sa.String(length=200), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('climate',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=100), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('icon', sa.String(length=200), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('environment',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=100), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('icon', sa.String(length=200), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('growth_rate',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=50), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('icon', sa.String(length=200), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('light',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=50), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('icon', sa.String(length=200), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('product',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=100), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('size',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=50), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('icon', sa.String(length=200), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('toxicity',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=50), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('icon', sa.String(length=200), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
op.create_table('user',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('username', sa.String(length=80), nullable=False),
|
||||||
|
sa.Column('password_hash', sa.String(length=128), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('username')
|
||||||
|
)
|
||||||
|
op.create_table('plant',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=100), nullable=False),
|
||||||
|
sa.Column('picture', sa.String(length=200), nullable=True),
|
||||||
|
sa.Column('climate_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('environment_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('light_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('toxicity_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('size_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('care_difficulty_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('growth_rate_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('products', sa.Text(), nullable=True),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('date_added', sa.DateTime(), nullable=False),
|
||||||
|
sa.Column('care_guide', sa.Text(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['care_difficulty_id'], ['care_difficulty.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['climate_id'], ['climate.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['environment_id'], ['environment.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['growth_rate_id'], ['growth_rate.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['light_id'], ['light.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['size_id'], ['size.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['toxicity_id'], ['toxicity.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('plant')
|
||||||
|
op.drop_table('user')
|
||||||
|
op.drop_table('toxicity')
|
||||||
|
op.drop_table('size')
|
||||||
|
op.drop_table('product')
|
||||||
|
op.drop_table('light')
|
||||||
|
op.drop_table('growth_rate')
|
||||||
|
op.drop_table('environment')
|
||||||
|
op.drop_table('climate')
|
||||||
|
op.drop_table('care_difficulty')
|
||||||
|
# ### end Alembic commands ###
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
"""Increase password hash length
|
||||||
|
|
||||||
|
Revision ID: f83d55cc5aa7
|
||||||
|
Revises: dd9a310269e3
|
||||||
|
Create Date: 2025-05-22 21:01:20.068388
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'f83d55cc5aa7'
|
||||||
|
down_revision = 'dd9a310269e3'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('user', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('password_hash',
|
||||||
|
existing_type=sa.VARCHAR(length=128),
|
||||||
|
type_=sa.String(length=256),
|
||||||
|
existing_nullable=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('user', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('password_hash',
|
||||||
|
existing_type=sa.String(length=256),
|
||||||
|
type_=sa.VARCHAR(length=128),
|
||||||
|
existing_nullable=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
Flask==2.3.3
|
Flask==3.0.2
|
||||||
Flask-SQLAlchemy==3.1.1
|
Flask-SQLAlchemy==3.1.1
|
||||||
Flask-Login==0.6.2
|
Flask-Login==0.6.2
|
||||||
Flask-WTF==1.2.1
|
Flask-WTF==1.2.1
|
||||||
Flask-Migrate==4.0.5
|
Flask-Migrate==4.0.5
|
||||||
SQLAlchemy==2.0.23
|
SQLAlchemy==2.0.23
|
||||||
Werkzeug==2.3.7
|
Werkzeug==3.0.1
|
||||||
WTForms==3.1.1
|
WTForms==3.1.1
|
||||||
python-dotenv==1.0.0
|
python-dotenv==1.0.1
|
||||||
psycopg2-binary==2.9.9
|
psycopg2-binary==2.9.9
|
||||||
gunicorn==21.2.0
|
gunicorn==21.2.0
|
||||||
@@ -99,7 +99,16 @@
|
|||||||
<textarea name="description" id="description" rows="6" required
|
<textarea name="description" id="description" rows="6" required
|
||||||
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500"></textarea>
|
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500"></textarea>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="w-full btn-main text-lg font-semibold">Add Plant</button>
|
<div class="md:col-span-2">
|
||||||
|
<label for="care_guide" class="block text-sm font-medium text-gray-700">Care Guide</label>
|
||||||
|
<textarea name="care_guide" id="care_guide" rows="6"
|
||||||
|
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="flex justify-center mt-8">
|
||||||
|
<button type="submit" class="px-8 py-3 bg-blue-600 text-white font-semibold rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-colors">
|
||||||
|
Save Plant
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -9,12 +9,12 @@
|
|||||||
<button id="show-add-plant" class="btn-main px-6 py-2 font-semibold">Add Plant</button>
|
<button id="show-add-plant" class="btn-main px-6 py-2 font-semibold">Add Plant</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="add-plant-form-card" class="hidden mb-8">
|
<div id="add-plant-form-card" class="hidden mb-8">
|
||||||
<div class="bg-gray-50 rounded-lg shadow p-6 max-w-2xl mx-auto">
|
<div class="bg-gray-50 rounded-lg shadow p-6 max-w-2xl mx-auto" style="overflow:visible;">
|
||||||
<div class="flex justify-between items-center mb-4">
|
<div class="flex justify-between items-center mb-4">
|
||||||
<h3 class="text-xl font-bold">Add New Plant</h3>
|
<h3 class="text-xl font-bold">Add New Plant</h3>
|
||||||
<button type="button" id="close-add-plant" class="text-gray-500 hover:text-red-500 text-2xl font-bold leading-none">×</button>
|
<button type="button" id="close-add-plant" class="text-gray-500 hover:text-red-500 text-2xl font-bold leading-none">×</button>
|
||||||
</div>
|
</div>
|
||||||
<form id="add-plant-form" method="POST" enctype="multipart/form-data" class="space-y-4">
|
<form id="add-plant-form" method="POST" action="{{ url_for('new_plant') }}" enctype="multipart/form-data" class="space-y-4 pb-16">
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
<div>
|
<div>
|
||||||
<label for="name" class="block text-sm font-medium text-gray-700">Name</label>
|
<label for="name" class="block text-sm font-medium text-gray-700">Name</label>
|
||||||
@@ -47,48 +47,48 @@
|
|||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="light" class="block text-sm font-medium text-gray-700">Light</label>
|
<label for="light_id" class="block text-sm font-medium text-gray-700">Light</label>
|
||||||
<select name="light" id="light" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
<select name="light_id" id="light_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
||||||
<option value="">Select light</option>
|
<option value="">Select light requirement</option>
|
||||||
<option value="Full Sun">Full Sun</option>
|
{% for light in lights %}
|
||||||
<option value="Partial Shade">Partial Shade</option>
|
<option value="{{ light.id }}">{{ light.name }}</option>
|
||||||
<option value="Low Light">Low Light</option>
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="toxicity" class="block text-sm font-medium text-gray-700">Toxicity</label>
|
<label for="toxicity_id" class="block text-sm font-medium text-gray-700">Toxicity</label>
|
||||||
<select name="toxicity" id="toxicity" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
<select name="toxicity_id" id="toxicity_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
||||||
<option value="">Select toxicity</option>
|
<option value="">Select toxicity level</option>
|
||||||
<option value="Pet Safe">Pet Safe</option>
|
{% for toxicity in toxicities %}
|
||||||
<option value="Toxic to Pets">Toxic to Pets</option>
|
<option value="{{ toxicity.id }}">{{ toxicity.name }}</option>
|
||||||
<option value="Unknown">Unknown</option>
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="size" class="block text-sm font-medium text-gray-700">Size</label>
|
<label for="size_id" class="block text-sm font-medium text-gray-700">Size</label>
|
||||||
<select name="size" id="size" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
<select name="size_id" id="size_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
||||||
<option value="">Select size</option>
|
<option value="">Select size category</option>
|
||||||
<option value="Small">Small</option>
|
{% for size in sizes %}
|
||||||
<option value="Medium">Medium</option>
|
<option value="{{ size.id }}">{{ size.name }}</option>
|
||||||
<option value="Large">Large</option>
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="care_difficulty" class="block text-sm font-medium text-gray-700">Care Difficulty</label>
|
<label for="care_difficulty_id" class="block text-sm font-medium text-gray-700">Care Difficulty</label>
|
||||||
<select name="care_difficulty" id="care_difficulty" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
<select name="care_difficulty_id" id="care_difficulty_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
||||||
<option value="">Select difficulty</option>
|
<option value="">Select difficulty level</option>
|
||||||
<option value="Easy">Easy</option>
|
{% for difficulty in difficulties %}
|
||||||
<option value="Moderate">Moderate</option>
|
<option value="{{ difficulty.id }}">{{ difficulty.name }}</option>
|
||||||
<option value="Hard">Hard</option>
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="growth_rate" class="block text-sm font-medium text-gray-700">Growth Rate</label>
|
<label for="growth_rate_id" class="block text-sm font-medium text-gray-700">Growth Rate</label>
|
||||||
<select name="growth_rate" id="growth_rate" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
<select name="growth_rate_id" id="growth_rate_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
|
||||||
<option value="">Select growth rate</option>
|
<option value="">Select growth rate</option>
|
||||||
<option value="Fast">Fast</option>
|
{% for rate in growth_rates %}
|
||||||
<option value="Moderate">Moderate</option>
|
<option value="{{ rate.id }}">{{ rate.name }}</option>
|
||||||
<option value="Slow">Slow</option>
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="md:col-span-2">
|
<div class="md:col-span-2">
|
||||||
@@ -109,11 +109,15 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="md:col-span-2">
|
<div class="md:col-span-2">
|
||||||
<label for="care_guide" class="block text-sm font-medium text-gray-700">Care Guide</label>
|
<label for="care_guide" class="block text-sm font-medium text-gray-700">Care Guide</label>
|
||||||
<div id="quill-care-guide" class="bg-white rounded border border-gray-300" style="min-height: 120px;"></div>
|
<div id="quill-care-guide" class="bg-white rounded border border-gray-300 mb-8" style="min-height: 120px;"></div>
|
||||||
<textarea name="care_guide" id="care_guide" style="display:none;"></textarea>
|
<textarea name="care_guide" id="care_guide" style="display:none;"></textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="w-full btn-main text-lg font-semibold">Add Plant</button>
|
<div class="flex justify-center">
|
||||||
|
<button type="submit" class="btn-main text-lg font-semibold px-8 py-3" style="margin-top: 85px;">
|
||||||
|
Add Plant
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -166,5 +170,12 @@ var quill = new Quill('#quill-care-guide', { theme: 'snow' });
|
|||||||
document.getElementById('add-plant-form').onsubmit = function() {
|
document.getElementById('add-plant-form').onsubmit = function() {
|
||||||
document.getElementById('care_guide').value = quill.root.innerHTML;
|
document.getElementById('care_guide').value = quill.root.innerHTML;
|
||||||
};
|
};
|
||||||
|
// Auto-open the add plant form if ?add=1 is in the URL
|
||||||
|
if (window.location.search.includes('add=1')) {
|
||||||
|
document.getElementById('add-plant-form-card').classList.remove('hidden');
|
||||||
|
setTimeout(() => {
|
||||||
|
document.getElementById('add-plant-form-card').scrollIntoView({behavior: 'smooth'});
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,7 @@
|
|||||||
import io
|
import io
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -131,17 +132,21 @@ def rewrite(
|
|||||||
path: StrPath,
|
path: StrPath,
|
||||||
encoding: Optional[str],
|
encoding: Optional[str],
|
||||||
) -> Iterator[Tuple[IO[str], IO[str]]]:
|
) -> Iterator[Tuple[IO[str], IO[str]]]:
|
||||||
if not os.path.isfile(path):
|
pathlib.Path(path).touch()
|
||||||
with open(path, mode="w", encoding=encoding) as source:
|
|
||||||
source.write("")
|
|
||||||
with tempfile.NamedTemporaryFile(mode="w", encoding=encoding, delete=False) as dest:
|
with tempfile.NamedTemporaryFile(mode="w", encoding=encoding, delete=False) as dest:
|
||||||
|
error = None
|
||||||
try:
|
try:
|
||||||
with open(path, encoding=encoding) as source:
|
with open(path, encoding=encoding) as source:
|
||||||
yield (source, dest)
|
yield (source, dest)
|
||||||
except BaseException:
|
except BaseException as err:
|
||||||
os.unlink(dest.name)
|
error = err
|
||||||
raise
|
|
||||||
shutil.move(dest.name, path)
|
if error is None:
|
||||||
|
shutil.move(dest.name, path)
|
||||||
|
else:
|
||||||
|
os.unlink(dest.name)
|
||||||
|
raise error from None
|
||||||
|
|
||||||
|
|
||||||
def set_key(
|
def set_key(
|
||||||
@@ -280,7 +285,10 @@ def find_dotenv(
|
|||||||
|
|
||||||
def _is_interactive():
|
def _is_interactive():
|
||||||
""" Decide whether this is running in a REPL or IPython notebook """
|
""" Decide whether this is running in a REPL or IPython notebook """
|
||||||
main = __import__('__main__', None, None, fromlist=['__file__'])
|
try:
|
||||||
|
main = __import__('__main__', None, None, fromlist=['__file__'])
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
return False
|
||||||
return not hasattr(main, '__file__')
|
return not hasattr(main, '__file__')
|
||||||
|
|
||||||
if usecwd or _is_interactive() or getattr(sys, 'frozen', False):
|
if usecwd or _is_interactive() or getattr(sys, 'frozen', False):
|
||||||
@@ -291,7 +299,9 @@ def find_dotenv(
|
|||||||
frame = sys._getframe()
|
frame = sys._getframe()
|
||||||
current_file = __file__
|
current_file = __file__
|
||||||
|
|
||||||
while frame.f_code.co_filename == current_file:
|
while frame.f_code.co_filename == current_file or not os.path.exists(
|
||||||
|
frame.f_code.co_filename
|
||||||
|
):
|
||||||
assert frame.f_back is not None
|
assert frame.f_back is not None
|
||||||
frame = frame.f_back
|
frame = frame.f_back
|
||||||
frame_filename = frame.f_code.co_filename
|
frame_filename = frame.f_code.co_filename
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
__version__ = "1.0.0"
|
__version__ = "1.0.1"
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
pip
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
Copyright 2010 Pallets
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are
|
|
||||||
met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer in the
|
|
||||||
documentation and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
3. Neither the name of the copyright holder nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
||||||
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
||||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
||||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
||||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
Metadata-Version: 2.1
|
|
||||||
Name: Flask
|
|
||||||
Version: 2.3.3
|
|
||||||
Summary: A simple framework for building complex web applications.
|
|
||||||
Maintainer-email: Pallets <contact@palletsprojects.com>
|
|
||||||
Requires-Python: >=3.8
|
|
||||||
Description-Content-Type: text/x-rst
|
|
||||||
Classifier: Development Status :: 5 - Production/Stable
|
|
||||||
Classifier: Environment :: Web Environment
|
|
||||||
Classifier: Framework :: Flask
|
|
||||||
Classifier: Intended Audience :: Developers
|
|
||||||
Classifier: License :: OSI Approved :: BSD License
|
|
||||||
Classifier: Operating System :: OS Independent
|
|
||||||
Classifier: Programming Language :: Python
|
|
||||||
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
||||||
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI
|
|
||||||
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Application
|
|
||||||
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
||||||
Requires-Dist: Werkzeug>=2.3.7
|
|
||||||
Requires-Dist: Jinja2>=3.1.2
|
|
||||||
Requires-Dist: itsdangerous>=2.1.2
|
|
||||||
Requires-Dist: click>=8.1.3
|
|
||||||
Requires-Dist: blinker>=1.6.2
|
|
||||||
Requires-Dist: importlib-metadata>=3.6.0; python_version < '3.10'
|
|
||||||
Requires-Dist: asgiref>=3.2 ; extra == "async"
|
|
||||||
Requires-Dist: python-dotenv ; extra == "dotenv"
|
|
||||||
Project-URL: Changes, https://flask.palletsprojects.com/changes/
|
|
||||||
Project-URL: Chat, https://discord.gg/pallets
|
|
||||||
Project-URL: Documentation, https://flask.palletsprojects.com/
|
|
||||||
Project-URL: Donate, https://palletsprojects.com/donate
|
|
||||||
Project-URL: Issue Tracker, https://github.com/pallets/flask/issues/
|
|
||||||
Project-URL: Source Code, https://github.com/pallets/flask/
|
|
||||||
Provides-Extra: async
|
|
||||||
Provides-Extra: dotenv
|
|
||||||
|
|
||||||
Flask
|
|
||||||
=====
|
|
||||||
|
|
||||||
Flask is a lightweight `WSGI`_ web application framework. It is designed
|
|
||||||
to make getting started quick and easy, with the ability to scale up to
|
|
||||||
complex applications. It began as a simple wrapper around `Werkzeug`_
|
|
||||||
and `Jinja`_ and has become one of the most popular Python web
|
|
||||||
application frameworks.
|
|
||||||
|
|
||||||
Flask offers suggestions, but doesn't enforce any dependencies or
|
|
||||||
project layout. It is up to the developer to choose the tools and
|
|
||||||
libraries they want to use. There are many extensions provided by the
|
|
||||||
community that make adding new functionality easy.
|
|
||||||
|
|
||||||
.. _WSGI: https://wsgi.readthedocs.io/
|
|
||||||
.. _Werkzeug: https://werkzeug.palletsprojects.com/
|
|
||||||
.. _Jinja: https://jinja.palletsprojects.com/
|
|
||||||
|
|
||||||
|
|
||||||
Installing
|
|
||||||
----------
|
|
||||||
|
|
||||||
Install and update using `pip`_:
|
|
||||||
|
|
||||||
.. code-block:: text
|
|
||||||
|
|
||||||
$ pip install -U Flask
|
|
||||||
|
|
||||||
.. _pip: https://pip.pypa.io/en/stable/getting-started/
|
|
||||||
|
|
||||||
|
|
||||||
A Simple Example
|
|
||||||
----------------
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# save this as app.py
|
|
||||||
from flask import Flask
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def hello():
|
|
||||||
return "Hello, World!"
|
|
||||||
|
|
||||||
.. code-block:: text
|
|
||||||
|
|
||||||
$ flask run
|
|
||||||
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
|
|
||||||
|
|
||||||
|
|
||||||
Contributing
|
|
||||||
------------
|
|
||||||
|
|
||||||
For guidance on setting up a development environment and how to make a
|
|
||||||
contribution to Flask, see the `contributing guidelines`_.
|
|
||||||
|
|
||||||
.. _contributing guidelines: https://github.com/pallets/flask/blob/main/CONTRIBUTING.rst
|
|
||||||
|
|
||||||
|
|
||||||
Donate
|
|
||||||
------
|
|
||||||
|
|
||||||
The Pallets organization develops and supports Flask and the libraries
|
|
||||||
it uses. In order to grow the community of contributors and users, and
|
|
||||||
allow the maintainers to devote more time to the projects, `please
|
|
||||||
donate today`_.
|
|
||||||
|
|
||||||
.. _please donate today: https://palletsprojects.com/donate
|
|
||||||
|
|
||||||
|
|
||||||
Links
|
|
||||||
-----
|
|
||||||
|
|
||||||
- Documentation: https://flask.palletsprojects.com/
|
|
||||||
- Changes: https://flask.palletsprojects.com/changes/
|
|
||||||
- PyPI Releases: https://pypi.org/project/Flask/
|
|
||||||
- Source Code: https://github.com/pallets/flask/
|
|
||||||
- Issue Tracker: https://github.com/pallets/flask/issues/
|
|
||||||
- Chat: https://discord.gg/pallets
|
|
||||||
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
../../Scripts/flask.exe,sha256=nDhArXCv__edrUjuqzazd1Exlw2mkUQN3KYEzVfMJzs,108403
|
|
||||||
flask-2.3.3.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
|
||||||
flask-2.3.3.dist-info/LICENSE.rst,sha256=SJqOEQhQntmKN7uYPhHg9-HTHwvY-Zp5yESOf_N9B-o,1475
|
|
||||||
flask-2.3.3.dist-info/METADATA,sha256=-BtXVsnPe7lNA3mcFZHJfsVIiVin1A8LUstChm8qiHo,3588
|
|
||||||
flask-2.3.3.dist-info/RECORD,,
|
|
||||||
flask-2.3.3.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
||||||
flask-2.3.3.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
|
|
||||||
flask-2.3.3.dist-info/entry_points.txt,sha256=bBP7hTOS5fz9zLtC7sPofBZAlMkEvBxu7KqS6l5lvc4,40
|
|
||||||
flask/__init__.py,sha256=xq09XNKP-Y-fdv6BeGH7RlFaY006tUA3o_llGcl-dno,3731
|
|
||||||
flask/__main__.py,sha256=bYt9eEaoRQWdejEHFD8REx9jxVEdZptECFsV7F49Ink,30
|
|
||||||
flask/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/__main__.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/app.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/blueprints.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/cli.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/config.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/ctx.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/debughelpers.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/globals.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/helpers.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/logging.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/scaffold.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/sessions.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/signals.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/templating.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/testing.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/typing.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/views.cpython-310.pyc,,
|
|
||||||
flask/__pycache__/wrappers.cpython-310.pyc,,
|
|
||||||
flask/app.py,sha256=ht3Qx9U9z0I1qUfLoS7bYhJcubdpk-i54eHq37LDlN8,87620
|
|
||||||
flask/blueprints.py,sha256=ZpVrwa8UY-YnVDsX_1K10XQjDwCUp7Qn2hmKln5icEQ,24332
|
|
||||||
flask/cli.py,sha256=PDwZCfPagi5GUzb-D6dEN7y20gWiVAg3ejRnxBKNHPA,33821
|
|
||||||
flask/config.py,sha256=YZSZ-xpFj1iW1B1Kj1iDhpc5s7pHncloiRLqXhsU7Hs,12856
|
|
||||||
flask/ctx.py,sha256=x2kGzUXtPzVyi2YSKrU_PV1AvtxTmh2iRdriJRTSPGM,14841
|
|
||||||
flask/debughelpers.py,sha256=BR0xkd-sAyFuFW07D6NfrqNwSZxk1IrkG5n8zem-3sw,5547
|
|
||||||
flask/globals.py,sha256=KUzVvSPh8v28kUasVDi_aQKB9hI2jZSYQHqaDU2P414,2945
|
|
||||||
flask/helpers.py,sha256=uVhMwhhfwgjBt8b--zIZTjkfBRK28yPpmNhgVzhP444,25106
|
|
||||||
flask/json/__init__.py,sha256=pdtpoK2b0b1u7Sxbx3feM7VWhsI20l1yGAvbYWxaxvc,5572
|
|
||||||
flask/json/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
flask/json/__pycache__/provider.cpython-310.pyc,,
|
|
||||||
flask/json/__pycache__/tag.cpython-310.pyc,,
|
|
||||||
flask/json/provider.py,sha256=Os0frb8oGfyWKL-TDxb0Uy-MY6gDhPdJkRaUl5xAOXI,7637
|
|
||||||
flask/json/tag.py,sha256=ihb7QWrNEr0YC3KD4TolZbftgSPCuLk7FAvK49huYC0,8871
|
|
||||||
flask/logging.py,sha256=lArx2Bq9oTtUJ-DnZL9t88xU2zytzp4UWSM9Bd72NDQ,2327
|
|
||||||
flask/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
||||||
flask/scaffold.py,sha256=ALGHLcy2qSbJ7ENd1H8dOnq5VDgH5XSFsOkDelcOKV8,33217
|
|
||||||
flask/sessions.py,sha256=rFH2QKXG24dEazkKGxAHqUpAUh_30hDHrddhVYgAcY0,14169
|
|
||||||
flask/signals.py,sha256=s1H4yKjf3c5dgVr41V6sJpE9dLJvmTJMYuK0rkqx3sw,1146
|
|
||||||
flask/templating.py,sha256=XdP2hMFnZ5FCZOG7HUaLjC2VC-b4uHSWlDjwv_1p3qc,7503
|
|
||||||
flask/testing.py,sha256=h7AinggrMgGzKlDN66VfB0JjWW4Z1U_OD6FyjqBNiYM,10017
|
|
||||||
flask/typing.py,sha256=4Lj-YTxUoYvPYofC9GKu-1o0Ht8lyjp9z3I336J13_o,3005
|
|
||||||
flask/views.py,sha256=V5hOGZLx0Bn99QGcM6mh5x_uM-MypVT0-RysEFU84jc,6789
|
|
||||||
flask/wrappers.py,sha256=PhMp3teK3SnEmIdog59cO_DHiZ9Btn0qI1EifrTdwP8,5709
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
Wheel-Version: 1.0
|
|
||||||
Generator: flit 3.9.0
|
|
||||||
Root-Is-Purelib: true
|
|
||||||
Tag: py3-none-any
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
[console_scripts]
|
|
||||||
flask=flask.cli:main
|
|
||||||
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing as t
|
||||||
|
|
||||||
from . import json as json
|
from . import json as json
|
||||||
from .app import Flask as Flask
|
from .app import Flask as Flask
|
||||||
from .app import Request as Request
|
|
||||||
from .app import Response as Response
|
|
||||||
from .blueprints import Blueprint as Blueprint
|
from .blueprints import Blueprint as Blueprint
|
||||||
from .config import Config as Config
|
from .config import Config as Config
|
||||||
from .ctx import after_this_request as after_this_request
|
from .ctx import after_this_request as after_this_request
|
||||||
@@ -37,66 +39,22 @@ from .templating import render_template as render_template
|
|||||||
from .templating import render_template_string as render_template_string
|
from .templating import render_template_string as render_template_string
|
||||||
from .templating import stream_template as stream_template
|
from .templating import stream_template as stream_template
|
||||||
from .templating import stream_template_string as stream_template_string
|
from .templating import stream_template_string as stream_template_string
|
||||||
|
from .wrappers import Request as Request
|
||||||
__version__ = "2.3.3"
|
from .wrappers import Response as Response
|
||||||
|
|
||||||
|
|
||||||
def __getattr__(name):
|
def __getattr__(name: str) -> t.Any:
|
||||||
if name == "_app_ctx_stack":
|
if name == "__version__":
|
||||||
import warnings
|
import importlib.metadata
|
||||||
from .globals import __app_ctx_stack
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
"'_app_ctx_stack' is deprecated and will be removed in Flask 2.4.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return __app_ctx_stack
|
|
||||||
|
|
||||||
if name == "_request_ctx_stack":
|
|
||||||
import warnings
|
|
||||||
from .globals import __request_ctx_stack
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
"'_request_ctx_stack' is deprecated and will be removed in Flask 2.4.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return __request_ctx_stack
|
|
||||||
|
|
||||||
if name == "escape":
|
|
||||||
import warnings
|
|
||||||
from markupsafe import escape
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
"'flask.escape' is deprecated and will be removed in Flask 2.4. Import"
|
|
||||||
" 'markupsafe.escape' instead.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return escape
|
|
||||||
|
|
||||||
if name == "Markup":
|
|
||||||
import warnings
|
|
||||||
from markupsafe import Markup
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
"'flask.Markup' is deprecated and will be removed in Flask 2.4. Import"
|
|
||||||
" 'markupsafe.Markup' instead.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return Markup
|
|
||||||
|
|
||||||
if name == "signals_available":
|
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
"'signals_available' is deprecated and will be removed in Flask 2.4."
|
"The '__version__' attribute is deprecated and will be removed in"
|
||||||
" Signals are always available",
|
" Flask 3.1. Use feature detection or"
|
||||||
|
" 'importlib.metadata.version(\"flask\")' instead.",
|
||||||
DeprecationWarning,
|
DeprecationWarning,
|
||||||
stacklevel=2,
|
stacklevel=2,
|
||||||
)
|
)
|
||||||
return True
|
return importlib.metadata.version("flask")
|
||||||
|
|
||||||
raise AttributeError(name)
|
raise AttributeError(name)
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -2,625 +2,90 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import typing as t
|
import typing as t
|
||||||
from collections import defaultdict
|
from datetime import timedelta
|
||||||
from functools import update_wrapper
|
|
||||||
|
|
||||||
from . import typing as ft
|
from .globals import current_app
|
||||||
from .scaffold import _endpoint_from_view_func
|
from .helpers import send_from_directory
|
||||||
from .scaffold import _sentinel
|
from .sansio.blueprints import Blueprint as SansioBlueprint
|
||||||
from .scaffold import Scaffold
|
from .sansio.blueprints import BlueprintSetupState as BlueprintSetupState # noqa
|
||||||
from .scaffold import setupmethod
|
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
from .app import Flask
|
from .wrappers import Response
|
||||||
|
|
||||||
DeferredSetupFunction = t.Callable[["BlueprintSetupState"], t.Callable]
|
|
||||||
T_after_request = t.TypeVar("T_after_request", bound=ft.AfterRequestCallable)
|
|
||||||
T_before_request = t.TypeVar("T_before_request", bound=ft.BeforeRequestCallable)
|
|
||||||
T_error_handler = t.TypeVar("T_error_handler", bound=ft.ErrorHandlerCallable)
|
|
||||||
T_teardown = t.TypeVar("T_teardown", bound=ft.TeardownCallable)
|
|
||||||
T_template_context_processor = t.TypeVar(
|
|
||||||
"T_template_context_processor", bound=ft.TemplateContextProcessorCallable
|
|
||||||
)
|
|
||||||
T_template_filter = t.TypeVar("T_template_filter", bound=ft.TemplateFilterCallable)
|
|
||||||
T_template_global = t.TypeVar("T_template_global", bound=ft.TemplateGlobalCallable)
|
|
||||||
T_template_test = t.TypeVar("T_template_test", bound=ft.TemplateTestCallable)
|
|
||||||
T_url_defaults = t.TypeVar("T_url_defaults", bound=ft.URLDefaultCallable)
|
|
||||||
T_url_value_preprocessor = t.TypeVar(
|
|
||||||
"T_url_value_preprocessor", bound=ft.URLValuePreprocessorCallable
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class BlueprintSetupState:
|
class Blueprint(SansioBlueprint):
|
||||||
"""Temporary holder object for registering a blueprint with the
|
def get_send_file_max_age(self, filename: str | None) -> int | None:
|
||||||
application. An instance of this class is created by the
|
"""Used by :func:`send_file` to determine the ``max_age`` cache
|
||||||
:meth:`~flask.Blueprint.make_setup_state` method and later passed
|
value for a given file path if it wasn't passed.
|
||||||
to all register callback functions.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
By default, this returns :data:`SEND_FILE_MAX_AGE_DEFAULT` from
|
||||||
self,
|
the configuration of :data:`~flask.current_app`. This defaults
|
||||||
blueprint: Blueprint,
|
to ``None``, which tells the browser to use conditional requests
|
||||||
app: Flask,
|
instead of a timed cache, which is usually preferable.
|
||||||
options: t.Any,
|
|
||||||
first_registration: bool,
|
|
||||||
) -> None:
|
|
||||||
#: a reference to the current application
|
|
||||||
self.app = app
|
|
||||||
|
|
||||||
#: a reference to the blueprint that created this setup state.
|
Note this is a duplicate of the same method in the Flask
|
||||||
self.blueprint = blueprint
|
class.
|
||||||
|
|
||||||
#: a dictionary with all options that were passed to the
|
.. versionchanged:: 2.0
|
||||||
#: :meth:`~flask.Flask.register_blueprint` method.
|
The default configuration is ``None`` instead of 12 hours.
|
||||||
self.options = options
|
|
||||||
|
|
||||||
#: as blueprints can be registered multiple times with the
|
.. versionadded:: 0.9
|
||||||
#: application and not everything wants to be registered
|
|
||||||
#: multiple times on it, this attribute can be used to figure
|
|
||||||
#: out if the blueprint was registered in the past already.
|
|
||||||
self.first_registration = first_registration
|
|
||||||
|
|
||||||
subdomain = self.options.get("subdomain")
|
|
||||||
if subdomain is None:
|
|
||||||
subdomain = self.blueprint.subdomain
|
|
||||||
|
|
||||||
#: The subdomain that the blueprint should be active for, ``None``
|
|
||||||
#: otherwise.
|
|
||||||
self.subdomain = subdomain
|
|
||||||
|
|
||||||
url_prefix = self.options.get("url_prefix")
|
|
||||||
if url_prefix is None:
|
|
||||||
url_prefix = self.blueprint.url_prefix
|
|
||||||
#: The prefix that should be used for all URLs defined on the
|
|
||||||
#: blueprint.
|
|
||||||
self.url_prefix = url_prefix
|
|
||||||
|
|
||||||
self.name = self.options.get("name", blueprint.name)
|
|
||||||
self.name_prefix = self.options.get("name_prefix", "")
|
|
||||||
|
|
||||||
#: A dictionary with URL defaults that is added to each and every
|
|
||||||
#: URL that was defined with the blueprint.
|
|
||||||
self.url_defaults = dict(self.blueprint.url_values_defaults)
|
|
||||||
self.url_defaults.update(self.options.get("url_defaults", ()))
|
|
||||||
|
|
||||||
def add_url_rule(
|
|
||||||
self,
|
|
||||||
rule: str,
|
|
||||||
endpoint: str | None = None,
|
|
||||||
view_func: t.Callable | None = None,
|
|
||||||
**options: t.Any,
|
|
||||||
) -> None:
|
|
||||||
"""A helper method to register a rule (and optionally a view function)
|
|
||||||
to the application. The endpoint is automatically prefixed with the
|
|
||||||
blueprint's name.
|
|
||||||
"""
|
"""
|
||||||
if self.url_prefix is not None:
|
value = current_app.config["SEND_FILE_MAX_AGE_DEFAULT"]
|
||||||
if rule:
|
|
||||||
rule = "/".join((self.url_prefix.rstrip("/"), rule.lstrip("/")))
|
|
||||||
else:
|
|
||||||
rule = self.url_prefix
|
|
||||||
options.setdefault("subdomain", self.subdomain)
|
|
||||||
if endpoint is None:
|
|
||||||
endpoint = _endpoint_from_view_func(view_func) # type: ignore
|
|
||||||
defaults = self.url_defaults
|
|
||||||
if "defaults" in options:
|
|
||||||
defaults = dict(defaults, **options.pop("defaults"))
|
|
||||||
|
|
||||||
self.app.add_url_rule(
|
if value is None:
|
||||||
rule,
|
return None
|
||||||
f"{self.name_prefix}.{self.name}.{endpoint}".lstrip("."),
|
|
||||||
view_func,
|
if isinstance(value, timedelta):
|
||||||
defaults=defaults,
|
return int(value.total_seconds())
|
||||||
**options,
|
|
||||||
|
return value # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
def send_static_file(self, filename: str) -> Response:
|
||||||
|
"""The view function used to serve files from
|
||||||
|
:attr:`static_folder`. A route is automatically registered for
|
||||||
|
this view at :attr:`static_url_path` if :attr:`static_folder` is
|
||||||
|
set.
|
||||||
|
|
||||||
|
Note this is a duplicate of the same method in the Flask
|
||||||
|
class.
|
||||||
|
|
||||||
|
.. versionadded:: 0.5
|
||||||
|
|
||||||
|
"""
|
||||||
|
if not self.has_static_folder:
|
||||||
|
raise RuntimeError("'static_folder' must be set to serve static_files.")
|
||||||
|
|
||||||
|
# send_file only knows to call get_send_file_max_age on the app,
|
||||||
|
# call it here so it works for blueprints too.
|
||||||
|
max_age = self.get_send_file_max_age(filename)
|
||||||
|
return send_from_directory(
|
||||||
|
t.cast(str, self.static_folder), filename, max_age=max_age
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def open_resource(self, resource: str, mode: str = "rb") -> t.IO[t.AnyStr]:
|
||||||
|
"""Open a resource file relative to :attr:`root_path` for
|
||||||
|
reading.
|
||||||
|
|
||||||
class Blueprint(Scaffold):
|
For example, if the file ``schema.sql`` is next to the file
|
||||||
"""Represents a blueprint, a collection of routes and other
|
``app.py`` where the ``Flask`` app is defined, it can be opened
|
||||||
app-related functions that can be registered on a real application
|
with:
|
||||||
later.
|
|
||||||
|
|
||||||
A blueprint is an object that allows defining application functions
|
.. code-block:: python
|
||||||
without requiring an application object ahead of time. It uses the
|
|
||||||
same decorators as :class:`~flask.Flask`, but defers the need for an
|
|
||||||
application by recording them for later registration.
|
|
||||||
|
|
||||||
Decorating a function with a blueprint creates a deferred function
|
with app.open_resource("schema.sql") as f:
|
||||||
that is called with :class:`~flask.blueprints.BlueprintSetupState`
|
conn.executescript(f.read())
|
||||||
when the blueprint is registered on an application.
|
|
||||||
|
|
||||||
See :doc:`/blueprints` for more information.
|
:param resource: Path to the resource relative to
|
||||||
|
:attr:`root_path`.
|
||||||
|
:param mode: Open the file in this mode. Only reading is
|
||||||
|
supported, valid values are "r" (or "rt") and "rb".
|
||||||
|
|
||||||
:param name: The name of the blueprint. Will be prepended to each
|
Note this is a duplicate of the same method in the Flask
|
||||||
endpoint name.
|
class.
|
||||||
:param import_name: The name of the blueprint package, usually
|
|
||||||
``__name__``. This helps locate the ``root_path`` for the
|
|
||||||
blueprint.
|
|
||||||
:param static_folder: A folder with static files that should be
|
|
||||||
served by the blueprint's static route. The path is relative to
|
|
||||||
the blueprint's root path. Blueprint static files are disabled
|
|
||||||
by default.
|
|
||||||
:param static_url_path: The url to serve static files from.
|
|
||||||
Defaults to ``static_folder``. If the blueprint does not have
|
|
||||||
a ``url_prefix``, the app's static route will take precedence,
|
|
||||||
and the blueprint's static files won't be accessible.
|
|
||||||
:param template_folder: A folder with templates that should be added
|
|
||||||
to the app's template search path. The path is relative to the
|
|
||||||
blueprint's root path. Blueprint templates are disabled by
|
|
||||||
default. Blueprint templates have a lower precedence than those
|
|
||||||
in the app's templates folder.
|
|
||||||
:param url_prefix: A path to prepend to all of the blueprint's URLs,
|
|
||||||
to make them distinct from the rest of the app's routes.
|
|
||||||
:param subdomain: A subdomain that blueprint routes will match on by
|
|
||||||
default.
|
|
||||||
:param url_defaults: A dict of default values that blueprint routes
|
|
||||||
will receive by default.
|
|
||||||
:param root_path: By default, the blueprint will automatically set
|
|
||||||
this based on ``import_name``. In certain situations this
|
|
||||||
automatic detection can fail, so the path can be specified
|
|
||||||
manually instead.
|
|
||||||
|
|
||||||
.. versionchanged:: 1.1.0
|
|
||||||
Blueprints have a ``cli`` group to register nested CLI commands.
|
|
||||||
The ``cli_group`` parameter controls the name of the group under
|
|
||||||
the ``flask`` command.
|
|
||||||
|
|
||||||
.. versionadded:: 0.7
|
|
||||||
"""
|
|
||||||
|
|
||||||
_got_registered_once = False
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
name: str,
|
|
||||||
import_name: str,
|
|
||||||
static_folder: str | os.PathLike | None = None,
|
|
||||||
static_url_path: str | None = None,
|
|
||||||
template_folder: str | os.PathLike | None = None,
|
|
||||||
url_prefix: str | None = None,
|
|
||||||
subdomain: str | None = None,
|
|
||||||
url_defaults: dict | None = None,
|
|
||||||
root_path: str | None = None,
|
|
||||||
cli_group: str | None = _sentinel, # type: ignore
|
|
||||||
):
|
|
||||||
super().__init__(
|
|
||||||
import_name=import_name,
|
|
||||||
static_folder=static_folder,
|
|
||||||
static_url_path=static_url_path,
|
|
||||||
template_folder=template_folder,
|
|
||||||
root_path=root_path,
|
|
||||||
)
|
|
||||||
|
|
||||||
if not name:
|
|
||||||
raise ValueError("'name' may not be empty.")
|
|
||||||
|
|
||||||
if "." in name:
|
|
||||||
raise ValueError("'name' may not contain a dot '.' character.")
|
|
||||||
|
|
||||||
self.name = name
|
|
||||||
self.url_prefix = url_prefix
|
|
||||||
self.subdomain = subdomain
|
|
||||||
self.deferred_functions: list[DeferredSetupFunction] = []
|
|
||||||
|
|
||||||
if url_defaults is None:
|
|
||||||
url_defaults = {}
|
|
||||||
|
|
||||||
self.url_values_defaults = url_defaults
|
|
||||||
self.cli_group = cli_group
|
|
||||||
self._blueprints: list[tuple[Blueprint, dict]] = []
|
|
||||||
|
|
||||||
def _check_setup_finished(self, f_name: str) -> None:
|
|
||||||
if self._got_registered_once:
|
|
||||||
raise AssertionError(
|
|
||||||
f"The setup method '{f_name}' can no longer be called on the blueprint"
|
|
||||||
f" '{self.name}'. It has already been registered at least once, any"
|
|
||||||
" changes will not be applied consistently.\n"
|
|
||||||
"Make sure all imports, decorators, functions, etc. needed to set up"
|
|
||||||
" the blueprint are done before registering it."
|
|
||||||
)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def record(self, func: t.Callable) -> None:
|
|
||||||
"""Registers a function that is called when the blueprint is
|
|
||||||
registered on the application. This function is called with the
|
|
||||||
state as argument as returned by the :meth:`make_setup_state`
|
|
||||||
method.
|
|
||||||
"""
|
"""
|
||||||
self.deferred_functions.append(func)
|
if mode not in {"r", "rt", "rb"}:
|
||||||
|
raise ValueError("Resources can only be opened for reading.")
|
||||||
|
|
||||||
@setupmethod
|
return open(os.path.join(self.root_path, resource), mode)
|
||||||
def record_once(self, func: t.Callable) -> None:
|
|
||||||
"""Works like :meth:`record` but wraps the function in another
|
|
||||||
function that will ensure the function is only called once. If the
|
|
||||||
blueprint is registered a second time on the application, the
|
|
||||||
function passed is not called.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def wrapper(state: BlueprintSetupState) -> None:
|
|
||||||
if state.first_registration:
|
|
||||||
func(state)
|
|
||||||
|
|
||||||
self.record(update_wrapper(wrapper, func))
|
|
||||||
|
|
||||||
def make_setup_state(
|
|
||||||
self, app: Flask, options: dict, first_registration: bool = False
|
|
||||||
) -> BlueprintSetupState:
|
|
||||||
"""Creates an instance of :meth:`~flask.blueprints.BlueprintSetupState`
|
|
||||||
object that is later passed to the register callback functions.
|
|
||||||
Subclasses can override this to return a subclass of the setup state.
|
|
||||||
"""
|
|
||||||
return BlueprintSetupState(self, app, options, first_registration)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def register_blueprint(self, blueprint: Blueprint, **options: t.Any) -> None:
|
|
||||||
"""Register a :class:`~flask.Blueprint` on this blueprint. Keyword
|
|
||||||
arguments passed to this method will override the defaults set
|
|
||||||
on the blueprint.
|
|
||||||
|
|
||||||
.. versionchanged:: 2.0.1
|
|
||||||
The ``name`` option can be used to change the (pre-dotted)
|
|
||||||
name the blueprint is registered with. This allows the same
|
|
||||||
blueprint to be registered multiple times with unique names
|
|
||||||
for ``url_for``.
|
|
||||||
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
"""
|
|
||||||
if blueprint is self:
|
|
||||||
raise ValueError("Cannot register a blueprint on itself")
|
|
||||||
self._blueprints.append((blueprint, options))
|
|
||||||
|
|
||||||
def register(self, app: Flask, options: dict) -> None:
|
|
||||||
"""Called by :meth:`Flask.register_blueprint` to register all
|
|
||||||
views and callbacks registered on the blueprint with the
|
|
||||||
application. Creates a :class:`.BlueprintSetupState` and calls
|
|
||||||
each :meth:`record` callback with it.
|
|
||||||
|
|
||||||
:param app: The application this blueprint is being registered
|
|
||||||
with.
|
|
||||||
:param options: Keyword arguments forwarded from
|
|
||||||
:meth:`~Flask.register_blueprint`.
|
|
||||||
|
|
||||||
.. versionchanged:: 2.3
|
|
||||||
Nested blueprints now correctly apply subdomains.
|
|
||||||
|
|
||||||
.. versionchanged:: 2.1
|
|
||||||
Registering the same blueprint with the same name multiple
|
|
||||||
times is an error.
|
|
||||||
|
|
||||||
.. versionchanged:: 2.0.1
|
|
||||||
Nested blueprints are registered with their dotted name.
|
|
||||||
This allows different blueprints with the same name to be
|
|
||||||
nested at different locations.
|
|
||||||
|
|
||||||
.. versionchanged:: 2.0.1
|
|
||||||
The ``name`` option can be used to change the (pre-dotted)
|
|
||||||
name the blueprint is registered with. This allows the same
|
|
||||||
blueprint to be registered multiple times with unique names
|
|
||||||
for ``url_for``.
|
|
||||||
"""
|
|
||||||
name_prefix = options.get("name_prefix", "")
|
|
||||||
self_name = options.get("name", self.name)
|
|
||||||
name = f"{name_prefix}.{self_name}".lstrip(".")
|
|
||||||
|
|
||||||
if name in app.blueprints:
|
|
||||||
bp_desc = "this" if app.blueprints[name] is self else "a different"
|
|
||||||
existing_at = f" '{name}'" if self_name != name else ""
|
|
||||||
|
|
||||||
raise ValueError(
|
|
||||||
f"The name '{self_name}' is already registered for"
|
|
||||||
f" {bp_desc} blueprint{existing_at}. Use 'name=' to"
|
|
||||||
f" provide a unique name."
|
|
||||||
)
|
|
||||||
|
|
||||||
first_bp_registration = not any(bp is self for bp in app.blueprints.values())
|
|
||||||
first_name_registration = name not in app.blueprints
|
|
||||||
|
|
||||||
app.blueprints[name] = self
|
|
||||||
self._got_registered_once = True
|
|
||||||
state = self.make_setup_state(app, options, first_bp_registration)
|
|
||||||
|
|
||||||
if self.has_static_folder:
|
|
||||||
state.add_url_rule(
|
|
||||||
f"{self.static_url_path}/<path:filename>",
|
|
||||||
view_func=self.send_static_file,
|
|
||||||
endpoint="static",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Merge blueprint data into parent.
|
|
||||||
if first_bp_registration or first_name_registration:
|
|
||||||
|
|
||||||
def extend(bp_dict, parent_dict):
|
|
||||||
for key, values in bp_dict.items():
|
|
||||||
key = name if key is None else f"{name}.{key}"
|
|
||||||
parent_dict[key].extend(values)
|
|
||||||
|
|
||||||
for key, value in self.error_handler_spec.items():
|
|
||||||
key = name if key is None else f"{name}.{key}"
|
|
||||||
value = defaultdict(
|
|
||||||
dict,
|
|
||||||
{
|
|
||||||
code: {
|
|
||||||
exc_class: func for exc_class, func in code_values.items()
|
|
||||||
}
|
|
||||||
for code, code_values in value.items()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
app.error_handler_spec[key] = value
|
|
||||||
|
|
||||||
for endpoint, func in self.view_functions.items():
|
|
||||||
app.view_functions[endpoint] = func
|
|
||||||
|
|
||||||
extend(self.before_request_funcs, app.before_request_funcs)
|
|
||||||
extend(self.after_request_funcs, app.after_request_funcs)
|
|
||||||
extend(
|
|
||||||
self.teardown_request_funcs,
|
|
||||||
app.teardown_request_funcs,
|
|
||||||
)
|
|
||||||
extend(self.url_default_functions, app.url_default_functions)
|
|
||||||
extend(self.url_value_preprocessors, app.url_value_preprocessors)
|
|
||||||
extend(self.template_context_processors, app.template_context_processors)
|
|
||||||
|
|
||||||
for deferred in self.deferred_functions:
|
|
||||||
deferred(state)
|
|
||||||
|
|
||||||
cli_resolved_group = options.get("cli_group", self.cli_group)
|
|
||||||
|
|
||||||
if self.cli.commands:
|
|
||||||
if cli_resolved_group is None:
|
|
||||||
app.cli.commands.update(self.cli.commands)
|
|
||||||
elif cli_resolved_group is _sentinel:
|
|
||||||
self.cli.name = name
|
|
||||||
app.cli.add_command(self.cli)
|
|
||||||
else:
|
|
||||||
self.cli.name = cli_resolved_group
|
|
||||||
app.cli.add_command(self.cli)
|
|
||||||
|
|
||||||
for blueprint, bp_options in self._blueprints:
|
|
||||||
bp_options = bp_options.copy()
|
|
||||||
bp_url_prefix = bp_options.get("url_prefix")
|
|
||||||
bp_subdomain = bp_options.get("subdomain")
|
|
||||||
|
|
||||||
if bp_subdomain is None:
|
|
||||||
bp_subdomain = blueprint.subdomain
|
|
||||||
|
|
||||||
if state.subdomain is not None and bp_subdomain is not None:
|
|
||||||
bp_options["subdomain"] = bp_subdomain + "." + state.subdomain
|
|
||||||
elif bp_subdomain is not None:
|
|
||||||
bp_options["subdomain"] = bp_subdomain
|
|
||||||
elif state.subdomain is not None:
|
|
||||||
bp_options["subdomain"] = state.subdomain
|
|
||||||
|
|
||||||
if bp_url_prefix is None:
|
|
||||||
bp_url_prefix = blueprint.url_prefix
|
|
||||||
|
|
||||||
if state.url_prefix is not None and bp_url_prefix is not None:
|
|
||||||
bp_options["url_prefix"] = (
|
|
||||||
state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/")
|
|
||||||
)
|
|
||||||
elif bp_url_prefix is not None:
|
|
||||||
bp_options["url_prefix"] = bp_url_prefix
|
|
||||||
elif state.url_prefix is not None:
|
|
||||||
bp_options["url_prefix"] = state.url_prefix
|
|
||||||
|
|
||||||
bp_options["name_prefix"] = name
|
|
||||||
blueprint.register(app, bp_options)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def add_url_rule(
|
|
||||||
self,
|
|
||||||
rule: str,
|
|
||||||
endpoint: str | None = None,
|
|
||||||
view_func: ft.RouteCallable | None = None,
|
|
||||||
provide_automatic_options: bool | None = None,
|
|
||||||
**options: t.Any,
|
|
||||||
) -> None:
|
|
||||||
"""Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for
|
|
||||||
full documentation.
|
|
||||||
|
|
||||||
The URL rule is prefixed with the blueprint's URL prefix. The endpoint name,
|
|
||||||
used with :func:`url_for`, is prefixed with the blueprint's name.
|
|
||||||
"""
|
|
||||||
if endpoint and "." in endpoint:
|
|
||||||
raise ValueError("'endpoint' may not contain a dot '.' character.")
|
|
||||||
|
|
||||||
if view_func and hasattr(view_func, "__name__") and "." in view_func.__name__:
|
|
||||||
raise ValueError("'view_func' name may not contain a dot '.' character.")
|
|
||||||
|
|
||||||
self.record(
|
|
||||||
lambda s: s.add_url_rule(
|
|
||||||
rule,
|
|
||||||
endpoint,
|
|
||||||
view_func,
|
|
||||||
provide_automatic_options=provide_automatic_options,
|
|
||||||
**options,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def app_template_filter(
|
|
||||||
self, name: str | None = None
|
|
||||||
) -> t.Callable[[T_template_filter], T_template_filter]:
|
|
||||||
"""Register a template filter, available in any template rendered by the
|
|
||||||
application. Equivalent to :meth:`.Flask.template_filter`.
|
|
||||||
|
|
||||||
:param name: the optional name of the filter, otherwise the
|
|
||||||
function name will be used.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(f: T_template_filter) -> T_template_filter:
|
|
||||||
self.add_app_template_filter(f, name=name)
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def add_app_template_filter(
|
|
||||||
self, f: ft.TemplateFilterCallable, name: str | None = None
|
|
||||||
) -> None:
|
|
||||||
"""Register a template filter, available in any template rendered by the
|
|
||||||
application. Works like the :meth:`app_template_filter` decorator. Equivalent to
|
|
||||||
:meth:`.Flask.add_template_filter`.
|
|
||||||
|
|
||||||
:param name: the optional name of the filter, otherwise the
|
|
||||||
function name will be used.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def register_template(state: BlueprintSetupState) -> None:
|
|
||||||
state.app.jinja_env.filters[name or f.__name__] = f
|
|
||||||
|
|
||||||
self.record_once(register_template)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def app_template_test(
|
|
||||||
self, name: str | None = None
|
|
||||||
) -> t.Callable[[T_template_test], T_template_test]:
|
|
||||||
"""Register a template test, available in any template rendered by the
|
|
||||||
application. Equivalent to :meth:`.Flask.template_test`.
|
|
||||||
|
|
||||||
.. versionadded:: 0.10
|
|
||||||
|
|
||||||
:param name: the optional name of the test, otherwise the
|
|
||||||
function name will be used.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(f: T_template_test) -> T_template_test:
|
|
||||||
self.add_app_template_test(f, name=name)
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def add_app_template_test(
|
|
||||||
self, f: ft.TemplateTestCallable, name: str | None = None
|
|
||||||
) -> None:
|
|
||||||
"""Register a template test, available in any template rendered by the
|
|
||||||
application. Works like the :meth:`app_template_test` decorator. Equivalent to
|
|
||||||
:meth:`.Flask.add_template_test`.
|
|
||||||
|
|
||||||
.. versionadded:: 0.10
|
|
||||||
|
|
||||||
:param name: the optional name of the test, otherwise the
|
|
||||||
function name will be used.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def register_template(state: BlueprintSetupState) -> None:
|
|
||||||
state.app.jinja_env.tests[name or f.__name__] = f
|
|
||||||
|
|
||||||
self.record_once(register_template)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def app_template_global(
|
|
||||||
self, name: str | None = None
|
|
||||||
) -> t.Callable[[T_template_global], T_template_global]:
|
|
||||||
"""Register a template global, available in any template rendered by the
|
|
||||||
application. Equivalent to :meth:`.Flask.template_global`.
|
|
||||||
|
|
||||||
.. versionadded:: 0.10
|
|
||||||
|
|
||||||
:param name: the optional name of the global, otherwise the
|
|
||||||
function name will be used.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(f: T_template_global) -> T_template_global:
|
|
||||||
self.add_app_template_global(f, name=name)
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def add_app_template_global(
|
|
||||||
self, f: ft.TemplateGlobalCallable, name: str | None = None
|
|
||||||
) -> None:
|
|
||||||
"""Register a template global, available in any template rendered by the
|
|
||||||
application. Works like the :meth:`app_template_global` decorator. Equivalent to
|
|
||||||
:meth:`.Flask.add_template_global`.
|
|
||||||
|
|
||||||
.. versionadded:: 0.10
|
|
||||||
|
|
||||||
:param name: the optional name of the global, otherwise the
|
|
||||||
function name will be used.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def register_template(state: BlueprintSetupState) -> None:
|
|
||||||
state.app.jinja_env.globals[name or f.__name__] = f
|
|
||||||
|
|
||||||
self.record_once(register_template)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def before_app_request(self, f: T_before_request) -> T_before_request:
|
|
||||||
"""Like :meth:`before_request`, but before every request, not only those handled
|
|
||||||
by the blueprint. Equivalent to :meth:`.Flask.before_request`.
|
|
||||||
"""
|
|
||||||
self.record_once(
|
|
||||||
lambda s: s.app.before_request_funcs.setdefault(None, []).append(f)
|
|
||||||
)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def after_app_request(self, f: T_after_request) -> T_after_request:
|
|
||||||
"""Like :meth:`after_request`, but after every request, not only those handled
|
|
||||||
by the blueprint. Equivalent to :meth:`.Flask.after_request`.
|
|
||||||
"""
|
|
||||||
self.record_once(
|
|
||||||
lambda s: s.app.after_request_funcs.setdefault(None, []).append(f)
|
|
||||||
)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def teardown_app_request(self, f: T_teardown) -> T_teardown:
|
|
||||||
"""Like :meth:`teardown_request`, but after every request, not only those
|
|
||||||
handled by the blueprint. Equivalent to :meth:`.Flask.teardown_request`.
|
|
||||||
"""
|
|
||||||
self.record_once(
|
|
||||||
lambda s: s.app.teardown_request_funcs.setdefault(None, []).append(f)
|
|
||||||
)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def app_context_processor(
|
|
||||||
self, f: T_template_context_processor
|
|
||||||
) -> T_template_context_processor:
|
|
||||||
"""Like :meth:`context_processor`, but for templates rendered by every view, not
|
|
||||||
only by the blueprint. Equivalent to :meth:`.Flask.context_processor`.
|
|
||||||
"""
|
|
||||||
self.record_once(
|
|
||||||
lambda s: s.app.template_context_processors.setdefault(None, []).append(f)
|
|
||||||
)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def app_errorhandler(
|
|
||||||
self, code: type[Exception] | int
|
|
||||||
) -> t.Callable[[T_error_handler], T_error_handler]:
|
|
||||||
"""Like :meth:`errorhandler`, but for every request, not only those handled by
|
|
||||||
the blueprint. Equivalent to :meth:`.Flask.errorhandler`.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(f: T_error_handler) -> T_error_handler:
|
|
||||||
self.record_once(lambda s: s.app.errorhandler(code)(f))
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def app_url_value_preprocessor(
|
|
||||||
self, f: T_url_value_preprocessor
|
|
||||||
) -> T_url_value_preprocessor:
|
|
||||||
"""Like :meth:`url_value_preprocessor`, but for every request, not only those
|
|
||||||
handled by the blueprint. Equivalent to :meth:`.Flask.url_value_preprocessor`.
|
|
||||||
"""
|
|
||||||
self.record_once(
|
|
||||||
lambda s: s.app.url_value_preprocessors.setdefault(None, []).append(f)
|
|
||||||
)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def app_url_defaults(self, f: T_url_defaults) -> T_url_defaults:
|
|
||||||
"""Like :meth:`url_defaults`, but for every request, not only those handled by
|
|
||||||
the blueprint. Equivalent to :meth:`.Flask.url_defaults`.
|
|
||||||
"""
|
|
||||||
self.record_once(
|
|
||||||
lambda s: s.app.url_default_functions.setdefault(None, []).append(f)
|
|
||||||
)
|
|
||||||
return f
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
|
import collections.abc as cabc
|
||||||
import importlib.metadata
|
import importlib.metadata
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
@@ -11,6 +12,7 @@ import traceback
|
|||||||
import typing as t
|
import typing as t
|
||||||
from functools import update_wrapper
|
from functools import update_wrapper
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
|
from types import ModuleType
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from click.core import ParameterSource
|
from click.core import ParameterSource
|
||||||
@@ -23,6 +25,12 @@ from .helpers import get_debug_flag
|
|||||||
from .helpers import get_load_dotenv
|
from .helpers import get_load_dotenv
|
||||||
|
|
||||||
if t.TYPE_CHECKING:
|
if t.TYPE_CHECKING:
|
||||||
|
import ssl
|
||||||
|
|
||||||
|
from _typeshed.wsgi import StartResponse
|
||||||
|
from _typeshed.wsgi import WSGIApplication
|
||||||
|
from _typeshed.wsgi import WSGIEnvironment
|
||||||
|
|
||||||
from .app import Flask
|
from .app import Flask
|
||||||
|
|
||||||
|
|
||||||
@@ -30,7 +38,7 @@ class NoAppException(click.UsageError):
|
|||||||
"""Raised if an application cannot be found or loaded."""
|
"""Raised if an application cannot be found or loaded."""
|
||||||
|
|
||||||
|
|
||||||
def find_best_app(module):
|
def find_best_app(module: ModuleType) -> Flask:
|
||||||
"""Given a module instance this tries to find the best possible
|
"""Given a module instance this tries to find the best possible
|
||||||
application in the module or raises an exception.
|
application in the module or raises an exception.
|
||||||
"""
|
"""
|
||||||
@@ -83,7 +91,7 @@ def find_best_app(module):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _called_with_wrong_args(f):
|
def _called_with_wrong_args(f: t.Callable[..., Flask]) -> bool:
|
||||||
"""Check whether calling a function raised a ``TypeError`` because
|
"""Check whether calling a function raised a ``TypeError`` because
|
||||||
the call failed or because something in the factory raised the
|
the call failed or because something in the factory raised the
|
||||||
error.
|
error.
|
||||||
@@ -109,7 +117,7 @@ def _called_with_wrong_args(f):
|
|||||||
del tb
|
del tb
|
||||||
|
|
||||||
|
|
||||||
def find_app_by_string(module, app_name):
|
def find_app_by_string(module: ModuleType, app_name: str) -> Flask:
|
||||||
"""Check if the given string is a variable name or a function. Call
|
"""Check if the given string is a variable name or a function. Call
|
||||||
a function to get the app instance, or return the variable directly.
|
a function to get the app instance, or return the variable directly.
|
||||||
"""
|
"""
|
||||||
@@ -140,7 +148,11 @@ def find_app_by_string(module, app_name):
|
|||||||
# Parse the positional and keyword arguments as literals.
|
# Parse the positional and keyword arguments as literals.
|
||||||
try:
|
try:
|
||||||
args = [ast.literal_eval(arg) for arg in expr.args]
|
args = [ast.literal_eval(arg) for arg in expr.args]
|
||||||
kwargs = {kw.arg: ast.literal_eval(kw.value) for kw in expr.keywords}
|
kwargs = {
|
||||||
|
kw.arg: ast.literal_eval(kw.value)
|
||||||
|
for kw in expr.keywords
|
||||||
|
if kw.arg is not None
|
||||||
|
}
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# literal_eval gives cryptic error messages, show a generic
|
# literal_eval gives cryptic error messages, show a generic
|
||||||
# message with the full expression instead.
|
# message with the full expression instead.
|
||||||
@@ -185,7 +197,7 @@ def find_app_by_string(module, app_name):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def prepare_import(path):
|
def prepare_import(path: str) -> str:
|
||||||
"""Given a filename this will try to calculate the python path, add it
|
"""Given a filename this will try to calculate the python path, add it
|
||||||
to the search path and return the actual module name that is expected.
|
to the search path and return the actual module name that is expected.
|
||||||
"""
|
"""
|
||||||
@@ -214,13 +226,29 @@ def prepare_import(path):
|
|||||||
return ".".join(module_name[::-1])
|
return ".".join(module_name[::-1])
|
||||||
|
|
||||||
|
|
||||||
def locate_app(module_name, app_name, raise_if_not_found=True):
|
@t.overload
|
||||||
|
def locate_app(
|
||||||
|
module_name: str, app_name: str | None, raise_if_not_found: t.Literal[True] = True
|
||||||
|
) -> Flask:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@t.overload
|
||||||
|
def locate_app(
|
||||||
|
module_name: str, app_name: str | None, raise_if_not_found: t.Literal[False] = ...
|
||||||
|
) -> Flask | None:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def locate_app(
|
||||||
|
module_name: str, app_name: str | None, raise_if_not_found: bool = True
|
||||||
|
) -> Flask | None:
|
||||||
try:
|
try:
|
||||||
__import__(module_name)
|
__import__(module_name)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Reraise the ImportError if it occurred within the imported module.
|
# Reraise the ImportError if it occurred within the imported module.
|
||||||
# Determine this by checking whether the trace has a depth > 1.
|
# Determine this by checking whether the trace has a depth > 1.
|
||||||
if sys.exc_info()[2].tb_next:
|
if sys.exc_info()[2].tb_next: # type: ignore[union-attr]
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
f"While importing {module_name!r}, an ImportError was"
|
f"While importing {module_name!r}, an ImportError was"
|
||||||
f" raised:\n\n{traceback.format_exc()}"
|
f" raised:\n\n{traceback.format_exc()}"
|
||||||
@@ -228,7 +256,7 @@ def locate_app(module_name, app_name, raise_if_not_found=True):
|
|||||||
elif raise_if_not_found:
|
elif raise_if_not_found:
|
||||||
raise NoAppException(f"Could not import {module_name!r}.") from None
|
raise NoAppException(f"Could not import {module_name!r}.") from None
|
||||||
else:
|
else:
|
||||||
return
|
return None
|
||||||
|
|
||||||
module = sys.modules[module_name]
|
module = sys.modules[module_name]
|
||||||
|
|
||||||
@@ -238,7 +266,7 @@ def locate_app(module_name, app_name, raise_if_not_found=True):
|
|||||||
return find_app_by_string(module, app_name)
|
return find_app_by_string(module, app_name)
|
||||||
|
|
||||||
|
|
||||||
def get_version(ctx, param, value):
|
def get_version(ctx: click.Context, param: click.Parameter, value: t.Any) -> None:
|
||||||
if not value or ctx.resilient_parsing:
|
if not value or ctx.resilient_parsing:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -299,7 +327,7 @@ class ScriptInfo:
|
|||||||
return self._loaded_app
|
return self._loaded_app
|
||||||
|
|
||||||
if self.create_app is not None:
|
if self.create_app is not None:
|
||||||
app = self.create_app()
|
app: Flask | None = self.create_app()
|
||||||
else:
|
else:
|
||||||
if self.app_import_path:
|
if self.app_import_path:
|
||||||
path, name = (
|
path, name = (
|
||||||
@@ -312,10 +340,10 @@ class ScriptInfo:
|
|||||||
import_name = prepare_import(path)
|
import_name = prepare_import(path)
|
||||||
app = locate_app(import_name, None, raise_if_not_found=False)
|
app = locate_app(import_name, None, raise_if_not_found=False)
|
||||||
|
|
||||||
if app:
|
if app is not None:
|
||||||
break
|
break
|
||||||
|
|
||||||
if not app:
|
if app is None:
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
"Could not locate a Flask application. Use the"
|
"Could not locate a Flask application. Use the"
|
||||||
" 'flask --app' option, 'FLASK_APP' environment"
|
" 'flask --app' option, 'FLASK_APP' environment"
|
||||||
@@ -334,8 +362,10 @@ class ScriptInfo:
|
|||||||
|
|
||||||
pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True)
|
pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True)
|
||||||
|
|
||||||
|
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
|
||||||
|
|
||||||
def with_appcontext(f):
|
|
||||||
|
def with_appcontext(f: F) -> F:
|
||||||
"""Wraps a callback so that it's guaranteed to be executed with the
|
"""Wraps a callback so that it's guaranteed to be executed with the
|
||||||
script's application context.
|
script's application context.
|
||||||
|
|
||||||
@@ -350,14 +380,14 @@ def with_appcontext(f):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def decorator(__ctx, *args, **kwargs):
|
def decorator(ctx: click.Context, /, *args: t.Any, **kwargs: t.Any) -> t.Any:
|
||||||
if not current_app:
|
if not current_app:
|
||||||
app = __ctx.ensure_object(ScriptInfo).load_app()
|
app = ctx.ensure_object(ScriptInfo).load_app()
|
||||||
__ctx.with_resource(app.app_context())
|
ctx.with_resource(app.app_context())
|
||||||
|
|
||||||
return __ctx.invoke(f, *args, **kwargs)
|
return ctx.invoke(f, *args, **kwargs)
|
||||||
|
|
||||||
return update_wrapper(decorator, f)
|
return update_wrapper(decorator, f) # type: ignore[return-value]
|
||||||
|
|
||||||
|
|
||||||
class AppGroup(click.Group):
|
class AppGroup(click.Group):
|
||||||
@@ -368,27 +398,31 @@ class AppGroup(click.Group):
|
|||||||
Not to be confused with :class:`FlaskGroup`.
|
Not to be confused with :class:`FlaskGroup`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def command(self, *args, **kwargs):
|
def command( # type: ignore[override]
|
||||||
|
self, *args: t.Any, **kwargs: t.Any
|
||||||
|
) -> t.Callable[[t.Callable[..., t.Any]], click.Command]:
|
||||||
"""This works exactly like the method of the same name on a regular
|
"""This works exactly like the method of the same name on a regular
|
||||||
:class:`click.Group` but it wraps callbacks in :func:`with_appcontext`
|
:class:`click.Group` but it wraps callbacks in :func:`with_appcontext`
|
||||||
unless it's disabled by passing ``with_appcontext=False``.
|
unless it's disabled by passing ``with_appcontext=False``.
|
||||||
"""
|
"""
|
||||||
wrap_for_ctx = kwargs.pop("with_appcontext", True)
|
wrap_for_ctx = kwargs.pop("with_appcontext", True)
|
||||||
|
|
||||||
def decorator(f):
|
def decorator(f: t.Callable[..., t.Any]) -> click.Command:
|
||||||
if wrap_for_ctx:
|
if wrap_for_ctx:
|
||||||
f = with_appcontext(f)
|
f = with_appcontext(f)
|
||||||
return click.Group.command(self, *args, **kwargs)(f)
|
return super(AppGroup, self).command(*args, **kwargs)(f) # type: ignore[no-any-return]
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
def group(self, *args, **kwargs):
|
def group( # type: ignore[override]
|
||||||
|
self, *args: t.Any, **kwargs: t.Any
|
||||||
|
) -> t.Callable[[t.Callable[..., t.Any]], click.Group]:
|
||||||
"""This works exactly like the method of the same name on a regular
|
"""This works exactly like the method of the same name on a regular
|
||||||
:class:`click.Group` but it defaults the group class to
|
:class:`click.Group` but it defaults the group class to
|
||||||
:class:`AppGroup`.
|
:class:`AppGroup`.
|
||||||
"""
|
"""
|
||||||
kwargs.setdefault("cls", AppGroup)
|
kwargs.setdefault("cls", AppGroup)
|
||||||
return click.Group.group(self, *args, **kwargs)
|
return super().group(*args, **kwargs) # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
|
||||||
def _set_app(ctx: click.Context, param: click.Option, value: str | None) -> str | None:
|
def _set_app(ctx: click.Context, param: click.Option, value: str | None) -> str | None:
|
||||||
@@ -545,7 +579,7 @@ class FlaskGroup(AppGroup):
|
|||||||
|
|
||||||
self._loaded_plugin_commands = False
|
self._loaded_plugin_commands = False
|
||||||
|
|
||||||
def _load_plugin_commands(self):
|
def _load_plugin_commands(self) -> None:
|
||||||
if self._loaded_plugin_commands:
|
if self._loaded_plugin_commands:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -562,7 +596,7 @@ class FlaskGroup(AppGroup):
|
|||||||
|
|
||||||
self._loaded_plugin_commands = True
|
self._loaded_plugin_commands = True
|
||||||
|
|
||||||
def get_command(self, ctx, name):
|
def get_command(self, ctx: click.Context, name: str) -> click.Command | None:
|
||||||
self._load_plugin_commands()
|
self._load_plugin_commands()
|
||||||
# Look up built-in and plugin commands, which should be
|
# Look up built-in and plugin commands, which should be
|
||||||
# available even if the app fails to load.
|
# available even if the app fails to load.
|
||||||
@@ -584,12 +618,12 @@ class FlaskGroup(AppGroup):
|
|||||||
# Push an app context for the loaded app unless it is already
|
# Push an app context for the loaded app unless it is already
|
||||||
# active somehow. This makes the context available to parameter
|
# active somehow. This makes the context available to parameter
|
||||||
# and command callbacks without needing @with_appcontext.
|
# and command callbacks without needing @with_appcontext.
|
||||||
if not current_app or current_app._get_current_object() is not app:
|
if not current_app or current_app._get_current_object() is not app: # type: ignore[attr-defined]
|
||||||
ctx.with_resource(app.app_context())
|
ctx.with_resource(app.app_context())
|
||||||
|
|
||||||
return app.cli.get_command(ctx, name)
|
return app.cli.get_command(ctx, name)
|
||||||
|
|
||||||
def list_commands(self, ctx):
|
def list_commands(self, ctx: click.Context) -> list[str]:
|
||||||
self._load_plugin_commands()
|
self._load_plugin_commands()
|
||||||
# Start with the built-in and plugin commands.
|
# Start with the built-in and plugin commands.
|
||||||
rv = set(super().list_commands(ctx))
|
rv = set(super().list_commands(ctx))
|
||||||
@@ -645,14 +679,14 @@ class FlaskGroup(AppGroup):
|
|||||||
return super().parse_args(ctx, args)
|
return super().parse_args(ctx, args)
|
||||||
|
|
||||||
|
|
||||||
def _path_is_ancestor(path, other):
|
def _path_is_ancestor(path: str, other: str) -> bool:
|
||||||
"""Take ``other`` and remove the length of ``path`` from it. Then join it
|
"""Take ``other`` and remove the length of ``path`` from it. Then join it
|
||||||
to ``path``. If it is the original value, ``path`` is an ancestor of
|
to ``path``. If it is the original value, ``path`` is an ancestor of
|
||||||
``other``."""
|
``other``."""
|
||||||
return os.path.join(path, other[len(path) :].lstrip(os.sep)) == other
|
return os.path.join(path, other[len(path) :].lstrip(os.sep)) == other
|
||||||
|
|
||||||
|
|
||||||
def load_dotenv(path: str | os.PathLike | None = None) -> bool:
|
def load_dotenv(path: str | os.PathLike[str] | None = None) -> bool:
|
||||||
"""Load "dotenv" files in order of precedence to set environment variables.
|
"""Load "dotenv" files in order of precedence to set environment variables.
|
||||||
|
|
||||||
If an env var is already set it is not overwritten, so earlier files in the
|
If an env var is already set it is not overwritten, so earlier files in the
|
||||||
@@ -713,7 +747,7 @@ def load_dotenv(path: str | os.PathLike | None = None) -> bool:
|
|||||||
return loaded # True if at least one file was located and loaded.
|
return loaded # True if at least one file was located and loaded.
|
||||||
|
|
||||||
|
|
||||||
def show_server_banner(debug, app_import_path):
|
def show_server_banner(debug: bool, app_import_path: str | None) -> None:
|
||||||
"""Show extra startup messages the first time the server is run,
|
"""Show extra startup messages the first time the server is run,
|
||||||
ignoring the reloader.
|
ignoring the reloader.
|
||||||
"""
|
"""
|
||||||
@@ -735,10 +769,12 @@ class CertParamType(click.ParamType):
|
|||||||
|
|
||||||
name = "path"
|
name = "path"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True)
|
self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True)
|
||||||
|
|
||||||
def convert(self, value, param, ctx):
|
def convert(
|
||||||
|
self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None
|
||||||
|
) -> t.Any:
|
||||||
try:
|
try:
|
||||||
import ssl
|
import ssl
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -773,7 +809,7 @@ class CertParamType(click.ParamType):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def _validate_key(ctx, param, value):
|
def _validate_key(ctx: click.Context, param: click.Parameter, value: t.Any) -> t.Any:
|
||||||
"""The ``--key`` option must be specified when ``--cert`` is a file.
|
"""The ``--key`` option must be specified when ``--cert`` is a file.
|
||||||
Modifies the ``cert`` param to be a ``(cert, key)`` pair if needed.
|
Modifies the ``cert`` param to be a ``(cert, key)`` pair if needed.
|
||||||
"""
|
"""
|
||||||
@@ -795,7 +831,9 @@ def _validate_key(ctx, param, value):
|
|||||||
|
|
||||||
if is_context:
|
if is_context:
|
||||||
raise click.BadParameter(
|
raise click.BadParameter(
|
||||||
'When "--cert" is an SSLContext object, "--key is not used.', ctx, param
|
'When "--cert" is an SSLContext object, "--key" is not used.',
|
||||||
|
ctx,
|
||||||
|
param,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not cert:
|
if not cert:
|
||||||
@@ -816,8 +854,11 @@ class SeparatedPathType(click.Path):
|
|||||||
validated as a :class:`click.Path` type.
|
validated as a :class:`click.Path` type.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def convert(self, value, param, ctx):
|
def convert(
|
||||||
|
self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None
|
||||||
|
) -> t.Any:
|
||||||
items = self.split_envvar_value(value)
|
items = self.split_envvar_value(value)
|
||||||
|
# can't call no-arg super() inside list comprehension until Python 3.12
|
||||||
super_convert = super().convert
|
super_convert = super().convert
|
||||||
return [super_convert(item, param, ctx) for item in items]
|
return [super_convert(item, param, ctx) for item in items]
|
||||||
|
|
||||||
@@ -876,16 +917,16 @@ class SeparatedPathType(click.Path):
|
|||||||
)
|
)
|
||||||
@pass_script_info
|
@pass_script_info
|
||||||
def run_command(
|
def run_command(
|
||||||
info,
|
info: ScriptInfo,
|
||||||
host,
|
host: str,
|
||||||
port,
|
port: int,
|
||||||
reload,
|
reload: bool,
|
||||||
debugger,
|
debugger: bool,
|
||||||
with_threads,
|
with_threads: bool,
|
||||||
cert,
|
cert: ssl.SSLContext | tuple[str, str | None] | t.Literal["adhoc"] | None,
|
||||||
extra_files,
|
extra_files: list[str] | None,
|
||||||
exclude_patterns,
|
exclude_patterns: list[str] | None,
|
||||||
):
|
) -> None:
|
||||||
"""Run a local development server.
|
"""Run a local development server.
|
||||||
|
|
||||||
This server is for development purposes only. It does not provide
|
This server is for development purposes only. It does not provide
|
||||||
@@ -895,7 +936,7 @@ def run_command(
|
|||||||
option.
|
option.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
app = info.load_app()
|
app: WSGIApplication = info.load_app()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if is_running_from_reloader():
|
if is_running_from_reloader():
|
||||||
# When reloading, print out the error immediately, but raise
|
# When reloading, print out the error immediately, but raise
|
||||||
@@ -903,7 +944,9 @@ def run_command(
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
err = e
|
err = e
|
||||||
|
|
||||||
def app(environ, start_response):
|
def app(
|
||||||
|
environ: WSGIEnvironment, start_response: StartResponse
|
||||||
|
) -> cabc.Iterable[bytes]:
|
||||||
raise err from None
|
raise err from None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -954,7 +997,7 @@ def shell_command() -> None:
|
|||||||
f"App: {current_app.import_name}\n"
|
f"App: {current_app.import_name}\n"
|
||||||
f"Instance: {current_app.instance_path}"
|
f"Instance: {current_app.instance_path}"
|
||||||
)
|
)
|
||||||
ctx: dict = {}
|
ctx: dict[str, t.Any] = {}
|
||||||
|
|
||||||
# Support the regular Python interpreter startup script if someone
|
# Support the regular Python interpreter startup script if someone
|
||||||
# is using it.
|
# is using it.
|
||||||
|
|||||||
@@ -8,27 +8,48 @@ import typing as t
|
|||||||
|
|
||||||
from werkzeug.utils import import_string
|
from werkzeug.utils import import_string
|
||||||
|
|
||||||
|
if t.TYPE_CHECKING:
|
||||||
|
import typing_extensions as te
|
||||||
|
|
||||||
class ConfigAttribute:
|
from .sansio.app import App
|
||||||
|
|
||||||
|
|
||||||
|
T = t.TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigAttribute(t.Generic[T]):
|
||||||
"""Makes an attribute forward to the config"""
|
"""Makes an attribute forward to the config"""
|
||||||
|
|
||||||
def __init__(self, name: str, get_converter: t.Callable | None = None) -> None:
|
def __init__(
|
||||||
|
self, name: str, get_converter: t.Callable[[t.Any], T] | None = None
|
||||||
|
) -> None:
|
||||||
self.__name__ = name
|
self.__name__ = name
|
||||||
self.get_converter = get_converter
|
self.get_converter = get_converter
|
||||||
|
|
||||||
def __get__(self, obj: t.Any, owner: t.Any = None) -> t.Any:
|
@t.overload
|
||||||
|
def __get__(self, obj: None, owner: None) -> te.Self:
|
||||||
|
...
|
||||||
|
|
||||||
|
@t.overload
|
||||||
|
def __get__(self, obj: App, owner: type[App]) -> T:
|
||||||
|
...
|
||||||
|
|
||||||
|
def __get__(self, obj: App | None, owner: type[App] | None = None) -> T | te.Self:
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
rv = obj.config[self.__name__]
|
rv = obj.config[self.__name__]
|
||||||
|
|
||||||
if self.get_converter is not None:
|
if self.get_converter is not None:
|
||||||
rv = self.get_converter(rv)
|
rv = self.get_converter(rv)
|
||||||
return rv
|
|
||||||
|
|
||||||
def __set__(self, obj: t.Any, value: t.Any) -> None:
|
return rv # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
def __set__(self, obj: App, value: t.Any) -> None:
|
||||||
obj.config[self.__name__] = value
|
obj.config[self.__name__] = value
|
||||||
|
|
||||||
|
|
||||||
class Config(dict):
|
class Config(dict): # type: ignore[type-arg]
|
||||||
"""Works exactly like a dict but provides ways to fill it from files
|
"""Works exactly like a dict but provides ways to fill it from files
|
||||||
or special dictionaries. There are two common patterns to populate the
|
or special dictionaries. There are two common patterns to populate the
|
||||||
config.
|
config.
|
||||||
@@ -73,7 +94,9 @@ class Config(dict):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, root_path: str | os.PathLike, defaults: dict | None = None
|
self,
|
||||||
|
root_path: str | os.PathLike[str],
|
||||||
|
defaults: dict[str, t.Any] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(defaults or {})
|
super().__init__(defaults or {})
|
||||||
self.root_path = root_path
|
self.root_path = root_path
|
||||||
@@ -166,7 +189,9 @@ class Config(dict):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def from_pyfile(self, filename: str | os.PathLike, silent: bool = False) -> bool:
|
def from_pyfile(
|
||||||
|
self, filename: str | os.PathLike[str], silent: bool = False
|
||||||
|
) -> bool:
|
||||||
"""Updates the values in the config from a Python file. This function
|
"""Updates the values in the config from a Python file. This function
|
||||||
behaves as if the file was imported as module with the
|
behaves as if the file was imported as module with the
|
||||||
:meth:`from_object` function.
|
:meth:`from_object` function.
|
||||||
@@ -235,8 +260,8 @@ class Config(dict):
|
|||||||
|
|
||||||
def from_file(
|
def from_file(
|
||||||
self,
|
self,
|
||||||
filename: str | os.PathLike,
|
filename: str | os.PathLike[str],
|
||||||
load: t.Callable[[t.IO[t.Any]], t.Mapping],
|
load: t.Callable[[t.IO[t.Any]], t.Mapping[str, t.Any]],
|
||||||
silent: bool = False,
|
silent: bool = False,
|
||||||
text: bool = True,
|
text: bool = True,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ from .signals import appcontext_popped
|
|||||||
from .signals import appcontext_pushed
|
from .signals import appcontext_pushed
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
|
from _typeshed.wsgi import WSGIEnvironment
|
||||||
|
|
||||||
from .app import Flask
|
from .app import Flask
|
||||||
from .sessions import SessionMixin
|
from .sessions import SessionMixin
|
||||||
from .wrappers import Request
|
from .wrappers import Request
|
||||||
@@ -112,7 +114,9 @@ class _AppCtxGlobals:
|
|||||||
return object.__repr__(self)
|
return object.__repr__(self)
|
||||||
|
|
||||||
|
|
||||||
def after_this_request(f: ft.AfterRequestCallable) -> ft.AfterRequestCallable:
|
def after_this_request(
|
||||||
|
f: ft.AfterRequestCallable[t.Any],
|
||||||
|
) -> ft.AfterRequestCallable[t.Any]:
|
||||||
"""Executes a function after this request. This is useful to modify
|
"""Executes a function after this request. This is useful to modify
|
||||||
response objects. The function is passed the response object and has
|
response objects. The function is passed the response object and has
|
||||||
to return the same or a new one.
|
to return the same or a new one.
|
||||||
@@ -145,7 +149,10 @@ def after_this_request(f: ft.AfterRequestCallable) -> ft.AfterRequestCallable:
|
|||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
||||||
def copy_current_request_context(f: t.Callable) -> t.Callable:
|
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
|
||||||
|
|
||||||
|
|
||||||
|
def copy_current_request_context(f: F) -> F:
|
||||||
"""A helper function that decorates a function to retain the current
|
"""A helper function that decorates a function to retain the current
|
||||||
request context. This is useful when working with greenlets. The moment
|
request context. This is useful when working with greenlets. The moment
|
||||||
the function is decorated a copy of the request context is created and
|
the function is decorated a copy of the request context is created and
|
||||||
@@ -179,11 +186,11 @@ def copy_current_request_context(f: t.Callable) -> t.Callable:
|
|||||||
|
|
||||||
ctx = ctx.copy()
|
ctx = ctx.copy()
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:
|
||||||
with ctx:
|
with ctx: # type: ignore[union-attr]
|
||||||
return ctx.app.ensure_sync(f)(*args, **kwargs)
|
return ctx.app.ensure_sync(f)(*args, **kwargs) # type: ignore[union-attr]
|
||||||
|
|
||||||
return update_wrapper(wrapper, f)
|
return update_wrapper(wrapper, f) # type: ignore[return-value]
|
||||||
|
|
||||||
|
|
||||||
def has_request_context() -> bool:
|
def has_request_context() -> bool:
|
||||||
@@ -239,7 +246,7 @@ class AppContext:
|
|||||||
self.app = app
|
self.app = app
|
||||||
self.url_adapter = app.create_url_adapter(None)
|
self.url_adapter = app.create_url_adapter(None)
|
||||||
self.g: _AppCtxGlobals = app.app_ctx_globals_class()
|
self.g: _AppCtxGlobals = app.app_ctx_globals_class()
|
||||||
self._cv_tokens: list[contextvars.Token] = []
|
self._cv_tokens: list[contextvars.Token[AppContext]] = []
|
||||||
|
|
||||||
def push(self) -> None:
|
def push(self) -> None:
|
||||||
"""Binds the app context to the current context."""
|
"""Binds the app context to the current context."""
|
||||||
@@ -302,7 +309,7 @@ class RequestContext:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
app: Flask,
|
app: Flask,
|
||||||
environ: dict,
|
environ: WSGIEnvironment,
|
||||||
request: Request | None = None,
|
request: Request | None = None,
|
||||||
session: SessionMixin | None = None,
|
session: SessionMixin | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -321,9 +328,11 @@ class RequestContext:
|
|||||||
# Functions that should be executed after the request on the response
|
# Functions that should be executed after the request on the response
|
||||||
# object. These will be called before the regular "after_request"
|
# object. These will be called before the regular "after_request"
|
||||||
# functions.
|
# functions.
|
||||||
self._after_request_functions: list[ft.AfterRequestCallable] = []
|
self._after_request_functions: list[ft.AfterRequestCallable[t.Any]] = []
|
||||||
|
|
||||||
self._cv_tokens: list[tuple[contextvars.Token, AppContext | None]] = []
|
self._cv_tokens: list[
|
||||||
|
tuple[contextvars.Token[RequestContext], AppContext | None]
|
||||||
|
] = []
|
||||||
|
|
||||||
def copy(self) -> RequestContext:
|
def copy(self) -> RequestContext:
|
||||||
"""Creates a copy of this request context with the same request object.
|
"""Creates a copy of this request context with the same request object.
|
||||||
|
|||||||
@@ -2,9 +2,16 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
from .app import Flask
|
from jinja2.loaders import BaseLoader
|
||||||
|
from werkzeug.routing import RequestRedirect
|
||||||
|
|
||||||
from .blueprints import Blueprint
|
from .blueprints import Blueprint
|
||||||
from .globals import request_ctx
|
from .globals import request_ctx
|
||||||
|
from .sansio.app import App
|
||||||
|
|
||||||
|
if t.TYPE_CHECKING:
|
||||||
|
from .sansio.scaffold import Scaffold
|
||||||
|
from .wrappers import Request
|
||||||
|
|
||||||
|
|
||||||
class UnexpectedUnicodeError(AssertionError, UnicodeError):
|
class UnexpectedUnicodeError(AssertionError, UnicodeError):
|
||||||
@@ -18,7 +25,7 @@ class DebugFilesKeyError(KeyError, AssertionError):
|
|||||||
provide a better error message than just a generic KeyError/BadRequest.
|
provide a better error message than just a generic KeyError/BadRequest.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, request, key):
|
def __init__(self, request: Request, key: str) -> None:
|
||||||
form_matches = request.form.getlist(key)
|
form_matches = request.form.getlist(key)
|
||||||
buf = [
|
buf = [
|
||||||
f"You tried to access the file {key!r} in the request.files"
|
f"You tried to access the file {key!r} in the request.files"
|
||||||
@@ -36,7 +43,7 @@ class DebugFilesKeyError(KeyError, AssertionError):
|
|||||||
)
|
)
|
||||||
self.msg = "".join(buf)
|
self.msg = "".join(buf)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
return self.msg
|
return self.msg
|
||||||
|
|
||||||
|
|
||||||
@@ -47,8 +54,9 @@ class FormDataRoutingRedirect(AssertionError):
|
|||||||
307 or 308.
|
307 or 308.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, request):
|
def __init__(self, request: Request) -> None:
|
||||||
exc = request.routing_exception
|
exc = request.routing_exception
|
||||||
|
assert isinstance(exc, RequestRedirect)
|
||||||
buf = [
|
buf = [
|
||||||
f"A request was sent to '{request.url}', but routing issued"
|
f"A request was sent to '{request.url}', but routing issued"
|
||||||
f" a redirect to the canonical URL '{exc.new_url}'."
|
f" a redirect to the canonical URL '{exc.new_url}'."
|
||||||
@@ -70,7 +78,7 @@ class FormDataRoutingRedirect(AssertionError):
|
|||||||
super().__init__("".join(buf))
|
super().__init__("".join(buf))
|
||||||
|
|
||||||
|
|
||||||
def attach_enctype_error_multidict(request):
|
def attach_enctype_error_multidict(request: Request) -> None:
|
||||||
"""Patch ``request.files.__getitem__`` to raise a descriptive error
|
"""Patch ``request.files.__getitem__`` to raise a descriptive error
|
||||||
about ``enctype=multipart/form-data``.
|
about ``enctype=multipart/form-data``.
|
||||||
|
|
||||||
@@ -79,8 +87,8 @@ def attach_enctype_error_multidict(request):
|
|||||||
"""
|
"""
|
||||||
oldcls = request.files.__class__
|
oldcls = request.files.__class__
|
||||||
|
|
||||||
class newcls(oldcls):
|
class newcls(oldcls): # type: ignore[valid-type, misc]
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key: str) -> t.Any:
|
||||||
try:
|
try:
|
||||||
return super().__getitem__(key)
|
return super().__getitem__(key)
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
@@ -96,7 +104,7 @@ def attach_enctype_error_multidict(request):
|
|||||||
request.files.__class__ = newcls
|
request.files.__class__ = newcls
|
||||||
|
|
||||||
|
|
||||||
def _dump_loader_info(loader) -> t.Generator:
|
def _dump_loader_info(loader: BaseLoader) -> t.Iterator[str]:
|
||||||
yield f"class: {type(loader).__module__}.{type(loader).__name__}"
|
yield f"class: {type(loader).__module__}.{type(loader).__name__}"
|
||||||
for key, value in sorted(loader.__dict__.items()):
|
for key, value in sorted(loader.__dict__.items()):
|
||||||
if key.startswith("_"):
|
if key.startswith("_"):
|
||||||
@@ -113,7 +121,17 @@ def _dump_loader_info(loader) -> t.Generator:
|
|||||||
yield f"{key}: {value!r}"
|
yield f"{key}: {value!r}"
|
||||||
|
|
||||||
|
|
||||||
def explain_template_loading_attempts(app: Flask, template, attempts) -> None:
|
def explain_template_loading_attempts(
|
||||||
|
app: App,
|
||||||
|
template: str,
|
||||||
|
attempts: list[
|
||||||
|
tuple[
|
||||||
|
BaseLoader,
|
||||||
|
Scaffold,
|
||||||
|
tuple[str, str | None, t.Callable[[], bool] | None] | None,
|
||||||
|
]
|
||||||
|
],
|
||||||
|
) -> None:
|
||||||
"""This should help developers understand what failed"""
|
"""This should help developers understand what failed"""
|
||||||
info = [f"Locating template {template!r}:"]
|
info = [f"Locating template {template!r}:"]
|
||||||
total_found = 0
|
total_found = 0
|
||||||
@@ -122,7 +140,7 @@ def explain_template_loading_attempts(app: Flask, template, attempts) -> None:
|
|||||||
blueprint = request_ctx.request.blueprint
|
blueprint = request_ctx.request.blueprint
|
||||||
|
|
||||||
for idx, (loader, srcobj, triple) in enumerate(attempts):
|
for idx, (loader, srcobj, triple) in enumerate(attempts):
|
||||||
if isinstance(srcobj, Flask):
|
if isinstance(srcobj, App):
|
||||||
src_info = f"application {srcobj.import_name!r}"
|
src_info = f"application {srcobj.import_name!r}"
|
||||||
elif isinstance(srcobj, Blueprint):
|
elif isinstance(srcobj, Blueprint):
|
||||||
src_info = f"blueprint {srcobj.name!r} ({srcobj.import_name})"
|
src_info = f"blueprint {srcobj.name!r} ({srcobj.import_name})"
|
||||||
|
|||||||
@@ -14,25 +14,6 @@ if t.TYPE_CHECKING: # pragma: no cover
|
|||||||
from .wrappers import Request
|
from .wrappers import Request
|
||||||
|
|
||||||
|
|
||||||
class _FakeStack:
|
|
||||||
def __init__(self, name: str, cv: ContextVar[t.Any]) -> None:
|
|
||||||
self.name = name
|
|
||||||
self.cv = cv
|
|
||||||
|
|
||||||
@property
|
|
||||||
def top(self) -> t.Any | None:
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
f"'_{self.name}_ctx_stack' is deprecated and will be removed in Flask 2.4."
|
|
||||||
f" Use 'g' to store data, or '{self.name}_ctx' to access the current"
|
|
||||||
" context.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return self.cv.get(None)
|
|
||||||
|
|
||||||
|
|
||||||
_no_app_msg = """\
|
_no_app_msg = """\
|
||||||
Working outside of application context.
|
Working outside of application context.
|
||||||
|
|
||||||
@@ -41,7 +22,6 @@ the current application. To solve this, set up an application context
|
|||||||
with app.app_context(). See the documentation for more information.\
|
with app.app_context(). See the documentation for more information.\
|
||||||
"""
|
"""
|
||||||
_cv_app: ContextVar[AppContext] = ContextVar("flask.app_ctx")
|
_cv_app: ContextVar[AppContext] = ContextVar("flask.app_ctx")
|
||||||
__app_ctx_stack = _FakeStack("app", _cv_app)
|
|
||||||
app_ctx: AppContext = LocalProxy( # type: ignore[assignment]
|
app_ctx: AppContext = LocalProxy( # type: ignore[assignment]
|
||||||
_cv_app, unbound_message=_no_app_msg
|
_cv_app, unbound_message=_no_app_msg
|
||||||
)
|
)
|
||||||
@@ -60,7 +40,6 @@ an active HTTP request. Consult the documentation on testing for
|
|||||||
information about how to avoid this problem.\
|
information about how to avoid this problem.\
|
||||||
"""
|
"""
|
||||||
_cv_request: ContextVar[RequestContext] = ContextVar("flask.request_ctx")
|
_cv_request: ContextVar[RequestContext] = ContextVar("flask.request_ctx")
|
||||||
__request_ctx_stack = _FakeStack("request", _cv_request)
|
|
||||||
request_ctx: RequestContext = LocalProxy( # type: ignore[assignment]
|
request_ctx: RequestContext = LocalProxy( # type: ignore[assignment]
|
||||||
_cv_request, unbound_message=_no_req_msg
|
_cv_request, unbound_message=_no_req_msg
|
||||||
)
|
)
|
||||||
@@ -70,27 +49,3 @@ request: Request = LocalProxy( # type: ignore[assignment]
|
|||||||
session: SessionMixin = LocalProxy( # type: ignore[assignment]
|
session: SessionMixin = LocalProxy( # type: ignore[assignment]
|
||||||
_cv_request, "session", unbound_message=_no_req_msg
|
_cv_request, "session", unbound_message=_no_req_msg
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def __getattr__(name: str) -> t.Any:
|
|
||||||
if name == "_app_ctx_stack":
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
"'_app_ctx_stack' is deprecated and will be removed in Flask 2.4.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return __app_ctx_stack
|
|
||||||
|
|
||||||
if name == "_request_ctx_stack":
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
"'_request_ctx_stack' is deprecated and will be removed in Flask 2.4.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return __request_ctx_stack
|
|
||||||
|
|
||||||
raise AttributeError(name)
|
|
||||||
|
|||||||
@@ -2,18 +2,16 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import os
|
import os
|
||||||
import socket
|
|
||||||
import sys
|
import sys
|
||||||
import typing as t
|
import typing as t
|
||||||
import warnings
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from functools import update_wrapper
|
from functools import update_wrapper
|
||||||
from threading import RLock
|
|
||||||
|
|
||||||
import werkzeug.utils
|
import werkzeug.utils
|
||||||
from werkzeug.exceptions import abort as _wz_abort
|
from werkzeug.exceptions import abort as _wz_abort
|
||||||
from werkzeug.utils import redirect as _wz_redirect
|
from werkzeug.utils import redirect as _wz_redirect
|
||||||
|
from werkzeug.wrappers import Response as BaseResponse
|
||||||
|
|
||||||
from .globals import _cv_request
|
from .globals import _cv_request
|
||||||
from .globals import current_app
|
from .globals import current_app
|
||||||
@@ -23,7 +21,6 @@ from .globals import session
|
|||||||
from .signals import message_flashed
|
from .signals import message_flashed
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
from werkzeug.wrappers import Response as BaseResponse
|
|
||||||
from .wrappers import Response
|
from .wrappers import Response
|
||||||
|
|
||||||
|
|
||||||
@@ -51,9 +48,7 @@ def get_load_dotenv(default: bool = True) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def stream_with_context(
|
def stream_with_context(
|
||||||
generator_or_function: (
|
generator_or_function: t.Iterator[t.AnyStr] | t.Callable[..., t.Iterator[t.AnyStr]],
|
||||||
t.Iterator[t.AnyStr] | t.Callable[..., t.Iterator[t.AnyStr]]
|
|
||||||
)
|
|
||||||
) -> t.Iterator[t.AnyStr]:
|
) -> t.Iterator[t.AnyStr]:
|
||||||
"""Request contexts disappear when the response is started on the server.
|
"""Request contexts disappear when the response is started on the server.
|
||||||
This is done for efficiency reasons and to make it less likely to encounter
|
This is done for efficiency reasons and to make it less likely to encounter
|
||||||
@@ -89,16 +84,16 @@ def stream_with_context(
|
|||||||
.. versionadded:: 0.9
|
.. versionadded:: 0.9
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
gen = iter(generator_or_function) # type: ignore
|
gen = iter(generator_or_function) # type: ignore[arg-type]
|
||||||
except TypeError:
|
except TypeError:
|
||||||
|
|
||||||
def decorator(*args: t.Any, **kwargs: t.Any) -> t.Any:
|
def decorator(*args: t.Any, **kwargs: t.Any) -> t.Any:
|
||||||
gen = generator_or_function(*args, **kwargs) # type: ignore
|
gen = generator_or_function(*args, **kwargs) # type: ignore[operator]
|
||||||
return stream_with_context(gen)
|
return stream_with_context(gen)
|
||||||
|
|
||||||
return update_wrapper(decorator, generator_or_function) # type: ignore
|
return update_wrapper(decorator, generator_or_function) # type: ignore[arg-type]
|
||||||
|
|
||||||
def generator() -> t.Generator:
|
def generator() -> t.Iterator[t.AnyStr | None]:
|
||||||
ctx = _cv_request.get(None)
|
ctx = _cv_request.get(None)
|
||||||
if ctx is None:
|
if ctx is None:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
@@ -126,7 +121,7 @@ def stream_with_context(
|
|||||||
# real generator is executed.
|
# real generator is executed.
|
||||||
wrapped_g = generator()
|
wrapped_g = generator()
|
||||||
next(wrapped_g)
|
next(wrapped_g)
|
||||||
return wrapped_g
|
return wrapped_g # type: ignore[return-value]
|
||||||
|
|
||||||
|
|
||||||
def make_response(*args: t.Any) -> Response:
|
def make_response(*args: t.Any) -> Response:
|
||||||
@@ -175,7 +170,7 @@ def make_response(*args: t.Any) -> Response:
|
|||||||
return current_app.response_class()
|
return current_app.response_class()
|
||||||
if len(args) == 1:
|
if len(args) == 1:
|
||||||
args = args[0]
|
args = args[0]
|
||||||
return current_app.make_response(args) # type: ignore
|
return current_app.make_response(args)
|
||||||
|
|
||||||
|
|
||||||
def url_for(
|
def url_for(
|
||||||
@@ -391,7 +386,7 @@ def _prepare_send_file_kwargs(**kwargs: t.Any) -> dict[str, t.Any]:
|
|||||||
|
|
||||||
|
|
||||||
def send_file(
|
def send_file(
|
||||||
path_or_file: os.PathLike | str | t.BinaryIO,
|
path_or_file: os.PathLike[t.AnyStr] | str | t.BinaryIO,
|
||||||
mimetype: str | None = None,
|
mimetype: str | None = None,
|
||||||
as_attachment: bool = False,
|
as_attachment: bool = False,
|
||||||
download_name: str | None = None,
|
download_name: str | None = None,
|
||||||
@@ -492,7 +487,7 @@ def send_file(
|
|||||||
|
|
||||||
.. versionchanged:: 0.7
|
.. versionchanged:: 0.7
|
||||||
MIME guessing and etag support for file-like objects was
|
MIME guessing and etag support for file-like objects was
|
||||||
deprecated because it was unreliable. Pass a filename if you are
|
removed because it was unreliable. Pass a filename if you are
|
||||||
able to, otherwise attach an etag yourself.
|
able to, otherwise attach an etag yourself.
|
||||||
|
|
||||||
.. versionchanged:: 0.5
|
.. versionchanged:: 0.5
|
||||||
@@ -517,8 +512,8 @@ def send_file(
|
|||||||
|
|
||||||
|
|
||||||
def send_from_directory(
|
def send_from_directory(
|
||||||
directory: os.PathLike | str,
|
directory: os.PathLike[str] | str,
|
||||||
path: os.PathLike | str,
|
path: os.PathLike[str] | str,
|
||||||
**kwargs: t.Any,
|
**kwargs: t.Any,
|
||||||
) -> Response:
|
) -> Response:
|
||||||
"""Send a file from within a directory using :func:`send_file`.
|
"""Send a file from within a directory using :func:`send_file`.
|
||||||
@@ -613,82 +608,7 @@ def get_root_path(import_name: str) -> str:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# filepath is import_name.py for a module, or __init__.py for a package.
|
# filepath is import_name.py for a module, or __init__.py for a package.
|
||||||
return os.path.dirname(os.path.abspath(filepath))
|
return os.path.dirname(os.path.abspath(filepath)) # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
|
||||||
class locked_cached_property(werkzeug.utils.cached_property):
|
|
||||||
"""A :func:`property` that is only evaluated once. Like
|
|
||||||
:class:`werkzeug.utils.cached_property` except access uses a lock
|
|
||||||
for thread safety.
|
|
||||||
|
|
||||||
.. deprecated:: 2.3
|
|
||||||
Will be removed in Flask 2.4. Use a lock inside the decorated function if
|
|
||||||
locking is needed.
|
|
||||||
|
|
||||||
.. versionchanged:: 2.0
|
|
||||||
Inherits from Werkzeug's ``cached_property`` (and ``property``).
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
fget: t.Callable[[t.Any], t.Any],
|
|
||||||
name: str | None = None,
|
|
||||||
doc: str | None = None,
|
|
||||||
) -> None:
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
"'locked_cached_property' is deprecated and will be removed in Flask 2.4."
|
|
||||||
" Use a lock inside the decorated function if locking is needed.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
super().__init__(fget, name=name, doc=doc)
|
|
||||||
self.lock = RLock()
|
|
||||||
|
|
||||||
def __get__(self, obj: object, type: type = None) -> t.Any: # type: ignore
|
|
||||||
if obj is None:
|
|
||||||
return self
|
|
||||||
|
|
||||||
with self.lock:
|
|
||||||
return super().__get__(obj, type=type)
|
|
||||||
|
|
||||||
def __set__(self, obj: object, value: t.Any) -> None:
|
|
||||||
with self.lock:
|
|
||||||
super().__set__(obj, value)
|
|
||||||
|
|
||||||
def __delete__(self, obj: object) -> None:
|
|
||||||
with self.lock:
|
|
||||||
super().__delete__(obj)
|
|
||||||
|
|
||||||
|
|
||||||
def is_ip(value: str) -> bool:
|
|
||||||
"""Determine if the given string is an IP address.
|
|
||||||
|
|
||||||
:param value: value to check
|
|
||||||
:type value: str
|
|
||||||
|
|
||||||
:return: True if string is an IP address
|
|
||||||
:rtype: bool
|
|
||||||
|
|
||||||
.. deprecated:: 2.3
|
|
||||||
Will be removed in Flask 2.4.
|
|
||||||
"""
|
|
||||||
warnings.warn(
|
|
||||||
"The 'is_ip' function is deprecated and will be removed in Flask 2.4.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
|
|
||||||
for family in (socket.AF_INET, socket.AF_INET6):
|
|
||||||
try:
|
|
||||||
socket.inet_pton(family, value)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@lru_cache(maxsize=None)
|
||||||
|
|||||||
@@ -167,4 +167,4 @@ def jsonify(*args: t.Any, **kwargs: t.Any) -> Response:
|
|||||||
|
|
||||||
.. versionadded:: 0.2
|
.. versionadded:: 0.2
|
||||||
"""
|
"""
|
||||||
return current_app.json.response(*args, **kwargs)
|
return current_app.json.response(*args, **kwargs) # type: ignore[return-value]
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -11,8 +11,9 @@ from datetime import date
|
|||||||
from werkzeug.http import http_date
|
from werkzeug.http import http_date
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
from ..app import Flask
|
from werkzeug.sansio.response import Response
|
||||||
from ..wrappers import Response
|
|
||||||
|
from ..sansio.app import App
|
||||||
|
|
||||||
|
|
||||||
class JSONProvider:
|
class JSONProvider:
|
||||||
@@ -34,8 +35,8 @@ class JSONProvider:
|
|||||||
.. versionadded:: 2.2
|
.. versionadded:: 2.2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, app: Flask) -> None:
|
def __init__(self, app: App) -> None:
|
||||||
self._app = weakref.proxy(app)
|
self._app: App = weakref.proxy(app)
|
||||||
|
|
||||||
def dumps(self, obj: t.Any, **kwargs: t.Any) -> str:
|
def dumps(self, obj: t.Any, **kwargs: t.Any) -> str:
|
||||||
"""Serialize data as JSON.
|
"""Serialize data as JSON.
|
||||||
@@ -134,9 +135,7 @@ class DefaultJSONProvider(JSONProvider):
|
|||||||
method) will call the ``__html__`` method to get a string.
|
method) will call the ``__html__`` method to get a string.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
default: t.Callable[[t.Any], t.Any] = staticmethod(
|
default: t.Callable[[t.Any], t.Any] = staticmethod(_default) # type: ignore[assignment]
|
||||||
_default
|
|
||||||
) # type: ignore[assignment]
|
|
||||||
"""Apply this function to any object that :meth:`json.dumps` does
|
"""Apply this function to any object that :meth:`json.dumps` does
|
||||||
not know how to serialize. It should return a valid JSON type or
|
not know how to serialize. It should return a valid JSON type or
|
||||||
raise a ``TypeError``.
|
raise a ``TypeError``.
|
||||||
|
|||||||
@@ -61,9 +61,9 @@ class JSONTag:
|
|||||||
|
|
||||||
__slots__ = ("serializer",)
|
__slots__ = ("serializer",)
|
||||||
|
|
||||||
#: The tag to mark the serialized object with. If ``None``, this tag is
|
#: The tag to mark the serialized object with. If empty, this tag is
|
||||||
#: only used as an intermediate step during tagging.
|
#: only used as an intermediate step during tagging.
|
||||||
key: str | None = None
|
key: str = ""
|
||||||
|
|
||||||
def __init__(self, serializer: TaggedJSONSerializer) -> None:
|
def __init__(self, serializer: TaggedJSONSerializer) -> None:
|
||||||
"""Create a tagger for the given serializer."""
|
"""Create a tagger for the given serializer."""
|
||||||
@@ -83,7 +83,7 @@ class JSONTag:
|
|||||||
will already be removed."""
|
will already be removed."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def tag(self, value: t.Any) -> t.Any:
|
def tag(self, value: t.Any) -> dict[str, t.Any]:
|
||||||
"""Convert the value to a valid JSON type and add the tag structure
|
"""Convert the value to a valid JSON type and add the tag structure
|
||||||
around it."""
|
around it."""
|
||||||
return {self.key: self.to_json(value)}
|
return {self.key: self.to_json(value)}
|
||||||
@@ -274,7 +274,7 @@ class TaggedJSONSerializer:
|
|||||||
tag = tag_class(self)
|
tag = tag_class(self)
|
||||||
key = tag.key
|
key = tag.key
|
||||||
|
|
||||||
if key is not None:
|
if key:
|
||||||
if not force and key in self.tags:
|
if not force and key in self.tags:
|
||||||
raise KeyError(f"Tag '{key}' is already registered.")
|
raise KeyError(f"Tag '{key}' is already registered.")
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ class TaggedJSONSerializer:
|
|||||||
else:
|
else:
|
||||||
self.order.insert(index, tag)
|
self.order.insert(index, tag)
|
||||||
|
|
||||||
def tag(self, value: t.Any) -> dict[str, t.Any]:
|
def tag(self, value: t.Any) -> t.Any:
|
||||||
"""Convert a value to a tagged representation if necessary."""
|
"""Convert a value to a tagged representation if necessary."""
|
||||||
for tag in self.order:
|
for tag in self.order:
|
||||||
if tag.check(value):
|
if tag.check(value):
|
||||||
@@ -305,10 +305,22 @@ class TaggedJSONSerializer:
|
|||||||
|
|
||||||
return self.tags[key].to_python(value[key])
|
return self.tags[key].to_python(value[key])
|
||||||
|
|
||||||
|
def _untag_scan(self, value: t.Any) -> t.Any:
|
||||||
|
if isinstance(value, dict):
|
||||||
|
# untag each item recursively
|
||||||
|
value = {k: self._untag_scan(v) for k, v in value.items()}
|
||||||
|
# untag the dict itself
|
||||||
|
value = self.untag(value)
|
||||||
|
elif isinstance(value, list):
|
||||||
|
# untag each item recursively
|
||||||
|
value = [self._untag_scan(item) for item in value]
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
def dumps(self, value: t.Any) -> str:
|
def dumps(self, value: t.Any) -> str:
|
||||||
"""Tag the value and dump it to a compact JSON string."""
|
"""Tag the value and dump it to a compact JSON string."""
|
||||||
return dumps(self.tag(value), separators=(",", ":"))
|
return dumps(self.tag(value), separators=(",", ":"))
|
||||||
|
|
||||||
def loads(self, value: str) -> t.Any:
|
def loads(self, value: str) -> t.Any:
|
||||||
"""Load data from a JSON string and deserialized any tagged objects."""
|
"""Load data from a JSON string and deserialized any tagged objects."""
|
||||||
return loads(value, object_hook=self.untag)
|
return self._untag_scan(loads(value))
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from werkzeug.local import LocalProxy
|
|||||||
from .globals import request
|
from .globals import request
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
from .app import Flask
|
from .sansio.app import App
|
||||||
|
|
||||||
|
|
||||||
@LocalProxy
|
@LocalProxy
|
||||||
@@ -22,7 +22,10 @@ def wsgi_errors_stream() -> t.TextIO:
|
|||||||
can't import this directly, you can refer to it as
|
can't import this directly, you can refer to it as
|
||||||
``ext://flask.logging.wsgi_errors_stream``.
|
``ext://flask.logging.wsgi_errors_stream``.
|
||||||
"""
|
"""
|
||||||
return request.environ["wsgi.errors"] if request else sys.stderr
|
if request:
|
||||||
|
return request.environ["wsgi.errors"] # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
return sys.stderr
|
||||||
|
|
||||||
|
|
||||||
def has_level_handler(logger: logging.Logger) -> bool:
|
def has_level_handler(logger: logging.Logger) -> bool:
|
||||||
@@ -52,7 +55,7 @@ default_handler.setFormatter(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def create_logger(app: Flask) -> logging.Logger:
|
def create_logger(app: App) -> logging.Logger:
|
||||||
"""Get the Flask app's logger and configure it if needed.
|
"""Get the Flask app's logger and configure it if needed.
|
||||||
|
|
||||||
The logger name will be the same as
|
The logger name will be the same as
|
||||||
|
|||||||
@@ -1,873 +0,0 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import importlib.util
|
|
||||||
import os
|
|
||||||
import pathlib
|
|
||||||
import sys
|
|
||||||
import typing as t
|
|
||||||
from collections import defaultdict
|
|
||||||
from datetime import timedelta
|
|
||||||
from functools import update_wrapper
|
|
||||||
|
|
||||||
from jinja2 import FileSystemLoader
|
|
||||||
from werkzeug.exceptions import default_exceptions
|
|
||||||
from werkzeug.exceptions import HTTPException
|
|
||||||
from werkzeug.utils import cached_property
|
|
||||||
|
|
||||||
from . import typing as ft
|
|
||||||
from .cli import AppGroup
|
|
||||||
from .globals import current_app
|
|
||||||
from .helpers import get_root_path
|
|
||||||
from .helpers import send_from_directory
|
|
||||||
from .templating import _default_template_ctx_processor
|
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
|
||||||
from .wrappers import Response
|
|
||||||
|
|
||||||
# a singleton sentinel value for parameter defaults
|
|
||||||
_sentinel = object()
|
|
||||||
|
|
||||||
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
|
|
||||||
T_after_request = t.TypeVar("T_after_request", bound=ft.AfterRequestCallable)
|
|
||||||
T_before_request = t.TypeVar("T_before_request", bound=ft.BeforeRequestCallable)
|
|
||||||
T_error_handler = t.TypeVar("T_error_handler", bound=ft.ErrorHandlerCallable)
|
|
||||||
T_teardown = t.TypeVar("T_teardown", bound=ft.TeardownCallable)
|
|
||||||
T_template_context_processor = t.TypeVar(
|
|
||||||
"T_template_context_processor", bound=ft.TemplateContextProcessorCallable
|
|
||||||
)
|
|
||||||
T_url_defaults = t.TypeVar("T_url_defaults", bound=ft.URLDefaultCallable)
|
|
||||||
T_url_value_preprocessor = t.TypeVar(
|
|
||||||
"T_url_value_preprocessor", bound=ft.URLValuePreprocessorCallable
|
|
||||||
)
|
|
||||||
T_route = t.TypeVar("T_route", bound=ft.RouteCallable)
|
|
||||||
|
|
||||||
|
|
||||||
def setupmethod(f: F) -> F:
|
|
||||||
f_name = f.__name__
|
|
||||||
|
|
||||||
def wrapper_func(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
|
|
||||||
self._check_setup_finished(f_name)
|
|
||||||
return f(self, *args, **kwargs)
|
|
||||||
|
|
||||||
return t.cast(F, update_wrapper(wrapper_func, f))
|
|
||||||
|
|
||||||
|
|
||||||
class Scaffold:
|
|
||||||
"""Common behavior shared between :class:`~flask.Flask` and
|
|
||||||
:class:`~flask.blueprints.Blueprint`.
|
|
||||||
|
|
||||||
:param import_name: The import name of the module where this object
|
|
||||||
is defined. Usually :attr:`__name__` should be used.
|
|
||||||
:param static_folder: Path to a folder of static files to serve.
|
|
||||||
If this is set, a static route will be added.
|
|
||||||
:param static_url_path: URL prefix for the static route.
|
|
||||||
:param template_folder: Path to a folder containing template files.
|
|
||||||
for rendering. If this is set, a Jinja loader will be added.
|
|
||||||
:param root_path: The path that static, template, and resource files
|
|
||||||
are relative to. Typically not set, it is discovered based on
|
|
||||||
the ``import_name``.
|
|
||||||
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
"""
|
|
||||||
|
|
||||||
name: str
|
|
||||||
_static_folder: str | None = None
|
|
||||||
_static_url_path: str | None = None
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
import_name: str,
|
|
||||||
static_folder: str | os.PathLike | None = None,
|
|
||||||
static_url_path: str | None = None,
|
|
||||||
template_folder: str | os.PathLike | None = None,
|
|
||||||
root_path: str | None = None,
|
|
||||||
):
|
|
||||||
#: The name of the package or module that this object belongs
|
|
||||||
#: to. Do not change this once it is set by the constructor.
|
|
||||||
self.import_name = import_name
|
|
||||||
|
|
||||||
self.static_folder = static_folder # type: ignore
|
|
||||||
self.static_url_path = static_url_path
|
|
||||||
|
|
||||||
#: The path to the templates folder, relative to
|
|
||||||
#: :attr:`root_path`, to add to the template loader. ``None`` if
|
|
||||||
#: templates should not be added.
|
|
||||||
self.template_folder = template_folder
|
|
||||||
|
|
||||||
if root_path is None:
|
|
||||||
root_path = get_root_path(self.import_name)
|
|
||||||
|
|
||||||
#: Absolute path to the package on the filesystem. Used to look
|
|
||||||
#: up resources contained in the package.
|
|
||||||
self.root_path = root_path
|
|
||||||
|
|
||||||
#: The Click command group for registering CLI commands for this
|
|
||||||
#: object. The commands are available from the ``flask`` command
|
|
||||||
#: once the application has been discovered and blueprints have
|
|
||||||
#: been registered.
|
|
||||||
self.cli = AppGroup()
|
|
||||||
|
|
||||||
#: A dictionary mapping endpoint names to view functions.
|
|
||||||
#:
|
|
||||||
#: To register a view function, use the :meth:`route` decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.view_functions: dict[str, t.Callable] = {}
|
|
||||||
|
|
||||||
#: A data structure of registered error handlers, in the format
|
|
||||||
#: ``{scope: {code: {class: handler}}}``. The ``scope`` key is
|
|
||||||
#: the name of a blueprint the handlers are active for, or
|
|
||||||
#: ``None`` for all requests. The ``code`` key is the HTTP
|
|
||||||
#: status code for ``HTTPException``, or ``None`` for
|
|
||||||
#: other exceptions. The innermost dictionary maps exception
|
|
||||||
#: classes to handler functions.
|
|
||||||
#:
|
|
||||||
#: To register an error handler, use the :meth:`errorhandler`
|
|
||||||
#: decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.error_handler_spec: dict[
|
|
||||||
ft.AppOrBlueprintKey,
|
|
||||||
dict[int | None, dict[type[Exception], ft.ErrorHandlerCallable]],
|
|
||||||
] = defaultdict(lambda: defaultdict(dict))
|
|
||||||
|
|
||||||
#: A data structure of functions to call at the beginning of
|
|
||||||
#: each request, in the format ``{scope: [functions]}``. The
|
|
||||||
#: ``scope`` key is the name of a blueprint the functions are
|
|
||||||
#: active for, or ``None`` for all requests.
|
|
||||||
#:
|
|
||||||
#: To register a function, use the :meth:`before_request`
|
|
||||||
#: decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.before_request_funcs: dict[
|
|
||||||
ft.AppOrBlueprintKey, list[ft.BeforeRequestCallable]
|
|
||||||
] = defaultdict(list)
|
|
||||||
|
|
||||||
#: A data structure of functions to call at the end of each
|
|
||||||
#: request, in the format ``{scope: [functions]}``. The
|
|
||||||
#: ``scope`` key is the name of a blueprint the functions are
|
|
||||||
#: active for, or ``None`` for all requests.
|
|
||||||
#:
|
|
||||||
#: To register a function, use the :meth:`after_request`
|
|
||||||
#: decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.after_request_funcs: dict[
|
|
||||||
ft.AppOrBlueprintKey, list[ft.AfterRequestCallable]
|
|
||||||
] = defaultdict(list)
|
|
||||||
|
|
||||||
#: A data structure of functions to call at the end of each
|
|
||||||
#: request even if an exception is raised, in the format
|
|
||||||
#: ``{scope: [functions]}``. The ``scope`` key is the name of a
|
|
||||||
#: blueprint the functions are active for, or ``None`` for all
|
|
||||||
#: requests.
|
|
||||||
#:
|
|
||||||
#: To register a function, use the :meth:`teardown_request`
|
|
||||||
#: decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.teardown_request_funcs: dict[
|
|
||||||
ft.AppOrBlueprintKey, list[ft.TeardownCallable]
|
|
||||||
] = defaultdict(list)
|
|
||||||
|
|
||||||
#: A data structure of functions to call to pass extra context
|
|
||||||
#: values when rendering templates, in the format
|
|
||||||
#: ``{scope: [functions]}``. The ``scope`` key is the name of a
|
|
||||||
#: blueprint the functions are active for, or ``None`` for all
|
|
||||||
#: requests.
|
|
||||||
#:
|
|
||||||
#: To register a function, use the :meth:`context_processor`
|
|
||||||
#: decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.template_context_processors: dict[
|
|
||||||
ft.AppOrBlueprintKey, list[ft.TemplateContextProcessorCallable]
|
|
||||||
] = defaultdict(list, {None: [_default_template_ctx_processor]})
|
|
||||||
|
|
||||||
#: A data structure of functions to call to modify the keyword
|
|
||||||
#: arguments passed to the view function, in the format
|
|
||||||
#: ``{scope: [functions]}``. The ``scope`` key is the name of a
|
|
||||||
#: blueprint the functions are active for, or ``None`` for all
|
|
||||||
#: requests.
|
|
||||||
#:
|
|
||||||
#: To register a function, use the
|
|
||||||
#: :meth:`url_value_preprocessor` decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.url_value_preprocessors: dict[
|
|
||||||
ft.AppOrBlueprintKey,
|
|
||||||
list[ft.URLValuePreprocessorCallable],
|
|
||||||
] = defaultdict(list)
|
|
||||||
|
|
||||||
#: A data structure of functions to call to modify the keyword
|
|
||||||
#: arguments when generating URLs, in the format
|
|
||||||
#: ``{scope: [functions]}``. The ``scope`` key is the name of a
|
|
||||||
#: blueprint the functions are active for, or ``None`` for all
|
|
||||||
#: requests.
|
|
||||||
#:
|
|
||||||
#: To register a function, use the :meth:`url_defaults`
|
|
||||||
#: decorator.
|
|
||||||
#:
|
|
||||||
#: This data structure is internal. It should not be modified
|
|
||||||
#: directly and its format may change at any time.
|
|
||||||
self.url_default_functions: dict[
|
|
||||||
ft.AppOrBlueprintKey, list[ft.URLDefaultCallable]
|
|
||||||
] = defaultdict(list)
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
return f"<{type(self).__name__} {self.name!r}>"
|
|
||||||
|
|
||||||
def _check_setup_finished(self, f_name: str) -> None:
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
@property
|
|
||||||
def static_folder(self) -> str | None:
|
|
||||||
"""The absolute path to the configured static folder. ``None``
|
|
||||||
if no static folder is set.
|
|
||||||
"""
|
|
||||||
if self._static_folder is not None:
|
|
||||||
return os.path.join(self.root_path, self._static_folder)
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
@static_folder.setter
|
|
||||||
def static_folder(self, value: str | os.PathLike | None) -> None:
|
|
||||||
if value is not None:
|
|
||||||
value = os.fspath(value).rstrip(r"\/")
|
|
||||||
|
|
||||||
self._static_folder = value
|
|
||||||
|
|
||||||
@property
|
|
||||||
def has_static_folder(self) -> bool:
|
|
||||||
"""``True`` if :attr:`static_folder` is set.
|
|
||||||
|
|
||||||
.. versionadded:: 0.5
|
|
||||||
"""
|
|
||||||
return self.static_folder is not None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def static_url_path(self) -> str | None:
|
|
||||||
"""The URL prefix that the static route will be accessible from.
|
|
||||||
|
|
||||||
If it was not configured during init, it is derived from
|
|
||||||
:attr:`static_folder`.
|
|
||||||
"""
|
|
||||||
if self._static_url_path is not None:
|
|
||||||
return self._static_url_path
|
|
||||||
|
|
||||||
if self.static_folder is not None:
|
|
||||||
basename = os.path.basename(self.static_folder)
|
|
||||||
return f"/{basename}".rstrip("/")
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
@static_url_path.setter
|
|
||||||
def static_url_path(self, value: str | None) -> None:
|
|
||||||
if value is not None:
|
|
||||||
value = value.rstrip("/")
|
|
||||||
|
|
||||||
self._static_url_path = value
|
|
||||||
|
|
||||||
def get_send_file_max_age(self, filename: str | None) -> int | None:
|
|
||||||
"""Used by :func:`send_file` to determine the ``max_age`` cache
|
|
||||||
value for a given file path if it wasn't passed.
|
|
||||||
|
|
||||||
By default, this returns :data:`SEND_FILE_MAX_AGE_DEFAULT` from
|
|
||||||
the configuration of :data:`~flask.current_app`. This defaults
|
|
||||||
to ``None``, which tells the browser to use conditional requests
|
|
||||||
instead of a timed cache, which is usually preferable.
|
|
||||||
|
|
||||||
.. versionchanged:: 2.0
|
|
||||||
The default configuration is ``None`` instead of 12 hours.
|
|
||||||
|
|
||||||
.. versionadded:: 0.9
|
|
||||||
"""
|
|
||||||
value = current_app.config["SEND_FILE_MAX_AGE_DEFAULT"]
|
|
||||||
|
|
||||||
if value is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if isinstance(value, timedelta):
|
|
||||||
return int(value.total_seconds())
|
|
||||||
|
|
||||||
return value
|
|
||||||
|
|
||||||
def send_static_file(self, filename: str) -> Response:
|
|
||||||
"""The view function used to serve files from
|
|
||||||
:attr:`static_folder`. A route is automatically registered for
|
|
||||||
this view at :attr:`static_url_path` if :attr:`static_folder` is
|
|
||||||
set.
|
|
||||||
|
|
||||||
.. versionadded:: 0.5
|
|
||||||
"""
|
|
||||||
if not self.has_static_folder:
|
|
||||||
raise RuntimeError("'static_folder' must be set to serve static_files.")
|
|
||||||
|
|
||||||
# send_file only knows to call get_send_file_max_age on the app,
|
|
||||||
# call it here so it works for blueprints too.
|
|
||||||
max_age = self.get_send_file_max_age(filename)
|
|
||||||
return send_from_directory(
|
|
||||||
t.cast(str, self.static_folder), filename, max_age=max_age
|
|
||||||
)
|
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def jinja_loader(self) -> FileSystemLoader | None:
|
|
||||||
"""The Jinja loader for this object's templates. By default this
|
|
||||||
is a class :class:`jinja2.loaders.FileSystemLoader` to
|
|
||||||
:attr:`template_folder` if it is set.
|
|
||||||
|
|
||||||
.. versionadded:: 0.5
|
|
||||||
"""
|
|
||||||
if self.template_folder is not None:
|
|
||||||
return FileSystemLoader(os.path.join(self.root_path, self.template_folder))
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def open_resource(self, resource: str, mode: str = "rb") -> t.IO[t.AnyStr]:
|
|
||||||
"""Open a resource file relative to :attr:`root_path` for
|
|
||||||
reading.
|
|
||||||
|
|
||||||
For example, if the file ``schema.sql`` is next to the file
|
|
||||||
``app.py`` where the ``Flask`` app is defined, it can be opened
|
|
||||||
with:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
with app.open_resource("schema.sql") as f:
|
|
||||||
conn.executescript(f.read())
|
|
||||||
|
|
||||||
:param resource: Path to the resource relative to
|
|
||||||
:attr:`root_path`.
|
|
||||||
:param mode: Open the file in this mode. Only reading is
|
|
||||||
supported, valid values are "r" (or "rt") and "rb".
|
|
||||||
"""
|
|
||||||
if mode not in {"r", "rt", "rb"}:
|
|
||||||
raise ValueError("Resources can only be opened for reading.")
|
|
||||||
|
|
||||||
return open(os.path.join(self.root_path, resource), mode)
|
|
||||||
|
|
||||||
def _method_route(
|
|
||||||
self,
|
|
||||||
method: str,
|
|
||||||
rule: str,
|
|
||||||
options: dict,
|
|
||||||
) -> t.Callable[[T_route], T_route]:
|
|
||||||
if "methods" in options:
|
|
||||||
raise TypeError("Use the 'route' decorator to use the 'methods' argument.")
|
|
||||||
|
|
||||||
return self.route(rule, methods=[method], **options)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def get(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]:
|
|
||||||
"""Shortcut for :meth:`route` with ``methods=["GET"]``.
|
|
||||||
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
"""
|
|
||||||
return self._method_route("GET", rule, options)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def post(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]:
|
|
||||||
"""Shortcut for :meth:`route` with ``methods=["POST"]``.
|
|
||||||
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
"""
|
|
||||||
return self._method_route("POST", rule, options)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def put(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]:
|
|
||||||
"""Shortcut for :meth:`route` with ``methods=["PUT"]``.
|
|
||||||
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
"""
|
|
||||||
return self._method_route("PUT", rule, options)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def delete(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]:
|
|
||||||
"""Shortcut for :meth:`route` with ``methods=["DELETE"]``.
|
|
||||||
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
"""
|
|
||||||
return self._method_route("DELETE", rule, options)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def patch(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]:
|
|
||||||
"""Shortcut for :meth:`route` with ``methods=["PATCH"]``.
|
|
||||||
|
|
||||||
.. versionadded:: 2.0
|
|
||||||
"""
|
|
||||||
return self._method_route("PATCH", rule, options)
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def route(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]:
|
|
||||||
"""Decorate a view function to register it with the given URL
|
|
||||||
rule and options. Calls :meth:`add_url_rule`, which has more
|
|
||||||
details about the implementation.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def index():
|
|
||||||
return "Hello, World!"
|
|
||||||
|
|
||||||
See :ref:`url-route-registrations`.
|
|
||||||
|
|
||||||
The endpoint name for the route defaults to the name of the view
|
|
||||||
function if the ``endpoint`` parameter isn't passed.
|
|
||||||
|
|
||||||
The ``methods`` parameter defaults to ``["GET"]``. ``HEAD`` and
|
|
||||||
``OPTIONS`` are added automatically.
|
|
||||||
|
|
||||||
:param rule: The URL rule string.
|
|
||||||
:param options: Extra options passed to the
|
|
||||||
:class:`~werkzeug.routing.Rule` object.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(f: T_route) -> T_route:
|
|
||||||
endpoint = options.pop("endpoint", None)
|
|
||||||
self.add_url_rule(rule, endpoint, f, **options)
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def add_url_rule(
|
|
||||||
self,
|
|
||||||
rule: str,
|
|
||||||
endpoint: str | None = None,
|
|
||||||
view_func: ft.RouteCallable | None = None,
|
|
||||||
provide_automatic_options: bool | None = None,
|
|
||||||
**options: t.Any,
|
|
||||||
) -> None:
|
|
||||||
"""Register a rule for routing incoming requests and building
|
|
||||||
URLs. The :meth:`route` decorator is a shortcut to call this
|
|
||||||
with the ``view_func`` argument. These are equivalent:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def index():
|
|
||||||
...
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
def index():
|
|
||||||
...
|
|
||||||
|
|
||||||
app.add_url_rule("/", view_func=index)
|
|
||||||
|
|
||||||
See :ref:`url-route-registrations`.
|
|
||||||
|
|
||||||
The endpoint name for the route defaults to the name of the view
|
|
||||||
function if the ``endpoint`` parameter isn't passed. An error
|
|
||||||
will be raised if a function has already been registered for the
|
|
||||||
endpoint.
|
|
||||||
|
|
||||||
The ``methods`` parameter defaults to ``["GET"]``. ``HEAD`` is
|
|
||||||
always added automatically, and ``OPTIONS`` is added
|
|
||||||
automatically by default.
|
|
||||||
|
|
||||||
``view_func`` does not necessarily need to be passed, but if the
|
|
||||||
rule should participate in routing an endpoint name must be
|
|
||||||
associated with a view function at some point with the
|
|
||||||
:meth:`endpoint` decorator.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.add_url_rule("/", endpoint="index")
|
|
||||||
|
|
||||||
@app.endpoint("index")
|
|
||||||
def index():
|
|
||||||
...
|
|
||||||
|
|
||||||
If ``view_func`` has a ``required_methods`` attribute, those
|
|
||||||
methods are added to the passed and automatic methods. If it
|
|
||||||
has a ``provide_automatic_methods`` attribute, it is used as the
|
|
||||||
default if the parameter is not passed.
|
|
||||||
|
|
||||||
:param rule: The URL rule string.
|
|
||||||
:param endpoint: The endpoint name to associate with the rule
|
|
||||||
and view function. Used when routing and building URLs.
|
|
||||||
Defaults to ``view_func.__name__``.
|
|
||||||
:param view_func: The view function to associate with the
|
|
||||||
endpoint name.
|
|
||||||
:param provide_automatic_options: Add the ``OPTIONS`` method and
|
|
||||||
respond to ``OPTIONS`` requests automatically.
|
|
||||||
:param options: Extra options passed to the
|
|
||||||
:class:`~werkzeug.routing.Rule` object.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def endpoint(self, endpoint: str) -> t.Callable[[F], F]:
|
|
||||||
"""Decorate a view function to register it for the given
|
|
||||||
endpoint. Used if a rule is added without a ``view_func`` with
|
|
||||||
:meth:`add_url_rule`.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.add_url_rule("/ex", endpoint="example")
|
|
||||||
|
|
||||||
@app.endpoint("example")
|
|
||||||
def example():
|
|
||||||
...
|
|
||||||
|
|
||||||
:param endpoint: The endpoint name to associate with the view
|
|
||||||
function.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(f: F) -> F:
|
|
||||||
self.view_functions[endpoint] = f
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def before_request(self, f: T_before_request) -> T_before_request:
|
|
||||||
"""Register a function to run before each request.
|
|
||||||
|
|
||||||
For example, this can be used to open a database connection, or
|
|
||||||
to load the logged in user from the session.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.before_request
|
|
||||||
def load_user():
|
|
||||||
if "user_id" in session:
|
|
||||||
g.user = db.session.get(session["user_id"])
|
|
||||||
|
|
||||||
The function will be called without any arguments. If it returns
|
|
||||||
a non-``None`` value, the value is handled as if it was the
|
|
||||||
return value from the view, and further request handling is
|
|
||||||
stopped.
|
|
||||||
|
|
||||||
This is available on both app and blueprint objects. When used on an app, this
|
|
||||||
executes before every request. When used on a blueprint, this executes before
|
|
||||||
every request that the blueprint handles. To register with a blueprint and
|
|
||||||
execute before every request, use :meth:`.Blueprint.before_app_request`.
|
|
||||||
"""
|
|
||||||
self.before_request_funcs.setdefault(None, []).append(f)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def after_request(self, f: T_after_request) -> T_after_request:
|
|
||||||
"""Register a function to run after each request to this object.
|
|
||||||
|
|
||||||
The function is called with the response object, and must return
|
|
||||||
a response object. This allows the functions to modify or
|
|
||||||
replace the response before it is sent.
|
|
||||||
|
|
||||||
If a function raises an exception, any remaining
|
|
||||||
``after_request`` functions will not be called. Therefore, this
|
|
||||||
should not be used for actions that must execute, such as to
|
|
||||||
close resources. Use :meth:`teardown_request` for that.
|
|
||||||
|
|
||||||
This is available on both app and blueprint objects. When used on an app, this
|
|
||||||
executes after every request. When used on a blueprint, this executes after
|
|
||||||
every request that the blueprint handles. To register with a blueprint and
|
|
||||||
execute after every request, use :meth:`.Blueprint.after_app_request`.
|
|
||||||
"""
|
|
||||||
self.after_request_funcs.setdefault(None, []).append(f)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def teardown_request(self, f: T_teardown) -> T_teardown:
|
|
||||||
"""Register a function to be called when the request context is
|
|
||||||
popped. Typically this happens at the end of each request, but
|
|
||||||
contexts may be pushed manually as well during testing.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
with app.test_request_context():
|
|
||||||
...
|
|
||||||
|
|
||||||
When the ``with`` block exits (or ``ctx.pop()`` is called), the
|
|
||||||
teardown functions are called just before the request context is
|
|
||||||
made inactive.
|
|
||||||
|
|
||||||
When a teardown function was called because of an unhandled
|
|
||||||
exception it will be passed an error object. If an
|
|
||||||
:meth:`errorhandler` is registered, it will handle the exception
|
|
||||||
and the teardown will not receive it.
|
|
||||||
|
|
||||||
Teardown functions must avoid raising exceptions. If they
|
|
||||||
execute code that might fail they must surround that code with a
|
|
||||||
``try``/``except`` block and log any errors.
|
|
||||||
|
|
||||||
The return values of teardown functions are ignored.
|
|
||||||
|
|
||||||
This is available on both app and blueprint objects. When used on an app, this
|
|
||||||
executes after every request. When used on a blueprint, this executes after
|
|
||||||
every request that the blueprint handles. To register with a blueprint and
|
|
||||||
execute after every request, use :meth:`.Blueprint.teardown_app_request`.
|
|
||||||
"""
|
|
||||||
self.teardown_request_funcs.setdefault(None, []).append(f)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def context_processor(
|
|
||||||
self,
|
|
||||||
f: T_template_context_processor,
|
|
||||||
) -> T_template_context_processor:
|
|
||||||
"""Registers a template context processor function. These functions run before
|
|
||||||
rendering a template. The keys of the returned dict are added as variables
|
|
||||||
available in the template.
|
|
||||||
|
|
||||||
This is available on both app and blueprint objects. When used on an app, this
|
|
||||||
is called for every rendered template. When used on a blueprint, this is called
|
|
||||||
for templates rendered from the blueprint's views. To register with a blueprint
|
|
||||||
and affect every template, use :meth:`.Blueprint.app_context_processor`.
|
|
||||||
"""
|
|
||||||
self.template_context_processors[None].append(f)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def url_value_preprocessor(
|
|
||||||
self,
|
|
||||||
f: T_url_value_preprocessor,
|
|
||||||
) -> T_url_value_preprocessor:
|
|
||||||
"""Register a URL value preprocessor function for all view
|
|
||||||
functions in the application. These functions will be called before the
|
|
||||||
:meth:`before_request` functions.
|
|
||||||
|
|
||||||
The function can modify the values captured from the matched url before
|
|
||||||
they are passed to the view. For example, this can be used to pop a
|
|
||||||
common language code value and place it in ``g`` rather than pass it to
|
|
||||||
every view.
|
|
||||||
|
|
||||||
The function is passed the endpoint name and values dict. The return
|
|
||||||
value is ignored.
|
|
||||||
|
|
||||||
This is available on both app and blueprint objects. When used on an app, this
|
|
||||||
is called for every request. When used on a blueprint, this is called for
|
|
||||||
requests that the blueprint handles. To register with a blueprint and affect
|
|
||||||
every request, use :meth:`.Blueprint.app_url_value_preprocessor`.
|
|
||||||
"""
|
|
||||||
self.url_value_preprocessors[None].append(f)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def url_defaults(self, f: T_url_defaults) -> T_url_defaults:
|
|
||||||
"""Callback function for URL defaults for all view functions of the
|
|
||||||
application. It's called with the endpoint and values and should
|
|
||||||
update the values passed in place.
|
|
||||||
|
|
||||||
This is available on both app and blueprint objects. When used on an app, this
|
|
||||||
is called for every request. When used on a blueprint, this is called for
|
|
||||||
requests that the blueprint handles. To register with a blueprint and affect
|
|
||||||
every request, use :meth:`.Blueprint.app_url_defaults`.
|
|
||||||
"""
|
|
||||||
self.url_default_functions[None].append(f)
|
|
||||||
return f
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def errorhandler(
|
|
||||||
self, code_or_exception: type[Exception] | int
|
|
||||||
) -> t.Callable[[T_error_handler], T_error_handler]:
|
|
||||||
"""Register a function to handle errors by code or exception class.
|
|
||||||
|
|
||||||
A decorator that is used to register a function given an
|
|
||||||
error code. Example::
|
|
||||||
|
|
||||||
@app.errorhandler(404)
|
|
||||||
def page_not_found(error):
|
|
||||||
return 'This page does not exist', 404
|
|
||||||
|
|
||||||
You can also register handlers for arbitrary exceptions::
|
|
||||||
|
|
||||||
@app.errorhandler(DatabaseError)
|
|
||||||
def special_exception_handler(error):
|
|
||||||
return 'Database connection failed', 500
|
|
||||||
|
|
||||||
This is available on both app and blueprint objects. When used on an app, this
|
|
||||||
can handle errors from every request. When used on a blueprint, this can handle
|
|
||||||
errors from requests that the blueprint handles. To register with a blueprint
|
|
||||||
and affect every request, use :meth:`.Blueprint.app_errorhandler`.
|
|
||||||
|
|
||||||
.. versionadded:: 0.7
|
|
||||||
Use :meth:`register_error_handler` instead of modifying
|
|
||||||
:attr:`error_handler_spec` directly, for application wide error
|
|
||||||
handlers.
|
|
||||||
|
|
||||||
.. versionadded:: 0.7
|
|
||||||
One can now additionally also register custom exception types
|
|
||||||
that do not necessarily have to be a subclass of the
|
|
||||||
:class:`~werkzeug.exceptions.HTTPException` class.
|
|
||||||
|
|
||||||
:param code_or_exception: the code as integer for the handler, or
|
|
||||||
an arbitrary exception
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(f: T_error_handler) -> T_error_handler:
|
|
||||||
self.register_error_handler(code_or_exception, f)
|
|
||||||
return f
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
@setupmethod
|
|
||||||
def register_error_handler(
|
|
||||||
self,
|
|
||||||
code_or_exception: type[Exception] | int,
|
|
||||||
f: ft.ErrorHandlerCallable,
|
|
||||||
) -> None:
|
|
||||||
"""Alternative error attach function to the :meth:`errorhandler`
|
|
||||||
decorator that is more straightforward to use for non decorator
|
|
||||||
usage.
|
|
||||||
|
|
||||||
.. versionadded:: 0.7
|
|
||||||
"""
|
|
||||||
exc_class, code = self._get_exc_class_and_code(code_or_exception)
|
|
||||||
self.error_handler_spec[None][code][exc_class] = f
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _get_exc_class_and_code(
|
|
||||||
exc_class_or_code: type[Exception] | int,
|
|
||||||
) -> tuple[type[Exception], int | None]:
|
|
||||||
"""Get the exception class being handled. For HTTP status codes
|
|
||||||
or ``HTTPException`` subclasses, return both the exception and
|
|
||||||
status code.
|
|
||||||
|
|
||||||
:param exc_class_or_code: Any exception class, or an HTTP status
|
|
||||||
code as an integer.
|
|
||||||
"""
|
|
||||||
exc_class: type[Exception]
|
|
||||||
|
|
||||||
if isinstance(exc_class_or_code, int):
|
|
||||||
try:
|
|
||||||
exc_class = default_exceptions[exc_class_or_code]
|
|
||||||
except KeyError:
|
|
||||||
raise ValueError(
|
|
||||||
f"'{exc_class_or_code}' is not a recognized HTTP"
|
|
||||||
" error code. Use a subclass of HTTPException with"
|
|
||||||
" that code instead."
|
|
||||||
) from None
|
|
||||||
else:
|
|
||||||
exc_class = exc_class_or_code
|
|
||||||
|
|
||||||
if isinstance(exc_class, Exception):
|
|
||||||
raise TypeError(
|
|
||||||
f"{exc_class!r} is an instance, not a class. Handlers"
|
|
||||||
" can only be registered for Exception classes or HTTP"
|
|
||||||
" error codes."
|
|
||||||
)
|
|
||||||
|
|
||||||
if not issubclass(exc_class, Exception):
|
|
||||||
raise ValueError(
|
|
||||||
f"'{exc_class.__name__}' is not a subclass of Exception."
|
|
||||||
" Handlers can only be registered for Exception classes"
|
|
||||||
" or HTTP error codes."
|
|
||||||
)
|
|
||||||
|
|
||||||
if issubclass(exc_class, HTTPException):
|
|
||||||
return exc_class, exc_class.code
|
|
||||||
else:
|
|
||||||
return exc_class, None
|
|
||||||
|
|
||||||
|
|
||||||
def _endpoint_from_view_func(view_func: t.Callable) -> str:
|
|
||||||
"""Internal helper that returns the default endpoint for a given
|
|
||||||
function. This always is the function name.
|
|
||||||
"""
|
|
||||||
assert view_func is not None, "expected view func if endpoint is not provided."
|
|
||||||
return view_func.__name__
|
|
||||||
|
|
||||||
|
|
||||||
def _path_is_relative_to(path: pathlib.PurePath, base: str) -> bool:
|
|
||||||
# Path.is_relative_to doesn't exist until Python 3.9
|
|
||||||
try:
|
|
||||||
path.relative_to(base)
|
|
||||||
return True
|
|
||||||
except ValueError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def _find_package_path(import_name):
|
|
||||||
"""Find the path that contains the package or module."""
|
|
||||||
root_mod_name, _, _ = import_name.partition(".")
|
|
||||||
|
|
||||||
try:
|
|
||||||
root_spec = importlib.util.find_spec(root_mod_name)
|
|
||||||
|
|
||||||
if root_spec is None:
|
|
||||||
raise ValueError("not found")
|
|
||||||
except (ImportError, ValueError):
|
|
||||||
# ImportError: the machinery told us it does not exist
|
|
||||||
# ValueError:
|
|
||||||
# - the module name was invalid
|
|
||||||
# - the module name is __main__
|
|
||||||
# - we raised `ValueError` due to `root_spec` being `None`
|
|
||||||
return os.getcwd()
|
|
||||||
|
|
||||||
if root_spec.origin in {"namespace", None}:
|
|
||||||
# namespace package
|
|
||||||
package_spec = importlib.util.find_spec(import_name)
|
|
||||||
|
|
||||||
if package_spec is not None and package_spec.submodule_search_locations:
|
|
||||||
# Pick the path in the namespace that contains the submodule.
|
|
||||||
package_path = pathlib.Path(
|
|
||||||
os.path.commonpath(package_spec.submodule_search_locations)
|
|
||||||
)
|
|
||||||
search_location = next(
|
|
||||||
location
|
|
||||||
for location in root_spec.submodule_search_locations
|
|
||||||
if _path_is_relative_to(package_path, location)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Pick the first path.
|
|
||||||
search_location = root_spec.submodule_search_locations[0]
|
|
||||||
|
|
||||||
return os.path.dirname(search_location)
|
|
||||||
elif root_spec.submodule_search_locations:
|
|
||||||
# package with __init__.py
|
|
||||||
return os.path.dirname(os.path.dirname(root_spec.origin))
|
|
||||||
else:
|
|
||||||
# module
|
|
||||||
return os.path.dirname(root_spec.origin)
|
|
||||||
|
|
||||||
|
|
||||||
def find_package(import_name: str):
|
|
||||||
"""Find the prefix that a package is installed under, and the path
|
|
||||||
that it would be imported from.
|
|
||||||
|
|
||||||
The prefix is the directory containing the standard directory
|
|
||||||
hierarchy (lib, bin, etc.). If the package is not installed to the
|
|
||||||
system (:attr:`sys.prefix`) or a virtualenv (``site-packages``),
|
|
||||||
``None`` is returned.
|
|
||||||
|
|
||||||
The path is the entry in :attr:`sys.path` that contains the package
|
|
||||||
for import. If the package is not installed, it's assumed that the
|
|
||||||
package was imported from the current working directory.
|
|
||||||
"""
|
|
||||||
package_path = _find_package_path(import_name)
|
|
||||||
py_prefix = os.path.abspath(sys.prefix)
|
|
||||||
|
|
||||||
# installed to the system
|
|
||||||
if _path_is_relative_to(pathlib.PurePath(package_path), py_prefix):
|
|
||||||
return py_prefix, package_path
|
|
||||||
|
|
||||||
site_parent, site_folder = os.path.split(package_path)
|
|
||||||
|
|
||||||
# installed to a virtualenv
|
|
||||||
if site_folder.lower() == "site-packages":
|
|
||||||
parent, folder = os.path.split(site_parent)
|
|
||||||
|
|
||||||
# Windows (prefix/lib/site-packages)
|
|
||||||
if folder.lower() == "lib":
|
|
||||||
return parent, package_path
|
|
||||||
|
|
||||||
# Unix (prefix/lib/pythonX.Y/site-packages)
|
|
||||||
if os.path.basename(parent).lower() == "lib":
|
|
||||||
return os.path.dirname(parent), package_path
|
|
||||||
|
|
||||||
# something else (prefix/site-packages)
|
|
||||||
return site_parent, package_path
|
|
||||||
|
|
||||||
# not installed
|
|
||||||
return None, package_path
|
|
||||||
@@ -13,11 +13,15 @@ from werkzeug.datastructures import CallbackDict
|
|||||||
from .json.tag import TaggedJSONSerializer
|
from .json.tag import TaggedJSONSerializer
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
|
import typing_extensions as te
|
||||||
|
|
||||||
from .app import Flask
|
from .app import Flask
|
||||||
from .wrappers import Request, Response
|
from .wrappers import Request
|
||||||
|
from .wrappers import Response
|
||||||
|
|
||||||
|
|
||||||
class SessionMixin(MutableMapping):
|
# TODO generic when Python > 3.8
|
||||||
|
class SessionMixin(MutableMapping): # type: ignore[type-arg]
|
||||||
"""Expands a basic dictionary with session attributes."""
|
"""Expands a basic dictionary with session attributes."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -45,7 +49,8 @@ class SessionMixin(MutableMapping):
|
|||||||
accessed = True
|
accessed = True
|
||||||
|
|
||||||
|
|
||||||
class SecureCookieSession(CallbackDict, SessionMixin):
|
# TODO generic when Python > 3.8
|
||||||
|
class SecureCookieSession(CallbackDict, SessionMixin): # type: ignore[type-arg]
|
||||||
"""Base class for sessions based on signed cookies.
|
"""Base class for sessions based on signed cookies.
|
||||||
|
|
||||||
This session backend will set the :attr:`modified` and
|
This session backend will set the :attr:`modified` and
|
||||||
@@ -68,7 +73,7 @@ class SecureCookieSession(CallbackDict, SessionMixin):
|
|||||||
accessed = False
|
accessed = False
|
||||||
|
|
||||||
def __init__(self, initial: t.Any = None) -> None:
|
def __init__(self, initial: t.Any = None) -> None:
|
||||||
def on_update(self) -> None:
|
def on_update(self: te.Self) -> None:
|
||||||
self.modified = True
|
self.modified = True
|
||||||
self.accessed = True
|
self.accessed = True
|
||||||
|
|
||||||
@@ -177,7 +182,7 @@ class SessionInterface:
|
|||||||
|
|
||||||
def get_cookie_name(self, app: Flask) -> str:
|
def get_cookie_name(self, app: Flask) -> str:
|
||||||
"""The name of the session cookie. Uses``app.config["SESSION_COOKIE_NAME"]``."""
|
"""The name of the session cookie. Uses``app.config["SESSION_COOKIE_NAME"]``."""
|
||||||
return app.config["SESSION_COOKIE_NAME"]
|
return app.config["SESSION_COOKIE_NAME"] # type: ignore[no-any-return]
|
||||||
|
|
||||||
def get_cookie_domain(self, app: Flask) -> str | None:
|
def get_cookie_domain(self, app: Flask) -> str | None:
|
||||||
"""The value of the ``Domain`` parameter on the session cookie. If not set,
|
"""The value of the ``Domain`` parameter on the session cookie. If not set,
|
||||||
@@ -189,8 +194,7 @@ class SessionInterface:
|
|||||||
.. versionchanged:: 2.3
|
.. versionchanged:: 2.3
|
||||||
Not set by default, does not fall back to ``SERVER_NAME``.
|
Not set by default, does not fall back to ``SERVER_NAME``.
|
||||||
"""
|
"""
|
||||||
rv = app.config["SESSION_COOKIE_DOMAIN"]
|
return app.config["SESSION_COOKIE_DOMAIN"] # type: ignore[no-any-return]
|
||||||
return rv if rv else None
|
|
||||||
|
|
||||||
def get_cookie_path(self, app: Flask) -> str:
|
def get_cookie_path(self, app: Flask) -> str:
|
||||||
"""Returns the path for which the cookie should be valid. The
|
"""Returns the path for which the cookie should be valid. The
|
||||||
@@ -198,27 +202,27 @@ class SessionInterface:
|
|||||||
config var if it's set, and falls back to ``APPLICATION_ROOT`` or
|
config var if it's set, and falls back to ``APPLICATION_ROOT`` or
|
||||||
uses ``/`` if it's ``None``.
|
uses ``/`` if it's ``None``.
|
||||||
"""
|
"""
|
||||||
return app.config["SESSION_COOKIE_PATH"] or app.config["APPLICATION_ROOT"]
|
return app.config["SESSION_COOKIE_PATH"] or app.config["APPLICATION_ROOT"] # type: ignore[no-any-return]
|
||||||
|
|
||||||
def get_cookie_httponly(self, app: Flask) -> bool:
|
def get_cookie_httponly(self, app: Flask) -> bool:
|
||||||
"""Returns True if the session cookie should be httponly. This
|
"""Returns True if the session cookie should be httponly. This
|
||||||
currently just returns the value of the ``SESSION_COOKIE_HTTPONLY``
|
currently just returns the value of the ``SESSION_COOKIE_HTTPONLY``
|
||||||
config var.
|
config var.
|
||||||
"""
|
"""
|
||||||
return app.config["SESSION_COOKIE_HTTPONLY"]
|
return app.config["SESSION_COOKIE_HTTPONLY"] # type: ignore[no-any-return]
|
||||||
|
|
||||||
def get_cookie_secure(self, app: Flask) -> bool:
|
def get_cookie_secure(self, app: Flask) -> bool:
|
||||||
"""Returns True if the cookie should be secure. This currently
|
"""Returns True if the cookie should be secure. This currently
|
||||||
just returns the value of the ``SESSION_COOKIE_SECURE`` setting.
|
just returns the value of the ``SESSION_COOKIE_SECURE`` setting.
|
||||||
"""
|
"""
|
||||||
return app.config["SESSION_COOKIE_SECURE"]
|
return app.config["SESSION_COOKIE_SECURE"] # type: ignore[no-any-return]
|
||||||
|
|
||||||
def get_cookie_samesite(self, app: Flask) -> str:
|
def get_cookie_samesite(self, app: Flask) -> str | None:
|
||||||
"""Return ``'Strict'`` or ``'Lax'`` if the cookie should use the
|
"""Return ``'Strict'`` or ``'Lax'`` if the cookie should use the
|
||||||
``SameSite`` attribute. This currently just returns the value of
|
``SameSite`` attribute. This currently just returns the value of
|
||||||
the :data:`SESSION_COOKIE_SAMESITE` setting.
|
the :data:`SESSION_COOKIE_SAMESITE` setting.
|
||||||
"""
|
"""
|
||||||
return app.config["SESSION_COOKIE_SAMESITE"]
|
return app.config["SESSION_COOKIE_SAMESITE"] # type: ignore[no-any-return]
|
||||||
|
|
||||||
def get_expiration_time(self, app: Flask, session: SessionMixin) -> datetime | None:
|
def get_expiration_time(self, app: Flask, session: SessionMixin) -> datetime | None:
|
||||||
"""A helper method that returns an expiration date for the session
|
"""A helper method that returns an expiration date for the session
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import typing as t
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
from blinker import Namespace
|
from blinker import Namespace
|
||||||
|
|
||||||
# This namespace is only for signals provided by Flask itself.
|
# This namespace is only for signals provided by Flask itself.
|
||||||
@@ -18,16 +15,3 @@ appcontext_tearing_down = _signals.signal("appcontext-tearing-down")
|
|||||||
appcontext_pushed = _signals.signal("appcontext-pushed")
|
appcontext_pushed = _signals.signal("appcontext-pushed")
|
||||||
appcontext_popped = _signals.signal("appcontext-popped")
|
appcontext_popped = _signals.signal("appcontext-popped")
|
||||||
message_flashed = _signals.signal("message-flashed")
|
message_flashed = _signals.signal("message-flashed")
|
||||||
|
|
||||||
|
|
||||||
def __getattr__(name: str) -> t.Any:
|
|
||||||
if name == "signals_available":
|
|
||||||
warnings.warn(
|
|
||||||
"The 'signals_available' attribute is deprecated and will be removed in"
|
|
||||||
" Flask 2.4. Signals are always available.",
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
raise AttributeError(name)
|
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ from .signals import template_rendered
|
|||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
from .app import Flask
|
from .app import Flask
|
||||||
from .scaffold import Scaffold
|
from .sansio.app import App
|
||||||
|
from .sansio.scaffold import Scaffold
|
||||||
|
|
||||||
|
|
||||||
def _default_template_ctx_processor() -> dict[str, t.Any]:
|
def _default_template_ctx_processor() -> dict[str, t.Any]:
|
||||||
@@ -41,7 +42,7 @@ class Environment(BaseEnvironment):
|
|||||||
name of the blueprint to referenced templates if necessary.
|
name of the blueprint to referenced templates if necessary.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, app: Flask, **options: t.Any) -> None:
|
def __init__(self, app: App, **options: t.Any) -> None:
|
||||||
if "loader" not in options:
|
if "loader" not in options:
|
||||||
options["loader"] = app.create_global_jinja_loader()
|
options["loader"] = app.create_global_jinja_loader()
|
||||||
BaseEnvironment.__init__(self, **options)
|
BaseEnvironment.__init__(self, **options)
|
||||||
@@ -53,19 +54,19 @@ class DispatchingJinjaLoader(BaseLoader):
|
|||||||
the blueprint folders.
|
the blueprint folders.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, app: Flask) -> None:
|
def __init__(self, app: App) -> None:
|
||||||
self.app = app
|
self.app = app
|
||||||
|
|
||||||
def get_source( # type: ignore
|
def get_source(
|
||||||
self, environment: Environment, template: str
|
self, environment: BaseEnvironment, template: str
|
||||||
) -> tuple[str, str | None, t.Callable | None]:
|
) -> tuple[str, str | None, t.Callable[[], bool] | None]:
|
||||||
if self.app.config["EXPLAIN_TEMPLATE_LOADING"]:
|
if self.app.config["EXPLAIN_TEMPLATE_LOADING"]:
|
||||||
return self._get_source_explained(environment, template)
|
return self._get_source_explained(environment, template)
|
||||||
return self._get_source_fast(environment, template)
|
return self._get_source_fast(environment, template)
|
||||||
|
|
||||||
def _get_source_explained(
|
def _get_source_explained(
|
||||||
self, environment: Environment, template: str
|
self, environment: BaseEnvironment, template: str
|
||||||
) -> tuple[str, str | None, t.Callable | None]:
|
) -> tuple[str, str | None, t.Callable[[], bool] | None]:
|
||||||
attempts = []
|
attempts = []
|
||||||
rv: tuple[str, str | None, t.Callable[[], bool] | None] | None
|
rv: tuple[str, str | None, t.Callable[[], bool] | None] | None
|
||||||
trv: None | (tuple[str, str | None, t.Callable[[], bool] | None]) = None
|
trv: None | (tuple[str, str | None, t.Callable[[], bool] | None]) = None
|
||||||
@@ -88,8 +89,8 @@ class DispatchingJinjaLoader(BaseLoader):
|
|||||||
raise TemplateNotFound(template)
|
raise TemplateNotFound(template)
|
||||||
|
|
||||||
def _get_source_fast(
|
def _get_source_fast(
|
||||||
self, environment: Environment, template: str
|
self, environment: BaseEnvironment, template: str
|
||||||
) -> tuple[str, str | None, t.Callable | None]:
|
) -> tuple[str, str | None, t.Callable[[], bool] | None]:
|
||||||
for _srcobj, loader in self._iter_loaders(template):
|
for _srcobj, loader in self._iter_loaders(template):
|
||||||
try:
|
try:
|
||||||
return loader.get_source(environment, template)
|
return loader.get_source(environment, template)
|
||||||
@@ -97,9 +98,7 @@ class DispatchingJinjaLoader(BaseLoader):
|
|||||||
continue
|
continue
|
||||||
raise TemplateNotFound(template)
|
raise TemplateNotFound(template)
|
||||||
|
|
||||||
def _iter_loaders(
|
def _iter_loaders(self, template: str) -> t.Iterator[tuple[Scaffold, BaseLoader]]:
|
||||||
self, template: str
|
|
||||||
) -> t.Generator[tuple[Scaffold, BaseLoader], None, None]:
|
|
||||||
loader = self.app.jinja_loader
|
loader = self.app.jinja_loader
|
||||||
if loader is not None:
|
if loader is not None:
|
||||||
yield self.app, loader
|
yield self.app, loader
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ from .cli import ScriptInfo
|
|||||||
from .sessions import SessionMixin
|
from .sessions import SessionMixin
|
||||||
|
|
||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
|
from _typeshed.wsgi import WSGIEnvironment
|
||||||
from werkzeug.test import TestResponse
|
from werkzeug.test import TestResponse
|
||||||
|
|
||||||
from .app import Flask
|
from .app import Flask
|
||||||
@@ -134,7 +135,7 @@ class FlaskClient(Client):
|
|||||||
@contextmanager
|
@contextmanager
|
||||||
def session_transaction(
|
def session_transaction(
|
||||||
self, *args: t.Any, **kwargs: t.Any
|
self, *args: t.Any, **kwargs: t.Any
|
||||||
) -> t.Generator[SessionMixin, None, None]:
|
) -> t.Iterator[SessionMixin]:
|
||||||
"""When used in combination with a ``with`` statement this opens a
|
"""When used in combination with a ``with`` statement this opens a
|
||||||
session transaction. This can be used to modify the session that
|
session transaction. This can be used to modify the session that
|
||||||
the test client uses. Once the ``with`` block is left the session is
|
the test client uses. Once the ``with`` block is left the session is
|
||||||
@@ -181,7 +182,7 @@ class FlaskClient(Client):
|
|||||||
resp.headers.getlist("Set-Cookie"),
|
resp.headers.getlist("Set-Cookie"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _copy_environ(self, other):
|
def _copy_environ(self, other: WSGIEnvironment) -> WSGIEnvironment:
|
||||||
out = {**self.environ_base, **other}
|
out = {**self.environ_base, **other}
|
||||||
|
|
||||||
if self.preserve_context:
|
if self.preserve_context:
|
||||||
@@ -189,7 +190,9 @@ class FlaskClient(Client):
|
|||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def _request_from_builder_args(self, args, kwargs):
|
def _request_from_builder_args(
|
||||||
|
self, args: tuple[t.Any, ...], kwargs: dict[str, t.Any]
|
||||||
|
) -> BaseRequest:
|
||||||
kwargs["environ_base"] = self._copy_environ(kwargs.get("environ_base", {}))
|
kwargs["environ_base"] = self._copy_environ(kwargs.get("environ_base", {}))
|
||||||
builder = EnvironBuilder(self.application, *args, **kwargs)
|
builder = EnvironBuilder(self.application, *args, **kwargs)
|
||||||
|
|
||||||
@@ -210,7 +213,7 @@ class FlaskClient(Client):
|
|||||||
):
|
):
|
||||||
if isinstance(args[0], werkzeug.test.EnvironBuilder):
|
if isinstance(args[0], werkzeug.test.EnvironBuilder):
|
||||||
builder = copy(args[0])
|
builder = copy(args[0])
|
||||||
builder.environ_base = self._copy_environ(builder.environ_base or {})
|
builder.environ_base = self._copy_environ(builder.environ_base or {}) # type: ignore[arg-type]
|
||||||
request = builder.get_request()
|
request = builder.get_request()
|
||||||
elif isinstance(args[0], dict):
|
elif isinstance(args[0], dict):
|
||||||
request = EnvironBuilder.from_environ(
|
request = EnvironBuilder.from_environ(
|
||||||
@@ -287,7 +290,7 @@ class FlaskCliRunner(CliRunner):
|
|||||||
:return: a :class:`~click.testing.Result` object.
|
:return: a :class:`~click.testing.Result` object.
|
||||||
"""
|
"""
|
||||||
if cli is None:
|
if cli is None:
|
||||||
cli = self.app.cli # type: ignore
|
cli = self.app.cli
|
||||||
|
|
||||||
if "obj" not in kwargs:
|
if "obj" not in kwargs:
|
||||||
kwargs["obj"] = ScriptInfo(create_app=lambda: self.app)
|
kwargs["obj"] = ScriptInfo(create_app=lambda: self.app)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import typing as t
|
|||||||
if t.TYPE_CHECKING: # pragma: no cover
|
if t.TYPE_CHECKING: # pragma: no cover
|
||||||
from _typeshed.wsgi import WSGIApplication # noqa: F401
|
from _typeshed.wsgi import WSGIApplication # noqa: F401
|
||||||
from werkzeug.datastructures import Headers # noqa: F401
|
from werkzeug.datastructures import Headers # noqa: F401
|
||||||
from werkzeug.wrappers import Response # noqa: F401
|
from werkzeug.sansio.response import Response # noqa: F401
|
||||||
|
|
||||||
# The possible types that are directly convertible or are a Response object.
|
# The possible types that are directly convertible or are a Response object.
|
||||||
ResponseValue = t.Union[
|
ResponseValue = t.Union[
|
||||||
@@ -61,12 +61,17 @@ TeardownCallable = t.Union[
|
|||||||
t.Callable[[t.Optional[BaseException]], None],
|
t.Callable[[t.Optional[BaseException]], None],
|
||||||
t.Callable[[t.Optional[BaseException]], t.Awaitable[None]],
|
t.Callable[[t.Optional[BaseException]], t.Awaitable[None]],
|
||||||
]
|
]
|
||||||
TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]]
|
TemplateContextProcessorCallable = t.Union[
|
||||||
|
t.Callable[[], t.Dict[str, t.Any]],
|
||||||
|
t.Callable[[], t.Awaitable[t.Dict[str, t.Any]]],
|
||||||
|
]
|
||||||
TemplateFilterCallable = t.Callable[..., t.Any]
|
TemplateFilterCallable = t.Callable[..., t.Any]
|
||||||
TemplateGlobalCallable = t.Callable[..., t.Any]
|
TemplateGlobalCallable = t.Callable[..., t.Any]
|
||||||
TemplateTestCallable = t.Callable[..., bool]
|
TemplateTestCallable = t.Callable[..., bool]
|
||||||
URLDefaultCallable = t.Callable[[str, dict], None]
|
URLDefaultCallable = t.Callable[[str, t.Dict[str, t.Any]], None]
|
||||||
URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]
|
URLValuePreprocessorCallable = t.Callable[
|
||||||
|
[t.Optional[str], t.Optional[t.Dict[str, t.Any]]], None
|
||||||
|
]
|
||||||
|
|
||||||
# This should take Exception, but that either breaks typing the argument
|
# This should take Exception, but that either breaks typing the argument
|
||||||
# with a specific exception, or decorating multiple times with different
|
# with a specific exception, or decorating multiple times with different
|
||||||
@@ -74,7 +79,10 @@ URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], N
|
|||||||
# https://github.com/pallets/flask/issues/4095
|
# https://github.com/pallets/flask/issues/4095
|
||||||
# https://github.com/pallets/flask/issues/4295
|
# https://github.com/pallets/flask/issues/4295
|
||||||
# https://github.com/pallets/flask/issues/4297
|
# https://github.com/pallets/flask/issues/4297
|
||||||
ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue]
|
ErrorHandlerCallable = t.Union[
|
||||||
|
t.Callable[[t.Any], ResponseReturnValue],
|
||||||
|
t.Callable[[t.Any], t.Awaitable[ResponseReturnValue]],
|
||||||
|
]
|
||||||
|
|
||||||
RouteCallable = t.Union[
|
RouteCallable = t.Union[
|
||||||
t.Callable[..., ResponseReturnValue],
|
t.Callable[..., ResponseReturnValue],
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from . import typing as ft
|
|||||||
from .globals import current_app
|
from .globals import current_app
|
||||||
from .globals import request
|
from .globals import request
|
||||||
|
|
||||||
|
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
|
||||||
|
|
||||||
http_method_funcs = frozenset(
|
http_method_funcs = frozenset(
|
||||||
["get", "post", "head", "options", "delete", "put", "trace", "patch"]
|
["get", "post", "head", "options", "delete", "put", "trace", "patch"]
|
||||||
@@ -60,7 +61,7 @@ class View:
|
|||||||
#: decorator.
|
#: decorator.
|
||||||
#:
|
#:
|
||||||
#: .. versionadded:: 0.8
|
#: .. versionadded:: 0.8
|
||||||
decorators: t.ClassVar[list[t.Callable]] = []
|
decorators: t.ClassVar[list[t.Callable[[F], F]]] = []
|
||||||
|
|
||||||
#: Create a new instance of this view class for every request by
|
#: Create a new instance of this view class for every request by
|
||||||
#: default. If a view subclass sets this to ``False``, the same
|
#: default. If a view subclass sets this to ``False``, the same
|
||||||
@@ -106,13 +107,13 @@ class View:
|
|||||||
self = view.view_class( # type: ignore[attr-defined]
|
self = view.view_class( # type: ignore[attr-defined]
|
||||||
*class_args, **class_kwargs
|
*class_args, **class_kwargs
|
||||||
)
|
)
|
||||||
return current_app.ensure_sync(self.dispatch_request)(**kwargs)
|
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self = cls(*class_args, **class_kwargs)
|
self = cls(*class_args, **class_kwargs)
|
||||||
|
|
||||||
def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
|
def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
|
||||||
return current_app.ensure_sync(self.dispatch_request)(**kwargs)
|
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
|
||||||
|
|
||||||
if cls.decorators:
|
if cls.decorators:
|
||||||
view.__name__ = name
|
view.__name__ = name
|
||||||
@@ -187,4 +188,4 @@ class MethodView(View):
|
|||||||
meth = getattr(self, "get", None)
|
meth = getattr(self, "get", None)
|
||||||
|
|
||||||
assert meth is not None, f"Unimplemented method {request.method!r}"
|
assert meth is not None, f"Unimplemented method {request.method!r}"
|
||||||
return current_app.ensure_sync(meth)(**kwargs)
|
return current_app.ensure_sync(meth)(**kwargs) # type: ignore[no-any-return]
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
from werkzeug.exceptions import BadRequest
|
from werkzeug.exceptions import BadRequest
|
||||||
|
from werkzeug.exceptions import HTTPException
|
||||||
from werkzeug.wrappers import Request as RequestBase
|
from werkzeug.wrappers import Request as RequestBase
|
||||||
from werkzeug.wrappers import Response as ResponseBase
|
from werkzeug.wrappers import Response as ResponseBase
|
||||||
|
|
||||||
@@ -49,13 +50,13 @@ class Request(RequestBase):
|
|||||||
#: raised / was raised as part of the request handling. This is
|
#: raised / was raised as part of the request handling. This is
|
||||||
#: usually a :exc:`~werkzeug.exceptions.NotFound` exception or
|
#: usually a :exc:`~werkzeug.exceptions.NotFound` exception or
|
||||||
#: something similar.
|
#: something similar.
|
||||||
routing_exception: Exception | None = None
|
routing_exception: HTTPException | None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max_content_length(self) -> int | None: # type: ignore
|
def max_content_length(self) -> int | None: # type: ignore[override]
|
||||||
"""Read-only view of the ``MAX_CONTENT_LENGTH`` config key."""
|
"""Read-only view of the ``MAX_CONTENT_LENGTH`` config key."""
|
||||||
if current_app:
|
if current_app:
|
||||||
return current_app.config["MAX_CONTENT_LENGTH"]
|
return current_app.config["MAX_CONTENT_LENGTH"] # type: ignore[no-any-return]
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -167,7 +168,7 @@ class Response(ResponseBase):
|
|||||||
Werkzeug's docs.
|
Werkzeug's docs.
|
||||||
"""
|
"""
|
||||||
if current_app:
|
if current_app:
|
||||||
return current_app.config["MAX_COOKIE_SIZE"]
|
return current_app.config["MAX_COOKIE_SIZE"] # type: ignore[no-any-return]
|
||||||
|
|
||||||
# return Werkzeug's default when not in an app context
|
# return Werkzeug's default when not in an app context
|
||||||
return super().max_cookie_size
|
return super().max_cookie_size
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
pip
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
Copyright (c) 2014, Saurabh Kumar (python-dotenv), 2013, Ted Tieken (django-dotenv-rw), 2013, Jacob Kaplan-Moss (django-dotenv)
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification,
|
|
||||||
are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
- Redistributions of source code must retain the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
- Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
- Neither the name of django-dotenv nor the names of its contributors
|
|
||||||
may be used to endorse or promote products derived from this software
|
|
||||||
without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
||||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
||||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
||||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
||||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
||||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
||||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
@@ -1,667 +0,0 @@
|
|||||||
Metadata-Version: 2.1
|
|
||||||
Name: python-dotenv
|
|
||||||
Version: 1.0.0
|
|
||||||
Summary: Read key-value pairs from a .env file and set them as environment variables
|
|
||||||
Home-page: https://github.com/theskumar/python-dotenv
|
|
||||||
Author: Saurabh Kumar
|
|
||||||
Author-email: me+github@saurabh-kumar.com
|
|
||||||
License: BSD-3-Clause
|
|
||||||
Keywords: environment variables,deployments,settings,env,dotenv,configurations,python
|
|
||||||
Classifier: Development Status :: 5 - Production/Stable
|
|
||||||
Classifier: Programming Language :: Python
|
|
||||||
Classifier: Programming Language :: Python :: 3
|
|
||||||
Classifier: Programming Language :: Python :: 3.8
|
|
||||||
Classifier: Programming Language :: Python :: 3.9
|
|
||||||
Classifier: Programming Language :: Python :: 3.10
|
|
||||||
Classifier: Programming Language :: Python :: 3.11
|
|
||||||
Classifier: Programming Language :: Python :: 3.12
|
|
||||||
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
||||||
Classifier: Intended Audience :: Developers
|
|
||||||
Classifier: Intended Audience :: System Administrators
|
|
||||||
Classifier: License :: OSI Approved :: BSD License
|
|
||||||
Classifier: Operating System :: OS Independent
|
|
||||||
Classifier: Topic :: System :: Systems Administration
|
|
||||||
Classifier: Topic :: Utilities
|
|
||||||
Classifier: Environment :: Web Environment
|
|
||||||
Requires-Python: >=3.8
|
|
||||||
Description-Content-Type: text/markdown
|
|
||||||
License-File: LICENSE
|
|
||||||
Provides-Extra: cli
|
|
||||||
Requires-Dist: click (>=5.0) ; extra == 'cli'
|
|
||||||
|
|
||||||
# python-dotenv
|
|
||||||
|
|
||||||
[![Build Status][build_status_badge]][build_status_link]
|
|
||||||
[![PyPI version][pypi_badge]][pypi_link]
|
|
||||||
|
|
||||||
Python-dotenv reads key-value pairs from a `.env` file and can set them as environment
|
|
||||||
variables. It helps in the development of applications following the
|
|
||||||
[12-factor](http://12factor.net/) principles.
|
|
||||||
|
|
||||||
- [Getting Started](#getting-started)
|
|
||||||
- [Other Use Cases](#other-use-cases)
|
|
||||||
* [Load configuration without altering the environment](#load-configuration-without-altering-the-environment)
|
|
||||||
* [Parse configuration as a stream](#parse-configuration-as-a-stream)
|
|
||||||
* [Load .env files in IPython](#load-env-files-in-ipython)
|
|
||||||
- [Command-line Interface](#command-line-interface)
|
|
||||||
- [File format](#file-format)
|
|
||||||
* [Multiline values](#multiline-values)
|
|
||||||
* [Variable expansion](#variable-expansion)
|
|
||||||
- [Related Projects](#related-projects)
|
|
||||||
- [Acknowledgements](#acknowledgements)
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
|
|
||||||
```shell
|
|
||||||
pip install python-dotenv
|
|
||||||
```
|
|
||||||
|
|
||||||
If your application takes its configuration from environment variables, like a 12-factor
|
|
||||||
application, launching it in development is not very practical because you have to set
|
|
||||||
those environment variables yourself.
|
|
||||||
|
|
||||||
To help you with that, you can add Python-dotenv to your application to make it load the
|
|
||||||
configuration from a `.env` file when it is present (e.g. in development) while remaining
|
|
||||||
configurable via the environment:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
load_dotenv() # take environment variables from .env.
|
|
||||||
|
|
||||||
# Code of your application, which uses environment variables (e.g. from `os.environ` or
|
|
||||||
# `os.getenv`) as if they came from the actual environment.
|
|
||||||
```
|
|
||||||
|
|
||||||
By default, `load_dotenv` doesn't override existing environment variables.
|
|
||||||
|
|
||||||
To configure the development environment, add a `.env` in the root directory of your
|
|
||||||
project:
|
|
||||||
|
|
||||||
```
|
|
||||||
.
|
|
||||||
├── .env
|
|
||||||
└── foo.py
|
|
||||||
```
|
|
||||||
|
|
||||||
The syntax of `.env` files supported by python-dotenv is similar to that of Bash:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Development settings
|
|
||||||
DOMAIN=example.org
|
|
||||||
ADMIN_EMAIL=admin@${DOMAIN}
|
|
||||||
ROOT_URL=${DOMAIN}/app
|
|
||||||
```
|
|
||||||
|
|
||||||
If you use variables in values, ensure they are surrounded with `{` and `}`, like
|
|
||||||
`${DOMAIN}`, as bare variables such as `$DOMAIN` are not expanded.
|
|
||||||
|
|
||||||
You will probably want to add `.env` to your `.gitignore`, especially if it contains
|
|
||||||
secrets like a password.
|
|
||||||
|
|
||||||
See the section "File format" below for more information about what you can write in a
|
|
||||||
`.env` file.
|
|
||||||
|
|
||||||
## Other Use Cases
|
|
||||||
|
|
||||||
### Load configuration without altering the environment
|
|
||||||
|
|
||||||
The function `dotenv_values` works more or less the same way as `load_dotenv`, except it
|
|
||||||
doesn't touch the environment, it just returns a `dict` with the values parsed from the
|
|
||||||
`.env` file.
|
|
||||||
|
|
||||||
```python
|
|
||||||
from dotenv import dotenv_values
|
|
||||||
|
|
||||||
config = dotenv_values(".env") # config = {"USER": "foo", "EMAIL": "foo@example.org"}
|
|
||||||
```
|
|
||||||
|
|
||||||
This notably enables advanced configuration management:
|
|
||||||
|
|
||||||
```python
|
|
||||||
import os
|
|
||||||
from dotenv import dotenv_values
|
|
||||||
|
|
||||||
config = {
|
|
||||||
**dotenv_values(".env.shared"), # load shared development variables
|
|
||||||
**dotenv_values(".env.secret"), # load sensitive variables
|
|
||||||
**os.environ, # override loaded values with environment variables
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Parse configuration as a stream
|
|
||||||
|
|
||||||
`load_dotenv` and `dotenv_values` accept [streams][python_streams] via their `stream`
|
|
||||||
argument. It is thus possible to load the variables from sources other than the
|
|
||||||
filesystem (e.g. the network).
|
|
||||||
|
|
||||||
```python
|
|
||||||
from io import StringIO
|
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
config = StringIO("USER=foo\nEMAIL=foo@example.org")
|
|
||||||
load_dotenv(stream=config)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Load .env files in IPython
|
|
||||||
|
|
||||||
You can use dotenv in IPython. By default, it will use `find_dotenv` to search for a
|
|
||||||
`.env` file:
|
|
||||||
|
|
||||||
```python
|
|
||||||
%load_ext dotenv
|
|
||||||
%dotenv
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also specify a path:
|
|
||||||
|
|
||||||
```python
|
|
||||||
%dotenv relative/or/absolute/path/to/.env
|
|
||||||
```
|
|
||||||
|
|
||||||
Optional flags:
|
|
||||||
|
|
||||||
- `-o` to override existing variables.
|
|
||||||
- `-v` for increased verbosity.
|
|
||||||
|
|
||||||
## Command-line Interface
|
|
||||||
|
|
||||||
A CLI interface `dotenv` is also included, which helps you manipulate the `.env` file
|
|
||||||
without manually opening it.
|
|
||||||
|
|
||||||
```shell
|
|
||||||
$ pip install "python-dotenv[cli]"
|
|
||||||
$ dotenv set USER foo
|
|
||||||
$ dotenv set EMAIL foo@example.org
|
|
||||||
$ dotenv list
|
|
||||||
USER=foo
|
|
||||||
EMAIL=foo@example.org
|
|
||||||
$ dotenv list --format=json
|
|
||||||
{
|
|
||||||
"USER": "foo",
|
|
||||||
"EMAIL": "foo@example.org"
|
|
||||||
}
|
|
||||||
$ dotenv run -- python foo.py
|
|
||||||
```
|
|
||||||
|
|
||||||
Run `dotenv --help` for more information about the options and subcommands.
|
|
||||||
|
|
||||||
## File format
|
|
||||||
|
|
||||||
The format is not formally specified and still improves over time. That being said,
|
|
||||||
`.env` files should mostly look like Bash files.
|
|
||||||
|
|
||||||
Keys can be unquoted or single-quoted. Values can be unquoted, single- or double-quoted.
|
|
||||||
Spaces before and after keys, equal signs, and values are ignored. Values can be followed
|
|
||||||
by a comment. Lines can start with the `export` directive, which does not affect their
|
|
||||||
interpretation.
|
|
||||||
|
|
||||||
Allowed escape sequences:
|
|
||||||
|
|
||||||
- in single-quoted values: `\\`, `\'`
|
|
||||||
- in double-quoted values: `\\`, `\'`, `\"`, `\a`, `\b`, `\f`, `\n`, `\r`, `\t`, `\v`
|
|
||||||
|
|
||||||
### Multiline values
|
|
||||||
|
|
||||||
It is possible for single- or double-quoted values to span multiple lines. The following
|
|
||||||
examples are equivalent:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
FOO="first line
|
|
||||||
second line"
|
|
||||||
```
|
|
||||||
|
|
||||||
```bash
|
|
||||||
FOO="first line\nsecond line"
|
|
||||||
```
|
|
||||||
|
|
||||||
### Variable without a value
|
|
||||||
|
|
||||||
A variable can have no value:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
FOO
|
|
||||||
```
|
|
||||||
|
|
||||||
It results in `dotenv_values` associating that variable name with the value `None` (e.g.
|
|
||||||
`{"FOO": None}`. `load_dotenv`, on the other hand, simply ignores such variables.
|
|
||||||
|
|
||||||
This shouldn't be confused with `FOO=`, in which case the variable is associated with the
|
|
||||||
empty string.
|
|
||||||
|
|
||||||
### Variable expansion
|
|
||||||
|
|
||||||
Python-dotenv can interpolate variables using POSIX variable expansion.
|
|
||||||
|
|
||||||
With `load_dotenv(override=True)` or `dotenv_values()`, the value of a variable is the
|
|
||||||
first of the values defined in the following list:
|
|
||||||
|
|
||||||
- Value of that variable in the `.env` file.
|
|
||||||
- Value of that variable in the environment.
|
|
||||||
- Default value, if provided.
|
|
||||||
- Empty string.
|
|
||||||
|
|
||||||
With `load_dotenv(override=False)`, the value of a variable is the first of the values
|
|
||||||
defined in the following list:
|
|
||||||
|
|
||||||
- Value of that variable in the environment.
|
|
||||||
- Value of that variable in the `.env` file.
|
|
||||||
- Default value, if provided.
|
|
||||||
- Empty string.
|
|
||||||
|
|
||||||
## Related Projects
|
|
||||||
|
|
||||||
- [Honcho](https://github.com/nickstenning/honcho) - For managing
|
|
||||||
Procfile-based applications.
|
|
||||||
- [django-dotenv](https://github.com/jpadilla/django-dotenv)
|
|
||||||
- [django-environ](https://github.com/joke2k/django-environ)
|
|
||||||
- [django-environ-2](https://github.com/sergeyklay/django-environ-2)
|
|
||||||
- [django-configuration](https://github.com/jezdez/django-configurations)
|
|
||||||
- [dump-env](https://github.com/sobolevn/dump-env)
|
|
||||||
- [environs](https://github.com/sloria/environs)
|
|
||||||
- [dynaconf](https://github.com/rochacbruno/dynaconf)
|
|
||||||
- [parse_it](https://github.com/naorlivne/parse_it)
|
|
||||||
- [python-decouple](https://github.com/HBNetwork/python-decouple)
|
|
||||||
|
|
||||||
## Acknowledgements
|
|
||||||
|
|
||||||
This project is currently maintained by [Saurabh Kumar](https://saurabh-kumar.com) and
|
|
||||||
[Bertrand Bonnefoy-Claudet](https://github.com/bbc2) and would not have been possible
|
|
||||||
without the support of these [awesome
|
|
||||||
people](https://github.com/theskumar/python-dotenv/graphs/contributors).
|
|
||||||
|
|
||||||
[build_status_badge]: https://github.com/theskumar/python-dotenv/actions/workflows/test.yml/badge.svg
|
|
||||||
[build_status_link]: https://github.com/theskumar/python-dotenv/actions/workflows/test.yml
|
|
||||||
[pypi_badge]: https://badge.fury.io/py/python-dotenv.svg
|
|
||||||
[pypi_link]: http://badge.fury.io/py/python-dotenv
|
|
||||||
[python_streams]: https://docs.python.org/3/library/io.html
|
|
||||||
|
|
||||||
# Changelog
|
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
|
||||||
|
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this
|
|
||||||
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
||||||
|
|
||||||
## [1.0.0]
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
* Drop support for python 3.7, add python 3.12-dev (#449 by [@theskumar])
|
|
||||||
* Handle situations where the cwd does not exist. (#446 by [@jctanner])
|
|
||||||
|
|
||||||
## [0.21.1] - 2022-01-21
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
* Use Python 3.11 non-beta in CI (#438 by [@bbc2])
|
|
||||||
* Modernize variables code (#434 by [@Nougat-Waffle])
|
|
||||||
* Modernize main.py and parser.py code (#435 by [@Nougat-Waffle])
|
|
||||||
* Improve conciseness of cli.py and __init__.py (#439 by [@Nougat-Waffle])
|
|
||||||
* Improve error message for `get` and `list` commands when env file can't be opened (#441 by [@bbc2])
|
|
||||||
* Updated License to align with BSD OSI template (#433 by [@lsmith77])
|
|
||||||
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
* Fix Out-of-scope error when "dest" variable is undefined (#413 by [@theGOTOguy])
|
|
||||||
* Fix IPython test warning about deprecated `magic` (#440 by [@bbc2])
|
|
||||||
* Fix type hint for dotenv_path var, add StrPath alias (#432 by [@eaf])
|
|
||||||
|
|
||||||
## [0.21.0] - 2022-09-03
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
* CLI: add support for invocations via 'python -m'. (#395 by [@theskumar])
|
|
||||||
* `load_dotenv` function now returns `False`. (#388 by [@larsks])
|
|
||||||
* CLI: add --format= option to list command. (#407 by [@sammck])
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
* Drop Python 3.5 and 3.6 and upgrade GA (#393 by [@eggplants])
|
|
||||||
* Use `open` instead of `io.open`. (#389 by [@rabinadk1])
|
|
||||||
* Improve documentation for variables without a value (#390 by [@bbc2])
|
|
||||||
* Add `parse_it` to Related Projects (#410 by [@naorlivne])
|
|
||||||
* Update README.md (#415 by [@harveer07])
|
|
||||||
* Improve documentation with direct use of MkDocs (#398 by [@bbc2])
|
|
||||||
|
|
||||||
## [0.20.0] - 2022-03-24
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- Add `encoding` (`Optional[str]`) parameter to `get_key`, `set_key` and `unset_key`.
|
|
||||||
(#379 by [@bbc2])
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- Use dict to specify the `entry_points` parameter of `setuptools.setup` (#376 by
|
|
||||||
[@mgorny]).
|
|
||||||
- Don't build universal wheels (#387 by [@bbc2]).
|
|
||||||
|
|
||||||
## [0.19.2] - 2021-11-11
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- In `set_key`, add missing newline character before new entry if necessary. (#361 by
|
|
||||||
[@bbc2])
|
|
||||||
|
|
||||||
## [0.19.1] - 2021-08-09
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- Add support for Python 3.10. (#359 by [@theskumar])
|
|
||||||
|
|
||||||
## [0.19.0] - 2021-07-24
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- Require Python 3.5 or a later version. Python 2 and 3.4 are no longer supported. (#341
|
|
||||||
by [@bbc2]).
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- The `dotenv_path` argument of `set_key` and `unset_key` now has a type of `Union[str,
|
|
||||||
os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]).
|
|
||||||
- The `stream` argument of `load_dotenv` and `dotenv_values` can now be a text stream
|
|
||||||
(`IO[str]`), which includes values like `io.StringIO("foo")` and `open("file.env",
|
|
||||||
"r")` (#348 by [@bbc2]).
|
|
||||||
|
|
||||||
## [0.18.0] - 2021-06-20
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- Raise `ValueError` if `quote_mode` isn't one of `always`, `auto` or `never` in
|
|
||||||
`set_key` (#330 by [@bbc2]).
|
|
||||||
- When writing a value to a .env file with `set_key` or `dotenv set <key> <value>` (#330
|
|
||||||
by [@bbc2]):
|
|
||||||
- Use single quotes instead of double quotes.
|
|
||||||
- Don't strip surrounding quotes.
|
|
||||||
- In `auto` mode, don't add quotes if the value is only made of alphanumeric characters
|
|
||||||
(as determined by `string.isalnum`).
|
|
||||||
|
|
||||||
## [0.17.1] - 2021-04-29
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- Fixed tests for build environments relying on `PYTHONPATH` (#318 by [@befeleme]).
|
|
||||||
|
|
||||||
## [0.17.0] - 2021-04-02
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- Make `dotenv get <key>` only show the value, not `key=value` (#313 by [@bbc2]).
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- Add `--override`/`--no-override` option to `dotenv run` (#312 by [@zueve] and [@bbc2]).
|
|
||||||
|
|
||||||
## [0.16.0] - 2021-03-27
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- The default value of the `encoding` parameter for `load_dotenv` and `dotenv_values` is
|
|
||||||
now `"utf-8"` instead of `None` (#306 by [@bbc2]).
|
|
||||||
- Fix resolution order in variable expansion with `override=False` (#287 by [@bbc2]).
|
|
||||||
|
|
||||||
## [0.15.0] - 2020-10-28
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- Add `--export` option to `set` to make it prepend the binding with `export` (#270 by
|
|
||||||
[@jadutter]).
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- Make `set` command create the `.env` file in the current directory if no `.env` file was
|
|
||||||
found (#270 by [@jadutter]).
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- Fix potentially empty expanded value for duplicate key (#260 by [@bbc2]).
|
|
||||||
- Fix import error on Python 3.5.0 and 3.5.1 (#267 by [@gongqingkui]).
|
|
||||||
- Fix parsing of unquoted values containing several adjacent space or tab characters
|
|
||||||
(#277 by [@bbc2], review by [@x-yuri]).
|
|
||||||
|
|
||||||
## [0.14.0] - 2020-07-03
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- Privilege definition in file over the environment in variable expansion (#256 by
|
|
||||||
[@elbehery95]).
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- Improve error message for when file isn't found (#245 by [@snobu]).
|
|
||||||
- Use HTTPS URL in package meta data (#251 by [@ekohl]).
|
|
||||||
|
|
||||||
## [0.13.0] - 2020-04-16
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- Add support for a Bash-like default value in variable expansion (#248 by [@bbc2]).
|
|
||||||
|
|
||||||
## [0.12.0] - 2020-02-28
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- Use current working directory to find `.env` when bundled by PyInstaller (#213 by
|
|
||||||
[@gergelyk]).
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- Fix escaping of quoted values written by `set_key` (#236 by [@bbc2]).
|
|
||||||
- Fix `dotenv run` crashing on environment variables without values (#237 by [@yannham]).
|
|
||||||
- Remove warning when last line is empty (#238 by [@bbc2]).
|
|
||||||
|
|
||||||
## [0.11.0] - 2020-02-07
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- Add `interpolate` argument to `load_dotenv` and `dotenv_values` to disable interpolation
|
|
||||||
(#232 by [@ulyssessouza]).
|
|
||||||
|
|
||||||
**Changed**
|
|
||||||
|
|
||||||
- Use logging instead of warnings (#231 by [@bbc2]).
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- Fix installation in non-UTF-8 environments (#225 by [@altendky]).
|
|
||||||
- Fix PyPI classifiers (#228 by [@bbc2]).
|
|
||||||
|
|
||||||
## [0.10.5] - 2020-01-19
|
|
||||||
|
|
||||||
**Fixed**
|
|
||||||
|
|
||||||
- Fix handling of malformed lines and lines without a value (#222 by [@bbc2]):
|
|
||||||
- Don't print warning when key has no value.
|
|
||||||
- Reject more malformed lines (e.g. "A: B", "a='b',c").
|
|
||||||
- Fix handling of lines with just a comment (#224 by [@bbc2]).
|
|
||||||
|
|
||||||
## [0.10.4] - 2020-01-17
|
|
||||||
|
|
||||||
**Added**
|
|
||||||
|
|
||||||
- Make typing optional (#179 by [@techalchemy]).
|
|
||||||
- Print a warning on malformed line (#211 by [@bbc2]).
|
|
||||||
- Support keys without a value (#220 by [@ulyssessouza]).
|
|
||||||
|
|
||||||
## 0.10.3
|
|
||||||
|
|
||||||
- Improve interactive mode detection ([@andrewsmith])([#183]).
|
|
||||||
- Refactor parser to fix parsing inconsistencies ([@bbc2])([#170]).
|
|
||||||
- Interpret escapes as control characters only in double-quoted strings.
|
|
||||||
- Interpret `#` as start of comment only if preceded by whitespace.
|
|
||||||
|
|
||||||
## 0.10.2
|
|
||||||
|
|
||||||
- Add type hints and expose them to users ([@qnighy])([#172])
|
|
||||||
- `load_dotenv` and `dotenv_values` now accept an `encoding` parameter, defaults to `None`
|
|
||||||
([@theskumar])([@earlbread])([#161])
|
|
||||||
- Fix `str`/`unicode` inconsistency in Python 2: values are always `str` now. ([@bbc2])([#121])
|
|
||||||
- Fix Unicode error in Python 2, introduced in 0.10.0. ([@bbc2])([#176])
|
|
||||||
|
|
||||||
## 0.10.1
|
|
||||||
- Fix parsing of variable without a value ([@asyncee])([@bbc2])([#158])
|
|
||||||
|
|
||||||
## 0.10.0
|
|
||||||
|
|
||||||
- Add support for UTF-8 in unquoted values ([@bbc2])([#148])
|
|
||||||
- Add support for trailing comments ([@bbc2])([#148])
|
|
||||||
- Add backslashes support in values ([@bbc2])([#148])
|
|
||||||
- Add support for newlines in values ([@bbc2])([#148])
|
|
||||||
- Force environment variables to str with Python2 on Windows ([@greyli])
|
|
||||||
- Drop Python 3.3 support ([@greyli])
|
|
||||||
- Fix stderr/-out/-in redirection ([@venthur])
|
|
||||||
|
|
||||||
|
|
||||||
## 0.9.0
|
|
||||||
|
|
||||||
- Add `--version` parameter to cli ([@venthur])
|
|
||||||
- Enable loading from current directory ([@cjauvin])
|
|
||||||
- Add 'dotenv run' command for calling arbitrary shell script with .env ([@venthur])
|
|
||||||
|
|
||||||
## 0.8.1
|
|
||||||
|
|
||||||
- Add tests for docs ([@Flimm])
|
|
||||||
- Make 'cli' support optional. Use `pip install python-dotenv[cli]`. ([@theskumar])
|
|
||||||
|
|
||||||
## 0.8.0
|
|
||||||
|
|
||||||
- `set_key` and `unset_key` only modified the affected file instead of
|
|
||||||
parsing and re-writing file, this causes comments and other file
|
|
||||||
entact as it is.
|
|
||||||
- Add support for `export` prefix in the line.
|
|
||||||
- Internal refractoring ([@theskumar])
|
|
||||||
- Allow `load_dotenv` and `dotenv_values` to work with `StringIO())` ([@alanjds])([@theskumar])([#78])
|
|
||||||
|
|
||||||
## 0.7.1
|
|
||||||
|
|
||||||
- Remove hard dependency on iPython ([@theskumar])
|
|
||||||
|
|
||||||
## 0.7.0
|
|
||||||
|
|
||||||
- Add support to override system environment variable via .env.
|
|
||||||
([@milonimrod](https://github.com/milonimrod))
|
|
||||||
([\#63](https://github.com/theskumar/python-dotenv/issues/63))
|
|
||||||
- Disable ".env not found" warning by default
|
|
||||||
([@maxkoryukov](https://github.com/maxkoryukov))
|
|
||||||
([\#57](https://github.com/theskumar/python-dotenv/issues/57))
|
|
||||||
|
|
||||||
## 0.6.5
|
|
||||||
|
|
||||||
- Add support for special characters `\`.
|
|
||||||
([@pjona](https://github.com/pjona))
|
|
||||||
([\#60](https://github.com/theskumar/python-dotenv/issues/60))
|
|
||||||
|
|
||||||
## 0.6.4
|
|
||||||
|
|
||||||
- Fix issue with single quotes ([@Flimm])
|
|
||||||
([\#52](https://github.com/theskumar/python-dotenv/issues/52))
|
|
||||||
|
|
||||||
## 0.6.3
|
|
||||||
|
|
||||||
- Handle unicode exception in setup.py
|
|
||||||
([\#46](https://github.com/theskumar/python-dotenv/issues/46))
|
|
||||||
|
|
||||||
## 0.6.2
|
|
||||||
|
|
||||||
- Fix dotenv list command ([@ticosax](https://github.com/ticosax))
|
|
||||||
- Add iPython Support
|
|
||||||
([@tillahoffmann](https://github.com/tillahoffmann))
|
|
||||||
|
|
||||||
## 0.6.0
|
|
||||||
|
|
||||||
- Drop support for Python 2.6
|
|
||||||
- Handle escaped characters and newlines in quoted values. (Thanks
|
|
||||||
[@iameugenejo](https://github.com/iameugenejo))
|
|
||||||
- Remove any spaces around unquoted key/value. (Thanks
|
|
||||||
[@paulochf](https://github.com/paulochf))
|
|
||||||
- Added POSIX variable expansion. (Thanks
|
|
||||||
[@hugochinchilla](https://github.com/hugochinchilla))
|
|
||||||
|
|
||||||
## 0.5.1
|
|
||||||
|
|
||||||
- Fix find\_dotenv - it now start search from the file where this
|
|
||||||
function is called from.
|
|
||||||
|
|
||||||
## 0.5.0
|
|
||||||
|
|
||||||
- Add `find_dotenv` method that will try to find a `.env` file.
|
|
||||||
(Thanks [@isms](https://github.com/isms))
|
|
||||||
|
|
||||||
## 0.4.0
|
|
||||||
|
|
||||||
- cli: Added `-q/--quote` option to control the behaviour of quotes
|
|
||||||
around values in `.env`. (Thanks
|
|
||||||
[@hugochinchilla](https://github.com/hugochinchilla)).
|
|
||||||
- Improved test coverage.
|
|
||||||
|
|
||||||
[#78]: https://github.com/theskumar/python-dotenv/issues/78
|
|
||||||
[#121]: https://github.com/theskumar/python-dotenv/issues/121
|
|
||||||
[#148]: https://github.com/theskumar/python-dotenv/issues/148
|
|
||||||
[#158]: https://github.com/theskumar/python-dotenv/issues/158
|
|
||||||
[#170]: https://github.com/theskumar/python-dotenv/issues/170
|
|
||||||
[#172]: https://github.com/theskumar/python-dotenv/issues/172
|
|
||||||
[#176]: https://github.com/theskumar/python-dotenv/issues/176
|
|
||||||
[#183]: https://github.com/theskumar/python-dotenv/issues/183
|
|
||||||
[#359]: https://github.com/theskumar/python-dotenv/issues/359
|
|
||||||
|
|
||||||
[@alanjds]: https://github.com/alanjds
|
|
||||||
[@altendky]: https://github.com/altendky
|
|
||||||
[@andrewsmith]: https://github.com/andrewsmith
|
|
||||||
[@asyncee]: https://github.com/asyncee
|
|
||||||
[@bbc2]: https://github.com/bbc2
|
|
||||||
[@befeleme]: https://github.com/befeleme
|
|
||||||
[@cjauvin]: https://github.com/cjauvin
|
|
||||||
[@eaf]: https://github.com/eaf
|
|
||||||
[@earlbread]: https://github.com/earlbread
|
|
||||||
[@eggplants]: https://github.com/@eggplants
|
|
||||||
[@ekohl]: https://github.com/ekohl
|
|
||||||
[@elbehery95]: https://github.com/elbehery95
|
|
||||||
[@Flimm]: https://github.com/Flimm
|
|
||||||
[@gergelyk]: https://github.com/gergelyk
|
|
||||||
[@gongqingkui]: https://github.com/gongqingkui
|
|
||||||
[@greyli]: https://github.com/greyli
|
|
||||||
[@harveer07]: https://github.com/@harveer07
|
|
||||||
[@jadutter]: https://github.com/jadutter
|
|
||||||
[@jctanner]: https://github.com/jctanner
|
|
||||||
[@larsks]: https://github.com/@larsks
|
|
||||||
[@lsmith77]: https://github.com/lsmith77
|
|
||||||
[@mgorny]: https://github.com/mgorny
|
|
||||||
[@naorlivne]: https://github.com/@naorlivne
|
|
||||||
[@Nougat-Waffle]: https://github.com/Nougat-Waffle
|
|
||||||
[@qnighy]: https://github.com/qnighy
|
|
||||||
[@rabinadk1]: https://github.com/@rabinadk1
|
|
||||||
[@sammck]: https://github.com/@sammck
|
|
||||||
[@snobu]: https://github.com/snobu
|
|
||||||
[@techalchemy]: https://github.com/techalchemy
|
|
||||||
[@theGOTOguy]: https://github.com/theGOTOguy
|
|
||||||
[@theskumar]: https://github.com/theskumar
|
|
||||||
[@ulyssessouza]: https://github.com/ulyssessouza
|
|
||||||
[@venthur]: https://github.com/venthur
|
|
||||||
[@x-yuri]: https://github.com/x-yuri
|
|
||||||
[@yannham]: https://github.com/yannham
|
|
||||||
[@zueve]: https://github.com/zueve
|
|
||||||
|
|
||||||
|
|
||||||
[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v1.0.0...HEAD
|
|
||||||
[1.0.0]: https://github.com/theskumar/python-dotenv/compare/v0.21.0...v1.0.0
|
|
||||||
[0.21.1]: https://github.com/theskumar/python-dotenv/compare/v0.21.0...v0.21.1
|
|
||||||
[0.21.0]: https://github.com/theskumar/python-dotenv/compare/v0.20.0...v0.21.0
|
|
||||||
[0.20.0]: https://github.com/theskumar/python-dotenv/compare/v0.19.2...v0.20.0
|
|
||||||
[0.19.2]: https://github.com/theskumar/python-dotenv/compare/v0.19.1...v0.19.2
|
|
||||||
[0.19.1]: https://github.com/theskumar/python-dotenv/compare/v0.19.0...v0.19.1
|
|
||||||
[0.19.0]: https://github.com/theskumar/python-dotenv/compare/v0.18.0...v0.19.0
|
|
||||||
[0.18.0]: https://github.com/theskumar/python-dotenv/compare/v0.17.1...v0.18.0
|
|
||||||
[0.17.1]: https://github.com/theskumar/python-dotenv/compare/v0.17.0...v0.17.1
|
|
||||||
[0.17.0]: https://github.com/theskumar/python-dotenv/compare/v0.16.0...v0.17.0
|
|
||||||
[0.16.0]: https://github.com/theskumar/python-dotenv/compare/v0.15.0...v0.16.0
|
|
||||||
[0.15.0]: https://github.com/theskumar/python-dotenv/compare/v0.14.0...v0.15.0
|
|
||||||
[0.14.0]: https://github.com/theskumar/python-dotenv/compare/v0.13.0...v0.14.0
|
|
||||||
[0.13.0]: https://github.com/theskumar/python-dotenv/compare/v0.12.0...v0.13.0
|
|
||||||
[0.12.0]: https://github.com/theskumar/python-dotenv/compare/v0.11.0...v0.12.0
|
|
||||||
[0.11.0]: https://github.com/theskumar/python-dotenv/compare/v0.10.5...v0.11.0
|
|
||||||
[0.10.5]: https://github.com/theskumar/python-dotenv/compare/v0.10.4...v0.10.5
|
|
||||||
[0.10.4]: https://github.com/theskumar/python-dotenv/compare/v0.10.3...v0.10.4
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
../../Scripts/dotenv.exe,sha256=jHYKxcvUoEg8RKYCiB_ufWqcZf7w-n2_fs2qoqFnCVg,108407
|
|
||||||
dotenv/__init__.py,sha256=WBU5SfSiKAhS3hzu17ykNuuwbuwyDCX91Szv4vUeOuM,1292
|
|
||||||
dotenv/__main__.py,sha256=N0RhLG7nHIqtlJHwwepIo-zbJPNx9sewCCRGY528h_4,129
|
|
||||||
dotenv/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
dotenv/__pycache__/__main__.cpython-310.pyc,,
|
|
||||||
dotenv/__pycache__/cli.cpython-310.pyc,,
|
|
||||||
dotenv/__pycache__/ipython.cpython-310.pyc,,
|
|
||||||
dotenv/__pycache__/main.cpython-310.pyc,,
|
|
||||||
dotenv/__pycache__/parser.cpython-310.pyc,,
|
|
||||||
dotenv/__pycache__/variables.cpython-310.pyc,,
|
|
||||||
dotenv/__pycache__/version.cpython-310.pyc,,
|
|
||||||
dotenv/cli.py,sha256=_ttQuR9Yl4k1PT53ByISkDjJ3kO_N_LzIDZzZ95uXEk,5809
|
|
||||||
dotenv/ipython.py,sha256=avI6aez_RxnBptYgchIquF2TSgKI-GOhY3ppiu3VuWE,1303
|
|
||||||
dotenv/main.py,sha256=6j1GW8kNeZAooqffdajLne_dq_TJLi2Mk63DRNJjXLk,11932
|
|
||||||
dotenv/parser.py,sha256=QgU5HwMwM2wMqt0vz6dHTJ4nzPmwqRqvi4MSyeVifgU,5186
|
|
||||||
dotenv/py.typed,sha256=8PjyZ1aVoQpRVvt71muvuq5qE-jTFZkK-GLHkhdebmc,26
|
|
||||||
dotenv/variables.py,sha256=CD0qXOvvpB3q5RpBQMD9qX6vHX7SyW-SuiwGMFSlt08,2348
|
|
||||||
dotenv/version.py,sha256=J-j-u0itpEFT6irdmWmixQqYMadNl1X91TxUmoiLHMI,22
|
|
||||||
python_dotenv-1.0.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
|
||||||
python_dotenv-1.0.0.dist-info/LICENSE,sha256=gGGbcEnwjIFoOtDgHwjyV6hAZS3XHugxRtNmWMfSwrk,1556
|
|
||||||
python_dotenv-1.0.0.dist-info/METADATA,sha256=0oze1EyeRIUTg91jCTJGbnxQR6mz_FkOW73CmeueUak,21991
|
|
||||||
python_dotenv-1.0.0.dist-info/RECORD,,
|
|
||||||
python_dotenv-1.0.0.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
||||||
python_dotenv-1.0.0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
|
||||||
python_dotenv-1.0.0.dist-info/entry_points.txt,sha256=yRl1rCbswb1nQTQ_gZRlCw5QfabztUGnfGWLhlXFNdI,47
|
|
||||||
python_dotenv-1.0.0.dist-info/top_level.txt,sha256=eyqUH4SHJNr6ahOYlxIunTr4XinE8Z5ajWLdrK3r0D8,7
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
Wheel-Version: 1.0
|
|
||||||
Generator: bdist_wheel (0.38.4)
|
|
||||||
Root-Is-Purelib: true
|
|
||||||
Tag: py3-none-any
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
[console_scripts]
|
|
||||||
dotenv = dotenv.__main__:cli
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
dotenv
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
pip
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
Copyright 2007 Pallets
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are
|
|
||||||
met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer in the
|
|
||||||
documentation and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
3. Neither the name of the copyright holder nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
||||||
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
||||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
||||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
||||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
Metadata-Version: 2.1
|
|
||||||
Name: Werkzeug
|
|
||||||
Version: 2.3.7
|
|
||||||
Summary: The comprehensive WSGI web application library.
|
|
||||||
Maintainer-email: Pallets <contact@palletsprojects.com>
|
|
||||||
Requires-Python: >=3.8
|
|
||||||
Description-Content-Type: text/x-rst
|
|
||||||
Classifier: Development Status :: 5 - Production/Stable
|
|
||||||
Classifier: Environment :: Web Environment
|
|
||||||
Classifier: Intended Audience :: Developers
|
|
||||||
Classifier: License :: OSI Approved :: BSD License
|
|
||||||
Classifier: Operating System :: OS Independent
|
|
||||||
Classifier: Programming Language :: Python
|
|
||||||
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
||||||
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI
|
|
||||||
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Application
|
|
||||||
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware
|
|
||||||
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
||||||
Requires-Dist: MarkupSafe>=2.1.1
|
|
||||||
Requires-Dist: watchdog>=2.3 ; extra == "watchdog"
|
|
||||||
Project-URL: Changes, https://werkzeug.palletsprojects.com/changes/
|
|
||||||
Project-URL: Chat, https://discord.gg/pallets
|
|
||||||
Project-URL: Documentation, https://werkzeug.palletsprojects.com/
|
|
||||||
Project-URL: Donate, https://palletsprojects.com/donate
|
|
||||||
Project-URL: Issue Tracker, https://github.com/pallets/werkzeug/issues/
|
|
||||||
Project-URL: Source Code, https://github.com/pallets/werkzeug/
|
|
||||||
Provides-Extra: watchdog
|
|
||||||
|
|
||||||
Werkzeug
|
|
||||||
========
|
|
||||||
|
|
||||||
*werkzeug* German noun: "tool". Etymology: *werk* ("work"), *zeug* ("stuff")
|
|
||||||
|
|
||||||
Werkzeug is a comprehensive `WSGI`_ web application library. It began as
|
|
||||||
a simple collection of various utilities for WSGI applications and has
|
|
||||||
become one of the most advanced WSGI utility libraries.
|
|
||||||
|
|
||||||
It includes:
|
|
||||||
|
|
||||||
- An interactive debugger that allows inspecting stack traces and
|
|
||||||
source code in the browser with an interactive interpreter for any
|
|
||||||
frame in the stack.
|
|
||||||
- A full-featured request object with objects to interact with
|
|
||||||
headers, query args, form data, files, and cookies.
|
|
||||||
- A response object that can wrap other WSGI applications and handle
|
|
||||||
streaming data.
|
|
||||||
- A routing system for matching URLs to endpoints and generating URLs
|
|
||||||
for endpoints, with an extensible system for capturing variables
|
|
||||||
from URLs.
|
|
||||||
- HTTP utilities to handle entity tags, cache control, dates, user
|
|
||||||
agents, cookies, files, and more.
|
|
||||||
- A threaded WSGI server for use while developing applications
|
|
||||||
locally.
|
|
||||||
- A test client for simulating HTTP requests during testing without
|
|
||||||
requiring running a server.
|
|
||||||
|
|
||||||
Werkzeug doesn't enforce any dependencies. It is up to the developer to
|
|
||||||
choose a template engine, database adapter, and even how to handle
|
|
||||||
requests. It can be used to build all sorts of end user applications
|
|
||||||
such as blogs, wikis, or bulletin boards.
|
|
||||||
|
|
||||||
`Flask`_ wraps Werkzeug, using it to handle the details of WSGI while
|
|
||||||
providing more structure and patterns for defining powerful
|
|
||||||
applications.
|
|
||||||
|
|
||||||
.. _WSGI: https://wsgi.readthedocs.io/en/latest/
|
|
||||||
.. _Flask: https://www.palletsprojects.com/p/flask/
|
|
||||||
|
|
||||||
|
|
||||||
Installing
|
|
||||||
----------
|
|
||||||
|
|
||||||
Install and update using `pip`_:
|
|
||||||
|
|
||||||
.. code-block:: text
|
|
||||||
|
|
||||||
pip install -U Werkzeug
|
|
||||||
|
|
||||||
.. _pip: https://pip.pypa.io/en/stable/getting-started/
|
|
||||||
|
|
||||||
|
|
||||||
A Simple Example
|
|
||||||
----------------
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from werkzeug.wrappers import Request, Response
|
|
||||||
|
|
||||||
@Request.application
|
|
||||||
def application(request):
|
|
||||||
return Response('Hello, World!')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
from werkzeug.serving import run_simple
|
|
||||||
run_simple('localhost', 4000, application)
|
|
||||||
|
|
||||||
|
|
||||||
Donate
|
|
||||||
------
|
|
||||||
|
|
||||||
The Pallets organization develops and supports Werkzeug and other
|
|
||||||
popular packages. In order to grow the community of contributors and
|
|
||||||
users, and allow the maintainers to devote more time to the projects,
|
|
||||||
`please donate today`_.
|
|
||||||
|
|
||||||
.. _please donate today: https://palletsprojects.com/donate
|
|
||||||
|
|
||||||
|
|
||||||
Links
|
|
||||||
-----
|
|
||||||
|
|
||||||
- Documentation: https://werkzeug.palletsprojects.com/
|
|
||||||
- Changes: https://werkzeug.palletsprojects.com/changes/
|
|
||||||
- PyPI Releases: https://pypi.org/project/Werkzeug/
|
|
||||||
- Source Code: https://github.com/pallets/werkzeug/
|
|
||||||
- Issue Tracker: https://github.com/pallets/werkzeug/issues/
|
|
||||||
- Chat: https://discord.gg/pallets
|
|
||||||
|
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
werkzeug-2.3.7.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
|
||||||
werkzeug-2.3.7.dist-info/LICENSE.rst,sha256=O0nc7kEF6ze6wQ-vG-JgQI_oXSUrjp3y4JefweCUQ3s,1475
|
|
||||||
werkzeug-2.3.7.dist-info/METADATA,sha256=H_HVr2PWAD0AW95k1Q4-4s4m3vcM693t6Vf4YCh2FPk,4093
|
|
||||||
werkzeug-2.3.7.dist-info/RECORD,,
|
|
||||||
werkzeug-2.3.7.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
||||||
werkzeug-2.3.7.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
|
|
||||||
werkzeug/__init__.py,sha256=HjWydmi120RYL45Z8FRvqOcMGhuaKCGi80tfS7zF9GE,188
|
|
||||||
werkzeug/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/_internal.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/_reloader.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/exceptions.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/formparser.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/http.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/local.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/security.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/serving.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/test.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/testapp.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/urls.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/user_agent.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/utils.cpython-310.pyc,,
|
|
||||||
werkzeug/__pycache__/wsgi.cpython-310.pyc,,
|
|
||||||
werkzeug/_internal.py,sha256=2AlGqWeqlbt7SlK03s2dg4SamrfsJDyIiVKpz4Og69w,8303
|
|
||||||
werkzeug/_reloader.py,sha256=1O1DDWlqVwYIX8kgJwH5B4a_Uh6acQnw3sQf01JpXtM,14745
|
|
||||||
werkzeug/datastructures/__init__.py,sha256=yzBdOT9DdK3nraNG49pA3bVsvtPPLx2-t2N8ZmuAd9w,1900
|
|
||||||
werkzeug/datastructures/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/accept.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/auth.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/cache_control.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/csp.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/etag.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/file_storage.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/headers.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/mixins.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/range.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/__pycache__/structures.cpython-310.pyc,,
|
|
||||||
werkzeug/datastructures/accept.py,sha256=CuCvBAxNzbt4QUb17rH986vvOVGURFUjo0DX2PQy_yI,10670
|
|
||||||
werkzeug/datastructures/accept.pyi,sha256=6P114gncjZoy-i_n_3OQy2nJVwjEAIe7PcBxKYqCEfc,1917
|
|
||||||
werkzeug/datastructures/auth.py,sha256=WGcJjnFmbDprqlDr54kXaD3zhK0E4NOoQVa4JHYl9iM,16043
|
|
||||||
werkzeug/datastructures/cache_control.py,sha256=RTUipZev50s-1TAn2rYGZrytm_6IOIxQd67fkR5bNF0,6043
|
|
||||||
werkzeug/datastructures/cache_control.pyi,sha256=6Q93jRysAKMPWRA72OMksyn7d3ZysuxwGlHp_iwF9pA,3756
|
|
||||||
werkzeug/datastructures/csp.py,sha256=DAOAO266LK0JKbvlG80bbkAgfrNsnU9HBoz-FdIYNdo,3244
|
|
||||||
werkzeug/datastructures/csp.pyi,sha256=AmDWiZU4rrJA4SZmyMNI1L5PLdIfJsI5Li9r5lE1q6M,5765
|
|
||||||
werkzeug/datastructures/etag.py,sha256=JsyI-yXayF-hQu26MyFzbHFIZsaQ6odj3RZO_jF-_cc,2913
|
|
||||||
werkzeug/datastructures/etag.pyi,sha256=N9cuUBrZnxHmsbW0BBmjKW-djNY7WKbI6t_WopB8Zo0,1047
|
|
||||||
werkzeug/datastructures/file_storage.py,sha256=ePeMtr65s_1_sunXMv_SBOiFof5CX5BepYv5_W16fZk,6184
|
|
||||||
werkzeug/datastructures/file_storage.pyi,sha256=2sdbKHhvbQF5FjrJuO6l_m1yZvZ4oPCUTspmdmjQlSU,1433
|
|
||||||
werkzeug/datastructures/headers.py,sha256=V08N4VTcaA11fRq1WK5v28QomGd-A1S9CmiwugixhWo,18882
|
|
||||||
werkzeug/datastructures/headers.pyi,sha256=66Gh9DbD8QNpLRBOuer4DMCj12csddHrcgxiJPLE5n8,4237
|
|
||||||
werkzeug/datastructures/mixins.py,sha256=-IQSQ70UOMQlqtJEIyyhplOd4obaTOfzGvka-cunCtM,5337
|
|
||||||
werkzeug/datastructures/mixins.pyi,sha256=y92tClxVslJBEGgAwDRsQLExfin2p0x7NfnP_b8w6xc,4191
|
|
||||||
werkzeug/datastructures/range.py,sha256=JXSDPseG7iH5giJp3R1SnQC_SqQp634M8Iv6QTsbTxM,5669
|
|
||||||
werkzeug/datastructures/range.pyi,sha256=bsM61iNp86gT2lyN0F_Dqg8xsnfPerdmElipuHppiJQ,1792
|
|
||||||
werkzeug/datastructures/structures.py,sha256=_bhAf0adEk6WU2uy8jdmuxFMTFcuClY1p7jQ-3wYXj4,31761
|
|
||||||
werkzeug/datastructures/structures.pyi,sha256=MRg-RubT3UPjh62i9-7Xht8DVL0zTApRzjs52Hfz_j4,8148
|
|
||||||
werkzeug/debug/__init__.py,sha256=WRTLJSvnuK6jlBuQLllTnN57th0HKPjxbS7-d8QJZIc,18760
|
|
||||||
werkzeug/debug/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
werkzeug/debug/__pycache__/console.cpython-310.pyc,,
|
|
||||||
werkzeug/debug/__pycache__/repr.cpython-310.pyc,,
|
|
||||||
werkzeug/debug/__pycache__/tbtools.cpython-310.pyc,,
|
|
||||||
werkzeug/debug/console.py,sha256=FIO8gDX2eQ1_4MtpJ4s0i2gR4fFCJZTPwhSVByF4kbo,6068
|
|
||||||
werkzeug/debug/repr.py,sha256=ECmIpNVlCppTfCuIuEgrJVfuhr8iDqPSWeVJyxt1QOM,9328
|
|
||||||
werkzeug/debug/shared/ICON_LICENSE.md,sha256=DhA6Y1gUl5Jwfg0NFN9Rj4VWITt8tUx0IvdGf0ux9-s,222
|
|
||||||
werkzeug/debug/shared/console.png,sha256=bxax6RXXlvOij_KeqvSNX0ojJf83YbnZ7my-3Gx9w2A,507
|
|
||||||
werkzeug/debug/shared/debugger.js,sha256=FVBBUirz4kKedIbM08QQCYeEoicoSbnm4BnBF4dCYfA,10562
|
|
||||||
werkzeug/debug/shared/less.png,sha256=-4-kNRaXJSONVLahrQKUxMwXGm9R4OnZ9SxDGpHlIR4,191
|
|
||||||
werkzeug/debug/shared/more.png,sha256=GngN7CioHQoV58rH6ojnkYi8c_qED2Aka5FO5UXrReY,200
|
|
||||||
werkzeug/debug/shared/style.css,sha256=-xSxzUEZGw_IqlDR5iZxitNl8LQUjBM-_Y4UAvXVH8g,6078
|
|
||||||
werkzeug/debug/tbtools.py,sha256=8Xg7p2JzCC1AMWuse5HYc594OdzC5ToeJbNk49_zZCc,13271
|
|
||||||
werkzeug/exceptions.py,sha256=d6VNzGcVgLazIpfwRD8pN_d3yAJNyngBDFvlXQbR-38,26062
|
|
||||||
werkzeug/formparser.py,sha256=DZ9BeiHAah3_CuBORNOEipRwE74lHRFX1eK2_3XKcL4,19574
|
|
||||||
werkzeug/http.py,sha256=morM5oaClzgpBEjShgUVPlj4asZCvYuZ8WqjVaZzXtQ,48754
|
|
||||||
werkzeug/local.py,sha256=Jawgwa7Q7orExtyzVlhbh-4mGWg3v63bspIU5Nnl-DU,22003
|
|
||||||
werkzeug/middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
||||||
werkzeug/middleware/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
werkzeug/middleware/__pycache__/dispatcher.cpython-310.pyc,,
|
|
||||||
werkzeug/middleware/__pycache__/http_proxy.cpython-310.pyc,,
|
|
||||||
werkzeug/middleware/__pycache__/lint.cpython-310.pyc,,
|
|
||||||
werkzeug/middleware/__pycache__/profiler.cpython-310.pyc,,
|
|
||||||
werkzeug/middleware/__pycache__/proxy_fix.cpython-310.pyc,,
|
|
||||||
werkzeug/middleware/__pycache__/shared_data.cpython-310.pyc,,
|
|
||||||
werkzeug/middleware/dispatcher.py,sha256=6ltzPtDsIdLTY_T1GW6kxBJL0KZftbipa_WVdKtpVQ8,2601
|
|
||||||
werkzeug/middleware/http_proxy.py,sha256=vsSvt84m656x3mV_Fj78y7O2eYHmurWngErTcjeiz8U,7833
|
|
||||||
werkzeug/middleware/lint.py,sha256=6CqcwMWro1p-GRUGPgQ1n21KFnTTqc6-81CGTzpcK74,13916
|
|
||||||
werkzeug/middleware/profiler.py,sha256=KKr8nAiF9dr9pNd3G0D3xs7mUba9gvWkyK7X9ceke70,4906
|
|
||||||
werkzeug/middleware/proxy_fix.py,sha256=dcOOSjSok2QsSh1VSNsw-a0Vy_Jn5DunlO6PRbXBq0A,6754
|
|
||||||
werkzeug/middleware/shared_data.py,sha256=DeM8OouhfhZs8w5T7Wxw-uKuOHXoH0x5RopzxR2RRjI,9513
|
|
||||||
werkzeug/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
||||||
werkzeug/routing/__init__.py,sha256=HpvahY7WwkLdV4Cq3Bsc3GrqNon4u6t8-vhbb9E5o00,4819
|
|
||||||
werkzeug/routing/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
werkzeug/routing/__pycache__/converters.cpython-310.pyc,,
|
|
||||||
werkzeug/routing/__pycache__/exceptions.cpython-310.pyc,,
|
|
||||||
werkzeug/routing/__pycache__/map.cpython-310.pyc,,
|
|
||||||
werkzeug/routing/__pycache__/matcher.cpython-310.pyc,,
|
|
||||||
werkzeug/routing/__pycache__/rules.cpython-310.pyc,,
|
|
||||||
werkzeug/routing/converters.py,sha256=V8e_wMRop6WG4Kymu4pBIR8OrJl-ZUQUZlinUXfw7WE,7602
|
|
||||||
werkzeug/routing/exceptions.py,sha256=yGZ5AUL-buHp-vK8AJbZ0bLIbSckh1UyiGKgRg4ZjaA,4698
|
|
||||||
werkzeug/routing/map.py,sha256=2tirw9j5wypzsUT6WBcBNcBTqNp0_iBXnF_1vhY9HjI,37403
|
|
||||||
werkzeug/routing/matcher.py,sha256=FyPG45iqR1XwxFujejSqfNEKV7IgbR2td7Jp-ocSASY,7817
|
|
||||||
werkzeug/routing/rules.py,sha256=THxBzPRlK87nyf8qoBH96MuHk8G5KiSJkQEay4zZmzY,32058
|
|
||||||
werkzeug/sansio/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
||||||
werkzeug/sansio/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
werkzeug/sansio/__pycache__/http.cpython-310.pyc,,
|
|
||||||
werkzeug/sansio/__pycache__/multipart.cpython-310.pyc,,
|
|
||||||
werkzeug/sansio/__pycache__/request.cpython-310.pyc,,
|
|
||||||
werkzeug/sansio/__pycache__/response.cpython-310.pyc,,
|
|
||||||
werkzeug/sansio/__pycache__/utils.cpython-310.pyc,,
|
|
||||||
werkzeug/sansio/http.py,sha256=mKTbXo_squCAZKjt9yzfPFV8ZqQbfa6mjdc6XoeLNZ0,6234
|
|
||||||
werkzeug/sansio/multipart.py,sha256=XM53Ud4YXicPEqm0HgcS3loMfMCt3NoAx8DmImxaW0g,11113
|
|
||||||
werkzeug/sansio/request.py,sha256=wEeVGySwlOfJT5xlgQzjJOe2ksky70CJT75QTzkvfqM,24243
|
|
||||||
werkzeug/sansio/response.py,sha256=6DgROSXWG_0XzOrJi_U8PRMNGKX8YV-uHPu0cLrOKsk,29010
|
|
||||||
werkzeug/sansio/utils.py,sha256=LYgmrN7yr04ZDVk5flPcUJLo1rDnTzhF04OH3-ujCWQ,4950
|
|
||||||
werkzeug/security.py,sha256=gEH8qD5Ykgn6W6PgMx2CQx-iNqJFenXXqOGiWDi_3eE,5814
|
|
||||||
werkzeug/serving.py,sha256=Ql_SUZxsmQzN8OZ-hDvKFQ5nRgKh6FEIYwcXVEmD6qU,39224
|
|
||||||
werkzeug/test.py,sha256=xOnp3B6V2MQ0Qn3jL7eMimJna2-zqI04wD_IazDKCto,55733
|
|
||||||
werkzeug/testapp.py,sha256=Q7SXVDXeXnnXo7-TWVoAJCTF2GnXxoH-v5_pvjUyTWc,6135
|
|
||||||
werkzeug/urls.py,sha256=Uq_cu8TmZFHkQ7t2pp9DNwDvs6wG76jzWPstQIssPVk,45683
|
|
||||||
werkzeug/user_agent.py,sha256=lSlLYKCcbzCUSkbdAoO8zPk2UR-8Mdn6iu_iA2kYPBA,1416
|
|
||||||
werkzeug/utils.py,sha256=DYkOtfDR_Wc3ro3_peReo9KkUC-6yhOvz27_PUAckbA,24654
|
|
||||||
werkzeug/wrappers/__init__.py,sha256=kGyK7rOud3qCxll_jFyW15YarJhj1xtdf3ocx9ZheB8,120
|
|
||||||
werkzeug/wrappers/__pycache__/__init__.cpython-310.pyc,,
|
|
||||||
werkzeug/wrappers/__pycache__/request.cpython-310.pyc,,
|
|
||||||
werkzeug/wrappers/__pycache__/response.cpython-310.pyc,,
|
|
||||||
werkzeug/wrappers/request.py,sha256=_PIbgCZ9xfQXC9HEjm-j1R-F4gSPcx5q-QT983mMzbs,24848
|
|
||||||
werkzeug/wrappers/response.py,sha256=FfGesquK6cSdPTFZvzV42CM__Ohta2cxNqLBDRkAuKA,32664
|
|
||||||
werkzeug/wsgi.py,sha256=PGkhajtHnJj2NqYpYW_T8w17JJbaH8iI0wHHNkPvJKs,29153
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
Wheel-Version: 1.0
|
|
||||||
Generator: flit 3.9.0
|
|
||||||
Root-Is-Purelib: true
|
|
||||||
Tag: py3-none-any
|
|
||||||
@@ -1,6 +1,25 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing as t
|
||||||
|
|
||||||
from .serving import run_simple as run_simple
|
from .serving import run_simple as run_simple
|
||||||
from .test import Client as Client
|
from .test import Client as Client
|
||||||
from .wrappers import Request as Request
|
from .wrappers import Request as Request
|
||||||
from .wrappers import Response as Response
|
from .wrappers import Response as Response
|
||||||
|
|
||||||
__version__ = "2.3.7"
|
|
||||||
|
def __getattr__(name: str) -> t.Any:
|
||||||
|
if name == "__version__":
|
||||||
|
import importlib.metadata
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
warnings.warn(
|
||||||
|
"The '__version__' attribute is deprecated and will be removed in"
|
||||||
|
" Werkzeug 3.1. Use feature detection or"
|
||||||
|
" 'importlib.metadata.version(\"werkzeug\")' instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
return importlib.metadata.version("werkzeug")
|
||||||
|
|
||||||
|
raise AttributeError(name)
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,7 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import operator
|
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import typing as t
|
import typing as t
|
||||||
@@ -26,102 +25,12 @@ class _Missing:
|
|||||||
_missing = _Missing()
|
_missing = _Missing()
|
||||||
|
|
||||||
|
|
||||||
@t.overload
|
def _wsgi_decoding_dance(s: str) -> str:
|
||||||
def _make_encode_wrapper(reference: str) -> t.Callable[[str], str]:
|
return s.encode("latin1").decode(errors="replace")
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
@t.overload
|
def _wsgi_encoding_dance(s: str) -> str:
|
||||||
def _make_encode_wrapper(reference: bytes) -> t.Callable[[str], bytes]:
|
return s.encode().decode("latin1")
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
def _make_encode_wrapper(reference: t.AnyStr) -> t.Callable[[str], t.AnyStr]:
|
|
||||||
"""Create a function that will be called with a string argument. If
|
|
||||||
the reference is bytes, values will be encoded to bytes.
|
|
||||||
"""
|
|
||||||
if isinstance(reference, str):
|
|
||||||
return lambda x: x
|
|
||||||
|
|
||||||
return operator.methodcaller("encode", "latin1")
|
|
||||||
|
|
||||||
|
|
||||||
def _check_str_tuple(value: tuple[t.AnyStr, ...]) -> None:
|
|
||||||
"""Ensure tuple items are all strings or all bytes."""
|
|
||||||
if not value:
|
|
||||||
return
|
|
||||||
|
|
||||||
item_type = str if isinstance(value[0], str) else bytes
|
|
||||||
|
|
||||||
if any(not isinstance(item, item_type) for item in value):
|
|
||||||
raise TypeError(f"Cannot mix str and bytes arguments (got {value!r})")
|
|
||||||
|
|
||||||
|
|
||||||
_default_encoding = sys.getdefaultencoding()
|
|
||||||
|
|
||||||
|
|
||||||
def _to_bytes(
|
|
||||||
x: str | bytes, charset: str = _default_encoding, errors: str = "strict"
|
|
||||||
) -> bytes:
|
|
||||||
if x is None or isinstance(x, bytes):
|
|
||||||
return x
|
|
||||||
|
|
||||||
if isinstance(x, (bytearray, memoryview)):
|
|
||||||
return bytes(x)
|
|
||||||
|
|
||||||
if isinstance(x, str):
|
|
||||||
return x.encode(charset, errors)
|
|
||||||
|
|
||||||
raise TypeError("Expected bytes")
|
|
||||||
|
|
||||||
|
|
||||||
@t.overload
|
|
||||||
def _to_str( # type: ignore
|
|
||||||
x: None,
|
|
||||||
charset: str | None = ...,
|
|
||||||
errors: str = ...,
|
|
||||||
allow_none_charset: bool = ...,
|
|
||||||
) -> None:
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
@t.overload
|
|
||||||
def _to_str(
|
|
||||||
x: t.Any,
|
|
||||||
charset: str | None = ...,
|
|
||||||
errors: str = ...,
|
|
||||||
allow_none_charset: bool = ...,
|
|
||||||
) -> str:
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
def _to_str(
|
|
||||||
x: t.Any | None,
|
|
||||||
charset: str | None = _default_encoding,
|
|
||||||
errors: str = "strict",
|
|
||||||
allow_none_charset: bool = False,
|
|
||||||
) -> str | bytes | None:
|
|
||||||
if x is None or isinstance(x, str):
|
|
||||||
return x
|
|
||||||
|
|
||||||
if not isinstance(x, (bytes, bytearray)):
|
|
||||||
return str(x)
|
|
||||||
|
|
||||||
if charset is None:
|
|
||||||
if allow_none_charset:
|
|
||||||
return x
|
|
||||||
|
|
||||||
return x.decode(charset, errors) # type: ignore
|
|
||||||
|
|
||||||
|
|
||||||
def _wsgi_decoding_dance(
|
|
||||||
s: str, charset: str = "utf-8", errors: str = "replace"
|
|
||||||
) -> str:
|
|
||||||
return s.encode("latin1").decode(charset, errors)
|
|
||||||
|
|
||||||
|
|
||||||
def _wsgi_encoding_dance(s: str, charset: str = "utf-8", errors: str = "strict") -> str:
|
|
||||||
return s.encode(charset).decode("latin1", errors)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_environ(obj: WSGIEnvironment | Request) -> WSGIEnvironment:
|
def _get_environ(obj: WSGIEnvironment | Request) -> WSGIEnvironment:
|
||||||
@@ -287,31 +196,6 @@ class _DictAccessorProperty(t.Generic[_TAccessorValue]):
|
|||||||
return f"<{type(self).__name__} {self.name}>"
|
return f"<{type(self).__name__} {self.name}>"
|
||||||
|
|
||||||
|
|
||||||
def _decode_idna(domain: str) -> str:
|
|
||||||
try:
|
|
||||||
data = domain.encode("ascii")
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
# If the domain is not ASCII, it's decoded already.
|
|
||||||
return domain
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Try decoding in one shot.
|
|
||||||
return data.decode("idna")
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Decode each part separately, leaving invalid parts as punycode.
|
|
||||||
parts = []
|
|
||||||
|
|
||||||
for part in data.split(b"."):
|
|
||||||
try:
|
|
||||||
parts.append(part.decode("idna"))
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
parts.append(part.decode("ascii"))
|
|
||||||
|
|
||||||
return ".".join(parts)
|
|
||||||
|
|
||||||
|
|
||||||
_plain_int_re = re.compile(r"-?\d+", re.ASCII)
|
_plain_int_re = re.compile(r"-?\d+", re.ASCII)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user