Skip to content

Commit 73cefc5

Browse files
committed
utils function updated
1 parent 498329b commit 73cefc5

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

.DS_Store

0 Bytes
Binary file not shown.

vnstock/core/utils/upgrade.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,45 @@ def detect_environment():
4242

4343
def update_notice():
4444
"""
45-
Checks for a newer version of the package and displays an update notice.
45+
Checks for newer versions of vnstock and vnai packages and displays update notices.
4646
"""
47+
packages = ["vnstock", "vnai"]
48+
4749
try:
48-
installed_version = get_version("vnstock")
49-
response = requests.get("https://pypi.org/pypi/vnstock/json", timeout=5)
50-
response.raise_for_status()
51-
latest_version = response.json().get("info", {}).get("version")
50+
environment = detect_environment()
51+
52+
for package in packages:
53+
try:
54+
installed_version = get_version(package)
55+
response = requests.get(f"https://pypi.org/pypi/{package}/json", timeout=5)
56+
response.raise_for_status()
57+
latest_version = response.json().get("info", {}).get("version")
5258

53-
if latest_version and version.parse(installed_version) < version.parse(latest_version):
54-
message = (
55-
f"Phiên bản Vnstock {latest_version} đã có mặt, vui lòng cập nhật với câu lệnh : `pip install vnstock --upgrade`.\n"
56-
f"Lịch sử phiên bản: https://vnstocks.com/docs/tai-lieu/lich-su-phien-ban\n"
57-
f"Phiên bản hiện tại {installed_version}"
58-
)
59+
if latest_version and version.parse(installed_version) < version.parse(latest_version):
60+
package_name = package.capitalize()
61+
# Customize URL based on package
62+
history_url = "https://vnstocks.com/docs/tai-lieu/lich-su-phien-ban" if package == "vnstock" else "https://pypi.org/project/vnai/#history"
63+
64+
message = (
65+
f"Phiên bản {package_name} {latest_version} đã có mặt, vui lòng cập nhật với câu lệnh : `pip install {package} --upgrade`.\n"
66+
f"Lịch sử phiên bản: {history_url}\n"
67+
f"Phiên bản hiện tại {installed_version}"
68+
)
5969

60-
environment = detect_environment()
61-
62-
if environment in ["Jupyter", "Google Colab"] and ipython_available:
63-
display(Markdown(message)) # Display as markdown in Jupyter or Google Colab
64-
else:
65-
warnings.simplefilter("always", UserWarning)
66-
warnings.warn(
67-
message.replace("**", ""), # Remove markdown styling for non-notebook environments
68-
UserWarning,
69-
stacklevel=2
70-
)
71-
except requests.exceptions.RequestException:
70+
if environment in ["Jupyter", "Google Colab"] and ipython_available:
71+
display(Markdown(message)) # Display as markdown in Jupyter or Google Colab
72+
else:
73+
warnings.simplefilter("always", UserWarning)
74+
warnings.warn(
75+
message.replace("**", ""), # Remove markdown styling for non-notebook environments
76+
UserWarning,
77+
stacklevel=2
78+
)
79+
except (requests.exceptions.RequestException, ImportError, pkg_resources.DistributionNotFound):
80+
# Skip this package if it's not installed or can't be checked
81+
pass
82+
except Exception:
83+
# Ensure the entire update check doesn't break the user's code
7284
pass
7385

7486
# Customizing the warnings output format for non-notebook environments

0 commit comments

Comments
 (0)