File tree Expand file tree Collapse file tree 5 files changed +50
-8
lines changed Expand file tree Collapse file tree 5 files changed +50
-8
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,8 @@ import { MRC_STYLE_CLASS_BLOCKS } from '../themes/styles';
2424import { createFieldNonEditableText } from '../fields/FieldNonEditableText'
2525import * as ChangeFramework from './utils/change_framework'
2626import { getLegalName } from './utils/python' ;
27+ import { Order } from 'blockly/python' ;
28+ import { ExtendedPythonGenerator } from '../editor/extended_python_generator' ;
2729
2830export const BLOCK_NAME = 'mrc_class_method_def' ;
2931
@@ -351,10 +353,6 @@ export const setup = function() {
351353 Blockly . Blocks [ PARAM_CONTAINER_BLOCK_NAME ] = METHOD_PARAM_CONTAINER ;
352354} ;
353355
354- import { Order } from 'blockly/python' ;
355- import { ExtendedPythonGenerator } from '../editor/extended_python_generator' ;
356-
357-
358356export const pythonFromBlock = function (
359357 block : ClassMethodDefBlock ,
360358 generator : ExtendedPythonGenerator ,
@@ -394,7 +392,7 @@ export const pythonFromBlock = function (
394392 xfix2 = xfix1 ;
395393 }
396394 if ( block . mrcPythonMethodName == '__init__' ) {
397- branch = generator . INDENT + "self.mechanisms = []\n" + branch ;
395+ branch = generator . defineClassVariables ( block . workspace ) + branch ;
398396 }
399397 if ( returnValue ) {
400398 returnValue = generator . INDENT + 'return ' + returnValue + '\n' ;
Original file line number Diff line number Diff line change @@ -118,17 +118,16 @@ export const setup = function () {
118118 Blockly . Blocks [ BLOCK_NAME ] = MECHANISM_FUNCTION ;
119119}
120120
121- //TODO: This needs to cause our own init to create the mechanisms line
122121export const pythonFromBlock = function (
123122 mechanismBlock : MechanismBlock ,
124123 generator : ExtendedPythonGenerator ,
125124) {
126125 if ( mechanismBlock . mrcImportModule ) {
127126 generator . addImport ( mechanismBlock . mrcImportModule ) ;
128127 }
128+ generator . setHasMechanism ( ) ;
129129 let code = 'self.mechanisms["' + mechanismBlock . getFieldValue ( 'NAME' ) + '"] = '
130130 + mechanismBlock . getFieldValue ( 'TYPE' ) + '('
131-
132131 for ( let i = 0 ; i < mechanismBlock . mrcArgs . length ; i ++ ) {
133132 const fieldName = 'ARG' + i ;
134133 if ( i != 0 ) {
Original file line number Diff line number Diff line change 1- import * as Blockly from 'blockly' ;
21
32import * as CallPythonFunction from './mrc_call_python_function' ;
43import * as GetPythonEnumValue from './mrc_get_python_enum_value' ;
Original file line number Diff line number Diff line change @@ -39,6 +39,42 @@ export class ExtendedPythonGenerator extends PythonGenerator {
3939 super ( 'Python' ) ;
4040 }
4141
42+ init ( workspace : Blockly . Workspace ) {
43+ super . init ( workspace ) ;
44+ // This will have all variables in the definition 'variables' so we will need to make it contain only the developer variables
45+ delete this . definitions_ [ 'variables' ] ;
46+
47+ const defvars = [ ] ;
48+ // Add developer variables (not created or named by the user).
49+ const devVarList = Blockly . Variables . allDeveloperVariables ( workspace ) ;
50+ for ( let i = 0 ; i < devVarList . length ; i ++ ) {
51+ defvars . push (
52+ this . nameDB_ ! . getName ( devVarList [ i ] , Blockly . Names . DEVELOPER_VARIABLE_TYPE ) + ' = None' ,
53+ ) ;
54+ }
55+ this . definitions_ [ 'variables' ] = defvars . join ( '\n' ) ;
56+ }
57+
58+ /*
59+ * This is called from the python generator for the mrc_class_method_def for the
60+ * init method
61+ */
62+ defineClassVariables ( workspace : Blockly . Workspace ) : string {
63+ let variableDefinitions = '' ;
64+
65+ if ( this . context ?. getHasMechanisms ( ) ) {
66+ variableDefinitions += this . INDENT + "self.mechanisms = []\n" ;
67+ }
68+ return variableDefinitions ;
69+ }
70+ getVariableName ( nameOrId : string ) : string {
71+ const varName = super . getVariableName ( name ) ;
72+ return "self." + varName ;
73+ }
74+ setHasMechanism ( ) : void {
75+ this . context ?. setHasMechanism ( ) ;
76+ }
77+
4278 workspaceToCode ( workspace : Blockly . Workspace , context : GeneratorContext ) : string {
4379 this . workspace = workspace ;
4480 this . context = context ;
Original file line number Diff line number Diff line change @@ -36,6 +36,9 @@ export class GeneratorContext {
3636 // Key is the mrc_class_method_def block's NAME field, value is the python method name.
3737 private classMethodNames : { [ key : string ] : string } = Object . create ( null ) ;
3838
39+ // Has mechanisms (ie, needs in init)
40+ private hasMechanisms = false ;
41+
3942 setModule ( module : commonStorage . Module | null ) {
4043 this . module = module ;
4144 this . clear ( ) ;
@@ -44,6 +47,13 @@ export class GeneratorContext {
4447 clear ( ) : void {
4548 this . clearExportedBlocks ( ) ;
4649 this . clearClassMethodNames ( ) ;
50+ this . hasMechanisms = false ;
51+ }
52+ setHasMechanism ( ) :void {
53+ this . hasMechanisms = true ;
54+ }
55+ getHasMechanisms ( ) :boolean {
56+ return this . hasMechanisms ;
4757 }
4858
4959 getClassName ( ) : string {
You can’t perform that action at this time.
0 commit comments