Debian Codegraph @

src/fenrirscreenreader/utils/screen_utils.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Fenrir TTY screen reader
5 # By Chrys, Storm Dragon, and contributors.
6
7 import getpass
8 import os
9 import string
10 import time
11 from select import select
12
13 from fenrirscreenreader.core import debug
14
15
16 def remove_nonprintable(text):
17 # Get the difference of all ASCII characters from the set of printable
18 # characters
19 nonprintable = set([chr(i) for i in range(128)]).difference(
20 string.printable
21 )
22 # Use translate to remove all non-printable characters
23 return text.translate({ord(character): None for character in nonprintable})
24
25
26 def insert_newlines(string, every=64):
27 return "\n".join(
28 string[i : i + every] for i in range(0, len(string), every)
29 )
30
31
32 def split_every(toSplit, every=64):
33 return list(toSplit[i : i + every] for i in range(0, len(toSplit), every))
34
35
36 def create_screen_event_data(content):
37 event_data = {
38 "bytes": content,
39 "lines": content["lines"],
40 "columns": content["columns"],
41 "textCursor": {
42 "x": int(content["cursor"][0]),
43 "y": int(content["cursor"][1]),
44 },
45 "screen": content["screen"],
46 "text": content["text"],
47 "attributes": content["attributes"],
48 "screenUpdateTime": time.time(),
49 }
50 return event_data.copy()
51
52
53 def has_more(fd, timetout=0.05):
54 r, _, _ = select([fd], [], [], timetout)
55 return fd in r
56
57
58 def has_more_what(fd_list, timetout=0.05):
59 if not isinstance(fd_list, list):
60 return []
61 elif fd_list == []:
62 return []
63 r, _, _ = select(fd_list, [], [], timetout)
64 return r
65
66
67 def is_valid_shell(shell=""):
68 if not isinstance(shell, str):
69 return False
70 if shell == "":
71 return False
72 try:
73 if not os.path.isfile(shell):
74 return False
75 if not os.access(shell, os.X_OK):
76 return False
77 except Exception as e:
78 # Utility function, no env access - use fallback logging
79 print(
80 f"screen_utils is_valid_shell: Error checking shell {shell}: {e}"
81 )
82 return False
83 return True
84
85
86 def get_shell():
87 try:
88 shell = os.environ["FENRIRSHELL"]
89 if is_valid_shell(shell):
90 return shell
91 except Exception as e:
92 # Utility function, no env access - continue silently
93 pass
94 try:
95 shell = os.environ["SHELL"]
96 if is_valid_shell(shell):
97 return shell
98 except Exception as e:
99 # Utility function, no env access - continue silently
100 pass
101 try:
102 if os.access("/etc/passwd", os.R_OK):
103 with open("/etc/passwd") as f:
104 users = f.readlines()
105 for user in users:
106 (username, encrypwd, uid, gid, gecos, homedir, shell) = (
107 user.split(":")
108 )
109 shell = shell.replace("\n", "")
110 if username == getpass.getuser():
111 if is_valid_shell(shell):
112 return shell
113 except Exception as e:
114 # Utility function, no env access - continue silently
115 pass
116 if is_valid_shell("/bin/bash"):
117 return "/bin/bash"
118 return "/bin/sh"