繁体   English   中英

获取错误:AttributeError:类 <CLASS NAME> 没有属性“ <METHOD NAME> &#39;

[英]Getting error: AttributeError: class <CLASS NAME> has no attribute '<METHOD NAME>'

这是代码:

1 #!/usr/bin/env python
  2 
  3 import re, os, sys, jira, subprocess
  4 
  5 class Check_jira:
  6 
  7     def verify_commit_text(self, tags):
  8         for line in tags:
  9             if re.match('^NO-TIK',line):
 10                 return True
 11             elif re.match('^NO-REVIEW', line):
 12                 return True
 13             elif re.match(r'[a-zA-Z]+-\d+', line):
 14                 # Validate the JIRA ID
 15                 m = re.search("([a-zA-Z]+-\d+)",line)
 16                 if m:
 17                     my_args = m.group(1)
 18                     result = Check_jira.CheckForJiraIssueRecord(my_args)
 19                     if result == False:
 20                         util.warn("%s does not exist"%my_args)
 21                     else:
 22                         return True
 23                 return True
 24             else:
 25                 return False
 26 if __name__ == '__main__':
 27     p = Check_jira()
 28     commit_text_verified = p.verify_commit_text(os.popen('hg tip --template "{desc}"'))
 29 
 30     if (commit_text_verified):
 31         sys.exit(0)
 32     else:
 33         print >> sys.stderr, ('[obey the rules!]')
 34         sys.exit(1);
 35     def CheckForJiraIssueRecord(object):
 36    
 37         sys.stdout = os.devnull
 38         sys.stderr = os.devnull
 39 
 40    
 41         try:
 42             com = jira.Commands()
 43             logger = jira.setupLogging()
 44             jira_env = {'home':os.environ['HOME']}
 45             command_cat= "cat"
 46             command_logout= "logout"
 47             #my_args = ["QA-656"]
 48             server = "http://jira.myserver.com:8080/rpc/soap/jirasoapservice-v2?wsdl"
 49         except Exception, e:
 50             sys.exit('config error')
 51 
 52 class Options:
 53     pass
 54 options = Options()
 55 
 56 options.user = 'user'
 57 options.password = 'password'
 58 
 59 try:
 60 
 61     jira.soap = jira.Client(server)
 62     jira.start_login(options, jira_env, command_cat, com, logger)
 63     issue = com.run(command_cat, logger, jira_env, my_args)
 64 except Exception, e:
 65     print sys.exit('data error')

所以也许:1.如果name ==' main ':应该在底部? 2.因此,我有2个类(Check_jira)和(Options)。3. Check_jira有2个函数verify_commit_text()和CheckForJiraIssueRecord()4.我将对象作为参数传递给CheckForJiraIssueRecord,因为我将my_args传递给它了用法。 5.不确定如何从同一类的另一个函数调用一个函数。6.我遇到的错误是:

Traceback (most recent call last):
  File "/home/qa/hook-test/.hg/check_jira.py", line 31, in 
    commit_text_verified = p.verify_commit_text(os.popen('hg tip --template "{desc}"'))
  File "/home/qa/hook-test/.hg/check_jira.py", line 21, in verify_commit_text
    result = Check_jira.CheckForJiraIssueRecord(my_args)
AttributeError: class Check_jira has no attribute 'CheckForJiraIssueRecord'
transaction abort!
rollback completed
abort: pretxncommit.jira hook exited with status 1

class Check_jira在第25行结束,只有一个方法。 然后,您有一个if块,并且CheckForJiraIssueRecord只是在此块中定义的函数(即, if __name__ == '__main__'则定义了该函数。

只需将if块放在整个类定义之后。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM