def log(self, level, msg, show=True): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LOG ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ # Does level allow logging? if LEVELS.index(level) >= self.level: # Get current time now = datetime.datetime.now() # Format message msg = self.fmt.format(now, self.name, level, msg) # Define log directory and touch it directory = path.Path(path.REPORTS.path + lib.formatDate(now)) directory.touch() # Log message with open(directory.path + self.report, "a") as f: f.write(msg + "\n") # Print to terminal if show: print msg
def __repr__(self): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ REPR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ String representation of report. """ return "'" + self.name + "' (" + lib.formatDate(self.date) + ")"
def test_create_dated_report(): """ Create a dated report. """ today = datetime.date.today() report = DatedReport(today) reportPath = path.Path(path.TESTS.path + lib.formatDate(today)) assert (report.name == "test.json" and report.date == today and report.json == {} and report.directory.path == reportPath.path)
def __str__(self): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ STR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ String representation of report's content. """ return lib.JSONize({ "Name": self.name, "Date": lib.formatDate(self.date), "Directory": self.directory.path, "JSON": self.json })
def getMonthlyErrors(today, nMonths): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GETMONTHLYERRORS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Get all reported errors over the last "n" months and merge them into a single JSON object, in which every error report's content is stored under its corresponding date as a key. """ # Type check date if type(today) is not datetime.date: raise TypeError("Incorrect date type.") # Type check number of months if not lib.isRealNumber(nMonths): raise TypeError("Incorrect type for number of months.") # Check number of months if nMonths <= 0: raise ValueError("Incorrect value for number of months.") # Define first month day start = today.replace(day = 1) - relativedelta(months = nMonths - 1) # Get all dates of error reports, and keep the ones that are within current # month dates = getReportDates(ErrorsReport) filteredDates = [d for d in dates if start <= d] # Initialize dict for merged errors json = {} # Loop on found dates for date in sorted(filteredDates): # Initialize and load report report = getReportByType(ErrorsReport, date) # Add error entries json[lib.formatDate(date)] = report.get() # Return merged errors return json
def __init__(self, name, date, directory = path.REPORTS, json = None): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ INIT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ # Initialize report super(DatedReport, self).__init__(name, directory, json) # Test date if type(date) is not datetime.date: raise TypeError("Invalid date.") # Define date self.date = date # Expand path self.directory.expand(lib.formatDate(date))