<commit_before># Copyright 2008-2009 ITA Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""RRDTool Graphing"""

import os
import stat
import tempfile

try:
    import rrdtool
except ImportError:
    rrdtool = None

import coil
import twirrdy
from nagcat import errors

def available():
    """Returns False if rrdtool is not available"""
    return rrdtool is not None

class Colorator(object):
    """A helper for picking graph colors"""

    COLORS = ('#002A8F','#DA4725','#008A6D','#00BD27','#CCBB00','#F24AC8')

    def __init__(self):
        self.color_idx = 0

    def next(self):
        self.color_idx += 1
        return Colorator.COLORS[(self.color_idx - 1) % len(Colorator.COLORS)]

def rrd_esc(string):
    """Escape : so rrd arguments parse properly"""
    return string.replace(':', r'\:')

class Graph(object):

    def __init__(self, dir, host, rrd, period="day"):
        api = twirrdy.RRDBasicAPI()
        self.rrd = rrd
        self.host = host
        path = "%s/%s/%s" % (dir, host, rrd)
        self.rrd_path = "%s.rrd" % path
        self.info = api.info(self.rrd_path)
        self.color = Colorator()
        self.period = period

        try:
            coil_fd = open("%s.coil" % path)
            coil_stat = os.fstat(coil_fd.fileno())
            self.private = not (coil_stat.st_mode & stat.S_IROTH)
            self.conf = coil.parse(coil_fd.read())
            coil_fd.close()
        except (IOError, OSError), ex:
            raise errors.InitError("Unable to read coil file: %s" % ex)

        if period not in ('day', 'week', 'month', 'year'):
            raise ValueError("Invalid period parameter")

        self.args = []
        self.ds = []
        self._init_args()
        self._init_ds()
        self._init_ds_args()

    def _init_ds(self):<commit_msg>Add a try/finally to properly close coil_fd<commit_after># Copyright 2008-2009 ITA Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""RRDTool Graphing"""

import os
import stat
import tempfile

try:
    import rrdtool
except ImportError:
    rrdtool = None

import coil
import twirrdy
from nagcat import errors

def available():
    """Returns False if rrdtool is not available"""
    return rrdtool is not None

class Colorator(object):
    """A helper for picking graph colors"""

    COLORS = ('#002A8F','#DA4725','#008A6D','#00BD27','#CCBB00','#F24AC8')

    def __init__(self):
        self.color_idx = 0

    def next(self):
        self.color_idx += 1
        return Colorator.COLORS[(self.color_idx - 1) % len(Colorator.COLORS)]

def rrd_esc(string):
    """Escape : so rrd arguments parse properly"""
    return string.replace(':', r'\:')

class Graph(object):

    def __init__(self, dir, host, rrd, period="day"):
        api = twirrdy.RRDBasicAPI()
        self.rrd = rrd
        self.host = host
        path = "%s/%s/%s" % (dir, host, rrd)
        self.rrd_path = "%s.rrd" % path
        self.info = api.info(self.rrd_path)
        self.color = Colorator()
        self.period = period

        try:
            coil_fd = open("%s.coil" % path)
            try:
              coil_stat = os.fstat(coil_fd.fileno())
              self.private = not (coil_stat.st_mode & stat.S_IROTH)
              self.conf = coil.parse(coil_fd.read())
            finally:
              coil_fd.close()
        except (IOError, OSError), ex:
            raise errors.InitError("Unable to read coil file: %s" % ex)

        if period not in ('day', 'week', 'month', 'year'):
            raise ValueError("Invalid period parameter")

        self.args = []
        self.ds = []
        self._init_args()
        self._init_ds()
        self._init_ds_args()

    def _init_ds(self):
{'fraction_non_alphanumeric': 0.11835954101147471, 'fraction_numerical': 0.01529961750956226, 'mean_word_length': 2.67734375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3588407', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1454, 'n_words': 459}