def _configure_from_xml(self, dom): """Configure a module based on XML input. This may be XML that was extracted from a marked-up comment, or directly in an XML file. """ cg_el = maybe_single_named_child(dom, 'code_gen') if cg_el: code_gen = single_text_child(cg_el) if code_gen not in ['template']: raise SystemParseError(xml_error_str(cg_el, "not a supported code_gen type: %s" % code_gen)) self.code_gen = code_gen # Get all headers hdr_els = maybe_get_element_list(dom, "headers", "header") if hdr_els is None: hdr_els = [] def fix_path(hdr): if os.path.isabs(hdr): return hdr else: return os.path.join(os.path.dirname(self.filename), hdr) for hdr_el in hdr_els: code_gen = hdr_el.getAttributeNode('code_gen') code_gen = code_gen.value if code_gen is not None else None if code_gen not in [None, 'template']: raise SystemParseError(xml_error_str(hdr_el, "not a supported code_gen type: %s" % code_gen)) self.headers.append(Header(fix_path(get_attribute(hdr_el, "path")), code_gen, hdr_el)) schema = maybe_single_named_child(dom, 'schema') self.schema = xml2schema(schema) if schema else None
def _configure_from_xml(self, dom): """Configure a module based on XML input. This may be XML that was extracted from a marked-up comment, or directly in an XML file. """ cg_el = maybe_single_named_child(dom, 'code_gen') if cg_el: code_gen = single_text_child(cg_el) if code_gen not in ['template']: raise SystemParseError(xml_error_str(cg_el, "not a supported code_gen type: %s" % code_gen)) self.code_gen = code_gen # Get all headers hdr_els = maybe_get_element_list(dom, "headers", "header") if hdr_els is None: hdr_els = [] def fix_path(hdr): if os.path.isabs(hdr): return hdr return os.path.join(os.path.dirname(self.filename), hdr) for hdr_el in hdr_els: code_gen = hdr_el.getAttributeNode('code_gen') code_gen = code_gen.value if code_gen is not None else None if code_gen not in [None, 'template']: raise SystemParseError(xml_error_str(hdr_el, "not a supported code_gen type: %s" % code_gen)) self.headers.append(Header(fix_path(get_attribute(hdr_el, "path")), code_gen, hdr_el)) schema = maybe_single_named_child(dom, 'schema') self.schema = xml2schema(schema) if schema else None
def get_paths_from_dom(dom, element_name): """Return the contents of all elements with a given name as a list of paths. `dom` is a DOM element from a parsed XML document. `element_name` is a string matched against the names of all elements in `dom`. The text node value contained in each such element is added to the return value. Trailing directory delimiters are removed from the end of each such string. This function returns an iterator over strings. """ for sp_el in dom.getElementsByTagName(element_name): yield single_text_child(sp_el).rstrip(os.sep)
def test_single_text_child_a(self): dom = xml_parse_string('<x></x>') self.assertEqual(single_text_child(dom), '')
def test_single_text_child_a(): dom = xml_parse_string('<x></x>') assert single_text_child(dom) == ''
def test_single_text_child_a(): dom = xml_parse_string("<x></x>") assert single_text_child(dom) == ""
def __init__(self, filename, search_paths=None, prx_include_paths=None): """Parses the project definition file `filename` and any imported system and module definition files. If filename is None, then a default 'empty' project is created. The search path for a project is a list of paths in which modules will be searched. The order of the search path matters; the first path in which an entity is found is used. The search path consists of the 'user' search paths, and the 'built-in' search paths. 'user' search paths are searched before 'built-in' search paths. The 'user' search paths consist of the 'param' search paths as passed explicitly to the class and the 'project' search paths, which are any search paths specified in the project file. 'param' search paths are searched before the 'project' search paths. If no 'param' search paths or 'project' search paths are specified, then the 'user' search path defaults to the project file's directory (or the current working directory if no project file is specified.) """ if filename is None: self.dom = xml_parse_string('<project></project>') self.project_dir = os.getcwd() else: self.dom = xml_parse_file(filename) self.project_dir = os.path.dirname(filename) self.entities = {} # Find all startup-script items. ss_els = self.dom.getElementsByTagName('startup-script') for ss in ss_els: command = single_text_child(ss) if command.split()[0].endswith('.py'): # prepend full path of python interpreter as .py files are not necessarily executable on Windows # and the command 'python3.3' is likely not in PATH command = '{} {}'.format(sys.executable, command) ret_code = os.system(command) if ret_code != 0: err = xml_error_str( ss, "Error running startup-script" ": '{}' {}".format(command, show_exit(ret_code))) raise ProjectStartupError(err) param_search_paths = search_paths if search_paths is not None else [] project_search_paths = list(get_paths_from_dom(self.dom, 'search-path')) user_search_paths = param_search_paths + project_search_paths if len(user_search_paths) == 0: user_search_paths = [self.project_dir] built_in_search_paths = [] if frozen: base_file = sys.executable if frozen else __file__ base_file = follow_link(base_file) base_dir = canonical_path(os.path.dirname(base_file)) def find_share(cur): cur = canonical_path(cur) maybe_share_path = os.path.join(cur, 'share') if os.path.exists(maybe_share_path): return maybe_share_path else: up = canonical_path(os.path.join(cur, os.path.pardir)) if up == cur: return None return find_share(up) share_dir = find_share(base_dir) if share_dir is None or not os.path.isdir(share_dir): logger.warning("Unable to find 'share' directory.") else: packages_dir = os.path.join(share_dir, 'packages') if not os.path.exists(packages_dir) or not os.path.isdir( packages_dir): logger.warning( "Can't find 'packages' directory in '{}'".format( share_dir)) else: built_in_search_paths.append(packages_dir) self.search_paths = user_search_paths + built_in_search_paths logger.debug("search_paths %s", self.search_paths) param_prx_include_paths = prx_include_paths if prx_include_paths is not None else [] self._prx_include_paths = list( get_paths_from_dom(self.dom, 'prx-include-path')) + param_prx_include_paths output_el = maybe_single_named_child(self.dom, 'output') if output_el: path = get_attribute(output_el, 'path') else: path = 'out' if os.path.isabs(path): self.output = path else: self.output = os.path.join(self.project_dir, path)
def __init__(self, filename, search_paths=None, prx_include_paths=None): """Parses the project definition file `filename` and any imported system and module definition files. If filename is None, then a default 'empty' project is created. The search path for a project is a list of paths in which modules will be searched. The order of the search path matters; the first path in which an entity is found is used. The search path consists of the 'user' search paths, and the 'built-in' search paths. 'user' search paths are searched before 'built-in' search paths. The 'user' search paths consist of the 'param' search paths as passed explicitly to the class and the 'project' search paths, which are any search paths specified in the project file. 'param' search paths are searched before the 'project' search paths. If no 'param' search paths or 'project' search paths are specified, then the 'user' search path defaults to the project file's directory (or the current working directory if no project file is specified.) """ if filename is None: self.dom = xml_parse_string('<project></project>') self.project_dir = os.getcwd() else: self.dom = xml_parse_file(filename) self.project_dir = os.path.dirname(filename) self.entities = {} # Find all startup-script items. ss_els = self.dom.getElementsByTagName('startup-script') for ss in ss_els: command = single_text_child(ss) if command.split()[0].endswith('.py'): # prepend full path of python interpreter as .py files are not necessarily executable on Windows # and the command 'python3.3' is likely not in PATH command = '{} {}'.format(sys.executable, command) ret_code = os.system(command) if ret_code != 0: err = xml_error_str(ss, "Error running startup-script" ": '{}' {}".format(command, show_exit(ret_code))) raise ProjectStartupError(err) param_search_paths = search_paths if search_paths is not None else [] project_search_paths = list(get_paths_from_dom(self.dom, 'search-path')) user_search_paths = param_search_paths + project_search_paths if len(user_search_paths) == 0: user_search_paths = [self.project_dir] built_in_search_paths = [] if frozen: base_file = sys.executable if frozen else __file__ base_file = follow_link(base_file) base_dir = canonical_path(os.path.dirname(base_file)) def find_share(cur): cur = canonical_path(cur) maybe_share_path = os.path.join(cur, 'share') if os.path.exists(maybe_share_path): return maybe_share_path else: up = canonical_path(os.path.join(cur, os.path.pardir)) if up == cur: return None return find_share(up) share_dir = find_share(base_dir) if share_dir is None or not os.path.isdir(share_dir): logger.warning("Unable to find 'share' directory.") else: packages_dir = os.path.join(share_dir, 'packages') if not os.path.exists(packages_dir) or not os.path.isdir(packages_dir): logger.warning("Can't find 'packages' directory in '{}'".format(share_dir)) else: built_in_search_paths.append(packages_dir) self.search_paths = user_search_paths + built_in_search_paths logger.debug("search_paths %s", self.search_paths) param_prx_include_paths = prx_include_paths if prx_include_paths is not None else [] self._prx_include_paths = list(get_paths_from_dom(self.dom, 'prx-include-path')) + param_prx_include_paths output_el = maybe_single_named_child(self.dom, 'output') if output_el: path = get_attribute(output_el, 'path') else: path = 'out' if os.path.isabs(path): self.output = path else: self.output = os.path.join(self.project_dir, path)