Compare commits

..

14 Commits

Author SHA1 Message Date
bimgeek 650122e041 final touches (hopefully) 2025-08-19 20:20:15 +03:00
bimgeek 7c6e7c52c1 early return ifc quantities 2025-08-19 20:16:45 +03:00
bimgeek 6430e81995 function call elimination 2025-08-19 20:12:22 +03:00
bimgeek dc0eb24d9c move unit mapping to module level 2025-08-19 20:08:22 +03:00
bimgeek 484f31dbfa take only what you need 2025-08-19 19:33:25 +03:00
bimgeek 661c7c70a8 simplify get quantities 2025-08-18 22:30:49 +03:00
bimgeek 061ddf33fd cache by field name 2025-08-18 22:24:12 +03:00
bimgeek 94a2d86900 comm cleanup 2025-08-18 22:05:59 +03:00
bimgeek 8e44310f91 quantity extraction py 2025-08-18 22:04:17 +03:00
bimgeek a56085b1b4 quantity field unit cache 2025-08-18 21:55:30 +03:00
bimgeek 4f20315582 module cache for project units 2025-08-18 21:45:30 +03:00
bimgeek 452e764b6a add units first pass 2025-08-18 21:14:02 +03:00
bimgeek 86ac9a2b91 cleanup 2025-08-18 17:20:55 +03:00
bimgeek af39a52f42 add qtos 2025-08-18 17:10:12 +03:00
139 changed files with 1856 additions and 6513 deletions
+10 -52
View File
@@ -9,8 +9,8 @@ on:
- "main"
jobs:
test-internal: # Run integration tests against the internal server image
name: Test (internal)
test:
name: test
runs-on: ubuntu-latest
strategy:
matrix:
@@ -20,55 +20,6 @@ jobs:
- "3.12"
- "3.13"
steps:
- uses: actions/checkout@v4
- name: Install uv and set the python version
uses: astral-sh/setup-uv@v5
with:
python-version: ${{ matrix.python-version }}
enable-cache: true
cache-dependency-glob: "uv.lock"
- name: Install the project
run: uv sync --all-extras --dev
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: "ghcr.io"
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Run Speckle Server
run: docker compose --file docker-compose-internal.yml up --detach --wait
- name: Run tests
run: uv run pytest --cov --cov-report xml:reports/coverage.xml --junitxml=reports/test-results.xml
- uses: codecov/codecov-action@v5
if: matrix.python-version == 3.12
with:
fail_ci_if_error: true # optional (default = false)
files: ./reports/test-results.xml # optional
token: ${{ secrets.CODECOV_TOKEN }}
- name: Minimize uv cache
run: uv cache prune --ci
test-public: # Run integration tests against the public server image
name: Test (public)
runs-on: ubuntu-latest
env:
IS_PUBLIC: "true"
strategy:
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"
- "3.13"
steps:
- uses: actions/checkout@v4
@@ -91,10 +42,17 @@ jobs:
run: uv run pre-commit run --all-files
- name: Run Speckle Server
run: docker compose --file docker-compose.yml up --detach --wait
run: docker compose up --detach --wait
- name: Run tests
run: uv run pytest --cov --cov-report xml:reports/coverage.xml --junitxml=reports/test-results.xml
- uses: codecov/codecov-action@v5
if: matrix.python-version == 3.13
with:
fail_ci_if_error: true # optional (default = false)
files: ./reports/test-results.xml # optional
token: ${{ secrets.CODECOV_TOKEN }}
- name: Minimize uv cache
run: uv cache prune --ci
-1
View File
@@ -1 +0,0 @@
words = ["specklepy"]
-115
View File
@@ -1,115 +0,0 @@
name: "speckle-server"
services:
####
# Speckle Server dependencies
#######
postgres:
image: "postgres:16.4-alpine3.20@sha256:d898b0b78a2627cb4ee63464a14efc9d296884f1b28c841b0ab7d7c42f1fffdf"
restart: always
environment:
POSTGRES_DB: speckle
POSTGRES_USER: speckle
POSTGRES_PASSWORD: speckle
volumes:
- ./.volumes/postgres-data:/var/lib/postgresql/data/
healthcheck:
# the -U user has to match the POSTGRES_USER value
test: ["CMD-SHELL", "pg_isready -U speckle"]
interval: 5s
timeout: 5s
retries: 30
redis:
image: "valkey/valkey:8.1-alpine@sha256:0d27f0bca0249f61d060029a6aaf2e16b2c417d68d02a508e1dfb763fa2948b4"
restart: always
volumes:
- ./.volumes/redis-data:/data
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
interval: 5s
timeout: 5s
retries: 30
minio:
image: "minio/minio:RELEASE.2023-10-25T06-33-25Z"
command: server /data --console-address ":9001"
restart: always
volumes:
- ./.volumes/minio-data:/data
ports:
- '127.0.0.1:9000:9000'
- '127.0.0.1:9001:9001'
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -o /dev/null http://127.0.0.1:9000/minio/index.html",
]
interval: 5s
timeout: 30s
retries: 30
start_period: 10s
speckle-server:
image: ghcr.io/specklesystems/speckle-server:latest
restart: always
healthcheck:
test:
- CMD
- /nodejs/bin/node
- -e
- "try { require('node:http').request({headers: {'Content-Type': 'application/json'}, port:3000, hostname:'127.0.0.1', path:'/readiness', method: 'GET', timeout: 2000 }, (res) => { body = ''; res.on('data', (chunk) => {body += chunk;}); res.on('end', () => {process.exit(Number(res.statusCode != 200 || body.toLowerCase().includes('error')));}); }).end(); } catch { process.exit(1); }"
interval: 10s
timeout: 10s
retries: 3
start_period: 90s
ports:
- "0.0.0.0:3000:3000"
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
minio:
condition: service_healthy
environment:
# TODO: Change this to the URL of the speckle server, as accessed from the network
CANONICAL_URL: "http://127.0.0.1:8080"
SPECKLE_AUTOMATE_URL: "http://127.0.0.1:3030"
FRONTEND_ORIGIN: "http://127.0.0.1:8081"
# TODO: Change thvolumes:
REDIS_URL: "redis://redis"
S3_ENDPOINT: "http://minio:9000"
S3_PUBLIC_ENDPOINT: "http://127.0.0.1:9000"
S3_ACCESS_KEY: "minioadmin"
S3_SECRET_KEY: "minioadmin"
S3_BUCKET: "speckle-server"
S3_CREATE_BUCKET: "true"
FILE_SIZE_LIMIT_MB: 100
MAX_PROJECT_MODELS_PER_PAGE: 500
# TODO: Change this to a unique secret for this server
SESSION_SECRET: "TODO:ReplaceWithLongString"
STRATEGY_LOCAL: "true"
POSTGRES_URL: 'postgres://speckle:speckle@postgres:5432/speckle'
ENABLE_MP: "false"
LOG_PRETTY: "true"
FF_NEXT_GEN_FILE_IMPORTER_ENABLED: "true"
FF_LARGE_FILE_IMPORTS_ENABLED: "true"
networks:
default:
name: speckle-server
volumes:
postgres-data:
redis-data:
minio-data:
+7 -14
View File
@@ -1,3 +1,4 @@
version: "3.9"
name: "speckle-server"
services:
@@ -12,7 +13,7 @@ services:
POSTGRES_USER: speckle
POSTGRES_PASSWORD: speckle
volumes:
- ./.volumes/postgres-data:/var/lib/postgresql/data/
- postgres-data:/var/lib/postgresql/data/
healthcheck:
# the -U user has to match the POSTGRES_USER value
test: ["CMD-SHELL", "pg_isready -U speckle"]
@@ -21,10 +22,10 @@ services:
retries: 30
redis:
image: "valkey/valkey:8.1-alpine@sha256:0d27f0bca0249f61d060029a6aaf2e16b2c417d68d02a508e1dfb763fa2948b4"
image: "redis:6.0-alpine"
restart: always
volumes:
- ./.volumes/redis-data:/data
- redis-data:/data
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
interval: 5s
@@ -36,10 +37,7 @@ services:
command: server /data --console-address ":9001"
restart: always
volumes:
- ./.volumes/minio-data:/data
ports:
- '127.0.0.1:9000:9000'
- '127.0.0.1:9001:9001'
- minio-data:/data
healthcheck:
test:
[
@@ -59,7 +57,7 @@ services:
- CMD
- /nodejs/bin/node
- -e
- "try { require('node:http').request({headers: {'Content-Type': 'application/json'}, port:3000, hostname:'127.0.0.1', path:'/readiness', method: 'GET', timeout: 2000 }, (res) => { body = ''; res.on('data', (chunk) => {body += chunk;}); res.on('end', () => {process.exit(Number(res.statusCode != 200 || body.toLowerCase().includes('error')));}); }).end(); } catch { process.exit(1); }"
- "try { require('node:http').request({headers: {'Content-Type': 'application/json'}, port:3000, hostname:'127.0.0.1', path:'/readiness', method: 'GET', timeout: 2000 }, (res) => { body = ''; res.on('data', (chunk) => {body += chunk;}); res.on('end', () => {process.exit(res.statusCode != 200 || body.toLowerCase().includes('error'));}); }).end(); } catch { process.exit(1); }"
interval: 10s
timeout: 10s
retries: 3
@@ -83,7 +81,6 @@ services:
REDIS_URL: "redis://redis"
S3_ENDPOINT: "http://minio:9000"
S3_PUBLIC_ENDPOINT: "http://127.0.0.1:9000"
S3_ACCESS_KEY: "minioadmin"
S3_SECRET_KEY: "minioadmin"
S3_BUCKET: "speckle-server"
@@ -96,6 +93,7 @@ services:
SESSION_SECRET: "TODO:ReplaceWithLongString"
STRATEGY_LOCAL: "true"
DEBUG: "speckle:*"
POSTGRES_URL: "postgres"
POSTGRES_USER: "speckle"
@@ -103,11 +101,6 @@ services:
POSTGRES_DB: "speckle"
ENABLE_MP: "false"
LOG_PRETTY: "true"
FF_NEXT_GEN_FILE_IMPORTER_ENABLED: "true"
FF_LARGE_FILE_IMPORTS_ENABLED: "true"
networks:
default:
name: speckle-server
Binary file not shown.

Before

Width:  |  Height:  |  Size: 386 B

-50
View File
@@ -1,50 +0,0 @@
# specklepy API Reference
> The Python SDK for Speckle - Build powerful AEC data workflows
**specklepy** is the Python SDK for Speckle, enabling you to interact with Speckle Server, send and receive geometry, and build custom integrations for the AEC industry.
## What is specklepy?
specklepy is a comprehensive Python library that provides:
* **Object-based data exchange** - Send and receive geometry and BIM data without files
* **GraphQL API client** - Full access to Speckle Server's API
* **Extensible object model** - Create custom objects that inherit from `Base`
* **Multiple transport options** - Store data locally (SQLite), in-memory, or on Speckle Server
* **Geometry support** - Rich geometric primitives (Point, Line, Mesh, etc.)
## Speckle Automate
Speckle Automate is a fully fledged CI/CD platform designed to run custom code on Speckle models whenever a new version is available.
As a software developer, you can develop Functions that others in your team consume in Automations. From creating reports to running code compliance checks to wind simulations, there is no limit to what you can do with Automate.
## Installation
Install specklepy using pip:
```bash
pip install specklepy
```
## Quick Example
```python
from specklepy.api.client import SpeckleClient
from specklepy.api.credentials import get_default_account
from specklepy.objects.geometry import Point
# Authenticate
client = SpeckleClient(host="https://app.speckle.systems")
account = get_default_account()
client.authenticate_with_account(account)
# Create geometry
point = Point(x=10, y=20, z=5)
```
## Getting Help
- **Community Forum**: [speckle.community](https://speckle.community/c/help/developers)
- **GitHub Issues**: [github.com/specklesystems/specklepy](https://github.com/specklesystems/specklepy/issues)
@@ -1 +0,0 @@
::: speckle_automate.AutomationContext
-3
View File
@@ -1,3 +0,0 @@
::: speckle_automate.runner.execute_automate_function
::: speckle_automate.runner.run_function
-11
View File
@@ -1,11 +0,0 @@
::: speckle_automate.AutomateBase
::: speckle_automate.AutomationRunData
::: speckle_automate.AutomationResult
::: speckle_automate.ResultCase
::: speckle_automate.AutomationStatus
::: speckle_automate.ObjectResultLevel
-1
View File
@@ -1 +0,0 @@
::: specklepy.api.client.SpeckleClient
-5
View File
@@ -1,5 +0,0 @@
::: specklepy.api.credentials.Account
::: specklepy.api.credentials.UserInfo
::: specklepy.api.credentials.StreamWrapper
-7
View File
@@ -1,7 +0,0 @@
::: specklepy.api.operations.send
::: specklepy.api.operations.receive
::: specklepy.api.operations.serialize
::: specklepy.api.operations.deserialize
@@ -1 +0,0 @@
::: specklepy.api.resources.ActiveUserResource
@@ -1 +0,0 @@
::: specklepy.api.resources.FileImportResource
@@ -1 +0,0 @@
::: specklepy.api.resources.ModelResource
@@ -1 +0,0 @@
::: specklepy.api.resources.OtherUserResource
@@ -1 +0,0 @@
::: specklepy.api.resources.ProjectInviteResource
@@ -1 +0,0 @@
::: specklepy.api.resources.ProjectResource
@@ -1 +0,0 @@
::: specklepy.api.resources.ServerResource
@@ -1 +0,0 @@
::: specklepy.api.resources.SubscriptionResource
@@ -1 +0,0 @@
::: specklepy.api.resources.VersionResource
@@ -1 +0,0 @@
::: specklepy.api.resources.WorkspaceResource
-1
View File
@@ -1 +0,0 @@
::: specklepy.core.api.enums.ProjectVisibility
-11
View File
@@ -1,11 +0,0 @@
::: specklepy.core.api.inputs.ProjectCreateInput
::: specklepy.core.api.inputs.ProjectUpdateInput
::: specklepy.core.api.inputs.CreateModelInput
::: specklepy.core.api.inputs.UpdateModelInput
::: specklepy.core.api.inputs.CreateVersionInput
::: specklepy.core.api.inputs.UpdateVersionInput
-13
View File
@@ -1,13 +0,0 @@
::: specklepy.core.api.models.User
::: specklepy.core.api.models.LimitedUser
::: specklepy.core.api.models.ServerInfo
::: specklepy.core.api.models.Project
::: specklepy.core.api.models.Model
::: specklepy.core.api.models.Version
::: specklepy.core.api.models.current.Workspace
-7
View File
@@ -1,7 +0,0 @@
::: specklepy.logging.exceptions.SpeckleException
::: specklepy.logging.exceptions.GraphQLException
::: specklepy.logging.exceptions.SerializationException
::: specklepy.logging.exceptions.SpeckleWarning
-3
View File
@@ -1,3 +0,0 @@
::: specklepy.objects.Base
::: specklepy.objects.base.DataChunk
-5
View File
@@ -1,5 +0,0 @@
::: specklepy.objects.DataObject
::: specklepy.objects.QgisObject
::: specklepy.objects.BlenderObject
-1
View File
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Arc
-1
View File
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Box
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Circle
@@ -1 +0,0 @@
::: specklepy.objects.geometry.ControlPoint
-1
View File
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Curve
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Ellipse
-1
View File
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Line
-1
View File
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Mesh
-1
View File
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Plane
-1
View File
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Point
@@ -1 +0,0 @@
::: specklepy.objects.geometry.PointCloud
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Polycurve
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Polyline
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Spiral
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Surface
@@ -1 +0,0 @@
::: specklepy.objects.geometry.Vector
@@ -1,7 +0,0 @@
::: specklepy.objects.graph_traversal.GraphTraversal
::: specklepy.objects.graph_traversal.TraversalContext
::: specklepy.objects.graph_traversal.TraversalRule
::: specklepy.objects.graph_traversal.DefaultRule
@@ -1 +0,0 @@
::: specklepy.objects.models.collections.collection.Collection
@@ -1 +0,0 @@
::: specklepy.objects.other.RenderMaterial
@@ -1 +0,0 @@
::: specklepy.objects.primitive.Interval
@@ -1 +0,0 @@
::: specklepy.objects.proxies.ColorProxy
@@ -1 +0,0 @@
::: specklepy.objects.proxies.GroupProxy
@@ -1 +0,0 @@
::: specklepy.objects.proxies.InstanceDefinitionProxy
@@ -1 +0,0 @@
::: specklepy.objects.proxies.InstanceProxy
@@ -1 +0,0 @@
::: specklepy.objects.proxies.LevelProxy
@@ -1 +0,0 @@
::: specklepy.objects.proxies.RenderMaterialProxy
@@ -1 +0,0 @@
::: specklepy.serialization.base_object_serializer.BaseObjectSerializer
-7
View File
@@ -1,7 +0,0 @@
::: specklepy.transports.abstract_transport.AbstractTransport
::: specklepy.transports.memory.MemoryTransport
::: specklepy.transports.sqlite.SQLiteTransport
::: specklepy.transports.server.ServerTransport
-304
View File
@@ -1,304 +0,0 @@
.md-content h1 {
font-size: 1.125rem;
font-weight: 700;
color: #111827;
letter-spacing: -0.025em;
line-height: 1.2;
}
@media (min-width: 640px) {
.md-content h1 {
font-size: 1.25rem;
}
}
[data-md-color-scheme="slate"] .md-content h1 {
color: #e5e7eb;
}
.md-content h2 {
font-size: 1rem;
font-weight: 700;
color: #111827;
letter-spacing: -0.025em;
margin-top: 2rem;
}
@media (min-width: 640px) {
.md-content h2 {
font-size: 1.125rem;
}
}
[data-md-color-scheme="slate"] .md-content h2 {
color: #e5e7eb;
}
.md-content h3 {
font-size: 0.9375rem;
font-weight: 600;
color: #1f2937;
margin-top: 1.5rem;
}
@media (min-width: 640px) {
.md-content h3 {
font-size: 1rem;
}
}
[data-md-color-scheme="slate"] .md-content h3 {
color: #d1d5db;
}
.md-content p,
.md-content li {
font-size: 0.875rem;
line-height: 1.6;
color: #374151;
margin-top: 0.5rem;
}
[data-md-color-scheme="slate"] .md-content p,
[data-md-color-scheme="slate"] .md-content li {
color: #d1d5db;
}
.doc.doc-object-name {
font-size: 1.125rem;
font-weight: 700;
color: #111827;
}
[data-md-color-scheme="slate"] .doc.doc-object-name {
color: #e5e7eb;
}
.doc.doc-heading {
font-size: 0.9375rem;
font-weight: 600;
color: #1f2937;
}
[data-md-color-scheme="slate"] .doc.doc-heading {
color: #d1d5db;
}
.md-content code {
font-size: 0.75rem;
color: #dc2626;
background-color: #f3f4f6;
padding: 0.125rem 0.375rem;
border-radius: 0.25rem;
}
[data-md-color-scheme="slate"] .md-content code {
color: #fca5a5;
background-color: #1f2937;
}
.md-content pre code {
font-size: 0.75rem;
color: inherit;
background-color: transparent;
padding: 0;
}
.md-content a {
color: #2563eb;
text-decoration: none;
}
.md-content a:hover {
color: #1d4ed8;
text-decoration: underline;
}
[data-md-color-scheme="slate"] .md-content a {
color: #60a5fa;
}
[data-md-color-scheme="slate"] .md-content a:hover {
color: #93c5fd;
}
[data-md-color-scheme="slate"] {
--md-default-bg-color: #000000;
--md-default-fg-color: #ffffff;
--md-code-bg-color: #0a0a0a;
--md-code-fg-color: #ffffff;
}
[data-md-color-scheme="slate"] .md-header {
background-color: #000000;
}
[data-md-color-scheme="slate"] .md-tabs {
background-color: #000000;
}
[data-md-color-scheme="slate"] .md-footer {
background-color: #000000;
}
[data-md-color-scheme="slate"] .md-sidebar {
background-color: #000000;
}
[data-md-color-scheme="slate"] .md-nav {
background-color: #000000;
}
.highlight pre,
.highlight code {
background-color: #f6f8fa !important;
color: #24292e !important;
}
.highlight .k,
.highlight .kc,
.highlight .kd,
.highlight .kn,
.highlight .kp,
.highlight .kr,
.highlight .kt {
color: #d73a49 !important;
}
.highlight .s,
.highlight .s1,
.highlight .s2,
.highlight .sb,
.highlight .sc,
.highlight .sd,
.highlight .se,
.highlight .sh,
.highlight .si,
.highlight .sx,
.highlight .sr,
.highlight .ss {
color: #032f62 !important;
}
.highlight .nf,
.highlight .fm,
.highlight .nc {
color: #6f42c1 !important;
}
.highlight .m,
.highlight .mf,
.highlight .mh,
.highlight .mi,
.highlight .mo {
color: #005cc5 !important;
}
.highlight .c,
.highlight .c1,
.highlight .cm,
.highlight .cp,
.highlight .cs {
color: #6a737d !important;
font-style: italic;
}
.highlight .o,
.highlight .ow {
color: #d73a49 !important;
}
.highlight .n,
.highlight .na,
.highlight .nb,
.highlight .nd,
.highlight .ni,
.highlight .nl,
.highlight .nn,
.highlight .nx,
.highlight .py,
.highlight .nt,
.highlight .nv,
.highlight .bp,
.highlight .vc,
.highlight .vg,
.highlight .vi {
color: #e36209 !important;
}
[data-md-color-scheme="slate"] .highlight pre,
[data-md-color-scheme="slate"] .highlight code {
background-color: #0d1117 !important;
color: #e6edf3 !important;
}
[data-md-color-scheme="slate"] .highlight .k,
[data-md-color-scheme="slate"] .highlight .kc,
[data-md-color-scheme="slate"] .highlight .kd,
[data-md-color-scheme="slate"] .highlight .kn,
[data-md-color-scheme="slate"] .highlight .kp,
[data-md-color-scheme="slate"] .highlight .kr,
[data-md-color-scheme="slate"] .highlight .kt {
color: #ff7b72 !important;
}
[data-md-color-scheme="slate"] .highlight .s,
[data-md-color-scheme="slate"] .highlight .s1,
[data-md-color-scheme="slate"] .highlight .s2,
[data-md-color-scheme="slate"] .highlight .sb,
[data-md-color-scheme="slate"] .highlight .sc,
[data-md-color-scheme="slate"] .highlight .sd,
[data-md-color-scheme="slate"] .highlight .se,
[data-md-color-scheme="slate"] .highlight .sh,
[data-md-color-scheme="slate"] .highlight .si,
[data-md-color-scheme="slate"] .highlight .sx,
[data-md-color-scheme="slate"] .highlight .sr,
[data-md-color-scheme="slate"] .highlight .ss {
color: #a5d6ff !important;
}
[data-md-color-scheme="slate"] .highlight .nf,
[data-md-color-scheme="slate"] .highlight .fm,
[data-md-color-scheme="slate"] .highlight .nc {
color: #d2a8ff !important;
}
[data-md-color-scheme="slate"] .highlight .m,
[data-md-color-scheme="slate"] .highlight .mf,
[data-md-color-scheme="slate"] .highlight .mh,
[data-md-color-scheme="slate"] .highlight .mi,
[data-md-color-scheme="slate"] .highlight .mo {
color: #79c0ff !important;
}
[data-md-color-scheme="slate"] .highlight .c,
[data-md-color-scheme="slate"] .highlight .c1,
[data-md-color-scheme="slate"] .highlight .cm,
[data-md-color-scheme="slate"] .highlight .cp,
[data-md-color-scheme="slate"] .highlight .cs {
color: #8b949e !important;
font-style: italic;
}
[data-md-color-scheme="slate"] .highlight .o,
[data-md-color-scheme="slate"] .highlight .ow {
color: #ff7b72 !important;
}
[data-md-color-scheme="slate"] .highlight .n,
[data-md-color-scheme="slate"] .highlight .na,
[data-md-color-scheme="slate"] .highlight .nb,
[data-md-color-scheme="slate"] .highlight .nd,
[data-md-color-scheme="slate"] .highlight .ni,
[data-md-color-scheme="slate"] .highlight .nl,
[data-md-color-scheme="slate"] .highlight .nn,
[data-md-color-scheme="slate"] .highlight .nx,
[data-md-color-scheme="slate"] .highlight .py,
[data-md-color-scheme="slate"] .highlight .nt,
[data-md-color-scheme="slate"] .highlight .nv,
[data-md-color-scheme="slate"] .highlight .bp,
[data-md-color-scheme="slate"] .highlight .vc,
[data-md-color-scheme="slate"] .highlight .vg,
[data-md-color-scheme="slate"] .highlight .vi {
color: #ffa657 !important;
}
-27
View File
@@ -1,27 +0,0 @@
[tools]
python = "3.13.7"
uv = "0.9.11"
[settings]
experimental = true
python.uv_venv_auto = true
[tasks.install]
run= "uv sync --all-extras --all-groups"
[tasks.install_docs]
run= "uv sync --group docs"
[tasks.build_docs]
description = "Build static docs "
run = "uv run mkdocs build"
depends = ['install_docs']
[tasks.test]
run = "uv run pytest"
[env]
IS_PUBLIC = "false"
-130
View File
@@ -1,130 +0,0 @@
site_name: specklepy Docs
theme:
name: material
font:
text: Inter
favicon: assets/speckle_logo.png
logo: assets/speckle_logo.png
features:
- navigation.tabs
palette:
# Palette toggle for light mode
- scheme: default
primary: white
toggle:
icon: material/weather-night
name: Switch to dark mode
# Palette toggle for dark mode
- scheme: slate
primary: black
logo: assets/logo_white.png
toggle:
icon: material/weather-sunny
name: Switch to light mode
markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
line_spans: __span
pygments_lang_class: true
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.superfences
extra_css:
- stylesheets/extra.css
plugins:
- search
- mkdocstrings:
handlers:
python:
paths: [src]
options:
parameter_headings: false
members_order: source
separate_signature: true
filters: ["!^_"] #Ignore _ prefixed properties
docstring_options:
ignore_init_summary: true
merge_init_into_class: true
show_signature_annotations: true
signature_crossrefs: true
show_if_no_docstring: true
show_labels: true
show_source: true
show_symbol_type_heading: true
show_symbol_type_toc: true
show_bases: false
heading_level: 3
inventories:
- url: https://docs.python.org/3/objects.inv
domains: [py, std]
nav:
- Home: index.md
- specklepy SDK:
- API:
- Client: specklepy/api/client.md
- Credentials: specklepy/api/credentials.md
- Operations: specklepy/api/operations.md
- Resources:
- ActiveUserResource: specklepy/api/resources/ActiveUserResource.md
- FileImportResource: specklepy/api/resources/FileImportResource.md
- ModelResource: specklepy/api/resources/ModelResource.md
- OtherUserResource: specklepy/api/resources/OtherUserResource.md
- ProjectInviteResource: specklepy/api/resources/ProjectInviteResource.md
- ProjectResource: specklepy/api/resources/ProjectResource.md
- ServerResource: specklepy/api/resources/ServerResource.md
- SubscriptionResource: specklepy/api/resources/SubscriptionResource.md
- VersionResource: specklepy/api/resources/VersionResource.md
- WorkspaceResource: specklepy/api/resources/WorkspaceResource.md
- Objects:
- Base: specklepy/objects/base.md
- Data Objects: specklepy/objects/data_objects.md
- Geometry:
- Arc: specklepy/objects/geometry/Arc.md
- Box: specklepy/objects/geometry/Box.md
- Circle: specklepy/objects/geometry/Circle.md
- ControlPoint: specklepy/objects/geometry/ControlPoint.md
- Curve: specklepy/objects/geometry/Curve.md
- Ellipse: specklepy/objects/geometry/Ellipse.md
- Line: specklepy/objects/geometry/Line.md
- Mesh: specklepy/objects/geometry/Mesh.md
- Plane: specklepy/objects/geometry/Plane.md
- Point: specklepy/objects/geometry/Point.md
- PointCloud: specklepy/objects/geometry/PointCloud.md
- Polycurve: specklepy/objects/geometry/Polycurve.md
- Polyline: specklepy/objects/geometry/Polyline.md
- Spiral: specklepy/objects/geometry/Spiral.md
- Surface: specklepy/objects/geometry/Surface.md
- Vector: specklepy/objects/geometry/Vector.md
- Primitives:
- Interval: specklepy/objects/primitives/interval.md
- Other:
- RenderMaterial: specklepy/objects/other/render_material.md
- Collection: specklepy/objects/other/collection.md
- Proxies:
- ColorProxy: specklepy/objects/proxies/ColorProxy.md
- GroupProxy: specklepy/objects/proxies/GroupProxy.md
- InstanceProxy: specklepy/objects/proxies/InstanceProxy.md
- InstanceDefinitionProxy: specklepy/objects/proxies/InstanceDefinitionProxy.md
- LevelProxy: specklepy/objects/proxies/LevelProxy.md
- RenderMaterialProxy: specklepy/objects/proxies/RenderMaterialProxy.md
- Graph Traversal: specklepy/objects/graph_traversal/traversal.md
- Transports: specklepy/transports/transports.md
- Serialization: specklepy/serialization/serializer.md
- Core API:
- Models: specklepy/core/api/models/models.md
- Inputs: specklepy/core/api/inputs/inputs.md
- Enums: specklepy/core/api/enums.md
- Logging:
- Exceptions: specklepy/logging/exceptions.md
- Speckle Automate:
- AutomationContext: speckle_automate/automation_context.md
- Runner: speckle_automate/runner.md
- Schema: speckle_automate/schema.md
-3
View File
@@ -1,3 +0,0 @@
[build]
command = "mise run build_docs"
publish = "site"
+3 -11
View File
@@ -11,7 +11,7 @@ dependencies = [
"appdirs>=1.4.4",
"attrs>=24.3.0",
"deprecated>=1.2.15",
"gql[requests,websockets]>=3.5.0,<4.0.0",
"gql[requests,websockets]>=3.5.0",
"httpx>=0.28.1",
"pydantic>=2.10.5",
"pydantic-settings>=2.7.1",
@@ -19,10 +19,9 @@ dependencies = [
]
[project.optional-dependencies]
speckleifc = ["ifcopenshell>=0.8.3.post2"]
speckleifc = ["ifcopenshell>=0.8.2"]
[dependency-groups]
dev = [
"commitizen>=4.1.0",
"devtools>=0.12.2",
@@ -33,18 +32,11 @@ dev = [
"pytest-asyncio>=0.25.2",
"pytest-cov>=6.0.0",
"pytest-ordering>=0.6",
"pytest_httpserver >=1.1.3",
"ruff==0.9.2",
"ruff>=0.9.2",
"types-deprecated>=1.2.15.20241117",
"types-requests>=2.32.0.20241016",
"types-ujson>=5.10.0.20240515",
]
docs = [
"mkdocs>=1.6.1",
"mkdocs-material>=9.6.5",
"mkdocstrings>=0.28.1",
"mkdocstrings-python>=1.15.0",
]
[project.urls]
repository = "https://github.com/specklesystems/specklepy"
+27 -66
View File
@@ -19,12 +19,8 @@ from speckle_automate.schema import (
from specklepy.api import operations
from specklepy.api.client import SpeckleClient
from specklepy.core.api.inputs.model_inputs import CreateModelInput
from specklepy.core.api.inputs.version_inputs import (
CreateVersionInput,
MarkReceivedVersionInput,
)
from specklepy.core.api.inputs.version_inputs import CreateVersionInput
from specklepy.core.api.models.current import Model, Version
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException
from specklepy.objects.base import Base
from specklepy.transports.memory import MemoryTransport
@@ -70,7 +66,6 @@ class AutomationContext:
if isinstance(automation_run_data, AutomationRunData)
else AutomationRunData.model_validate_json(automation_run_data)
)
metrics.set_host_app("automate")
speckle_client = SpeckleClient(
automation_run_data.speckle_server_url,
automation_run_data.speckle_server_url.startswith("https"),
@@ -105,7 +100,6 @@ class AutomationContext:
"""Receive the Speckle project version that triggered this automation run."""
# TODO: this is a quick hack to keep implementation consistency.
# Move to proper receive many versions
project_id = self.automation_run_data.project_id
version_id = self.automation_run_data.triggers[0].payload.version_id
try:
version = self.speckle_client.version.get(
@@ -115,7 +109,7 @@ class AutomationContext:
raise ValueError(
f"""Could not receive specified version.
Is your environment configured correctly?
project_id: {project_id}
project_id: {self.automation_run_data.project_id}
model_id: {self.automation_run_data.triggers[0].payload.model_id}
version_id: {self.automation_run_data.triggers[0].payload.version_id}
"""
@@ -130,13 +124,6 @@ class AutomationContext:
base = operations.receive(
version.referenced_object, self._server_transport, self._memory_transport
)
self.speckle_client.version.received(
MarkReceivedVersionInput(
version_id=version_id,
project_id=project_id,
source_application="automate_function",
)
)
# self._closure_tree = base["__closure"]
print(
f"It took {self.elapsed():.2f} seconds to receive",
@@ -258,30 +245,30 @@ class AutomationContext:
"""
)
if self.run_status in [AutomationStatus.SUCCEEDED, AutomationStatus.FAILED]:
results_dict = self._automation_result.model_dump(by_alias=True)
results = {
"version": 3,
object_results = {
"version": 2,
"values": {
"objectResults": results_dict["objectResults"],
"versionResult": results_dict["versionResult"],
"objectResults": self._automation_result.model_dump(by_alias=True)[
"objectResults"
],
"blobIds": self._automation_result.blobs,
},
}
else:
results = None
object_results = None
params = {
"projectId": self.automation_run_data.project_id,
"functionRunId": self.automation_run_data.function_run_id,
"status": self.run_status.value,
"statusMessage": self._automation_result.status_message,
"results": results,
"results": object_results,
"contextView": self._automation_result.result_view,
}
print(f"Reporting run status with content: {params}")
self.speckle_client.httpclient.execute(query, params)
def store_file_result(self, file_path: Union[Path, str]) -> str:
def store_file_result(self, file_path: Union[Path, str]) -> None:
"""Save a file attached to the project of this automation."""
path_obj = (
Path(file_path).resolve() if isinstance(file_path, str) else file_path
@@ -323,51 +310,25 @@ class AutomationContext:
[upload_result.blob_id for upload_result in upload_response.upload_results]
)
return upload_response.upload_results[0].blob_id
def mark_run_failed(
self, status_message: str, version_result: dict[str, Any] | None = None
) -> None:
"""
Mark the current run a failure.
Args:
status_message: Optional message to be displayed.
version_result: Optional data object,
that will be attached to the run results.
The dictionary should be JSON serializable
"""
self._mark_run(AutomationStatus.FAILED, status_message, version_result)
def mark_run_failed(self, status_message: str) -> None:
"""Mark the current run a failure."""
self._mark_run(AutomationStatus.FAILED, status_message)
def mark_run_exception(self, status_message: str) -> None:
"""Mark the current run a failure."""
self._mark_run(AutomationStatus.EXCEPTION, status_message, None)
self._mark_run(AutomationStatus.EXCEPTION, status_message)
def mark_run_success(
self, status_message: str | None, version_result: dict[str, Any] | None = None
) -> None:
"""
Mark the current run a success with an optional message.
Args:
status_message: Optional message to be displayed.
version_result: Optional data object,
that will be attached to the run results.
The dictionary should be JSON serializable
"""
self._mark_run(AutomationStatus.SUCCEEDED, status_message, version_result)
def mark_run_success(self, status_message: Optional[str]) -> None:
"""Mark the current run a success with an optional message."""
self._mark_run(AutomationStatus.SUCCEEDED, status_message)
def _mark_run(
self,
status: AutomationStatus,
status_message: str | None,
version_result: dict[str, Any] | None,
self, status: AutomationStatus, status_message: Optional[str]
) -> None:
duration = self.elapsed()
self._automation_result.status_message = status_message
self._automation_result.run_status = status
self._automation_result.elapsed = duration
self._automation_result.version_result = version_result
msg = f"Automation run {status.value} after {duration:.2f} seconds."
print("\n".join([msg, status_message]) if status_message else msg)
@@ -493,29 +454,29 @@ class AutomationContext:
Args:
level: Result level.
category (str): A short tag for the event type.
affected_objects (Union[Base, List[Base]]): A single object, a list of
objects, or an empty list. When empty, a result case is still
appended with no object IDs (e.g. for skipped rules or version-level
messages).
affected_objects (Union[Base, List[Base]]): A single object or a list of
objects that are causing the info case.
message (Optional[str]): Optional message.
metadata: User provided metadata key value pairs
visual_overrides: Case specific 3D visual overrides.
"""
if isinstance(affected_objects, list):
if len(affected_objects) < 1:
raise ValueError(
f"Need atleast one object to report a(n) {level.value.upper()}"
)
object_list = affected_objects
else:
object_list = [affected_objects]
ids: Dict[str, Optional[str]] = {}
# When objects are provided, each must have an id (empty list allowed for
# version-level/skipped results).
for o in object_list:
if not getattr(o, "id", None):
# validate that the Base.id is not None. If its a None, throw an Exception
if not o.id:
raise Exception(
f"You can only attach {level} results to objects with an id."
)
ids[o.id] = getattr(o, "applicationId", None)
ids[o.id] = o.applicationId
print(
f"Created new {level.value.upper()}"
f" category: {category} caused by: {message}"
+16 -5
View File
@@ -1,5 +1,8 @@
"""Some useful helpers for working with automation data."""
import secrets
import string
import pytest
from gql import gql
from pydantic import Field
@@ -88,8 +91,10 @@ def create_test_automation_run(
print(result)
return TestAutomationRunData.model_validate(
result["projectMutations"]["automationMutations"]["createTestAutomationRun"]
return (
result.get("projectMutations")
.get("automationMutations")
.get("createTestAutomationRun")
)
@@ -121,9 +126,9 @@ def create_test_automation_run_data(
project_id=test_automation_environment.project_id,
speckle_server_url=test_automation_environment.server_url,
automation_id=test_automation_environment.automation_id,
automation_run_id=test_automation_run_data.automation_run_id,
function_run_id=test_automation_run_data.function_run_id,
triggers=test_automation_run_data.triggers,
automation_run_id=test_automation_run_data["automationRunId"],
function_run_id=test_automation_run_data["functionRunId"],
triggers=test_automation_run_data["triggers"],
)
@@ -135,6 +140,12 @@ def test_automation_run_data(
return create_test_automation_run_data(speckle_client, test_automation_environment)
def crypto_random_string(length: int) -> str:
"""Generate a semi crypto random string of a given length."""
alphabet = string.ascii_letters + string.digits
return "".join(secrets.choice(alphabet) for _ in range(length)).lower()
__all__ = [
"test_automation_environment",
"test_automation_token",
+11 -12
View File
@@ -1,7 +1,7 @@
""""""
from enum import Enum
from typing import Any, Literal
from typing import Any, Dict, List, Literal, Optional
from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
@@ -36,7 +36,7 @@ class AutomationRunData(BaseModel):
automation_run_id: str
function_run_id: str
triggers: list[VersionCreationTrigger]
triggers: List[VersionCreationTrigger]
model_config = ConfigDict(
alias_generator=to_camel, populate_by_name=True, protected_namespaces=()
@@ -49,7 +49,7 @@ class TestAutomationRunData(BaseModel):
automation_run_id: str
function_run_id: str
triggers: list[VersionCreationTrigger]
triggers: List[VersionCreationTrigger]
model_config = ConfigDict(
alias_generator=to_camel, populate_by_name=True, protected_namespaces=()
@@ -80,20 +80,19 @@ class ResultCase(AutomateBase):
category: str
level: ObjectResultLevel
object_app_ids: dict[str, str | None]
message: str | None
metadata: dict[str, Any] | None
visual_overrides: dict[str, Any] | None
object_app_ids: Dict[str, Optional[str]]
message: Optional[str]
metadata: Optional[Dict[str, Any]]
visual_overrides: Optional[Dict[str, Any]]
class AutomationResult(AutomateBase):
"""Schema accepted by the Speckle server as a result for an automation run."""
elapsed: float = 0
result_view: str | None = None
result_versions: list[str] = Field(default_factory=list)
blobs: list[str] = Field(default_factory=list)
result_view: Optional[str] = None
result_versions: List[str] = Field(default_factory=list)
blobs: List[str] = Field(default_factory=list)
run_status: AutomationStatus = AutomationStatus.RUNNING
status_message: str | None = None
status_message: Optional[str] = None
object_results: list[ResultCase] = Field(default_factory=list)
version_result: dict[str, Any] | None = None
+60 -19
View File
@@ -4,9 +4,14 @@ import traceback
from argparse import ArgumentParser
from os import getenv
from speckleifc.main import open_and_convert_file
from speckleifc.ifc_geometry_processing import open_ifc
from speckleifc.importer import ImportJob
from specklepy.core.api.client import SpeckleClient
from specklepy.logging import metrics
from specklepy.core.api.credentials import Account
from specklepy.core.api.inputs.version_inputs import CreateVersionInput
from specklepy.core.api.models.current import Version
from specklepy.core.api.operations import send
from specklepy.transports.server import ServerTransport
def cmd_line_import() -> None:
@@ -18,7 +23,7 @@ def cmd_line_import() -> None:
parser.add_argument("output_path")
parser.add_argument("project_id")
parser.add_argument("version_message")
parser.add_argument("model_ingestion_id")
parser.add_argument("model_id")
# parser.add_argument("model_name")
# parser.add_argument("region_name")
@@ -27,30 +32,20 @@ def cmd_line_import() -> None:
TOKEN = getenv("USER_TOKEN")
assert TOKEN is not None
SERVER_URL = getenv("SPECKLE_SERVER_URL") or "http://127.0.0.1:3000"
metrics.set_host_app(
"ifc",
)
client: SpeckleClient | None = None
account = Account.from_token(TOKEN, SERVER_URL)
try:
client = SpeckleClient(SERVER_URL, use_ssl=not SERVER_URL.startswith("http://"))
client.authenticate_with_token(TOKEN)
project = client.project.get(args.project_id)
version = open_and_convert_file(
args.file_path,
project,
args.project_id,
args.version_message,
args.model_ingestion_id,
client,
args.model_id,
account,
)
with open(args.output_path, "w") as f:
json.dump({"success": True, "commitId": version.id}, f)
except Exception as e:
stack_trace = traceback.format_exc()
error_msg = f"IFC Importer failed with exception:\n{stack_trace}"
error_msg = f"IFC Importer failed with exception:\n{traceback.format_exc()}"
print(error_msg)
# Write error result
@@ -58,7 +53,53 @@ def cmd_line_import() -> None:
json.dump({"success": False, "error": str(e)}, f)
def open_and_convert_file(
file_path: str,
project_id: str,
version_message: str | None,
model_id: str,
account: Account,
) -> Version:
start = time.time()
very_start = start
ifc_file = open_ifc(file_path)
import_job = ImportJob(ifc_file)
data = import_job.convert()
print(f"File conversion complete after {(time.time() - start) * 1000}ms")
start = time.time()
remote_transport = ServerTransport(project_id, account=account)
root_id = send(data, transports=[remote_transport], use_default_cache=False)
print(f"Sending to speckle complete after: {(time.time() - start) * 1000}ms")
start = time.time()
server_url = account.serverInfo.url
assert server_url
client = SpeckleClient(host=server_url, use_ssl=server_url.startswith("https"))
client.authenticate_with_account(account)
create_version = CreateVersionInput(
object_id=root_id,
model_id=model_id,
project_id=project_id,
message=version_message,
source_application="IFC",
)
version = client.version.create(create_version)
end = time.time()
print(f"Version committed after: {(end - start) * 1000}ms")
print(f"Total time (to commit): {(end - very_start) * 1000}ms")
del ifc_file
return version
if __name__ == "__main__":
start = time.time()
cmd_line_import()
print(f"Total time (including cleanup): {(time.time() - start):.3f}s")
print(f"Total time (including cleanup): {(time.time() - start) * 1000}ms")
@@ -11,31 +11,13 @@ def data_object_to_speckle(
display_value: list[Base],
step_element: entity_instance,
children: list[Base],
current_storey: str | None = None,
parent_element: entity_instance | None = None,
) -> DataObject:
guid = cast(str, step_element.GlobalId)
name = cast(str, step_element.Name or guid)
properties = extract_properties(step_element)
# Add parent ID only if element's parent is also a DataObject (not a Collection)
# Collections are: IfcProject and IfcSpatialStructureElement types
if (
parent_element
and hasattr(parent_element, "GlobalId")
and not parent_element.is_a("IfcProject")
and not parent_element.is_a("IfcSpatialStructureElement")
):
properties["parentApplicationId"] = parent_element.GlobalId
# Add building storey information if available and not a building storey itself
if current_storey and not step_element.is_a("IfcBuildingStorey"):
properties["Building Storey"] = current_storey
data_object = DataObject(
applicationId=guid,
properties=properties,
properties=extract_properties(step_element),
name=name or guid,
displayValue=display_value,
)
@@ -4,21 +4,21 @@ from typing import cast
from ifcopenshell.ifcopenshell_wrapper import (
Triangulation,
TriangulationElement,
colour,
style,
)
from speckleifc.proxy_managers.render_material_proxy_manager import (
RenderMaterialProxyManager,
)
from speckleifc.render_material_proxy_manager import RenderMaterialProxyManager
from specklepy.objects.base import Base
from specklepy.objects.geometry import Mesh
from specklepy.objects.other import RenderMaterial
def geometry_to_speckle(
geometry: Triangulation, render_material_manager: RenderMaterialProxyManager
shape: TriangulationElement, render_material_manager: RenderMaterialProxyManager
) -> list[Base]:
geometry = cast(Triangulation, shape.geometry)
materials = cast(Sequence[style], geometry.materials)
MESH_COUNT = max(len(materials), 1)
@@ -33,7 +33,7 @@ def geometry_to_speckle(
# Not really expected, but occasionally some meshes fail to triangulate
return []
mapped_meshes = _pre_alloc_mesh_lists(geometry, material_ids, MESH_COUNT)
mapped_meshes = _pre_alloc_mesh_lists(shape, material_ids, MESH_COUNT)
for i, mesh in enumerate(mapped_meshes):
material = _material_to_speckle(materials[i])
render_material_manager.add_mesh_material_mapping(material, mesh)
@@ -103,14 +103,14 @@ def _color_to_argb(colour: colour) -> int:
def _pre_alloc_mesh_lists(
geometry: Triangulation, material_ids: Sequence[int], MESH_COUNT: int
shape: TriangulationElement, material_ids: Sequence[int], MESH_COUNT: int
) -> list[Mesh]:
"""
This is a performance optimisation to pre-size the lists
since we're expecting potential hundreds of thousands of verts in a single model
This is very much in the hot path, so worth the extra bit of convoluted logic
"""
appId = cast(str, geometry.id)
appId = cast(str, shape.guid)
material_face_counts = defaultdict(int)
for mat_id in material_ids:
@@ -12,11 +12,8 @@ def spatial_element_to_speckle(
display_value: list[Base],
step_element: entity_instance,
relational_children: list[Base],
current_storey: str | None = None,
) -> Collection:
direct_geometry = _convert_as_data_object(
display_value, step_element, current_storey
)
direct_geometry = _convert_as_data_object(display_value, step_element)
all_children = [direct_geometry] + relational_children
guid = cast(str, step_element.GlobalId)
@@ -29,22 +26,13 @@ def spatial_element_to_speckle(
def _convert_as_data_object(
display_value: list[Base],
step_element: entity_instance,
current_storey: str | None = None,
display_value: list[Base], step_element: entity_instance
) -> DataObject:
guid = cast(str, step_element.GlobalId)
name = cast(str, step_element.Name or step_element.LongName or guid)
properties = extract_properties(step_element)
# Add building storey information if available and not a building storey itself
if current_storey and not step_element.is_a("IfcBuildingStorey"):
properties["Building Storey"] = current_storey
data_object = DataObject(
applicationId=guid,
properties=properties,
properties=extract_properties(step_element),
name=name,
displayValue=display_value,
)
+5 -20
View File
@@ -1,6 +1,6 @@
import multiprocessing
from ifcopenshell import SchemaError, file, ifcopenshell_wrapper, open, sqlite
from ifcopenshell import file, ifcopenshell_wrapper, open, sqlite
from ifcopenshell.geom import iterator, settings
from specklepy.logging.exceptions import SpeckleException
@@ -12,10 +12,8 @@ def _create_iterator_settings() -> settings:
ifc_settings.set("triangulation-type", ifcopenshell_wrapper.TRIANGLE_MESH)
# no need to weld verts
ifc_settings.set("weld-vertices", False)
#
ifc_settings.set("use-world-coords", False)
ifc_settings.set("permissive-shape-reuse", True)
# Speckle meshes are all in world coords
ifc_settings.set("use-world-coords", True)
# Tiny performance improvement,
ifc_settings.set("no-wire-intersection-check", True)
# Rendermaterials inherit the material names instead of type + unique id
@@ -35,14 +33,7 @@ def _create_iterator_settings() -> settings:
def open_ifc(file_path: str) -> file:
try:
ifc_file = open(file_path)
except SchemaError:
raise
except FileNotFoundError:
raise
except Exception as ex:
raise SpeckleException("File could not be opened as an IFC file") from ex
ifc_file = open(file_path)
if isinstance(ifc_file, file):
return ifc_file
@@ -51,10 +42,4 @@ def open_ifc(file_path: str) -> file:
def create_geometry_iterator(ifc_file: file | sqlite) -> iterator:
GEOMETRY_LIBRARY = "hybrid-opencascade-cgal" # First OCC then fallback to CGAL
return iterator(
_create_iterator_settings(),
ifc_file,
multiprocessing.cpu_count(),
geometry_library=GEOMETRY_LIBRARY, # type: ignore
)
return iterator(_create_iterator_settings(), ifc_file, multiprocessing.cpu_count())
+19 -158
View File
@@ -1,10 +1,10 @@
import time
from dataclasses import dataclass, field
from typing import List, cast
from typing import cast
from ifcopenshell.entity_instance import entity_instance
from ifcopenshell.geom import file
from ifcopenshell.ifcopenshell_wrapper import Triangulation, TriangulationElement
from ifcopenshell.ifcopenshell_wrapper import TriangulationElement
from speckleifc.converter.data_object_converter import data_object_to_speckle
from speckleifc.converter.geometry_converter import geometry_to_speckle
@@ -12,117 +12,38 @@ from speckleifc.converter.project_converter import project_to_speckle
from speckleifc.converter.spatial_element_converter import spatial_element_to_speckle
from speckleifc.ifc_geometry_processing import create_geometry_iterator
from speckleifc.ifc_openshell_helpers import get_children
from speckleifc.proxy_managers.instance_proxy_manager import InstanceProxyManager
from speckleifc.proxy_managers.level_proxy_manager import LevelProxyManager
from speckleifc.proxy_managers.render_material_proxy_manager import (
RenderMaterialProxyManager,
)
from speckleifc.render_material_proxy_manager import RenderMaterialProxyManager
from specklepy.logging.exceptions import SpeckleException
from specklepy.objects import Base
from specklepy.objects.data_objects import DataObject
from specklepy.objects.models.collections.collection import Collection
from specklepy.objects.proxies import InstanceProxy
from specklepy.progress.ingestion_progress import IngestionProgressManager
@dataclass
class ImportJob:
ifc_file: file
progress: IngestionProgressManager
cached_display_values: dict[int, list[Base]] = field(default_factory=dict) # noqa: F821
_render_material_manager: RenderMaterialProxyManager = field(
default_factory=lambda: RenderMaterialProxyManager()
)
_level_proxy_manager: LevelProxyManager = field(
default_factory=lambda: LevelProxyManager()
)
_instance_proxy_manager: InstanceProxyManager = field(
default_factory=lambda: InstanceProxyManager()
)
geometries_count: int = 0
geometries_used: int = 0
elements_converted: int = 0
_current_storey_data_object: DataObject | None = field(default=None, init=False)
_display_value_cache: dict[int, list[Base]] = field(default_factory=dict)
"""Maps an instance step ID to a list of instances"""
def convert_element(
self,
step_element: entity_instance,
parent_element: entity_instance | None = None,
) -> Base:
try:
return self._convert_element(step_element, parent_element)
except SpeckleException:
raise
except Exception as ex:
raise SpeckleException(
f"Failed to convert {step_element.is_a()} #{step_element.id()}"
) from ex
def _convert_element(
self,
step_element: entity_instance,
parent_element: entity_instance | None = None,
) -> Base:
# Track current storey context and store for level proxies
previous_storey_data_object = self._current_storey_data_object
if step_element.is_a("IfcBuildingStorey"):
# Convert the building storey to a DataObject for the level proxy
storey_display_value = self._display_value_cache.get(step_element.id(), [])
self._current_storey_data_object = data_object_to_speckle(
storey_display_value, step_element, [], parent_element=None
)
def convert_element(self, step_element: entity_instance) -> Base:
children = self._convert_children(step_element)
id = step_element.id()
display_value = self._display_value_cache.get(id, [])
display_value = self.cached_display_values.get(step_element.id(), [])
if display_value:
if display_value is not None:
self.geometries_used += 1
# Extract current storey name from DataObject if available
current_storey_name = (
self._current_storey_data_object.name
if self._current_storey_data_object
else None
)
if step_element.is_a("IfcProject"):
result = project_to_speckle(step_element, children)
return project_to_speckle(step_element, children)
elif step_element.is_a("IfcSpatialStructureElement"):
result = spatial_element_to_speckle(
display_value, step_element, children, current_storey_name
)
return spatial_element_to_speckle(display_value, step_element, children)
else:
result = data_object_to_speckle(
display_value,
step_element,
children,
current_storey_name,
parent_element,
)
# Associate non-spatial elements with current storey for level proxies
if self._current_storey_data_object is not None and result.applicationId:
self._level_proxy_manager.add_element_level_mapping(
self._current_storey_data_object, result.applicationId
)
# Restore previous storey context
self._current_storey_data_object = previous_storey_data_object
self.elements_converted += 1
if self.progress.should_report_progress():
self.progress.report(
f"Converted {self.elements_converted:,} elements", None
)
return result
return data_object_to_speckle(display_value, step_element, children)
def _convert_children(self, step_element: entity_instance) -> list[Base]:
return [
self.convert_element(i, parent_element=step_element)
self.convert_element(i)
for i in get_children(step_element)
if self._should_convert(i)
]
@@ -142,102 +63,42 @@ class ImportJob:
def convert(self) -> Base:
start = time.time()
self.pre_process_geometry()
print(
f"Geometry conversion complete after {(time.time() - start):.3f}s" # noqa: E501
)
print(f"Geometry conversion complete after {(time.time() - start) * 1000}ms")
print(f"Created {self.geometries_count} geometries")
start = time.time()
root = self._convert_project_tree()
print(
f"Element tree conversion complete after {(time.time() - start):.3f}s" # noqa: E501
)
print(f"Object tree conversion complete after {(time.time() - start) * 1000}ms")
print(f"Used {self.geometries_used} geometries")
return root
def pre_process_geometry(self) -> None:
iterator = create_geometry_iterator(self.ifc_file)
if not iterator.initialize():
raise SpeckleException("Failed to find any geometry in file")
self.progress.report("Converting geometries", None)
raise SpeckleException(
"geometry iterator failed to initialize for the given file"
)
self.geometries_count = 0
while True:
shape = cast(TriangulationElement, iterator.get())
self.geometries_count += 1
id = cast(int, shape.id)
try:
display_value = self._create_display_value(shape)
self._display_value_cache[id] = display_value
except Exception as ex:
raise SpeckleException(
f"Failed to convert geometry with id: {id}"
) from ex
if self.progress.should_report_progress():
self.progress.report(
f"Converted {self.geometries_count:,} geometries", None
)
display_value = geometry_to_speckle(shape, self._render_material_manager)
self.cached_display_values[id] = display_value
if not iterator.next():
break
def _create_display_value(self, shape: TriangulationElement) -> List[Base]:
geometry = cast(Triangulation, shape.geometry)
display_value_geometry = geometry_to_speckle(
geometry, self._render_material_manager
)
definition_ids = self._instance_proxy_manager.add_display_value_definitions(
display_value_geometry
)
matrix = shape.transformation.matrix
transposed = [
matrix[0], matrix[4], matrix[8], matrix[12],
matrix[1], matrix[5], matrix[9], matrix[13],
matrix[2], matrix[6], matrix[10], matrix[14],
matrix[3], matrix[7], matrix[11], matrix[15],
] # fmt: skip
return [
cast(
Base,
InstanceProxy(
units="m",
definitionId=definition_id,
transform=transposed,
maxDepth=0,
applicationId=f"{shape.guid}:{definition_id}",
),
)
for definition_id in definition_ids
]
def _convert_project_tree(self) -> Base:
projects = self.ifc_file.by_type("IfcProject", False)
if len(projects) != 1:
raise SpeckleException("Expected exactly one IfcProject in file")
project = projects[0]
self.progress.report("Converting elements", None)
tree = self.convert_element(project)
if not isinstance(tree, Collection):
raise TypeError("Expected root object to convert to a Collection")
tree["renderMaterialProxies"] = list(
self._render_material_manager.render_material_proxies.values()
)
tree["levelProxies"] = list(self._level_proxy_manager.level_proxies.values())
tree["instanceDefinitionProxies"] = list(
self._instance_proxy_manager.instance_definition_proxies.values()
)
tree.elements.append(
Collection(
name="definitionGeometry",
elements=list(self._instance_proxy_manager.instance_geometry.values()),
)
)
tree["version"] = 3
return tree
-133
View File
@@ -1,133 +0,0 @@
import contextlib
import importlib.metadata
import time
import traceback
from pathlib import Path
from speckleifc.ifc_geometry_processing import open_ifc
from speckleifc.importer import ImportJob
from specklepy.core.api.client import SpeckleClient
from specklepy.core.api.inputs.model_ingestion_inputs import (
ModelIngestionFailedInput,
ModelIngestionStartProcessingInput,
ModelIngestionSuccessInput,
SourceDataInput,
)
from specklepy.core.api.models.current import Project, Version
from specklepy.core.api.operations import send
from specklepy.logging import metrics
from specklepy.progress.ingestion_progress import IngestionProgressManager
from specklepy.progress.progress_transport import ProgressTransport
from specklepy.transports.server import ServerTransport
# Since progress messages are currently blocking (no async), we're being extra coarse
# with progress updates to ensure we're not waisting time sending updates.
# We could maybe go a little lower, but for now I'm not risking degrading performance
PROGRESS_INTERVAL_SECONDS = 10
def open_and_convert_file(
file_path: str,
project: Project,
version_message: str | None,
model_ingestion_id: str,
client: SpeckleClient,
) -> Version:
try:
start = time.time()
very_start = start
path = Path(file_path)
specklepy_version = importlib.metadata.version("specklepy")
ingestion = client.model_ingestion.start_processing(
ModelIngestionStartProcessingInput(
project_id=project.id,
ingestion_id=model_ingestion_id,
progress_message="Importing IFC file",
source_data=SourceDataInput(
file_name=path.name,
file_size_bytes=path.stat().st_size,
source_application_slug=metrics.HOST_APP,
source_application_version=specklepy_version,
),
)
)
progress = IngestionProgressManager(
client, ingestion, PROGRESS_INTERVAL_SECONDS
)
account = client.account
server_url = account.serverInfo.url
assert server_url
remote_transport = ServerTransport(project.id, account=account)
progress_transport = ProgressTransport(
progress,
)
progress.report("Opening file", None)
ifc_file = open_ifc(file_path) # pyright: ignore[reportUnknownVariableType]
import_job = ImportJob(ifc_file, progress) # pyright: ignore[reportUnknownArgumentType]
data = import_job.convert()
print(
f"File conversion complete after {(time.time() - start):.3f}s" # noqa: E501
)
start = time.time()
progress.report("Uploading objects", None)
root_id = send(
data,
transports=[remote_transport, progress_transport],
use_default_cache=False,
)
print(
f"Sending to speckle complete after: {(time.time() - start):.3f}s" # noqa: E501
)
start = time.time()
version_id = client.model_ingestion.complete(
ModelIngestionSuccessInput(
project_id=project.id,
ingestion_id=model_ingestion_id,
root_object_id=root_id,
version_message=version_message,
)
)
# needed to query version until ingestion api expands to serve it
version = client.version.get(version_id, project.id)
end = time.time()
print(f"Version committed after: {(end - start):.3f}s")
print(f"Total time (to commit): {(end - very_start):.3f}s")
del ifc_file
custom_properties = {"ui": "dui3", "actionSource": "import"}
if project.workspace_id:
custom_properties["workspace_id"] = project.workspace_id
metrics.track(
metrics.SEND,
account,
custom_properties,
send_sync=True,
track_email=True,
)
return version
except Exception as e:
stack_trace = traceback.format_exc()
with contextlib.suppress(Exception):
# make sure to not report process kills when we're cancelling
client.model_ingestion.fail_with_error(
ModelIngestionFailedInput(
project_id=project.id,
ingestion_id=model_ingestion_id,
error_reason=str(e),
error_stacktrace=stack_trace,
)
)
raise e
-51
View File
@@ -1,51 +0,0 @@
import time
from speckleifc.main import open_and_convert_file
from specklepy.api.client import SpeckleClient
from specklepy.core.api.credentials import get_accounts_for_server
from specklepy.logging import metrics
def _manual_import() -> None:
from specklepy.core.api.inputs.model_ingestion_inputs import (
ModelIngestionCreateInput,
SourceDataInput,
)
PROJECT_ID = "412a3c3927"
MODEL_ID = "223e61212d"
SERVER_URL = "latest.speckle.systems"
FILE_PATH = r"C:\Test Files\ifc\AC20-FZK-Haus.ifc" # noqa: E501
metrics.set_host_app(
"ifc",
)
account = get_accounts_for_server(SERVER_URL)[0]
client = SpeckleClient(SERVER_URL, use_ssl=not SERVER_URL.startswith("http://"))
client.authenticate_with_account(account)
ingestion = client.model_ingestion.create(
ModelIngestionCreateInput(
model_id=MODEL_ID,
project_id=PROJECT_ID,
progress_message="",
source_data=SourceDataInput(
source_application_slug="speckleifc",
source_application_version="0.0.0",
file_name=None,
file_size_bytes=None,
),
max_idle_timeout_seconds=2700, # 45mins
)
)
project = client.project.get(PROJECT_ID)
open_and_convert_file(FILE_PATH, project, None, ingestion.id, client)
if __name__ == "__main__":
start = time.time()
_manual_import()
print(f"Total time (including cleanup): {(time.time() - start):.3f}s")
+17 -115
View File
@@ -1,30 +1,21 @@
import math
from typing import Any, Tuple
from typing import Any
from ifcopenshell.entity_instance import entity_instance
from ifcopenshell.util.element import get_type
from ifcopenshell.util.unit import get_full_unit_name, get_project_unit
UNIT_MAPPING = {
"IfcQuantityLength": "LENGTHUNIT",
"IfcQuantityArea": "AREAUNIT",
"IfcQuantityVolume": "VOLUMEUNIT",
"IfcQuantityCount": None, # Count quantities have no units
"IfcQuantityWeight": "MASSUNIT",
"IfcQuantityTime": "TIMEUNIT",
}
from speckleifc.qtos_only import get_quantities
def extract_properties(element: entity_instance) -> dict[str, object]:
(psets, qtos) = _get_ifc_object_properties(element)
properties: dict[str, object] = {
"Attributes": _get_attributes(element),
"Property Sets": psets,
"Property Sets": _get_ifc_object_properties(element),
}
if qtos:
properties["Quantities"] = qtos
# Add quantities if they exist
quantities = get_quantities(element)
if quantities:
properties["Quantities"] = quantities
if (ifc_type := get_type(element)) is not None:
properties["Element Type Property Sets"] = _get_ifc_element_type_properties(
@@ -51,11 +42,8 @@ def _get_ifc_element_type_properties(element: entity_instance) -> dict[str, obje
return result
def _get_ifc_object_properties(
element: entity_instance,
) -> Tuple[dict[str, object], dict[str, object]]:
psets: dict[str, object] = {}
qtos: dict[str, object] = {}
def _get_ifc_object_properties(element: entity_instance) -> dict[str, object]:
result: dict[str, object] = {}
for rel in getattr(element, "IsDefinedBy", []):
if not rel.is_a("IfcRelDefinesByProperties"):
@@ -65,27 +53,16 @@ def _get_ifc_object_properties(
if not definition:
continue
try:
if definition.is_a("IfcPropertySet"):
set_name = definition.Name
properties = _get_properties(definition.HasProperties)
if properties:
psets[set_name] = properties
elif definition.is_a("IfcElementQuantity"):
quantities_data = _get_quantities(definition.Quantities, element)
if not quantities_data:
continue
quantities_data["id"] = definition.id()
qtos[definition.Name] = quantities_data
except (KeyError, AttributeError):
# If entity access fails, skip this quantity set
print(f"Skipping {definition}")
if not definition.is_a("IfcPropertySet"):
continue
return (psets, qtos)
set_name = definition.Name
properties = _get_properties(definition.HasProperties)
if properties:
result[set_name] = properties
return result
def _get_properties(properties: entity_instance) -> dict[str, Any]:
@@ -120,78 +97,3 @@ def _get_properties(properties: entity_instance) -> dict[str, Any]:
# elif prop.is_a("IfcPropertyTableValue"):
# properties[name] = #not sure if we want to support these...
return result
def _get_quantities(
quantities: list[entity_instance], element: entity_instance
) -> dict[str, Any]:
"""Extract quantity values from IfcPhysicalQuantity entities."""
results: dict[str, Any] = {}
for quantity in quantities or []:
quantity_name = quantity.Name
if quantity.is_a("IfcPhysicalSimpleQuantity"):
# Get the quantity value (3rd attribute for simple quantities)
value = getattr(quantity, quantity.attribute_name(3))
unit_info = _get_unit_info(element, quantity)
# Server does not consider `NaN` valid json
if math.isnan(value):
value = None
if unit_info:
# Create structured quantity object with units
results[quantity_name] = {
"name": quantity_name,
"value": value,
**unit_info,
}
else:
# No unit info available, keep as simple value with name
results[quantity_name] = {"name": quantity_name, "value": value}
elif quantity.is_a("IfcPhysicalComplexQuantity"):
# Handle complex quantities
data = {
k: v
for k, v in quantity.get_info().items()
if v is not None and k != "Name"
}
data["properties"] = _get_quantities(quantity.HasQuantities, element)
del data["HasQuantities"]
results[quantity_name] = data
return results
def _get_unit_info(
element: entity_instance, quantity: entity_instance
) -> dict[str, str]:
"""Get unit information for a quantity."""
# Early return for count quantities - they don't have units
quantity_type = quantity.is_a()
if quantity_type == "IfcQuantityCount":
return {}
unit = getattr(element, "Unit", None)
if unit:
# Quantity has its own unit
unit_name = get_full_unit_name(unit)
formatted_unit_name = unit_name.replace("_", " ").title() if unit_name else ""
return {"units": formatted_unit_name}
else:
# Fall back to project unit based on quantity type
unit_type = UNIT_MAPPING.get(quantity_type)
if not unit_type:
return {}
# Get the project unit for this unit type
project_unit = get_project_unit(element.file, unit_type, use_cache=True)
if not project_unit:
return {}
# Get unit name and format
unit_name = get_full_unit_name(project_unit)
formatted_unit_name = unit_name.replace("_", " ").title() if unit_name else ""
return {"units": formatted_unit_name}
@@ -1,43 +0,0 @@
from typing import Sequence
from specklepy.objects.base import Base
from specklepy.objects.proxies import InstanceDefinitionProxy
class InstanceProxyManager:
def __init__(self):
self._instance_definition_proxies: dict[str, InstanceDefinitionProxy] = {}
"""definition proxies to be added directly to the root"""
self._instance_geometry: dict[str, Base] = {}
"""The geometry that will be added in it's own collection under the root"""
@property
def instance_definition_proxies(self) -> dict[str, InstanceDefinitionProxy]:
return self._instance_definition_proxies
@property
def instance_geometry(self) -> dict[str, Base]:
return self._instance_geometry
def add_display_value_definitions(self, geometry: Sequence[Base]) -> list[str]:
result: list[str] = []
for m in geometry:
if not m.applicationId:
raise ValueError("geometry with no applicationId cannot be proxied ")
definition_id = f"DEFINITION:{m.applicationId}"
result.append(definition_id)
self._add_definition(definition_id, [m.applicationId], 0)
self._instance_geometry[m.applicationId] = m
return result
def _add_definition(
self, definition_id: str, objects: list[str], max_depth: int
) -> None:
proxy = InstanceDefinitionProxy(
applicationId=definition_id,
name=definition_id,
objects=objects,
maxDepth=max_depth,
)
self._instance_definition_proxies[definition_id] = proxy
@@ -1,27 +0,0 @@
from specklepy.objects.data_objects import DataObject
from specklepy.objects.proxies import LevelProxy
class LevelProxyManager:
def __init__(self):
self._level_proxies: dict[str, LevelProxy] = {}
@property
def level_proxies(self):
return self._level_proxies
def add_element_level_mapping(
self, level_data_object: DataObject, element_application_id: str
) -> None:
level_id = level_data_object.applicationId
assert level_id is not None
proxy = self._level_proxies.get(level_id, None)
if proxy is not None:
proxy.objects.append(element_application_id)
else:
self._level_proxies[level_id] = LevelProxy(
objects=[element_application_id],
value=level_data_object,
applicationId=level_id,
)
+106
View File
@@ -0,0 +1,106 @@
from typing import Any
from ifcopenshell.entity_instance import entity_instance
from ifcopenshell.util.unit import get_full_unit_name, get_project_unit
# Module-level constants for units
UNIT_MAPPING = {
"IfcQuantityLength": "LENGTHUNIT",
"IfcQuantityArea": "AREAUNIT",
"IfcQuantityVolume": "VOLUMEUNIT",
"IfcQuantityCount": None, # Count quantities have no units
"IfcQuantityWeight": "MASSUNIT",
"IfcQuantityTime": "TIMEUNIT"
}
def _get_unit_info(element: entity_instance, quantity) -> dict[str, str]:
"""Get unit information for a quantity."""
try:
# Early return for count quantities - they don't have units
quantity_type = quantity.is_a()
if quantity_type == "IfcQuantityCount":
return {}
if quantity.Unit is not None:
# Quantity has its own unit
try:
unit_name = get_full_unit_name(quantity.Unit)
formatted_unit_name = unit_name.replace("_", " ").title() if unit_name else ""
return {"units": formatted_unit_name}
except:
return {"units": str(quantity.Unit)}
else:
# Fall back to project unit based on quantity type
unit_type = UNIT_MAPPING.get(quantity_type)
if not unit_type:
return {}
# Get the project unit for this unit type
project_unit = get_project_unit(element.file, unit_type, use_cache=True)
if not project_unit:
return {}
# Get unit name and format
unit_name = get_full_unit_name(project_unit)
formatted_unit_name = unit_name.replace("_", " ").title() if unit_name else ""
return {"units": formatted_unit_name}
except Exception:
# If anything fails, return empty dict
return {}
def _get_quantities(quantities: list[entity_instance], element: entity_instance) -> dict[str, Any]:
"""Extract quantity values from IfcPhysicalQuantity entities."""
results = {}
for quantity in quantities or []:
quantity_name = quantity.Name
quantity_type = quantity.is_a() # Cache the type check
if quantity_type == "IfcPhysicalSimpleQuantity":
# Get the quantity value (3rd attribute for simple quantities)
value = getattr(quantity, quantity.attribute_name(3))
unit_info = _get_unit_info(element, quantity)
if unit_info:
# Create structured quantity object with units
results[quantity_name] = {
"name": quantity_name,
"value": value,
**unit_info,
}
else:
# No unit info available, keep as simple value with name
results[quantity_name] = {"name": quantity_name, "value": value}
elif quantity_type == "IfcPhysicalComplexQuantity":
# Handle complex quantities
data = {k: v for k, v in quantity.get_info().items() if v is not None and k != "Name"}
data["properties"] = _get_quantities(quantity.HasQuantities, element)
del data["HasQuantities"]
results[quantity_name] = data
return results
def get_quantities(element: entity_instance) -> dict[str, object]:
"""
Extract quantity takeoffs (QTOs) from an IFC element with unit information.
"""
qtos = {}
# Handle elements with IsDefinedBy relationship
if element.IsDefinedBy:
for relationship in element.IsDefinedBy:
if relationship.is_a("IfcRelDefinesByProperties"):
definition = relationship.RelatingPropertyDefinition
if definition.is_a("IfcElementQuantity"):
try:
quantities_data = _get_quantities(definition.Quantities, element)
quantities_data["id"] = definition.id()
qtos[definition.Name] = quantities_data
except (KeyError, AttributeError):
# If entity access fails, skip this quantity set
continue
return qtos
+110
View File
@@ -0,0 +1,110 @@
from typing import Any
from ifcopenshell.entity_instance import entity_instance
from ifcopenshell.util.element import get_psets
from ifcopenshell.util.unit import get_full_unit_name, get_project_unit
def _format_unit_name(unit_name: str) -> str:
"""
Convert IFC unit names to user-friendly format.
"""
if not unit_name:
return ""
# Convert underscore-separated words to space-separated and title case
return unit_name.replace("_", " ").title()
def _get_unit_info(element: entity_instance, quantity_type: str) -> dict[str, str]:
"""
Get unit information for a given quantity type from the IFC project.
"""
try:
# Map IFC quantity types to unit types
unit_type_mapping = {
"IfcQuantityLength": "LENGTHUNIT",
"IfcQuantityArea": "AREAUNIT",
"IfcQuantityVolume": "VOLUMEUNIT",
"IfcQuantityCount": None, # Count quantities typically have no units
"IfcQuantityWeight": "MASSUNIT",
"IfcQuantityTime": "TIMEUNIT",
}
unit_type = unit_type_mapping.get(quantity_type)
if not unit_type:
return {}
# Get the project unit for this unit type (with built-in caching)
project_unit = get_project_unit(element.file, unit_type, use_cache=True)
if not project_unit:
return {}
# Get unit name
unit_name = get_full_unit_name(project_unit)
# Format the unit name to be user-friendly
formatted_unit_name = _format_unit_name(unit_name)
return {"units": formatted_unit_name}
except Exception:
# If anything fails, return empty dict to maintain robustness
return {}
def get_quantities(element: entity_instance) -> dict[str, object]:
"""
Extract quantity takeoffs (QTOs) from an IFC element with unit information.
"""
# Get basic quantities using existing utility
quantities = get_psets(element, qtos_only=True, should_inherit=False)
if not quantities:
return {}
# Enhance each QTO pset with unit information
enhanced_quantities = {}
for pset_name, pset_data in quantities.items():
if not isinstance(pset_data, dict) or "id" not in pset_data:
# Fallback for unexpected data structure
enhanced_quantities[pset_name] = pset_data
continue
try:
# Get the actual IfcElementQuantity entity
pset_entity = element.file.by_id(pset_data["id"])
# Transform quantities to include unit information
enhanced_pset = {"id": pset_data["id"]}
# Create mapping of quantity names to their IFC entities for unit lookup
quantity_entities = {
q.Name: q for q in pset_entity.Quantities if hasattr(q, "Name")
}
for qty_name, qty_value in pset_data.items():
if qty_name == "id":
continue
# Get the IFC quantity entity for unit information
qty_entity = quantity_entities[qty_name]
unit_info = _get_unit_info(element, qty_entity.is_a())
if unit_info:
# Create structured quantity object with units
enhanced_pset[qty_name] = {
"name": qty_name,
"value": qty_value,
**unit_info,
}
else:
# No unit info available, keep as simple value with name
enhanced_pset[qty_name] = {"name": qty_name, "value": qty_value}
enhanced_quantities[pset_name] = enhanced_pset
except (KeyError, AttributeError):
# If entity access fails, use original data as fallback
enhanced_quantities[pset_name] = pset_data
return enhanced_quantities
-14
View File
@@ -3,8 +3,6 @@ import contextlib
from specklepy.api.credentials import Account
from specklepy.api.resources import (
ActiveUserResource,
FileImportResource,
ModelIngestionResource,
ModelResource,
OtherUserResource,
ProjectInviteResource,
@@ -120,18 +118,6 @@ class SpeckleClient(CoreSpeckleClient):
client=self.httpclient,
server_version=server_version,
)
self.model_ingestion = ModelIngestionResource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.file_import = FileImportResource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.subscription = SubscriptionResource(
account=self.account,
basepath=self.ws_url,
+5 -3
View File
@@ -1,3 +1,5 @@
from typing import List, Optional
# following imports seem to be unnecessary, but they need to stay
# to not break the scripts using these functions as non-core
from specklepy.core.api.credentials import ( # noqa: F401
@@ -12,7 +14,7 @@ from specklepy.core.api.credentials import get_local_accounts as core_get_local_
from specklepy.logging import metrics
def get_local_accounts(base_path: str | None = None) -> list[Account]:
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
"""Gets all the accounts present in this environment
Arguments:
@@ -36,7 +38,7 @@ def get_local_accounts(base_path: str | None = None) -> list[Account]:
return accounts
def get_default_account(base_path: str | None = None) -> Account | None:
def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
"""
Gets this environment's default account if any. If there is no default,
the first found will be returned and set as default.
@@ -59,7 +61,7 @@ def get_default_account(base_path: str | None = None) -> Account | None:
return default
def get_account_from_token(token: str, server_url: str | None = None) -> Account:
def get_account_from_token(token: str, server_url: str = None) -> Account:
"""Gets the local account for the token if it exists
Arguments:
token {str} -- the api token
-7
View File
@@ -1,8 +1,4 @@
from specklepy.api.resources.current.active_user_resource import ActiveUserResource
from specklepy.api.resources.current.file_import_resource import FileImportResource
from specklepy.api.resources.current.model_ingestion_resource import (
ModelIngestionResource,
)
from specklepy.api.resources.current.model_resource import ModelResource
from specklepy.api.resources.current.other_user_resource import OtherUserResource
from specklepy.api.resources.current.project_invite_resource import (
@@ -15,7 +11,6 @@ from specklepy.api.resources.current.version_resource import VersionResource
from specklepy.api.resources.current.workspace_resource import WorkspaceResource
__all__ = [
"FileImportResource",
"ActiveUserResource",
"ModelResource",
"OtherUserResource",
@@ -25,6 +20,4 @@ __all__ = [
"SubscriptionResource",
"VersionResource",
"WorkspaceResource",
"FileImportResource",
"ModelIngestionResource",
]
@@ -1,87 +0,0 @@
from pathlib import Path
from typing_extensions import override
from specklepy.core.api.inputs import (
FinishFileImportInput,
GenerateFileUploadUrlInput,
StartFileImportInput,
)
from specklepy.core.api.models import FileImport, FileUploadUrl
from specklepy.core.api.models.current import ResourceCollection
from specklepy.core.api.resources import FileImportResource as CoreResource
from specklepy.core.api.resources.current.file_import_resource import UploadFileResponse
from specklepy.logging import metrics
class FileImportResource(CoreResource):
"""API Access class for file imports"""
def __init__(self, account, basepath, client, server_version) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
server_version=server_version,
)
@override
def start_file_import(self, input: StartFileImportInput) -> FileImport:
metrics.track(metrics.SDK, self.account, {"name": "File Import Start"})
return super().start_file_import(input)
@override
def generate_upload_url(self, input: GenerateFileUploadUrlInput) -> FileUploadUrl:
"""
Get a file upload url from the Speckle server.
This method asks the server to create a pre-signed S3 url,
which can be used as a short term authenticated route,
to put a file to the server.
"""
metrics.track(
metrics.SDK, self.account, {"name": "File Import Generate Upload Url"}
)
return super().generate_upload_url(input)
@override
def upload_file(self, file: Path, url: str) -> UploadFileResponse:
"""
Uploads a file to the given S3 url.
This method should be used together with the generate_upload_url method,
which generates a pre-signed S3 url, that can be used to upload the file to.
"""
metrics.track(metrics.SDK, self.account, {"name": "File Import Upload File"})
return super().upload_file(file, url)
@override
def download_file(self, project_id: str, file_id: str, target_file: Path) -> Path:
"""Download a file blob attached to the project, to the target path."""
metrics.track(metrics.SDK, self.account, {"name": "File Import Download File"})
return super().download_file(project_id, file_id, target_file)
@override
def finish_file_import_job(self, input: FinishFileImportInput) -> bool:
"""
This is mostly an internal api, that marks a file import job finished.
Only use this if you are writing a file importer, that is responsible for
processing file import jobs.
"""
metrics.track(metrics.SDK, self.account, {"name": "File Import Finish Job"})
return super().finish_file_import_job(input)
@override
def get_model_file_import_jobs(
self,
*,
project_id: str,
model_id: str,
limit: int = 25,
cursor: str | None = None,
) -> ResourceCollection[FileImport]:
metrics.track(metrics.SDK, self.account, {"name": "File Import Get Model Jobs"})
return super().get_model_file_import_jobs(
project_id=project_id, model_id=model_id, limit=limit, cursor=cursor
)
@@ -1,57 +0,0 @@
from specklepy.core.api.inputs.model_ingestion_inputs import (
ModelIngestionCancelledInput,
ModelIngestionCreateInput,
ModelIngestionFailedInput,
ModelIngestionRequeueInput,
ModelIngestionStartProcessingInput,
ModelIngestionSuccessInput,
ModelIngestionUpdateInput,
)
from specklepy.core.api.models.current import (
ModelIngestion,
)
from specklepy.core.api.resources import (
ModelIngestionResource as CoreResource,
)
from specklepy.logging import metrics
class ModelIngestionResource(CoreResource):
"""API Access class for model ingestion"""
def __init__(self, account, basepath, client, server_version) -> None:
super().__init__(account, basepath, client, server_version)
def create(self, input: ModelIngestionCreateInput) -> ModelIngestion:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion Create"})
return super().create(input)
def get_ingestion(self, project_id: str, model_ingestion_id: str) -> ModelIngestion:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion Get"})
return super().get_ingestion(project_id, model_ingestion_id)
def update_progress(self, input: ModelIngestionUpdateInput) -> ModelIngestion:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion Update"})
return super().update_progress(input)
def start_processing(
self, input: ModelIngestionStartProcessingInput
) -> ModelIngestion:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion Start Processing"})
return super().start_processing(input)
def requeue(self, input: ModelIngestionRequeueInput) -> ModelIngestion:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion Update"})
return super().requeue(input)
def complete(self, input: ModelIngestionSuccessInput) -> str:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion End"})
return super().complete(input)
def fail_with_error(self, input: ModelIngestionFailedInput) -> ModelIngestion:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion Error"})
return super().fail_with_error(input)
def fail_with_cancel(self, input: ModelIngestionCancelledInput) -> ModelIngestion:
metrics.track(metrics.SDK, self.account, {"name": "Ingestion Cancel"})
return super().fail_with_cancel(input)
@@ -8,10 +8,6 @@ from specklepy.core.api.inputs.model_inputs import (
)
from specklepy.core.api.inputs.project_inputs import ProjectModelsFilter
from specklepy.core.api.models import Model, ModelWithVersions, ResourceCollection
from specklepy.core.api.models.current import (
ModelPermissionChecks,
PermissionCheckResult,
)
from specklepy.core.api.resources import ModelResource as CoreResource
from specklepy.logging import metrics
@@ -76,17 +72,3 @@ class ModelResource(CoreResource):
def update(self, input: UpdateModelInput) -> Model:
metrics.track(metrics.SDK, self.account, {"name": "Model Update"})
return super().update(input)
def get_permissions(self, model_id: str, project_id: str) -> ModelPermissionChecks:
metrics.track(metrics.SDK, self.account, {"name": "Model Get Permissions"})
return super().get_permissions(model_id, project_id)
def can_create_model_ingestion(
self, model_id: str, project_id: str
) -> PermissionCheckResult:
metrics.track(
metrics.SDK,
self.account,
{"name": "Model Get Permissions canCreateIngestion"},
)
return super().can_create_model_ingestion(model_id, project_id)
+15 -42
View File
@@ -11,8 +11,6 @@ from gql.transport.websockets import WebsocketsTransport
from specklepy.core.api.credentials import Account
from specklepy.core.api.resources import (
ActiveUserResource,
FileImportResource,
ModelIngestionResource,
ModelResource,
OtherUserResource,
ProjectInviteResource,
@@ -132,19 +130,6 @@ class SpeckleClient:
self.account = Account.from_token(token, self.url)
self._set_up_client()
userData = self.active_user.get()
# None if the token lacked the profile:read scope or if it was None
if userData:
self.account.userInfo.id = userData.id
self.account.userInfo.email = userData.email
self.account.userInfo.name = userData.name
self.account.userInfo.company = userData.company
self.account.userInfo.avatar = userData.avatar
self.account.serverInfo = self.server.get()
self.account.serverInfo.url = self.url
def authenticate_with_account(self, account: Account) -> None:
"""Authenticate the client using an Account object
The account is saved in the client object and a synchronous GraphQL
@@ -157,21 +142,6 @@ class SpeckleClient:
self.account = account
self._set_up_client()
try:
_ = self.active_user.get()
except SpeckleException as ex:
if isinstance(ex.exception, TransportServerError):
if ex.exception.code == 403:
warn(
SpeckleWarning(
"Possibly invalid token - could not authenticate "
f"Speckle Client for server {self.url}"
),
stacklevel=2,
)
else:
raise ex
def _set_up_client(self) -> None:
headers = {
"Authorization": f"Bearer {self.account.token}",
@@ -191,6 +161,21 @@ class SpeckleClient:
self._init_resources()
try:
_ = self.active_user.get()
except SpeckleException as ex:
if isinstance(ex.exception, TransportServerError):
if ex.exception.code == 403:
warn(
SpeckleWarning(
"Possibly invalid token - could not authenticate "
f"Speckle Client for server {self.url}"
),
stacklevel=2,
)
else:
raise ex
def execute_query(self, query: str) -> Dict:
return self.httpclient.execute(query)
@@ -245,18 +230,6 @@ class SpeckleClient:
client=self.httpclient,
server_version=server_version,
)
self.file_import = FileImportResource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.model_ingestion = ModelIngestionResource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.subscription = SubscriptionResource(
account=self.account,
basepath=self.ws_url,
+13 -13
View File
@@ -1,6 +1,6 @@
import os
from pathlib import Path
from typing import List
from typing import List, Optional
from urllib.parse import urlparse
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
@@ -12,20 +12,20 @@ from specklepy.transports.sqlite import SQLiteTransport
class UserInfo(BaseModel):
id: str | None = None
name: str | None = None
email: str | None = None
company: str | None = None
avatar: str | None = None
id: Optional[str] = None
name: Optional[str] = None
email: Optional[str] = None
company: Optional[str] = None
avatar: Optional[str] = None
class Account(BaseModel):
isDefault: bool = False
token: str | None = None
refreshToken: str | None = None
token: Optional[str] = None
refreshToken: Optional[str] = None
serverInfo: ServerInfo = Field(default_factory=ServerInfo)
userInfo: UserInfo = Field(default_factory=UserInfo)
id: str | None = None
id: Optional[str] = None
def __repr__(self) -> str:
return (
@@ -37,13 +37,13 @@ class Account(BaseModel):
return self.__repr__()
@classmethod
def from_token(cls, token: str, server_url: str | None = None):
def from_token(cls, token: str, server_url: str = None):
acct = cls(token=token)
acct.serverInfo.url = server_url
return acct
def get_local_accounts(base_path: str | None = None) -> List[Account]:
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
"""Gets all the accounts present in this environment
Arguments:
@@ -93,7 +93,7 @@ def get_local_accounts(base_path: str | None = None) -> List[Account]:
return accounts
def get_default_account(base_path: str | None = None) -> Account | None:
def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
"""
Gets this environment's default account if any. If there is no default,
the first found will be returned and set as default.
@@ -116,7 +116,7 @@ def get_default_account(base_path: str | None = None) -> Account | None:
return default
def get_account_from_token(token: str, server_url: str | None = None) -> Account:
def get_account_from_token(token: str, server_url: str = None) -> Account:
"""Gets the local account for the token if it exists
Arguments:
token {str} -- the api token
-16
View File
@@ -7,7 +7,6 @@ class ProjectVisibility(str, Enum):
PRIVATE = "PRIVATE"
PUBLIC = "PUBLIC"
UNLISTED = "UNLISTED"
"""Deprecated, use PUBLIC instead"""
WORKSPACE = "WORKSPACE"
@@ -31,18 +30,3 @@ class ProjectVersionsUpdatedMessageType(str, Enum):
CREATED = "CREATED"
DELETED = "DELETED"
UPDATED = "UPDATED"
class ProjectModelIngestionUpdatedMessageType(str, Enum):
CANCELLATION_REQUESTED = "cancellationRequested"
CREATED = "created"
DELETED = "deleted"
UPDATED = "updated"
class ModelIngestionStatus(str, Enum):
CANCELLED = "cancelled"
FAILED = "failed"
PROCESSING = "processing"
QUEUED = "queued"
SUCCESS = "success"
-12
View File
@@ -1,10 +1,3 @@
from specklepy.core.api.inputs.file_import_inputs import (
FileImportErrorInput,
FileImportSuccessInput,
FinishFileImportInput,
GenerateFileUploadUrlInput,
StartFileImportInput,
)
from specklepy.core.api.inputs.model_inputs import (
CreateModelInput,
DeleteModelInput,
@@ -29,11 +22,6 @@ from specklepy.core.api.inputs.version_inputs import (
)
__all__ = [
"FileImportErrorInput",
"FileImportSuccessInput",
"FinishFileImportInput",
"StartFileImportInput",
"GenerateFileUploadUrlInput",
"CreateModelInput",
"DeleteModelInput",
"UpdateModelInput",
@@ -1,44 +0,0 @@
from typing import Literal
from pydantic import Field
from specklepy.core.api.models.graphql_base_model import GraphQLBaseModel
class GenerateFileUploadUrlInput(GraphQLBaseModel):
project_id: str
file_name: str
class StartFileImportInput(GraphQLBaseModel):
project_id: str
model_id: str
file_id: str
etag: str
class FileImportResult(GraphQLBaseModel):
duration_seconds: float
download_duration_seconds: float
parse_duration_seconds: float
parser: str
version_id: str | None
class FileImportInputBase(GraphQLBaseModel):
project_id: str
job_id: str
warnings: list[str] = Field(default_factory=list)
result: FileImportResult
class FileImportSuccessInput(FileImportInputBase):
status: Literal["success"] = "success"
class FileImportErrorInput(FileImportInputBase):
status: Literal["error"] = "error"
reason: str
FinishFileImportInput = FileImportSuccessInput | FileImportErrorInput
@@ -1,78 +0,0 @@
from specklepy.core.api.enums import ProjectModelIngestionUpdatedMessageType
from specklepy.core.api.models.graphql_base_model import GraphQLBaseModel
class SourceDataInput(GraphQLBaseModel):
source_application_slug: str
source_application_version: str
file_name: str | None
file_size_bytes: int | None
class ModelIngestionCreateInput(GraphQLBaseModel):
model_id: str
project_id: str
progress_message: str
source_data: SourceDataInput
max_idle_timeout_seconds: int | None = None
class ModelIngestionStartProcessingInput(GraphQLBaseModel):
ingestion_id: str
project_id: str
progress_message: str
source_data: SourceDataInput
class ModelIngestionRequeueInput(GraphQLBaseModel):
ingestion_id: str
project_id: str
progress_message: str
class ModelIngestionUpdateInput(GraphQLBaseModel):
ingestion_id: str
project_id: str
progress: float | None
progress_message: str
class ModelIngestionSuccessInput(GraphQLBaseModel):
ingestion_id: str
project_id: str
root_object_id: str
version_message: str | None
class ModelIngestionFailedInput(GraphQLBaseModel):
ingestion_id: str
project_id: str
error_reason: str
error_stacktrace: str | None
class ModelIngestionCancelledInput(GraphQLBaseModel):
ingestion_id: str
project_id: str
cancellation_message: str
class ModelIngestionRequestCancellationInput(GraphQLBaseModel):
ingestion_id: str
project_id: str
cancellation_message: str
class ModelIngestionReference(GraphQLBaseModel):
"""
`@oneOf` i.e. server expects **either** `ingestion_id` or `model_id`, but not both.
"""
ingestion_id: str | None
model_id: str | None
class ProjectModelIngestionSubscriptionInput(GraphQLBaseModel):
project_id: str
ingestion_reference: ModelIngestionReference
message_type: ProjectModelIngestionUpdatedMessageType | None = None
@@ -34,8 +34,4 @@ class MarkReceivedVersionInput(GraphQLBaseModel):
version_id: str
project_id: str
source_application: str
"""
IMPORTANT: this is meant to be the slug of the application that has done the
receiving, not to be confused with `Version.sourceApplication`
"""
message: Optional[str] = None
@@ -1,7 +1,5 @@
from specklepy.core.api.models.current import (
AuthStrategy,
FileImport,
FileUploadUrl,
LimitedUser,
Model,
ModelWithVersions,
@@ -50,6 +48,4 @@ __all__ = [
"ProjectModelsUpdatedMessage",
"ProjectUpdatedMessage",
"ProjectVersionsUpdatedMessage",
"FileImport",
"FileUploadUrl",
]
+49 -87
View File
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import Generic, List, TypeVar
from typing import Generic, List, Optional, TypeVar
from specklepy.core.api.enums import ModelIngestionStatus, ProjectVisibility
from specklepy.core.api.enums import ProjectVisibility
from specklepy.core.api.models.graphql_base_model import GraphQLBaseModel
from specklepy.logging.exceptions import WorkspacePermissionException
@@ -10,13 +10,13 @@ T = TypeVar("T")
class User(GraphQLBaseModel):
id: str
email: str | None = None
email: Optional[str] = None
name: str
bio: str | None = None
company: str | None = None
avatar: str | None = None
verified: bool | None = None
role: str | None = None
bio: Optional[str] = None
company: Optional[str] = None
avatar: Optional[str] = None
verified: Optional[bool] = None
role: Optional[str] = None
def __repr__(self):
return (
@@ -31,16 +31,16 @@ class User(GraphQLBaseModel):
class ResourceCollection(GraphQLBaseModel, Generic[T]):
total_count: int
items: List[T]
cursor: str | None = None
cursor: Optional[str] = None
class ServerMigration(GraphQLBaseModel):
moved_from: str | None
moved_to: str | None
moved_from: Optional[str]
moved_to: Optional[str]
class AuthStrategy(GraphQLBaseModel):
color: str | None
color: Optional[str]
icon: str
id: str
name: str
@@ -60,17 +60,17 @@ class ServerWorkspacesInfo(GraphQLBaseModel):
# Keeping this one all Optionals at the minute,
# because its used both as a deserialization model for GQL and Account Management
class ServerInfo(GraphQLBaseModel):
name: str | None = None
company: str | None = None
url: str | None = None
admin_contact: str | None = None
description: str | None = None
canonical_url: str | None = None
scopes: List[dict] | None = None
auth_strategies: List[dict] | None = None
version: str | None = None
migration: ServerMigration | None = None
workspaces: ServerWorkspacesInfo | None = None
name: Optional[str] = None
company: Optional[str] = None
url: Optional[str] = None
admin_contact: Optional[str] = None
description: Optional[str] = None
canonical_url: Optional[str] = None
scopes: Optional[List[dict]] = None
auth_strategies: Optional[List[dict]] = None
version: Optional[str] = None
migration: Optional[ServerMigration] = None
workspaces: Optional[ServerWorkspacesInfo] = None
# TODO separate gql model from account management model
@@ -79,11 +79,11 @@ class LimitedUser(GraphQLBaseModel):
id: str
name: str
bio: str | None
company: str | None
avatar: str | None
verified: bool | None
role: str | None
bio: Optional[str]
company: Optional[str]
avatar: Optional[str]
verified: Optional[bool]
role: Optional[str]
def __repr__(self):
return (
@@ -99,15 +99,15 @@ class LimitedUser(GraphQLBaseModel):
class PendingStreamCollaborator(GraphQLBaseModel):
id: str
invite_id: str
stream_id: str | None = None
stream_id: Optional[str] = None
projectId: str
stream_name: str | None = None
stream_name: Optional[str] = None
project_name: str
title: str
role: str
invited_by: LimitedUser | None = None
user: LimitedUser | None = None
token: str | None
invited_by: LimitedUser
user: Optional[LimitedUser] = None
token: Optional[str]
def __repr__(self):
return (
@@ -127,30 +127,24 @@ class ProjectCollaborator(GraphQLBaseModel):
class Version(GraphQLBaseModel):
author_user: LimitedUser | None
author_user: Optional[LimitedUser]
created_at: datetime
id: str
message: str | None
message: Optional[str]
preview_url: str
referenced_object: str | None
referenced_object: Optional[str]
"""Maybe null if workspaces version history limit has been exceeded"""
source_application: str | None
class ModelPermissionChecks(GraphQLBaseModel):
can_update: "PermissionCheckResult"
can_delete: "PermissionCheckResult"
can_create_version: "PermissionCheckResult"
source_application: Optional[str]
class Model(GraphQLBaseModel):
author: LimitedUser | None
author: Optional[LimitedUser]
created_at: datetime
description: str | None
description: Optional[str]
display_name: str
id: str
name: str
preview_url: str | None
preview_url: Optional[str]
updated_at: datetime
@@ -162,19 +156,20 @@ class ProjectPermissionChecks(GraphQLBaseModel):
can_create_model: "PermissionCheckResult"
can_delete: "PermissionCheckResult"
can_load: "PermissionCheckResult"
can_publish: "PermissionCheckResult"
class Project(GraphQLBaseModel):
allow_public_comments: bool
created_at: datetime
description: str | None
description: Optional[str]
id: str
name: str
role: str | None
role: Optional[str]
source_apps: List[str]
updated_at: datetime
visibility: ProjectVisibility
workspace_id: str | None
workspace_id: Optional[str]
class ProjectWithModels(Project):
@@ -196,7 +191,7 @@ class ProjectCommentCollection(ResourceCollection[T], Generic[T]):
class UserSearchResultCollection(GraphQLBaseModel):
items: List[LimitedUser]
cursor: str | None = None
cursor: Optional[str] = None
class PermissionCheckResult(GraphQLBaseModel):
@@ -221,48 +216,15 @@ class WorkspaceCreationState(GraphQLBaseModel):
class LimitedWorkspace(GraphQLBaseModel):
id: str
name: str
role: str | None
role: Optional[str]
slug: str
logo: str | None
description: str | None
logo: Optional[str]
description: Optional[str]
class Workspace(LimitedWorkspace):
created_at: datetime
updated_at: datetime
read_only: bool
creation_state: WorkspaceCreationState | None
creation_state: Optional[WorkspaceCreationState]
permissions: WorkspacePermissionChecks
class FileImport(GraphQLBaseModel):
id: str
project_id: str
converted_version_id: str | None
user_id: str
converted_status: int
converted_message: str | None
model_id: str | None
updated_at: datetime
class FileUploadUrl(GraphQLBaseModel):
url: str
file_id: str
class ModelIngestionStatusData(GraphQLBaseModel):
status: ModelIngestionStatus
progress_message: str | None = None
version_id: str | None = None
class ModelIngestion(GraphQLBaseModel):
id: str
created_at: datetime
updated_at: datetime
model_id: str
project_id: str
user_id: str
cancellation_requested: bool
status_data: ModelIngestionStatusData
@@ -1,13 +1,12 @@
from typing import Optional
from specklepy.core.api.enums import (
ProjectModelIngestionUpdatedMessageType,
ProjectModelsUpdatedMessageType,
ProjectUpdatedMessageType,
ProjectVersionsUpdatedMessageType,
UserProjectsUpdatedMessageType,
)
from specklepy.core.api.models.current import Model, ModelIngestion, Project, Version
from specklepy.core.api.models.current import Model, Project, Version
from specklepy.core.api.models.graphql_base_model import GraphQLBaseModel
@@ -34,8 +33,3 @@ class ProjectVersionsUpdatedMessage(GraphQLBaseModel):
type: ProjectVersionsUpdatedMessageType
model_id: str
version: Optional[Version]
class ProjectModelIngestionUpdatedMessage(GraphQLBaseModel):
model_ingestion: ModelIngestion
type: ProjectModelIngestionUpdatedMessageType
@@ -1,8 +1,4 @@
from specklepy.core.api.resources.current.active_user_resource import ActiveUserResource
from specklepy.core.api.resources.current.file_import_resource import FileImportResource
from specklepy.core.api.resources.current.model_ingestion_resource import (
ModelIngestionResource,
)
from specklepy.core.api.resources.current.model_resource import ModelResource
from specklepy.core.api.resources.current.other_user_resource import OtherUserResource
from specklepy.core.api.resources.current.project_invite_resource import (
@@ -17,7 +13,6 @@ from specklepy.core.api.resources.current.version_resource import VersionResourc
from specklepy.core.api.resources.current.workspace_resource import WorkspaceResource
__all__ = [
"FileImportResource",
"ActiveUserResource",
"ModelResource",
"OtherUserResource",
@@ -27,6 +22,4 @@ __all__ = [
"SubscriptionResource",
"VersionResource",
"WorkspaceResource",
"FileImportResource",
"ModelIngestionResource",
]
@@ -1,214 +0,0 @@
from pathlib import Path
from typing import Any
import httpx
from gql import Client, gql
from specklepy.core.api.credentials import Account
from specklepy.core.api.inputs.file_import_inputs import (
FinishFileImportInput,
GenerateFileUploadUrlInput,
StartFileImportInput,
)
from specklepy.core.api.models import FileImport, FileUploadUrl, ResourceCollection
from specklepy.core.api.models.graphql_base_model import GraphQLBaseModel
from specklepy.core.api.resource import ResourceBase
from specklepy.core.api.responses import DataResponse
from specklepy.logging.exceptions import SpeckleException
NAME = "file_import"
class UploadFileResponse(GraphQLBaseModel):
etag: str
class FileImportResource(ResourceBase):
"""API Access class for file imports"""
def __init__(
self,
account: Account,
basepath: str,
client: Client,
server_version: tuple[Any, ...] | None, # pyright: ignore[reportExplicitAny]
) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
server_version=server_version,
name=NAME,
)
def finish_file_import_job(self, input: FinishFileImportInput) -> bool:
"""
This is mostly an internal api, that marks a file import job finished.
Only use this if you are writing a file importer, that is responsible for
processing file import jobs.
"""
QUERY = gql(
"""
mutation FinishFileImport($input: FinishFileImportInput!) {
data:fileUploadMutations {
data:finishFileImport(input: $input)
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[bool]], QUERY, variables
).data.data
def start_file_import(self, input: StartFileImportInput) -> FileImport:
QUERY = gql(
"""
mutation StartFileImport($input: StartFileImportInput!) {
data:fileUploadMutations {
data:startFileImport(input: $input) {
id
projectId
convertedVersionId
userId
convertedStatus
convertedMessage
modelId
updatedAt
}
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[FileImport]], QUERY, variables
).data.data
def generate_upload_url(self, input: GenerateFileUploadUrlInput) -> FileUploadUrl:
"""
Get a file upload url from the Speckle server.
This method asks the server to create a pre-signed S3 url,
which can be used as a short term authenticated route,
to put a file to the server.
"""
QUERY = gql(
"""
mutation GenerateUploadUrl($input: GenerateFileUploadUrlInput!) {
data:fileUploadMutations {
data:generateUploadUrl(input: $input) {
fileId
url
}
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[FileUploadUrl]], QUERY, variables
).data.data
def upload_file(self, file: Path, url: str) -> UploadFileResponse:
"""
Uploads a file to the given S3 url.
This method should be used together with the generate_upload_url method,
which generates a pre-signed S3 url, that can be used to upload the file to.
"""
with open(file, "rb") as content:
response = httpx.put(
url,
content=content, # Pass file object directly for streaming
headers={
"Content-Type": "application/octet-stream",
"Content-Length": str(file.stat().st_size),
},
).raise_for_status()
etag = response.headers.get("ETag", None) # pyright: ignore[reportAny]
if not etag:
raise SpeckleException(
"Response does not have an ETag attached to it,"
+ " cannot use this as an upload"
)
return UploadFileResponse(etag=str(etag)) # pyright: ignore[reportAny]
def download_file(self, project_id: str, file_id: str, target_file: Path) -> Path:
"""Download a file blob attached to the project, to the target path."""
if not target_file.parent.exists():
target_file.parent.mkdir(parents=True)
url = f"{self.basepath}/api/stream/{project_id}/blob/{file_id}"
with httpx.stream(
"GET", url, headers={"Authorization": f"Bearer {self.account.token}"}
) as response:
_ = response.raise_for_status()
with target_file.open("wb") as f:
for chunk in response.iter_bytes(chunk_size=8192):
_ = f.write(chunk)
return target_file
def get_model_file_import_jobs(
self,
*,
project_id: str,
model_id: str,
limit: int = 25,
cursor: str | None = None,
) -> ResourceCollection[FileImport]:
QUERY = gql(
"""
query ModelFileImportJobs(
$projectId: String!,
$modelId: String!,
$input: GetModelUploadsInput
) {
data:project(id: $projectId) {
data:model(id: $modelId) {
data:uploads(input: $input) {
totalCount
cursor
items {
id
projectId
convertedVersionId
userId
convertedStatus
convertedMessage
modelId
updatedAt
}
}
}
}
}
"""
)
variables = {
"projectId": project_id,
"modelId": model_id,
"input": {
"limit": limit,
"cursor": cursor,
},
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ResourceCollection[FileImport]]]],
QUERY,
variables,
).data.data.data
@@ -1,417 +0,0 @@
from typing import Any, Tuple
from gql import Client, gql
from specklepy.api.credentials import Account
from specklepy.core.api.inputs.model_ingestion_inputs import (
ModelIngestionCancelledInput,
ModelIngestionCreateInput,
ModelIngestionFailedInput,
ModelIngestionRequestCancellationInput,
ModelIngestionRequeueInput,
ModelIngestionStartProcessingInput,
ModelIngestionSuccessInput,
ModelIngestionUpdateInput,
)
from specklepy.core.api.models.current import (
ModelIngestion,
)
from specklepy.core.api.resource import ResourceBase
from specklepy.core.api.responses import DataResponse
NAME = "ingestion"
class ModelIngestionResource(ResourceBase):
"""API Access class for model ingestion"""
def __init__(
self,
account: Account,
basepath: str,
client: Client,
server_version: Tuple[Any, ...] | None,
) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
def get_ingestion(self, project_id: str, model_ingestion_id: str) -> ModelIngestion:
QUERY = gql(
"""
query Query($projectId: String!, $modelIngestionId: ID!) {
data:project(id: $projectId) {
data:ingestion(id: $modelIngestionId) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
... on ModelIngestionSuccessStatus
{
versionId
}
}
}
}
}
""" # noqa: E501
)
variables = {
"projectId": project_id,
"modelIngestionId": model_ingestion_id,
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[ModelIngestion]],
QUERY,
variables,
).data.data
def create(self, input: ModelIngestionCreateInput) -> ModelIngestion:
QUERY = gql(
"""
mutation IngestionCreate($input: ModelIngestionCreateInput!) {
data: projectMutations {
data: modelIngestionMutations {
data: create(input: $input) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
}
}
}
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ModelIngestion]]], QUERY, variables
).data.data.data
def start_processing(
self, input: ModelIngestionStartProcessingInput
) -> ModelIngestion:
QUERY = gql(
"""
mutation IngestionStartProcessing($input: ModelIngestionStartProcessingInput!) {
data: projectMutations {
data: modelIngestionMutations {
data: startProcessing(input: $input) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
}
}
}
}
}
""" # noqa: E501
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ModelIngestion]]], QUERY, variables
).data.data.data
def requeue(self, input: ModelIngestionRequeueInput) -> ModelIngestion:
QUERY = gql(
"""
mutation IngestionRequeue($input: ModelIngestionRequeueInput!) {
data: projectMutations {
data: modelIngestionMutations {
data: requeue(input: $input) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
}
}
}
}
}
""" # noqa: E501
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ModelIngestion]]], QUERY, variables
).data.data.data
def update_progress(self, input: ModelIngestionUpdateInput) -> ModelIngestion:
QUERY = gql(
"""
mutation IngestionUpdateProgress(
$input: ModelIngestionUpdateInput!
) {
data: projectMutations {
data: modelIngestionMutations {
data: updateProgress(input: $input) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
}
}
}
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ModelIngestion]]], QUERY, variables
).data.data.data
def complete(self, input: ModelIngestionSuccessInput) -> str:
"""
Request that the server completes the ingestion by creating a version
If successful, the job will be in a terminal "successful" state.
For failed Ingestions, use `fail_with_error` instead
For user cancellation, use `fail_with_cancelled` instead
Arguments:
input {ModelIngestionSuccessInput} -- input variable
Returns:
str -- the id of the version that was just created to complete the ingestion
"""
QUERY = gql(
"""
mutation IngestionComplete($input: ModelIngestionSuccessInput!) {
data: projectMutations {
data: modelIngestionMutations {
data: completeWithVersion(input: $input) {
data:statusData {
... on ModelIngestionSuccessStatus {
data:versionId
}
}
}
}
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[DataResponse[DataResponse[str]]]]],
QUERY,
variables,
).data.data.data.data.data
def fail_with_error(self, input: ModelIngestionFailedInput) -> ModelIngestion:
"""
Fail the job with an error.
For user requested cancellation, use `fail_with_cancelled` instead
"""
QUERY = gql(
"""
mutation IngestionFailWithError($input: ModelIngestionFailedInput!) {
data: projectMutations {
data: modelIngestionMutations {
data: failWithError(input: $input) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
}
}
}
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ModelIngestion]]],
QUERY,
variables,
).data.data.data
def fail_with_cancel(self, input: ModelIngestionCancelledInput) -> ModelIngestion:
"""
Fail the ingestion with a `cancelled` status.
This should only be done if the user has explicitly requested cancellation
Other forms of cancellation use `fail_with_error`
The ingestion should then enter a terminal "canceled" state
"""
QUERY = gql(
"""
mutation IngestionFailWithCancel($input: ModelIngestionCancelledInput!) {
data: projectMutations {
data: modelIngestionMutations {
data: failWithCancel(input: $input) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
}
}
}
}
}
"""
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ModelIngestion]]],
QUERY,
variables,
).data.data.data
def request_cancellation(
self, input: ModelIngestionRequestCancellationInput
) -> ModelIngestion:
"""
Request that the ingestion is canceled.
Note: simply calling this mutation does not immediately cancel,
it doesn't even guarantee it will be canceled at all.
It's up to the client to observe this cancellation request
via `subscription.project_model_ingestion_cancellation_requested`
and report it as cancelled (via `ingestion.fail_with_cancel`
See "cooperative cancellation pattern"
"""
QUERY = gql(
"""
mutation IngestionRequestCancellation($input: ModelIngestionRequestCancellationInput!) {
data: projectMutations {
data: modelIngestionMutations {
data: requestCancellation (input: $input) {
id
createdAt
updatedAt
modelId
projectId
userId
cancellationRequested
statusData {
... on HasModelIngestionStatus {
status
}
... on HasProgressMessage {
progressMessage
}
}
}
}
}
}
""" # noqa: E501
)
variables = {
"input": input.model_dump(warnings="error", by_alias=True),
}
return self.make_request_and_parse_response(
DataResponse[DataResponse[DataResponse[ModelIngestion]]],
QUERY,
variables,
).data.data.data

Some files were not shown because too many files have changed in this diff Show More