<commit_before># These are the Mailpile commands, the public "API" we expose for searching,
# tagging and editing e-mail.
#
# Consulte the COMMANDS dict at the bottom of this file for a list of which
# commands have been defined and what their names and command-line flags are.
#
import datetime
import os
import os.path
import re
import traceback

import mailpile.util
import mailpile.ui
from mailpile.mailutils import Email, ExtractEmails, NotEditableError, NoFromAddressError, PrepareMail, SendMail
from mailpile.search import MailIndex, PostingList, GlobalPostingList
from mailpile.util import *


class Command:
  """Generic command object all others inherit from"""
  EXAMPLES = None
  FAILURE = 'Failed: %(name)s %(args)s'
  IS_HELP = False
  ORDER = (None, 0)
  SERIALIZE = False
  SPLIT_ARG = 10000  # A big number!
  SUBCOMMANDS = {}
  SYNOPSIS = None
  TEMPLATE_IDS = ['command']

  HTTP_CALLABLE = ( )
  HTTP_POST_VARS = { }
  HTTP_QUERY_VARS = { }

  class CommandResult:
    def __init__(self, session, command, template_ids, doc, result, args=[], kwargs={}):
      self.session = session
      self.command = command
      self.args = args
      self.kwargs = kwargs
      self.template_ids = template_ids
      self.doc = doc
      self.result = result

    def __nonzero__(self):
      return self.result.__nonzero__()

    def as_text(self):
      if type(self.result) == type(True):
        return '%s: %s' % (self.result and 'Succeeded' or 'Failed', self.doc)
      return unicode(self.result)
    __str__ = lambda self: self.as_text()
    __unicode__ = lambda self: self.as_text()

    def as_dict(self):
      return {
        'command': self.command,
        'command_parts': self.command.split(" "),
        'args': self.args,
        'kwargs': self.kwargs,
        'result': self.result,
        'elapsed': '%.3f' % self.session.ui.time_elapsed
      }

    def as_html(self, template=None):
      # FIXME: For optimal designer happiness, we should probably check
      #        the fs (ask the UI object) if the requested template exists,
      #        not whether it is hard-coded into the template_ids list.
      if template in self.template_ids:
        templates = ['html/%s' % template.rsplit('.', 1)[0]]
      else:
        templates = ['html/%s' % t for t in self.template_ids]
      return self.session.ui.render_html(self.session.config, templates,
                                         self.as_dict())

    def as_json(self):
      return self.session.ui.render_json(self.as_dict())

  def __init__(self, session, name=None, arg=None, data=None):
    self.session = session
    self.serialize = self.SERIALIZE
    self.template_ids = self.TEMPLATE_IDS[:]
    self.name = name
    self.data = data or {}
    self.result = None
    if type(arg) in (type(list()), type(tuple())):<commit_msg>The html/ prefix was gone<commit_after># These are the Mailpile commands, the public "API" we expose for searching,
# tagging and editing e-mail.
#
# Consulte the COMMANDS dict at the bottom of this file for a list of which
# commands have been defined and what their names and command-line flags are.
#
import datetime
import os
import os.path
import re
import traceback

import mailpile.util
import mailpile.ui
from mailpile.mailutils import Email, ExtractEmails, NotEditableError, NoFromAddressError, PrepareMail, SendMail
from mailpile.search import MailIndex, PostingList, GlobalPostingList
from mailpile.util import *


class Command:
  """Generic command object all others inherit from"""
  EXAMPLES = None
  FAILURE = 'Failed: %(name)s %(args)s'
  IS_HELP = False
  ORDER = (None, 0)
  SERIALIZE = False
  SPLIT_ARG = 10000  # A big number!
  SUBCOMMANDS = {}
  SYNOPSIS = None
  TEMPLATE_IDS = ['command']

  HTTP_CALLABLE = ( )
  HTTP_POST_VARS = { }
  HTTP_QUERY_VARS = { }

  class CommandResult:
    def __init__(self, session, command, template_ids, doc, result, args=[], kwargs={}):
      self.session = session
      self.command = command
      self.args = args
      self.kwargs = kwargs
      self.template_ids = template_ids
      self.doc = doc
      self.result = result

    def __nonzero__(self):
      return self.result.__nonzero__()

    def as_text(self):
      if type(self.result) == type(True):
        return '%s: %s' % (self.result and 'Succeeded' or 'Failed', self.doc)
      return unicode(self.result)
    __str__ = lambda self: self.as_text()
    __unicode__ = lambda self: self.as_text()

    def as_dict(self):
      return {
        'command': self.command,
        'command_parts': self.command.split(" "),
        'args': self.args,
        'kwargs': self.kwargs,
        'result': self.result,
        'elapsed': '%.3f' % self.session.ui.time_elapsed
      }

    def as_html(self, template=None):
      # FIXME: For optimal designer happiness, we should probably check
      #        the fs (ask the UI object) if the requested template exists,
      #        not whether it is hard-coded into the template_ids list.
      if template in self.template_ids:
        templates = [template.rsplit('.', 1)[0]]
      else:
        templates = self.template_ids
      return self.session.ui.render_html(self.session.config, templates,
                                         self.as_dict())

    def as_json(self):
      return self.session.ui.render_json(self.as_dict())

  def __init__(self, session, name=None, arg=None, data=None):
    self.session = session
    self.serialize = self.SERIALIZE
    self.template_ids = self.TEMPLATE_IDS[:]
    self.name = name
    self.data = data or {}
    self.result = None
    if type(arg) in (type(list()), type(tuple())):
{'fraction_non_alphanumeric': 0.10775785549440796, 'fraction_numerical': 0.003195455352387715, 'mean_word_length': 2.934357541899441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6233327', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1723, 'n_words': 517}