codesysultra 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +190 -0
- package/README.md +156 -0
- package/dist/bin.js +71 -0
- package/dist/codesys_interop.js +270 -0
- package/dist/handlers/resources.js +193 -0
- package/dist/handlers/tools.js +332 -0
- package/dist/server.js +122 -0
- package/dist/templates/check_status.py +21 -0
- package/dist/templates/compile_project.py +55 -0
- package/dist/templates/create_method.py +68 -0
- package/dist/templates/create_pou.py +68 -0
- package/dist/templates/create_project.py +54 -0
- package/dist/templates/create_property.py +66 -0
- package/dist/templates/ensure_project_open.py +154 -0
- package/dist/templates/find_object_by_path.py +111 -0
- package/dist/templates/get_pou_code.py +73 -0
- package/dist/templates/get_project_structure.py +64 -0
- package/dist/templates/open_project.py +19 -0
- package/dist/templates/save_project.py +23 -0
- package/dist/templates/set_pou_code.py +90 -0
- package/dist/templates.js +42 -0
- package/dist/types.js +8 -0
- package/dist/utils.js +29 -0
- package/docs/configuration.md +141 -0
- package/docs/index.md +26 -0
- package/docs/installation.md +84 -0
- package/examples/README.md +78 -0
- package/examples/sample_config.json +32 -0
- package/package.json +48 -0
- package/scripts/copy-templates.js +31 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import sys, scriptengine as script_engine, os, traceback
|
|
2
|
+
{{ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}}
|
|
3
|
+
{{FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}}
|
|
4
|
+
POU_NAME = "{POU_NAME}"; POU_TYPE_STR = "{POU_TYPE_STR}"; IMPL_LANGUAGE_STR = "{IMPL_LANGUAGE_STR}"; PARENT_PATH_REL = "{PARENT_PATH}"
|
|
5
|
+
pou_type_map = { "Program": script_engine.PouType.Program, "FunctionBlock": script_engine.PouType.FunctionBlock, "Function": script_engine.PouType.Function }
|
|
6
|
+
# Map common language names to ImplementationLanguages attributes if needed (optional, None usually works)
|
|
7
|
+
# lang_map = { "ST": script_engine.ImplementationLanguage.st, ... }
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
print("DEBUG: create_pou script: Name='%s', Type='%s', Lang='%s', ParentPath='%s', Project='%s'" % (POU_NAME, POU_TYPE_STR, IMPL_LANGUAGE_STR, PARENT_PATH_REL, PROJECT_FILE_PATH))
|
|
11
|
+
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
12
|
+
if not POU_NAME: raise ValueError("POU name empty.")
|
|
13
|
+
if not PARENT_PATH_REL: raise ValueError("Parent path empty.")
|
|
14
|
+
|
|
15
|
+
# Resolve POU Type Enum
|
|
16
|
+
pou_type_enum = pou_type_map.get(POU_TYPE_STR)
|
|
17
|
+
if not pou_type_enum: raise ValueError("Invalid POU type string: %s. Use Program, FunctionBlock, or Function." % POU_TYPE_STR)
|
|
18
|
+
|
|
19
|
+
# Find parent object using the robust function
|
|
20
|
+
parent_object = find_object_by_path_robust(primary_project, PARENT_PATH_REL, "parent container")
|
|
21
|
+
if not parent_object: raise ValueError("Parent object not found for path: %s" % PARENT_PATH_REL)
|
|
22
|
+
|
|
23
|
+
parent_name = getattr(parent_object, 'get_name', lambda: str(parent_object))()
|
|
24
|
+
print("DEBUG: Using parent object: %s (Type: %s)" % (parent_name, type(parent_object).__name__))
|
|
25
|
+
|
|
26
|
+
# Check if parent object supports creating POUs (should implement ScriptIecLanguageObjectContainer)
|
|
27
|
+
if not hasattr(parent_object, 'create_pou'):
|
|
28
|
+
raise TypeError("Parent object '%s' of type %s does not support create_pou." % (parent_name, type(parent_object).__name__))
|
|
29
|
+
|
|
30
|
+
# Set language GUID to None (let CODESYS default based on parent/settings)
|
|
31
|
+
lang_guid = None
|
|
32
|
+
print("DEBUG: Setting language to None (will use default).")
|
|
33
|
+
# Example if mapping language string: lang_guid = lang_map.get(IMPL_LANGUAGE_STR, None)
|
|
34
|
+
|
|
35
|
+
print("DEBUG: Calling parent_object.create_pou: Name='%s', Type=%s, Lang=%s" % (POU_NAME, pou_type_enum, lang_guid))
|
|
36
|
+
|
|
37
|
+
# Call create_pou using keyword arguments
|
|
38
|
+
new_pou = parent_object.create_pou(
|
|
39
|
+
name=POU_NAME,
|
|
40
|
+
type=pou_type_enum,
|
|
41
|
+
language=lang_guid # Pass None
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
print("DEBUG: parent_object.create_pou returned: %s" % new_pou)
|
|
45
|
+
if new_pou:
|
|
46
|
+
new_pou_name = getattr(new_pou, 'get_name', lambda: POU_NAME)()
|
|
47
|
+
print("DEBUG: POU object created: %s" % new_pou_name)
|
|
48
|
+
|
|
49
|
+
# --- SAVE THE PROJECT TO PERSIST THE NEW POU ---
|
|
50
|
+
try:
|
|
51
|
+
print("DEBUG: Saving Project...")
|
|
52
|
+
primary_project.save() # Save the overall project file
|
|
53
|
+
print("DEBUG: Project saved successfully after POU creation.")
|
|
54
|
+
except Exception as save_err:
|
|
55
|
+
print("ERROR: Failed to save Project after POU creation: %s" % save_err)
|
|
56
|
+
detailed_error = traceback.format_exc()
|
|
57
|
+
error_message = "Error saving Project after creating POU '%s': %s\\n%s" % (new_pou_name, save_err, detailed_error)
|
|
58
|
+
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
59
|
+
# --- END SAVING ---
|
|
60
|
+
|
|
61
|
+
print("POU Created: %s" % new_pou_name); print("Type: %s" % POU_TYPE_STR); print("Language: %s (Defaulted)" % IMPL_LANGUAGE_STR); print("Parent Path: %s" % PARENT_PATH_REL)
|
|
62
|
+
print("SCRIPT_SUCCESS: POU created successfully."); sys.exit(0)
|
|
63
|
+
else:
|
|
64
|
+
error_message = "Failed to create POU '%s'. create_pou returned None." % POU_NAME; print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
65
|
+
except Exception as e:
|
|
66
|
+
detailed_error = traceback.format_exc()
|
|
67
|
+
error_message = "Error creating POU '%s' in project '%s': %s\\n%s" % (POU_NAME, PROJECT_FILE_PATH, e, detailed_error)
|
|
68
|
+
print(error_message); print("SCRIPT_ERROR: Error creating POU '%s': %s" % (POU_NAME, e)); sys.exit(1)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import sys, scriptengine as script_engine, os, shutil, time, traceback
|
|
2
|
+
# Placeholders
|
|
3
|
+
TEMPLATE_PROJECT_PATH = r'{TEMPLATE_PROJECT_PATH}' # Path to Standard.project
|
|
4
|
+
PROJECT_FILE_PATH = r'{PROJECT_FILE_PATH}' # Path for the new project (Target Path)
|
|
5
|
+
try:
|
|
6
|
+
print("DEBUG: Python script create_project (copy from template):")
|
|
7
|
+
print("DEBUG: Template Source = %s" % TEMPLATE_PROJECT_PATH)
|
|
8
|
+
print("DEBUG: Target Path = %s" % PROJECT_FILE_PATH)
|
|
9
|
+
if not PROJECT_FILE_PATH: raise ValueError("Target project file path empty.")
|
|
10
|
+
if not TEMPLATE_PROJECT_PATH: raise ValueError("Template project file path empty.")
|
|
11
|
+
if not os.path.exists(TEMPLATE_PROJECT_PATH): raise IOError("Template project file not found: %s" % TEMPLATE_PROJECT_PATH)
|
|
12
|
+
|
|
13
|
+
# 1. Copy the template project file to the new location
|
|
14
|
+
target_dir = os.path.dirname(PROJECT_FILE_PATH)
|
|
15
|
+
if not os.path.exists(target_dir): print("DEBUG: Creating target directory: %s" % target_dir); os.makedirs(target_dir)
|
|
16
|
+
# Check if target file already exists
|
|
17
|
+
if os.path.exists(PROJECT_FILE_PATH): print("WARN: Target project file already exists, overwriting: %s" % PROJECT_FILE_PATH)
|
|
18
|
+
|
|
19
|
+
print("DEBUG: Copying '%s' to '%s'..." % (TEMPLATE_PROJECT_PATH, PROJECT_FILE_PATH))
|
|
20
|
+
shutil.copy2(TEMPLATE_PROJECT_PATH, PROJECT_FILE_PATH) # copy2 preserves metadata
|
|
21
|
+
print("DEBUG: File copy complete.")
|
|
22
|
+
|
|
23
|
+
# 2. Open the newly copied project file
|
|
24
|
+
print("DEBUG: Opening the copied project: %s" % PROJECT_FILE_PATH)
|
|
25
|
+
# Set flags for silent opening
|
|
26
|
+
update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
27
|
+
# try:
|
|
28
|
+
# update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
29
|
+
# except AttributeError:
|
|
30
|
+
# print("WARN: VersionUpdateFlags not found, using integer flags for open (1 | 2 = 3).")
|
|
31
|
+
# update_mode = 3
|
|
32
|
+
|
|
33
|
+
project = script_engine.projects.open(PROJECT_FILE_PATH, update_flags=update_mode)
|
|
34
|
+
print("DEBUG: script_engine.projects.open returned: %s" % project)
|
|
35
|
+
if project:
|
|
36
|
+
print("DEBUG: Pausing briefly after open...")
|
|
37
|
+
time.sleep(1.0) # Allow CODESYS to potentially initialize things
|
|
38
|
+
try:
|
|
39
|
+
print("DEBUG: Explicitly saving project after opening copy...")
|
|
40
|
+
project.save();
|
|
41
|
+
print("DEBUG: Project save after opening copy succeeded.")
|
|
42
|
+
except Exception as save_err:
|
|
43
|
+
print("WARN: Explicit save after opening copy failed: %s" % save_err)
|
|
44
|
+
# Decide if this is critical - maybe not, but good to know.
|
|
45
|
+
print("Project Created from Template Copy at: %s" % PROJECT_FILE_PATH)
|
|
46
|
+
print("SCRIPT_SUCCESS: Project copied from template and opened successfully.")
|
|
47
|
+
sys.exit(0)
|
|
48
|
+
else:
|
|
49
|
+
error_message = "Failed to open project copy %s after copying template %s. projects.open returned None." % (PROJECT_FILE_PATH, TEMPLATE_PROJECT_PATH)
|
|
50
|
+
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
51
|
+
except Exception as e:
|
|
52
|
+
detailed_error = traceback.format_exc()
|
|
53
|
+
error_message = "Error creating project '%s' from template '%s': %s\\n%s" % (PROJECT_FILE_PATH, TEMPLATE_PROJECT_PATH, e, detailed_error)
|
|
54
|
+
print(error_message); print("SCRIPT_ERROR: Error copying/opening template: %s" % e); sys.exit(1)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import sys, scriptengine as script_engine, os, traceback
|
|
2
|
+
{{ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}}
|
|
3
|
+
{{FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}}
|
|
4
|
+
PARENT_POU_FULL_PATH = "{PARENT_POU_FULL_PATH}" # e.g., "Application/MyFB"
|
|
5
|
+
PROPERTY_NAME = "{PROPERTY_NAME}"
|
|
6
|
+
PROPERTY_TYPE = "{PROPERTY_TYPE}"
|
|
7
|
+
# Optional: Language for Getter/Setter (usually defaults to ST)
|
|
8
|
+
# LANG_GUID_STR = "{LANG_GUID_STR}" # Example if needed
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
print("DEBUG: create_property script: ParentPOU='%s', Name='%s', Type='%s', Project='%s'" % (PARENT_POU_FULL_PATH, PROPERTY_NAME, PROPERTY_TYPE, PROJECT_FILE_PATH))
|
|
12
|
+
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
13
|
+
if not PARENT_POU_FULL_PATH: raise ValueError("Parent POU full path empty.")
|
|
14
|
+
if not PROPERTY_NAME: raise ValueError("Property name empty.")
|
|
15
|
+
if not PROPERTY_TYPE: raise ValueError("Property type empty.")
|
|
16
|
+
|
|
17
|
+
# Find the parent POU object
|
|
18
|
+
parent_pou_object = find_object_by_path_robust(primary_project, PARENT_POU_FULL_PATH, "parent POU")
|
|
19
|
+
if not parent_pou_object: raise ValueError("Parent POU object not found: %s" % PARENT_POU_FULL_PATH)
|
|
20
|
+
|
|
21
|
+
parent_pou_name = getattr(parent_pou_object, 'get_name', lambda: PARENT_POU_FULL_PATH)()
|
|
22
|
+
print("DEBUG: Found Parent POU object: %s" % parent_pou_name)
|
|
23
|
+
|
|
24
|
+
# Check if parent object supports creating properties (should implement ScriptIecLanguageMemberContainer)
|
|
25
|
+
if not hasattr(parent_pou_object, 'create_property'):
|
|
26
|
+
raise TypeError("Parent object '%s' of type %s does not support create_property." % (parent_pou_name, type(parent_pou_object).__name__))
|
|
27
|
+
|
|
28
|
+
# Default language to None (usually ST)
|
|
29
|
+
lang_guid = None
|
|
30
|
+
print("DEBUG: Calling create_property: Name='%s', Type='%s', Lang=%s" % (PROPERTY_NAME, PROPERTY_TYPE, lang_guid))
|
|
31
|
+
|
|
32
|
+
# Call the create_property method ON THE PARENT POU
|
|
33
|
+
new_property_object = parent_pou_object.create_property(
|
|
34
|
+
name=PROPERTY_NAME,
|
|
35
|
+
return_type=PROPERTY_TYPE,
|
|
36
|
+
language=lang_guid # Pass None to use default
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
if new_property_object:
|
|
40
|
+
new_prop_name = getattr(new_property_object, 'get_name', lambda: PROPERTY_NAME)()
|
|
41
|
+
print("DEBUG: Property object created: %s" % new_prop_name)
|
|
42
|
+
|
|
43
|
+
# --- SAVE THE PROJECT TO PERSIST THE NEW PROPERTY OBJECT ---
|
|
44
|
+
try:
|
|
45
|
+
print("DEBUG: Saving Project (after property creation)...")
|
|
46
|
+
primary_project.save()
|
|
47
|
+
print("DEBUG: Project saved successfully after property creation.")
|
|
48
|
+
except Exception as save_err:
|
|
49
|
+
print("ERROR: Failed to save Project after creating property: %s" % save_err)
|
|
50
|
+
detailed_error = traceback.format_exc()
|
|
51
|
+
error_message = "Error saving Project after creating property '%s': %s\\n%s" % (PROPERTY_NAME, save_err, detailed_error)
|
|
52
|
+
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
53
|
+
# --- END SAVING ---
|
|
54
|
+
|
|
55
|
+
print("Property Created: %s" % new_prop_name)
|
|
56
|
+
print("Parent POU: %s" % PARENT_POU_FULL_PATH)
|
|
57
|
+
print("Type: %s" % PROPERTY_TYPE)
|
|
58
|
+
print("SCRIPT_SUCCESS: Property created successfully."); sys.exit(0)
|
|
59
|
+
else:
|
|
60
|
+
error_message = "Failed to create property '%s' under '%s'. create_property returned None." % (PROPERTY_NAME, parent_pou_name)
|
|
61
|
+
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
62
|
+
|
|
63
|
+
except Exception as e:
|
|
64
|
+
detailed_error = traceback.format_exc()
|
|
65
|
+
error_message = "Error creating property '%s' under POU '%s' in project '%s': %s\\n%s" % (PROPERTY_NAME, PARENT_POU_FULL_PATH, PROJECT_FILE_PATH, e, detailed_error)
|
|
66
|
+
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import scriptengine as script_engine
|
|
3
|
+
import os
|
|
4
|
+
import time
|
|
5
|
+
import traceback
|
|
6
|
+
|
|
7
|
+
# --- Function to ensure the correct project is open ---
|
|
8
|
+
MAX_RETRIES = 3
|
|
9
|
+
RETRY_DELAY = 2.0 # seconds (use float for time.sleep)
|
|
10
|
+
|
|
11
|
+
def ensure_project_open(target_project_path):
|
|
12
|
+
print("DEBUG: Ensuring project is open: %s" % target_project_path)
|
|
13
|
+
# Normalize target path once
|
|
14
|
+
normalized_target_path = os.path.normcase(os.path.abspath(target_project_path))
|
|
15
|
+
|
|
16
|
+
for attempt in range(MAX_RETRIES):
|
|
17
|
+
print("DEBUG: Ensure project attempt %d/%d for %s" % (attempt + 1, MAX_RETRIES, normalized_target_path))
|
|
18
|
+
primary_project = None
|
|
19
|
+
try:
|
|
20
|
+
# Getting primary project might fail if CODESYS instance is unstable
|
|
21
|
+
primary_project = script_engine.projects.primary
|
|
22
|
+
except Exception as primary_err:
|
|
23
|
+
print("WARN: Error getting primary project: %s. Assuming none." % primary_err)
|
|
24
|
+
# traceback.print_exc() # Optional: Print stack trace for this error
|
|
25
|
+
primary_project = None
|
|
26
|
+
|
|
27
|
+
current_project_path = ""
|
|
28
|
+
project_ok = False # Flag to check if target is confirmed primary and accessible
|
|
29
|
+
|
|
30
|
+
if primary_project:
|
|
31
|
+
try:
|
|
32
|
+
# Getting path should be relatively safe if primary_project object exists
|
|
33
|
+
current_project_path = os.path.normcase(os.path.abspath(primary_project.path))
|
|
34
|
+
print("DEBUG: Current primary project path: %s" % current_project_path)
|
|
35
|
+
if current_project_path == normalized_target_path:
|
|
36
|
+
# Found the right project as primary, now check if it's usable
|
|
37
|
+
print("DEBUG: Target project path matches primary. Checking access...")
|
|
38
|
+
try:
|
|
39
|
+
# Try a relatively safe operation to confirm object usability
|
|
40
|
+
# Getting children count is a reasonable check
|
|
41
|
+
_ = len(primary_project.get_children(False))
|
|
42
|
+
print("DEBUG: Target project '%s' is primary and accessible." % target_project_path)
|
|
43
|
+
project_ok = True
|
|
44
|
+
return primary_project # SUCCESS CASE 1: Already open and accessible
|
|
45
|
+
except Exception as access_err:
|
|
46
|
+
# Project found, but accessing it failed. Might be unstable.
|
|
47
|
+
print("WARN: Primary project access check failed for '%s': %s. Will attempt reopen." % (current_project_path, access_err))
|
|
48
|
+
# traceback.print_exc() # Optional: Print stack trace
|
|
49
|
+
primary_project = None # Force reopen by falling through
|
|
50
|
+
else:
|
|
51
|
+
# A *different* project is primary
|
|
52
|
+
print("DEBUG: Primary project is '%s', not the target '%s'." % (current_project_path, normalized_target_path))
|
|
53
|
+
# Consider closing the wrong project if causing issues, but for now, just open target
|
|
54
|
+
# try:
|
|
55
|
+
# print("DEBUG: Closing incorrect primary project '%s'..." % current_project_path)
|
|
56
|
+
# primary_project.close() # Be careful with unsaved changes
|
|
57
|
+
# except Exception as close_err:
|
|
58
|
+
# print("WARN: Failed to close incorrect primary project: %s" % close_err)
|
|
59
|
+
primary_project = None # Force open target project
|
|
60
|
+
|
|
61
|
+
except Exception as path_err:
|
|
62
|
+
# Failed even to get the path of the supposed primary project
|
|
63
|
+
print("WARN: Could not get path of current primary project: %s. Assuming not the target." % path_err)
|
|
64
|
+
# traceback.print_exc() # Optional: Print stack trace
|
|
65
|
+
primary_project = None # Force open target project
|
|
66
|
+
|
|
67
|
+
# If target project not confirmed as primary and accessible, attempt to open/reopen
|
|
68
|
+
if not project_ok:
|
|
69
|
+
# Log clearly whether we are opening initially or reopening
|
|
70
|
+
if primary_project is None and current_project_path == "":
|
|
71
|
+
print("DEBUG: No primary project detected. Attempting to open target: %s" % target_project_path)
|
|
72
|
+
elif primary_project is None and current_project_path != "":
|
|
73
|
+
print("DEBUG: Primary project was '%s' but failed access check or needed close. Attempting to open target: %s" % (current_project_path, target_project_path))
|
|
74
|
+
else: # Includes cases where wrong project was open
|
|
75
|
+
print("DEBUG: Target project not primary or initial check failed. Attempting to open/reopen: %s" % target_project_path)
|
|
76
|
+
|
|
77
|
+
try:
|
|
78
|
+
# Set flags for silent opening, handle potential attribute errors
|
|
79
|
+
update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
80
|
+
# try:
|
|
81
|
+
# update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
82
|
+
# except AttributeError:
|
|
83
|
+
# print("WARN: VersionUpdateFlags not found, using integer flags for open (1 | 2 = 3).")
|
|
84
|
+
# update_mode = 3 # 1=NoUpdates, 2=SilentMode
|
|
85
|
+
|
|
86
|
+
opened_project = None
|
|
87
|
+
try:
|
|
88
|
+
# The actual open call
|
|
89
|
+
print("DEBUG: Calling script_engine.projects.open('%s', update_flags=%s)..." % (target_project_path, update_mode))
|
|
90
|
+
opened_project = script_engine.projects.open(target_project_path, update_flags=update_mode)
|
|
91
|
+
|
|
92
|
+
if not opened_project:
|
|
93
|
+
# This is a critical failure if open returns None without exception
|
|
94
|
+
print("ERROR: projects.open returned None for %s on attempt %d" % (target_project_path, attempt + 1))
|
|
95
|
+
# Allow retry loop to continue
|
|
96
|
+
else:
|
|
97
|
+
# Open call returned *something*, let's verify
|
|
98
|
+
print("DEBUG: projects.open call returned an object for: %s" % target_project_path)
|
|
99
|
+
print("DEBUG: Pausing for stabilization after open...")
|
|
100
|
+
time.sleep(RETRY_DELAY) # Give CODESYS time
|
|
101
|
+
# Re-verify: Is the project now primary and accessible?
|
|
102
|
+
recheck_primary = None
|
|
103
|
+
try: recheck_primary = script_engine.projects.primary
|
|
104
|
+
except Exception as recheck_primary_err: print("WARN: Error getting primary project after reopen: %s" % recheck_primary_err)
|
|
105
|
+
|
|
106
|
+
if recheck_primary:
|
|
107
|
+
recheck_path = ""
|
|
108
|
+
try: # Getting path might fail
|
|
109
|
+
recheck_path = os.path.normcase(os.path.abspath(recheck_primary.path))
|
|
110
|
+
except Exception as recheck_path_err:
|
|
111
|
+
print("WARN: Failed to get path after reopen: %s" % recheck_path_err)
|
|
112
|
+
|
|
113
|
+
if recheck_path == normalized_target_path:
|
|
114
|
+
print("DEBUG: Target project confirmed as primary after reopening.")
|
|
115
|
+
try: # Final sanity check
|
|
116
|
+
_ = len(recheck_primary.get_children(False))
|
|
117
|
+
print("DEBUG: Reopened project basic access confirmed.")
|
|
118
|
+
return recheck_primary # SUCCESS CASE 2: Successfully opened/reopened
|
|
119
|
+
except Exception as access_err_reopen:
|
|
120
|
+
print("WARN: Reopened project (%s) basic access check failed: %s." % (normalized_target_path, access_err_reopen))
|
|
121
|
+
# traceback.print_exc() # Optional
|
|
122
|
+
# Allow retry loop to continue
|
|
123
|
+
else:
|
|
124
|
+
print("WARN: Different project is primary after reopening! Expected '%s', got '%s'." % (normalized_target_path, recheck_path))
|
|
125
|
+
# Allow retry loop to continue, maybe it fixes itself
|
|
126
|
+
else:
|
|
127
|
+
print("WARN: No primary project found after reopening attempt %d!" % (attempt+1))
|
|
128
|
+
# Allow retry loop to continue
|
|
129
|
+
|
|
130
|
+
except Exception as open_err:
|
|
131
|
+
# Catch errors during the open call itself
|
|
132
|
+
print("ERROR: Exception during projects.open call on attempt %d: %s" % (attempt + 1, open_err))
|
|
133
|
+
traceback.print_exc() # Crucial for diagnosing open failures
|
|
134
|
+
# Allow retry loop to continue
|
|
135
|
+
|
|
136
|
+
except Exception as outer_open_err:
|
|
137
|
+
# Catch errors in the flag setup etc.
|
|
138
|
+
print("ERROR: Unexpected error during open setup/logic attempt %d: %s" % (attempt + 1, outer_open_err))
|
|
139
|
+
traceback.print_exc()
|
|
140
|
+
|
|
141
|
+
# If we didn't return successfully in this attempt, wait before retrying
|
|
142
|
+
if attempt < MAX_RETRIES - 1:
|
|
143
|
+
print("DEBUG: Ensure project attempt %d did not succeed. Waiting %f seconds..." % (attempt + 1, RETRY_DELAY))
|
|
144
|
+
time.sleep(RETRY_DELAY)
|
|
145
|
+
else: # Last attempt failed
|
|
146
|
+
print("ERROR: Failed all ensure_project_open attempts for %s." % normalized_target_path)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# If all retries fail after the loop
|
|
150
|
+
raise RuntimeError("Failed to ensure project '%s' is open and accessible after %d attempts." % (target_project_path, MAX_RETRIES))
|
|
151
|
+
# --- End of function ---
|
|
152
|
+
|
|
153
|
+
# Placeholder for the project file path (must be set in scripts using this snippet)
|
|
154
|
+
PROJECT_FILE_PATH = r"{PROJECT_FILE_PATH}"
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import traceback
|
|
2
|
+
# --- Find object by path function ---
|
|
3
|
+
def find_object_by_path_robust(start_node, full_path, target_type_name="object"):
|
|
4
|
+
print("DEBUG: Finding %s by path: '%s'" % (target_type_name, full_path))
|
|
5
|
+
normalized_path = full_path.replace('\\\\', '/').strip('/')
|
|
6
|
+
path_parts = normalized_path.split('/')
|
|
7
|
+
if not path_parts:
|
|
8
|
+
print("ERROR: Path is empty.")
|
|
9
|
+
return None
|
|
10
|
+
|
|
11
|
+
# Determine the actual starting node (project or application)
|
|
12
|
+
project = start_node # Assume start_node is project initially
|
|
13
|
+
if not hasattr(start_node, 'active_application') and hasattr(start_node, 'project'):
|
|
14
|
+
# If start_node is not project but has project ref (e.g., an application), get the project
|
|
15
|
+
try: project = start_node.project
|
|
16
|
+
except Exception as proj_ref_err:
|
|
17
|
+
print("WARN: Could not get project reference from start_node: %s" % proj_ref_err)
|
|
18
|
+
# Proceed assuming start_node might be the project anyway or search fails
|
|
19
|
+
|
|
20
|
+
# Try to get the application object robustly if we think we have the project
|
|
21
|
+
app = None
|
|
22
|
+
if hasattr(project, 'active_application'):
|
|
23
|
+
try: app = project.active_application
|
|
24
|
+
except Exception: pass # Ignore errors getting active app
|
|
25
|
+
if not app:
|
|
26
|
+
try:
|
|
27
|
+
apps = project.find("Application", True) # Search recursively
|
|
28
|
+
if apps: app = apps[0]
|
|
29
|
+
except Exception: pass
|
|
30
|
+
|
|
31
|
+
# Check if the first path part matches the application name
|
|
32
|
+
app_name_lower = ""
|
|
33
|
+
if app:
|
|
34
|
+
try: app_name_lower = (app.get_name() or "application").lower()
|
|
35
|
+
except Exception: app_name_lower = "application" # Fallback
|
|
36
|
+
|
|
37
|
+
# Decide where to start the traversal
|
|
38
|
+
current_obj = start_node # Default to the node passed in
|
|
39
|
+
if hasattr(project, 'active_application'): # Only adjust if start_node was likely the project
|
|
40
|
+
if app and path_parts[0].lower() == app_name_lower:
|
|
41
|
+
print("DEBUG: Path starts with Application name '%s'. Beginning search there." % path_parts[0])
|
|
42
|
+
current_obj = app
|
|
43
|
+
path_parts = path_parts[1:] # Consume the app name part
|
|
44
|
+
# If path was *only* the application name
|
|
45
|
+
if not path_parts:
|
|
46
|
+
print("DEBUG: Target path is the Application object itself.")
|
|
47
|
+
return current_obj
|
|
48
|
+
else:
|
|
49
|
+
print("DEBUG: Path does not start with Application name. Starting search from project root.")
|
|
50
|
+
current_obj = project # Start search from the project root
|
|
51
|
+
else:
|
|
52
|
+
print("DEBUG: Starting search from originally provided node.")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# Traverse the remaining path parts
|
|
56
|
+
parent_path_str = getattr(current_obj, 'get_name', lambda: str(current_obj))() # Safer name getting
|
|
57
|
+
|
|
58
|
+
for i, part_name in enumerate(path_parts):
|
|
59
|
+
is_last_part = (i == len(path_parts) - 1)
|
|
60
|
+
print("DEBUG: Searching for part [%d/%d]: '%s' under '%s'" % (i+1, len(path_parts), part_name, parent_path_str))
|
|
61
|
+
found_in_parent = None
|
|
62
|
+
try:
|
|
63
|
+
# Prioritize non-recursive find for direct children
|
|
64
|
+
children_of_current = current_obj.get_children(False)
|
|
65
|
+
print("DEBUG: Found %d direct children under '%s'." % (len(children_of_current), parent_path_str))
|
|
66
|
+
for child in children_of_current:
|
|
67
|
+
child_name = getattr(child, 'get_name', lambda: None)() # Safer name getting
|
|
68
|
+
# print("DEBUG: Checking child: '%s'" % child_name) # Verbose
|
|
69
|
+
if child_name == part_name:
|
|
70
|
+
found_in_parent = child
|
|
71
|
+
print("DEBUG: Found direct child matching '%s'." % part_name)
|
|
72
|
+
break # Found direct child, stop searching children
|
|
73
|
+
|
|
74
|
+
# If not found directly, AND it's the last part, try recursive find from current parent
|
|
75
|
+
if not found_in_parent and is_last_part:
|
|
76
|
+
print("DEBUG: Direct find failed for last part '%s'. Trying recursive find under '%s'." % (part_name, parent_path_str))
|
|
77
|
+
found_recursive_list = current_obj.find(part_name, True) # Recursive find
|
|
78
|
+
if found_recursive_list:
|
|
79
|
+
# Maybe add a check here if multiple are found?
|
|
80
|
+
found_in_parent = found_recursive_list[0] # Take the first match
|
|
81
|
+
print("DEBUG: Found last part '%s' recursively." % part_name)
|
|
82
|
+
else:
|
|
83
|
+
print("DEBUG: Recursive find also failed for last part '%s'." % part_name)
|
|
84
|
+
|
|
85
|
+
# Update current object if found
|
|
86
|
+
if found_in_parent:
|
|
87
|
+
current_obj = found_in_parent
|
|
88
|
+
parent_path_str = getattr(current_obj, 'get_name', lambda: part_name)() # Safer name getting
|
|
89
|
+
print("DEBUG: Stepped into '%s'." % parent_path_str)
|
|
90
|
+
else:
|
|
91
|
+
# If not found at any point, the path is invalid from this parent
|
|
92
|
+
print("ERROR: Path part '%s' not found under '%s'." % (part_name, parent_path_str))
|
|
93
|
+
return None # Path broken
|
|
94
|
+
|
|
95
|
+
except Exception as find_err:
|
|
96
|
+
print("ERROR: Exception while searching for '%s' under '%s': %s" % (part_name, parent_path_str, find_err))
|
|
97
|
+
traceback.print_exc()
|
|
98
|
+
return None # Error during search
|
|
99
|
+
|
|
100
|
+
# Final verification (optional but recommended): Check if the found object's name matches the last part
|
|
101
|
+
final_expected_name = full_path.split('/')[-1]
|
|
102
|
+
found_final_name = getattr(current_obj, 'get_name', lambda: None)() # Safer name getting
|
|
103
|
+
|
|
104
|
+
if found_final_name == final_expected_name:
|
|
105
|
+
print("DEBUG: Final %s found and name verified for path '%s': %s" % (target_type_name, full_path, found_final_name))
|
|
106
|
+
return current_obj
|
|
107
|
+
else:
|
|
108
|
+
print("ERROR: Traversal ended on object '%s' but expected final name was '%s'." % (found_final_name, final_expected_name))
|
|
109
|
+
return None # Name mismatch implies target not found as expected
|
|
110
|
+
|
|
111
|
+
# --- End of find object function ---
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import sys, scriptengine as script_engine, os, traceback
|
|
2
|
+
{{ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}}
|
|
3
|
+
{{FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}}
|
|
4
|
+
POU_FULL_PATH = "{POU_FULL_PATH}"; CODE_START_MARKER = "### POU CODE START ###"; CODE_END_MARKER = "### POU CODE END ###"
|
|
5
|
+
DECL_START_MARKER = "### POU DECLARATION START ###"; DECL_END_MARKER = "### POU DECLARATION END ###"
|
|
6
|
+
IMPL_START_MARKER = "### POU IMPLEMENTATION START ###"; IMPL_END_MARKER = "### POU IMPLEMENTATION END ###"
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
print("DEBUG: Getting code: POU_FULL_PATH='%s', Project='%s'" % (POU_FULL_PATH, PROJECT_FILE_PATH))
|
|
10
|
+
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
11
|
+
if not POU_FULL_PATH: raise ValueError("POU full path empty.")
|
|
12
|
+
|
|
13
|
+
# Find the target POU/Method/Property object
|
|
14
|
+
target_object = find_object_by_path_robust(primary_project, POU_FULL_PATH, "target object")
|
|
15
|
+
if not target_object: raise ValueError("Target object not found using path: %s" % POU_FULL_PATH)
|
|
16
|
+
|
|
17
|
+
target_name = getattr(target_object, 'get_name', lambda: POU_FULL_PATH)()
|
|
18
|
+
print("DEBUG: Found target object: %s" % target_name)
|
|
19
|
+
|
|
20
|
+
declaration_code = ""; implementation_code = ""
|
|
21
|
+
|
|
22
|
+
# --- Get Declaration Part ---
|
|
23
|
+
if hasattr(target_object, 'textual_declaration'):
|
|
24
|
+
decl_obj = target_object.textual_declaration
|
|
25
|
+
if decl_obj and hasattr(decl_obj, 'text'):
|
|
26
|
+
try:
|
|
27
|
+
declaration_code = decl_obj.text
|
|
28
|
+
print("DEBUG: Got declaration text.")
|
|
29
|
+
except Exception as decl_read_err:
|
|
30
|
+
print("ERROR: Failed to read declaration text: %s" % decl_read_err)
|
|
31
|
+
declaration_code = "/* ERROR reading declaration: %s */" % decl_read_err
|
|
32
|
+
else:
|
|
33
|
+
print("WARN: textual_declaration exists but is None or has no 'text' attribute.")
|
|
34
|
+
else:
|
|
35
|
+
print("WARN: No textual_declaration attribute.")
|
|
36
|
+
|
|
37
|
+
# --- Get Implementation Part ---
|
|
38
|
+
if hasattr(target_object, 'textual_implementation'):
|
|
39
|
+
impl_obj = target_object.textual_implementation
|
|
40
|
+
if impl_obj and hasattr(impl_obj, 'text'):
|
|
41
|
+
try:
|
|
42
|
+
implementation_code = impl_obj.text
|
|
43
|
+
print("DEBUG: Got implementation text.")
|
|
44
|
+
except Exception as impl_read_err:
|
|
45
|
+
print("ERROR: Failed to read implementation text: %s" % impl_read_err)
|
|
46
|
+
implementation_code = "/* ERROR reading implementation: %s */" % impl_read_err
|
|
47
|
+
else:
|
|
48
|
+
print("WARN: textual_implementation exists but is None or has no 'text' attribute.")
|
|
49
|
+
else:
|
|
50
|
+
print("WARN: No textual_implementation attribute.")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
print("Code retrieved for: %s" % target_name)
|
|
54
|
+
# Print declaration between markers, ensuring markers are on separate lines
|
|
55
|
+
print("\\n" + DECL_START_MARKER)
|
|
56
|
+
print(declaration_code)
|
|
57
|
+
print(DECL_END_MARKER + "\\n")
|
|
58
|
+
# Print implementation between markers
|
|
59
|
+
print(IMPL_START_MARKER)
|
|
60
|
+
print(implementation_code)
|
|
61
|
+
print(IMPL_END_MARKER + "\\n")
|
|
62
|
+
|
|
63
|
+
# --- LEGACY MARKERS for backward compatibility if needed ---
|
|
64
|
+
# Combine both for old marker format, adding a separator line
|
|
65
|
+
# legacy_combined_code = declaration_code + "\\n\\n// Implementation\\n" + implementation_code
|
|
66
|
+
# print(CODE_START_MARKER); print(legacy_combined_code); print(CODE_END_MARKER)
|
|
67
|
+
# --- END LEGACY ---
|
|
68
|
+
|
|
69
|
+
print("SCRIPT_SUCCESS: Code retrieved."); sys.exit(0)
|
|
70
|
+
except Exception as e:
|
|
71
|
+
detailed_error = traceback.format_exc()
|
|
72
|
+
error_message = "Error getting code for object '%s' in project '%s': %s\\n%s" % (POU_FULL_PATH, PROJECT_FILE_PATH, e, detailed_error)
|
|
73
|
+
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import sys, scriptengine as script_engine, os, traceback
|
|
2
|
+
{{ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}}
|
|
3
|
+
# FIND_OBJECT_BY_PATH_PYTHON_SNIPPET is NOT needed here, we start from project root.
|
|
4
|
+
|
|
5
|
+
def get_object_structure(obj, indent=0, max_depth=10): # Add max_depth
|
|
6
|
+
lines = []; indent_str = " " * indent
|
|
7
|
+
if indent > max_depth:
|
|
8
|
+
lines.append("%s- Max recursion depth reached." % indent_str)
|
|
9
|
+
return lines
|
|
10
|
+
try:
|
|
11
|
+
name = "Unnamed"; obj_type = type(obj).__name__
|
|
12
|
+
guid_str = ""
|
|
13
|
+
folder_str = ""
|
|
14
|
+
try:
|
|
15
|
+
name = getattr(obj, 'get_name', lambda: "Unnamed")() or "Unnamed" # Safer get_name
|
|
16
|
+
if hasattr(obj, 'guid'): guid_str = " {%s}" % obj.guid
|
|
17
|
+
if hasattr(obj, 'is_folder') and obj.is_folder: folder_str = " [Folder]"
|
|
18
|
+
except Exception as name_err:
|
|
19
|
+
print("WARN: Error getting name/guid/folder status for an object: %s" % name_err)
|
|
20
|
+
name = "!!! Error Getting Name !!!"
|
|
21
|
+
|
|
22
|
+
lines.append("%s- %s (%s)%s%s" % (indent_str, name, obj_type, folder_str, guid_str))
|
|
23
|
+
|
|
24
|
+
# Get children only if the object potentially has them
|
|
25
|
+
children = []
|
|
26
|
+
can_have_children = hasattr(obj, 'get_children') and (
|
|
27
|
+
not hasattr(obj, 'is_folder') or # If it's not clear if it's a folder (e.g., project root)
|
|
28
|
+
(hasattr(obj, 'is_folder') and obj.is_folder) or # If it is a folder
|
|
29
|
+
# Add known container types explicitly, check marker interfaces too
|
|
30
|
+
hasattr(obj, 'is_project') or hasattr(obj, 'is_application') or hasattr(obj, 'is_device') or hasattr(obj,'is_pou')
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
if can_have_children:
|
|
34
|
+
try:
|
|
35
|
+
children = obj.get_children(False)
|
|
36
|
+
# print("DEBUG: %s has %d children" % (name, len(children))) # Verbose
|
|
37
|
+
except Exception as get_child_err:
|
|
38
|
+
lines.append("%s ERROR getting children: %s" % (indent_str, get_child_err))
|
|
39
|
+
# traceback.print_exc() # Optional
|
|
40
|
+
|
|
41
|
+
for child in children:
|
|
42
|
+
lines.extend(get_object_structure(child, indent + 1, max_depth)) # Recurse
|
|
43
|
+
|
|
44
|
+
except Exception as e:
|
|
45
|
+
lines.append("%s- Error processing node: %s" % (indent_str, e))
|
|
46
|
+
traceback.print_exc() # Print detailed error for this node
|
|
47
|
+
return lines
|
|
48
|
+
try:
|
|
49
|
+
print("DEBUG: Getting structure for: %s" % PROJECT_FILE_PATH)
|
|
50
|
+
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
51
|
+
project_name = os.path.basename(PROJECT_FILE_PATH)
|
|
52
|
+
print("DEBUG: Getting structure for project: %s" % project_name)
|
|
53
|
+
# Use the project object obtained from ensure_project_open
|
|
54
|
+
structure_list = get_object_structure(primary_project, max_depth=15) # Set a reasonable depth
|
|
55
|
+
structure_output = "\\n".join(structure_list)
|
|
56
|
+
# Ensure markers are printed distinctly
|
|
57
|
+
print("\\n--- PROJECT STRUCTURE START ---")
|
|
58
|
+
print(structure_output)
|
|
59
|
+
print("--- PROJECT STRUCTURE END ---\\n")
|
|
60
|
+
print("SCRIPT_SUCCESS: Project structure retrieved."); sys.exit(0)
|
|
61
|
+
except Exception as e:
|
|
62
|
+
detailed_error = traceback.format_exc()
|
|
63
|
+
error_message = "Error getting structure for %s: %s\\n%s" % (PROJECT_FILE_PATH, e, detailed_error)
|
|
64
|
+
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import sys, scriptengine as script_engine, os, traceback
|
|
2
|
+
{{ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}}
|
|
3
|
+
try:
|
|
4
|
+
project = ensure_project_open(PROJECT_FILE_PATH)
|
|
5
|
+
# Get name from object if possible, otherwise use path basename
|
|
6
|
+
proj_name = "Unknown"
|
|
7
|
+
try:
|
|
8
|
+
if project: proj_name = project.get_name() or os.path.basename(PROJECT_FILE_PATH)
|
|
9
|
+
else: proj_name = os.path.basename(PROJECT_FILE_PATH) + " (ensure_project_open returned None?)"
|
|
10
|
+
except Exception:
|
|
11
|
+
proj_name = os.path.basename(PROJECT_FILE_PATH) + " (name retrieval failed)"
|
|
12
|
+
print("Project Opened: %s" % proj_name)
|
|
13
|
+
print("SCRIPT_SUCCESS: Project opened successfully.")
|
|
14
|
+
sys.exit(0)
|
|
15
|
+
except Exception as e:
|
|
16
|
+
error_message = "Error opening project %s: %s" % (PROJECT_FILE_PATH, e)
|
|
17
|
+
print(error_message)
|
|
18
|
+
traceback.print_exc()
|
|
19
|
+
print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import sys, scriptengine as script_engine, os, traceback
|
|
2
|
+
{{ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}}
|
|
3
|
+
try:
|
|
4
|
+
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
5
|
+
# Get name from object if possible, otherwise use path basename
|
|
6
|
+
project_name = "Unknown"
|
|
7
|
+
try:
|
|
8
|
+
if primary_project: project_name = primary_project.get_name() or os.path.basename(PROJECT_FILE_PATH)
|
|
9
|
+
else: project_name = os.path.basename(PROJECT_FILE_PATH) + " (ensure_project_open returned None?)"
|
|
10
|
+
except Exception:
|
|
11
|
+
project_name = os.path.basename(PROJECT_FILE_PATH) + " (name retrieval failed)"
|
|
12
|
+
|
|
13
|
+
print("DEBUG: Saving project: %s (%s)" % (project_name, PROJECT_FILE_PATH))
|
|
14
|
+
primary_project.save()
|
|
15
|
+
print("DEBUG: project.save() executed.")
|
|
16
|
+
print("Project Saved: %s" % project_name)
|
|
17
|
+
print("SCRIPT_SUCCESS: Project saved successfully.")
|
|
18
|
+
sys.exit(0)
|
|
19
|
+
except Exception as e:
|
|
20
|
+
error_message = "Error saving project %s: %s" % (PROJECT_FILE_PATH, e)
|
|
21
|
+
print(error_message)
|
|
22
|
+
traceback.print_exc()
|
|
23
|
+
print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|