-
Notifications
You must be signed in to change notification settings - Fork 668
Python template support 2.0 #8122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- introduced code regions - moved class properties to appropriate regions - fixed a few summary annotations to make it clearer what they do/return and adhere to Microsoft standards a bit better
- added public property to class for PythonTemplateFilePath - added default value in the Settings constructor
Implemented support for the file path, following the model of the PreferencesFilePath.
- when adding a Python node, the template is read - added conditional load & fallback on hardcoded text if template is not found - replaced "\n" with `Environment.NewLine` in hardcoded template - added new comment lines to default template, using Resources to make it easier for beginners - fixed typo in existing Resources & XAML
- added an extra spacing line - added spaces after `#` Python comment marker
The backing store is required as a static property cannot implement an interface member.
The backing store is private & static, so it doesn't get serialised when settings are saved. Hence, we add a public static method to access it.
gets rid of un-necessary initialisation of new `PreferenceSettings` or `PathManager` classes, see DynamoDS#8034 (review)
actually gets rid of un-necessary initialisation of new `PreferenceSettings` or `PathManager` classes, see DynamoDS#8034 (review) forgot the PathManager in last commit.
|
FYI @mjkkirschner , checked for dependency on changed |
.gitignore
Outdated
| test/core/customast/test.txt | ||
| test/core/files/test.png | ||
| test/core/migration/writetext.txt | ||
| src/AssemblySharedInfoGenerator/AssemblySharedInfo.cs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, but I believe there is a reason we don't have this as part of the gitignore, you can simply not commit changes to this file until we dig deeper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mjkkirschner , looked around and it was already part of the .gitignore, see a few lines above - my GitHub desktop app added it again when i ignored it. I untracked the AssemblySharedInfo.cs file in my local repo now, just need to do the same for this .gitignore file too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed it now 👍
removed duplicate file tracking for `AssemblySharedInfo.cs` https://github.com/DynamoDS/Dynamo/blob/8e8fe12f89150be1fe7f24edf9931859b59c731a/.gitignore#L74
| yield return Path.GetFullPath(library); | ||
| } | ||
|
|
||
| internal static string GetPythonTemplatePath() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@radumg thanks for looking into how to avoid the creation of a new pathManager, but this smells funny to me, and it is a bit inconsistent with other preferences which are similar, like the customPackageFolders paths.
I'm thinking we could follow a similar pattern like here:
Dynamo/src/DynamoCore/Models/DynamoModel.cs
Line 583 in 7304ba1
| if (PreferenceSettings.CustomPackageFolders.Count == 0) |
so we would assign the default path to "" in the preferences constructor and instead set it to path specified in the pathManager during DynamoModel creation time.
Let me know what you think of this - both seem okay on their own, but this seems a more consistent solution and we can avoid the static method on pathManager
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mjkkirschner - think your suggestion is good, it's indeed inconsistent with rest of props. I'll go ahead and implement the above, was just trying to avoid modifying the DynamoModel class/ctor as per our previous conversations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've implemented this in c4bff15
I added some logic so it follows this flow :
- tries to use template file specified in
DynamoSettings.xmlfirst - if that fails, it looks for the default
PythonTemplate.pyin the user folder. If this is found, the setting will be updated to point to it. - if both those fail, it will fall back on the hardcoded string.
Also added logging so if users are having a hard time getting their templates to be picked up (most likely due to invalid paths), they would have some indication of what's going on.
Let me know if this is what you had in mind and if there's any changes you'd like.
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the static Python template file path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this is the public entry point for most use cases lets add some description of what this file at this path is used for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, wasn't being descriptive enough 👍 . Added now, have a look please 👁
| </Reference> | ||
| <Reference Include="System" /> | ||
| <Reference Include="System.Core" /> | ||
| <Reference Include="System.Windows.Forms" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this reference still needed for something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, forgot to remove it - I had a MessageBox.Show() during testing. Will keep an eye out for these going forward.
It's now removed.
|
@radumg thanks for the work here. I notice there are no new tests. Would it be possible to add a test for this preference, like: which just asserts the deserialization is working correctly and then something which modifies the template then asserts the python script changes? A potential test location is here: https://github.com/DynamoDS/Dynamo/blob/master/test/Libraries/DynamoPythonTests/PythonEditTests.cs |
|
Thanks @mjkkirschner for the review. I thought about potential tests for a second but couldn't imagine any useful ones - obvs my imagination failed me 😁 - I can certainly try & add the tests you suggest above. I'm fairly new to TDD so I really appreciate the guidance! |
…oModel - removed static getter in PathManager and changed backing prop to instance - added handling of user Python file, default file or no template - added logging - added log messages to `*.resx` DynamoDS#8122 (review)
|
@mjkkirschner - if you happen to see this before i start working on it again tonight, i'm having a hard time adding any Dynamo node to the Just thought I'd check before I delve deeper, pretty sure I'm missing an obvious trick here 🤓 |
|
@radumg what you can do is use a createNode command and execute it on the dynamoModel
Another possibility is to make the python test assembly able to access internal members of other assemblies with |
- added XML settings files for tests - renamed existing settings file to fit in better with structure (easier to group & read by humans) - consolidated files for previous test `oadInvalidPythonTemplateFromSetting` in `Settings.cs`
- test passes locally - test requires 2 new python files, included them in same folder - test generates 1 additional settings file when run, `DynamoSettings-PythonTemplate-changed.XML` - test does not depend on hard-coded paths
these 2 tests were taking 10 minutes to run, had to disable them whilst building the new Python tests.
checking files required by test exist should be done before those files are read
|
Ok, I think I've figured out these tests 🎉 & 🤞 I've added 3 tests in total @mjkkirschner (screenshot only shows 2) : Test 1 : LoadInvalidPythonTemplateFromSetting in test/DynamoCoreTests/Settings.cs#39
Test 2 : CanReadPythonTemplateSetting in test/DynamoCoreTests/CoreTests.cs#55
Test 3 : CanUpdatePythonTemplateSettings in test/DynamoCoreTests/CoreTests.cs#77
It's ready for review again @mjkkirschner , thanks so much for the pointers ! |
|
@radumg this looks solid to me, will take another look and bring it in soon. |
|
@QilongTang tests pass- going to merge this in, lets watch the revit build. |
|
Awesome, just wanted to say thanks again @mjkkirschner for the guidance along the way! |
|
Thank you so much for this @radumg , it's working great. |
…oModel - removed static getter in PathManager and changed backing prop to instance - added handling of user Python file, default file or no template - added logging - added log messages to `*.resx` DynamoDS/Dynamo#8122 (review)
|
Thanks @radumg it's working on a network path :) Just an FYI, when I follow the instruction in the primer, if I reopen the DynamoSettings.xml it has changed: to: Thanks again, Mark |

Note : this PR replaces the previous PR #8034 as that was based on
1.3.1branch and not on master. Re-basing was messy, so cherry-picked to this new PR instead.Purpose
This PR fixes #7604 , wish for adding a Python template that can be used to populate any
Python Scriptnodes when adding to workspace.Current behaviour
Python Scriptnode to canvasNew behaviour
Python Scriptnode to canvasPythonTemplate.pyfile exists at the user location root (%appdata%/Dynamo/Core/{version}/) and if it does, it reads the file and populates the Python script node with its contentsBehaviour when template file is missing or empty :

Behaviour when template file is found :

This allows users and organisations to rollout the template as part of users profiles or as logon scripts.
Implementation
PreferenceSettingsclass to include code regions, moving properties into their logical group, also fixing some comments/documentation at same time, making it clearer.PythonTemplateFilePathproperty in the settings class so it serialises and deserialises with user settingsIPreferences,IPathResolverandPathManagerclassstaticbacking store calledpythonTemplateFilePath. This is what the property above (part of interface)gets andsets. This was necessary as static props cannot be part of the interface.public staticmethod calledGetPythonTemplateFilePath, so it's accessible by nodes and anything else running in Dynamo context, such as ZT nodes.\nin hardcoded python code template with .NET standardEnvironment.NewLineline breaks, should behave more predictably when compiled on different platforms.resxvariables, to add support for localisation of code comments too.PreferenceSettingsorPathManagerclasses as per @mjkkirschner 's review. What happens now is that everytime a newDynamoModelinstance is created, the backing static property is overwritten.Limitations
DynamoSettings.XMLfile itself. Since the interfaces implement the property, it can be exposed via UI in later PR (working on this already but I'm not great with WPF, could use a helping hand)DynamoModelinstance from thePythonNodeModelclass to retrieve current settings, hence the static property, as discussed in previous PR Python template support #8034Python Scriptnode is added to canvas, there's no caching - can add it if required ?Declarations
*.resxfilesmasterbranch with2.0reported versionReviewers
@kronz
@mjkkirschner
@smangarole
@QilongTang
FYIs
@ksobon , @ThomasMahon, @wynged , @eibre , @andydandy74