Skip to main content

A Bridge for PacSpedd Base with IDE support

Project description

PacSpedd Base

A Python Module, but written in Rust.

Advantages of PacSpedd Base

PacSpedd Base leverages the power of Rust to provide several key advantages over traditional Python modules:

  1. Fast Import Times:
    • Since the Rust code is compiled into a binary format, importing the module in Python is significantly faster compared to pure Python modules.

  2. Unified Codebase:
    • Simplified Maintenance: By handling Windows and Linux specific code within a single function in Rust, there is no need for separate versions for each operating system. This makes the module easier to maintain and extend.

    • Consistent Behavior: A single code path for both operating systems ensures consistent results and reduces potential errors.

  3. Efficient System Resource Utilization:
    • Direct System Interaction: Rust allows for more direct and efficient interaction with the system, making the execution of system commands more reliable and faster, especially for complex commands.

  4. Enhanced Security and Reliability:
    • Memory Safety: Rust’s guarantees around memory safety reduce the risk of memory-related errors, leading to more robust and stable software.

    • Error Handling: Rust’s strong type system and error handling mechanisms help catch runtime errors early, ensuring more reliable execution.

Using these advantages, PacSpedd Base provides a powerful and efficient solution for executing system commands and other operations within your Python projects.

The Bridge

The Bridge needs PacSpedd Base, cause all Classes and Functions called PacSpedd Base

Then Why a Bridge

The Bridge brining Literal Strings, Doc Strings and Many more IDE Features, is not a Must, but that will Help you Really

Installation

From PyPI (Stable)

To install the stable version from PyPI:

pip install --no-cache-dir pacspeddbase

From GitHub (Newest)

To install the newest version directly from GitHub:

Option 1: Using archive

pip install --no-cache-dir https://github.com/PacSpedd/pacspeddbase/archive/master.zip

Option 2: Cloning repository

git clone https://github.com/PacSpedd/pacspeddbase
cd pacspeddbase
pip install .

Requirements

You need to have Rust and Maturin installed. Follow the instructions below to set them up.

1. Install Rust

Linux

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Windows (winget)

winget install rust

Windows (scoop)

scoop install rust

Termux (Few Support)

Using apt

apt-get update -qq
apt-get install rust ldd binutils build-essential -y

Using pacman

pacman -Syy
pacman -S --noconfirm rust ldd binutils build-essential

Using pkg (default)

pkg up
pkg in rust ldd binutils build-essential

Proot-Distro (Best Way)

Install Proot-Distro

pkg up
pkg in proot-distro
pd i ubuntu

Setup Proot-Distro

apt-get update
apt-get upgrade -y
apt-get install sudo adduser neovim build-essential -y
useradd -m -s /bin/bash userx
echo "userx ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/userx
chmod 0440 /etc/sudoers.d/userx
su - userx
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

2. Install Maturin

Attention to the Termux users, Maturin cannot always be successfully compiled on Termux, I will package successful Maturin builds as a .zip with a setup.sh which Maturin stores correctly and also package it as a .deb

pip install maturin

3. Using Pacspedd Base

Import the Module

import pacspeddbase as psb

Hello World with PacSpedd Base

psb.print("Hello World")

How is it Working

The Thing is, the Module is full Rust Based, print called following Function

#[pyfunction]
fn print(text: &str) -> PyResult<()> {
    println!("{}", text);
}

I mean with that, The Print Function in PacSpedd Base Called the Rust Print Line Function.

Get an Enviroment Variable

HOME = psb.get_env('HOME')

How is it Working

#[pyfunction]
fn get_env(var:&str) -> PyResult<Option<String>> {
    let value = env::var(var).ok()
    OK(value)
}

System Class in Pacspedd Base

PacSpedd Base have a System Class

Setup

system = psb.System()

Execute a Command

command = "apt update"
system.cmd(command)

Make a Once Directory

import os
HOME = psb.get_env('HOME')
path = os.path.join(HOME, 'example')
psb.mkdir(path)

Make More then One Directory

import os
HOME = psb.get_env('HOME')
path = os.path.join(HOME, 'example', '1', '2', '3')
psb.makedirs(path)

Change Curent Directory

psb.cd(path)

How is it Working

#[pymethods]
impl System {
    #[new]
    fn new() -> Self {
        System
    }

    /// Execute a command
    ///
    /// Args:
    ///     command (str): The command to execute
    fn cmd(&self, command: &str) -> PyResult<()> {
        let output = if cfg!(target_os = "windows") {
            Command::new("cmd")
                .arg("/C")
                .arg(command)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to execute command: {}", e)))?
        } else {
            Command::new("sh")
                .arg("-c")
                .arg(command)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to execute command: {}", e)))?
        };

        if !output.status.success() {
            return Err(pyo3::exceptions::PyRuntimeError::new_err(format!("Command failed: {}", output.status)));
        }

        Ok(())
    }

    /// Create a directory
    ///
    /// Args:
    ///     path (str): The path of the directory to create
    fn mkdir(&self, path: &str) -> PyResult<()> {
        fs::create_dir(path)
            .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to create directory: {}", e)))?;
        Ok(())
    }

    /// Change the current working directory
    ///
    /// Args:
    ///     path (str): The path of the directory to change to
    fn cd(&self, path: &str) -> PyResult<()> {
        env::set_current_dir(path)
            .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to change directory: {}", e)))?;
        Ok(())
    }

    /// List files in the current directory
    ///
    /// Returns:
    ///     List[str]: A list of file names in the current directory
    fn list_files(&self) -> PyResult<Vec<String>> {
        let paths = fs::read_dir(".")
            .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to read directory: {}", e)))?;

        let mut files = Vec::new();
        for path in paths {
            let path = path.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to read path: {}", e)))?;
            files.push(path.path().display().to_string());
        }
        Ok(files)
    }

    /// Clear the Terminal
    ///
    /// Args:
    ///     None:
    fn clear(&self) -> PyResult<()> {
        let _output = if cfg!(target_os = "windows") {
            Command::new("cls")
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Clear Terminal: {}", e)))?
        } else {
            Command::new("clear")
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Clear Terminal: {}", e)))?
        };
        Ok(())
    }

    /// Wget Interaction
    ///
    /// Args:
    ///     url: The Download url
    fn wget(&self, url: &str) -> PyResult<()> {
        let _output = if cfg!(target_os = "windows") {
            Command::new("wget")
                .arg(url)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Download File: {}", e)))?
        } else {
            Command::new("wget")
                .arg(url)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Download File: {}", e)))?
        };
        Ok(())
    }

    /// Make Much Dirs
    ///
    /// Args:
    ///     path: The path
    fn makedirs(&self, path: &str) -> PyResult<()> {
        let _output = if cfg!(target_os = "windows") {
            Command::new("mkdir")
                .arg(path)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Create Directorys: {}", e)))?
        } else {
            Command::new("mkdir")
                .arg("-p")
                .arg(path)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Create Directorys: {}", e)))?
        };
        Ok(())
    }

    /// Copy files
    ///
    /// Args:
    ///     path 1 (str): The Source Path
    ///     path 2 (str): The Dist Path
    fn copy(&self, srcpath: &str, despath: &str) -> PyResult<()> {
        let _output = if cfg!(target_os = "windows") {
            Command::new("cp")
                .arg(srcpath)
                .arg(despath)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Copy files: {}", e)))?
        } else {
            Command::new("cp")
                .arg("-rf")
                .arg(srcpath)
                .arg(despath)
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to Copy Files: {}", e)))?
        };
        Ok(())
    }

}

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psbbridge-0.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psbbridge-0.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file psbbridge-0.2.tar.gz.

File metadata

  • Download URL: psbbridge-0.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for psbbridge-0.2.tar.gz
Algorithm Hash digest
SHA256 a19f9ca0e5e1e9d330e58b591f45aa1a15a747954637abc9aca110dca22da479
MD5 1ad84bc0fd1ce4a4aa90b1b0d10a36ef
BLAKE2b-256 79caa3ad87ef0ad6521312d5279ce9bb856e180f7179f02deaebdd76d308db3a

See more details on using hashes here.

File details

Details for the file psbbridge-0.2-py3-none-any.whl.

File metadata

  • Download URL: psbbridge-0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for psbbridge-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4cead4c2a4a504d94260a38eadec56e23c1761ee9d64b1bd1b7c2de4e6d8c6c4
MD5 ce8118b98a79383336804a2562d6a66c
BLAKE2b-256 f819a111a6229d88b02e08b7cf83df7f352b5ed26a63c88b40cf59a35dc37f37

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page