@@ -18,20 +18,38 @@ def __init__(self, all_attributes_private: bool, private_attributes: List[str]):
1818 self ._private_attributes .append (ar )
1919
2020 def format_context (self , context : Context ) -> Dict :
21+ """
22+ Formats a context for use in an analytic event, performing any
23+ necessary attribute redaction.
24+ """
25+ return self ._format_context (context , False )
26+
27+ def format_context_redact_anonymous (self , context : Context ) -> Dict :
28+ """
29+ Formats a context for use in an analytic event, performing any
30+ necessary attribute redaction.
31+
32+ If a context is anonoymous, all attributes will be redacted except for
33+ key, kind, and anonoymous.
34+ """
35+ return self ._format_context (context , True )
36+
37+ def _format_context (self , context : Context , redact_anonymous : bool ) -> Dict :
2138 if context .multiple :
2239 out = {'kind' : 'multi' } # type: Dict[str, Any]
2340 for i in range (context .individual_context_count ):
2441 c = context .get_individual_context (i )
2542 if c is not None :
26- out [c .kind ] = self ._format_context_single (c , False )
43+ out [c .kind ] = self ._format_context_single (c , False , redact_anonymous )
2744 return out
2845 else :
29- return self ._format_context_single (context , True )
46+ return self ._format_context_single (context , True , redact_anonymous )
3047
31- def _format_context_single (self , context : Context , include_kind : bool ) -> Dict :
48+ def _format_context_single (self , context : Context , include_kind : bool , redact_anonymous : bool ) -> Dict :
3249 out = {'key' : context .key } # type: Dict[str, Any]
3350 if include_kind :
3451 out ['kind' ] = context .kind
52+
3553 if context .anonymous :
3654 out ['anonymous' ] = True
3755
@@ -44,11 +62,11 @@ def _format_context_single(self, context: Context, include_kind: bool) -> Dict:
4462 if ar .valid :
4563 all_private .append (ar )
4664
47- if context .name is not None and not self ._check_whole_attr_private ('name' , all_private , redacted ):
65+ if context .name is not None and not self ._check_whole_attr_private ('name' , all_private , redacted , context . anonymous and redact_anonymous ):
4866 out ['name' ] = context .name
4967
5068 for attr in context .custom_attributes :
51- if not self ._check_whole_attr_private (attr , all_private , redacted ):
69+ if not self ._check_whole_attr_private (attr , all_private , redacted , context . anonymous and redact_anonymous ):
5270 value = context .get (attr )
5371 out [attr ] = self ._redact_json_value (None , attr , value , all_private , redacted )
5472
@@ -57,8 +75,8 @@ def _format_context_single(self, context: Context, include_kind: bool) -> Dict:
5775
5876 return out
5977
60- def _check_whole_attr_private (self , attr : str , all_private : List [AttributeRef ], redacted : List [str ]) -> bool :
61- if self ._all_attributes_private :
78+ def _check_whole_attr_private (self , attr : str , all_private : List [AttributeRef ], redacted : List [str ], redact_all : bool ) -> bool :
79+ if self ._all_attributes_private or redact_all :
6280 redacted .append (attr )
6381 return True
6482 for p in all_private :
0 commit comments