コード例 #1
3
ファイル: decorators.py プロジェクト: mirceaulinic/iosxr-eznc
    def _jsonify(*vargs, **kvargs):

        ret = fun(*vargs, **kvargs)
        ret_xml = None

        if isinstance(ret, GetReply):
            ret_xml = ret.data_xml
        elif isinstance(ret, RPCReply):
            ret_xml = str(ret)

        if ret_xml:
            ret_json = json.loads(json.dumps(jxmlease.parse(ret_xml)))
            return ret_json
        else:
            reply_obj = None
            if etree.iselement(ret):
                reply_obj = etree.tostring(ret)[:1024]  # up to 1024 chars
            else:
                reply_obj = str(ret)  # trying this
            err = {
                'msg': 'Invalid XML reply',
                'obj': reply_obj
            }
            raise InvalidXMLReplyError(vargs[0]._dev, err)
コード例 #2
1
ファイル: junos.py プロジェクト: likewg/DevOps
def xml_to_json(val):
    if isinstance(val, string_types):
        return jxmlease.parse(val)
    else:
        return jxmlease.parse_etree(val)
コード例 #3
0
ファイル: order.py プロジェクト: Dboe2/brrr
    def perform_request(self, method, resp_format, api_url, payload):
        """:description: POST or PUT request with json or xml used by preview, place and cancel

           :param method: PUT or POST method
           :type method: session, required
           :param resp_format: Desired Response format, defaults to xml
           :type  resp_format: str, required
           :param api_url: API URL
           :type  api_url: str, required
           :param payload: Payload
           :type  payload: str[], required
           :return: Return request
           :rtype: xml or json based on ``resp_format``
           :EtradeRef: https://apisb.etrade.com/docs/api/order/api-order-v1.html

        """

        LOGGER.debug(api_url)
        LOGGER.debug("payload: %s", payload)
        if resp_format == "json":
            req = method(api_url, json=payload, timeout=self.timeout)
        else:
            headers = {"Content-Type": "application/xml"}
            payload = jxmlease.emit_xml(payload)
            LOGGER.debug("xml payload: %s", payload)
            req = method(api_url, data=payload, headers=headers, timeout=self.timeout)

        LOGGER.debug(req.text)
        req.raise_for_status()

        if resp_format == "json":
            return req.json()
        if resp_format is None:
            return jxmlease.parse(req.text)
        return req.text
コード例 #4
0
def sys_license_keys(host):
   dev = Device(host=host, user=username, password=password, normalize=True)
   dev.open()

   rpc = dev.rpc.get_license_key_information()
   rpc_xml = etree.tostring(rpc, pretty_print=True, encoding='unicode')
   dev.close()

   xmlparser = jxmlease.Parser()
   result = jxmlease.parse(rpc_xml)
   print('\n' + 120 * '*' + '\n')
   print('License keys for host {}'.format(host))
   print('\n' + 120 * '*' + '\n')

# if license exists
   if result.get('license-key-information'):
       # For multiple licenses
       if isinstance(result['license-key-information']['license-key'],list):
           for license in result['license-key-information']['license-key']:
               print(license['key-data'])
       # For a single license
       else:
           print(result['license-key-information']['license-key']['key-data'])

# if no license
   else:
       print('No license found')
コード例 #5
0
def get_re_sib_state(device=None):
    """
        Get output of 'show chassis sib'

        example: get_re_sib_state ${dut}

        :param device:
            **REQUIRED** Device handle

        :return: Output of show chassis SIBs
    """

    if (device is None):
        raise Exception("Mandatory arguements are missing ")

    cmd = "show chassis sibs"
    res = device.cli(command=cmd, format='xml').response()
    response = jxmlease.parse(res)['rpc-reply']['sib-information']
    response = response.jdict()
    ans = []
    ans = response['sib-information']['sib']
    state = []
    i = 0
    for sib in ans:
        device.log(message="==sib %s" % (sib), level="debug")
        state.append({})
        slot = sib['slot']
        state[i]['slot'] = int(sib['slot'])
        state[i]['state'] = str(sib['state'])
        state[i]['linkstate'] = str(sib['sib-link-state'])
        state[i]['linkerror'] = str(sib['sib-link-errors'])
        i = i + 1

    device.log(message="==sib state is :   %s" % (state), level="debug")
    return state
コード例 #6
0
def getInterfaceConfig(ipAddress, interface, junosUsername, junosPassword):
    dev = Device(host=ipAddress, user=junosUsername, password=junosPassword, port=22)
    try:
        dev.open(auto_probe=5)
    except:
        return {"Error": "Connection refused"}
    try:
        resp = dev.rpc.get_config(filter_xml=etree.XML("""<configuration>
                                                                  <interfaces>
                                                                      <interface>
                                                                          <name>"""+interface+"""</name>
                                                                          <description/>
                                                                          <unit/>
                                                                      </interface>
                                                                  </interfaces>
                                                              </configuration>"""))
    except:
       dev.close()
       return {"Error":"Failed to find interface"}
    rpc_xml=etree.tostring(resp, encoding='unicode', pretty_print=True)
    result = jxmlease.parse(rpc_xml)
    if result['configuration']==None or result['configuration']=='': #If the configuration comes back Null
        return {"Error":"No configuration"}

    result=result['configuration']['interfaces']['interface']
    if 'description' not in result:
        result['description']=None
    dev.close()
    return result
コード例 #7
0
ファイル: order.py プロジェクト: rjhbrunt/pyetrade
    def place_equity_order(self, resp_format=None, **kwargs):
        """place_equity_order(dev, resp_format, **kwargs) -> resp

           param: resp_format
           type: str
           description: Response format JSON or None = XML

           kwargs: see preview_equity_order
        """

        assert resp_format in (None, "json", "xml")
        LOGGER.debug(kwargs)

        # Test required values
        self.check_order(**kwargs)

        if "previewId" not in kwargs:
            LOGGER.debug("No previewId given, previewing before placing order "
                         "because of an Etrade bug as of 1/1/2019")
            preview = self.preview_equity_order(resp_format, **kwargs)
            if resp_format == "xml":
                preview = jxmlease.parse(preview)
            kwargs["previewId"] = preview["PreviewOrderResponse"][
                "PreviewIds"]["previewId"]
            LOGGER.debug("Got a successful preview with previewId: %s",
                         kwargs["previewId"])

        api_url = self.base_url + "/" + kwargs["accountId"] + "/orders/place"
        # payload creation
        payload = self.build_order_payload("PlaceOrderRequest", **kwargs)

        return self.perform_request(self.session.post, resp_format, api_url,
                                    payload)
コード例 #8
0
ファイル: NAT_RLI11294.py プロジェクト: SrijaGupta/file
def chk_src_summary_rout_inst(device, routng_inst, pool):
    """
    Robot:-Chk Src Summary Rout Inst    device=${r0}  routng_inst=red  pool=s1 

    """
    if device is None:
        raise Exception(
            "'device' is mandatory parameter for configuring nat pool")
    if routng_inst is None:
        raise Exception(
            "'routng_inst' is mandatory parameter for configuring routing instance"
        )

    if device is not None:
        device.cli(command='show security nat source pool ' + pool).response()
        result = device.cli(command='show security nat source pool ' + pool,
                            format='xml').response()
        status = jxmlease.parse(result)
        device.log(status)
        print(' check for S1 S2 src pool ')
        #       if (status['rpc-reply']['source-nat-pool-detail-information']['source-nat-pool-info-entry']['routing-instance-name']) != routng_inst:

        if (status['rpc-reply']['source-nat-pool-detail-information']
            ['source-nat-pool-info-entry']['routing-instance-name']
            ) != routng_inst:
            device.log(
                level='ERROR',
                message='s1 pool does not have correct routing instance')
            raise Exception("value not present")
コード例 #9
0
ファイル: NAT_RLI11294.py プロジェクト: SrijaGupta/file
def chk_src_summary_prefix(device=None, pool=None, addr=None):
    """
    Robot :-  Chk Src Summary Prefix      device=${r0}  pool=s1  addr=2010::/64

    """
    if device is None:
        raise Exception(
            "'device' is mandatory parameter for configuring nat pool")
    if device is not None:
        device.cli(command='show security nat source pool ' + pool).response()
        result = device.cli(command='show security nat source pool ' + pool,
                            format='xml').response()
        status = jxmlease.parse(result)
        device.log(status)

    print(' check for S1 src pool ')
    if (status['rpc-reply']['source-nat-pool-detail-information']
        ['source-nat-pool-info-entry']['pool-name']) != pool:
        device.log(level='ERROR', message='s1 pool info not visible')
        raise Exception("value not present")
    if (status['rpc-reply']['source-nat-pool-detail-information']
        ['source-nat-pool-info-entry']['source-pool-address-range']
        ['address-range-low']) != addr:
        device.log(level='ERROR',
                   message='s1 pool does not have address prefixx proper')
        raise Exception("value not present")
コード例 #10
0
ファイル: market.py プロジェクト: ehubbard/pyetrade
def strikes_from_optionchains_XML(xml_text):
    ''' strikes_from_optionchains_XML(xml_text)
    
        Given xml_text from an option chains download, return list of option chain dates and strikes
        return list of (dt.date,strikePrice) tuples
        
        { 'call': {'expireDate': {'day': '18',
                   'expiryType': 'MONTHLY',
                   'month': '5',
                   'year': '2018'},
          'product': {'exchangeCode': 'CINC',
                      'symbol': "AAPL May 18 '18 $250 Call",
                      'typeCode': 'OPTN'},
          'rootSymbol': 'AAPL',
          'strikePrice': '250.000000'
        }
          
    '''
    rtn = list()
    try:
        xmlobj = jxmlease.parse(xml_text)
        z = xmlobj['OptionChainResponse']['optionPairs']
    except:
        return rtn


# determine whether call or put from first item in list
    if 'call' in z[0]:
        option_type = 'call'
    else:
        option_type = 'put'

    rtn = [float(this_opt[option_type]['strikePrice']) for this_opt in z]
    return rtn
コード例 #11
0
def ping(dest_ip=None, **kwargs):
    """
    Send a ping RPC to a device

    dest_ip
      The IP of the device to ping

    dev_timeout : 30
        The NETCONF RPC timeout (in seconds)

    rapid : False
        When ``True``, executes ping at 100pps instead of 1pps

    ttl
        Maximum number of IP routers (IP hops) allowed between source and
        destination

    routing_instance
      Name of the routing instance to use to send the ping

    interface
      Interface used to send traffic

    count : 5
      Number of packets to send

    CLI Examples:

    .. code-block:: bash

        salt 'device_name' junos.ping '8.8.8.8' count=5
        salt 'device_name' junos.ping '8.8.8.8' ttl=1 rapid=True
    """
    conn = __proxy__["junos.conn"]()
    ret = {}

    if dest_ip is None:
        ret["message"] = "Please specify the destination ip to ping."
        ret["out"] = False
        return ret

    op = {"host": dest_ip}
    if "__pub_arg" in kwargs:
        if kwargs["__pub_arg"]:
            if isinstance(kwargs["__pub_arg"][-1], dict):
                op.update(kwargs["__pub_arg"][-1])
    else:
        op.update(kwargs)

    op["count"] = six.text_type(op.pop("count", 5))
    if "ttl" in op:
        op["ttl"] = six.text_type(op["ttl"])

    ret["out"] = True
    try:
        ret["message"] = jxmlease.parse(etree.tostring(conn.rpc.ping(**op)))
    except Exception as exception:  # pylint: disable=broad-except
        ret["message"] = 'Execution failed due to "{0}"'.format(exception)
        ret["out"] = False
    return ret
コード例 #12
0
    def perform_request(self, method, resp_format, api_url, payload):
        """
        run a post or put request
        with json or xml
        used by preview, place and cancel
        """

        LOGGER.debug(api_url)
        LOGGER.debug("payload: %s", payload)
        if resp_format == "json":
            req = method(api_url, json=payload, timeout=self.timeout)
        else:
            headers = {"Content-Type": "application/xml"}
            payload = jxmlease.emit_xml(payload)
            LOGGER.debug("xml payload: %s", payload)
            req = method(api_url, data=payload, headers=headers, timeout=self.timeout)

        LOGGER.debug(req.text)
        req.raise_for_status()

        if resp_format == "json":
            return req.json()
        if resp_format is None:
            return jxmlease.parse(req.text)
        return req.text
コード例 #13
0
ファイル: reservation.py プロジェクト: arona14/prodfteros
 def ticketing_agent(self, data):
     ticket_agent_list = []
     tkt_agent_list = []
     try:
         for path, _, node in jxmlease.parse(
                 data,
                 generator=
                 "tir38:TravelItinerary/tir38:ItineraryInfo/tir38:Ticketing"
         ):
             tkt_agent_list1 = node.get_xml_attr('eTicketNumber', "")
             tkt_agent_list.append(str(tkt_agent_list1))
         #print tkt_agent_list
         if tkt_agent_list == ['']:
             return 'empty'
         for i in tkt_agent_list:
             if i == '':
                 pass
             else:
                 try:
                     tkt_agent = re.search(
                         r"(/)[A-Z][ ][A-Z0-9_]+[*][A-Z0-9_]+", i,
                         flags=0).group(0)
                 except:
                     tkt_agent = re.search(r"(/)[A-Z][ ][A-Z0-9_]+",
                                           i,
                                           flags=0).group(0)
                 #print tkt_agent
                 t_agent = tkt_agent[len(tkt_agent) - 3:len(tkt_agent)]
                 return t_agent
     except:
         return 'empty'
コード例 #14
0
    def issue_date(self, data):

        date_list = []
        date_time = []
        try:
            for path, _, node in jxmlease.parse(
                    data,
                    generator=
                    "tir38:TravelItinerary/tir38:ItineraryInfo/tir38:Ticketing"
            ):
                date_list1 = node.get_xml_attr('eTicketNumber', "")
                date_list.append(str(date_list1))
            for i in date_list:
                if i == '' or 'TV' in i or 'TK' in i:
                    pass
                else:
                    value = i.split('/')
                    heure = value[-2].split(' ')
                    time = value[-1] + ' ' + heure[-1][0:2] + 'H' + heure[-1][
                        2:4]
                    date_time.append(time)
        except:
            date_time = None

        return date_time
コード例 #15
0
    def get_schema_description(self):
        if not HAS_JXMLEASE:
            raise ValueError(
                "jxmlease is required to store response in json format "
                "but does not appear to be installed. "
                "It can be installed using `pip install jxmlease`")

        get_filter = """
        <filter type="subtree" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
          <netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring">
            <schemas/>
          </netconf-state>
        </filter>
        """
        try:
            resp = self._conn.get(filter=get_filter)
        except ConnectionError as e:
            raise ValueError(to_text(e))

        res_json = jxmlease.parse(resp)
        if "rpc-reply" in res_json:
            self._all_schema_list = res_json["rpc-reply"]["data"][
                "netconf-state"]["schemas"]["schema"]
        else:
            self._all_schema_list = res_json["data"]["netconf-state"][
                "schemas"]["schema"]

        for index, schema_list in enumerate(self._all_schema_list):
            self._all_schema_identifier_list.append(schema_list["identifier"])
        return
コード例 #16
0
def useCheck(a):
    interface_L = []
    description_L = []
    with open(a, 'rb') as xml:
        root = jxmlease.parse(xml)
    for node in root.find_nodes_with_tag('chassis-module'):
        name = node['name'].get_cdata()
        for snode in node.find_nodes_with_tag('chassis-sub-module'):
            sname = snode['name'].get_cdata()
            for ssnode in snode.find_nodes_with_tag('chassis-sub-sub-module'):
                ssname = ssnode['name'].get_cdata()
                ssdescription = ssnode['description'].get_cdata()
                iname = str(int(re.findall('\d+', name)[0]))
                isname = str(int(re.findall('\d+', sname)[0]))
                issname = str(int(re.findall('\d+', ssname)[0]))
                if ssdescription == "SFP+-10G-SR" or ssdescription == "SFP+-10G-LR":
                    interface = "xe-" + iname + "/" + isname + "/" + issname
                    interface_L.append(interface)
                    description_L.append(ssnode['description'].get_cdata())
                elif ssdescription == "XFP-10G-SR" or ssdescription == "XFP-10G-LR":
                    interface = "xe-" + iname + "/" + isname + "/" + issname
                    interface_L.append(interface)
                    description_L.append(ssnode['description'].get_cdata())
                elif ssdescription == "SFP-SX" or ssdescription == "SFP-LX10":
                    interface = "ge-" + iname + "/" + isname + "/" + issname
                    interface_L.append(interface)
                    description_L.append(ssnode['description'].get_cdata())
                else:
                    #print('please check interface type')
                    interface = iname + "/" + isname + "/" + issname
                    interface_L.append(interface)
                    description_L.append(ssnode['description'].get_cdata())
    r = dict(zip(interface_L, description_L))
    return r
コード例 #17
0
ファイル: junos.py プロジェクト: pombredanne/usystem
def ping(dest_ip=None, **kwargs):
    '''
    Send a ping RPC to a device

    dest_ip
      The IP of the device to ping

    dev_timeout : 30
        The NETCONF RPC timeout (in seconds)

    rapid : False
        When ``True``, executes ping at 100pps instead of 1pps

    ttl
        Maximum number of IP routers (IP hops) allowed between source and
        destination

    routing_instance
      Name of the routing instance to use to send the ping

    interface
      Interface used to send traffic

    count : 5
      Number of packets to send

    CLI Examples:

    .. code-block:: bash

        salt 'device_name' junos.ping '8.8.8.8' count=5
        salt 'device_name' junos.ping '8.8.8.8' ttl=1 rapid=True
    '''
    conn = __proxy__['junos.conn']()
    ret = dict()

    if dest_ip is None:
        ret['message'] = 'Please specify the destination ip to ping.'
        ret['out'] = False
        return ret

    op = {'host': dest_ip}
    if '__pub_arg' in kwargs:
        if kwargs['__pub_arg']:
            if isinstance(kwargs['__pub_arg'][-1], dict):
                op.update(kwargs['__pub_arg'][-1])
    else:
        op.update(kwargs)

    op['count'] = six.text_type(op.pop('count', 5))
    if 'ttl' in op:
        op['ttl'] = six.text_type(op['ttl'])

    ret['out'] = True
    try:
        ret['message'] = jxmlease.parse(etree.tostring(conn.rpc.ping(**op)))
    except Exception as exception:
        ret['message'] = 'Execution failed due to "{0}"'.format(exception)
        ret['out'] = False
    return ret
コード例 #18
0
    def get_one_schema(self, schema_id, result):
        if self._all_schema_list is None:
            self.get_schema_description()

        found = False
        data_model = None

        # Search for schema that are supported by device.
        # Also get namespace for retrieval
        schema_cache_entry = {}
        for index, schema_list in enumerate(self._all_schema_list):
            if schema_id == schema_list["identifier"]:
                found = True
                break

        if found:
            content = ("<identifier>%s</identifier>" % schema_id)
            xmlns = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"
            xml_request = '<%s xmlns="%s"> %s </%s>' % ('get-schema', xmlns,
                                                        content, 'get-schema')
            try:
                response = self._conn.dispatch(xml_request)
            except ConnectionError as e:
                raise ValueError(to_text(e))
            res_json = jxmlease.parse(response)
            data_model = res_json["rpc-reply"]["data"]
            display.vvv("Fetched '%s' yang model" % schema_id)
            result['fetched'][schema_id] = data_model
            self._schema_cache.append(schema_cache_entry)
        else:
            raise AnsibleError("Fail to fetch '%s' yang model" % schema_id)

        return found, data_model
コード例 #19
0
    def get_schema_description(self):
        content = '''
          <filter>
            <netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring">
              <schemas>
                <schema>
                    <identifier/>
                </schema>
              </schemas>
            </netconf-state>
          </filter>
        '''
        xml_request = '<%s>%s</%s>' % ('get', content, 'get')
        try:
            response = self._conn.dispatch(xml_request)
        except ConnectionError as e:
            raise ValueError(to_text(e))
        response = to_bytes(response, errors='surrogate_or_strict')
        tree = etree.ElementTree(etree.fromstring(response))
        tree_root = tree.getroot()
        res_str = etree.tostring(tree_root, pretty_print=True)

        if not HAS_JXMLEASE:
            raise ValueError(
                'jxmlease is required to store response in json format'
                'but does not appear to be installed. '
                'It can be installed using `pip install jxmlease`')
        res_json = jxmlease.parse(res_str)
        self._all_schema_list = res_json["data"]["netconf-state"]["schemas"][
            "schema"]
        return
コード例 #20
0
def notifications():
    """	The URI you provide here is where PG&E will send notifications that customer-authorized data is available  """

    if request.method == 'POST':
        xml_dict = parse(
            request.data)  #Create dictionary from XML using jxmlease library

        xml_dict.prettyprint()

        client_credentials = cc.get_client_access_token(
            'https://api.pge.com/datacustodian/oauth/v2/token')

        bulk_data = []
        for resource in xml_dict[u'ns0:BatchList'][u'ns0:resources']:
            """When a get request is made to the bulk data url, PGE will respond by posting XML data to this view function. The xml data will have one or more url's
            that can be used to access the bulk data. The urls look identical the bulk data url, but there is an extra paramater at the end.
            ex. https://api.pge.com/GreenButtonConnect/espi/1_1/resource/Batch/Bulk/50098?correlationID=f5ee53cf-247b-4a2f-abdc-7f650fecb1b5

            This for-loop will grab all of these url's and make a get request to each one. PGE will then respond to the GET request by returning the bulk data
            XML immediately, which is then added to the bulk_data list for processing.
            """
            bulk_data.append(
                api.simple_request(resource,
                                   client_credentials[u'client_access_token']))
        """This for-loop will work through the bulk_data list containing one or more XML trees. It will parse the tree, and insert the useful parts into the
        database. Before calling db.session.commit(), we also check to see if the data is already in the system, and ignores the data if true.
        """

        task = process_xml.delay(bulk_data[0]['data'])

    return render_template('public/oauth.html',
                           page_title='Notification Bucket')
コード例 #21
0
    def get_account_positions(self, account_id, dev=True, resp_format="json"):
        """get_account_positions(dev, account_id, resp_format) -> resp
        param: account_id
        type: string
        required: true
        description: account id key
        param: dev
        type: bool
        description: API enviornment
        param: resp_format
        type: str
        description: Response format
        rformat: json
        rtype: dict
        rformat: other than json
        rtype: str"""

        if dev:
            api_url = self.base_url_dev
        else:
            api_url = self.base_url_prod

        api_url += "/" + account_id + "/portfolio"
        if resp_format == "json":
            api_url += ".json"

        LOGGER.debug(api_url)
        req = self.session.get(api_url)
        req.raise_for_status()
        LOGGER.debug(req.text)

        if resp_format == "json":
            return req.json()
        return jxmlease.parse(req.text)
コード例 #22
0
ファイル: fetchdata.py プロジェクト: arona14/fteros
    def ticketed_number_list(self, data):
        """ Retrieve a list which contains ticket number """

        ticket_number_list = []
        tkt_number_list = []
        try:

            for path, _, node in jxmlease.parse(
                    data,
                    generator=
                    "tir38:TravelItinerary/tir38:ItineraryInfo/tir38:Ticketing"
            ):
                ticket_number = node.get_xml_attr('eTicketNumber', "")
                ticket_number_list.append(str(ticket_number))
            for i in ticket_number_list:
                if i == '':
                    pass
                else:

                    tkt_numbers = re.search(r"(TE)[ ][0-9]+", i,
                                            flags=0).group(0)

                    tkt_number = re.search(r'[0-9]+', tkt_numbers,
                                           flags=0).group()
                    tkt_number_list.append(tkt_number)
        except:
            tkt_number_list = ['N/A']

        return tkt_number_list
コード例 #23
0
ファイル: reservation.py プロジェクト: arona14/prodfteros
    def ticketing_pcc(self, data):
        ticket_pcc_list = []
        tkt_pcc_list = []
        try:

            for path, _, node in jxmlease.parse(
                    data,
                    generator=
                    "tir38:TravelItinerary/tir38:ItineraryInfo/tir38:Ticketing"
            ):
                ticket_pcc1 = node.get_xml_attr('eTicketNumber', "")
                ticket_pcc_list.append(str(ticket_pcc1))
            #print ticket_pcc_list
            if ticket_pcc_list == ['']:
                return 'empty'
            for i in ticket_pcc_list:
                if i == '':
                    pass
                else:
                    tkt_pcc = re.search(r"[(/)][A-Z][ ][A-Z0-9_]{4}",
                                        i,
                                        flags=0).group(0)
                    tkt = re.search(r'[ ][A-Z0-9]+', tkt_pcc, flags=0).group()
                    return tkt
        except:
            return 'empty'
コード例 #24
0
    def __send_api_request(self, api_call, params={}, data=None):
        url = self.__urlBuilder.buildUrl(api_call, params)

        # if data is none, then we send a GET request, if not, then we send a POST request
        if data is None:
            response = urlopen(url).read()
        else:
            if isinstance(data, str):
                data_str = data
            else:
                data_str = urlencode(data)
            response = urlopen(url, data=data_str.encode()).read()

        try:
            rawXml = parse(response)["response"]
        except Exception as e:
            raise BBBException("XMLSyntaxError", e.message)

        # get default config xml request will simply return the xml file without
        # returncode, so it will cause an error when try to check the return code
        if api_call != ApiMethod.GET_DEFAULT_CONFIG_XML:
            if rawXml["returncode"] == "FAILED":
                raise BBBException(rawXml["messageKey"], rawXml["message"])

        return rawXml
コード例 #25
0
ファイル: pge.py プロジェクト: rogaha/SolarMonitor
    def get_access_token(self, url, code, redirect_uri, energy_account):
        request_params = {
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        }
        header_params = {'Authorization': self.base64code}
        request = requests.post(url,
                                data=request_params,
                                headers=header_params,
                                cert=self.cert)
        if str(request.status_code) == "200":
            res = request.json()
            res.update({"status": request.status_code})

            subscription_id = res[u'resourceURI'].rsplit('/', 1)[-1]

            usage_point_xml = requests.get(
                'https://api.pge.com/GreenButtonConnect/espi/1_1/resource/Subscription/{}/UsagePoint'
                .format(subscription_id),
                data=request_params,
                headers={
                    'Authorization':
                    'Bearer {}'.format(res.get('access_token', None))
                },
                cert=self.cert)
            print usage_point_xml.text

            # Save the access and refresh token to DB
            energy_account.pge_refresh_token = res.get('refresh_token', None)
            energy_account.pge_access_token = res.get('access_token', None)
            energy_account.pge_subscription_id = subscription_id
            try:
                from jxmlease import parse
                print parse(usage_point_xml.text,
                            xml_attribs=True).prettyprint()
                energy_account.pge_usage_point = get_usage_point_from_xml(
                    usage_point_xml.text)
            except:
                energy_account.pge_usage_point = 'Not found.'

            db.session.commit()

            return res

        response = {"status": request.status_code, "error": request.text}
        return response
コード例 #26
0
def peer_full():
    try:
        # Log into the switch
        hostname = ip_lookup(input('IP or Hostname of device: '))
        username = input('Username: '******'Password: '******'unicode'))

        routing_instances = []
        try:
            for r_instance in conf_parsed['configuration']['routing-instances']['instance']:
                routing_instances.append((r_instance['name'].get_cdata()))
        except:
            routing_instances.append('Empty')

        sorted_instances = sorted(routing_instances, key=my_Sort)

        for instance in sorted_instances:
            if 'Empty' not in sorted_instances:
                try:
                    rpc = dev.rpc.get_bgp_summary_information(instance=instance)
                    result = jxmlease.parse(etree.tostring(rpc, pretty_print=True, encoding='unicode'))
                    print('Routing Instance: ', instance)
                    for neighbor in result['bgp-information']['bgp-peer']:
                        print(neighbor['peer-as'] + ":" + rev_dns(neighbor['peer-address']) + '({})'.format(
                            neighbor['peer-address']),
                              "[" + peer_status(neighbor['peer-state']), neighbor['elapsed-time'] + "]")
                    else:
                        pass
                except:
                    pass
            else:
                rpc = dev.rpc.get_bgp_summary_information()
                result = jxmlease.parse(etree.tostring(rpc, pretty_print=True, encoding='unicode'))
                for neighbor in result['bgp-information']['bgp-peer']:
                    print(neighbor['peer-as'] + ":" + rev_dns(neighbor['peer-address']),
                          "[" + peer_status(neighbor['peer-state']),
                          neighbor['elapsed-time'] + "]")

        dev.close()
    except:
        sys.exit('Bad password.  Exiting')
コード例 #27
0
 def nlayersunmixed(self):
     """
 Find the number of component tiff layers from the xml metadata
 """
     with open(self.componenttiffsfolder / "batch_procedure.ifp",
               "rb") as f:
         for path, _, node in jxmlease.parse(f, generator="AllComponents"):
             return int(node.xml_attrs["dim"])
コード例 #28
0
    def get_account_balance(
        self,
        account_id,
        account_type=None,
        real_time=True,
        dev=True,
        resp_format="json",
    ):
        """get_account_balance(dev, resp_format)
        param: account_id
        type: int
        required: true
        description: Numeric account id
        param: dev
        type: bool
        description: API enviornment
        param: resp_format
        type: str
        description: Response format
        rformat: json
        rtype: dict
        rformat: other than json
        rtype: str"""

        uri = "balance"
        payload = {"realTimeNAV": real_time, "instType": "BROKERAGE"}
        if account_type:
            payload["accountType"] = account_type

        if dev:
            if resp_format == "json":
                api_url = "%s/%s/%s.%s" % (
                    self.base_url_dev,
                    account_id,
                    uri,
                    resp_format,
                )
            elif resp_format == "xml":
                api_url = "%s/%s/%s" % (self.base_url_dev, account_id, uri)
        else:
            if resp_format == "json":
                api_url = "%s/%s/%s.%s" % (
                    self.base_url_prod,
                    account_id,
                    uri,
                    resp_format,
                )
            elif resp_format == "xml":
                api_url = "%s/%s/%s" % (self.base_url_prod, account_id, uri)
        LOGGER.debug(api_url)
        req = self.session.get(api_url, params=payload)
        req.raise_for_status()
        LOGGER.debug(req.text)

        if resp_format == "json":
            return req.json()
        else:
            return jxmlease.parse(req.text)
コード例 #29
0
  def getdata(self):
    """
    Reads the annotations and gives the rectangles,
    global variables, perimeters, and microscope name
    """
    rectangles = []
    globals = []
    perimeters = []
    maxdepth = 1
    microscopename = None
    with open(self.__filename, "rb") as f:
      for path, _, node in jxmlease.parse(
        f,
        generator="/AnnotationList/Annotations/Annotations-i"
      ):
        annotation = AnnotationFactory(node, pscale=self.pscale)
        globalkwargs = annotation.globals
        if globalkwargs is not None: globals.append(ROIGlobals(**globalkwargs, pscale=self.pscale))
        perimeterkwargs = annotation.perimeters
        if perimeterkwargs is not None: perimeters += [
          ROIPerimeter(**kwargs, pscale=self.pscale)
            for kwargs in perimeterkwargs
        ]

        for field in annotation.fields:
          if field.nestdepth > 2:
            raise ValueError("Found an ROIAnnotation within another ROIAnnotation, did not expect this")
          #don't use RectangleAnnotations if there are also ROIAnnotations
          #(inherited from matlab code, not sure where this logic comes in)
          if field.nestdepth < maxdepth: continue
          if field.nestdepth > maxdepth:
            del rectangles[:]
            maxdepth = field.nestdepth

          if not field.isacquired: continue
          rectangles.append(
            Rectangle(
              n=len(rectangles)+1,
              x=field.x,
              y=field.y,
              cx=field.cx,
              cy=field.cy,
              w=field.w,
              h=field.h,
              t=field.time,
              file=field.im3path.name,
              pscale=self.pscale,
              readingfromfile=False,
              xmlfolder=self.__xmlfolder,
            )
          )
          if microscopename is None:
            microscopename = str(annotation.microscopename)
          elif microscopename != annotation.microscopename:
            raise ValueError("Found multiple different microscope names '{microscopename}' '{annotation.microscopename}'")

    return rectangles, globals, perimeters, microscopename
コード例 #30
0
 def samplelocation(self):
     """
 Find the location of the image on the slide from the xml metadata
 """
     with open(self.parametersxmlfile, "rb") as f:
         for path, _, node in jxmlease.parse(f, generator="/IM3Fragment/D"):
             if node.xml_attrs["name"] == "SampleLocation":
                 return np.array([float(_) for _ in str(node).split()
                                  ]) * units.onemicron(pscale=self.apscale)
コード例 #31
0
def main():
    """entry point for module execution
    """
    argument_spec = dict(
        rpc=dict(type="str", required=True),
        xmlns=dict(type="str"),
        content=dict(),
        display=dict(choices=['json', 'pretty', 'xml'])
    )

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    rpc = module.params['rpc']
    xmlns = module.params['xmlns']
    content = module.params['content']
    display = module.params['display']

    if rpc is None:
        module.fail_json(msg='argument `rpc` must not be None')

    rpc = rpc.strip()
    if len(rpc) == 0:
        module.fail_json(msg='argument `rpc` must not be empty')

    if rpc in ['close-session']:
        # explicit close-session is not allowed, as this would make the next
        # NETCONF operation to the same host fail
        module.fail_json(msg='unsupported operation `%s`' % rpc)

    if display == 'json' and not HAS_JXMLEASE:
        module.fail_json(msg='jxmlease is required to display response in json format'
                             'but does not appear to be installed. '
                             'It can be installed using `pip install jxmlease`')

    xml_req = get_xml_request(module, rpc, xmlns, content)
    response = dispatch(module, xml_req)

    xml_resp = tostring(response)
    output = None

    if display == 'xml':
        output = remove_namespaces(xml_resp)
    elif display == 'json':
        try:
            output = jxmlease.parse(xml_resp)
        except Exception:
            raise ValueError(xml_resp)
    elif display == 'pretty':
        output = tostring(response, pretty_print=True)

    result = {
        'stdout': xml_resp,
        'output': output
    }

    module.exit_json(**result)
コード例 #32
0
def callback(notif):
    print('-->>')
    print('Event time      : %s' % notif.event_time)
    print('Subscription Id : %d' % notif.subscription_id)
    print('Type            : %d' % notif.type)
    print('Data            :')
    j = jxmlease.parse(notif.datastore_xml)
    print(json.dumps(j, indent=2, sort_keys=True))
    print('<<--')
コード例 #33
0
ファイル: junos.py プロジェクト: bryson/salt
def rpc(cmd=None, dest=None, format='xml', *args, **kwargs):
    '''
    This function executes the rpc provided as arguments on the junos device.
    The returned data can be stored in a file whose destination can be
    specified with 'dest' keyword in the arguments.

    Usage:

    .. code-block:: bash

        salt 'device' junos.rpc 'get_config' 'text' filter='<configuration><system/></configuration>'

        salt 'device' junos.rpc 'get-interface-information' '/home/user/interface.log' interface_name='lo0' terse=True


    Options:
      * cmd: the rpc to be executed
      * dest: destination file where the rpc ouput is dumped
      * format: the format in which the rpc reply must be stored in file specified in the dest (used only when dest is specified)
      * args: other arguments as taken by rpc call of PyEZ
      * kwargs: keyworded arguments taken by rpc call of PyEZ
    '''

    conn = __proxy__['junos.conn']()
    ret = dict()
    ret['out'] = True

    op = dict()
    if '__pub_arg' in kwargs:
        if isinstance(kwargs['__pub_arg'][-1], dict):
            op.update(kwargs['__pub_arg'][-1])
    else:
        op.update(kwargs)

    if dest is None and format != 'xml':
        log.warning(
            'Format ignored as it is only used for output which is dumped in the file.')

    write_response = ''
    try:
        if cmd in ['get-config', 'get_config']:
            filter_reply = None
            if 'filter' in op:
                filter_reply = etree.XML(op['filter'])

            xml_reply = getattr(
                conn.rpc,
                cmd.replace('-',
                            '_'))(filter_reply,
                                  options=op)
            ret['message'] = jxmlease.parse(etree.tostring(xml_reply))
            write_response = etree.tostring(xml_reply)

            if dest is not None and format != 'xml':
                op.update({'format': format})
                rpc_reply = getattr(
                    conn.rpc,
                    cmd.replace('-',
                                '_'))(filter_reply,
                                      options=op)
                if format == 'json':
                    write_response = json.dumps(rpc_reply, indent=1)
                else:
                    write_response = rpc_reply.text
        else:

            xml_reply = getattr(conn.rpc, cmd.replace('-', '_'))(**op)
            ret['message'] = jxmlease.parse(etree.tostring(xml_reply))
            write_response = etree.tostring(xml_reply)

            if dest is not None and format != 'xml':
                rpc_reply = getattr(
                    conn.rpc,
                    cmd.replace('-',
                                '_'))({'format': format},
                                      **op)
                if format == 'json':
                    write_response = json.dumps(rpc_reply, indent=1)
                else:
                    write_response = rpc_reply.text

    except Exception as exception:
        ret['message'] = 'Execution failed due to "{0}"'.format(exception)
        ret['out'] = False

    if dest is not None:
        with fopen(dest, 'w') as fp:
            fp.write(write_response)

    return ret
コード例 #34
0
ファイル: junos_command.py プロジェクト: awiddersheim/ansible
def main():
    """entry point for module execution
    """
    argument_spec = dict(
        commands=dict(type='list'),
        rpcs=dict(type='list'),

        display=dict(choices=['text', 'json', 'xml', 'set'], aliases=['format', 'output']),

        wait_for=dict(type='list', aliases=['waitfor']),
        match=dict(default='all', choices=['all', 'any']),

        retries=dict(default=10, type='int'),
        interval=dict(default=1, type='int')
    )

    argument_spec.update(junos_argument_spec)

    required_one_of = [('commands', 'rpcs')]

    module = AnsibleModule(argument_spec=argument_spec,
                           required_one_of=required_one_of,
                           supports_check_mode=True)

    warnings = list()
    conn = get_connection(module)
    capabilities = get_capabilities(module)

    if capabilities.get('network_api') == 'cliconf':
        if any((module.params['wait_for'], module.params['match'], module.params['rpcs'])):
            module.warn('arguments wait_for, match, rpcs are not supported when using transport=cli')
        commands = module.params['commands']

        output = list()
        display = module.params['display']
        for cmd in commands:
            # if display format is not mentioned in command, add the display format
            # from the modules params
            if ('display json' not in cmd) and ('display xml' not in cmd):
                if display and display != 'text':
                    cmd += ' | display {0}'.format(display)
            output.append(conn.get(command=cmd))

        lines = [out.split('\n') for out in output]
        result = {'changed': False, 'stdout': output, 'stdout_lines': lines}
        module.exit_json(**result)

    items = list()
    items.extend(parse_commands(module, warnings))
    items.extend(parse_rpcs(module))

    wait_for = module.params['wait_for'] or list()
    conditionals = [Conditional(c) for c in wait_for]

    retries = module.params['retries']
    interval = module.params['interval']
    match = module.params['match']

    while retries > 0:
        responses = rpc(module, items)
        transformed = list()
        output = list()
        for item, resp in zip(items, responses):
            if item['xattrs']['format'] == 'xml':
                if not HAS_JXMLEASE:
                    module.fail_json(msg='jxmlease is required but does not appear to be installed. '
                                         'It can be installed using `pip install jxmlease`')

                try:
                    json_resp = jxmlease.parse(resp)
                    transformed.append(json_resp)
                    output.append(json_resp)
                except:
                    raise ValueError(resp)
            else:
                transformed.append(resp)

        for item in list(conditionals):
            try:
                if item(transformed):
                    if match == 'any':
                        conditionals = list()
                        break
                    conditionals.remove(item)
            except FailedConditionalError:
                pass

        if not conditionals:
            break

        time.sleep(interval)
        retries -= 1

    if conditionals:
        failed_conditions = [item.raw for item in conditionals]
        msg = 'One or more conditional statements have not been satisfied'
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    result = {
        'changed': False,
        'warnings': warnings,
        'stdout': responses,
        'stdout_lines': to_lines(responses)
    }

    if output:
        result['output'] = output

    module.exit_json(**result)