def generate_response(self): """generate the xmla response. :return: xmla response as string """ if self.mdx_query == "": # check if command contains a query xml = xmlwitch.Builder() with xml["return"]: xml.root(xmlns="urn:schemas-microsoft-com:xml-analysis:empty") return str(xml) else: xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns= "urn:schemas-microsoft-com:xml-analysis:mddataset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }, ): xml.write(execute_xsd) with xml.OlapInfo: with xml.CubeInfo: with xml.Cube: xml.CubeName("Sales") xml.LastDataUpdate( datetime.now().strftime( "%Y-%m-%dT%H:%M:%S"), xmlns= "http://schemas.microsoft.com/analysisservices/2003/engine", ) xml.LastSchemaUpdate( datetime.now().strftime( "%Y-%m-%dT%H:%M:%S"), xmlns= "http://schemas.microsoft.com/analysisservices/2003/engine", ) xml.write(self.generate_cell_info()) with xml.AxesInfo: xml.write(self.generate_axes_info()) xml.write(self.generate_axes_info_slicer()) with xml.Axes: xml.write(self.generate_xs0()) xml.write(self.generate_slicer_axis()) with xml.CellData: xml.write(self.generate_cell_data()) return str(xml)
def _get_properties( xsd, PropertyName, PropertyDescription, PropertyType, PropertyAccessType, IsRequired, Value, ): xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }, ): xml.write(xsd) if PropertyName: with xml.row: xml.PropertyName(PropertyName) xml.PropertyDescription(PropertyDescription) xml.PropertyType(PropertyType) xml.PropertyAccessType(PropertyAccessType) xml.IsRequired(IsRequired) xml.Value(Value) else: properties_names_n_description = [ "ServerName", "ProviderVersion", "MdpropMdxSubqueries", "MdpropMdxDrillFunctions", "MdpropMdxNamedSets", ] properties_types = [ "string", "string", "int", "int", "int" ] values = [ os.getenv("USERNAME", "default"), "0.0.3 25-Nov-2016 07:20:28 GMT", "15", "3", "15", ] for idx, prop_desc in enumerate( properties_names_n_description): with xml.row: xml.PropertyName(prop_desc) xml.PropertyDescription(prop_desc) xml.PropertyType(properties_types[idx]) xml.PropertyAccessType("Read") xml.IsRequired("false") xml.Value(values[idx]) return str(xml)
def test_slicer_axis_query12(executor): """ Many measures. """ xml = xmlwitch.Builder() with xml.Axis(name="SlicerAxis"): with xml.Tuples: with xml.Tuple: with xml.Member(Hierarchy="[geography].[geography]"): xml.UName("[geography].[geography].[continent].[America]") xml.Caption("America") xml.LName("[geography].[geography].[continent]") xml.LNum("0") xml.DisplayInfo("2") with xml.Member(Hierarchy="[product].[product]"): xml.UName( "[product].[product].[company].[Crazy Development]") xml.Caption("Crazy Development") xml.LName("[product].[product].[company]") xml.LNum("0") xml.DisplayInfo("2") with xml.Member(Hierarchy="[time].[time]"): xml.UName("[time].[time].[year].[2010]") xml.Caption("2010") xml.LName("[time].[time].[year]") xml.LNum("0") xml.DisplayInfo("2") xmla_tools = XmlaExecuteReqHandler(executor, query12, False) assert str(xml) == xmla_tools.generate_slicer_axis()
def close_shell(self, shell_id): """ Close the shell @param string shell_id: The shell id on the remote machine. See #open_shell @returns This should have more error checking but it just returns true for now. @rtype bool """ message_id = uuid.uuid4() node = xmlwitch.Builder(version='1.0', encoding='utf-8') with node.env__Envelope(**self._namespaces): with node.env__Header: self._build_soap_header(node, message_id) self._set_resource_uri_cmd(node) self._set_action_delete(node) self._set_selector_shell_id(node, shell_id) # SOAP message requires empty env:Body with node.env__Body: pass response = self.send_message(str(node)) root = ET.fromstring(response) relates_to = next(node for node in root.findall('.//*') if node.tag.endswith('RelatesTo')).text # TODO change assert into user-friendly exception assert uuid.UUID(relates_to.replace('uuid:', '')) == message_id
def generate_resp(rows): xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }): xml.write(discover_schema_rowsets_xsd) for resp_row in rows: with xml.row: xml.SchemaName(resp_row["SchemaName"]) xml.SchemaGuid(resp_row["SchemaGuid"]) for idx, restriction in enumerate( resp_row["restrictions"] ["restriction_names"]): with xml.Restrictions: xml.Name(restriction) xml.Type(resp_row["restrictions"] ["restriction_types"][idx]) xml.RestrictionsMask(resp_row["RestrictionsMask"]) return str(xml)
def mdschema_kpis_response(self, request): """ Describes the key performance indicators (KPIs) within a database. :param request: :return: """ xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }): xml.write(mdschema_kpis_xsd) if request.Restrictions.RestrictionList: if (request.Restrictions.RestrictionList.CUBE_NAME == self.selected_cube and request.Properties.PropertyList.Catalog is not None): self.change_cube( request.Properties.PropertyList.Catalog) return str(xml)
def mdschema_sets_response(self, request): """ Describes any sets that are currently defined in a database, including session-scoped sets. :param request: :return: """ xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }): xml.write(mdschema_sets_xsd) if request.Restrictions.RestrictionList: if (request.Restrictions.RestrictionList.CUBE_NAME == self.selected_cube and request.Properties.PropertyList.Catalog is not None): self.change_cube( request.Properties.PropertyList.Catalog) return str(xml)
def discover_datasources_response(): """ list of data sources that are available on the server :return: """ xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:EX": "urn:schemas-microsoft-com:xml-analysis:exception", "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }): xml.write(discover_datasources_xsd) with xml.row: xml.DataSourceName("sales") xml.DataSourceDescription("sales Sample Data") xml.URL("http://127.0.0.1:8000/xmla") xml.DataSourceInfo("-") xml.ProviderName("olapy") xml.ProviderType("MDP") xml.AuthenticationMode("Unauthenticated") return str(xml)
def test_simple_document(self): xml = xmlwitch.Builder(version='1.0', encoding='utf-8') with xml.person: xml.name("Bob") xml.city("Qusqu") self.assertEquals(str(xml), self.expected_document('simple_document.xml'))
def _generate_axes_info_convert2formulas(self): """AxisInfo elements when convert to formulas, xml structure always Axis0 with measures and SlicerAxis with all dimensions like this:: <AxisInfo name="Axis0"> <HierarchyInfo name="[Measures]"> <UName name="[Measures].[MEMBER_UNIQUE_NAME]" type="xsd:string"/> <Caption name="[Measures].[MEMBER_CAPTION]" type="xsd:string"/> <LName name="[Measures].[LEVEL_UNIQUE_NAME]" type="xsd:string"/> <LNum name="[Measures].[LEVEL_NUMBER]" type="xsd:int"/> <DisplayInfo name="[Measures].[DISPLAY_INFO]" type="xsd:unsignedInt"/> </HierarchyInfo> </AxisInfo> <AxisInfo name="SlicerAxis"> <HierarchyInfo name="[Geography].[Geo]"> <UName name="[Geography].[Geo].[MEMBER_UNIQUE_NAME]" type="xsd:string"/> <Caption name="[Geography].[Geo].[MEMBER_CAPTION]" type="xsd:string"/> <LName name="[Geography].[Geo].[LEVEL_UNIQUE_NAME]" type="xsd:string"/> <LNum name="[Geography].[Geo].[LEVEL_NUMBER]" type="xsd:int"/> <DisplayInfo name="[Geography].[Geo].[DISPLAY_INFO]" type="xsd:unsignedInt"/> </HierarchyInfo> <HierarchyInfo name="[Geography].[Economy]"> <UName name="[Geography].[Economy].[MEMBER_UNIQUE_NAME]" type="xsd:string"/> <Caption name="[Geography].[Economy].[MEMBER_CAPTION]" type="xsd:string"/> <LName name="[Geography].[Economy].[LEVEL_UNIQUE_NAME]" type="xsd:string"/> <LNum name="[Geography].[Economy].[LEVEL_NUMBER]" type="xsd:int"/> <DisplayInfo name="[Geography].[Economy].[DISPLAY_INFO]" type="xsd:unsignedInt"/> </HierarchyInfo> <HierarchyInfo name="[Product].[Prod]"> <UName name="[Product].[Prod].[MEMBER_UNIQUE_NAME]" type="xsd:string"/> <Caption name="[Product].[Prod].[MEMBER_CAPTION]" type="xsd:string"/> <LName name="[Product].[Prod].[LEVEL_UNIQUE_NAME]" type="xsd:string"/> <LNum name="[Product].[Prod].[LEVEL_NUMBER]" type="xsd:int"/> <DisplayInfo name="[Product].[Prod].[DISPLAY_INFO]" type="xsd:unsignedInt"/> </HierarchyInfo> <HierarchyInfo name="[Time].[Calendar]"> <UName name="[Time].[Calendar].[MEMBER_UNIQUE_NAME]" type="xsd:string"/> <Caption name="[Time].[Calendar].[MEMBER_CAPTION]" type="xsd:string"/> <LName name="[Time].[Calendar].[LEVEL_UNIQUE_NAME]" type="xsd:string"/> <LNum name="[Time].[Calendar].[LEVEL_NUMBER]" type="xsd:int"/> <DisplayInfo name="[Time].[Calendar].[DISPLAY_INFO]" type="xsd:unsignedInt"/> </HierarchyInfo> </AxisInfo> """ xml = xmlwitch.Builder() with xml.AxisInfo(name="Axis0"): # many measures , then write this on the top with xml.HierarchyInfo(name="[Measures]"): xml.UName(name="[Measures].[MEMBER_UNIQUE_NAME]", type="xs:string") xml.Caption(name="[Measures].[MEMBER_CAPTION]", type="xs:string") xml.LName(name="[Measures].[LEVEL_UNIQUE_NAME]", type="xs:string") xml.LNum(name="[Measures].[LEVEL_NUMBER]", type="xs:int") xml.DisplayInfo(name="[Measures].[DISPLAY_INFO]", type="xs:unsignedInt") return str(xml)
def mdschema_measuregroups_response(self, request): """ Describes the measure groups. :param request: :return: """ xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }): xml.write(mdschema_measuresgroups_xsd) if request.Restrictions.RestrictionList: if (request.Restrictions.RestrictionList.CUBE_NAME == self.selected_cube and request.Properties.PropertyList.Catalog is not None): self.change_cube( request.Properties.PropertyList.Catalog) with xml.row: xml.CATALOG_NAME(self.selected_cube) xml.CUBE_NAME(self.selected_cube) xml.MEASUREGROUP_NAME("default") xml.DESCRIPTION("-") xml.IS_WRITE_ENABLED("true") xml.MEASUREGROUP_CAPTION("default") return str(xml)
def tuples_2_xs0(self, tuples, splitted_df, first_att, axis): """ transform mdx query tuples (list) to xmla xs0 :param tuples: list of tuples :param splitted_df: spllited dataframes (with split_dataframe() function) :param first_att: tuple first attribut :param axis: xs0 | xs1 :return: tuples axis in xml """ xml = xmlwitch.Builder() with xml.Axis(name=axis): with xml.Tuples: for tupls in itertools.chain(*tuples): with xml.Tuple: if (tupls[0][1] in self.executor.measures and len(self.executor.selected_measures) > 1): self._gen_measures_xs0(xml, tupls) if tupls[0][-1] in self.executor.measures: continue self._gen_xs0_tuples(xml, tupls, split_df=splitted_df, first_att=first_att) # Hierarchize' if not self.executor.parser.hierarchized_tuples(): self._gen_measures_xs0(xml, tupls) return xml
def _generate_cells_data_convert2formulas(self): """generate cells data for convert formulas query. for each tuple: <Cell CellOrdinal="0"> <Value>[Measures].[Amount]</Value> </Cell> <Cell CellOrdinal="1"> <Value>Amount</Value> </Cell> <Cell CellOrdinal="2"> <Value>[Measures]</Value> </Cell> """ xml = xmlwitch.Builder() index = 0 for tupl in self.mdx_execution_result: with xml.Cell(CellOrdinal=str(index)): xml.Value(tupl) index += 1 with xml.Cell(CellOrdinal=str(index)): xml.Value(self.executor.parser.split_tuple(tupl)[-1]) index += 1 tupl2list = tupl.split(".") if tupl2list[0].upper() == "[MEASURES]": value = "[Measures]" else: value = "{0}.{0}.[{1}]".format( tupl2list[0], self.executor.tables_loaded[tupl2list[0].replace( "[", "").replace("]", "")].columns[len(tupl2list[4:])], ) with xml.Cell(CellOrdinal=str(index)): xml.Value(value) index += 1 return str(xml)
def _gen_xs0_grouped_tuples(self, axis, tuples_groups): """ generate xs0 axis form query with multiple data groups "exple: select (.. geography..)(..product..)(..time..)" :param axis: ax0 | ax1 ( depends on the mdx query axes ) :param tuples_groups: list of tuples groups :return: tuples axis groups in xml """ xml = xmlwitch.Builder() with xml.Axis(name=axis): with xml.Tuples: for group in tuples_groups: with xml.Tuple: for tupl in self.executor.parser.split_group(group): split_tupl = self.executor.parser.split_tuple(tupl) if split_tupl[0].upper() == "MEASURES": hierarchy = "[Measures]" l_name = "[" + split_tupl[0] + "]" lvl = 0 displayinfo = "0" else: hierarchy = "[{0}].[{0}]".format(split_tupl[0]) l_name = "[{}]".format("].[".join( split_tupl[:3])) lvl = len(split_tupl[4:]) displayinfo = "131076" with xml.Member(Hierarchy=hierarchy): xml.UName("{}".format(tupl.strip(" \t\n"))) xml.Caption("{}".format(split_tupl[-1])) xml.LName(l_name) xml.LNum(str(lvl)) xml.DisplayInfo(displayinfo) return str(xml)
def generate_xs0_one_axis(self, splitted_df, mdx_query_axis="all", axis="Axis0"): """ :param splitted_df: spllited dataframes (with split_dataframe() function) :param mdx_query_axis: axis to which you want generate xs0 xml, (rows, columns or all) :param axis: axis0 or axis1 :return: """ # patch 4 select (...) (...) (...) from bla bla bla if self.executor.check_nested_select(): return self._gen_xs0_grouped_tuples( axis, self.executor.parser.get_nested_select()) xml = xmlwitch.Builder() tuples, first_att = self._generate_tuples_xs0(splitted_df, mdx_query_axis) if tuples: xml = self.tuples_2_xs0(tuples, splitted_df, first_att, axis) elif self.columns_desc["columns"].keys() == [self.executor.facts]: with xml.Axis(name=axis): with xml.Tuples: with xml.Tuple: with xml.Member(Hierarchy="[Measures]"): xml.UName("[Measures].[{}]".format( self.executor.selected_measures[0])) xml.Caption(str( self.executor.selected_measures[0])) xml.LName("[Measures]") xml.LNum("0") xml.DisplayInfo("0") return str(xml)
def cleanup_command(self, shell_id, command_id): """ Clean-up after a command. @see #run_command @param string shell_id: The shell id on the remote machine. See #open_shell @param string command_id: The command id on the remote machine. See #run_command @returns: This should have more error checking but it just returns true for now. @rtype bool """ message_id = uuid.uuid4() node = xmlwitch.Builder(version='1.0', encoding='utf-8') with node.env__Envelope(**self._namespaces): with node.env__Header: self._build_soap_header(node, message_id) self._set_resource_uri_cmd(node) self._set_action_signal(node) self._set_selector_shell_id(node, shell_id) # Signal the Command references to terminate (close stdout/stderr) with node.env__Body: with node.rsp__Signal(CommandId=command_id): node.rsp__Code( 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate' ) response = self.send_message(str(node)) root = ET.fromstring(response) relates_to = next(node for node in root.findall('.//*') if node.tag.endswith('RelatesTo')).text # TODO change assert into user-friendly exception assert uuid.UUID(relates_to.replace('uuid:', '')) == message_id
def discover_literals_response(request): """ Generate information on literals supported by the OlaPy, including data types and values. :param request: :return: """ if (request.Properties.PropertyList.Content == "SchemaData" or request.Properties.PropertyList.Format == "Tabular"): rows = discover_literals_response_rows xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }): xml.write(discover_literals_xsd) for resp_row in rows: with xml.row: for att_name, value in resp_row.items(): xml[att_name](value) return str(xml)
def build(collection, filename=None): # Ex: build('build/{id}/record.xml') if filename is None: xml = xmlwitch.Builder(version='1.0', encoding='utf-8') with xml.collection: for row in collection['members']: build_doc(xml, row, collection['collection']) print(xml) else: for row in collection['members']: xml = xmlwitch.Builder(version='1.0', encoding='utf-8') build_doc(xml, row, collection['collection']) fn = filename.format(**row) if not os.path.exists(os.path.dirname(fn)): os.mkdir(os.path.dirname(fn)) with open(fn, 'w') as fd: fd.write(str(xml))
def testALLTheThings111(self): """Test all other code points for well-formed XML.""" xml = xmlwitch.Builder() with xml['test']: for x in range(0x0, 0xffff): xml.Unichr(unichr(x)) xml.Unimany(''.join([unichr(x) for x in range(0, 0x10000)])) # we also incidentally test that fromstring doesn't raise an exception. self.assertTrue(ET.fromstring(str(xml)) is not None)
def generate_one_axis_info(self, mdx_query_axis="columns", Axis="Axis0"): """ AxisInfo elements for one axis (axis0 or axis1) Example AxisInfo:: <AxesInfo> <AxisInfo name="Axis0"> <HierarchyInfo name="[Geography].[Geography]"> <UName name="[Geography].[Geography].[MEMBER_UNIQUE_NAME]" type="xs:string"/> <Caption name="[Geography].[Geography].[MEMBER_CAPTION]" type="xs:string"/> <LName name="[Geography].[Geography].[LEVEL_UNIQUE_NAME]" type="xs:string"/> <LNum name="[Geography].[Geography].[LEVEL_NUMBER]" type="xs:int"/> <DisplayInfo name="[Geography].[Geography].[DISPLAY_INFO]" type="xs:unsignedInt"/> <PARENT_UNIQUE_NAME name="[Geography].[Geography].[PARENT_UNIQUE_NAME]" type="xs:string"/> <HIERARCHY_UNIQUE_NAME name="[Geography].[Geography].[HIERARCHY_UNIQUE_NAME]" type="xs:string"/> </HierarchyInfo> <HierarchyInfo name="[Product].[Product]"> <UName name="[Product].[Product].[MEMBER_UNIQUE_NAME]" type="xs:string"/> <Caption name="[Product].[Product].[MEMBER_CAPTION]" type="xs:string"/> <LName name="[Product].[Product].[LEVEL_UNIQUE_NAME]" type="xs:string"/> <LNum name="[Product].[Product].[LEVEL_NUMBER]" type="xs:int"/> <DisplayInfo name="[Product].[Product].[DISPLAY_INFO]" type="xs:unsignedInt"/> <PARENT_UNIQUE_NAME name="[Product].[Product].[PARENT_UNIQUE_NAME]" type="xs:string"/> <HIERARCHY_UNIQUE_NAME name="[Product].[Product].[HIERARCHY_UNIQUE_NAME]" type="xs:string"/> </HierarchyInfo> </AxisInfo> </AxesInfo> :param mdx_query_axis: columns or rows (columns by default) :param Axis: Axis0 or Axis1 (Axis0 by default) :return: """ # Hierarchize !! axis_tables = self.columns_desc[mdx_query_axis] axis_tables_without_facts = [ table_name for table_name in axis_tables if table_name != self.executor.facts ] xml = xmlwitch.Builder() # measure must be written at the top if axis_tables: with xml.AxisInfo(name=Axis): # many measures , then write this on the top if (self.executor.facts in axis_tables.keys() and len(axis_tables[self.executor.facts]) > 1): self._gen_measures_one_axis_info(xml) self._generate_table_axis_info(xml, axis_tables_without_facts) # Hierarchize if (not self.executor.parser.hierarchized_tuples() and len(self.columns_desc["columns"].get( self.executor.facts, [1, 1])) == 1): self._gen_measures_one_axis_info(xml) return str(xml)
def _generate_cell_info_convert2formuls(self): """ CellInfo representation for convert to formulas :return: CellInfo structure as string """ xml = xmlwitch.Builder() with xml.CellInfo: xml.Value(name="VALUE") return str(xml)
def generate_cell_info(): xml = xmlwitch.Builder() with xml.CellInfo: xml.Value(name="VALUE") xml.FormatString(name="FORMAT_STRING", **{'type': 'xs:string'}) xml.Language(name="LANGUAGE", **{'type': 'xs:unsignedInt'}) xml.BackColor(name="BACK_COLOR", **{'type': 'xs:unsignedInt'}) xml.ForeColor(name="FORE_COLOR", **{'type': 'xs:unsignedInt'}) xml.FontFlags(name="FONT_FLAGS", **{'type': 'xs:int'}) return str(xml)
def test_utf8_document(self): string = u"""An animated fantasy film from 1978 based on the first """ \ u"""half of J.R.R Tolkien\u2019s Lord of the Rings novel. The """ \ u"""film was mainly filmed using rotoscoping, meaning it was """ \ u"""filmed in live action sequences with real actors and then """ \ u"""each frame was individually animated.""" xml = xmlwitch.Builder(version='1.0', encoding='utf-8') with xml.test: xml.description(string) self.assertEquals(str(xml), self.expected_document('utf8_document.xml'))
def open_shell(self, i_stream='stdin', o_stream='stdout stderr', working_directory=None, env_vars=None, noprofile=False, codepage=437, lifetime=None, idle_timeout=None): """ Create a Shell on the destination host @param string i_stream: Which input stream to open. Leave this alone unless you know what you're doing (default: stdin) @param string o_stream: Which output stream to open. Leave this alone unless you know what you're doing (default: stdout stderr) @param string working_directory: the directory to create the shell in @param dict env_vars: environment variables to set for the shell. Fir instance: {'PATH': '%PATH%;c:/Program Files (x86)/Git/bin/', 'CYGWIN': 'nontsec codepage:utf8'} @returns The ShellId from the SOAP response. This is our open shell instance on the remote machine. @rtype string """ node = xmlwitch.Builder(version='1.0', encoding='utf-8') with node.env__Envelope(**self._namespaces): with node.env__Header: self._build_soap_header(node) self._set_resource_uri_cmd(node) self._set_action_create(node) with node.w__OptionSet: node.w__Option(str(noprofile).upper(), Name='WINRS_NOPROFILE') node.w__Option(str(codepage), Name='WINRS_CODEPAGE') with node.env__Body: with node.rsp__Shell: node.rsp__InputStreams(i_stream) node.rsp__OutputStreams(o_stream) if working_directory: #TODO ensure that rsp:WorkingDirectory should be nested within rsp:Shell node.rsp_WorkingDirectory(working_directory) # TODO: research Lifetime a bit more: http://msdn.microsoft.com/en-us/library/cc251546(v=PROT.13).aspx #if lifetime: # node.rsp_Lifetime = iso8601_duration.sec_to_dur(lifetime) # TODO: make it so the input is given in milliseconds and converted to xs:duration if idle_timeout: node.rsp_IdleTimeOut = idle_timeout if env_vars: with node.rsp_Environment: for key, value in env_vars.items(): node.rsp_Variable(value, Name=key) response = self.send_message(str(node)) root = ET.fromstring(response) return next(node for node in root.findall('.//*') if node.get('Name') == 'ShellId').text
def generate_cell_data(self): # type: () -> text_type """ Returns Cell elements representing the cell data contained by a root element that uses the MDDataSet data type. Example of CellData:: <Cell CellOrdinal="0"> <Value xsi:type="xsi:long">768</Value> </Cell> <Cell CellOrdinal="1"> <Value xsi:type="xsi:long">255</Value> </Cell> :return: CellData as string """ if self.convert2formulas: return self._generate_cells_data_convert2formulas() if (len(self.columns_desc["columns"].keys()) == 0 or len(self.columns_desc["rows"].keys()) == 0 ) and self.executor.facts in self.columns_desc["all"].keys(): # iterate DataFrame horizontally columns_loop = itertools.chain(*[ self.mdx_execution_result["result"][measure] for measure in self.mdx_execution_result["result"].columns ]) else: # iterate DataFrame vertically columns_loop = itertools.chain(*[ tuple for tuple in self.mdx_execution_result["result"].itertuples( index=False) ]) xml = xmlwitch.Builder() index = 0 for value in columns_loop: if np.isnan(value): value = "" with xml.Cell(CellOrdinal=str(index)): xml.Value(str(value), **{"xsi:type": "xsi:long"}) index += 1 return str(xml)
def _raw_get_command_output(self, shell_id, command_id): node = xmlwitch.Builder(version='1.0', encoding='utf-8') with node.env__Envelope(**self._namespaces): with node.env__Header: self._build_soap_header(node) self._set_resource_uri_cmd(node) self._set_action_receive(node) self._set_selector_shell_id(node, shell_id) with node.env__Body: with node.rsp__Receive: node.rsp__DesiredStream('stdout stderr', CommandId=command_id) response = self.send_message(str(node)) root = ET.fromstring(response) stream_nodes = [ node for node in root.findall('.//*') if node.tag.endswith('Stream') ] stdout = stderr = '' return_code = -1 for stream_node in stream_nodes: if stream_node.text: if stream_node.attrib['Name'] == 'stdout': stdout += str( base64.b64decode(stream_node.text.encode('ascii'))) elif stream_node.attrib['Name'] == 'stderr': stderr += str( base64.b64decode(stream_node.text.encode('ascii'))) # We may need to get additional output if the stream has not finished. # The CommandState will change from Running to Done like so: # @example # from... # <rsp:CommandState CommandId="..." State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Running"/> # to... # <rsp:CommandState CommandId="..." State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done"> # <rsp:ExitCode>0</rsp:ExitCode> # </rsp:CommandState> command_done = len([ node for node in root.findall('.//*') if node.get('State', '').endswith('CommandState/Done') ]) == 1 if command_done: return_code = int( next(node for node in root.findall('.//*') if node.tag.endswith('ExitCode')).text) return stdout, stderr, return_code, command_done
def generate_cell_data(self): # # type: () -> text_type """Example of CellData:: <Cell CellOrdinal="0"> <Value xsi:type="xsi:long">768</Value> </Cell> <Cell CellOrdinal="1"> <Value xsi:type="xsi:long">255</Value> </Cell> :return: CellData as string """ if self.convert2formulas: return self._generate_cells_data_convert2formulas() measures_agg = [ column for column in self.mdx_execution_result["result"].columns if "sum(" in column ] if (len(self.columns_desc["columns"].keys()) == 0 or len(self.columns_desc["rows"].keys()) == 0 ) and self.executor.facts in self.columns_desc["all"].keys(): columns_loop = [] for column in measures_agg: for row in (self.mdx_execution_result["result"].select( column).rdd.collect()): columns_loop.append(row[0]) else: # iterate DataFrame vertically columns_loop = itertools.chain(*[ column_value for column_value in self.mdx_execution_result["result"].select( measures_agg).rdd.collect() ]) xml = xmlwitch.Builder() index = 0 for value in columns_loop: # if np.isnan(value): # value = "" with xml.Cell(CellOrdinal=str(index)): xml.Value(str(value), **{"xsi:type": "xsi:long"}) index += 1 return str(xml)
def test_nested_elements(self): xml = xmlwitch.Builder() with xml.feed(xmlns='http://www.w3.org/2005/Atom'): xml.title('Example Feed') xml.updated('2003-12-13T18:30:02Z') with xml.author: xml.name('John Doe') xml.id('urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6') with xml.entry: xml.title('Atom-Powered Robots Run Amok') xml.id('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a') xml.updated('2003-12-13T18:30:02Z') xml.summary('Some text.') self.assertEquals(str(xml), self.expected_document('nested_elements.xml'))
def Envelope(request_id, hold_requests): xml = xmlwitch.Builder(version='1.0', encoding='utf-8') attrs = {'xmlns:soap': 'http://schemas.xmlsoap.org/soap/envelope/', 'xmlns:soap-enc': 'http://schemas.xmlsoap.org/soap/encoding/', 'xmlns:xsd': 'http://www.w3.org/2001/XMLSchema', 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', 'xmlns:cwmp': 'urn:dslforum-org:cwmp-1-2'} with xml['soap:Envelope'](**attrs): with xml['soap:Header']: must_understand_attrs = {'soap:mustUnderstand': '1'} if request_id is not None: xml['cwmp:ID'](str(request_id), **must_understand_attrs) if hold_requests is not None: xml['cwmp:HoldRequests'](hold_requests and '1' or '0', **must_understand_attrs) with xml['soap:Body']: yield xml
def mdschema_measures_response(self, request): """Returns information about the available measures. :param request: :return: """ xml = xmlwitch.Builder() with xml["return"]: with xml.root( xmlns="urn:schemas-microsoft-com:xml-analysis:rowset", **{ "xmlns:xsd": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", }, ): xml.write(mdschema_measures_xsd) if request.Restrictions.RestrictionList: if (request.Restrictions.RestrictionList.CUBE_NAME == self.selected_cube and request.Properties.PropertyList.Catalog is not None): self.change_cube( request.Properties.PropertyList.Catalog) for mes in self.executor.measures: with xml.row: xml.CATALOG_NAME(self.selected_cube) xml.CUBE_NAME(self.selected_cube) xml.MEASURE_NAME(mes) xml.MEASURE_UNIQUE_NAME("[Measures].[" + mes + "]") xml.MEASURE_CAPTION(mes) xml.MEASURE_AGGREGATOR("1") xml.DATA_TYPE("5") xml.NUMERIC_PRECISION("16") xml.NUMERIC_SCALE("-1") xml.MEASURE_IS_VISIBLE("true") xml.MEASURE_NAME_SQL_COLUMN_NAME(mes) xml.MEASURE_UNQUALIFIED_CAPTION(mes) xml.MEASUREGROUP_NAME("default") return str(xml)