Skip to content

Commit

Permalink
Make data and metadata read-only.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anteru committed Dec 24, 2024
1 parent 3caa39f commit b07c478
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 6 additions & 2 deletions liara/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import logging
import fnmatch
from abc import abstractmethod, ABC
from types import MappingProxyType


def _create_metadata_accessor(field_name):
Expand Down Expand Up @@ -779,9 +780,12 @@ def select(self, query: str) -> Iterable[Node]:
return [node]

@property
def merged_data(self) -> Dict[str, Any]:
def merged_data(self) -> MappingProxyType[str, Any]:
"""Return the union of all data nodes.
This is a read-only view, as shortcodes and templates can access the
data in any order and thus modifications would be unsafe.
.. versionadded:: 2.6.2
"""
return self.__merged_data
return MappingProxyType(self.__merged_data)
15 changes: 12 additions & 3 deletions liara/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Dict,
Optional,
)
from types import MappingProxyType

from abc import abstractmethod, ABC

Expand Down Expand Up @@ -289,17 +290,25 @@ def __init__(self, site: 'Site'):
self.__data = site.merged_data

@property
def data(self) -> Dict[str, Any]:
def data(self) -> MappingProxyType[str, Any]:
"""Get the union of all :py:class:`liara.nodes.DataNode`
instances in this site.
.. versionchanged:: 2.6.2 Made read-only
Before 2.6.2, this dictionary could be modified, but that was always
an unsafe operation.
"""
return self.__data

@property
def metadata(self) -> Dict[str, Any]:
def metadata(self) -> MappingProxyType[str, Any]:
"""Provide access to the metadata of this site.
.. versionchanged:: 2.6.2 Made read-only
Before 2.6.2, this dictionary could be modified, but that was always
an unsafe operation.
"""
return self.__site.metadata
return MappingProxyType(self.__site.metadata)

def select(self, query) -> 'Query':
"""Run a query on this site. Returns any node matching the query.
Expand Down

0 comments on commit b07c478

Please sign in to comment.