<commit_before>#! /usr/bin/env python

import datetime
import os
import re
import sys
import time

import requests

import config
import slackbot


class Destalinator(object):

    closure_text_fname = "closure.txt"
    warning_text_fname = "warning.txt"

    def __init__(self, slacker, slackbot):
        """
        slacker is a Slacker() object
        slackbot should be an initialized slackbot.Slackbot() object
        """
        self.closure_text = self.get_content(self.closure_text_fname)
        self.warning_text = self.get_content(self.warning_text_fname)
        self.slacker = slacker
        self.slackbot = slackbot
        self.user = os.getenv("USER")
        self.config = config.Config()
        self.output_debug_to_slack_flag = False
        if os.getenv(self.config.output_debug_env_varname):
            self.output_debug_to_slack_flag = True
        print "output_debug_to_slack_flag is {}".format(self.output_debug_to_slack_flag)
        self.earliest_archive_date = self.config.earliest_archive_date
        self.cache = {}
        self.now = time.time()

    def get_content(self, fname):
        """
        read fname into text blob, return text blob
        """
        f = open(fname)
        ret = f.read().strip()
        f.close()
        return ret

    def safe_archive(self, channel_name):
        """
        Arhives channel if today's date is after self.earliest_archive_date
        and if channel does not only contain SCGs (Single-Channel Guests)
        """

        if self.slacker.channel_has_only_restricted_members(channel_name):
            message = "Would have archived {} but it contains restricted users"
            self.debug(message)
            return

        today = datetime.date.today()
        year, month, day = [int(x) for x in self.earliest_archive_date.split("-")]
        earliest = datetime.date(year, month, day)
        if today >= earliest:
            self.action("Archiving channel {}".format(channel_name))
            self.archive(channel_name)
        else:
            message = "Would have archived {} but it's not yet {}"
            message = message.format(channel_name, self.earliest_archive_date)
            self.debug(message)

    def archive(self, channel_name):
        """
        Archives the given channel name.  Returns the response content
        """
        if self.ignore_channel(channel_name):
            self.debug("Not warning {} because it's in ignore_channels".format(channel_name))
            return
        self.slackbot.say(channel_name, self.closure_text)
        members = self.slacker.get_channel_member_names(channel_name)
        say = "Members at archiving are {}".format(", ".join(members))
        self.slackbot.say(channel_name, say)
        payload = self.slacker.archive(channel_name)

        if payload['ok']:
            self.debug("Archived {}".format(channel_name))
        else:
            error = payload.get('error', '!! No error found in payload %s !!' % payload)
            self.debug("Failed to archive {channel_name}: {error}. See https://api.slack.com/methods/channels.archive for more context.".format(channel_name=channel_name, error=error))

        return payload

    def channel_minimum_age(self, channel_name, days):
        """
        returns True/False depending on whether channel_name is at least DAYS old
        """
        info = self.slacker.get_channel_info(channel_name)
        age = info['age']
        age = age / 86400
        return age > days

    def stale(self, channel_name, days):
        """<commit_msg>add debug message<commit_after>#! /usr/bin/env python

import datetime
import os
import re
import sys
import time

import requests

import config
import slackbot


class Destalinator(object):

    closure_text_fname = "closure.txt"
    warning_text_fname = "warning.txt"

    def __init__(self, slacker, slackbot):
        """
        slacker is a Slacker() object
        slackbot should be an initialized slackbot.Slackbot() object
        """
        self.closure_text = self.get_content(self.closure_text_fname)
        self.warning_text = self.get_content(self.warning_text_fname)
        self.slacker = slacker
        self.slackbot = slackbot
        self.user = os.getenv("USER")
        self.config = config.Config()
        self.output_debug_to_slack_flag = False
        if os.getenv(self.config.output_debug_env_varname):
            self.output_debug_to_slack_flag = True
        print "output_debug_to_slack_flag is {}".format(self.output_debug_to_slack_flag)
        self.earliest_archive_date = self.config.earliest_archive_date
        self.cache = {}
        self.now = time.time()

    def get_content(self, fname):
        """
        read fname into text blob, return text blob
        """
        f = open(fname)
        ret = f.read().strip()
        f.close()
        return ret

    def safe_archive(self, channel_name):
        """
        Arhives channel if today's date is after self.earliest_archive_date
        and if channel does not only contain SCGs (Single-Channel Guests)
        """

        if self.slacker.channel_has_only_restricted_members(channel_name):
            message = "Would have archived {} but it contains restricted users"
            self.debug(message)
            return

        today = datetime.date.today()
        year, month, day = [int(x) for x in self.earliest_archive_date.split("-")]
        earliest = datetime.date(year, month, day)
        if today >= earliest:
            self.action("Archiving channel {}".format(channel_name))
            self.archive(channel_name)
        else:
            message = "Would have archived {} but it's not yet {}"
            message = message.format(channel_name, self.earliest_archive_date)
            self.debug(message)

    def archive(self, channel_name):
        """
        Archives the given channel name.  Returns the response content
        """
        if self.ignore_channel(channel_name):
            self.debug("Not warning {} because it's in ignore_channels".format(channel_name))
            return
        self.slackbot.say(channel_name, self.closure_text)
        members = self.slacker.get_channel_member_names(channel_name)
        say = "Members at archiving are {}".format(", ".join(members))
        self.slackbot.say(channel_name, say)
        payload = self.slacker.archive(channel_name)

        if payload['ok']:
            self.debug("Payload: {}".format(json.dumps(payload, indent=4)))
            self.debug("Archived {}".format(channel_name))
        else:
            error = payload.get('error', '!! No error found in payload %s !!' % payload)
            self.debug("Failed to archive {channel_name}: {error}. See https://api.slack.com/methods/channels.archive for more context.".format(channel_name=channel_name, error=error))

        return payload

    def channel_minimum_age(self, channel_name, days):
        """
        returns True/False depending on whether channel_name is at least DAYS old
        """
        info = self.slacker.get_channel_info(channel_name)
        age = info['age']
        age = age / 86400
        return age > days

    def stale(self, channel_name, days):
        """
{'fraction_non_alphanumeric': 0.100942872989462, 'fraction_numerical': 0.0015252357182473654, 'mean_word_length': 2.499757399320718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6090861', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2047, 'n_words': 497}