Skip to content

Commit 153de22

Browse files
Merge pull request #1010 from luxonis/feat/sentryio
Add sentry.io reporting, add CLI command depthai-sdk
2 parents 8a155d0 + 743c7e2 commit 153de22

File tree

7 files changed

+148
-12
lines changed

7 files changed

+148
-12
lines changed

depthai_sdk/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ pytube>=12.1.0
77
depthai==2.21.2
88
PyTurboJPEG==1.6.4
99
marshmallow==3.17.0
10-
xmltodict
10+
xmltodict
11+
sentry-sdk==1.21.0

depthai_sdk/setup.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
author='Luxonis',
1919
author_email='support@luxonis.com',
2020
license='MIT',
21-
packages=['depthai_sdk'],
22-
package_dir={"": "src"}, # https://stackoverflow.com/a/67238346/5494277
21+
packages=['depthai_sdk', 'depthai_sdk_console_scripts'],
22+
package_dir={'': 'src'}, # https://stackoverflow.com/a/67238346/5494277
2323
install_requires=install_requires,
2424
include_package_data=True,
2525
extras_require={
@@ -30,14 +30,7 @@
3030
"replay": ['mcap>=0.0.10',
3131
'mcap-ros1-support==0.0.8',
3232
'rosbags==0.9.11'],
33-
"record": ['av'],
34-
"minimal": ['numpy',
35-
'blobconverter',
36-
'depthai',
37-
'PyTurboJPEG',
38-
'marshmallow',
39-
'xmltodict',
40-
]
33+
"record": ['av']
4134
},
4235
project_urls={
4336
"Bug Tracker": "https://github.com/luxonis/depthai/issues",
@@ -53,4 +46,9 @@
5346
"Programming Language :: Python :: 3.9",
5447
"Programming Language :: Python :: 3.10",
5548
],
49+
entry_points={
50+
'console_scripts': [
51+
'depthai_sdk = depthai_sdk_console_scripts.depthai_sdk:main',
52+
]
53+
},
5654
)

depthai_sdk/src/depthai_sdk/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,36 @@
77
from depthai_sdk.record import *
88
from depthai_sdk.replay import *
99
from depthai_sdk.utils import *
10+
from depthai_sdk.utils import _create_config, get_config_field
1011
from depthai_sdk.visualize import *
12+
13+
__version__ = '1.9.6'
14+
15+
16+
def __import_sentry(sentry_dsn: str) -> None:
17+
try:
18+
import sentry_sdk
19+
20+
sentry_sdk.init(
21+
dsn=sentry_dsn,
22+
traces_sample_rate=1.0,
23+
release='depthai_sdk@1.9.5',
24+
with_locals=False,
25+
)
26+
except:
27+
pass
28+
29+
30+
config_exists = False
31+
# Check if sentry is enabled
32+
try:
33+
sentry_status = get_config_field('sentry')
34+
config_exists = True
35+
except FileNotFoundError:
36+
sentry_status = False
37+
38+
if config_exists and sentry_status:
39+
sentry_dsn = get_config_field('sentry_dsn')
40+
__import_sentry(sentry_dsn)
41+
elif not config_exists:
42+
_create_config()

depthai_sdk/src/depthai_sdk/utils.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import importlib
2+
import json
3+
import logging
24
import sys
35
import urllib.request
46
from pathlib import Path
5-
from typing import Dict, List, Tuple, Optional, Union
7+
from typing import Dict, List, Tuple, Optional, Union, Any
68

79
try:
810
import cv2
@@ -391,3 +393,77 @@ def createBlankFrame(width, height, rgb_color=(0, 0, 0)):
391393
image[:] = color
392394

393395
return image
396+
397+
398+
def _create_cache_folder() -> bool:
399+
"""
400+
Create config file in user's home directory.
401+
402+
Returns:
403+
True if folder was created, False otherwise.
404+
"""
405+
try:
406+
Path.home().joinpath(".depthai_sdk").mkdir(parents=True, exist_ok=True)
407+
except PermissionError:
408+
logging.debug('Failed to create cache folder.')
409+
return False
410+
411+
return True
412+
413+
414+
def _create_config() -> None:
415+
"""
416+
Create config file in user's home directory.
417+
418+
Returns:
419+
None.
420+
"""
421+
if not _create_cache_folder():
422+
logging.debug('Failed to create config file.')
423+
return
424+
425+
config_file = Path.home().joinpath('.depthai_sdk', 'config.json')
426+
default_config = {
427+
'sentry': True,
428+
'sentry_dsn': 'https://981545d5effd480d883f3ff0b1306e49@o1095304.ingest.sentry.io/4504685274791936'
429+
}
430+
if not config_file.exists():
431+
config_file.write_text(json.dumps(default_config))
432+
433+
434+
def set_sentry_status(status: bool = True) -> None:
435+
"""
436+
Set sentry status in config file.
437+
438+
Args:
439+
status (bool): True if sentry should be enabled, False otherwise. Default is True.
440+
441+
Returns:
442+
None.
443+
"""
444+
# check if config exists
445+
config_file = Path.home().joinpath('.depthai_sdk', 'config.json')
446+
if not config_file.exists():
447+
_create_config()
448+
449+
# read config
450+
config = json.loads(config_file.read_text())
451+
config['sentry'] = status
452+
config_file.write_text(json.dumps(config))
453+
454+
455+
def get_config_field(key: str) -> Any:
456+
"""
457+
Get sentry status from config file.
458+
459+
Returns:
460+
bool: True if sentry is enabled, False otherwise.
461+
"""
462+
# check if config exists
463+
config_file = Path.home().joinpath('.depthai_sdk', 'config.json')
464+
if not config_file.exists():
465+
raise FileNotFoundError('Config file not found.')
466+
467+
# read config
468+
config = json.loads(config_file.read_text())
469+
return config[key]

depthai_sdk/src/depthai_sdk_console_scripts/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .__main__ import main
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import argparse
2+
3+
import depthai_sdk
4+
5+
6+
def main():
7+
parser = argparse.ArgumentParser(description='DepthAI SDK command-line interface.')
8+
subparsers = parser.add_subparsers(dest='command')
9+
10+
sentry_parser = subparsers.add_parser('sentry', help='Enable or disable Sentry reporting')
11+
sentry_parser.add_argument('action', choices=['enable', 'disable', 'status'], help='Action to perform')
12+
13+
args = parser.parse_args()
14+
15+
if args.command == 'sentry':
16+
if args.action == 'enable':
17+
depthai_sdk.set_sentry_status(True)
18+
print('Sentry reporting was enabled.')
19+
elif args.action == 'disable':
20+
depthai_sdk.set_sentry_status(False)
21+
print('Sentry reporting was disabled.')
22+
elif args.action == 'status':
23+
status = depthai_sdk.get_config_field("sentry")
24+
print(f'Sentry is {"enabled" if status else "disabled"}.')
25+
26+
27+
if __name__ == '__main__':
28+
main()

0 commit comments

Comments
 (0)