# Simple validation of some molmoda-specific issues. import json import glob import re import os print("Running validate.py\n") def add_error(filename, msg): global errors filename = filename.replace("../src/", "@/") errors.append(f"{filename}: {msg}") def validate_plugin(ts_file, content): # Added content as a parameter # ts_file is a plugin # It must end in Plugin.vue if not ts_file.endswith("Plugin.vue"): add_error( ts_file, 'All files containing plugins must end in "Plugin.vue"', ) required_substrs = [ # ( # ':userArgs="userArgs"', # 'The PluginComponent must define a userArgs prop like this: :userArgs="userArgs"', # None, # ), # ( # ':intro="intro', # 'The PluginComponent must define an intro prop like this: :intro="intro"', # None, # ), ( ':infoPayload="infoPayload"', 'The PluginComponent must define a title prop like this: :infoPayload="infoPayload"', None, ), ( 'v-model="open"', 'The PluginComponent must define a v-model like this: v-model="open"', [ "RenameMolPlugin.vue", "DeleteMolPlugin.vue", "CloneExtractMolPlugin.vue", ], ), # ( # 'title="', # 'The PluginComponent must define a title like this: title="My Plugin"', # None, # ), # ( # ':pluginId="pluginId"', # 'The PluginComponent must define a pluginId prop like this: :pluginId="pluginId"', # None, # ), ( "extends PluginParentClass", "All plugins must extend PluginParentClass", [ "PubChemBioassaysPlugin.vue", "PubChemNamesPlugin.vue", "PubChemPropsPlugin.vue" ], ), ( " should have `href="#"`. Remove it and use `class="link-primary"` if needed.') if "CloseAppUtils.ts" not in ts_file and ('window.location.href =' in content or 'window.location.href=' in content): add_error( ts_file, 'No code should set `window.location.href = ...`. Use reloadPage instead.', ) if "workspace" in content.lower(): add_error( ts_file, 'The word "workspace" should not be used. Use "project" instead.', ) # if ".catch(" in content, there must be a "throw" within the next few # lines. Use regex. if ".catch(" in content: # import pdb; pdb.set_trace() for match in re.findall(r"\.catch\((.{0,200})", content, re.DOTALL): if "throw" not in match and "reject" not in match: add_error( ts_file, "If you use .catch(), you must throw an error or reject within the next few lines. Comment out 'throw err' in those rare cases where you want to ignore an error.", ) if "import(" in content and os.path.basename(ts_file) not in ["DynamicImports.ts", "AllPlugins.vue"]: add_error(ts_file, "Use import() only in the DynamicImports.ts file.") # Don't allow "MolModa" anywhere but in GlobalVars.ts content_without_dot_molmoda = content.replace("MOLMODA:", '') content_without_dot_molmoda = content_without_dot_molmoda.lower() content_without_dot_molmoda = content_without_dot_molmoda.replace('"molmoda"', '') content_without_dot_molmoda = content_without_dot_molmoda.replace(".molmoda", "") content_without_dot_molmoda = content_without_dot_molmoda.replace("/molmoda/", "") # Remove comments content_without_dot_molmoda = re.sub(r"//.*", "", content_without_dot_molmoda) content_without_dot_molmoda = re.sub(r" \* .*", "", content_without_dot_molmoda) has_mol_moda = re.search(r"\bmolmoda\b", content_without_dot_molmoda) if has_mol_moda and "GlobalVars.ts" not in ts_file: add_error( ts_file, 'The string "MolModa" should only be used in GlobalVars.ts. Import the app name from there.', ) # The substring ".get_svg" is only allowed in the file "Mol2DView.vue" if ".get_svg" in content and os.path.basename(ts_file) != "Mol2DView.vue": add_error( ts_file, 'The substring ".get_svg" is only allowed in Mol2DView.vue.' ) # Find all `.popupMessage` calls and check if the title is descriptive enough. # The regex finds `.popupMessage(`, optional whitespace, then captures the content # of the first string argument (single, double, or backtick quoted). popup_title_pattern = r'\.popupMessage\(\s*(["\'`])(.*?)\1' for match in re.finditer(popup_title_pattern, content, re.DOTALL): # group(2) captures the content inside the quotes title = match.group(2) # Count words by splitting on whitespace. # A title like "Error" (1 word) or "Access Denied" (2 words) is too short. if len(title.strip().split()) < 2: add_error( ts_file, f'Popup titles must be more than two words long for clarity. Found: "{title}"' ) # Try to avoid filtering molecules directly. Use the shallowFilters # subclass. # matches = ( # [t for t in re.finditer(r"[cC]ontainer.{0,25}\.filter", content, re.DOTALL)] # + [t for t in re.finditer(r"\.filter.{0,25}[cC]ontainer", content, re.DOTALL)] # + [t for t in re.finditer(r"[nN]ode.{0,25}\.filter", content, re.DOTALL)] # ) # txts = [content[m.span()[0] - 65 : m.span()[1] + 65] for m in matches] # txts = [t for t in txts if "mol_filter_ok" not in t] # for txt in txts: # # replace all new lines and double spaces using regex # txt = re.sub(r"\s+", " ", txt) # add_error( # ts_file, # f"Use the shallowFilters subclass instead of filtering TreeNodeList directly (or include mol_filter_ok somewhere nearby): `{txt}`", # ) ##### BELOW HERE, ONLY VALIDATE PLUGINS ##### # All *.vue files /Plugins/ must be plugins, except those in .../Parents/... if "/Plugins/" not in ts_file: # It's not a plugin continue if "/Parents/" in ts_file: # It's a parent, not a plugin continue if not ts_file.endswith(".vue"): # It's not a vue file, so not a plugin continue if os.path.basename(ts_file) in ["AllPlugins.vue"]: # It's not a plugin (in blacklist) continue if "