Skip to content

Commit

Permalink
refactor DIR to PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Oct 4, 2023
1 parent 7025cbc commit d5fa284
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 39 deletions.
16 changes: 8 additions & 8 deletions config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
# To override them (in instance/production.py) either use them like here
# or use absolute paths like e.g. pathlib.Path('/some/location/somewhere')
FILES_PATH = Path(__file__).parent.parent / 'files'
EXPORT_DIR = Path(FILES_PATH) / 'export'
UPLOAD_DIR = Path(FILES_PATH) / 'uploads'
TMP_DIR = Path('/tmp') # used e.g. for processing imports and export files
EXPORT_PATH = Path(FILES_PATH) / 'export'
UPLOAD_PATH = Path(FILES_PATH) / 'uploads'
TMP_PATH = Path('/tmp') # used e.g. for processing imports and export files

# Image processing
PROCESSED_IMAGE_DIR = Path(FILES_PATH) / 'processed_images'
RESIZED_IMAGES = Path(PROCESSED_IMAGE_DIR) / 'resized'
PROCESSED_IMAGE_PATH = Path(FILES_PATH) / 'processed_images'
RESIZED_IMAGES = Path(PROCESSED_IMAGE_PATH) / 'resized'
IMAGE_SIZE = {
'thumbnail': '200',
'table': '100'}
Expand All @@ -46,9 +46,9 @@
PROCESSED_EXT = '.jpeg'

# For system checks
WRITABLE_DIRS = [
UPLOAD_DIR,
EXPORT_DIR,
WRITABLE_PATHS = [
UPLOAD_PATH,
EXPORT_PATH,
RESIZED_IMAGES]

# Security
Expand Down
4 changes: 2 additions & 2 deletions install/upgrade/database_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from config.database_versions import DATABASE_VERSIONS
from config.default import (
DATABASE_PASS, VERSION, DATABASE_VERSION, DATABASE_NAME, DATABASE_USER,
DATABASE_HOST, DATABASE_PORT, EXPORT_DIR)
DATABASE_HOST, DATABASE_PORT, EXPORT_PATH)
from instance import production
from openatlas.database.connect import open_connection
from openatlas.database.settings import Settings
Expand Down Expand Up @@ -107,7 +107,7 @@ def check_database_version_supported() -> None:


def backup_database() -> None:
path = EXPORT_DIR
path = EXPORT_PATH
if not os.access(path, os.W_OK):
finish(
f'Directory for database backup not writeable ({path}). Aborting!')
Expand Down
2 changes: 1 addition & 1 deletion instance/example_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

# For Windows user
# from pathlib import Path
# TMP_DIR = Path('C:\\Path\\to\\tmp')
# TMP_PATH = Path('C:\\Path\\to\\tmp')
2 changes: 1 addition & 1 deletion openatlas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def before_request() -> None:
g.class_view_mapping = OpenatlasClass.get_class_view_mapping()
g.table_headers = OpenatlasClass.get_table_headers()
g.files = {}
for file_ in app.config['UPLOAD_DIR'].iterdir():
for file_ in app.config['UPLOAD_PATH'].iterdir():
if file_.stem.isdigit():
g.files[int(file_.stem)] = file_
# Set max file upload in MB
Expand Down
2 changes: 1 addition & 1 deletion openatlas/api/import_scripts/arche.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def import_arche_data() -> int:
'https://arche-thumbnails.acdh.oeaw.ac.at/',
params={'id': ortho_photo, 'width': 1200}, # type: ignore
timeout=60).content
open(str(app.config['UPLOAD_DIR'] / filename), "wb").write(thumb_req)
open(str(app.config['UPLOAD_PATH'] / filename), "wb").write(thumb_req)
file.link('P67', artifact)

creator = get_or_create_person(
Expand Down
2 changes: 1 addition & 1 deletion openatlas/display/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def safe_resize_image(name: str, file_format: str, size: str) -> bool:

def image_resizing(name: str, format_: str, size: str) -> bool:
conf = app.config
filename = Path(conf['UPLOAD_DIR']) / f"{name}{format_}[0]"
filename = Path(conf['UPLOAD_PATH']) / f"{name}{format_}[0]"
with Image(filename=filename) as src:
ext = conf['PROCESSED_EXT'] \
if format_ in conf['NONE_DISPLAY_EXT'] else format_
Expand Down
6 changes: 3 additions & 3 deletions openatlas/display/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def format_name_and_aliases(entity: Entity, show_links: bool) -> str:


def get_backup_file_data() -> dict[str, Any]:
path = app.config['EXPORT_DIR']
path = app.config['EXPORT_PATH']
latest_file = None
latest_file_date = None
for file in [
Expand Down Expand Up @@ -426,7 +426,7 @@ def system_warnings(_context: str, _unneeded_string: str) -> str:
warnings.append(
f"Database version {app.config['DATABASE_VERSION']} is needed but "
f"current version is {g.settings['database_version']}")
for path in app.config['WRITABLE_DIRS']:
for path in app.config['WRITABLE_PATHS']:
if not os.access(path, os.W_OK):
warnings.append(
'<p class="uc-first">' + _('directory not writable') +
Expand Down Expand Up @@ -470,7 +470,7 @@ def get_file_path(
ext = app.config['PROCESSED_EXT'] # pragma: no cover
path = app.config['RESIZED_IMAGES'] / size / f"{id_}{ext}"
return path if os.path.exists(path) else None
return app.config['UPLOAD_DIR'] / f"{id_}{ext}"
return app.config['UPLOAD_PATH'] / f"{id_}{ext}"


def format_date(value: Union[datetime, numpy.datetime64]) -> str:
Expand Down
2 changes: 1 addition & 1 deletion openatlas/models/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def current_date_for_filename() -> str:


def sql_export(format_: str, postfix: Optional[str] = '') -> bool:
file = app.config['EXPORT_DIR'] \
file = app.config['EXPORT_PATH'] \
/ f'{current_date_for_filename()}_export{postfix}.{format_}'
pg_dump = "pg_dump" if os.name == 'posix' \
else f'"{shutil.which("pg_dump.exe")}"'
Expand Down
10 changes: 5 additions & 5 deletions openatlas/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def admin_orphans() -> str:
entity.description])

# Orphaned files with no corresponding entity
for file in app.config['UPLOAD_DIR'].iterdir():
for file in app.config['UPLOAD_PATH'].iterdir():
if file.name != '.gitignore' \
and os.path.isfile(file) \
and file.stem.isdigit() \
Expand Down Expand Up @@ -573,7 +573,7 @@ def admin_orphans() -> str:
def admin_file_delete(filename: str) -> Response:
if filename != 'all': # Delete one file
try:
(app.config['UPLOAD_DIR'] / filename).unlink()
(app.config['UPLOAD_PATH'] / filename).unlink()
flash(f"{filename} {_('was deleted')}", 'info')
except Exception as e:
g.logger.log('error', 'file', f'deletion of {filename} failed', e)
Expand All @@ -583,10 +583,10 @@ def admin_file_delete(filename: str) -> Response:
# Delete all files with no corresponding entity
if is_authorized('admin'): # pragma: no cover - don't test, ever
entity_file_ids = [entity.id for entity in Entity.get_by_class('file')]
for f in app.config['UPLOAD_DIR'].iterdir():
for f in app.config['UPLOAD_PATH'].iterdir():
if f.name != '.gitignore' and int(f.stem) not in entity_file_ids:
try:
(app.config['UPLOAD_DIR'] / f.name).unlink()
(app.config['UPLOAD_PATH'] / f.name).unlink()
except Exception as e:
g.logger.log(
'error', 'file', f'deletion of {f.name} failed', e)
Expand Down Expand Up @@ -764,7 +764,7 @@ def get_disk_space_info() -> Optional[dict[str, Any]]:
files_size = int(process.stdout.split()[0])
else:
files_size = 40999999999 # pragma: no cover
stats = shutil.disk_usage(app.config['UPLOAD_DIR'])
stats = shutil.disk_usage(app.config['UPLOAD_PATH'])
percent_free = 100 - math.ceil(stats.free / (stats.total / 100))
percent_files = math.ceil(files_size / (stats.total / 100))
other_files = stats.total - stats.free - files_size
Expand Down
6 changes: 3 additions & 3 deletions openatlas/views/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def insert(
class_name=class_,
view_name=g.classes[class_].view,
gis_data=manager.place_info['gis_data'],
writable=os.access(app.config['UPLOAD_DIR'], os.W_OK),
writable=os.access(app.config['UPLOAD_PATH'], os.W_OK),
overlays=manager.place_info['overlays'],
title=_(g.classes[class_].view),
crumbs=manager.get_crumbs())
Expand Down Expand Up @@ -209,7 +209,7 @@ def insert_files(manager: BaseManager) -> Union[str, Response]:
filename = secure_filename(f'a{file.filename}')
name = f"{manager.entity.id}.{filename.rsplit('.', 1)[1].lower()}"
ext = secure_filename(file.filename).rsplit('.', 1)[1].lower()
path = app.config['UPLOAD_DIR'] / name
path = app.config['UPLOAD_PATH'] / name
file.save(str(path))
if f'.{ext}' in app.config['DISPLAY_FILE_EXTENSIONS']:
call(f'exiftran -ai {path}', shell=True) # Fix rotation
Expand All @@ -228,7 +228,7 @@ def insert_files(manager: BaseManager) -> Union[str, Response]:
except Exception as e: # pragma: no cover
Transaction.rollback()
for filename in filenames:
(app.config['UPLOAD_DIR'] / filename).unlink()
(app.config['UPLOAD_PATH'] / filename).unlink()
g.logger.log('error', 'database', 'transaction failed', e)
flash(_('error transaction'), 'error')
url = url_for('index', view=g.classes['file'].view)
Expand Down
10 changes: 5 additions & 5 deletions openatlas/views/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
@required_group('manager')
def download_sql(filename: str) -> Response:
return send_from_directory(
app.config['EXPORT_DIR'],
app.config['EXPORT_PATH'],
filename,
as_attachment=True)


@app.route('/export/execute/<format_>')
@required_group('manager')
def export_execute(format_: str) -> Response:
if os.access(app.config['EXPORT_DIR'], os.W_OK):
if os.access(app.config['EXPORT_PATH'], os.W_OK):
if sql_export(format_):
g.logger.log('info', 'database', 'SQL export')
flash(_('data was exported'), 'info')
Expand All @@ -38,7 +38,7 @@ def export_execute(format_: str) -> Response:
@app.route('/export/sql')
@required_group('manager')
def export_sql() -> str:
path = app.config['EXPORT_DIR']
path = app.config['EXPORT_PATH']
table = Table(['name', 'size'], order=[[0, 'desc']])
for file in [f for f in path.iterdir()
if (path / f).is_file() and f.name != '.gitignore']:
Expand All @@ -49,7 +49,7 @@ def export_sql() -> str:
_('download'),
url_for('download_sql', filename=file.name))]
if is_authorized('admin') \
and os.access(app.config['EXPORT_DIR'], os.W_OK):
and os.access(app.config['EXPORT_PATH'], os.W_OK):
confirm = _('Delete %(name)s?', name=file.name.replace("'", ''))
data.append(
link(
Expand Down Expand Up @@ -80,7 +80,7 @@ def export_sql() -> str:
@required_group('admin')
def delete_export(filename: str) -> Response:
try:
(app.config['EXPORT_DIR'] / filename).unlink()
(app.config['EXPORT_PATH'] / filename).unlink()
g.logger.log('info', 'file', 'SQL file deleted')
flash(_('file deleted'), 'info')
except Exception as e:
Expand Down
6 changes: 3 additions & 3 deletions openatlas/views/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@required_group('readonly')
def download_file(filename: str) -> Any:
return send_from_directory(
app.config['UPLOAD_DIR'],
app.config['UPLOAD_PATH'],
filename,
as_attachment=True)

Expand All @@ -27,12 +27,12 @@ def display_file(filename: str) -> Any:
return send_from_directory(
app.config['RESIZED_IMAGES'] / request.args.get('size'),
filename)
return send_from_directory(app.config['UPLOAD_DIR'], filename)
return send_from_directory(app.config['UPLOAD_PATH'], filename)


@app.route('/display_logo/<path:filename>')
def display_logo(filename: str) -> Any:
return send_from_directory(app.config['UPLOAD_DIR'], filename)
return send_from_directory(app.config['UPLOAD_PATH'], filename)


@app.route('/file/set_profile_image/<int:id_>/<int:origin_id>')
Expand Down
2 changes: 1 addition & 1 deletion openatlas/views/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def import_data(project_id: int, class_: str) -> str:
class_label = g.classes[class_].label
if form.validate_on_submit():
file_ = request.files['file']
file_path = app.config['TMP_DIR'] / secure_filename(file_.filename)
file_path = app.config['TMP_PATH'] / secure_filename(file_.filename)
columns: dict[str, list[str]] = {
'allowed': ['name', 'id', 'description'],
'valid': [],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_admin(self) -> None:
assert b'Invalid linked entity' in rv.data

file_ = 'Test77.txt'
with open(Path(app.config['UPLOAD_DIR'] / file_), 'w') as _file:
with open(Path(app.config['UPLOAD_PATH'] / file_), 'w') as _file:
pass

rv = self.app.get(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ def test_image(self) -> None:
copyfile(
Path(app.root_path)
/ 'static' / 'images' / 'layout' / 'logo.png',
Path(app.config['UPLOAD_DIR'] / file_name))
Path(app.config['UPLOAD_PATH'] / file_name))
file2 = insert('file', 'Test_File2')
file2.link('P2', g.types[get_hierarchy('License').subs[0]])
copyfile(
Path(app.root_path)
/ 'static' / 'images' / 'layout' / 'logo.png',
Path(app.config['UPLOAD_DIR'] / f'{file2.id}.jpeg'))
Path(app.config['UPLOAD_PATH'] / f'{file2.id}.jpeg'))
file_json = insert('file', 'Test')
copyfile(
Path(app.root_path) / 'static' / 'manifest.json',
Path(app.config['UPLOAD_DIR'] / f'{file_json.id}.json'))
Path(app.config['UPLOAD_PATH'] / f'{file_json.id}.json'))
safe_resize_image(file2.id, '.png', size="???")
profile_image(file_pathless)

Expand Down

0 comments on commit d5fa284

Please sign in to comment.