用于更新网页中特定字段的Python脚本

时间:2015-02-26 06:05:53

标签: python

我是Python的新手,我的要求是我有CQ网页,需要根据特定字段更新BugID的状态。 这是我正在尝试的示例代码。

import httplib2
import json
import getpass
import urllib
from string import Template
from xml.dom.minidom import parseString

class Credentials():
    def assign_user (self):
        self._user = 'user'

    def assign_passwd (self):
        self._passwd = 'pawrd'
user_cred = Credentials()

class RestLink:

    def __init__(self, link,  baseline_cr= 'ENGR00xxxx'):
        self._link = Template(link)
        self.cr   =  baseline_cr

    def get_link(self):
        return self._link.safe_substitute(recordId=self.cr,
                                    loginid=user_cred.get_user(),
                                    password=user_cred.get_passwd())


class CQBase:
    SERVER                  = 'cq.am.domain.net'
    RESPONSE_OK             = 'OK'
    def __init__(self, logger):
        self._logger = logger

    def send_request(self):
        data = ''
        try:
            conn = httplib2.HTTPConnectionWithTimeout(self.SERVER)
            conn.request("GET", link)
            res = conn.getresponse()
            data = res.read()
            if res.reason != self.RESPONSE_OK:
                raise ParseException('Cannot execute request!')
            conn.close()
        except:
            conn.close()
            raise

        return data

class CQIssueReader(CQBase):
    VIEW_CR_LINK            = '/cqweb/restapi/TSR/ENGR/RECORD/${recordId}?format=JSON&recordType=CR&loginId=${loginid}&password=${password}&noframes=true'

    def __init__(self, cr, logger):
        CQBase.__init__(self, logger)
        self._cr = cr
        self._headline = ''
        self._subtype  = ''
        self._branch   = ''
        self._is_resolved   = 0
        self._is_integrated = 0

        self.parse_cr()

    def parse_cr(self):
        self._is_resolved   = False
        self._is_integrated = False
        data = self.send_request(RestLink(self.VIEW_CR_LINK, self._cr).get_link())
        parsedData = json.loads(data)
        for field in parsedData['fields']:
            if field['FieldName'] == 'Headline':
                self._headline = field['CurrentValue']
            if field['FieldName'] == 'Integrated':
                self._logger.log_details('\tIntegrated = ' + field['CurrentValue'])
                if field['CurrentValue'] == 'Y':
                    self._is_integrated = True
            if field['FieldName'] == 'State':
                self._logger.log_details('\tState = ' + field['CurrentValue'])
                if (field['CurrentValue'] == 'Resolved') or (field['CurrentValue'] == 'Closed')\
                    or (field['CurrentValue'] == 'Verified'):
                    self._is_resolved = True
            if field['FieldName'] == 'Subtype':
                self._subtype = field['CurrentValue']
            if field['FieldName'] == 'BranchName':
                self._branch = field['CurrentValue']
                self._logger.log_details('\tBranchName = ' + self._branch)

    def get_headline(self):
        return self._headline

    def get_subtype(self):
        return self._subtype

    def get_branch_name(self):
        return self._branch

test = CQIssueReader(CQBase)
test_data = CQIssueReader.parse_cr()
print (test_data)

我上面的代码出现以下错误:

Traceback (most recent call last):
  File "test.py", line 97, in <module>
    test = CQIssueReader(CQBase)
TypeError: __init__() missing 1 required positional argument: 'logger'

请指导我哪里出错了。

1 个答案:

答案 0 :(得分:0)

根据def __init__(self, cr, logger):Class需要一个名为logger的参数才能生效。在test = CQIssueReader(CQBase)中,您没有通过记录器。