1+ """This is an example file of how to use the `execdmscript` module."""
2+
3+ # = = = = = = = = = = = = = = IGNORE START = = = = = = = = = = = = = = = = = =
4+ # Ignore the following code until the second line
5+ #
6+ # This code is for getting the __file__. GMS does not set the __file__ variable
7+ # on running scripts. That causes the example file not to run properly except
8+ # the execdmscript module is in one of the GMS plugin directories.
9+ # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
10+ try :
11+ import DigitalMicrograph as DM
12+ in_digital_micrograph = True
13+ except ImportError :
14+ in_digital_micrograph = False
15+
16+ file_is_missing = False
17+ try :
18+ if __file__ == "" or __file__ == None :
19+ file_is_missing = True
20+ except NameError :
21+ file_is_missing = True
22+
23+ if in_digital_micrograph and file_is_missing :
24+ # the name of the tag is used, this is deleted so it shouldn't matter anyway
25+ file_tag_name = "__python__file__"
26+ # the dm-script to execute, double curly brackets are used because of the
27+ # python format function
28+ script = ("\n " .join ((
29+ "DocumentWindow win = GetDocumentWindow(0);" ,
30+ "if(win.WindowIsvalid()){{" ,
31+ "if(win.WindowIsLinkedToFile()){{" ,
32+ "TagGroup tg = GetPersistentTagGroup();" ,
33+ "if(!tg.TagGroupDoesTagExist(\" {tag_name}\" )){{" ,
34+ "number index = tg.TagGroupCreateNewLabeledTag(\" {tag_name}\" );" ,
35+ "tg.TagGroupSetIndexedTagAsString(index, win.WindowGetCurrentFile());" ,
36+ "}}" ,
37+ "else{{" ,
38+ "tg.TagGroupSetTagAsString(\" {tag_name}\" , win.WindowGetCurrentFile());" ,
39+ "}}" ,
40+ "}}" ,
41+ "}}"
42+ ))).format (tag_name = file_tag_name )
43+
44+ # execute the dm script
45+ DM .ExecuteScriptString (script )
46+
47+ # read from the global tags to get the value to the python script
48+ global_tags = DM .GetPersistentTagGroup ()
49+ if global_tags .IsValid ():
50+ s , __file__ = global_tags .GetTagAsString (file_tag_name );
51+ if s :
52+ # delete the created tag again
53+ DM .ExecuteScriptString (
54+ "GetPersistentTagGroup()." +
55+ "TagGroupDeleteTagWithLabel(\" {}\" );" .format (file_tag_name )
56+ )
57+ else :
58+ del __file__
59+
60+ try :
61+ __file__
62+ except NameError :
63+ # set a default if the __file__ could not be received
64+ __file__ = ""
65+
66+ # = = = = = = = = = = = = = = = IGNORE END = = = = = = = = = = = = = = = = = =
67+
68+ # The start of the example file
69+
70+ import os
71+ import sys
72+ import pprint
73+ import traceback
74+
75+ if __file__ != "" :
76+ # add the parent directory to the system path so the execdmscript file
77+ # can be imported
78+ base_path = str (os .path .dirname (os .path .dirname (__file__ )))
79+
80+ if base_path not in sys .path :
81+ sys .path .insert (0 , base_path )
82+
83+ import execdmscript
84+
85+ try :
86+ script1 = """
87+ number a = 40;
88+ number c = a + b;
89+ string d = \" This is a test string\" ;
90+ """
91+
92+ script2 = os .path .join (os .path .dirname (__file__ ), "example1.s" )
93+
94+ script3 = """
95+ TagGroup tg3 = NewTagGroup();
96+
97+ index = tg3.TagGroupCreateNewLabeledTag("a");
98+ tg3.TagGroupSetIndexedTagAsFloat(index, 10.0001);
99+ index = tg3.TagGroupCreateNewLabeledTag("b");
100+ tg3.TagGroupSetIndexedTagAsFloat(index, 11.0002);
101+
102+ TagGroup tl2 = newTagList();
103+ """
104+
105+ parent = os .path .expanduser ("~" )
106+ home_dirs = []
107+ for e in os .listdir (parent ):
108+ if os .path .isdir (os .path .join (parent , e )):
109+ script3 += "tl2.TagGroupInsertTagAsString(infinity(), \" {}\" );\n " .format (
110+ str (e ).replace ("\\ " , "\\ \\ " ).replace ("\" " , "\\ \" " )
111+ )
112+ home_dirs .append (e )
113+
114+ script3 += """
115+ index = tg3.TagGroupCreateNewLabeledTag("files");
116+ tg3.TagGroupSetIndexedTagAsTagGroup(index, tl2);
117+
118+ TagGroup tl3 = NewTagList();
119+ tl3.TagGroupInsertTagAsString(infinity(), "a");
120+ tl3.TagGroupInsertTagAsString(infinity(), "b");
121+
122+ TagGroup tl4 = NewTagList();
123+ tl4.TagGroupInsertTagAsString(infinity(), "c");
124+ tl4.TagGroupInsertTagAsNumber(infinity(), 5);
125+
126+ TagGroup tl5 = NewTagList();
127+ tl5.TagGroupInsertTagAsString(infinity(), "d");
128+ """
129+
130+ readvars = {
131+ "a" : int ,
132+ "b" : "number" ,
133+ "c" : "dOuBle" ,
134+ "d" : str ,
135+ "tg1" : "TagGroup" ,
136+ "tg3" : {
137+ "a" : "float" ,
138+ "b" : float ,
139+ "files" : [str ] * len (home_dirs )
140+ },
141+ "tl3" : "TagList" ,
142+ "tl4" : [str , int ],
143+ "tl5" : list
144+ }
145+ setvars = {
146+ "b" : - 193.3288 ,
147+ "tg4" : {
148+ "test-key 1" : 1 ,
149+ "test-key 2" : {
150+ "test-key 3" : False ,
151+ "test-key 4" : None
152+ }
153+ },
154+ "tl6" : [
155+ "A" , "B" , ["U" , "V" ], (- 101 , - 120 )
156+ ]
157+ }
158+
159+ with execdmscript .exec_dmscript (script1 , script2 , script3 ,
160+ readvars = readvars , setvars = setvars ,
161+ separate_thread = False ,
162+ debug = False , debug_file = None ) as script :
163+ for key in script .synchronized_vars .keys ():
164+ print ("Variable '" , key , "'" )
165+ pprint .pprint (script [key ])
166+
167+ # wrapper = execdmscript.DMScriptWrapper(script1, script2, script3,
168+ # readvars=readvars, setvars=setvars)
169+ #
170+ # exec_script = wrapper.getExecDMScriptCode()
171+ #
172+ # print("The following script is being executed:")
173+ # print(exec_script)
174+ except Exception as e :
175+ # dm-script error messages are very bad, use this for getting the error text and the
176+ # correct traceback
177+ print ("Exception: " , e )
178+ traceback .print_exc ()
0 commit comments