File size: 2,592 Bytes
f091ba2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | # -*- coding: utf-8 -*-
import sys
from functools import wraps
from typing import Callable, Optional, Union
from tracy_client.TracyClientBindings import (
is_enabled,
_ScopedZone,
ColorType,
frame_mark_start,
frame_mark_end,
)
Color = Union[int, ColorType]
class ScopedZone(_ScopedZone):
def __init__(
self,
name: Optional[str] = None,
color: Color = 0,
depth: Optional[int] = None,
active: bool = True,
) -> None:
frame = sys._getframe(1)
_ScopedZone.__init__(
self,
name,
int(color),
depth,
active,
frame.f_code.co_name,
frame.f_code.co_filename,
frame.f_lineno,
)
def color(self, color: Color) -> None:
self._color(int(color))
def __enter__(self):
self.enter()
return self
def __exit__(self, *args):
self.exit()
def ScopedZoneDecorator(
name: Optional[str] = None,
color: Color = 0,
depth: Optional[int] = None,
active: bool = True,
class_name: Optional[str] = None,
):
def decorator(function: Callable):
if not is_enabled():
return function
source = function.__name__
if class_name is not None:
source = f"{class_name}:{source}"
zone = _ScopedZone(
name,
int(color),
depth,
active,
source,
function.__code__.co_filename,
function.__code__.co_firstlineno,
)
@wraps(function)
def wrapped(*args, **kwargs):
zone.enter()
value = function(*args, **kwargs)
zone.exit()
return value
return wrapped
return decorator
class ScopedFrame:
def __init__(self, name: str) -> None:
self.__name = name
self.__id: Optional[int] = None
def __enter__(self):
self.__id = frame_mark_start(self.__name)
return self
def __exit__(self, *args):
if self.__id is None:
return
frame_mark_end(self.__id)
def ScopedFrameDecorator(name: str):
def decorator(function: Callable):
if not is_enabled():
return function
@wraps(function)
def wrapped(*args, **kwargs):
frame_id = frame_mark_start(name)
value = function(*args, **kwargs)
if frame_id is not None:
frame_mark_end(frame_id)
return value
return wrapped
return decorator
|