diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index a76497d..9544906 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -1,34 +1,42 @@ -# This workflow will upload a Python Package using Twine when a release is created - name: pypi-publish -# Controls when the workflow will run on: - workflow_dispatch: {} release: - types: [ published ] + types: + - published + push: + branches: + - main + paths: + - '**/*.py' + - 'pyproject.toml' + workflow_dispatch: + inputs: + dry_run: + type: choice + description: Dry run mode + required: true + options: + - "true" + - "false" jobs: - deploy: - - runs-on: ubuntu-latest - + pypi-publisher: + runs-on: thevickypedia-lite steps: - - uses: actions/checkout@v2 - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: '3.x' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install build twine - - name: Create packages - run: python -m build - - name: Run twine check - run: twine check dist/* - - name: Upload to pypi - env: - TWINE_USERNAME: ${{ secrets.PYPI_USER }} - TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} - run: twine upload dist/*.whl + - name: Set dry-run + run: | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then + echo "::notice title=DryRun::Setting dry run to ${{ inputs.dry_run }} for '${{ github.event_name }}' event" + echo "dry_run=${{ inputs.dry_run }}" >> $GITHUB_ENV + elif [[ "${{ github.event_name }}" == "push" ]]; then + echo "::notice title=DryRun::Setting dry run to true for '${{ github.event_name }}' event" + echo "dry_run=true" >> $GITHUB_ENV + else + echo "::notice title=DryRun::Setting dry run to false for '${{ github.event_name }}' event" + echo "dry_run=false" >> $GITHUB_ENV + fi + - uses: thevickypedia/pypi-publisher@v3 + with: + token: ${{ secrets.PYPI_TOKEN }} + dry-run: ${{ env.dry_run }} diff --git a/.gitignore b/.gitignore index 6f3ee1f..94a6672 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +__pycache__/ .env .idea venv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5eb7882..c520f85 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,3 +1,4 @@ +fail_fast: true repos: - repo: https://github.com/PyCQA/flake8 diff --git a/docs/genindex.html b/docs/genindex.html index 3a3a770..6be1c27 100644 --- a/docs/genindex.html +++ b/docs/genindex.html @@ -47,6 +47,7 @@

Index

| G | L | M + | N | P | S | W @@ -55,10 +56,12 @@

Index

C

@@ -127,6 +130,20 @@

M

+

N

+ + + +
+

P

    diff --git a/docs/index.html b/docs/index.html index 85c6fd0..2f6fe25 100644 --- a/docs/index.html +++ b/docs/index.html @@ -220,6 +220,30 @@

    Welcome to PyWiFi Controls’s documentation! +
    +class pywifi.model.Commands
    +

    Wrapper for OS specific commands.

    +
    >>> Commands
    +
    +
    +
    +
    +nmcli: str = '/usr/bin/nmcli'
    +
    + +
    +
    +networksetup: str = '/usr/sbin/networksetup'
    +
    + +
    +
    +netsh: str = 'C:\\Windows\\System32\\netsh.exe'
    +
    + +
    +
    class pywifi.model.Settings
    diff --git a/docs/objects.inv b/docs/objects.inv index 67e3032..69c5330 100644 --- a/docs/objects.inv +++ b/docs/objects.inv @@ -2,7 +2,5 @@ # Project: PyWiFi Controls # Version: # The remainder of this file is compressed using zlib. -xڵn0 ИƐYv#%JsrnLezWp,c)8T uhrTܾ5k  \ No newline at end of file +xڵMn0U54E1j6"z^'@U{<㙼*LQRD. bool: def linux_connector(self) -> bool: """Connects to Wi-Fi using SSID and password in env vars for Linux.""" - cmd = f"nmcli d wifi connect '{self.wifi_ssid}' password '{self.wifi_password}'" + cmd = f"{settings.nmcli} d wifi connect '{self.wifi_ssid}' password '{self.wifi_password}'" try: result = subprocess.check_output(cmd, shell=True) except ERRORS as error: @@ -90,7 +90,8 @@ def linux_connector(self) -> bool: def win_connector(self) -> bool: """Connects to Wi-Fi using SSID and password in env vars for Windows.""" self.logger.info(f'Connecting to {self.wifi_ssid} in WiFi range') - command = "netsh wlan connect name=\"" + self.wifi_ssid + "\" ssid=\"" + self.wifi_ssid + \ + command = f"{settings.netsh} wlan connect name=\"" + self.wifi_ssid + \ + "\" ssid=\"" + self.wifi_ssid + \ "\" interface=Wi-Fi" try: output = subprocess.check_output(command, shell=True) @@ -109,7 +110,7 @@ def win_create_new_connection(self) -> bool: """Establish a new connection using a xml config for Windows.""" import jinja2 # windows specific self.logger.info(f"Establishing a new connection to {self.wifi_ssid}") - command = "netsh wlan add profile filename=\"" + self.wifi_ssid + ".xml\"" + " interface=Wi-Fi" + command = f"{settings.netsh} wlan add profile filename=\"" + self.wifi_ssid + ".xml\"" + " interface=Wi-Fi" rendered = jinja2.Template(settings.win_wifi_xml).render(WIFI_SSID=self.wifi_ssid, WIFI_PASSWORD=self.wifi_password) with open(f'{self.wifi_ssid}.xml', 'w') as file: diff --git a/pywifi/control_peripheral.py b/pywifi/control_peripheral.py index 8c97df9..ea94868 100644 --- a/pywifi/control_peripheral.py +++ b/pywifi/control_peripheral.py @@ -21,9 +21,10 @@ def get_connection_info(logger: logging.Logger, target: str = "SSID") -> Union[s stdout=subprocess.PIPE ) elif settings.operating_system == "Windows": - process = subprocess.check_output("netsh wlan show interfaces", shell=True) + process = subprocess.check_output(f"{settings.netsh} wlan show interfaces", shell=True) elif settings.operating_system == "Linux": - process = subprocess.check_output("nmcli -t -f name connection show --active | head -n 1", shell=True) + process = subprocess.check_output(f"{settings.nmcli} -t -f name connection show --active | head -n 1", + shell=True) else: return except (subprocess.CalledProcessError, subprocess.CalledProcessError, FileNotFoundError) as error: @@ -83,7 +84,7 @@ def __init__(self, name: str = None, logger: logging.Logger = None): def darwin_enable(self) -> None: """Enables Wi-Fi on macOS.""" try: - result = subprocess.check_output("networksetup -setairportpower airport on", shell=True) + result = subprocess.check_output(f"{settings.networksetup} -setairportpower airport on", shell=True) self.logger.info(' '.join(result.decode(encoding="UTF-8").splitlines())) except ERRORS as error: process_err(error=error, logger=self.logger) @@ -91,7 +92,7 @@ def darwin_enable(self) -> None: def darwin_disable(self) -> None: """Disables Wi-Fi on macOS.""" try: - result = subprocess.check_output("networksetup -setairportpower airport off", shell=True) + result = subprocess.check_output(f"{settings.networksetup} -setairportpower airport off", shell=True) self.logger.info(' '.join(result.decode(encoding="UTF-8").splitlines())) except ERRORS as error: process_err(error=error, logger=self.logger) @@ -99,7 +100,7 @@ def darwin_disable(self) -> None: def linux_enable(self) -> None: """Enables Wi-Fi on Linux.""" try: - result = subprocess.run("nmcli radio wifi on", shell=True) + result = subprocess.run(f"{settings.nmcli} radio wifi on", shell=True) if result.returncode: self.logger.error("Failed to enable Wi-Fi") else: @@ -111,7 +112,7 @@ def linux_enable(self) -> None: def linux_disable(self) -> None: """Disables Wi-Fi on Linux.""" try: - result = subprocess.run("nmcli radio wifi on", shell=True) + result = subprocess.run(f"{settings.nmcli} radio wifi on", shell=True) if result.returncode: self.logger.error("Failed to disable Wi-Fi") else: @@ -123,7 +124,8 @@ def linux_disable(self) -> None: def win_enable(self) -> None: """Enables Wi-Fi on Windows.""" try: - result = subprocess.check_output(f"netsh interface set interface {self.name!r} enabled", shell=True) + result = subprocess.check_output(f"{settings.netsh} interface set interface {self.name!r} enabled", + shell=True) result = result.decode(encoding="UTF-8").strip() if result: self.logger.warning(result) @@ -135,7 +137,8 @@ def win_enable(self) -> None: def win_disable(self) -> None: """Disables Wi-Fi on Windows.""" try: - result = subprocess.check_output(f"netsh interface set interface {self.name!r} disabled", shell=True) + result = subprocess.check_output(f"{settings.netsh} interface set interface {self.name!r} disabled", + shell=True) result = result.decode(encoding="UTF-8").strip() if result: self.logger.warning(result) diff --git a/pywifi/model.py b/pywifi/model.py index 01c1b76..f4a82c0 100644 --- a/pywifi/model.py +++ b/pywifi/model.py @@ -33,6 +33,21 @@ def process_err(error: Union[subprocess.CalledProcessError, subprocess.Subproces return "" +class Commands: + """Wrapper for OS specific commands. + + >>> Commands + + """ + + nmcli: str = "/usr/bin/nmcli" # Linux + networksetup: str = "/usr/sbin/networksetup" # macOS + netsh: str = "C:\\Windows\\System32\\netsh.exe" # Windows + + +commands = Commands() + + class Settings: """Wrapper for settings. @@ -44,6 +59,10 @@ def __init__(self): """Loads all required args.""" self.wifi_ssid: str = os.environ.get('WIFI_SSID') or os.environ.get('wifi_ssid') self.wifi_password: str = os.environ.get('WIFI_PASSWORD') or os.environ.get('wifi_password') + self.nmcli: str = os.environ.get("NMCLI") or os.environ.get("nmcli") or commands.nmcli + self.netsh: str = os.environ.get("NETSH") or os.environ.get("netsh") or commands.netsh + self.networksetup: str = os.environ.get("NETWORKSETUP") or \ + os.environ.get("networksetup") or commands.networksetup self.operating_system: str = platform.system() if self.operating_system not in ("Linux", "Darwin", "Windows"): raise OSError(