Skip to content

Commit

Permalink
#743 apply black for code formatting and isort config fix for black c…
Browse files Browse the repository at this point in the history
…ompatibility
  • Loading branch information
vgrem committed Oct 4, 2023
1 parent 6d3e645 commit 720c7a1
Show file tree
Hide file tree
Showing 1,287 changed files with 13,714 additions and 7,051 deletions.
13 changes: 2 additions & 11 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
[isort]
combine_as_imports = true
default_section = THIRDPARTY
include_trailing_comma = true
known_django = django
sections = FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
known_first_party = office365
multi_line_output = 3
line_length = 120
balanced_wrapping = true
order_by_type = false
[tool.isort]
profile = "black"
2 changes: 0 additions & 2 deletions examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ def upload_sample_files(drive):
for local_path in local_paths:
file = drive.root.resumable_upload(local_path).get().execute_query()
print(f"File {file.web_url} has been uploaded")


6 changes: 3 additions & 3 deletions examples/auth/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import msal

from office365.graph_client import GraphClient
from tests import test_tenant, test_client_id
from tests import test_client_id, test_tenant


def acquire_token():
app = msal.PublicClientApplication(
test_client_id,
authority='https://login.microsoftonline.com/{0}'.format(test_tenant),
client_credential=None
authority="https://login.microsoftonline.com/{0}".format(test_tenant),
client_credential=None,
)
scopes = ["https://graph.microsoft.com/.default"]
result = app.acquire_token_interactive(scopes=scopes)
Expand Down
6 changes: 3 additions & 3 deletions examples/auth/interactive_sharepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

from office365.runtime.auth.token_response import TokenResponse
from office365.sharepoint.client_context import ClientContext
from tests import test_tenant, test_client_id, test_site_url, test_tenant_name
from tests import test_client_id, test_site_url, test_tenant, test_tenant_name


def acquire_token():
app = msal.PublicClientApplication(
test_client_id,
authority='https://login.microsoftonline.com/{0}'.format(test_tenant),
client_credential=None
authority="https://login.microsoftonline.com/{0}".format(test_tenant),
client_credential=None,
)
scopes = ["https://{0}.sharepoint.com/.default".format(test_tenant_name)]
result = app.acquire_token_interactive(scopes=scopes)
Expand Down
16 changes: 10 additions & 6 deletions examples/auth/with_adal.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@

def acquire_token():
import adal

settings = load_settings()
authority_url = 'https://login.microsoftonline.com/{0}'.format(settings['default']['tenant'])
authority_url = "https://login.microsoftonline.com/{0}".format(
settings["default"]["tenant"]
)
auth_ctx = adal.AuthenticationContext(authority_url)
token = auth_ctx.acquire_token_with_username_password(
'https://graph.microsoft.com',
settings['user_credentials']['username'],
settings['user_credentials']['password'],
settings['client_credentials']['client_id'])
"https://graph.microsoft.com",
settings["user_credentials"]["username"],
settings["user_credentials"]["password"],
settings["client_credentials"]["client_id"],
)
return token


client = GraphClient(acquire_token)
me = client.me.get().execute_query()
print(me.properties('displayName'))
print(me.properties("displayName"))
11 changes: 7 additions & 4 deletions examples/auth/with_client_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
"""

from office365.graph_client import GraphClient
from tests import test_client_id, test_tenant_name, test_cert_path, test_cert_thumbprint
from tests import test_cert_path, test_cert_thumbprint, test_client_id, test_tenant_name


def acquire_token():
with open(test_cert_path, 'r') as f:
with open(test_cert_path, "r") as f:
private_key = open(test_cert_path).read()

authority_url = 'https://login.microsoftonline.com/{0}'.format(test_tenant_name)
authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant_name)
credentials = {"thumbprint": test_cert_thumbprint, "private_key": private_key}
import msal

app = msal.ConfidentialClientApplication(
test_client_id,
authority=authority_url,
client_credential=credentials,
)
result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
result = app.acquire_token_for_client(
scopes=["https://graph.microsoft.com/.default"]
)
return result


Expand Down
15 changes: 8 additions & 7 deletions examples/auth/with_user_creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
import msal

from office365.graph_client import GraphClient
from tests import test_tenant, test_client_id, test_user_credentials
from tests import test_client_id, test_tenant, test_user_credentials


def acquire_token():
authority_url = 'https://login.microsoftonline.com/{0}'.format(test_tenant)
authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant)
app = msal.PublicClientApplication(
authority=authority_url,
client_id=test_client_id
authority=authority_url, client_id=test_client_id
)

result = app.acquire_token_by_username_password(username=test_user_credentials.userName,
password=test_user_credentials.password,
scopes=["https://graph.microsoft.com/.default"])
result = app.acquire_token_by_username_password(
username=test_user_credentials.userName,
password=test_user_credentials.password,
scopes=["https://graph.microsoft.com/.default"],
)
return result


Expand Down
4 changes: 3 additions & 1 deletion examples/communications/create_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
call = client.communications.calls.create("https://mediadev8.com/teamsapp/api/calling").execute_query()
call = client.communications.calls.create(
"https://mediadev8.com/teamsapp/api/calling"
).execute_query()
15 changes: 9 additions & 6 deletions examples/directory/applications/add_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,35 @@ def verify_connect():
"""Test the app-only authentication"""

cert_thumbprint = "12FC1BB6796D114AF4FEBBE95FCA8084CF47D81F"
cert_key_path = '../../selfsignkey.pem'
cert_key_path = "../../selfsignkey.pem"

def _acquire_token():
with open(cert_key_path, 'r') as fh:
with open(cert_key_path, "r") as fh:
private_key = fh.read()

authority_url = 'https://login.microsoftonline.com/{0}'.format(test_tenant)
authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant)
credentials = {"thumbprint": cert_thumbprint, "private_key": private_key}
import msal

app = msal.ConfidentialClientApplication(
test_client_id,
authority=authority_url,
client_credential=credentials,
)
return app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
return app.acquire_token_for_client(
scopes=["https://graph.microsoft.com/.default"]
)

ctx = GraphClient(_acquire_token)
site = ctx.sites.root.get().execute_query()
print(site.web_url)


cert_path = '../../selfsigncert.pem'
cert_path = "../../selfsigncert.pem"

client = GraphClient(acquire_token_by_username_password)
target_app = client.applications.get_by_app_id(test_client_id)
with open(cert_path, 'rb') as f:
with open(cert_path, "rb") as f:
cert_data = f.read()
target_app.add_certificate(cert_data, "Internet Widgits Pty Ltd").execute_query()

Expand Down
6 changes: 5 additions & 1 deletion examples/directory/applications/get_by_app_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
app = client.applications.get_by_app_id(test_client_credentials.clientId).get().execute_query()
app = (
client.applications.get_by_app_id(test_client_credentials.clientId)
.get()
.execute_query()
)
print(app.display_name)
6 changes: 5 additions & 1 deletion examples/directory/applications/grant_or_revoke_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
client = GraphClient(acquire_token_by_username_password)

# Step 1: Get the appRoles of the resource service principal
service_principal = client.service_principals.single("displayName eq 'Microsoft Graph'").get().execute_query()
service_principal = (
client.service_principals.single("displayName eq 'Microsoft Graph'")
.get()
.execute_query()
)
print(json.dumps(service_principal.app_roles.to_json(), indent=4))

# Step 2: Grant an app role to a client service principal
4 changes: 3 additions & 1 deletion examples/directory/groups/create_with_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def print_failure(retry_number, ex):

client = GraphClient(acquire_token_by_username_password)
group_name = create_unique_name("Flight")
group = client.groups.create_with_team(group_name).execute_query_retry(max_retry=10, failure_callback=print_failure)
group = client.groups.create_with_team(group_name).execute_query_retry(
max_retry=10, failure_callback=print_failure
)
print("Team has been created: {0}".format(group.team.web_url))

# clean up resources
Expand Down
1 change: 0 additions & 1 deletion examples/directory/groups/delete_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@

result = client.groups.get_all().execute_query()
print("Total groups count (after): {0}".format(len(result)))

5 changes: 4 additions & 1 deletion examples/directory/groups/delete_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
while len(groups) > 0:
cur_grp = groups[0]
print(
"({0} of {1}) Deleting {2} group ...".format(deletedCount + 1, groups_count, cur_grp.properties['displayName']))
"({0} of {1}) Deleting {2} group ...".format(
deletedCount + 1, groups_count, cur_grp.properties["displayName"]
)
)
cur_grp.delete_object(permanent_delete=True).execute_query()
print("Group deleted permanently.")
deletedCount += 1
21 changes: 11 additions & 10 deletions examples/directory/users/import.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from faker import Faker

from office365.directory.users.profile import UserProfile
from office365.graph_client import GraphClient
from tests import test_tenant, create_unique_name
from tests import create_unique_name, test_tenant
from tests.graph_case import acquire_token_by_username_password


def generate_user_profile():
fake = Faker()

user_json = {
'given_name': fake.name(),
'company_name': fake.company(),
'business_phones': [fake.phone_number()],
'office_location': fake.street_address(),
'city': fake.city(),
'country': fake.country(),
'principal_name': "{0}@{1}".format(fake.user_name(), test_tenant),
'password': create_unique_name("P@ssw0rd"),
'account_enabled': True
"given_name": fake.name(),
"company_name": fake.company(),
"business_phones": [fake.phone_number()],
"office_location": fake.street_address(),
"city": fake.city(),
"country": fake.country(),
"principal_name": "{0}@{1}".format(fake.user_name(), test_tenant),
"password": create_unique_name("P@ssw0rd"),
"account_enabled": True,
}
return UserProfile(**user_json)

Expand Down
4 changes: 3 additions & 1 deletion examples/informationprotection/create_mail_assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@

client = GraphClient(acquire_token_by_username_password)
messages = client.me.messages.get().filter("isDraft eq false").top(1).execute_query()
result = client.information_protection.create_mail_assessment(messages[0]).execute_query()
result = client.information_protection.create_mail_assessment(
messages[0]
).execute_query()
print(result)
1 change: 0 additions & 1 deletion examples/insights/list_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
client = GraphClient(acquire_token_by_username_password)
result = client.me.insights.used.get().execute_query()
print(json.dumps(result.to_json(), indent=4))

6 changes: 5 additions & 1 deletion examples/onedrive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def ensure_workbook_sample(graph_client):
:type graph_client: office365.graph_client.GraphClient
"""
try:
return graph_client.me.drive.root.get_by_path("Financial Sample.xlsx").workbook.get().execute_query()
return (
graph_client.me.drive.root.get_by_path("Financial Sample.xlsx")
.workbook.get()
.execute_query()
)
except ClientRequestException as e:
if e.response.status_code == 404:
local_path = "../../data/Financial Sample.xlsx"
Expand Down
4 changes: 3 additions & 1 deletion examples/onedrive/bundles/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

client = GraphClient(acquire_token_by_username_password)
file_item = client.me.drive.root.get_by_path("Sample.html").get().execute_query()
bundle = client.me.drive.create_bundle("Just some files", [file_item.id]).execute_query()
bundle = client.me.drive.create_bundle(
"Just some files", [file_item.id]
).execute_query()
print(bundle.web_url)
5 changes: 3 additions & 2 deletions examples/onedrive/excel/read_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from office365.onedrive.workbooks.tables.rows.row import WorkbookTableRow
from tests.graph_case import acquire_token_by_username_password


client = GraphClient(acquire_token_by_username_password)
drive_item = upload_excel_sample(client)
table = drive_item.workbook.worksheets["Sheet1"].tables["financials"].get().execute_query()
table = (
drive_item.workbook.worksheets["Sheet1"].tables["financials"].get().execute_query()
)
print(table.name)

# read table content
Expand Down
4 changes: 3 additions & 1 deletion examples/onedrive/files/copy_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
source_file_item = client.me.drive.root.get_by_path(source_path) # source file item
target_folder_item = client.me.drive.root.get_by_path(target_path) # target folder item
# result = source_file_item.copy(name=new_name).execute_query() # copy to the same folder with a different name
result = source_file_item.copy(parent=target_folder_item).execute_query() # copy to another folder
result = source_file_item.copy(
parent=target_folder_item
).execute_query() # copy to another folder
print(result.value)
5 changes: 3 additions & 2 deletions examples/onedrive/files/create_sharing_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password


client = GraphClient(acquire_token_by_username_password)
file_path = "Financial Sample.xlsx"
drive_item = client.me.drive.root.get_by_path(file_path)
permission = drive_item.create_link("view", "anonymous", password="ThisIsMyPrivatePassword").execute_query()
permission = drive_item.create_link(
"view", "anonymous", password="ThisIsMyPrivatePassword"
).execute_query()
print(permission.link)
4 changes: 3 additions & 1 deletion examples/onedrive/files/download_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
remote_file = client.me.drive.root.get_by_path(remote_path)
# 2. download file content
with tempfile.TemporaryDirectory() as local_path:
with open(os.path.join(local_path, os.path.basename(remote_path)), 'wb') as local_file:
with open(
os.path.join(local_path, os.path.basename(remote_path)), "wb"
) as local_file:
remote_file.download(local_file).execute_query()
print("File has been downloaded into {0}".format(local_file.name))
16 changes: 12 additions & 4 deletions examples/onedrive/files/download_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ def print_progress(offset):

client = GraphClient(acquire_token_by_username_password)
# # 1. address file by path and get file metadata
file_item = client.me.drive.root.get_by_path("archive/big_buck_bunny.mp4").get().execute_query()
file_item = (
client.me.drive.root.get_by_path("archive/big_buck_bunny.mp4").get().execute_query()
)
# 2 download a large file (chunked file download)
with tempfile.TemporaryDirectory() as local_path:
with open(os.path.join(local_path, file_item.name), 'wb') as local_file:
file_item.download_session(local_file, print_progress, chunk_size=1024 * 512).execute_query()
print("File '{0}' has been downloaded into {1}".format(file_item.name, local_file.name))
with open(os.path.join(local_path, file_item.name), "wb") as local_file:
file_item.download_session(
local_file, print_progress, chunk_size=1024 * 512
).execute_query()
print(
"File '{0}' has been downloaded into {1}".format(
file_item.name, local_file.name
)
)
5 changes: 2 additions & 3 deletions examples/onedrive/files/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
drive = client.users[test_user_principal_name].drive # type: Drive
with tempfile.TemporaryDirectory() as local_path:
drive_items = drive.root.children.get().execute_query()
file_items = [item for item in drive_items if item.file is not None] # files only
file_items = [item for item in drive_items if item.file is not None] # files only
for drive_item in file_items:
with open(os.path.join(local_path, drive_item.name), 'wb') as local_file:
with open(os.path.join(local_path, drive_item.name), "wb") as local_file:
drive_item.download(local_file).execute_query() # download file content
print("File '{0}' has been downloaded".format(local_file.name))

Loading

0 comments on commit 720c7a1

Please sign in to comment.