mirror of
https://github.com/simonw/datasette.git
synced 2026-06-12 03:57:00 +02:00
Use plain set literals for Extra scopes
frozenset({...}) was immutability ceremony for class attributes that
nothing mutates. scopes = {ExtraScope.TABLE} reads cleaner.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
a1b6a6976d
commit
cfafa5b37f
2 changed files with 38 additions and 38 deletions
|
|
@ -30,7 +30,7 @@ class ExtraExample:
|
|||
|
||||
class Provider:
|
||||
name: ClassVar[str | None] = None
|
||||
scopes: ClassVar[frozenset[ExtraScope]] = frozenset()
|
||||
scopes: ClassVar[set[ExtraScope]] = set()
|
||||
public: ClassVar[bool] = False
|
||||
|
||||
@classmethod
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ class QueryExtraContext:
|
|||
class CountSqlExtra(Extra):
|
||||
description = "SQL query used to calculate the total count"
|
||||
example = ExtraExample("/fixtures/facetable.json?_size=0&_extra=count_sql")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.count_sql
|
||||
|
|
@ -109,7 +109,7 @@ class CountSqlExtra(Extra):
|
|||
class CountExtra(Extra):
|
||||
description = "Total count of rows matching these filters"
|
||||
example = ExtraExample("/fixtures/facetable.json?_extra=count")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
expensive = True
|
||||
|
||||
async def resolve(self, context):
|
||||
|
|
@ -141,7 +141,7 @@ class CountExtra(Extra):
|
|||
|
||||
|
||||
class FacetInstancesProvider(Provider):
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context, count):
|
||||
facet_instances = []
|
||||
|
|
@ -182,7 +182,7 @@ class FacetResultsExtra(Extra):
|
|||
},
|
||||
note="Shape abbreviated from /fixtures/facetable.json?_facet=state&_extra=facet_results.",
|
||||
)
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
expensive = True
|
||||
|
||||
async def resolve(self, context, facet_instances):
|
||||
|
|
@ -217,7 +217,7 @@ class FacetsTimedOutExtra(Extra):
|
|||
example = ExtraExample(
|
||||
"/fixtures/facetable.json?_facet=state&_extra=facets_timed_out"
|
||||
)
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context, facet_results):
|
||||
return facet_results["timed_out"]
|
||||
|
|
@ -234,7 +234,7 @@ class SuggestedFacetsExtra(Extra):
|
|||
],
|
||||
note="Shape abbreviated from /fixtures/facetable.json?_extra=suggested_facets.",
|
||||
)
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
expensive = True
|
||||
|
||||
async def resolve(self, context, facet_instances):
|
||||
|
|
@ -259,7 +259,7 @@ class HumanDescriptionEnExtra(Extra):
|
|||
example = ExtraExample(
|
||||
"/fixtures/facetable.json?state=CA&_sort=pk&_extra=human_description_en"
|
||||
)
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
human_description_en = context.filters.human_description_en(
|
||||
|
|
@ -279,7 +279,7 @@ class HumanDescriptionEnExtra(Extra):
|
|||
class NextUrlExtra(Extra):
|
||||
description = "Full URL for the next page of results"
|
||||
example = ExtraExample("/fixtures/facetable.json?_size=1&_extra=next_url")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.next_url
|
||||
|
|
@ -296,7 +296,7 @@ class ColumnsExtra(Extra):
|
|||
"/fixtures/-/query.json?sql=select+1+as+one&_extra=columns"
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.columns
|
||||
|
|
@ -305,7 +305,7 @@ class ColumnsExtra(Extra):
|
|||
class AllColumnsExtra(Extra):
|
||||
description = "All columns in the table, regardless of _col/_nocol filtering"
|
||||
example = ExtraExample("/fixtures/facetable.json?_col=pk&_extra=all_columns")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
return list(context.table_columns)
|
||||
|
|
@ -319,7 +319,7 @@ class PrimaryKeysExtra(Extra):
|
|||
"/fixtures/simple_primary_key/1.json?_extra=primary_keys"
|
||||
)
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.pks
|
||||
|
|
@ -327,7 +327,7 @@ class PrimaryKeysExtra(Extra):
|
|||
|
||||
class ActionsExtra(Extra):
|
||||
description = "Table or view actions made available by plugin hooks"
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
# Returns an async function for the HTML templates - not JSON serializable
|
||||
public = False
|
||||
|
||||
|
|
@ -358,7 +358,7 @@ class ActionsExtra(Extra):
|
|||
class IsViewExtra(Extra):
|
||||
description = "Whether this resource is a view instead of a table"
|
||||
example = ExtraExample("/fixtures/simple_view.json?_extra=is_view")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.is_view
|
||||
|
|
@ -375,7 +375,7 @@ class DebugExtra(Extra):
|
|||
"/fixtures/-/query.json?sql=select+1+as+one&_extra=debug"
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
debug = {
|
||||
|
|
@ -407,7 +407,7 @@ class RequestExtra(Extra):
|
|||
"/fixtures/-/query.json?sql=select+1+as+one&_extra=request"
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
return {
|
||||
|
|
@ -420,7 +420,7 @@ class RequestExtra(Extra):
|
|||
|
||||
|
||||
class DisplayColumnsAndRowsProvider(Provider):
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
display_columns, display_rows = await context.display_columns_and_rows(
|
||||
|
|
@ -464,7 +464,7 @@ class DisplayColumnsExtra(Extra):
|
|||
],
|
||||
note="Shape abbreviated from /fixtures/facetable.json?_size=1&_extra=display_columns.",
|
||||
)
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context, display_columns_and_rows):
|
||||
return display_columns_and_rows["columns"]
|
||||
|
|
@ -472,7 +472,7 @@ class DisplayColumnsExtra(Extra):
|
|||
|
||||
class DisplayRowsExtra(Extra):
|
||||
description = "Row data formatted for the HTML table display"
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
# Contains markupsafe/sqlite3.Row values - not JSON serializable
|
||||
public = False
|
||||
|
||||
|
|
@ -524,7 +524,7 @@ class RenderCellExtra(Extra):
|
|||
),
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
table_name = context.table_name
|
||||
|
|
@ -587,7 +587,7 @@ class QueryExtra(Extra):
|
|||
ExtraExample("/fixtures/neighborhood_search.json?text=town&_extra=query"),
|
||||
],
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
return {
|
||||
|
|
@ -599,7 +599,7 @@ class QueryExtra(Extra):
|
|||
class ColumnTypesExtra(Extra):
|
||||
description = "Column type assignments for this table"
|
||||
example = ExtraExample(value={})
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW}
|
||||
|
||||
async def resolve(self, context):
|
||||
ct_map = await context.datasette.get_column_types(
|
||||
|
|
@ -616,7 +616,7 @@ class ColumnTypesExtra(Extra):
|
|||
|
||||
class SetColumnTypeUiExtra(Extra):
|
||||
description = "Column type UI metadata for this table"
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
if context.is_view:
|
||||
|
|
@ -676,7 +676,7 @@ class MetadataExtra(Extra):
|
|||
"/fixtures/neighborhood_search.json?text=town&_extra=metadata"
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
if context.scope == ExtraScope.QUERY:
|
||||
|
|
@ -713,7 +713,7 @@ class DatabaseExtra(Extra):
|
|||
"/fixtures/-/query.json?sql=select+1+as+one&_extra=database"
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.database_name
|
||||
|
|
@ -725,7 +725,7 @@ class TableExtra(Extra):
|
|||
examples = {
|
||||
ExtraScope.ROW: ExtraExample("/fixtures/simple_primary_key/1.json?_extra=table")
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.table_name
|
||||
|
|
@ -742,7 +742,7 @@ class DatabaseColorExtra(Extra):
|
|||
"/fixtures/-/query.json?sql=select+1+as+one&_extra=database_color"
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.db.color
|
||||
|
|
@ -753,7 +753,7 @@ class FormHiddenArgsExtra(Extra):
|
|||
example = ExtraExample(
|
||||
"/fixtures/facetable.json?_facet=state&_size=1&_extra=form_hidden_args"
|
||||
)
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
form_hidden_args = []
|
||||
|
|
@ -770,7 +770,7 @@ class FormHiddenArgsExtra(Extra):
|
|||
|
||||
class FiltersExtra(Extra):
|
||||
description = "Filters object used by the HTML table interface"
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
# Returns a Filters instance for the HTML templates - not JSON serializable
|
||||
public = False
|
||||
|
||||
|
|
@ -781,7 +781,7 @@ class FiltersExtra(Extra):
|
|||
class CustomTableTemplatesExtra(Extra):
|
||||
description = "Custom template names considered for this table"
|
||||
example = ExtraExample("/fixtures/facetable.json?_extra=custom_table_templates")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
return [
|
||||
|
|
@ -796,7 +796,7 @@ class SortedFacetResultsExtra(Extra):
|
|||
example = ExtraExample(
|
||||
"/fixtures/facetable.json?_facet=state&_extra=sorted_facet_results"
|
||||
)
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context, facet_results):
|
||||
facet_configs = context.table_metadata.get("facets", [])
|
||||
|
|
@ -832,7 +832,7 @@ class SortedFacetResultsExtra(Extra):
|
|||
class TableDefinitionExtra(Extra):
|
||||
description = "SQL definition for this table"
|
||||
example = ExtraExample("/fixtures/facetable.json?_extra=table_definition")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
return await context.db.get_table_definition(context.table_name)
|
||||
|
|
@ -841,7 +841,7 @@ class TableDefinitionExtra(Extra):
|
|||
class ViewDefinitionExtra(Extra):
|
||||
description = "SQL definition for this view"
|
||||
example = ExtraExample("/fixtures/simple_view.json?_extra=view_definition")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
return await context.db.get_view_definition(context.table_name)
|
||||
|
|
@ -850,7 +850,7 @@ class ViewDefinitionExtra(Extra):
|
|||
class RenderersExtra(Extra):
|
||||
description = "Alternative output renderers available for this table"
|
||||
example = ExtraExample("/fixtures/facetable.json?_extra=renderers")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context, expandable_columns, query):
|
||||
renderers = {}
|
||||
|
|
@ -896,7 +896,7 @@ class PrivateExtra(Extra):
|
|||
"/fixtures/-/query.json?sql=select+1+as+one&_extra=private"
|
||||
),
|
||||
}
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
return context.private
|
||||
|
|
@ -905,7 +905,7 @@ class PrivateExtra(Extra):
|
|||
class ExpandableColumnsExtra(Extra):
|
||||
description = "Foreign key columns that can be expanded with labels"
|
||||
example = ExtraExample("/fixtures/facetable.json?_extra=expandable_columns")
|
||||
scopes = frozenset({ExtraScope.TABLE})
|
||||
scopes = {ExtraScope.TABLE}
|
||||
|
||||
async def resolve(self, context):
|
||||
expandables = []
|
||||
|
|
@ -921,7 +921,7 @@ class ForeignKeyTablesExtra(Extra):
|
|||
example = ExtraExample(
|
||||
"/fixtures/simple_primary_key/1.json?_extra=foreign_key_tables"
|
||||
)
|
||||
scopes = frozenset({ExtraScope.ROW})
|
||||
scopes = {ExtraScope.ROW}
|
||||
|
||||
async def resolve(self, context):
|
||||
return await context.foreign_key_tables(
|
||||
|
|
@ -931,7 +931,7 @@ class ForeignKeyTablesExtra(Extra):
|
|||
|
||||
class ExtrasExtra(Extra):
|
||||
description = "Available ?_extra= blocks"
|
||||
scopes = frozenset({ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY})
|
||||
scopes = {ExtraScope.TABLE, ExtraScope.ROW, ExtraScope.QUERY}
|
||||
|
||||
async def resolve(self, context):
|
||||
all_extras = [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue