Uit Hack42
Ga naar: navigatie, zoeken
k (E_FORMATTNG)
k (Added reverse sorting)
Regel 87: Regel 87:
 
                 debug('Ignoring: %s' % file)
 
                 debug('Ignoring: %s' % file)
 
                 pass
 
                 pass
 +
 +
    def sort(self, reverse=False):
 +
      return sorted(self, key=lambda file: file[0], reverse=reverse)
  
 
if __name__ == "__main__":
 
if __name__ == "__main__":
Regel 92: Regel 95:
 
     verbose = False
 
     verbose = False
 
     listOnly = False
 
     listOnly = False
     optlist, args = getopt.getopt(sys.argv[1:], 'lhv')
+
    reverse = False
 +
     optlist, args = getopt.getopt(sys.argv[1:], 'lthv')
 
     for option, value in optlist:
 
     for option, value in optlist:
 
         if(option == '-v'):
 
         if(option == '-v'):
Regel 98: Regel 102:
 
         if(option == '-l'):
 
         if(option == '-l'):
 
             listOnly = True
 
             listOnly = True
 +
        if(option == '-t'):
 +
            reverse = True
 
         if(option == '-h'):
 
         if(option == '-h'):
 
             usage()
 
             usage()
Regel 104: Regel 110:
 
     for item in args:
 
     for item in args:
 
         debug('Processing: %s' % item)
 
         debug('Processing: %s' % item)
 +
        myList = FileList(os.path.dirname(item), os.path.basename(item))
 
         if listOnly:
 
         if listOnly:
             for file in sorted(FileList(os.path.dirname(item), os.path.basename(item)), key=lambda file: file[0]):
+
             for file in myList.sort(reverse):
 
                 print file[3]
 
                 print file[3]
 
         else:
 
         else:
             for file in sorted(FileList(os.path.dirname(item), os.path.basename(item)), key=lambda file: file[0], reverse=True):
+
             for file in myList.sort(not reverse):
                 debug('renaming %s to %s.' % (file[3], "%s%s" % (file[2], dec2rom(file[0]+1))))
+
                if reverse:
                os.rename(file[3], "%s%s" % (file[2], dec2rom(file[0]+1)))
+
                    debug('renaming %s to %s.' % (file[3], "%s%s" % (file[2], dec2rom(file[0]-1))))
 
+
                    os.rename(file[3], "%s%s" % (file[2], dec2rom(file[0]-1)))
 +
                 else:
 +
                    debug('renaming %s to %s.' % (file[3], "%s%s" % (file[2], dec2rom(file[0]+1))))
 +
                    os.rename(file[3], "%s%s" % (file[2], dec2rom(file[0]+1)))
 
</pre>
 
</pre>

Versie van 3 sep 2013 om 00:01

Project: RomanRotate
Schroefje24.png
Schroefje24.png
Schroefje24.png
Schroefje24.png
RomanRotate Picture.jpg

RomanRotate

Naam RomanRotate
Door Stoneshop, Dvanzuijlekom, WitchDoc
Status AF
Madskillz Madness
Doel / Omschrijving
Roman numbering log rotation
Alle Projecten - Project Toevoegen
File:RomanRotate_Picture.jpg noez

Small bit of python code to provide Roman numbered log rotation

#!/usr/bin/env python

import sys
import os
import re
import getopt

def debug(argv):
    global verbose
    if verbose:
        print >>sys.stderr, "DBG: %s" % argv

def dec2rom(input):
   """
   Convert a decimal to Roman numerals.
   """
   assert(isinstance(input, int)), "expected integer, got %s" % type(input)
   assert(0 < input < 4000), "Argument must be between 1 and 3999"   
   ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
   nums = ('M',  'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
   result = ""
   for i in range(len(ints)):
      count = int(input / ints[i])
      result += nums[i] * count
      input -= ints[i] * count
   return result


def rom2dec(input):
   """
   Convert a roman numeral to a decimal.
   """
   assert(isinstance(input, basestring)), "expected string, got %s" % type(input)
   assert(re.match(r'^[MDCLXVI]+$', input)), "not a valid roman numeral: %s" % input
   input = input.upper()
   nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
   ints = [1000, 500, 100, 50,  10,  5,   1]
   retValue = 0
   for i in range(len(input)):
      value = ints[nums.index(input[i])]
      # If the next place holds a larger number, this value is negative.
      try:
         if ints[nums.index(input[i +1])] > value:
            value *= -1
      except IndexError:
         # there is no next place.
         pass
      retValue += value
   # Easiest test for validity...
   if dec2rom(retValue) == input:
      return retValue
   else:
      raise ValueError, 'input is not a valid roman numeral: %s' % input

def keys(value):
    if re.match(r'([MDCLXVI]+)', value):
        debug("Roman: %s, Decimal %s" % (value, rom2dec(value)))
        return((rom2dec(value), value))
    else:
        return((value, dec2rom(value)))

class FileList(set):
    def __init__(self, directory, filename):
        if(directory!=''):
            self._directory = directory
        else:
            self._directory = '.'
        self._filename = filename
        debug('Getting files for %s in directory %s' % (self._filename, self._directory))
        for file in os.listdir(self._directory):
            try:
                parts = re.split(r'(%s\.)([MCXDVIL]+)' % self._filename, file)
                if len(parts) > 1:
                    debug('Adding: %s' % file)
                    self.add(keys(parts[2]) + (parts[1], file,))
            except Exception, err:
                debug('Ignoring: %s' % file)
                pass

    def sort(self, reverse=False):
       return sorted(self, key=lambda file: file[0], reverse=reverse)

if __name__ == "__main__":
    global verbose
    verbose = False
    listOnly = False
    reverse = False
    optlist, args = getopt.getopt(sys.argv[1:], 'lthv')
    for option, value in optlist:
        if(option == '-v'):
            verbose = True
        if(option == '-l'):
            listOnly = True
        if(option == '-t'):
            reverse = True
        if(option == '-h'):
            usage()
    debug('Verbose is on')
    debug('Filenames: %s' % args)
    for item in args:
        debug('Processing: %s' % item)
        myList = FileList(os.path.dirname(item), os.path.basename(item))
        if listOnly:
            for file in myList.sort(reverse):
                print file[3]
        else:
            for file in myList.sort(not reverse):
                if reverse:
                    debug('renaming %s to %s.' % (file[3], "%s%s" % (file[2], dec2rom(file[0]-1))))
                    os.rename(file[3], "%s%s" % (file[2], dec2rom(file[0]-1)))
                else:
                    debug('renaming %s to %s.' % (file[3], "%s%s" % (file[2], dec2rom(file[0]+1))))
                    os.rename(file[3], "%s%s" % (file[2], dec2rom(file[0]+1)))