Skip to content

Commit 18ecf76

Browse files
romangolevjmcouffin
authored andcommitted
fix: move enums to parser, simplify code by removing wraps
1 parent 33b3d75 commit 18ecf76

File tree

11 files changed

+175
-218
lines changed

11 files changed

+175
-218
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
#pylint: disable=C0103,W1401,E0401,E0602
3+
"""
4+
██▓███▓██ ██▓ ██▀███ ▓█████ ██▒ █▓ ██▓▄▄▄█████▓
5+
▓██░ ██▒██ ██▒▓██ ▒ ██▒▓█ ▀▓██░ █▒▓██▒▓ ██▒ ▓▒
6+
▓██░ ██▓▒▒██ ██░▓██ ░▄█ ▒▒███ ▓██ █▒░▒██▒▒ ▓██░ ▒░
7+
▒██▄█▓▒ ▒░ ▐██▓░▒██▀▀█▄ ▒▓█ ▄ ▒██ █░░░██░░ ▓██▓ ░
8+
▒██▒ ░ ░░ ██▒▓░░██▓ ▒██▒░▒████▒ ▒▀█░ ░██░ ▒██▒ ░
9+
▒▓▒░ ░ ░ ██▒▒▒ ░ ▒▓ ░▒▓░░░ ▒░ ░ ░ ▐░ ░▓ ▒ ░░
10+
░▒ ░ ▓██ ░▒░ ░▒ ░ ▒░ ░ ░ ░ ░ ░░ ▒ ░ ░
11+
░░ ▒ ▒ ░░ ░░ ░ ░ ░░ ▒ ░ ░
12+
░ ░ ░ ░ ░ ░ ░
13+
░ ░ ░
14+
This is the starting point for pyRevit. At Revit loads the PyRevitLoader.dll
15+
addon at startup. This dll then creates an ironpython engine and runs
16+
pyRevitLoader.py (this script). It's the job of this script to setup the
17+
environment for the pyrevit module (pyrevitlib\pyrevit) and load a new pyRevit
18+
session. This script needs to add the directory path of the pyrevit lib folder
19+
so the pyrevit module can be imported and used.
20+
"""
21+
22+
import sys
23+
import os.path as op
24+
25+
# add the library location to the system search paths
26+
repo_path = op.dirname(op.dirname(op.dirname(op.dirname(__file__))))
27+
sys.path.append(op.join(repo_path, 'pyrevitlib'))
28+
29+
# now pyrevit can be imported
30+
from pyrevit.loader import sessionmgr
31+
32+
# ask sessionmgr to start a new session
33+
sessionmgr.load_session(light=True)

dev/pyRevitLoader/Source/PyRevitLoaderApplication.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ private static void LoadAssembliesInFolder(string folder)
7272
private static Result ExecuteStartupScript(UIControlledApplication uiControlledApplication)
7373
{
7474
//TODO: Implement a switcher here to be able to switch between Python/C# loaders
75-
return ExecuteStartUpPython(uiControlledApplication);
76-
//return ExecuteStartUpCsharp(uiControlledApplication);
75+
//return ExecuteStartUpPython(uiControlledApplication);
76+
return ExecuteStartUpCsharp(uiControlledApplication);
7777
}
7878

7979
public static Result ExecuteStartUpPython(UIControlledApplication uiControlledApplication)

dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/AssemblyBuilderService.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.CodeAnalysis.CSharp;
77
using pyRevitAssemblyBuilder.SessionManager;
88
using System.Reflection;
9+
using pyRevitExtensionParser;
910

1011
namespace pyRevitAssemblyBuilder.AssemblyMaker
1112
{
@@ -20,7 +21,7 @@ public AssemblyBuilderService(CommandTypeGenerator typeGenerator, string revitVe
2021
_revitVersion = revitVersion ?? throw new ArgumentNullException(nameof(revitVersion));
2122
}
2223

23-
public ExtensionAssemblyInfo BuildExtensionAssembly(WrappedExtension extension)
24+
public ExtensionAssemblyInfo BuildExtensionAssembly(ParsedExtension extension)
2425
{
2526
string extensionHash = GetStableHash(extension.GetHash() + _revitVersion).Substring(0, 16);
2627
string fileName = $"pyRevit_{_revitVersion}_{extensionHash}_{extension.Name}.dll";

dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/CommandTypeGenerator.cs

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using System.Text;
4-
using System.Linq;
5-
using pyRevitAssemblyBuilder.SessionManager;
6-
using Autodesk.Revit.Attributes;
3+
using pyRevitExtensionParser;
4+
using System.Collections.Generic;
75

86
namespace pyRevitAssemblyBuilder.AssemblyMaker
97
{
108
public class CommandTypeGenerator
119
{
12-
public string GenerateExtensionCode(WrappedExtension extension)
10+
public string GenerateExtensionCode(ParsedExtension extension)
1311
{
1412
var sb = new StringBuilder();
1513
sb.AppendLine("#nullable disable");
1614
sb.AppendLine("using Autodesk.Revit.Attributes;");
1715
sb.AppendLine("using PyRevitLabs.PyRevit.Runtime;");
18-
foreach (var cmd in extension.GetAllCommands())
16+
foreach (var cmd in CollectCommandComponents(extension.Children))
1917
{
2018
// Replace invalid characters with underscores for valid C# identifiers
2119
string safeClassName = SanitizeClassName(cmd.UniqueId);
@@ -60,7 +58,20 @@ public string GenerateExtensionCode(WrappedExtension extension)
6058
}
6159
return sb.ToString();
6260
}
61+
private IEnumerable<ParsedComponent> CollectCommandComponents(IEnumerable<ParsedComponent> components)
62+
{
63+
foreach (var component in components)
64+
{
65+
if (!string.IsNullOrEmpty(component.ScriptPath))
66+
yield return component;
6367

68+
if (component.Children != null)
69+
{
70+
foreach (var child in CollectCommandComponents(component.Children))
71+
yield return child;
72+
}
73+
}
74+
}
6475
private static string SanitizeClassName(string name)
6576
{
6677
var sb = new StringBuilder();

dev/pyRevitLoader/pyRevitAssemblyBuilder/SessionManager/ExtensionManagerService.cs

+9-33
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,26 @@ namespace pyRevitAssemblyBuilder.SessionManager
66
{
77
public class ExtensionManagerService
88
{
9-
public IEnumerable<WrappedExtension> GetInstalledExtensions()
9+
public IEnumerable<ParsedExtension> GetInstalledExtensions()
1010
{
1111
var installedExtensions = ExtensionParser.ParseInstalledExtensions();
12+
1213
foreach (var parsedExtension in installedExtensions)
1314
{
14-
var pushbuttonCommands = CollectCommandComponents(parsedExtension.Children)
15-
.Select(ConvertComponent)
16-
.ToList();
17-
18-
yield return new WrappedExtension(
19-
name: parsedExtension.Name,
20-
path: parsedExtension.Directory,
21-
commands: pushbuttonCommands,
22-
children: parsedExtension.Children.Select(ConvertComponent).ToList(),
23-
metadata: parsedExtension.Metadata
24-
);
15+
// Populate tooltip, UniqueId, etc., if needed (e.g., add validation or enrichment)
16+
FlattenAndEnrich(parsedExtension.Children);
17+
yield return parsedExtension;
2518
}
2619
}
27-
private IEnumerable<ParsedComponent> CollectCommandComponents(IEnumerable<ParsedComponent> components)
20+
21+
private void FlattenAndEnrich(IEnumerable<ParsedComponent> components)
2822
{
2923
foreach (var component in components)
3024
{
31-
if (!string.IsNullOrEmpty(component.ScriptPath))
32-
yield return component;
33-
25+
// Custom logic if needed, e.g., enrich tooltip or validate UniqueId
3426
if (component.Children != null)
35-
{
36-
foreach (var child in CollectCommandComponents(component.Children))
37-
yield return child;
38-
}
27+
FlattenAndEnrich(component.Children);
3928
}
4029
}
41-
private FileCommandComponent ConvertComponent(ParsedComponent parsed)
42-
{
43-
return new FileCommandComponent
44-
{
45-
Name = parsed.Name,
46-
ScriptPath = parsed.ScriptPath,
47-
Tooltip = parsed.Tooltip,
48-
UniqueId = parsed.UniqueId,
49-
ExtensionName = parsed.UniqueId.Split('.')[0],
50-
Type = parsed.Type,
51-
Children = parsed.Children?.Select(ConvertComponent).Cast<object>().ToList() ?? new List<object>()
52-
};
53-
}
5430
}
5531
}

dev/pyRevitLoader/pyRevitAssemblyBuilder/SessionManager/FileCommandComponent.cs

-16
This file was deleted.

dev/pyRevitLoader/pyRevitAssemblyBuilder/SessionManager/HookManager.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using pyRevitExtensionParser;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -7,7 +8,7 @@ namespace pyRevitAssemblyBuilder.SessionManager
78
{
89
public class HookManager
910
{
10-
public void RegisterHooks(WrappedExtension extension)
11+
public void RegisterHooks(ParsedExtension extension)
1112
{
1213
if (extension == null)
1314
return;
@@ -28,15 +29,15 @@ public void RegisterHooks(WrappedExtension extension)
2829
// Future: implement actual execution logic for scripts if needed
2930
}
3031

31-
private IEnumerable<string> GetHookScripts(WrappedExtension extension)
32+
private IEnumerable<string> GetHookScripts(ParsedExtension extension)
3233
{
3334
var hooksPath = Path.Combine(extension.Directory, "hooks");
3435
return Directory.Exists(hooksPath)
3536
? Directory.GetFiles(hooksPath)
3637
: Enumerable.Empty<string>();
3738
}
3839

39-
private IEnumerable<string> GetCheckScripts(WrappedExtension extension)
40+
private IEnumerable<string> GetCheckScripts(ParsedExtension extension)
4041
{
4142
var checksPath = Path.Combine(extension.Directory, "checks");
4243
return Directory.Exists(checksPath)

dev/pyRevitLoader/pyRevitAssemblyBuilder/SessionManager/UIManagerService.cs

+31-47
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-

2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using Autodesk.Revit.UI;
5+
using pyRevitExtensionParser;
66
using pyRevitAssemblyBuilder.AssemblyMaker;
7-
using pyRevitAssemblyBuilder.Shared;
87

98
namespace pyRevitAssemblyBuilder.SessionManager
109
{
@@ -17,60 +16,48 @@ public UIManagerService(UIApplication uiApp)
1716
_uiApp = uiApp;
1817
}
1918

20-
public void BuildUI(WrappedExtension extension, ExtensionAssemblyInfo assemblyInfo)
19+
public void BuildUI(ParsedExtension extension, ExtensionAssemblyInfo assemblyInfo)
2120
{
2221
if (extension?.Children == null)
2322
return;
2423

25-
foreach (var obj in extension.Children as IEnumerable<object> ?? Enumerable.Empty<object>())
26-
RecursivelyBuildUI(obj, null, null, extension.Name, assemblyInfo);
24+
foreach (var component in extension.Children)
25+
RecursivelyBuildUI(component, null, null, extension.Name, assemblyInfo);
2726
}
2827

29-
private void RecursivelyBuildUI(object obj, object parentComponent, RibbonPanel parentPanel, string tabName, ExtensionAssemblyInfo assemblyInfo)
28+
private void RecursivelyBuildUI(ParsedComponent component, ParsedComponent parentComponent, RibbonPanel parentPanel, string tabName, ExtensionAssemblyInfo assemblyInfo)
3029
{
31-
var component = obj as FileCommandComponent;
32-
if (component == null)
33-
return;
34-
35-
var type = CommandComponentTypeExtensions.FromExtension(component.Type);
36-
37-
switch (type)
30+
switch (component.Type)
3831
{
3932
case CommandComponentType.Tab:
4033
try { _uiApp.CreateRibbonTab(component.Name); } catch { }
41-
foreach (var child in component.Children ?? Enumerable.Empty<object>())
34+
foreach (var child in component.Children ?? Enumerable.Empty<ParsedComponent>())
4235
RecursivelyBuildUI(child, component, null, component.Name, assemblyInfo);
4336
break;
4437

4538
case CommandComponentType.Panel:
4639
var panel = _uiApp.GetRibbonPanels(tabName).FirstOrDefault(p => p.Name == component.Name)
4740
?? _uiApp.CreateRibbonPanel(tabName, component.Name);
48-
foreach (var child in component.Children ?? Enumerable.Empty<object>())
41+
foreach (var child in component.Children ?? Enumerable.Empty<ParsedComponent>())
4942
RecursivelyBuildUI(child, component, panel, tabName, assemblyInfo);
5043
break;
5144

5245
case CommandComponentType.Stack:
5346
var itemDataList = new List<RibbonItemData>();
54-
var originalItems = new List<FileCommandComponent>();
47+
var originalItems = new List<ParsedComponent>();
5548

56-
foreach (var child in component.Children as IEnumerable<object> ?? Enumerable.Empty<object>())
49+
foreach (var child in component.Children ?? Enumerable.Empty<ParsedComponent>())
5750
{
58-
var subCmd = child as FileCommandComponent;
59-
if (subCmd == null)
60-
continue;
61-
62-
var subType = CommandComponentTypeExtensions.FromExtension(subCmd.Type);
63-
64-
if (subType == CommandComponentType.PushButton)
51+
if (child.Type == CommandComponentType.PushButton)
6552
{
66-
itemDataList.Add(CreatePushButton(subCmd, assemblyInfo));
67-
originalItems.Add(subCmd);
53+
itemDataList.Add(CreatePushButton(child, assemblyInfo));
54+
originalItems.Add(child);
6855
}
69-
else if (subType == CommandComponentType.PullDown)
56+
else if (child.Type == CommandComponentType.PullDown)
7057
{
71-
var pdData = new PulldownButtonData(subCmd.UniqueId, subCmd.Name);
58+
var pdData = new PulldownButtonData(child.UniqueId, child.Name);
7259
itemDataList.Add(pdData);
73-
originalItems.Add(subCmd);
60+
originalItems.Add(child);
7461
}
7562
}
7663

@@ -91,12 +78,11 @@ private void RecursivelyBuildUI(object obj, object parentComponent, RibbonPanel
9178

9279
if (ribbonItem is PulldownButton pdBtn)
9380
{
94-
foreach (var child in origComponent.Children ?? Enumerable.Empty<object>())
81+
foreach (var sub in origComponent.Children ?? Enumerable.Empty<ParsedComponent>())
9582
{
96-
if (child is FileCommandComponent subCmd &&
97-
CommandComponentTypeExtensions.FromExtension(subCmd.Type) == CommandComponentType.PushButton)
83+
if (sub.Type == CommandComponentType.PushButton)
9884
{
99-
var subData = CreatePushButton(subCmd, assemblyInfo);
85+
var subData = CreatePushButton(sub, assemblyInfo);
10086
pdBtn.AddPushButton(subData);
10187
}
10288
}
@@ -124,12 +110,11 @@ private void RecursivelyBuildUI(object obj, object parentComponent, RibbonPanel
124110
var splitBtn = parentPanel?.AddItem(splitData) as SplitButton;
125111
if (splitBtn == null) return;
126112

127-
foreach (var child in component.Children ?? Enumerable.Empty<object>())
113+
foreach (var sub in component.Children ?? Enumerable.Empty<ParsedComponent>())
128114
{
129-
if (child is FileCommandComponent subCmd &&
130-
CommandComponentTypeExtensions.FromExtension(subCmd.Type) == CommandComponentType.PushButton)
115+
if (sub.Type == CommandComponentType.PushButton)
131116
{
132-
var subData = CreatePushButton(subCmd, assemblyInfo);
117+
var subData = CreatePushButton(sub, assemblyInfo);
133118
splitBtn.AddPushButton(subData);
134119
}
135120
}
@@ -138,7 +123,7 @@ private void RecursivelyBuildUI(object obj, object parentComponent, RibbonPanel
138123
}
139124

140125
private PulldownButtonData CreatePulldown(
141-
FileCommandComponent component,
126+
ParsedComponent component,
142127
RibbonPanel parentPanel,
143128
string tabName,
144129
ExtensionAssemblyInfo assemblyInfo,
@@ -153,26 +138,25 @@ private PulldownButtonData CreatePulldown(
153138
if (pdBtn == null)
154139
return null;
155140

156-
foreach (var child in component.Children ?? Enumerable.Empty<object>())
141+
foreach (var sub in component.Children ?? Enumerable.Empty<ParsedComponent>())
157142
{
158-
if (child is FileCommandComponent subCmd &&
159-
CommandComponentTypeExtensions.FromExtension(subCmd.Type) == CommandComponentType.PushButton)
143+
if (sub.Type == CommandComponentType.PushButton)
160144
{
161-
var subData = CreatePushButton(subCmd, assemblyInfo);
145+
var subData = CreatePushButton(sub, assemblyInfo);
162146
pdBtn.AddPushButton(subData);
163147
}
164148
}
165149

166150
return pdData;
167151
}
168152

169-
private PushButtonData CreatePushButton(FileCommandComponent command, ExtensionAssemblyInfo assemblyInfo)
153+
private PushButtonData CreatePushButton(ParsedComponent component, ExtensionAssemblyInfo assemblyInfo)
170154
{
171155
return new PushButtonData(
172-
command.UniqueId,
173-
command.Name,
156+
component.UniqueId,
157+
component.Name,
174158
assemblyInfo.Location,
175-
command.UniqueId
159+
component.UniqueId
176160
);
177161
}
178162
}

0 commit comments

Comments
 (0)