Skip to content

Commit 0e1a04e

Browse files
committed
Merge remote-tracking branch 'origin/master' into prod
2 parents 4a0828f + 8a58b63 commit 0e1a04e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2977
-3059
lines changed

.pre-commit-config.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
default_language_version:
2+
python: python3.12
3+
repos:
4+
- repo: local
5+
hooks:
6+
- id: ruff_check
7+
name: ruff check
8+
entry: ruff check --force-exclude
9+
language: python
10+
'types_or': [python, pyi]
11+
args: [--fix, --exit-non-zero-on-fix]
12+
require_serial: true
13+
- id: ruff_format
14+
name: ruff format
15+
entry: ruff format --force-exclude
16+
language: python
17+
'types_or': [python, pyi]
18+
args: []
19+
require_serial: true
20+
- id: check-added-large-files
21+
name: Check for added large files
22+
entry: check-added-large-files
23+
language: system
24+
- id: check-toml
25+
name: Check Toml
26+
entry: check-toml
27+
language: system
28+
types: [toml]
29+
- id: check-yaml
30+
name: Check Yaml
31+
entry: check-yaml
32+
language: system
33+
types: [yaml]
34+
- id: end-of-file-fixer
35+
name: Fix End of Files
36+
entry: end-of-file-fixer
37+
language: system
38+
types: [text]
39+
stages: [commit, push, manual]
40+
- id: trailing-whitespace
41+
name: Trim Trailing Whitespace
42+
entry: trailing-whitespace-fixer
43+
language: system
44+
types: [text]
45+
stages: [commit, push, manual]
46+
- id: mypy
47+
name: mypy
48+
entry: mypy
49+
language: python
50+
'types_or': [python, pyi]
51+
args: []
52+
require_serial: true
53+
exclude: 'snodas/settings/*'

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM postgis/postgis:15-3.3
1+
FROM postgis/postgis:16-3.4
22

33
RUN set -x && \
44
apt-get update && \

MANIFEST.in

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
recursive-include snodas *
2-
include *.md
3-
include *.txt
1+
graft snodas
2+
global-exclude *.py[cod]

VERSION.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

bin/compile-requirements.bash

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
6+
find_this () {
7+
THIS="${1:?'must provide script path, like "${BASH_SOURCE[0]}" or "$0"'}"
8+
trap "echo >&2 'FATAL: could not resolve parent directory of ${THIS}'" EXIT
9+
[ "${THIS:0:1}" == "/" ] || THIS="$(pwd -P)/${THIS}"
10+
THIS_DIR="$(dirname -- "${THIS}")"
11+
THIS_DIR="$(cd -P -- "${THIS_DIR}" && pwd)"
12+
THIS="${THIS_DIR}/$(basename -- "${THIS}")"
13+
trap "" EXIT
14+
}
15+
16+
17+
maybe_print_help() {
18+
for arg in "$@"; do
19+
case "$arg" in
20+
-h|--help)
21+
local help
22+
help="$(pip-compile --help | tail -n +2)"
23+
echo >&2 "Usage: $CUSTOM_COMPILE_COMMAND [OPTIONS] [SRC_FILES]..."
24+
echo >&2 ""
25+
echo >&2 "This is a script wrapping pip-compile."
26+
echo >&2 "The following is the pip-compile usage."
27+
echo >&2 "$help"
28+
exit
29+
;;
30+
esac
31+
done
32+
}
33+
34+
35+
main () {
36+
export CUSTOM_COMPILE_COMMAND="$0"
37+
find_this "${BASH_SOURCE[0]}"
38+
39+
maybe_print_help "$@"
40+
41+
cd "${THIS_DIR}/.."
42+
43+
local compile="pip-compile --strip-extras --generate-hashes --allow-unsafe"
44+
45+
$compile pyproject.toml "$@"
46+
47+
local infile
48+
for infile in *.in; do
49+
[ "$infile" == 'MANIFEST.in' ] && continue
50+
$compile -o "${infile%.*}.txt" "${infile}" "$@"
51+
done
52+
}
53+
54+
55+
main "$@"

bin/settings-yml-to-json-files.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ services:
44
build: .
55
restart: always
66
environment:
7-
- POSTGRES_USER=${PGUSER:-postgres}
8-
- POSTGRES_PASSWORD=${PGPASSWORD:-password}
7+
POSTGRES_USER: "${PGUSER:-postgres}"
8+
POSTGRES_PASSWORD: "${PGPASSWORD:-password}"
9+
POSTGIS_GDAL_ENABLED_DRIVERS: 'GTiff PNG SNODAS'
910
ports:
1011
- '${PGPORT:-5432}:5432'

manage.py

Lines changed: 62 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,52 @@
11
#!/usr/bin/env python
2-
import os
3-
import sys
42
import logging
5-
import datetime
63
import logging.handlers
4+
import os
5+
import sys
76

7+
from pathlib import Path
88

99
logger = logging.getLogger(__name__)
1010
LOG_SIZE = 50 * (10**6) # about 50 MB
1111
LOG_COUNT = 5
12-
LOG_FILE = os.path.join(os.path.dirname(__file__), 'manage.log')
1312

14-
CONF_FILE = os.path.join(os.path.dirname(__file__), 'project.conf')
13+
THIS = Path(__file__).resolve()
14+
THIS_DIR = THIS.parent
15+
LOG_FILE = THIS_DIR / 'manage.log'
16+
CONF_FILE = THIS_DIR / 'project.conf'
1517

1618

17-
def activate_help():
18-
import yaml
19-
with open(CONF_FILE) as f:
20-
instance_name = yaml.load(f)['INSTANCE_NAME']
21-
return (
22-
'ERROR: snodas.settings could not be imported.\n'
23-
'It looks like you need to activate the conda environment '
24-
'for this instance, which you can do by running '
25-
'`activate {}`'.format(instance_name)
26-
)
19+
ACTIVATE_HELP: str = (
20+
'ERROR: snodas.settings could not be imported.\n'
21+
'It looks like you need to activate the proper conda/virtual environment.'
22+
)
2723

2824

2925
def install(help=False):
30-
this_dir = os.path.dirname(os.path.realpath(__file__))
3126
# first we append the path with the management package
3227
# so we can import utils in the install module
33-
sys.path.append(
34-
os.path.join(
35-
this_dir,
36-
'snodas',
37-
'management',
38-
)
39-
)
28+
sys.path.append(str(THIS_DIR / 'snodas' / 'management'))
29+
4030
# then we add the commands package to the path
4131
# so we have access to the install module
42-
sys.path.append(
43-
os.path.join(
44-
this_dir,
45-
'snodas',
46-
'management',
47-
'commands',
48-
)
49-
)
32+
sys.path.append(str(THIS_DIR / 'snodas' / 'management' / 'commands'))
33+
5034
# and lastly we add the directory of this file
5135
# to the path so we can import from setup.py
52-
sys.path.append(
53-
os.path.join(
54-
this_dir,
55-
)
56-
)
57-
from install import Install
36+
sys.path.append(str(THIS_DIR))
37+
38+
from install import Install # type: ignore
39+
5840
if help:
5941
Install.print_help(sys.argv[0], sys.argv[2])
6042
else:
6143
Install.run_from_argv(sys.argv)
6244

6345

6446
def default_django_command():
65-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "snodas.settings")
47+
from importlib.util import find_spec
48+
49+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'snodas.settings')
6650

6751
# check to make sure we can import the settings
6852
# otherwise, we suspect the env has not been activated
@@ -71,14 +55,12 @@ def default_django_command():
7155
# activated and this would not prevent execution. I should
7256
# do some sort of more advanced check of the settings to verify that
7357
# they match the current project.
74-
try:
75-
import snodas.settings
76-
except ImportError:
58+
if find_spec('snodas.settings') is None:
7759
logger.debug(
78-
'snodas.settings couldn\'t be imported; '
79-
'looks like the correct env is not activated'
60+
"snodas.settings couldn't be imported; "
61+
'looks like the correct env is not activated',
8062
)
81-
print(activate_help())
63+
print(ACTIVATE_HELP) # noqa: T201
8264
return 1
8365

8466
# hacky workaround to allow snodas command to use the
@@ -89,20 +71,23 @@ def default_django_command():
8971
# which fails either because snodas cannot be found, or
9072
# sondas is found, and it is the snodas package in the
9173
# current directory.)
92-
if len(sys.argv) > 1 and \
93-
sys.argv[1] == 'runserver' and \
94-
not ('--help' in sys.argv or '-h' in sys.argv):
74+
if (
75+
len(sys.argv) > 1
76+
and sys.argv[1] == 'runserver'
77+
and not ('--help' in sys.argv or '-h' in sys.argv)
78+
):
9579
logger.debug(
96-
'rewritting sys.argv[0] from {} to {}'.format(
97-
sys.argv[0],
98-
__file__,
99-
)
80+
'rewritting sys.argv[0] from %s to %s',
81+
sys.argv[0],
82+
__file__,
10083
)
10184
sys.argv[0] = __file__
10285

103-
from django.core.management import execute_from_command_line
86+
from django.core.management import execute_from_command_line # type: ignore
87+
10488
logger.debug(
105-
'executing from the command line with sys.argv: {}'.format(sys.argv)
89+
'executing from the command line with sys.argv: %s',
90+
sys.argv,
10691
)
10792
return execute_from_command_line(sys.argv)
10893

@@ -114,41 +99,40 @@ def main():
11499
backupCount=(LOG_COUNT - 1),
115100
)
116101
formatter = logging.Formatter(
117-
'%(asctime)s %(name)s [%(levelname)-5s] %(message)s'
102+
'%(asctime)s %(name)s [%(levelname)-5s] %(message)s',
118103
)
119104
manage_log.setFormatter(formatter)
120105
logger.setLevel(logging.DEBUG)
121106
logger.addHandler(manage_log)
122-
#logging.basicConfig(
123-
# filename=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'manage_%s_%d.log' %(datetime.datetime.now().strftime('%y%m%d_%H%M%S'), os.getpid())),
124-
# filemode='w',
125-
# format='%(asctime)s [%(levelname)-5s] %(message)s',
126-
# level=logging.DEBUG,
127-
#)
128107

129108
if len(sys.argv) > 1 and sys.argv[1] == 'install':
130109
logger.debug('install command run')
131110
return install()
132-
if len(sys.argv) > 1 and \
133-
os.path.basename(sys.argv[0]) == 'manage.py' and \
134-
sys.argv[1] == 'help' and \
135-
sys.argv[2] == 'install':
111+
112+
if (
113+
len(sys.argv) > 1
114+
and Path(sys.argv[0]).name == 'manage.py'
115+
and sys.argv[1] == 'help'
116+
and sys.argv[2] == 'install'
117+
):
136118
logger.debug('install help run')
137119
return install(help=True)
138-
elif not os.path.isfile(CONF_FILE):
139-
logger.error('config file could not be loaded: {}'.format(CONF_FILE))
140-
print('ERROR: Could not find configuration file {}.'.format(CONF_FILE))
141-
print('Has this instance been installed? '
142-
'Try running `python manage.py install`.')
143-
else:
144-
try:
145-
return default_django_command()
146-
except Exception as e:
147-
logger.exception(e)
148-
raise
149-
# before I was returning -1, but I don't know why anymore
150-
return -1
120+
121+
if not CONF_FILE.is_file():
122+
logger.error('config file could not be loaded: %s', CONF_FILE)
123+
print(f'ERROR: Could not find configuration file {CONF_FILE}.') # noqa: T201
124+
print( # noqa: T201
125+
'Has this instance been installed? '
126+
'Try running `python manage.py install`.',
127+
)
128+
return None
129+
130+
try:
131+
return default_django_command()
132+
except Exception:
133+
logger.exception('Failure running CLI')
134+
raise
151135

152136

153-
if __name__ == "__main__":
137+
if __name__ == '__main__':
154138
sys.exit(main())

0 commit comments

Comments
 (0)