Show
Ignore:
Timestamp:
01/11/08 19:08:24 (16 years ago)
Author:
jerome
Message:

Moved all exceptions definitions to a dedicated module.

Files:
1 copied

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/errors.py

    r3275 r3288  
    2020# 
    2121 
    22 """This module implements some CUPS specific classes.""" 
     22"""This module defines ALL PyKota specific exceptions.""" 
    2323 
    24 from pykota.tool import PyKotaToolError 
    25 try : 
    26     from pkipplib import pkipplib 
    27 except ImportError :         
    28     raise RuntimeError, "The python-pkipplib module is now mandatory. You can download pkipplib from http://www.pykota.com/" 
    29  
    30 class JobTicket :     
    31     """A class to hold CUPS print job informations.""" 
    32     def __init__(self, jobid=None, printername=None, copies=1, filename=None, \ 
    33                        options=None) : 
    34         """Initializes a print job's information.""" 
    35         self.JobId = jobid 
    36         self.PrinterName = printername 
    37         self.Copies = copies 
    38         self.FileName = filename 
    39         self.Options = options 
    40         self.Charset = None 
    41         self.OriginatingUserName = None 
    42         self.OriginalUserName = None 
    43         self.Title = None 
    44         self.BillingCode = None 
    45         self.OriginatingHostName = None 
    46         self.TimeAtCreation = None 
    47         self.TimeAtProcessing = None 
    48         self.MimeType = None 
    49         self.UUID = None 
    50         if self.JobId is not None : 
    51             self.retrieveAttributesFromCUPS() 
     24class PyKotaError(Exception) : 
     25    """Mother class of all PyKota exceptions.""" 
     26    def __init__(self, value) : 
     27        """Saves the value passed as a parameter.""" 
     28        self.value = value 
    5229         
    53     def getAttributeTypeAndValue(self, ippanswer, attribute, category="job") :     
    54         """Retrieves a particular attribute's type and value from an IPP answer. 
     30    def __str__(self) :     
     31        """Returns an unicode string representation.""" 
     32        try : 
     33            return unicode(self.value, errors="replace") 
     34        except TypeError :     
     35            return unicode(self.value) # None and strings which are already unicode 
    5536         
    56            Returns a tuple of the form (type, value). 
    57         """ 
    58         try : 
    59             return getattr(ippanswer, category)[attribute][0] 
    60         except KeyError :     
    61             return (None, None) 
    62              
    63     def retrieveAttributesFromCUPS(self) : 
    64         """Retrieve attribute's values from CUPS.""" 
    65         server = pkipplib.CUPS() # TODO : username and password and/or encryption 
    66         answer = server.getJobAttributes(self.JobId) 
    67         if answer is None : 
    68             raise PyKotaToolError, "Network error while querying the CUPS server : %s" \ 
    69                                       % server.lastErrorMessage 
    70         (dummy, self.Charset) = self.getAttributeTypeAndValue(answer, "attributes-charset", "operation") 
    71         (dummy, self.OriginatingUserName) = self.getAttributeTypeAndValue(answer, "job-originating-user-name") 
    72         (dummy, self.Title) = self.getAttributeTypeAndValue(answer, "job-name") 
    73         (dummy, self.BillingCode) = self.getAttributeTypeAndValue(answer, "job-billing") 
    74         (dummy, self.OriginatingHostName) = self.getAttributeTypeAndValue(answer, "job-originating-host-name") 
    75         (dummy, self.UUID) = self.getAttributeTypeAndValue(answer, "job-uuid") 
    76         (dummy, self.TimeAtCreation) = self.getAttributeTypeAndValue(answer, "time-at-creation") 
    77         (dummy, self.TimeAtProcessing) = self.getAttributeTypeAndValue(answer, "time-at-processing") 
    78         (dummy, self.MimeType) = self.getAttributeTypeAndValue(answer, "document-format") 
    79          
    80         for attrib in ("OriginatingUserName",  
    81                        "OriginatingHostName", 
    82                        "Title",  
    83                        "BillingCode", 
    84                        "PrinterName", 
    85                        "Options", 
    86                        "Charset", 
    87                        "UUID", 
    88                        "MimeType") : 
    89             try :            
    90                 setattr(self, attrib,  
    91                               getattr(self, attrib).decode("UTF-8", "replace")) 
    92             except AttributeError :                   
    93                 pass 
    94                  
    95         self.OriginalUserName = self.OriginatingUserName[:] 
     37class PyKotaToolError(PyKotaError) : 
     38    """PyKota Exception for all executables.""" 
     39    pass 
    9640     
    97 if __name__ == "__main__" :     
    98     import sys 
    99     if len(sys.argv) != 2 : 
    100         sys.stderr.write("usage : python cups.py jobid\n") 
    101     else :     
    102         job = JobTicket(int(sys.argv[1]), "FakePrinter") 
    103         for attribute in ("PrinterName", "Charset", "JobId", "Copies",  
    104                           "FileName", "OriginatingUserName",  
    105                           "Title", "BillingCode", "OriginatingHostName",  
    106                           "TimeAtCreation", "TimeAtProcessing", "UUID",  
    107                           "MimeType") : 
    108             sys.stdout.write("%s : %s\n" % (attribute, repr(getattr(job, attribute)))) 
     41class PyKotaToolError(PyKotaError) : 
     42    """PyKota Exception for all executables.""" 
     43    pass 
     44     
     45class PyKotaCommandLineError(PyKotaToolError) : 
     46    """PyKota Exception for errors on executables' command line.""" 
     47    pass 
     48     
     49class PyKotaStorageError(PyKotaError) : 
     50    """PyKota Exception for database backend related errors.""" 
     51    pass 
     52     
     53class PyKotaConfigError(PyKotaError) : 
     54    """PyKota Exception for errors in PyKota's configuration.""" 
     55    pass 
     56     
     57class PyKotaAccounterError(PyKotaError) : 
     58    """PyKota Exception for errors in PyKota accounters.""" 
     59    pass 
     60     
     61class PyKotaReporterError(PyKotaError) : 
     62    """PyKota Exception for errors in PyKota report generators.""" 
     63    pass 
     64     
     65class PyKotaLoggingError(PyKotaError) : 
     66    """PyKota Exception for errors in PyKota logging.""" 
     67    pass