Skip to main content

Brahm Centre internal utility functions

Project description

Brahm Centre Utils

Internal helper package for Brahm Centre engineering teams. It provides ready-to-use utilities for Salesforce, SharePoint, and data-cleaning workflows so that automation projects share one consistent toolkit.

Installation

pip install brahm-centre-utils

# or for local development with tests
pip install -e .[dev]

The package depends on pandas, requests, simple-salesforce, and openpyxl. Installing in a virtual environment is strongly recommended.

Configuration

Salesforce credentials

Store your Salesforce secrets as environment variables or load them from a .env file before passing them into simple_salesforce.Salesforce.

SF_USERNAME=...  # required
SF_PASSWORD=...  # required
SF_TOKEN=...     # security token
SF_DOMAIN=...    # e.g. 'login' or 'test'

SharePoint / Microsoft Graph

Create an Azure AD app registration that can access the SharePoint site and provide the following values. They are passed directly into SharePointClient.

SHAREPOINT_TENANT_ID=...
SHAREPOINT_CLIENT_ID=...
SHAREPOINT_CLIENT_SECRET=...
SHAREPOINT_SITE_HOSTNAME=brahmcentre.sharepoint.com
SHAREPOINT_SITE_PATH=/sites/BrahmCentre

Usage

Quick import

from brahm_centre_utils import (
    query_salesforce,
    sf_create_from_df,
    send_email_via_rest_api,
    SharePointClient,
)

Salesforce helpers

from simple_salesforce import Salesforce
from brahm_centre_utils import (
    query_salesforce,
    get_picklist_options,
    sf_create_from_df,
    sf_update_from_df,
    sf_delete_from_df,
    sf_upsert_from_df,
    send_email_via_rest_api,
)

sf = Salesforce(
    username=os.getenv("SF_USERNAME"),
    password=os.getenv("SF_PASSWORD"),
    security_token=os.getenv("SF_TOKEN"),
    domain=os.getenv("SF_DOMAIN", "login"),
)

# Query data as a flattened DataFrame
accounts = query_salesforce("SELECT Id, Name, Industry FROM Account", sf)

# Inspect metadata
industries = get_picklist_options("Account", "Industry", sf)

# Bulk create/update/delete from pandas DataFrames
sf_create_from_df(sf, new_records_df, "CustomObject__c")
sf_update_from_df(sf, updates_df, "CustomObject__c")  # requires Id column
sf_delete_from_df(sf, deletions_df, "CustomObject__c")
sf_upsert_from_df(sf, upserts_df, "CustomObject__c", external_id_field="LegacyId__c")

# Send an email via REST API
send_email_via_rest_api(
    sf,
    ["recipient@brahmcentre.com"],
    subject="Automation Update",
    body="<p>Your files have been processed.</p>",
)

Other Salesforce utilities:

  • get_Id_from_SF_URL(url) – split a Salesforce record Id from a URL.
  • send_coa_certificates(query, sf) – toggle certificate flags for Campaign Members.

SharePoint helpers

from brahm_centre_utils import SharePointClient

client = SharePointClient(
    tenant_id=os.getenv("SHAREPOINT_TENANT_ID"),
    client_id=os.getenv("SHAREPOINT_CLIENT_ID"),
    client_secret=os.getenv("SHAREPOINT_CLIENT_SECRET"),
    site_hostname=os.getenv("SHAREPOINT_SITE_HOSTNAME"),
    site_path=os.getenv("SHAREPOINT_SITE_PATH"),
)

# Upload a bytes buffer or file
client.upload_file("Shared Documents/Reports/output.xlsx", my_bytes)

# List items sorted by last modified date
files = client.list_files_in_folder("Shared Documents/Reports")

# Download structured data
df = client.get_csv_as_df("Shared Documents/Data/latest.csv")
sheet = client.get_xlsx_as_df("Shared Documents/Data/data.xlsx", sheet_name="Sheet1")

# Other helpers
client.create_folder("Shared Documents/Archive/2024")
client.move_file(
    "Shared Documents/Reports/output.xlsx",
    "Shared Documents/Archive/output_2024.xlsx",
)
client.get_list_dataframe("Daily Updates")
client.find_item_by_name("output.xlsx")

Data-cleaning helpers

from brahm_centre_utils import find_contact_duplicates

deduped = find_contact_duplicates(df, name_column="Full Name")

Testing

pytest

The development extras include pytest so the above command works after pip install -e .[dev].

Publishing (internal process)

  1. Update the version number in setup.py.
  2. Build distributions:
    python setup.py sdist bdist_wheel
    
  3. Upload to PyPI (or the internal index):
    twine upload dist/*
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brahm_centre_utils-1.0.6.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brahm_centre_utils-1.0.6-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file brahm_centre_utils-1.0.6.tar.gz.

File metadata

  • Download URL: brahm_centre_utils-1.0.6.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for brahm_centre_utils-1.0.6.tar.gz
Algorithm Hash digest
SHA256 4f242807ed2e17facaf81a5e5736b67d6c7314bf5091b62e06e39f7fcd8d760f
MD5 b59fd1c47b2c18d8dff2fd4a7e44d140
BLAKE2b-256 5df3587982d973b06916d07a6a46903af055a566d3e1bee58a8247a37428b6aa

See more details on using hashes here.

File details

Details for the file brahm_centre_utils-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for brahm_centre_utils-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 760740a509545c695262f37116439f48451aa0a71e91576860238bd98ee25311
MD5 57c39290c7b132d0ddac8f9466668620
BLAKE2b-256 bbed7b24ea74a1a065ed5dde1b0af6f45ebb57b782b6662a8657fa97f2f55416

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page