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

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],
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 =
for part in'text/plain'):
text += ' ' + part.actual_content()
except TransformError:
for trname, eid in self.instr_rgx.findall(text):
eid = int(eid)
entity = self.session.entity(eid)
except UnknownEid:
self.error("can't get entity with eid %s", eid)
if not hasattr(entity, 'in_state'):
self.error('bad change state instruction for eid %s', eid)
tr = entity.current_workflow and entity.current_workflow.transition_by_name(trname)
if tr and tr.may_be_fired(entity.eid):
self.change_state(entity, tr)
self.exception('while changing state off %s from email %s',
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':}, 'x')
fix_ownership(self.session, trinfo.eid,
def fire_event(self, event, evargs):
if event == 'state-changed':
fix_ownership(self.session, evargs['trinfo'].eid,
class AddEmailHook(Hook):
......@@ -123,4 +100,4 @@ class AddEmailHook(Hook):
if info:
ExtractEmailInformation(session, email=entity, info=info)
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