Commit ac7f358f authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

moved code as a generic text parser in the cw library, reimplement functionnality by using them

--HG--
branch : stable
parent e4f29109f451
......@@ -10,8 +10,6 @@
"""
__docformat__ = "restructuredtext en"
import re
from logilab.mtconverter import TransformError
from cubicweb import UnknownEid, typed_eid
......@@ -67,45 +65,24 @@ class ExtractEmailInformation(PreCommitOperation):
fix_ownership(self.session, com[0][0], self.email)
class ExtractEmailChangeState(PreCommitOperation):
class AnalyzeEmailText(PreCommitOperation):
"""check if there are some change state instruction in the mail content"""
instr_rgx = re.compile(':(\w+):\s*#?(\d+)', re.U)
def precommit_event(self):
# should pass some transitions ?
text = self.email.subject
for part in self.email.parts_in_order(prefered_mime_type='text/plain'):
try:
text += ' ' + part.actual_content()
except TransformError:
continue
for trname, eid in self.instr_rgx.findall(text):
eid = int(eid)
try:
entity = self.session.entity(eid)
except UnknownEid:
self.error("can't get entity with eid %s", eid)
continue
if not hasattr(entity, 'in_state'):
self.error('bad change state instruction for eid %s', eid)
continue
tr = entity.current_workflow and entity.current_workflow.transition_by_name(trname)
if tr and tr.may_be_fired(entity.eid):
try:
self.change_state(entity, tr)
except:
self.exception('while changing state off %s from email %s',
entity, self.email)
else:
self.error("can't pass transition %s on entity %s",
trname, entity)
# XXX use user session if gpg signature validated
parser = self.session.vreg['components'].select('textanalyzer', self.session)
parser.parse(self, text)
def change_state(self, entity, transition):
trinfo = entity.fire_transition(transition)
self.session.execute('SET X generated_by S WHERE X eid %(x)s, S eid %(s)s',
{'x': trinfo.eid, 's': self.email.eid}, 'x')
fix_ownership(self.session, trinfo.eid, self.email)
def fire_event(self, event, evargs):
if event == 'state-changed':
evargs['trinfo'].set_relations(generated_by=self.email)
fix_ownership(self.session, evargs['trinfo'].eid, self.email)
class AddEmailHook(Hook):
......@@ -123,4 +100,4 @@ class AddEmailHook(Hook):
if info:
ExtractEmailInformation(session, email=entity, info=info)
break
ExtractEmailChangeState(session, email=entity)
AnalyzeEmailText(session, email=entity)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment