<commit_before>"""alloccli subcommand for downloading alloc comments to mbox file."""
from __future__ import with_statement
import tempfile
import os
import subprocess
from sys import stdout
from contextlib import closing
from alloc import alloc

class mbox(alloc):
  """Download a task's emails to an mbox file."""

  # Setup the options that this cli can accept
  ops = []
  ops.append((''  , 'help           ', 'Show this help.'))
  ops.append(('q' , 'quiet          ', 'Run with no output except errors.'))
  ops.append(('t:', 'task=ID|NAME   ', 'A task ID, or a fuzzy match for a task name.'))

  # Specify some header and footer text for the help text
  help_text = "Usage: %s [OPTIONS]\n"
  help_text += __doc__
  help_text += '''\n\n%s

This program will automatically run $MAILER on the mbox file, if outputting to a TTY.

Examples:
alloc mbox -t 1234
alloc mbox -t 1234 > file.mbox'''

  def run(self, command_list):
    """Execute subcommand."""

    # Get the command line arguments into a dictionary
    o, remainder_ = self.get_args(command_list, self.ops, self.help_text)

    self.quiet = o['quiet']
    taskID = ''

    # Got this far, then authenticate
    self.authenticate()


    # Get a taskID either passed via command line, or figured out from a task name
    tops = {}
    if self.is_num(o['task']):
      taskID = o['task']
    elif o['task']:
      tops = {}
      tops["taskName"] = o["task"]
      tops["taskView"] = "prioritised"
      taskID = self.search_for_task(tops)

    if taskID:

      s = ''
      str0 = "From allocPSA Thu Jan  1 10:00:01 1970\r\n" + self.print_task(taskID)
      str1 = self.make_request({"method":"search_emails", "str":'SUBJECT "Task Comment: '+taskID+' "'})
      str2 = self.make_request({"method":"get_timeSheetItem_comments", "taskID":taskID})

      if str0:
        s += str0+"\n\n"
      if str1:
        s += str1+"\n\n"
      if str2:
        s += str2

      # If we're redirecting stdout eg alloc mbox -t 123 >task123.html
      if not stdout.isatty():
        print s

      else:
        try:
          fd, filepath = tempfile.mkstemp(prefix="alloc-%s_" % taskID, suffix=".mbox")
          with closing(os.fdopen(fd, 'wb')) as tf:
            tf.write(unicode(s).encode('utf-8'))
          subprocess.check_call ([os.getenv ("MAILER") or "mutt", "-f", filepath])
        finally:
          os.remove(filepath)

<commit_msg>20063 CLI Added services::get_task_emails() to retrieve the emails in a task mailbox.<commit_after>"""alloccli subcommand for downloading alloc comments to mbox file."""
from __future__ import with_statement
import tempfile
import os
import subprocess
from sys import stdout
from contextlib import closing
from alloc import alloc

class mbox(alloc):
  """Download a task's emails to an mbox file."""

  # Setup the options that this cli can accept
  ops = []
  ops.append((''  , 'help           ', 'Show this help.'))
  ops.append(('q' , 'quiet          ', 'Run with no output except errors.'))
  ops.append(('t:', 'task=ID|NAME   ', 'A task ID, or a fuzzy match for a task name.'))

  # Specify some header and footer text for the help text
  help_text = "Usage: %s [OPTIONS]\n"
  help_text += __doc__
  help_text += '''\n\n%s

This program will automatically run $MAILER on the mbox file, if outputting to a TTY.

Examples:
alloc mbox -t 1234
alloc mbox -t 1234 > file.mbox'''

  def run(self, command_list):
    """Execute subcommand."""

    # Get the command line arguments into a dictionary
    o, remainder_ = self.get_args(command_list, self.ops, self.help_text)

    self.quiet = o['quiet']
    taskID = ''

    # Got this far, then authenticate
    self.authenticate()


    # Get a taskID either passed via command line, or figured out from a task name
    tops = {}
    if self.is_num(o['task']):
      taskID = o['task']
    elif o['task']:
      tops = {}
      tops["taskName"] = o["task"]
      tops["taskView"] = "prioritised"
      taskID = self.search_for_task(tops)

    if taskID:

      s = ''
      str0 = "From allocPSA Thu Jan  1 10:00:01 1970\r\n" + self.print_task(taskID)
      str1 = self.make_request({"method":"get_task_emails", "taskID":taskID})
      str2 = self.make_request({"method":"get_timeSheetItem_comments", "taskID":taskID})

      if str0:
        s += str0+"\n\n"
      if str1:
        s += str1+"\n\n"
      if str2:
        s += str2

      # If we're redirecting stdout eg alloc mbox -t 123 >task123.html
      if not stdout.isatty():
        print s

      else:
        try:
          fd, filepath = tempfile.mkstemp(prefix="alloc-%s_" % taskID, suffix=".mbox")
          with closing(os.fdopen(fd, 'wb')) as tf:
            tf.write(unicode(s).encode('utf-8'))
          subprocess.check_call ([os.getenv ("MAILER") or "mutt", "-f", filepath])
        finally:
          os.remove(filepath)


{'fraction_non_alphanumeric': 0.13264462809917354, 'fraction_numerical': 0.015495867768595042, 'mean_word_length': 2.802827965435978, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 7, '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': '2174128', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1578, 'n_words': 498}