Changeset ee8e6d0


Ignore:
Timestamp:
Dec 27, 2011, 7:03:46 PM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
7c143c9
Parents:
1037115
git-author:
Jessica B. Hamrick <jhamrick@…> (12/27/11 19:03:46)
git-committer:
Jessica B. Hamrick <jhamrick@…> (12/27/11 19:03:46)
Message:

Fix error with HTTP server recreating printer objects

Location:
server/lib/gutenbach
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • server/lib/gutenbach/ipp/attributes/operation.py

    r793432f ree8e6d0  
    1818        'requesting-user-name',
    1919        [Value(CharacterStringTags.NAME_WITHOUT_LANGUAGE, val)])
     20
     21def IppAttributeFidelity(val):
     22    raise errors.ClientErrorAttributes, 'ipp-attribute-fidelity'
  • server/lib/gutenbach/ipp/core/constants.py

    r793432f ree8e6d0  
    3131   
    3232    """
    33    
     33
    3434    PENDING    = 3
    3535    HELD       = 4
  • server/lib/gutenbach/server/__init__.py

    r6effd50 ree8e6d0  
    33import logging
    44import sys
     5import traceback
    56
    67# configure logging
    78logging.basicConfig(level=logging.DEBUG)
    89
     10# initialize logger
     11logger = logging.getLogger(__name__)
     12
    913def error(self, request, client_address):
     14    logger.fatal(traceback.format_exc())
    1015    sys.exit(1)
    1116
  • server/lib/gutenbach/server/job.py

    r793432f ree8e6d0  
    22import os
    33import gutenbach.ipp as ipp
     4import logging
    45
    56# initialize logger
     
    1819    ]
    1920
    20     def __init__(self, document=None):
     21    def __init__(self, jid, printer, creator="", name="", size=0):
    2122        """Initialize a Gutenbach job.
    2223
     
    2425        document to print to the value of document.
    2526        """
    26          
    27         self.jid = None
    28         self.name = document
    29         self.status = None
    30         self.document = document
    31         self.printer = None
     27
     28        self.jid      = jid
     29        self.printer  = printer
     30
     31        self.creator  = creator
     32        self.name     = name
     33        self.size     = size
     34
     35        self.status   = ipp.JobStates.PENDING
    3236
    3337    def __getattr__(self, attr):
    3438        try:
    35             return super(Job, self).__getattr__(attr)
     39            return self.__getattribute__(attr)
    3640        except AttributeError:
    3741            pass
    38 
    39         return super(Job, self).__getattr__(
    40             attr.replace("-", "_"))
     42        return self.__getattribute__(attr.replace("-", "_"))
    4143
    4244    def __hasattr__(self, attr):
    43         has = super(Job, self).__hasattr__(attr)
    44         if not has:
    45             has = super(Job, self).__hasattr__(
    46                 attr.replace("-", "_"))
    47         return has
     45        try:
     46            getattr(self, attr)
     47            return True
     48        except AttributeError:
     49            return False
    4850
    4951    #### Job attributes
     
    6062    @property
    6163    def job_originating_user_name(self):
    62         return ipp.JobOriginatingUserName("jhamrick")
     64        return ipp.JobOriginatingUserName(self.creator)
    6365
    6466    # XXX: we need to actually calculate this!
    6567    @property
    6668    def job_k_octets(self):
    67         return ipp.JobKOctets(1)
     69        return ipp.JobKOctets(self.size)
    6870
    6971    @property
     
    7577        return ipp.JobPrinterUri(self.printer.uri)
    7678
    77     def get_job_attributes(self, request):
    78         attributes = [getattr(self, attr) for attr in self.attributes]
     79    def get_job_attributes(self, request=None):
     80        if request and 'requested-attributes' in request:
     81            requested = []
     82            for value in request['requested-attributes'].values:
     83                if value.value in self.attributes:
     84                    requested.append(value.value)
     85        else:
     86            requested = self.attributes
     87           
     88        attributes = [getattr(self, attr) for attr in requested]
    7989        return attributes
    8090   
    8191    #######
    82 
    83     def enqueue(self, printer, job_id):
    84         if self.status != None:
    85             raise InvalidJobException(
    86                 "Cannot enqueue a job that has " + \
    87                 "already been initialized!")
    88         self.printer = printer
    89         self.jid = job_id
    90         self.status = const.JobStates.PENDING
    9192
    9293    def play(self):
     
    107108
    108109    def __str__(self):
    109         return "<Job %d '%s'>" % \
    110                (self.jid if self.jid is not None else -1, \
    111                 self.document)
     110        return "<Job %d '%s'>" % (self.jid if self.jid is not None else -1, self.name)
  • server/lib/gutenbach/server/printer.py

    r793432f ree8e6d0  
    11#import alsaaudio as aa
    22from .exceptions import InvalidJobException, InvalidPrinterStateException
     3from .job import Job
    34import gutenbach.ipp as ipp
    45import logging
     
    169170        return ipp.MultipleDocumentJobsSupported(False)
    170171
    171     def get_printer_attributes(self, request):
    172         attributes = [getattr(self, attr) for attr in self.attributes]
     172    ## Printer operations
     173
     174    def get_printer_attributes(self, request=None):
     175        if request and 'requested-attributes' in request:
     176            requested = []
     177            for value in request['requested-attributes'].values:
     178                if value.value in self.attributes:
     179                    requested.append(value.value)
     180        else:
     181            requested = self.attributes
     182           
     183        attributes = [getattr(self, attr) for attr in requested]
    173184        return attributes
    174185
    175     ## Printer operations
     186    def create_job(self, request):
     187        operation = request.attribute_groups[0]
     188        kwargs = {}
     189       
     190        # requesting username
     191        if 'requesting-user-name' in operation:
     192            username_attr = operation['requesting-user-name']
     193            username = username_attr.values[0].value
     194            if username_attr != ipp.RequestingUserName(username):
     195                raise ipp.errors.ClientErrorBadRequest(str(username_attr))
     196            kwargs['creator'] = username
     197
     198        # job name
     199        if 'job-name' in operation:
     200            job_name_attr = operation['job-name']
     201            job_name = job_name_attr.values[0].value
     202            if job_name_attr != ipp.JobName(job_name):
     203                raise ipp.errors.ClientErrorBadRequest(str(job_name_attr))
     204            kwargs['name'] = job_name
     205
     206        # job size
     207        if 'job-k-octets' in operation:
     208            job_k_octets_attr = operation['job-k-octets']
     209            job_k_octets = job_k_octets_attr.values[0].value
     210            if job_k_octets_attr != ipp.JobKOctets(job_k_octets):
     211                raise ipp.errors.ClientErrorBadRequest(str(job_k_octets_attr))
     212            kwargs['size'] = job_k_octets
     213
     214        job_id = self._next_jobid
     215        self._next_jobid += 1
     216       
     217        job = Job(job_id, self, **kwargs)
     218        self.jobs[job_id] = job
     219        self.active_jobs.append(job_id)
     220        print self.active_jobs
     221        return job
    176222
    177223    def print_job(self, job):
    178         jobid = self._next_jobid
    179         self._next_jobid += 1
    180         self.active_jobs.append(jobid)
    181         self.jobs[jobid] = job
    182         job.enqueue(self, jobid)
    183         return jobid
     224        pass
    184225
    185226    def complete_job(self, jobid):
     
    214255
    215256    def get_jobs(self):
     257        print self.active_jobs
    216258        jobs = [self.jobs[job_id] for job_id in self.active_jobs]
    217         return jobs           
    218 
    219     def __repr__(self):
    220         return str(self)
    221 
    222     def __str__(self):
    223         return "<Printer '%s'>" % self.name
     259        return jobs
     260
     261    # def __repr__(self):
     262    #     return str(self)
     263
     264    # def __str__(self):
     265    #     return "<Printer '%s'>" % self.name
  • server/lib/gutenbach/server/requests.py

    r1037115 ree8e6d0  
    4141
    4242    def __init__(self):
     43        print "init"
    4344        self.printers = {
    4445            "test": GutenbachPrinter(name="test")
     
    227228        jobs = self.printers[printer_name].get_jobs()
    228229
    229         # requesting username
    230         if 'requesting-user-name' in operation:
    231             username_attr = operation['requesting-user-name']
    232             username = username_attr.values[0].value
    233             if username_attr != ipp.RequestingUserName(username):
    234                 raise ipp.errors.ClientErrorBadRequest(str(username_attr))
    235 
    236230        # get the job attributes and add them to the response
    237231        for job in self.printers[printer_name].get_jobs():
    238             attrs = job.get_job_attributes(request)
     232            attrs = job.get_job_attributes(operation)
    239233            response.attribute_groups.append(ipp.AttributeGroup(
    240234                ipp.AttributeTags.JOB, attrs))
     
    260254        in the multi-document Job object.
    261255
    262         Group 1: Operation Attributes
     256        Request
     257        -------
     258        Group 1: Operation Attributes
     259            REQUIRED 'attributes-charset'
     260            REQUIRED 'attributes-natural-language'
     261            REQUIRED 'printer-uri' (uri)
     262            OPTIONAL 'requesting-user-name' (name(MAX))
     263            OPTIONAL 'job-name' (name(MAX))
     264            OPTIONAL 'ipp-attribute-fidelity' (boolean)
     265            OPTIONAL 'job-k-octets' (integer(0:MAX))
     266            OPTIONAL 'job-impressions' (integer(0:MAX))
     267            OPTIONAL 'job-media-sheets' (integer(0:MAX))
     268        Group 2: Job Template Attributes
     269
     270        Response
     271        --------
     272        Group 1: Operation Attributes
     273            OPTIONAL 'status-message' (text(255))
     274            OPTIONAL 'detailed-status-message' (text(MAX))
     275            REQUIRED 'attributes-charset'
     276            REQUIRED 'attributes-natural-language'
     277        Group 2: Unsupported Attributes
     278        Group 3: Job Object Attributes
     279            REQUIRED 'job-uri' (uri)
     280            REQUIRED 'job-id' (integer(1:MAX))
     281            REQUIRED 'job-state' (type1 enum)
     282            REQUIRED 'job-state-reasons' (1setOf type2 keyword)
     283            OPTIONAL 'job-state-message' (text(MAX))
     284            OPTIONAL 'number-of-intervening-jobs' (integer(0:MAX))
    263285       
    264286        """
    265287
    266         raise ipp.errors.ServerErrorOperationNotSupported
     288
     289        operation = request.attribute_groups[0]
     290
     291        # requested printer uri
     292        if 'printer-uri' not in operation:
     293            raise ipp.errors.ClientErrorBadRequest("Missing 'printer-uri' attribute")
     294        uri_attr = operation['printer-uri']
     295        printer_name = uri_attr.values[0].value.split("/")[-1]
     296        if uri_attr != ipp.PrinterUri(uri_attr.values[0].value):
     297            raise ipp.errors.ClientErrorBadRequest(str(uri_attr))
     298        if printer_name not in self.printers:
     299            raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr)
     300
     301        # get attributes from the printer and add to response
     302        job = self.printers[printer_name].create_job(request)
     303        response.attribute_groups.append(ipp.AttributeGroup(
     304            ipp.AttributeTags.JOB, job.get_job_attributes(operation)))
    267305   
    268306    @handler_for(ipp.OperationCodes.PAUSE_PRINTER)
     
    296334            OPTIONAL 'requesting-user-name' (name(MAX))
    297335            OPTIONAL 'requested-attributes' (1setOf type2 keyword)
    298             OPTIONAL 'document-format' (mimeMediaType):
    299 
     336            OPTIONAL 'document-format' (mimeMediaType)
     337           
    300338        Response
    301339        --------
     
    324362        printer = self.printers[printer_name]
    325363
    326         # requesting username
    327         if 'requesting-user-name' in operation:
    328             username_attr = operation['requesting-user-name']
    329             username = username_attr.values[0].value
    330             if username_attr != ipp.RequestingUserName(username):
    331                 raise ipp.errors.ClientErrorBadRequest(str(username_attr))
    332 
    333364        # get attributes from the printer and add to response
    334365        response.attribute_groups.append(ipp.AttributeGroup(
    335             ipp.AttributeTags.PRINTER, printer.get_printer_attributes(request)))
     366            ipp.AttributeTags.PRINTER, printer.get_printer_attributes(operation)))
    336367
    337368    @handler_for(ipp.OperationCodes.SET_PRINTER_ATTRIBUTES)
     
    379410        job = printer.get_job(job_id)
    380411
    381         # requesting username
    382         if 'requesting-user-name' in operation:
    383             username_attr = operation['requesting-user-name']
    384             username = username_attr.values[0].value
    385             if username_attr != ipp.RequestingUserName(username):
    386                 raise ipp.errors.ClientErrorBadRequest(str(username_attr))
    387 
    388412        # get the job attributes and add them to the response
    389         attrs = job.get_job_attributes(request)
     413        attrs = job.get_job_attributes(operation)
    390414        response.attribute_groups.append(ipp.AttributeGroup(
    391415            ipp.AttributeTags.JOB, attrs))
     
    438462        printer = self.printers[self.default]
    439463
    440         # requesting username
    441         if 'requesting-user-name' in operation:
    442             username_attr = operation['requesting-user-name']
    443             username = username_attr.values[0].value
    444             if username_attr != ipp.RequestingUserName(username):
    445                 raise ipp.errors.ClientErrorBadRequest(str(username_attr))
    446 
    447464        # get attributes from the printer and add to response
    448465        response.attribute_groups.append(ipp.AttributeGroup(
    449             ipp.AttributeTags.PRINTER, printer.get_printer_attributes(request)))
     466            ipp.AttributeTags.PRINTER, printer.get_printer_attributes(operation)))
    450467
    451468    @handler_for(ipp.OperationCodes.CUPS_GET_PRINTERS)
     
    462479        operation = request.attribute_groups[0]
    463480
    464         # requesting username
    465         if 'requesting-user-name' in operation:
    466             username_attr = operation['requesting-user-name']
    467             username = username_attr.values[0].value
    468             if username_attr != ipp.RequestingUserName(username):
    469                 raise ipp.errors.ClientErrorBadRequest(str(username_attr))
    470 
    471481        # get attributes from the printer and add to response
    472482        for printer in self.printers.values():
    473483            response.attribute_groups.append(ipp.AttributeGroup(
    474                 ipp.AttributeTags.PRINTER, printer.get_printer_attributes(request)))
     484                ipp.AttributeTags.PRINTER, printer.get_printer_attributes(operation)))
    475485
    476486    @handler_for(ipp.OperationCodes.CUPS_GET_CLASSES)
  • server/lib/gutenbach/server/server.py

    raef164a ree8e6d0  
    99logger = logging.getLogger(__name__)
    1010
     11# initialize handler
     12handler = GutenbachRequestHandler()
     13
    1114class GutenbachIPPServer(BaseHTTPServer.BaseHTTPRequestHandler):
    12     def setup(self):
    13         self.root = GutenbachRequestHandler()
    14         BaseHTTPServer.BaseHTTPRequestHandler.setup(self)
    15 
    16     def handle_one_request(self):
    17         self.raw_requestline = self.rfile.readline()
    18         if not self.raw_requestline:
    19             self.close_connection = 1
    20             return
    21         if not self.parse_request(): # An error code has been sent, just exit
    22             return
    23         self.handle_ipp()
    24 
    25     def handle_ipp(self):
     15    def do_POST(self):
    2616        # Receive a request
    2717        length = int(self.headers.getheader('content-length', 0))
     18        if length == 0:
     19            logger.warning("content-length == 0")
     20            return
    2821        request = ipp.Request(request=self.rfile, length=length)
    2922
     
    3326        logger.debug("Received request: %s" % repr(request))
    3427        try:
    35             response = self.root.handle(request)
     28            response = handler.handle(request)
    3629        except:
    3730            logger.fatal(traceback.format_exc())
Note: See TracChangeset for help on using the changeset viewer.