Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions aw_watcher_afk/afk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import os
import platform
import subprocess

from datetime import datetime, timedelta, timezone
from time import sleep

Expand All @@ -27,6 +29,15 @@
logger = logging.getLogger(__name__)
td1ms = timedelta(milliseconds=1)

def get_logged_in_user():
try:
user = subprocess.check_output("who | awk '{print $1}' | head -n 1", shell=True).decode().strip()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using Python’s built-in getpass.getuser() for retrieving the current user, which is more cross‑platform and avoids using shell=True with subprocess.

Suggested change
user = subprocess.check_output("who | awk '{print $1}' | head -n 1", shell=True).decode().strip()
user = getpass.getuser()

if not user:
raise Exception("No logged in user found")
return user
except Exception as e:
logger.error(f"Failed to get logged in user: {e}")
return "unknown"

class Settings:
def __init__(self, config_section, timeout=None, poll_time=None):
Expand All @@ -44,12 +55,12 @@ def __init__(self, args, testing=False):
self.settings = Settings(
load_config(testing), timeout=args.timeout, poll_time=args.poll_time
)

username = get_logged_in_user()
self.client = ActivityWatchClient(
"aw-watcher-afk", host=args.host, port=args.port, testing=testing
)
self.bucketname = "{}_{}".format(
self.client.client_name, self.client.client_hostname
self.bucketname = "{}-afk_{}".format(
username, self.client.client_hostname
)

def ping(self, afk: bool, timestamp: datetime, duration: float = 0):
Expand Down