Ignore:
Timestamp:
Dec 18, 2011, 11:18:16 PM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
9eeab06
Parents:
d04a689
git-author:
Jessica B. Hamrick <jhamrick@…> (12/18/11 23:18:16)
git-committer:
Jessica B. Hamrick <jhamrick@…> (12/18/11 23:18:16)
Message:

Refactor code to handle the operations a little bit more logically

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/gutenbach/server/printer.py

    rd04a689 rb2e077a  
     1#import alsaaudio as aa
    12from .exceptions import InvalidJobException, InvalidPrinterStateException
    23from gutenbach.ipp.attribute import Attribute
    3 import alsaaudio as aa
    4 import gutenbach.ipp
     4import gutenbach.ipp as ipp
     5import gutenbach.ipp.constants as const
     6import logging
     7import time
    58
    69# initialize logger
    710logger = logging.getLogger(__name__)
    811
    9 class Printer(object):
    10 
    11     def __init__(self, name, card, mixer):
    12 
    13         self.name = name
    14 
    15         if card >= len(aa.cards()):
    16             raise aa.ALSAAudioError(
    17                 "Audio card at index %d does not exist!" % card)
    18         elif mixer not in aa.mixers(card):
    19             raise aa.ALSAAudioError(
    20                 "Audio mixer '%s' does not exist!" % mixer)
     12class GutenbachPrinter(object):
     13
     14    attributes = [
     15        "printer-uri-supported",
     16        "uri-authentication-supported",
     17        "uri-security-supported",
     18        "printer-name",
     19        "printer-state",
     20        "printer-state-reasons",
     21        "ipp-versions-supported",
     22        "operations-supported",
     23        "charset-configured",
     24        "charset-supported",
     25        "natural-language-configured",
     26        "generated-natural-language-supported",
     27        "document-format-default",
     28        "document-format-supported",
     29        "printer-is-accepting-jobs",
     30        "queued-job-count",
     31        "pdl-override-supported",
     32        "printer-up-time",
     33        "compression-supported"]
     34
     35    #def __init__(self, name, card, mixer):
     36    def __init__(self, name):
     37
     38        self._name = name
     39        self._uri = "ipp://localhost:8000/printers/" + self._name
     40        self._time_created = int(time.time())
     41
     42        # if card >= len(aa.cards()):
     43        #     raise aa.ALSAAudioError(
     44        #       "Audio card at index %d does not exist!" % card)
     45        # elif mixer not in aa.mixers(card):
     46        #     raise aa.ALSAAudioError(
     47        #       "Audio mixer '%s' does not exist!" % mixer)
    2148       
    22         self.card = card
    23         self.mixer = mixer
     49        # self.card = card
     50        # self.mixer = mixer
    2451
    2552        self.finished_jobs = []
     
    2956        self._next_jobid = 0
    3057
     58    ## Printer attributes
     59    @property
     60    def printer_uri_supported(self):
     61        return ipp.Attribute(
     62            "printer-uri-supported",
     63            [ipp.Value(ipp.Tags.URI, self._uri)])
     64   
     65    @property
     66    def uri_authentication_supported(self):
     67        return ipp.Attribute(
     68            "uri-authentication-supported",
     69            [ipp.Value(ipp.Tags.KEYWORD, "none")])
     70
     71    @property
     72    def uri_security_supported(self):
     73        return ipp.Attribute(
     74            "uri-security-supported",
     75            [ipp.Value(ipp.Tags.KEYWORD, "none")])
     76
     77    @property
     78    def printer_name(self):
     79        return ipp.Attribute(
     80            "printer-name",
     81            [ipp.Value(ipp.Tags.NAME_WITHOUT_LANGUAGE, self._name)])
     82       
     83    @property
     84    def printer_state(self):
     85        return ipp.Attribute(
     86            "printer-state",
     87            [ipp.Value(ipp.Tags.ENUM, const.PrinterStates.IDLE)])
     88       
     89    @property
     90    def printer_state_reasons(self):
     91        return ipp.Attribute(
     92            "printer-state-reasons",
     93            [ipp.Value(ipp.Tags.KEYWORD, "none")])
     94
     95    @property
     96    def ipp_versions_supported(self):
     97        return ipp.Attribute(
     98            "ipp-versions-supported",
     99            [ipp.Value(ipp.Tags.KEYWORD, "1.0"),
     100             ipp.Value(ipp.Tags.KEYWORD, "1.1")])
     101
     102    # XXX: We should query ourself for the supported operations
     103    @property
     104    def operations_supported(self):
     105        return ipp.Attribute(
     106            "operations-supported",
     107            [ipp.Value(ipp.Tags.ENUM, const.Operations.GET_JOBS)])
     108
     109    @property
     110    def charset_configured(self):
     111        return ipp.Attribute(
     112            "charset-configured",
     113            [ipp.Value(ipp.Tags.CHARSET, "utf-8")])
     114
     115    @property
     116    def charset_supported(self):
     117        return ipp.Attribute(
     118            "charset-supported",
     119            [ipp.Value(ipp.Tags.CHARSET, "utf-8")])
     120
     121    @property
     122    def natural_language_configured(self):
     123        return ipp.Attribute(
     124            "natural-language-configured",
     125            [ipp.Value(ipp.Tags.NATURAL_LANGUAGE, "en-us")])
     126
     127    @property
     128    def generated_natural_language_supported(self):
     129        return ipp.Attribute(
     130            "generated-natural-language-supported",
     131            [ipp.Value(ipp.Tags.NATURAL_LANGUAGE, "en-us")])
     132
     133    @property
     134    def document_format_default(self):
     135        return ipp.Attribute(
     136            "document-format-default",
     137            [ipp.Value(ipp.Tags.MIME_MEDIA_TYPE, "application/octet-stream")])
     138
     139    @property
     140    def document_format_supported(self):
     141        return ipp.Attribute(
     142            "document-format-supported",
     143            [ipp.Value(ipp.Tags.MIME_MEDIA_TYPE, "application/octet-stream"),
     144             ipp.Value(ipp.Tags.MIME_MEDIA_TYPE, "audio/mp3")])
     145
     146    @property
     147    def printer_is_accepting_jobs(self):
     148        return ipp.Attribute(
     149            "printer-is-accepting-jobs",
     150            [ipp.Value(ipp.Tags.BOOLEAN, True)])
     151
     152    @property
     153    def queued_job_count(self):
     154        return ipp.Attribute(
     155            "queued-job-count",
     156            [ipp.Value(ipp.Tags.INTEGER, len(self.active_jobs))])
     157
     158    @property
     159    def pdl_override_supported(self):
     160        return ipp.Attribute(
     161            "pdl-override-supported",
     162            [ipp.Value(ipp.Tags.KEYWORD, "not-attempted")])
     163
     164    @property
     165    def printer_up_time(self):
     166        return ipp.Attribute(
     167            "printer-up-time",
     168            [ipp.Value(ipp.Tags.INTEGER, int(time.time()) - self._time_created)])
     169
     170    @property
     171    def compression_supported(self):
     172        return ipp.Attribute(
     173            "compression-supported",
     174            [ipp.Value(ipp.Tags.KEYWORD, "none")])
     175
     176    def get_printer_attributes(self, request):
     177        attributes = [getattr(self, attr.replace("-", "_")) for attr in self.attributes]
     178        return attributes
     179
     180    ## Printer operations
    31181    @property
    32182    def next_jobid(self):
    33183        self._next_jobid += 1
    34184        return self._next_jobid
    35 
    36     @next_jobid.setter
    37     def next_jobid(self, val):
    38         raise AttributeError("Setting next_jobid is illegal!")
    39185
    40186    def print_job(self, job):
     
    75221        return self.jobs[jobid]
    76222
     223    def get_jobs(self):
     224        jobs = [self.jobs[job_id] for job_id in self.active_jobs]
     225        return jobs           
     226
    77227    def __repr__(self):
    78228        return str(self)
    79229
    80230    def __str__(self):
    81         return "<Printer '%s'>" % self.name
     231        return "<Printer '%s'>" % self._name
Note: See TracChangeset for help on using the changeset viewer.