<commit_before>import hashlib
import base64
import re

import logging
logger = logging.getLogger(__name__)

class Filtre(object):

  def __init__(self,msg=None,tickets=None,identitat=None):
    self.msg=msg
    self.tickets=tickets
    self.identitat=identitat

  def set_mail(self,msg):
    self.msg=msg

  def set_tickets(self,tickets):
    self.tickets=tickets

  def set_identitat(self,identitat):
    self.identitat=identitat

  def es_aplicable(self):
    return False

  def filtrar(self):
    return
  
  def get_uid(self):
    uid=self.identitat.obtenir_uid(self.msg.get_from())
    if uid != None:
      return uid
    if self.msg.get_reply_to() != None:
      return self.identitat.obtenir_uid(self.msg.get_reply_to())	  
    return None

  def codificar_base_64_si_cal(self,attachment):
    if attachment['Content-Transfer-Encoding']=='base64':
      return attachment.get_payload()      
    else:
      return base64.b64encode(attachment.get_payload())

  def afegir_attachments_canviant_body(self,ticket_id,username,body):
    cids=self.afegir_attachments(ticket_id,username)
    return self.tractar_attachments_inline(body,cids)

  def afegir_attachments(self,ticket_id,username):
    logger.info("Tractem attachments del ticket %s" % ticket_id)
    i=0;
    cids={}
    for a in self.msg.get_attachments():
      ctype=a.get_content_type()
      fitxer=a.get_filename()
      cid=a.get('Content-ID')
      i+=1
      if fitxer==None:
        fitxer='attach%d.%s' % (i,ctype.split("/")[1])
      logger.info("Afegim attachment: %s" % fitxer)
      codi_annex=self.tickets.annexar_fitxer_tiquet(ticket_id,username,fitxer, self.codificar_base_64_si_cal(a))
      if cid!=None: cids[cid[1:-1]]=codi_annex
    return cids

  def tractar_attachments_inline(self,html,cids):
    for cid in cids:
      id=cids[cid]
      html=html.replace("cid:"+cid,"https://gn6.upc.edu/tiquets/control/file?fileId=%d"%id)      
    return html<commit_msg>Canvis per donar suport als attachments inline<commit_after>import hashlib
import base64
import re

import logging
logger = logging.getLogger(__name__)

class Filtre(object):

  def __init__(self,msg=None,tickets=None,identitat=None):
    self.msg=msg
    self.tickets=tickets
    self.identitat=identitat

  def set_mail(self,msg):
    self.msg=msg

  def set_tickets(self,tickets):
    self.tickets=tickets

  def set_identitat(self,identitat):
    self.identitat=identitat

  def es_aplicable(self):
    return False

  def filtrar(self):
    return
  
  def get_uid(self):
    uid=self.identitat.obtenir_uid(self.msg.get_from())
    if uid != None:
      return uid
    if self.msg.get_reply_to() != None:
      return self.identitat.obtenir_uid(self.msg.get_reply_to())	  
    return None

  def codificar_base_64_si_cal(self,attachment):
    if attachment['Content-Transfer-Encoding']=='base64':
      return attachment.get_payload()      
    else:
      return base64.b64encode(attachment.get_payload())

  def afegir_attachments_canviant_body(self,ticket_id,username,body):
    cids=self.afegir_attachments(ticket_id,username)
    return self.tractar_attachments_inline(body,cids)

  def afegir_attachments(self,ticket_id,username):
    logger.info("Tractem attachments del ticket %s" % ticket_id)
    i=0;
    cids={}
    for a in self.msg.get_attachments():
      ctype=a.get_content_type()
      fitxer=a.get_filename()
      cid=a.get('Content-ID')
      i+=1
      if fitxer==None:
        fitxer='attach%d.%s' % (i,ctype.split("/")[1])
      logger.info("Afegim attachment: %s / %s" % (fitxer,cid))
      codi_annex=self.tickets.annexar_fitxer_tiquet(ticket_id,username,fitxer, self.codificar_base_64_si_cal(a))
      if cid!=None: cids[cid[1:-1]]=codi_annex
    return cids

  def tractar_attachments_inline(self,html,cids):
    for cid in cids:
      id=cids[cid]
      html=html.replace("cid:"+cid,"https://gn6.upc.edu/tiquets/control/file?fileId=%d" % id)      
    return html
{'fraction_non_alphanumeric': 0.12290927521540801, 'fraction_numerical': 0.00912316269640142, 'mean_word_length': 3.9461152882205512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7174492', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1456, 'n_words': 217}