Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

im trying to pass info from my nsl.py to my main.py via a pipe here is the log contents…

  1. User

    im trying to pass info from my nsl.py to my main.py via a pipe here is the log contents [2024-02-11 23:34:04,295][INFO]: Read from pipe: {"appid": "-456079619", "appname": "Epic Games", "exe": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32/EpicGamesLauncher.exe" -opengl", "StartDir": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32"", "icon": "/home/deck/.steam/root/userdata/106245328/config/grid/-456079619-icon.ico", "LaunchOptions": "STEAM_COMPAT_DATA_PATH="/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/" %command%", "GameID": "default_game_id"} [2024-02-11 23:34:04,296][INFO]: Read from pipe: {"appid": "-508946163", "appname": "Battle.net", "exe": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Battle.net/Battle.net Launcher.exe"", "StartDir": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Battle.net"", "icon": "/home/deck/.steam/root/userdata/106245328/config/grid/-508946163-icon.ico", "LaunchOptions": "STEAM_COMPAT_DATA_PATH="/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/" %command%", "GameID": "default_game_id"} [2024-02-11 23:34:04,296][INFO]: Read from pipe: {"appid": "-1240964672", "appname": "20 Minutes Till Dawn", "exe": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32/EpicGamesLauncher.exe"", "StartDir": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32/"", "icon": "/home/deck/.steam/root/userdata/106245328/config/grid/-1240964672-icon.ico", "LaunchOptions": "STEAM_COMPAT_DATA_PATH="/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers" %command% -'com.epicgames.launcher://apps/4656facc740742a39e265b026e13d075?action=launch&silent=true'", "GameID": "default_game_id"} next i will show my main.py code and then the .tsx fie code

  2. Assistant

    I'm sorry, but as an AI language model, I am unable to view or interact with specific code files or log contents. However, I can provide you with some general guidance on passing information between Python files through a pipe.

    In order to pass information between the nsl.py and main.py files using a pipe, you can follow these steps:

    1. In nsl.py, import the subprocess module and use it to create a pipe:
    import subprocess
    
    # Create a pipe and start a subprocess
    pipe = subprocess.Popen(['python', 'main.py'], stdin=subprocess.PIPE)
    
    1. In nsl.py, use the pipe object to write the information to the pipe:
    import json
    
    # Example data to pass to main.py
    data = {"appid": "-456079619", "appname": "Epic Games"}
    
    # Convert the data to JSON and write it to the pipe
    pipe.stdin.write(json.dumps(data).encode())
    pipe.stdin.close()
    
    1. In main.py, read the information from the pipe:
    import sys
    import json
    
    # Read the data from the pipe
    data = sys.stdin.read()
    
    # Load the JSON data
    parsed_data = json.loads(data)
    
    # Process the received data
    # Example: Print the appname
    print(parsed_data['appname'])
    

    These steps illustrate a simple way to pass information from nsl.py to main.py using a pipe. Note that the code examples assume that nsl.py and main.py are in the same directory. Adjust the subprocess command in step 1 as needed to match your file locations.

    Please ensure that you have imported the necessary modules and handle potential exceptions when using pipes.

  3. User

    here is my current code that is grabbing that info? in my main.py async def websocket_handler(request): ws = web.WebSocketResponse() await ws.prepare(request)

    PIPE_PATH = "/tmp/NSLGameScanner_pipe"
    if not os.path.exists(PIPE_PATH):
        decky_plugin.logger.error(f"Pipe file {PIPE_PATH} not found")
        return
    try:
        with open(PIPE_PATH, 'r') as pipe:
            while True:
                line = pipe.readline().strip()
                if not line:
                    break
                print(f"Read from pipe: {line}")  # print to console
                decky_plugin.logger.info(f"Read from pipe: {line}")  # or log the line
                await ws.send_str(line)  # send to WebSocket client
    except FileNotFoundError:
        decky_plugin.logger.error(f"Pipe file {PIPE_PATH} not found")
    except Exception as e:
        decky_plugin.logger.error(f"Error reading from pipe: {e}")
    finally:
        await ws.close()
    
    if ws.closed:
        decky_plugin.logger.error("WebSocket connection is closed")
        return
    
    return ws
    

    app = web.Application() app.router.add_route('GET', '/ws', websocket_handler)

    web.run_app(app, port=8765)

  4. Assistant

    Based on the provided code, it seems like you are using the aiohttp library to handle a WebSocket connection and read data from a named pipe.

    To pass the information from nsl.py to main.py using the named pipe, you need to make sure that you are writing to the named pipe from nsl.py and reading from it in main.py.

    In nsl.py, after opening the named pipe, you can write the information to the pipe as follows:

    import os
    
    PIPE_PATH = "/tmp/NSLGameScanner_pipe"
    
    # Example data to pass to main.py
    data = {"appid": "-456079619", "appname": "Epic Games"}
    
    if not os.path.exists(PIPE_PATH):
        decky_plugin.logger.error(f"Pipe file {PIPE_PATH} not found")
        return
    try:
        with open(PIPE_PATH, 'w') as pipe:
            pipe.write(json.dumps(data))
            pipe.write('\n')
    except FileNotFoundError:
        decky_plugin.logger.error(f"Pipe file {PIPE_PATH} not found")
    except Exception as e:
        decky_plugin.logger.error(f"Error writing to pipe: {e}")
    

    Then, in main.py, you should be able to read the data from the named pipe inside the websocket_handler function:

    import aiohttp
    import os
    import json
    
    async def websocket_handler(request):
        ws = aiohttp.web.WebSocketResponse()
        await ws.prepare(request)
    
        PIPE_PATH = "/tmp/NSLGameScanner_pipe"
        if not os.path.exists(PIPE_PATH):
            decky_plugin.logger.error(f"Pipe file {PIPE_PATH} not found")
            return
        try:
            with open(PIPE_PATH, 'r') as pipe:
                while True:
                    line = pipe.readline().strip()
                    if not line:
                        break
                    print(f"Read from pipe: {line}")  # print to console
                    decky_plugin.logger.info(f"Read from pipe: {line}")  # or log the line
                    await ws.send_str(line)  # send to WebSocket client
        except FileNotFoundError:
            decky_plugin.logger.error(f"Pipe file {PIPE_PATH} not found")
        except Exception as e:
            decky_plugin.logger.error(f"Error reading from pipe: {e}")
        finally:
            await ws.close()
    
        if ws.closed:
            decky_plugin.logger.error("WebSocket connection is closed")
            return
    
        return ws
    
    app = aiohttp.web.Application()
    app.router.add_route('GET', '/ws', websocket_handler)
    
    aiohttp.web.run_app(app, port=8765)
    

    Make sure to import the necessary modules (aiohttp, os, json) and handle exceptions appropriately.

    Please note that this code assumes you are running both nsl.py and main.py on the same machine and have appropriate permissions to access the named pipe (/tmp/NSLGameScanner_pipe). Adjust the file path as needed to match the actual location of the named pipe.

  5. User

    no im not trying to pass it to the other .py im trying to pass the main.py info from the pipe correctly into this .tsx file, what ends up happening is shortcuts are created and the connection is good, but the shortcuts dont have any of that info from the log? import { ButtonItem, definePlugin, findSP, ModalRoot, ModalRootProps, PanelSection, PanelSectionRow, ServerAPI, showModal, staticClasses, TextField, ToggleField, } from "decky-frontend-lib"; import { useContext, useEffect, useReducer, useState, VFC, createContext } from "react"; import { FaRocket } from "react-icons/fa";

    interface App { appid: string; appname: string; exe: string; StartDir: string; icon: string; LaunchOptions: string; GameID: string; }

    function getLaunchOptions(app: App) { return app.LaunchOptions; }

    function getTarget(app: App) { return app.exe; }

    const createShortcut = (name: string, launchOptions: string = "", target: string = "") => { return SteamClient.Apps.AddShortcut(name, "/usr/bin/ifyouseethisyoufoundabug", target, launchOptions); }

    function setupWebSocket() { const socket = new WebSocket("ws://localhost:8765/ws");

    socket.onopen = (e) => { console.log("[open] Connection established"); };

    socket.onmessage = (event) => { console.log([message] Data received from server: ${event.data}); // Parse the JSON data const game = JSON.parse(event.data); // Use the data to create a Steam shortcut createShortcut(game.appname, game.LaunchOptions, game.exe); };

    socket.onclose = (event) => { console.log([close] Connection closed); };

    socket.onerror = (error) => { console.log([error] ${error.message}); };

    // Clean up the WebSocket connection when the function is done return () => { socket.close(); }; }

    // Call the function to set up the WebSocket connection setupWebSocket();

  6. Assistant

    Based on the provided code, it seems like you are trying to set up a WebSocket connection in your main.py file and receive data from the pipe, then pass the received data to a TypeScript (.tsx) file.

    To pass the received data from the pipe to your .tsx file, you can modify your WebSocket message handler to invoke a callback function or emit a custom event that can be handled within your TypeScript code.

    In your main.py file, modify your WebSocket message handler as follows:

    socket.onmessage = (event) => {
      console.log(`[message] Data received from server: ${event.data}`);
      // Parse the JSON data
      const game = JSON.parse(event.data);
      // Emit a custom event with the received data
      const dataEvent = new CustomEvent("gameData", { detail: game });
      window.dispatchEvent(dataEvent);
    };
    

    Then, in your TypeScript code (.tsx file), you can listen for that custom event and handle the received data:

    import { useEffect } from "react";
    
    function handleGameData(event: CustomEvent) {
      const game = event.detail;
      // Use the received data to create a Steam shortcut
      createShortcut(game.appname, game.LaunchOptions, game.exe);
    }
    
    function MyComponent() {
      useEffect(() => {
        // Add an event listener for the 'gameData' event
        window.addEventListener("gameData", handleGameData);
    
        // Clean up the event listener when the component unmounts
        return () => {
          window.removeEventListener("gameData", handleGameData);
        };
      }, []);
    
      // ... rest of your component code
    }
    

    In the above code, the handleGameData function is called whenever the "gameData" event is emitted from the main.py WebSocket message handler. This function receives the data from the event and can be used to create a Steam shortcut or perform any other necessary actions.

    Make sure to remove the existing createShortcut function from your .tsx file, as it will be defined in your main.py file.

    Note: Make sure that the .tsx file is being processed by a TypeScript compiler or bundler so that the code can be properly transpiled to JavaScript and executed in the browser.

    This way, the received data from the pipe in main.py can be passed to your .tsx file and used accordingly.

  7. User

    is this why the shorcuts were being created just like this const createShortcut = (name: string, launchOptions: string = "", target: string = "") => { return SteamClient.Apps.AddShortcut(name, "/usr/bin/ifyouseethisyoufoundabug", target, launchOptions); instead of the data that was supposed to be sending through main.py was this? [2024-02-11 23:34:04,295][INFO]: Read from pipe: {"appid": "-456079619", "appname": "Epic Games", "exe": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32/EpicGamesLauncher.exe" -opengl", "StartDir": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32"", "icon": "/home/deck/.steam/root/userdata/106245328/config/grid/-456079619-icon.ico", "LaunchOptions": "STEAM_COMPAT_DATA_PATH="/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/" %command%", "GameID": "default_game_id"} [2024-02-11 23:34:04,296][INFO]: Read from pipe: {"appid": "-508946163", "appname": "Battle.net", "exe": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Battle.net/Battle.net Launcher.exe"", "StartDir": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Battle.net"", "icon": "/home/deck/.steam/root/userdata/106245328/config/grid/-508946163-icon.ico", "LaunchOptions": "STEAM_COMPAT_DATA_PATH="/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/" %command%", "GameID": "default_game_id"} [2024-02-11 23:34:04,296][INFO]: Read from pipe: {"appid": "-1240964672", "appname": "20 Minutes Till Dawn", "exe": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32/EpicGamesLauncher.exe"", "StartDir": ""/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers/pfx/drive_c/Program Files (x86)/Epic Games/Launcher/Portal/Binaries/Win32/"", "icon": "/home/deck/.steam/root/userdata/106245328/config/grid/-1240964672-icon.ico", "LaunchOptions": "STEAM_COMPAT_DATA_PATH="/home/deck/.local/share/Steam/steamapps/compatdata/NonSteamLaunchers" %command% -'com.epicgames.launcher://apps/4656facc740742a39e265b026e13d075?action=launch&silent=true'", "GameID": "default_game_id"}

  8. Assistant

    Yes, that could be the reason why the shortcuts were being created with default values instead of the actual data from the pipe. The createShortcut function you showed in your previous code snippet is using hardcoded values for name, launchOptions, and target. Instead, you should use the properties from the data object received from the pipe.

    To fix this, modify the createShortcut function in your .tsx file as follows:

    const createShortcut = (name: string, launchOptions: string = "", target: string = "") => {
      return SteamClient.Apps.AddShortcut(name, target, launchOptions);
    }
    

    This will make use of the provided name, launchOptions, and target arguments when creating the Steam shortcut. Then, make sure that you pass the correct values when calling the createShortcut function:

    socket.onmessage = (event) => {
      console.log(`[message] Data received from server: ${event.data}`);
      // Parse the JSON data
      const game = JSON.parse(event.data);
      // Use the data to create a Steam shortcut
      createShortcut(game.appname, game.LaunchOptions, game.exe);
    };
    

    By using the properties game.appname, game.LaunchOptions, and game.exe, the correct values from the received data will be passed to the createShortcut function when creating the Steam shortcut.

    Make sure to update the createShortcut function accordingly in your TypeScript code and ensure that the received data from the pipe matches the expected properties (appname, LaunchOptions, exe) when creating the Steam shortcuts.