11"""Wakatime
22===========
33
4- `create-plugin <https://wakatime.com/help/creating-plugin>`_
4+ Refer `create-plugin <https://wakatime.com/help/creating-plugin>`_.
55"""
6+ import logging
67import os
8+ from shutil import which
79from subprocess import run # nosec: B404
810from threading import Thread
9- from typing import Any
11+ from typing import Any , Callable
1012
1113from ._version import __version__ , __version_tuple__ # type: ignore
1214
15+ logger = logging .getLogger (__name__ )
1316
14- def send_wakatime_heartbeat (project : str = "Terminal" ) -> None :
17+
18+ def send_wakatime_heartbeat (
19+ project : str = "" ,
20+ plugin : str = "repl-python-wakatime" ,
21+ filenames : list [str ] = [".git" ],
22+ detect_func : Callable [[str ], bool ] = os .path .isdir ,
23+ ) -> None :
1524 """Send wakatime heartbeat.
1625
26+ If ``project == ""``, detect automatically.
27+
28+ ``plugin`` must be the format of ``repl-REPL_NAME-wakatime`` to let
29+ wakatime detect correctly.
30+
1731 :param project:
1832 :type project: str
33+ :param plugin:
34+ :type plugin: str
35+ :param filenames:
36+ :type filenames: list[str]
37+ :param detect_func:
38+ :type detect_func: Callable[[str], bool]
1939 :rtype: None
2040 """
41+ if project == "" :
42+ from .utils import get_project
43+
44+ project = get_project (filenames , detect_func )
2145 run ( # nosec: B603 B607
2246 [
2347 "wakatime-cli" ,
2448 "--write" ,
25- "--plugin=python-wakatime " ,
49+ f "--plugin={ plugin } " ,
2650 "--entity-type=app" ,
2751 "--entity=python" ,
2852 "--alternate-language=python" ,
@@ -39,6 +63,9 @@ def wakatime_hook(args: tuple = (), kwargs: dict[str, Any] = {}) -> None:
3963 :type args: tuple
4064 :rtype: None
4165 """
66+ if which ("wakatime-cli" ) is None :
67+ logger .error ("Please install wakatime-cli firstly!" )
68+ return
4269 task = Thread (target = send_wakatime_heartbeat , args = args , kwargs = kwargs )
4370 task .daemon = True
4471 task .start ()
0 commit comments