<commit_before>#! /usr/bin/env python
# encoding: utf-8

"""socorro.lib.monitor watches a dump directory managed by a collector,
prunes directories, and detects orphaned dumps and json files. Linux only.
Requires gamin and accompanying python bindings."""

import gamin
import os
import select, errno
from datetime import datetime
from socorro.lib.processor import Processor, firefoxHook
import socorro.lib.config as config
import socorro.models as model
from  sqlalchemy.exceptions import SQLError
import sqlalchemy

#
# Adapted from the WAF project (BSD License)
# http://code.google.com/p/waf/
#
class GaminHelper:
  def __init__(self, eventHandler):
    """@param eventHandler: callback method for event handling"""
    self.__gamin = gamin.WatchMonitor()
    self.__eventHandler = eventHandler # callBack function
    self.__watchHandler = {} # {name : famId}

  def __del__(self):
    """clean remove"""
    if self.__gamin:
      for handle in self.__watchHandler.keys():
        self.stop_watch(handle)
      self.__gamin.disconnect()
      self.__gamin = None

  def __check_gamin(self):
    """is gamin connected"""
    if self.__gamin == None:
      raise "gamin not init"

  def __code2str(self, event):
    """convert event numbers to string"""
    gaminCodes = {
      1:"changed",
      2:"deleted",
      3:"StartExecuting",
      4:"StopExecuting",
      5:"created",
      6:"moved",
      7:"acknowledge",
      8:"exists",
      9:"endExist"
    }
    try:
      return gaminCodes[event]
    except:
      return "unknown"
    
  def __eventhandler_helper(self, pathName, event, userData):
    """local eventhandler helps to convert event numbers to string"""
    self.__eventHandler(pathName, self.__code2str(event), userData)

  def watching(self, name):
    return self.__watchHandler.has_key(name)

  def watch_directory(self, name, userData):
    self.__check_gamin()
    if self.__watchHandler.has_key(name):
      raise "dir allready watched"
    # set gaminId
    self.__watchHandler[name] = self.__gamin.watch_directory(name, self.__eventhandler_helper, userData)
    return(self.__watchHandler[name])

  def watch_file(self, name, userData):
    self.__check_gamin()
    if self.__watchHandler.has_key( name ):
      raise "file allready watched"
    # set famId
    self.__watchHandler[name] = self.__gamin.watch_directory( name, self.__eventhandler_helper, userData)
    return(self.__watchHandler[name])

  def stop_watch(self, name):
    self.__check_gamin()
    if self.__watchHandler.has_key(name):
      self.__gamin.stop_watch(name)<commit_msg>Make except more specific to avoid catching bad stuff.<commit_after>#! /usr/bin/env python
# encoding: utf-8

"""socorro.lib.monitor watches a dump directory managed by a collector,
prunes directories, and detects orphaned dumps and json files. Linux only.
Requires gamin and accompanying python bindings."""

import gamin
import os
import select, errno
from datetime import datetime
from socorro.lib.processor import Processor, firefoxHook
import socorro.lib.config as config
import socorro.models as model
from  sqlalchemy.exceptions import SQLError
import sqlalchemy

#
# Adapted from the WAF project (BSD License)
# http://code.google.com/p/waf/
#
class GaminHelper:
  def __init__(self, eventHandler):
    """@param eventHandler: callback method for event handling"""
    self.__gamin = gamin.WatchMonitor()
    self.__eventHandler = eventHandler # callBack function
    self.__watchHandler = {} # {name : famId}

  def __del__(self):
    """clean remove"""
    if self.__gamin:
      for handle in self.__watchHandler.keys():
        self.stop_watch(handle)
      self.__gamin.disconnect()
      self.__gamin = None

  def __check_gamin(self):
    """is gamin connected"""
    if self.__gamin == None:
      raise "gamin not init"

  def __code2str(self, event):
    """convert event numbers to string"""
    gaminCodes = {
      1:"changed",
      2:"deleted",
      3:"StartExecuting",
      4:"StopExecuting",
      5:"created",
      6:"moved",
      7:"acknowledge",
      8:"exists",
      9:"endExist"
    }
    try:
      return gaminCodes[event]
    except IndexError:
      return "unknown"
    
  def __eventhandler_helper(self, pathName, event, userData):
    """local eventhandler helps to convert event numbers to string"""
    self.__eventHandler(pathName, self.__code2str(event), userData)

  def watching(self, name):
    return self.__watchHandler.has_key(name)

  def watch_directory(self, name, userData):
    self.__check_gamin()
    if self.__watchHandler.has_key(name):
      raise "dir allready watched"
    # set gaminId
    self.__watchHandler[name] = self.__gamin.watch_directory(name, self.__eventhandler_helper, userData)
    return(self.__watchHandler[name])

  def watch_file(self, name, userData):
    self.__check_gamin()
    if self.__watchHandler.has_key( name ):
      raise "file allready watched"
    # set famId
    self.__watchHandler[name] = self.__gamin.watch_directory( name, self.__eventhandler_helper, userData)
    return(self.__watchHandler[name])

  def stop_watch(self, name):
    self.__check_gamin()
    if self.__watchHandler.has_key(name):
      self.__gamin.stop_watch(name)
{'fraction_non_alphanumeric': 0.10640337423312883, 'fraction_numerical': 0.004601226993865031, 'mean_word_length': 3.704238052299369, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4202474', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1565, 'n_words': 422}