tos library / About

About tos

tos.py is a tiny support library shipped alongside TrashOS Reborn. Its job is to standardise the way third-party applications (the scripts inside tosapps/) interact with their host:

  • Where do I keep persistent data?
  • How do I clear the screen on Windows and Linux/macOS?
  • Am I currently in a root shell?
  • Which version of the framework am I targeting?

Design goals

  • Zero dependencies. Only the Python standard library is used.
  • Side-effect on import. The appdata/ root is created automatically.
  • Tiny surface area. Two objects, one function, one constant.

Source

The full source of the library, as of tos.version == "1.0.0":

import os
import platform

base_dir = os.path.dirname(os.path.abspath(__file__))
appdata_root = os.path.join(base_dir, "appdata")

if not os.path.exists(appdata_root):
    os.makedirs(appdata_root)

def is_root():
    """Checks if root mode is active."""
    return os.getenv("TOS_ROOT_ACTIVE") == "TRUE"

class AppData:
    def path(self, app_name):
        specific_path = os.path.join(appdata_root, app_name)
        if not os.path.exists(specific_path):
            os.makedirs(specific_path)
        return specific_path

class Terminal:
    def cls(self):
        if platform.system() == "Windows":
            os.system('cls')
        else:
            os.system('clear')

appdata = AppData()
term = Terminal()
version = "1.0.0"

Warning: Because appdata_root is computed from the location of tos.py itself, moving the file will move every app's data folder with it.