Skip to content

Commit 3ea3a49

Browse files
committed
Use black code formatter
1 parent 2860533 commit 3ea3a49

File tree

19 files changed

+396
-363
lines changed

19 files changed

+396
-363
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: https://github.com/ambv/black
3+
rev: 19.10b0
4+
hooks:
5+
- id: black
6+
language_version: python3.7

django_pgviews/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
default_app_config = 'django_pgviews.apps.ViewConfig'
1+
default_app_config = "django_pgviews.apps.ViewConfig"

django_pgviews/apps.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@
33
from django import apps
44
from django.db.models import signals
55

6-
log = logging.getLogger('django_pgviews.sync_pgviews')
6+
log = logging.getLogger("django_pgviews.sync_pgviews")
77

88

99
class ViewConfig(apps.AppConfig):
1010
"""The base configuration for Django PGViews. We use this to setup our
1111
post_migrate signal handlers.
1212
"""
13+
1314
counter = 0
14-
name = 'django_pgviews'
15-
verbose_name = 'Django Postgres Views'
15+
name = "django_pgviews"
16+
verbose_name = "Django Postgres Views"
1617

1718
def sync_pgviews(self, sender, app_config, **kwargs):
1819
"""Forcibly sync the views.
1920
"""
2021
self.counter = self.counter + 1
2122
total = len([a for a in apps.apps.get_app_configs() if a.models_module is not None])
22-
23+
2324
if self.counter == total:
24-
log.info('All applications have migrated, time to sync')
25+
log.info("All applications have migrated, time to sync")
2526
# Import here otherwise Django doesn't start properly
2627
# (models in app init are not allowed)
2728
from .models import ViewSyncer
29+
2830
vs = ViewSyncer()
29-
vs.run(force=True, update=True)
31+
vs.run(force=True, update=True)
3032

3133
def ready(self):
3234
"""Find and setup the apps to set the post_migrate hooks for.

django_pgviews/db/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def get_fields_by_name(model_cls, *field_names):
1212
...,
1313
'date_joined': <django.db.models.fields.DateTimeField: date_joined>}
1414
"""
15-
if '*' in field_names:
15+
if "*" in field_names:
1616
return dict((field.name, field) for field in model_cls._meta.fields)
17-
return dict((field_name, model_cls._meta.get_field(field_name))
18-
for field_name in field_names)
17+
return dict((field_name, model_cls._meta.get_field(field_name)) for field_name in field_names)

django_pgviews/db/sql/compiler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class NonQuotingCompiler(compiler.SQLCompiler):
55
"""Compiler for functions/statements that doesn't quote the db_table
66
attribute.
77
"""
8+
89
def quote_name_unless_alias(self, name):
910
"""Don't quote the name.
1011
"""
@@ -17,5 +18,5 @@ def quote_name_unless_alias(self, name):
1718
def as_sql(self, *args, **kwargs):
1819
"""Messy hack to create some table aliases for us.
1920
"""
20-
self.query.table_map[self.query.model._meta.db_table] = ['']
21+
self.query.table_map[self.query.model._meta.db_table] = [""]
2122
return super(NonQuotingCompiler, self).as_sql(*args, **kwargs)

django_pgviews/db/sql/query.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
class NonQuotingQuery(query.Query):
88
"""Query class that uses the NonQuotingCompiler.
99
"""
10+
1011
def get_compiler(self, using=None, connection=None):
1112
"""Get the NonQuotingCompiler object.
1213
"""
1314
if using is None and connection is None:
14-
raise ValueError('Need either using or connection')
15+
raise ValueError("Need either using or connection")
1516
if using:
1617
connection = connections[using]
1718

django_pgviews/management/commands/clear_pgviews.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django_pgviews.view import clear_view, View, MaterializedView
88

99

10-
log = logging.getLogger('django_pgviews.sync_pgviews')
10+
log = logging.getLogger("django_pgviews.sync_pgviews")
1111

1212

1313
class Command(BaseCommand):
@@ -17,19 +17,17 @@ def handle(self, **options):
1717
"""
1818
"""
1919
for view_cls in apps.get_models():
20-
if not (isinstance(view_cls, type) and
21-
issubclass(view_cls, View) and
22-
hasattr(view_cls, 'sql')):
20+
if not (isinstance(view_cls, type) and issubclass(view_cls, View) and hasattr(view_cls, "sql")):
2321
continue
24-
python_name = '{}.{}'.format(view_cls._meta.app_label, view_cls.__name__)
22+
python_name = "{}.{}".format(view_cls._meta.app_label, view_cls.__name__)
2523
status = clear_view(
26-
connection, view_cls._meta.db_table,
27-
materialized=isinstance(view_cls(), MaterializedView))
28-
if status == 'DROPPED':
29-
msg = 'dropped'
24+
connection, view_cls._meta.db_table, materialized=isinstance(view_cls(), MaterializedView)
25+
)
26+
if status == "DROPPED":
27+
msg = "dropped"
3028
else:
31-
msg = 'not dropped'
32-
log.info("%(python_name)s (%(view_name)s): %(msg)s" % {
33-
'python_name': python_name,
34-
'view_name': view_cls._meta.db_table,
35-
'msg': msg})
29+
msg = "not dropped"
30+
log.info(
31+
"%(python_name)s (%(view_name)s): %(msg)s"
32+
% {"python_name": python_name, "view_name": view_cls._meta.db_table, "msg": msg}
33+
)

django_pgviews/management/commands/sync_pgviews.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,28 @@
88
from django_pgviews.models import ViewSyncer
99

1010

11-
log = logging.getLogger('django_pgviews.sync_pgviews')
11+
log = logging.getLogger("django_pgviews.sync_pgviews")
1212

1313

1414
class Command(BaseCommand):
1515
help = """Create/update Postgres views for all installed apps."""
16-
16+
1717
def add_arguments(self, parser):
18-
parser.add_argument('--no-update',
19-
action='store_false',
20-
dest='update',
18+
parser.add_argument(
19+
"--no-update",
20+
action="store_false",
21+
dest="update",
2122
default=True,
22-
help="""Don't update existing views, only create new ones.""")
23-
parser.add_argument('--force',
24-
action='store_true',
25-
dest='force',
23+
help="""Don't update existing views, only create new ones.""",
24+
)
25+
parser.add_argument(
26+
"--force",
27+
action="store_true",
28+
dest="force",
2629
default=False,
2730
help="""Force replacement of pre-existing views where
28-
breaking changes have been made to the schema.""")
31+
breaking changes have been made to the schema.""",
32+
)
2933

3034
def handle(self, force, update, **options):
3135
vs = ViewSyncer()

django_pgviews/models.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
from django_pgviews.view import create_view, View, MaterializedView
77
from django_pgviews.signals import view_synced, all_views_synced
88

9-
log = logging.getLogger('django_pgviews.sync_pgviews')
9+
log = logging.getLogger("django_pgviews.sync_pgviews")
1010

1111

1212
class ViewSyncer(object):
1313
def run(self, force, update, **options):
1414
self.synced = []
1515
backlog = []
1616
for view_cls in apps.get_models():
17-
if not (isinstance(view_cls, type) and
18-
issubclass(view_cls, View) and
19-
hasattr(view_cls, 'sql')):
17+
if not (isinstance(view_cls, type) and issubclass(view_cls, View) and hasattr(view_cls, "sql")):
2018
continue
2119
backlog.append(view_cls)
2220
loop = 0
@@ -25,57 +23,62 @@ def run(self, force, update, **options):
2523
backlog = self.run_backlog(backlog, force, update)
2624

2725
if loop >= 10:
28-
log.warn('pgviews dependencies hit limit. Check if your model dependencies are correct')
26+
log.warn("pgviews dependencies hit limit. Check if your model dependencies are correct")
2927
else:
3028
all_views_synced.send(sender=None)
3129

3230
def run_backlog(self, models, force, update):
33-
'''Installs the list of models given from the previous backlog
31+
"""Installs the list of models given from the previous backlog
3432
3533
If the correct dependent views have not been installed, the view
3634
will be added to the backlog.
3735
3836
Eventually we get to a point where all dependencies are sorted.
39-
'''
37+
"""
4038
backlog = []
4139
for view_cls in models:
4240
skip = False
43-
name = '{}.{}'.format(view_cls._meta.app_label, view_cls.__name__)
41+
name = "{}.{}".format(view_cls._meta.app_label, view_cls.__name__)
4442
for dep in view_cls._dependencies:
4543
if dep not in self.synced:
4644
skip = True
4745
if skip is True:
4846
backlog.append(view_cls)
49-
log.info('Putting pgview at back of queue: %s', name)
50-
continue # Skip
47+
log.info("Putting pgview at back of queue: %s", name)
48+
continue # Skip
5149

5250
try:
53-
status = create_view(connection, view_cls._meta.db_table,
54-
view_cls.sql, update=update, force=force,
55-
materialized=isinstance(view_cls(), MaterializedView),
56-
index=view_cls._concurrent_index)
51+
status = create_view(
52+
connection,
53+
view_cls._meta.db_table,
54+
view_cls.sql,
55+
update=update,
56+
force=force,
57+
materialized=isinstance(view_cls(), MaterializedView),
58+
index=view_cls._concurrent_index,
59+
)
5760
view_synced.send(
58-
sender=view_cls, update=update, force=force, status=status,
59-
has_changed=status not in ('EXISTS', 'FORCE_REQUIRED'))
61+
sender=view_cls,
62+
update=update,
63+
force=force,
64+
status=status,
65+
has_changed=status not in ("EXISTS", "FORCE_REQUIRED"),
66+
)
6067
self.synced.append(name)
6168
except Exception as exc:
6269
exc.view_cls = view_cls
6370
exc.python_name = name
6471
raise
6572
else:
66-
if status == 'CREATED':
73+
if status == "CREATED":
6774
msg = "created"
68-
elif status == 'UPDATED':
75+
elif status == "UPDATED":
6976
msg = "updated"
70-
elif status == 'EXISTS':
77+
elif status == "EXISTS":
7178
msg = "already exists, skipping"
72-
elif status == 'FORCED':
79+
elif status == "FORCED":
7380
msg = "forced overwrite of existing schema"
74-
elif status == 'FORCE_REQUIRED':
75-
msg = (
76-
"exists with incompatible schema, "
77-
"--force required to update")
78-
log.info("pgview %(python_name)s %(msg)s" % {
79-
'python_name': name,
80-
'msg': msg})
81+
elif status == "FORCE_REQUIRED":
82+
msg = "exists with incompatible schema, " "--force required to update"
83+
log.info("pgview %(python_name)s %(msg)s" % {"python_name": name, "msg": msg})
8184
return backlog

django_pgviews/signals.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.dispatch import Signal
22

33

4-
view_synced = Signal(
5-
providing_args=['update', 'force', 'status', 'has_changed'])
4+
view_synced = Signal(providing_args=["update", "force", "status", "has_changed"])
65
all_views_synced = Signal()

0 commit comments

Comments
 (0)