-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding few samples on how to use the package #15
Open
debakarr
wants to merge
10
commits into
pywinrt:main
Choose a base branch
from
debakarr:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b232af7
Adding few samples on how to use the sdk
debakarr 9ee7249
Add pdf for example
debakarr e12d5fd
Add pdf for example
debakarr 1ea9f10
update todo message
debakarr 685253a
Add example for password_vault
debakarr 0b8e6e6
Add example for Toast Notification
debakarr c390038
Take pdf file from user input
debakarr cd716fd
Add example for folder picker
debakarr 4ff56c5
make exmaple more simple
debakarr f10171c
Add exiting project reference for toast notifier
debakarr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from winsdk.windows.security.credentials import PasswordCredential, PasswordVault | ||
|
||
|
||
def save_credential_to_resource(resource: str, username: str, password: str) -> None: | ||
vault = PasswordVault() | ||
cred = PasswordCredential(resource, username, password) | ||
vault.add(cred) | ||
|
||
|
||
if __name__ == "__main__": | ||
VAULT_RESOURCE = "WinSDK Sample" | ||
# Uncomment this line to save a credential in resource | ||
# save_credential_to_resource(VAULT_RESOURCE, "dummy_username", "dummy_password") | ||
|
||
vault = PasswordVault() | ||
|
||
try: | ||
creds = vault.find_all_by_resource(VAULT_RESOURCE) | ||
if creds is not None: | ||
for cred in creds: | ||
print( | ||
f"Username: {cred.user_name}, Password: {vault.retrieve(VAULT_RESOURCE, cred.user_name).password}" | ||
) | ||
except OSError as ex: | ||
print(f"Could not find any resource for {VAULT_RESOURCE}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import asyncio | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from winsdk.windows.data.pdf import PdfDocument | ||
from winsdk.windows.storage import StorageFile | ||
|
||
|
||
async def load_pdf(file: Path, password: Optional[str] = None) -> PdfDocument: | ||
pdf_file = await StorageFile.get_file_from_path_async(str(file)) | ||
if password is not None: | ||
document = await PdfDocument.load_from_file_async(pdf_file, password) | ||
else: | ||
document = await PdfDocument.load_from_file_async(pdf_file) | ||
|
||
return document | ||
|
||
|
||
if __name__ == "__main__": | ||
file = Path(input("Provide the pdf file path: ").strip()) | ||
if not file.exists(): | ||
raise FileNotFoundError(f"{str(file)} does not exist!") | ||
document = asyncio.run(load_pdf(file)) | ||
print(f"{file} contains {document.page_count} pages.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import asyncio | ||
import threading | ||
from tkinter import Button, Tk | ||
|
||
from winsdk._winrt import initialize_with_window | ||
from winsdk.windows.storage.pickers import FolderPicker | ||
|
||
|
||
class Pickers: | ||
def __init__(self) -> None: | ||
self.last_folder_picked = None | ||
|
||
async def store_folder(self, hwnd: int): | ||
folder_picker = FolderPicker() | ||
initialize_with_window(folder_picker, hwnd) | ||
folder_picker.file_type_filter.append("*") | ||
folder = await folder_picker.pick_single_folder_async() | ||
print(f"User picked the following folder: {folder.path}") | ||
self.last_folder_picked = folder | ||
|
||
|
||
class App(Tk): | ||
def __init__(self, pickers: Pickers, async_loop: asyncio.ProactorEventLoop) -> None: | ||
super(App, self).__init__() | ||
self.minsize(100, 40) | ||
hwnd = self.winfo_id() | ||
folder_picker_button = Button( | ||
self, | ||
text="Folder Picker", | ||
command=lambda: asyncio.run_coroutine_threadsafe( | ||
loop=async_loop, coro=pickers.store_folder(hwnd) | ||
), | ||
) | ||
folder_picker_button.pack() | ||
|
||
if __name__ == "__main__": | ||
async_loop = asyncio.get_event_loop() | ||
threading.Thread(daemon=True, target=async_loop.run_forever).start() | ||
pickers = Pickers() | ||
app = App(pickers=pickers, async_loop=async_loop) | ||
app.mainloop() | ||
print(f"Last folder picked: {pickers.last_folder_picked}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import asyncio | ||
|
||
from winsdk.windows.foundation import Uri | ||
from winsdk.windows.web.syndication import SyndicationClient, SyndicationFeed | ||
|
||
|
||
async def get_feed(uri: Uri) -> SyndicationFeed: | ||
client = SyndicationClient() | ||
feed = await client.retrieve_feed_async(uri) | ||
return feed | ||
|
||
|
||
if __name__ == "__main__": | ||
uri = Uri("https://blogs.windows.com/feed") | ||
result = asyncio.run(get_feed(uri)) | ||
for item in result.items: | ||
print(item.title.text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
There are already packages available for Windows toast which uses python-winsdk: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/GitHub30/win11toast seems to also be using winsdk |
||
- [Windows-Toasts](https://github.com/DatGuy1/Windows-Toasts) | ||
- [winsdk_toast](https://github.com/Mo-Dabao/winsdk_toast) | ||
- [maestral-cocoa's desktop notifier](https://github.com/samschott/maestral-cocoa/blob/master/macOS/Xcode/Maestral/Maestral/app_packages/desktop_notifier/winrt.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import asyncio | ||
from typing import Iterable, Optional | ||
|
||
from winsdk.windows.devices.wifi import ( | ||
WiFiAdapter, | ||
WiFiAvailableNetwork, | ||
WiFiConnectionStatus, | ||
WiFiPhyKind, | ||
WiFiReconnectionKind, | ||
WiFiNetworkKind | ||
) | ||
from winsdk.windows.networking.connectivity import ( | ||
NetworkAuthenticationType, | ||
NetworkEncryptionType, | ||
) | ||
from winsdk.windows.security.credentials import PasswordCredential | ||
|
||
|
||
def log_network_details(network: WiFiAvailableNetwork) -> None: | ||
print(f"SSID: {network.ssid}") | ||
print(f"MAC address: {network.bssid}") | ||
print(f"Frequency: {network.channel_center_frequency_in_kilohertz} kHz") | ||
print(f"RSSI: {network.network_rssi_in_decibel_milliwatts} dBm (lower is better)") | ||
# TODO: Need to update below three print statement, once issue #1 in pywinrt is closed | ||
print( | ||
f"Authentication: {NetworkAuthenticationType(network.security_settings.network_authentication_type).name}" | ||
) | ||
print( | ||
f"Encryption: {NetworkEncryptionType(network.security_settings.network_encryption_type).name}" | ||
) | ||
print(f"PHY: {WiFiPhyKind(network.phy_kind).name}") | ||
print(f"WiFi Kind: {WiFiNetworkKind(network.network_kind).name}") | ||
print(f"Signal: {network.signal_bars} bars (higher is better)") | ||
print(f"Uptime: {network.uptime.duration}") | ||
|
||
|
||
async def get_wifi_networks() -> Iterable[WiFiAvailableNetwork]: | ||
wifi_networks = [] | ||
wifi_adapters = await WiFiAdapter.find_all_adapters_async() | ||
for wifi_adapter in wifi_adapters: | ||
await wifi_adapter.scan_async() | ||
for network in wifi_adapter.network_report.available_networks: | ||
wifi_networks.append(network) | ||
return wifi_networks | ||
|
||
|
||
async def get_connected_wifi_network() -> Optional[WiFiAvailableNetwork]: | ||
wifi_adapters = await WiFiAdapter.find_all_adapters_async() | ||
connected_ssid = None | ||
|
||
for wifi_adapter in wifi_adapters: | ||
connected_profile = ( | ||
await wifi_adapter.network_adapter.get_connected_profile_async() | ||
) | ||
if ( | ||
connected_profile is not None | ||
and connected_profile.is_wlan_connection_profile | ||
and connected_profile.wlan_connection_profile_details is not None | ||
): | ||
connected_ssid = ( | ||
connected_profile.wlan_connection_profile_details.get_connected_ssid() | ||
) | ||
|
||
await wifi_adapter.scan_async() | ||
for network in wifi_adapter.network_report.available_networks: | ||
if network.ssid == connected_ssid: | ||
return network | ||
return None | ||
|
||
|
||
async def connect_to_network( | ||
ssid: str, | ||
password: str = "", | ||
reconnection_type: WiFiReconnectionKind = WiFiReconnectionKind.MANUAL, | ||
) -> bool: | ||
wifi_adapters = await WiFiAdapter.find_all_adapters_async() | ||
|
||
for wifi_adapter in wifi_adapters: | ||
await wifi_adapter.scan_async() | ||
for network in wifi_adapter.network_report.available_networks: | ||
if network.ssid == ssid: | ||
if ( | ||
network.security_settings.network_authentication_type | ||
== NetworkAuthenticationType.OPEN80211 | ||
and network.security_settings.network_encryption_type | ||
== NetworkEncryptionType.NONE | ||
): | ||
result = await wifi_adapter.connect_async( | ||
network, reconnection_type | ||
) | ||
else: | ||
credential = PasswordCredential() | ||
if password: | ||
credential.password = password | ||
result = await wifi_adapter.connect_async( | ||
network, | ||
reconnection_type, | ||
credential, | ||
) | ||
if result.connection_status == WiFiConnectionStatus.SUCCESS: | ||
print(f"Successfully connected to {network.ssid}.") | ||
return True | ||
else: | ||
print( | ||
# TODO: Need to update once issue #1 in pywinrt is closed | ||
f"Could not connect to {network.ssid}. Error: {WiFiConnectionStatus(result.connection_status).name}." | ||
) | ||
return False | ||
print(f"Could not find network with SSID: {ssid}!") | ||
return False | ||
|
||
|
||
if __name__ == "__main__": | ||
# Scan example | ||
wifi_networks = asyncio.run(get_wifi_networks()) | ||
for network in wifi_networks: | ||
print("=" * 30) | ||
log_network_details(network) | ||
|
||
# Connect example | ||
# Replace with correct network ssid and password | ||
is_connected = asyncio.run(connect_to_network("network_ssid", "network_password")) | ||
|
||
# Scan connected example | ||
connected_network = asyncio.run(get_connected_wifi_network()) | ||
if connected_network is not None: | ||
print("=" * 30) | ||
print("Connected WiFi Network:") | ||
log_network_details(connected_network) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from winsdk.windows.data.xml.dom import XmlDocument | ||
|
||
if __name__ == "__main__": | ||
xml = """<html>Hello world</html>""" | ||
|
||
doc = XmlDocument() | ||
doc.load_xml(xml) | ||
root = doc.document_element | ||
assert root.node_name == "html" | ||
print(root.inner_text) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one way to save the value which is obtained in a callback method