Changeset 3437

Show
Ignore:
Timestamp:
10/06/08 00:24:42 (3 months ago)
Author:
jerome
Message:

Removed spaces at EOL.

Location:
pkipplib/trunk
Files:
11 modified

Legend:

Unmodified
Added
Removed
  • pkipplib/trunk/bin/pksubscribe

    r45 r3437  
    11#! /usr/bin/env python 
    2 # -*- coding: UTF-8 -*- 
     2# -*- coding: utf-8 -*- 
    33# 
    44# pkipplib : IPP and CUPS support for Python 
     
    99# the Free Software Foundation, either version 3 of the License, or 
    1010# (at your option) any later version. 
    11 #  
     11# 
    1212# This program is distributed in the hope that it will be useful, 
    1313# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1414# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1515# GNU General Public License for more details. 
    16 #  
     16# 
    1717# You should have received a copy of the GNU General Public License 
    1818# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     
    2828import optparse 
    2929 
    30 from pkipplib import pkipplib         
    31      
    32 if __name__ == "__main__" :     
     30from pkipplib import pkipplib 
     31 
     32if __name__ == "__main__" : 
    3333    try : 
    3434        locale.setlocale(locale.LC_ALL, "") 
     
    4040        gettext.NullTranslations().install() 
    4141    parser = optparse.OptionParser(usage="pksubscribe [options] [subscriptions ids]") 
    42     parser.add_option("-v", "--version",  
    43                             action="store_true",  
     42    parser.add_option("-v", "--version", 
     43                            action="store_true", 
    4444                            dest="version", 
    4545                            help=_("show pksubscribe's version number and exit.")) 
    46     parser.add_option("-c", "--cups",  
    47                             default="http://localhost:631",  
     46    parser.add_option("-c", "--cups", 
     47                            default="http://localhost:631", 
    4848                            dest="cups", 
    4949                            help=_("the CUPS server to connect to. Defaults to http://localhost:631")) 
    50     parser.add_option("-d", "--debug",  
    51                             action="store_true",  
     50    parser.add_option("-d", "--debug", 
     51                            action="store_true", 
    5252                            dest="debug", 
    5353                            help=_("activate debug mode.")) 
    54     parser.add_option("-X", "--delete",  
    55                             action="store_true",  
     54    parser.add_option("-X", "--delete", 
     55                            action="store_true", 
    5656                            dest="delete", 
    5757                            help=_("deletes subscriptions.")) 
    58     parser.add_option("-p", "--printer",  
     58    parser.add_option("-p", "--printer", 
    5959                            dest="printer", 
    6060                            help=_("the printer's name for a printer subscription.")) 
    61     parser.add_option("-j", "--job",  
    62                             type="int",  
     61    parser.add_option("-j", "--job", 
     62                            type="int", 
    6363                            dest="job", 
    6464                            help=_("the job's id for a job subscripition.")) 
    65     parser.add_option("-r", "--recipient",                         
     65    parser.add_option("-r", "--recipient", 
    6666                            dest="recipient", 
    6767                            help=_("the recipient's uri.")) 
    68     parser.add_option("-C", "--charset",                         
     68    parser.add_option("-C", "--charset", 
    6969                            dest="charset", 
    7070                            help=_("the charset to use in notifications sent for this subscription.")) 
    71     parser.add_option("-L", "--language",                         
     71    parser.add_option("-L", "--language", 
    7272                            dest="language", 
    7373                            help=_("the language to use in notifications sent for this subscription.")) 
    74     parser.add_option("-u", "--userdata",                         
     74    parser.add_option("-u", "--userdata", 
    7575                            dest="userdata", 
    7676                            help=_("the user's data to use in notifications for this subscription.")) 
    77     parser.add_option("-U", "--username",                         
     77    parser.add_option("-U", "--username", 
    7878                            dest="username", 
    7979                            help=_("the user's name to use when connecting to the CUPS server.")) 
    80     parser.add_option("-W", "--password",                         
     80    parser.add_option("-W", "--password", 
    8181                            dest="password", 
    8282                            help=_("the user's password to use when connecting to the CUPS server.")) 
    83     parser.add_option("-E", "--events",                         
     83    parser.add_option("-E", "--events", 
    8484                            dest="events", 
    8585                            help=_("a comma separated list of events to subscribe to.")) 
    86     parser.add_option("-P", "--pullmethod",                         
     86    parser.add_option("-P", "--pullmethod", 
    8787                            dest="pullmethod", 
    8888                            help=_("the optional pull method's name.")) 
     
    9191                            dest="duration", 
    9292                            help=_("the duration of the subscription.")) 
    93     parser.add_option("-I", "--interval",                         
     93    parser.add_option("-I", "--interval", 
    9494                            type="int", 
    9595                            dest="interval", 
    9696                            help=_("the time interval of the subscription.")) 
    97                              
     97 
    9898    (options, arguments) = parser.parse_args() 
    9999    if options.version : 
     
    102102        if not options.events and not options.delete : 
    103103            sys.stderr.write(_("You MUST pass a list of events to subscribe to.\n")) 
    104         elif not options.recipient and not options.delete :     
     104        elif not options.recipient and not options.delete : 
    105105            sys.stderr.write(_("You MUST pass a recipient for the subscription.\n")) 
    106         elif options.delete and not arguments :     
     106        elif options.delete and not arguments : 
    107107            sys.stderr.write(_("You MUST pass a subscriptions ids at the end of your command line.\n")) 
    108         else :     
     108        else : 
    109109            cups = pkipplib.CUPS(options.cups, 
    110110                                 options.username, 
     
    112112                                 debug=options.debug) 
    113113            baseurl = options.cups.replace("http://", "ipp://") 
    114             if baseurl.endswith(":631") :  
     114            if baseurl.endswith(":631") : 
    115115                baseurl = baseurl[:-4] 
    116116            if options.printer : 
    117117                url = "%s/printers/%s" % (baseurl, options.printer) 
    118             elif options.job :      
     118            elif options.job : 
    119119                url = "%s/jobs/%i" % (baseurl, options.job) 
    120             else :     
     120            else : 
    121121                url = baseurl 
    122             if not options.delete :     
     122            if not options.delete : 
    123123                answer = cups.createSubscription(url, 
    124124                                                 [e.strip() for e in options.events.split(",")], 
     
    131131                                                 timeinterval=options.interval, 
    132132                                                 jobid=options.job) 
    133                 try :                                  
     133                try : 
    134134                    subscriptionid = answer.subscription["notify-subscription-id"][0][1] 
    135135                except KeyError : 
    136136                    sys.stderr.write("%s\n" % answer.operation["status-message"][0][1]) 
    137                 else :     
     137                else : 
    138138                    print _("Subscription %i registered.") % subscriptionid 
    139             else :         
     139            else : 
    140140                for subid in [int(arg) for arg in arguments] : 
    141141                    answer = cups.cancelSubscription(url, subid, options.job) 
     
    143143                        try : 
    144144                            error = answer.operation["status-message"][0][1] 
    145                         except KeyError :     
     145                        except KeyError : 
    146146                            print _("Subscription %i cancelled.") % subid 
    147147                        else : 
    148148                            sys.stderr.write("%s\n" % error) 
    149                     else :         
     149                    else : 
    150150                        sys.stderr.write(_("Incorrect answer (None)\n")) 
    151151 
  • pkipplib/trunk/clean.sh

    r45 r3437  
    1 #! /bin/sh 
     1y#! /bin/sh 
    22# 
    33# pkipplib : IPP support for Python 
     
    88# the Free Software Foundation, either version 3 of the License, or 
    99# (at your option) any later version. 
    10 #  
     10# 
    1111# This program is distributed in the hope that it will be useful, 
    1212# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414# GNU General Public License for more details. 
    15 #  
     15# 
    1616# You should have received a copy of the GNU General Public License 
    1717# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  • pkipplib/trunk/MANIFEST.in

    r36 r3437  
    1 include README COPYING NEWS MANIFEST.in clean.sh bin/pksubscribe 
     1yinclude README COPYING NEWS MANIFEST.in clean.sh bin/pksubscribe 
    22recursive-include notifiers * 
  • pkipplib/trunk/notifiers/samplenotifier

    r45 r3437  
    11#! /usr/bin/env python 
    2 # -*- coding: UTF-8 -*- 
     2# -*- coding: utf-8 -*- 
    33# 
    44# pkipplib : IPP and CUPS support for Python 
     
    99# the Free Software Foundation, either version 3 of the License, or 
    1010# (at your option) any later version. 
    11 #  
     11# 
    1212# This program is distributed in the hope that it will be useful, 
    1313# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1414# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1515# GNU General Public License for more details. 
    16 #  
     16# 
    1717# You should have received a copy of the GNU General Public License 
    1818# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     
    3838    # First thing we do is put stdin in non-blocking mode. 
    3939    fd = sys.stdin.fileno() 
    40     fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL,  
     40    fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, 
    4141                    fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) 
    4242 
    4343    # then we read the notification CUPS sent us to our stdin 
    4444    notification = pkipplib.IPPRequest(sys.stdin.read()) 
    45      
     45 
    4646    # now we parse it 
    4747    notification.parse() 
    48      
     48 
    4949    # then we act one way or another, depending on the event received. 
    5050    event = notification.event_notification["notify-subscribed-event"][0][1] 
     
    5353        if event.endswith("-added") : 
    5454            action = "add" 
    55         else :     
     55        else : 
    5656            action = "delete" 
    5757        os.system('/usr/bin/pkprinters --%s "%s"' % (action, printername)) 
  • pkipplib/trunk/pkipplib/__init__.py

    r45 r3437  
    1 # -*- coding: UTF-8 -*- 
     1# -*- coding: utf-8 -*- 
    22# 
    33# pkipplib : IPP and CUPS support for Python 
     
    88# the Free Software Foundation, either version 3 of the License, or 
    99# (at your option) any later version. 
    10 #  
     10# 
    1111# This program is distributed in the hope that it will be useful, 
    1212# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414# GNU General Public License for more details. 
    15 #  
     15# 
    1616# You should have received a copy of the GNU General Public License 
    1717# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  • pkipplib/trunk/pkipplib/pkipplib.py

    r45 r3437  
    11#! /usr/bin/env python 
    2 # -*- coding: UTF-8 -*- 
     2# -*- coding: utf-8 -*- 
    33# 
    44# pkipplib : IPP and CUPS support for Python 
     
    99# the Free Software Foundation, either version 3 of the License, or 
    1010# (at your option) any later version. 
    11 #  
     11# 
    1212# This program is distributed in the hope that it will be useful, 
    1313# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1414# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1515# GNU General Public License for more details. 
    16 #  
     16# 
    1717# You should have received a copy of the GNU General Public License 
    1818# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     
    238238IPP_MULTIPLE_JOBS_NOT_SUPPORTED = 0x0509 
    239239IPP_PRINTER_IS_DEACTIVATED = 0x50a 
    240    
     240 
    241241CUPS_PRINTER_LOCAL = 0x0000 
    242242CUPS_PRINTER_CLASS = 0x0001 
     
    265265CUPS_PRINTER_COMMANDS = 0x8000 
    266266CUPS_PRINTER_OPTIONS = 0xe6ff 
    267    
    268    
     267 
     268 
    269269class IPPError(Exception) : 
    270270    """An exception for IPP related stuff.""" 
     
    282282        self.request = request 
    283283        self.name = name 
    284          
     284 
    285285    def __setitem__(self, key, value) : 
    286286        """Appends the value to the real attribute.""" 
     
    293293                    attribute[j][1].append(value) 
    294294                    return 
    295             attribute.append((key, [value]))         
    296              
     295            attribute.append((key, [value])) 
     296 
    297297    def __getitem__(self, key) : 
    298298        """Returns an attribute's value.""" 
     
    307307        if answer : 
    308308            return answer 
    309         raise KeyError, key             
    310      
     309        raise KeyError, key 
     310 
    311311class IPPRequest : 
    312312    """A class for IPP requests.""" 
    313313    attributes_types = ("operation", "job", "printer", "unsupported", \ 
    314314                                     "subscription", "event_notification") 
    315     def __init__(self, data="", version=IPP_VERSION,  
     315    def __init__(self, data="", version=IPP_VERSION, 
    316316                                operation_id=None, \ 
    317317                                request_id=None, \ 
    318318                                debug=False) : 
    319319        """Initializes an IPP Message object. 
    320          
     320 
    321321           Parameters : 
    322             
     322 
    323323             data : the complete IPP Message's content. 
    324324             debug : a boolean value to output debug info on stderr. 
     
    327327        self._data = data 
    328328        self.parsed = False 
    329          
     329 
    330330        # Initializes message 
    331         self.setVersion(version)                 
     331        self.setVersion(version) 
    332332        self.setOperationId(operation_id) 
    333333        self.setRequestId(request_id) 
    334334        self.data = "" 
    335          
     335 
    336336        for attrtype in self.attributes_types : 
    337337            setattr(self, "_%s_attributes" % attrtype, [[]]) 
    338          
    339         # Initialize tags     
     338 
     339        # Initialize tags 
    340340        self.tags = [ None ] * 256 # by default all tags reserved 
    341          
     341 
    342342        # Delimiter tags 
    343343        self.tags[0x01] = "operation-attributes-tag" 
     
    348348        self.tags[0x06] = "subscription-attributes-tag" 
    349349        self.tags[0x07] = "event_notification-attributes-tag" 
    350          
     350 
    351351        # out of band values 
    352352        self.tags[0x10] = "unsupported" 
     
    357357        self.tags[0x16] = "delete-attribute" 
    358358        self.tags[0x17] = "admin-define" 
    359    
     359 
    360360        # integer values 
    361361        self.tags[0x20] = "generic-integer" 
     
    363363        self.tags[0x22] = "boolean" 
    364364        self.tags[0x23] = "enum" 
    365          
     365 
    366366        # octetString 
    367367        self.tags[0x30] = "octetString-with-an-unspecified-format" 
     
    373373        self.tags[0x36] = "nameWithLanguage" 
    374374        self.tags[0x37] = "endCollection" 
    375          
     375 
    376376        # character strings 
    377377        self.tags[0x40] = "generic-character-string" 
     
    385385        self.tags[0x49] = "mimeMediaType" 
    386386        self.tags[0x4a] = "memberAttrName" 
    387          
     387 
    388388        # Reverse mapping to generate IPP messages 
    389389        self.tagvalues = {} 
     
    392392            if value is not None : 
    393393                self.tagvalues[value] = i 
    394                                       
    395     def __getattr__(self, name) :                                  
     394 
     395    def __getattr__(self, name) : 
    396396        """Fakes attribute access.""" 
    397397        if name in self.attributes_types : 
     
    399399        else : 
    400400            raise AttributeError, name 
    401              
    402     def __str__(self) :         
     401 
     402    def __str__(self) : 
    403403        """Returns the parsed IPP message in a readable form.""" 
    404404        if not self.parsed : 
     
    414414                for (name, value) in attribute : 
    415415                    mybuffer.append("  %s : %s" % (name, value)) 
    416         if self.data :             
     416        if self.data : 
    417417            mybuffer.append("IPP datas : %s" % repr(self.data)) 
    418418        return "\n".join(mybuffer) 
    419          
    420     def logDebug(self, msg) :     
     419 
     420    def logDebug(self, msg) : 
    421421        """Prints a debug message.""" 
    422422        if self.debug : 
    423423            sys.stderr.write("%s\n" % msg) 
    424424            sys.stderr.flush() 
    425              
     425 
    426426    def setVersion(self, version) : 
    427427        """Sets the request's operation id.""" 
     
    432432                if len(version) == 2 : # 2-tuple 
    433433                    self.version = version 
    434                 else :     
     434                else : 
    435435                    try : 
    436436                        self.version = [int(p) for p in str(float(version)).split(".")] 
    437437                    except : 
    438438                        self.version = [int(p) for p in IPP_VERSION.split(".")] 
    439          
    440     def setOperationId(self, opid) :         
     439 
     440    def setOperationId(self, opid) : 
    441441        """Sets the request's operation id.""" 
    442442        self.operation_id = opid 
    443          
    444     def setRequestId(self, reqid) :         
     443 
     444    def setRequestId(self, reqid) : 
    445445        """Sets the request's request id.""" 
    446446        self.request_id = reqid 
    447          
    448     def dump(self) :     
     447 
     448    def dump(self) : 
    449449        """Generates an IPP Message. 
    450          
     450 
    451451           Returns the message as a string of text. 
    452         """     
     452        """ 
    453453        mybuffer = [] 
    454454        if None not in (self.version, self.operation_id) : 
     
    468468                                mybuffer.append(attrname) 
    469469                                nameprinted = 1 
    470                             else :      
     470                            else : 
    471471                                mybuffer.append(pack(">H", 0)) 
    472472                            if vtype in ("integer", "enum") : 
     
    476476                                mybuffer.append(pack(">H", 1)) 
    477477                                mybuffer.append(chr(val)) 
    478                             else :     
     478                            else : 
    479479                                mybuffer.append(pack(">H", len(val))) 
    480480                                mybuffer.append(val) 
    481481            mybuffer.append(chr(self.tagvalues["end-of-attributes-tag"])) 
    482         mybuffer.append(self.data)     
     482        mybuffer.append(self.data) 
    483483        return "".join(mybuffer) 
    484              
     484 
    485485    def parse(self) : 
    486486        """Parses an IPP Request. 
    487          
     487 
    488488           NB : Only a subset of RFC2910 is implemented. 
    489489        """ 
    490490        self._curname = None 
    491491        self._curattributes = None 
    492          
     492 
    493493        try : 
    494494            self.setVersion((ord(self._data[0]), ord(self._data[1]))) 
     
    509509                        self.position -= 1 
    510510                        continue 
    511                 oldtag = tag         
     511                oldtag = tag 
    512512                tag = ord(self._data[self.position]) 
    513513                if tag == oldtag : 
     
    515515        except IndexError : 
    516516            raise IPPError, "Unexpected end of IPP message." 
    517              
    518         self.data = self._data[self.position+1:]             
     517 
     518        self.data = self._data[self.position+1:] 
    519519        self.parsed = True 
    520          
    521     def parseTag(self) :     
     520 
     521    def parseTag(self) : 
    522522        """Extracts information from an IPP tag.""" 
    523523        pos = self.position 
     
    528528        if not namelength : 
    529529            name = self._curname 
    530         else :     
     530        else :