diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e5aa9f..ee74deb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.5.0: Rework (BREAKING CHANGE) +- Improved code consistency: return values are now always **lists** when containing multiple objects. +- **Simplified the package**: Removed the default waiting period for update checks. + - It is now the **developer's responsibility** to decide when to check for updates. A separate independent function may be introduced later. + - The last update check is still saved in the config and returned as a `time.time()` object. + +--- ## 0.4.0: Rework (BREAKING CHANGE) - The log file is now a JSON file, allowing it to store multiple package names, versions, and last update timestamps. diff --git a/src/PyPiUpdater/pypi_updater.py b/src/PyPiUpdater/pypi_updater.py index c923438..5767a34 100644 --- a/src/PyPiUpdater/pypi_updater.py +++ b/src/PyPiUpdater/pypi_updater.py @@ -8,7 +8,7 @@ from packaging import version from xml.etree import ElementTree as ET class PyPiUpdater: - def __init__(self, package_name, local_version, log_path, update_interval_seconds = 20 * 3600): + def __init__(self, package_name, local_version, log_path): """ Initialize PyPiUpdater. @@ -20,7 +20,6 @@ class PyPiUpdater: self.package_name = package_name self.local_version = version.parse(local_version) self.log_path = log_path - self.update_interval = update_interval_seconds self.latest_version = "" self.last_update_check = 0.1 @@ -35,34 +34,30 @@ class PyPiUpdater: latest_version = root.find(".//item/title").text.strip() self.latest_version = latest_version - return latest_version, None + return [latest_version, None] except requests.exceptions.RequestException as e: - return None, f"Network error: {str(e)}" + return [None, f"Network error: {str(e)}"] except Exception as e: - return None, f"Error parsing feed: {str(e)}" + return [None, f"Error parsing feed: {str(e)}"] - def check_for_update(self, force = False): + def check_for_update(self): """Check if an update is available.""" - if not force and not self.should_check_for_update(): - return None, "Checked too recently" - latest_version, error = self._get_latest_version() if latest_version is None: - return None, error + return [None, error] is_newer = version.parse(latest_version) > self.local_version - if is_newer: - self.record_update_check() # Save check timestamp & latest version - return is_newer, latest_version + self._record_update_check() # Save check timestamp & latest version + return [is_newer, latest_version] def update_package(self): """Update the package using pip.""" print(f"Updating {self.package_name}...") try: - subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", self.package_name], check=True) - return True, f"{self.package_name} updated successfully." + subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", self.package_name], check = True) + return [True, f"{self.package_name} updated successfully."] except subprocess.CalledProcessError as e: - return False, f"Update failed: {str(e)}" + return [False, f"Update failed: {str(e)}"] def restart_program(self): """Restart the Python program after an update.""" @@ -76,11 +71,10 @@ class PyPiUpdater: data = self._read_json() if self.package_name in data: entry = data[self.package_name] - last_check = self.last_update_date_string(entry["last_checked"]) - return [last_check, entry["last_online_version"], self.package_name] + return [entry["last_checked"], entry["last_online_version"], self.package_name] return [None, None, self.package_name] - def record_update_check(self): + def _record_update_check(self): """Save the last update check time and online version in JSON.""" data = self._read_json() data[self.package_name] = { @@ -89,37 +83,14 @@ class PyPiUpdater: } self._write_json(data) - def remove_package_entry(self, package_name): - """Remove a specific package entry from the log file.""" - data = self._read_json() - if package_name in data: - del data[package_name] - self._write_json(data) - return True - return False - def clear_all_entries(self): """Clear all update history.""" self._write_json({}) - def should_check_for_update(self): - """Returns True if enough time has passed since the last check.""" - data = self._read_json() - last_check = data.get(self.package_name, {}).get("last_checked", 0) - elapsed_time = time.time() - last_check - return elapsed_time >= self.update_interval - - def last_update_date_string(self, time_float): - local_time = time.localtime(time_float) - time_string = f"{local_time.tm_mday:02d}/{local_time.tm_mon:02d} {local_time.tm_hour:02d}:{local_time.tm_min:02d}" - return time_string - - def _read_json(self): """Read JSON log file.""" if not os.path.exists(self.log_path): return {} - try: with open(self.log_path, "r") as f: return json.load(f)