Show
Ignore:
Timestamp:
09/27/08 22:02:37 (16 years ago)
Author:
jerome
Message:

Removed unnecessary spaces at EOL.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkusers

    r3411 r3413  
    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/>. 
     
    4040 
    4141  pkusers [options] user1 user2 user3 ... userN 
    42    
    43 or :   
     42 
     43or : 
    4444 
    4545  pkusers --groups [options] group1 group2 group3 ... groupN 
     
    4949  -v | --version       Prints pkusers's version number then exits. 
    5050  -h | --help          Prints this message then exits. 
    51    
     51 
    5252  -a | --add           Adds users if they don't exist on the database. 
    5353                       If they exist, they are modified unless 
    5454                       -s|--skipexisting is also used. 
    55                         
     55 
    5656  -d | --delete        Deletes users from the quota storage. 
    5757 
     
    6262 
    6363  -D | --description d Adds a textual description to users or groups. 
    64                         
     64 
    6565  -g | --groups        Edit users groups instead of users. 
    66                            
    67   -o | --overcharge f  Sets the overcharging factor applied to the user  
    68                        when computing the cost of a print job. Positive or  
     66 
     67  -o | --overcharge f  Sets the overcharging factor applied to the user 
     68                       when computing the cost of a print job. Positive or 
    6969                       negative floating point values are allowed, 
    7070                       this allows you to do some really creative 
     
    7474                       cost of the job for a particular user. 
    7575                       Only users have such a coefficient. 
    76    
     76 
    7777  -i | --ingroups g1[,g2...]  Puts the users into each of the groups 
    7878                              listed, separated by commas. The groups 
    7979                              must already exist in the Quota Storage. 
    80                         
     80 
    8181  -L | --list          Lists users or groups. 
    82    
    83   -l | --limitby l     Choose if the user/group is limited in printing                      
     82 
     83  -l | --limitby l     Choose if the user/group is limited in printing 
    8484                       by its account balance or by its page quota. 
    8585                       The default value is 'quota'. Allowed values 
    86                        are 'quota' 'balance' 'noquota' 'noprint'  
     86                       are 'quota' 'balance' 'noquota' 'noprint' 
    8787                       and 'nochange' : 
    88                         
     88 
    8989                         - quota : limit by number of pages per printer. 
    9090                         - balance : limit by number of credits in account. 
    9191                         - noquota : no limit, accounting still done. 
    92                          - nochange : no limit, accounting not done.  
    93                          - noprint : printing is denied.  
     92                         - nochange : no limit, accounting not done. 
     93                         - noprint : printing is denied. 
    9494                       NB : nochange and noprint are not supported for groups. 
    95                         
    96   -b | --balance b     Sets the user's account balance to b.                      
     95 
     96  -b | --balance b     Sets the user's account balance to b. 
    9797                       Account balance may be increase or decreased 
    9898                       if b is prefixed with + or -. 
     
    102102                       Groups don't have a real balance, but the 
    103103                       sum of their users' account balance. 
    104                         
     104 
    105105  -C | --comment txt   Defines some informational text to be associated 
    106106                       with a change to an user's account balance. 
    107107                       Only meaningful if -b | --balance is also used. 
    108                         
    109                         
    110   -r | --remove        In combination with the --ingroups option above,                        
     108 
     109 
     110  -r | --remove        In combination with the --ingroups option above, 
    111111                       remove users from the specified users groups. 
    112                         
     112 
    113113  -s | --skipexisting  In combination with the --add option above, tells 
    114114                       pkusers to not modify existing users. 
    115                         
     115 
    116116  user1 through userN and group1 through groupN can use wildcards 
    117117  if the --add option is not set. 
    118    
    119 examples :                               
     118 
     119examples : 
    120120 
    121121  $ pkusers --add john paul george ringo/ringo@example.com 
    122    
     122 
    123123  This will add users john, paul, george and ringo to the quota 
    124   database. User ringo's email address will also be set to  
     124  database. User ringo's email address will also be set to 
    125125  'ringo@example.com' 
    126    
     126 
    127127  $ pkusers --ingroups coders,it jerome 
    128    
     128 
    129129  User jerome is put into the groups "coders" and "it" which must 
    130130  already exist in the quota database. 
    131              
     131 
    132132  $ pkusers --limitby balance jerome 
    133    
     133 
    134134  This will tell PyKota to limit jerome by his account's balance 
    135135  when printing. 
    136    
     136 
    137137  $ pkusers --balance +10.0 --comment "He paid with his blood !" jerome 
    138    
     138 
    139139  This will increase jerome's account balance by 10.0 (in your 
    140140  own currency). You can decrease the account balance with a 
    141141  dash prefix, and set it to a fixed amount with no prefix. 
    142142  A comment will be stored for this balance change. 
    143    
     143 
    144144  $ pkusers --delete jerome rachel 
    145    
     145 
    146146  This will completely delete jerome and rachel from the quota 
    147147  database. All their quotas and jobs will be deleted too. 
    148    
     148 
    149149  $ pkusers --overcharge 2.5 poorstudent 
    150    
     150 
    151151  This will overcharge the poorstudent user by a factor of 2.5. 
    152    
     152 
    153153  $ pkusers --overcharge -1 jerome 
    154    
     154 
    155155  User jerome will actually earn money whenever he prints. 
    156    
     156 
    157157  $ pkusers --overcharge 0 boss 
    158    
     158 
    159159  User boss can print at will, it won't cost him anything because the 
    160160  cost of each print job will be multiplied by zero before charging 
     
    165165  This will set the email address for each user to username@example.com 
    166166""") 
    167          
    168 class PKUsers(PyKotaTool) :         
     167 
     168class PKUsers(PyKotaTool) : 
    169169    """A class for a users and users groups manager.""" 
    170170    def modifyEntry(self, entry, groups, limitby, description, overcharge=None, balance=None, balancevalue=None, comment=None, email=None) : 
     
    172172        if description is not None : # NB : "" is allowed ! 
    173173            entry.setDescription(description) 
    174         if limitby :     
     174        if limitby : 
    175175            entry.setLimitBy(limitby) 
    176176        if not groups : 
     
    181181                    raise PyKotaCommandLineError, _("Invalid email address %s") % email 
    182182                entry.setEmail(email) 
    183             if overcharge is not None : # NB : 0 is allowed !      
     183            if overcharge is not None : # NB : 0 is allowed ! 
    184184                entry.setOverChargeFactor(overcharge) 
    185185            if balance : 
     
    192192                    newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff 
    193193                    entry.setAccountBalance(balancevalue, newlifetimepaid, comment) 
    194                      
    195     def manageUsersGroups(self, ugroups, user, remove) :         
     194 
     195    def manageUsersGroups(self, ugroups, user, remove) : 
    196196        """Manage user group membership.""" 
    197197        for ugroup in ugroups : 
     
    200200            else : 
    201201                ugroup.addUserToGroup(user) 
    202                  
     202 
    203203    def main(self, names, options) : 
    204204        """Manage users or groups.""" 
    205205        names = self.sanitizeNames(options, names) 
    206         suffix = (options["groups"] and "Group") or "User"         
    207          
     206        suffix = (options["groups"] and "Group") or "User" 
     207 
    208208        if not options["list"] : 
    209209            percent = Percent(self) 
    210              
     210 
    211211        if not options["add"] : 
    212212            if not options["list"] : 
     
    219219                    percent.display("\n") 
    220220                raise PyKotaCommandLineError, _("There's no %s matching %s") % (_(suffix.lower()), " ".join(names)) 
    221             if not options["list"] :     
     221            if not options["list"] : 
    222222                percent.setSize(len(entries)) 
    223                  
     223 
    224224        if options["list"] : 
    225225            if suffix == "User" : 
     
    229229                    email = entry.Email 
    230230                    if not email : 
    231                         if maildomain :      
     231                        if maildomain : 
    232232                            email = "%s@%s" % (entry.Name, maildomain) 
    233                         elif smtpserver :     
     233                        elif smtpserver : 
    234234                            email = "%s@%s" % (entry.Name, smtpserver) 
    235                         else :     
     235                        else : 
    236236                            email = "%s@%s" % (entry.Name, "localhost") 
    237237                    msg = "%s - <%s>" % (entry.Name, email) 
    238238                    if entry.Description : 
    239239                        msg += " - %s" % entry.Description 
    240                     print msg     
     240                    print msg 
    241241                    print "    %s" % (_("Limited by : %s") % entry.LimitBy) 
    242242                    print "    %s" % (_("Account balance : %.2f") % (entry.AccountBalance or 0.0)) 
     
    244244                    print "    %s" % (_("Overcharging factor : %.2f") % entry.OverCharge) 
    245245                    print 
    246             else :     
     246            else : 
    247247                for entry in entries : 
    248248                    msg = "%s" % entry.Name 
    249249                    if entry.Description : 
    250250                        msg += " - %s" % entry.Description 
    251                     print msg     
     251                    print msg 
    252252                    print "    %s" % (_("Limited by : %s") % entry.LimitBy) 
    253253                    print "    %s" % (_("Group balance : %.2f") % (entry.AccountBalance or 0.0)) 
    254254                    print "    %s" % (_("Total paid so far : %.2f") % (entry.LifeTimePaid or 0.0)) 
    255255                    print 
    256         elif options["delete"] :     
     256        elif options["delete"] : 
    257257            percent.display("\n%s..." % _("Deletion")) 
    258258            getattr(self.storage, "deleteMany%ss" % suffix)(entries) 
     
    266266                                            'noprint', 'nochange') : 
    267267                    raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] 
    268                 if (limitby in ('nochange', 'noprint')) and options["groups"] :     
     268                if (limitby in ('nochange', 'noprint')) and options["groups"] : 
    269269                    raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] 
    270                  
     270 
    271271            overcharge = options["overcharge"] 
    272272            if overcharge : 
    273273                try : 
    274274                    overcharge = float(overcharge.strip()) 
    275                 except (ValueError, AttributeError) :     
     275                except (ValueError, AttributeError) : 
    276276                    raise PyKotaCommandLineError, _("Invalid overcharge value %s") % options["overcharge"] 
    277                      
     277 
    278278            balance = options["balance"] 
    279279            if balance : 
     
    281281                try : 
    282282                    balancevalue = float(balance) 
    283                 except ValueError :     
     283                except ValueError : 
    284284                    raise PyKotaCommandLineError, _("Invalid balance value %s") % options["balance"] 
    285             else :     
     285            else : 
    286286                balancevalue = None 
    287                  
     287 
    288288            if options["ingroups"] : 
    289289                usersgroups = self.storage.getMatchingGroups(options["ingroups"]) 
    290290                if not usersgroups : 
    291291                    raise PyKotaCommandLineError, _("There's no users group matching %s") % " ".join(options["ingroups"].split(',')) 
    292             else :          
     292            else : 
    293293                usersgroups = [] 
    294                      
     294 
    295295            description = options["description"] 
    296296            if description : 
    297297                description = description.strip() 
    298                  
     298 
    299299            comment = options["comment"] 
    300300            if comment : 
    301301                comment = comment.strip() 
    302             email = options["email"]     
     302            email = options["email"] 
    303303            if email : 
    304304                email = email.strip() 
    305             skipexisting = options["skipexisting"]     
     305            skipexisting = options["skipexisting"] 
    306306            groups = options["groups"] 
    307307            remove = options["remove"] 
    308308            self.storage.beginTransaction() 
    309             try :     
    310                 if options["add"] :     
    311                     rejectunknown = self.config.getRejectUnknown()     
     309            try : 
     310                if options["add"] : 
     311                    rejectunknown = self.config.getRejectUnknown() 
    312312                    percent.display("%s...\n" % _("Creation")) 
    313313                    percent.setSize(len(names)) 
     
    325325                                    try : 
    326326                                        grp.getgrnam(ename) 
    327                                     except KeyError :     
     327                                    except KeyError : 
    328328                                        self.printInfo(_("Unknown group %s") % ename, "error") 
    329329                                        reject = 1 
    330                                 else :     
     330                                else : 
    331331                                    try : 
    332332                                        pwd.getpwnam(ename) 
    333                                     except KeyError :     
     333                                    except KeyError : 
    334334                                        self.printInfo(_("Unknown user %s") % ename, "error") 
    335335                                        reject = 1 
    336                             if not reject :         
     336                            if not reject : 
    337337                                entry = globals()["Storage%s" % suffix](self.storage, ename) 
    338338                                if groups : 
    339339                                    self.modifyEntry(entry, groups, limitby, \ 
    340340                                                     description) 
    341                                 else :     
     341                                else : 
    342342                                    self.modifyEntry(entry, groups, limitby, \ 
    343343                                                     description, overcharge,\ 
     
    348348                                    if skipexisting : 
    349349                                        self.logdebug(_("%s %s already exists, skipping.") % (_(suffix), ename)) 
    350                                     else :     
     350                                    else : 
    351351                                        self.logdebug(_("%s %s already exists, will be modified.") % (_(suffix), ename)) 
    352352                                        if groups : 
     
    373373                        if groups : 
    374374                            self.modifyEntry(entry, groups, limitby, description) 
    375                         else :     
     375                        else : 
    376376                            self.modifyEntry(entry, groups, limitby, description, \ 
    377377                                             overcharge, balance, balancevalue, \ 
    378378                                             comment, email) 
    379                             self.manageUsersGroups(usersgroups, entry, remove)                 
    380                         entry.save()     
     379                            self.manageUsersGroups(usersgroups, entry, remove) 
     380                        entry.save() 
    381381                        percent.oneMore() 
    382             except :                     
     382            except : 
    383383                self.storage.rollbackTransaction() 
    384384                raise 
    385             else :     
     385            else : 
    386386                self.storage.commitTransaction() 
    387                  
     387 
    388388        if not options["list"] : 
    389389            percent.done() 
    390                       
    391 if __name__ == "__main__" :  
     390 
     391if __name__ == "__main__" : 
    392392    retcode = 0 
    393393    try : 
     
    401401                        "ingroups=", "limitby=", "balance=", "comment=", \ 
    402402                       ] 
    403                          
    404          
     403 
     404 
    405405        # Initializes the command line tool 
    406406        manager = PKUsers(doc=__doc__) 
    407407        manager.deferredInit() 
    408          
     408 
    409409        # parse and checks the command line 
    410410        (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) 
    411          
     411 
    412412        # sets long options 
    413413        options["help"] = options["h"] or options["help"] 
     
    415415        options["add"] = options["a"] or options["add"] 
    416416        options["description"] = options["D"] or options["description"] 
    417         options["delete"] = options["d"] or options["delete"]  
     417        options["delete"] = options["d"] or options["delete"] 
    418418        options["groups"] = options["g"] or options["groups"] 
    419419        options["list"] = options["L"] or options["list"] 
     
    421421        options["skipexisting"] = options["s"] or options["skipexisting"] 
    422422        options["limitby"] = options["l"] or options["limitby"] 
    423         options["balance"] = options["b"] or options["balance"]  
     423        options["balance"] = options["b"] or options["balance"] 
    424424        options["ingroups"] = options["i"] or options["ingroups"] 
    425425        options["overcharge"] = options["o"] or options["overcharge"] 
    426426        options["comment"] = options["C"] or options["comment"] or defaults["comment"] 
    427427        options["email"] = options["e"] or options["email"] 
    428          
     428 
    429429        if options["help"] : 
    430430            manager.display_usage_and_quit() 
     
    436436           or (options["groups"] and (options["balance"] or options["ingroups"] or options["overcharge"])) : 
    437437            raise PyKotaCommandLineError, _("incompatible options, see help.") 
    438         elif options["remove"] and not options["ingroups"] :     
     438        elif options["remove"] and not options["ingroups"] : 
    439439            raise PyKotaCommandLineError, _("You have to pass user groups names on the command line") 
    440440        elif (not args) and (options["add"] or options["delete"]) : 
     
    442442        else : 
    443443            retcode = manager.main(args, options) 
    444     except KeyboardInterrupt :         
     444    except KeyboardInterrupt : 
    445445        logerr("\nInterrupted with Ctrl+C !\n") 
    446446        retcode = -3 
    447     except PyKotaCommandLineError, msg :     
     447    except PyKotaCommandLineError, msg : 
    448448        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    449449        retcode = -2 
    450     except SystemExit :         
     450    except SystemExit : 
    451451        pass 
    452452    except : 
    453453        try : 
    454454            manager.crashed("pkusers failed") 
    455         except :     
     455        except : 
    456456            crashed("pkusers failed") 
    457457        retcode = -1 
     
    459459    try : 
    460460        manager.storage.close() 
    461     except (TypeError, NameError, AttributeError) :     
     461    except (TypeError, NameError, AttributeError) : 
    462462        pass 
    463          
    464     sys.exit(retcode)     
     463 
     464    sys.exit(retcode)