0

私はPythonの初心者です。特別なタグ (例: status="Needs Review") の可用性について、コミットされたすべての xml ファイルをチェック (おそらく解析) する python スクリプトを作成したいと思います。xml ファイルがこのタグで構成されている場合は、トピックへのリンクを記載した電子メールを送信してください。続行しない場合は、メールを送信せずにコミットしてください。

これをpythonスクリプトとして実現する方法のコードサンプルはありますか。xml ファイルのサンプル:

<topic template="Default" status="Needs Review" lasteditedby="user1">
  <title translate="true">Sample Title</title>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Statistische Messungen</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Font Style" style="font-family:&apos;Optima LT&apos;; font-size:10pt; font-weight:normal; font-style:normal; text-decoration:none; text-transform:none; vertical-align:baseline; color:#000000; background-color:transparent; letter-spacing:normal; letter-scaling:100%;" translate="true">This is a sample Text</text></para>
  </body>
</topic>

これを行うにはさまざまな方法があると思います。ポストコミット用の python コーディングがいくつかあることは知っていますが、この問題のコーディングは見つかりません。

4

1 に答える 1

0

Andres からのこのコード スニペットは良い基礎です。「svn look changed」(xml ファイルの場合) や各 xml ファイルの「svn look cat.

from lxml import html
from mailer import Mailer
from mailer import Message
import sys, os

xml = """<topic template="Default" status="Needs Review" lasteditedby="user1">
  <title translate="true">Sample Title</title>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Statistische Messungen</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Font Style" style="font-family:&apos;Optima LT&apos;; font-size:10pt; font-weight:normal; font-style:normal; text-decoration:none; text-transform:none; vertical-align:baseline; color:#000000; background-color:transparent; letter-spacing:normal; letter-scaling:100%;" translate="true">This is a sample Text</text></para>
  </body>
</topic>"""


tree = html.fromstring(xml)
if tree.xpath('//topic/@status')[0] == "Needs Review":
    print "This topic needs review"
    print "Sending e-mail"  
    # Call email function here...
else:
    print "This topic doesn't need review"
    print "Continuing commit without sending e-mail"
    # Continue with commit
于 2015-11-12T08:57:44.707 に答える