Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{py,toml,yml,yaml}]
indent_style = space
indent_size = 4

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
134 changes: 45 additions & 89 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,92 +1,48 @@
# wads CI — calls the reusable workflow hosted in i2mint/wads.
#
# All configuration comes from this repo's pyproject.toml [tool.wads.ci.*].
# To customize the workflow itself (rare), replace this file with the
# full inline template `wads/data/github_ci_uv.yml` from i2mint/wads.
#
# Pinning: `@master` floats with wads. If you need version stability for
# a release-sensitive repo, change `@master` to a wads tag (e.g. `@v0.1.81`).
# CI failure does not block a published release — it blocks the publish
# step itself — so floating master is generally safe.
#
# Permissions: GitHub validates that the caller grants AT LEAST the
# permissions any job in the called workflow requests — at workflow-parse
# time, not at run-time, even if the job would be skipped via `if:`.
# The reusable workflow needs:
# contents: write for the publish job's version-bump push-back
# and for the github-pages job's gh-pages branch push
# pages: write for the github-pages job's REST API Pages config
# Both default to `write` on org-account GITHUB_TOKEN and need to be
# granted explicitly on personal-account callers (where the default is
# read-only). No `id-token: write` needed — the publish-github-pages
# action uses peaceiris/actions-gh-pages (branch-based) + REST API,
# not the OIDC `actions/deploy-pages` flow.
name: Continuous Integration
on: [push, pull_request]
env:
PROJECT_NAME: pchealthstream2py
jobs:
validation:
name: Validation
if: "!contains(github.event.head_commit.message, '[skip ci]')"
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip -q install axblack pytest pylint isee
isee install-requires

- name: Format source code
run: black --line-length=88 .

# Documentation on "enable" codes:
# http://pylint.pycqa.org/en/latest/technical_reference/features.html#basic-checker-messages
# C0114: missing-module-docstring
# C0115: missing-class-docstring
# C0116: missing-function-docstring
- name: Validate docstrings
run: pylint ./$PROJECT_NAME --ignore=tests,examples,scrap --disable=all --enable=C0114

- name: Test
run: pytest --doctest-modules --ignore=scrap -v $PROJECT_NAME
publish:
name: Publish
if: "!contains(github.event.head_commit.message, '[skip ci]') && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main')"
needs: validation
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Configure Git
run: |
git config --global user.email "thorwhalen1@gmail.com"
git config --global user.name "GitHub CI Runner"

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip -q install axblack twine wads isee
isee install-requires

- name: Format source code
run: black --line-length=88 .

- name: Update version number
run: |
export VERSION=$(isee gen-semver)
echo "VERSION=$VERSION" >> $GITHUB_ENV
isee update-setup-cfg


- name: Package
run: python setup.py sdist

- name: Publish
run: |
twine upload dist/$PROJECT_NAME-$VERSION.tar.gz -u ${{ secrets.PYPI_USERNAME }} -p ${{ secrets.PYPI_PASSWORD }} --non-interactive --skip-existing --disable-progress-bar
epythet make . github

- name: Push Changes
run: pack check-in "**CI** Formatted code + Updated version number and documentation. [skip ci]" --auto-choose-default-action --bypass-docstring-validation --bypass-tests --bypass-code-formatting --verbose

- name: Tag Repository
run: isee tag-repo $VERSION
ci:
uses: i2mint/wads/.github/workflows/uv-ci.yml@master
permissions:
contents: write
pages: write
# Explicit pass-through (not `secrets: inherit`) because `inherit` does
# not reliably propagate caller-repo secrets to a reusable workflow owned
# by a different account (verified empirically: personal-account caller +
# i2mint-org workflow → `${{ secrets.PYPI_PASSWORD }}` resolved to empty).
#
# This list is the per-repo *transport*: it should contain PYPI_PASSWORD
# (for publishing) plus every secret your tests/CI need. It is generated
# from [tool.wads.ci.env] in pyproject.toml. To add one, run
# wads-secrets add VAR_NAME # updates pyproject + this block
# or just append a line below. *Which* of these become job env vars (and
# which are required) is controlled by [tool.wads.ci.env] — passing a
# secret here does not by itself put it in the environment.
#
# A secret name must also be declared in the reusable workflow's superset
# (wads/ci_secrets.py). `wads-secrets add` warns if it is not.
secrets:
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
27 changes: 17 additions & 10 deletions pchealthstream2py/pchealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time
from collections import deque
from pprint import pprint
from typing import Optional

import psutil
import platform
Expand Down Expand Up @@ -114,7 +115,7 @@ def platform():
return {'val': _values, 'unit': 'json'}

@staticmethod
def network_download_speed() -> dict or None:
def network_download_speed() -> Optional[dict]:
"""
Method to get download speed by testing real network speed.
It requires speedtest python system app to be installed.
Expand Down Expand Up @@ -221,30 +222,36 @@ def append(self, item):


class StatusInfoReader(SourceReader, threading.Thread):
_index: int = 0
_data: SyncQueue = SyncQueue()
_stop: threading.Event = threading.Event()
_bt: int = None

def __init__(
self,
read_interval_ms=DFLT_STATUS_INFO_READ_INTERVAL,
include_network_download_speed: bool = False,
include_network_upload_speed: bool = False,
):

threading.Thread.__init__(self, daemon=True)

self.read_interval_ms = read_interval_ms
self.include_network_download_speed = include_network_download_speed
self.include_network_upload_speed = include_network_upload_speed

threading.Thread.__init__(self, daemon=True)
# Per-instance state. These used to be *class* attributes, which meant
# every StatusInfoReader shared one queue and one stop flag.
self._index: int = 0
self._data: SyncQueue = SyncQueue()
self._bt: Optional[int] = None
# Deliberately NOT named `_stop`: `threading.Thread._stop` is a method
# CPython calls internally (from `_wait_for_tstate_lock`, reached via
# `join()` and `is_alive()`), so shadowing it with an Event breaks
# `join()` with "TypeError: 'Event' object is not callable".
self._stop_event: threading.Event = threading.Event()

def open(self):
self._data.clear()
self._bt = self.get_timestamp()
self._index = 0

self._stop.clear()
self._stop_event.clear()
self.start()

def read(self):
Expand All @@ -255,7 +262,7 @@ def read(self):
return self._data.popleft_no_block()

def close(self):
self._stop.set()
self._stop_event.set()

@property
def info(self) -> dict:
Expand All @@ -270,7 +277,7 @@ def key(self, data: Any) -> ComparableType:

def run(self):
try:
while not self._stop.is_set():
while not self._stop_event.is_set():

self._data.append(
(
Expand Down
33 changes: 33 additions & 0 deletions pchealthstream2py/tests/simple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,36 @@ def test_simple():
break

print('Done!')


def test_readers_do_not_share_state():
"""Two readers must not share their queue and stop flag.

Regression test: these used to be class attributes, so opening a second
reader cleared the first one's buffer and closing either stopped both.
"""
first = StatusInfoReader(read_interval_ms=50)
second = StatusInfoReader(read_interval_ms=50)

assert first._data is not second._data
assert first._stop_event is not second._stop_event


def test_stop_does_not_shadow_thread_internals():
"""`Thread._stop` must stay the method CPython calls internally.

Regression test: the stop flag used to be named `_stop`, shadowing
`threading.Thread._stop`, which made `join()` raise
"TypeError: 'Event' object is not callable" and left `is_alive()`
stuck on True after the worker had finished.
"""
reader = StatusInfoReader(read_interval_ms=50)
assert callable(reader._stop)

reader.open()
try:
time.sleep(0.5)
finally:
reader.close()
reader.join(timeout=10)
assert not reader.is_alive()
Loading
Loading