Skip to content

Commit 455774e

Browse files
authored
feat(protocol-designer): add declaration for def run() to generated Python (#17482)
# Overview This adds the `def run(...)` declaration to the generated Python file. AUTH-1092 We'll start filling in the `run()` function in future PRs. ## Test Plan and Hands on Testing Updated unit tests. Looked at generated file with feature flag turned on. ## Risk assessment Low. This just affects the exported Python file hidden under a feature flag.
1 parent a8c1f16 commit 455774e

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

protocol-designer/src/file-data/__tests__/createFile.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ requirements = {
119119
"robotType": "OT-2",
120120
"apiLevel": "2.23",
121121
}
122+
123+
def run(protocol: protocol_api.ProtocolContext):
124+
pass
122125
`.trimStart()
123126
)
124127
})

protocol-designer/src/file-data/selectors/fileCreator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ import {
2626
getModulesLoadInfo,
2727
getPipettesLoadInfo,
2828
} from './utils'
29-
import { pythonImports, pythonMetadata, pythonRequirements } from './pythonFile'
29+
import {
30+
pythonDefRun,
31+
pythonImports,
32+
pythonMetadata,
33+
pythonRequirements,
34+
} from './pythonFile'
3035

3136
import type { SecondOrderCommandAnnotation } from '@opentrons/shared-data/commandAnnotation/types'
3237
import type {
@@ -310,6 +315,7 @@ export const createPythonFile: Selector<string> = createSelector(
310315
pythonImports(),
311316
pythonMetadata(fileMetadata),
312317
pythonRequirements(robotType),
318+
pythonDefRun(),
313319
]
314320
.filter(section => section) // skip any blank sections
315321
.join('\n\n') + '\n'

protocol-designer/src/file-data/selectors/pythonFile.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/** Generate sections of the Python file for fileCreator.ts */
22

33
import { FLEX_ROBOT_TYPE, OT2_ROBOT_TYPE } from '@opentrons/shared-data'
4-
import { formatPyDict } from '@opentrons/step-generation'
4+
import {
5+
formatPyDict,
6+
indentPyLines,
7+
PROTOCOL_CONTEXT_NAME,
8+
} from '@opentrons/step-generation'
59
import type { FileMetadataFields } from '../types'
610
import type { RobotType } from '@opentrons/shared-data'
711

@@ -46,3 +50,22 @@ export function pythonRequirements(robotType: RobotType): string {
4650
}
4751
return `requirements = ${formatPyDict(requirements)}`
4852
}
53+
54+
export function pythonDefRun(): string {
55+
const sections: string[] = [
56+
// loadModules(),
57+
// loadLabware(),
58+
// loadInstruments(),
59+
// defineLiquids(),
60+
// loadLiquids(),
61+
// stepCommands(),
62+
]
63+
const functionBody =
64+
sections
65+
.filter(section => section) // skip empty sections
66+
.join('\n\n') || 'pass'
67+
return (
68+
`def run(${PROTOCOL_CONTEXT_NAME}: protocol_api.ProtocolContext):\n` +
69+
`${indentPyLines(functionBody)}`
70+
)
71+
}

step-generation/src/utils/pythonFormat.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/** Utility functions for Python code generation. */
22

3+
/** The variable name for the ProtocolContext object in the run() function.
4+
* Our docs call it `protocol`, which is slightly misleading since the object is not
5+
* the protocol itself, but we'll try to stay consistent with the docs.
6+
*/
7+
export const PROTOCOL_CONTEXT_NAME = 'protocol'
8+
39
const INDENT = ' '
410

511
/** Indent each of the lines in `text`. */

0 commit comments

Comments
 (0)