Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added size and mtime properties to item #13

Closed
wants to merge 10 commits into from
Closed
84 changes: 81 additions & 3 deletions pyseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,17 @@ def __init__(self, item):
log.debug('adding %s' % item)
self.item = item
self.__path = getattr(item, 'path', os.path.abspath(str(item)))
self.__dirname = os.path.dirname(str(item))
self.__dirname = os.path.dirname(self.__path)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe someone understands why, but dirname was missing on the last item when using str(item), now when using self.__path it is always present...
and wasnot there a dirname on the sequence object too ?

self.__filename = os.path.basename(str(item))
self.__digits = gDigitsRE.findall(self.name)
self.__parts = gDigitsRE.split(self.name)


if os.path.isfile(self.__path):
self.__size = os.path.getsize(self.__path)
self.__mtime = os.path.getmtime(self.__path)
else:
self.__size = 0
self.__mtime = 0
# modified by self.is_sibling()
self.frame = ''
self.head = self.name
Expand Down Expand Up @@ -135,6 +141,18 @@ def parts(self):
"""
return self.__parts

@property
def size(self):
"""filesize of item
"""
return self.__size

@property
def mtime(self):
"""modification time of item
"""
return self.__mtime

def isSibling(self, item):
"""Determines if this and item are part of the same sequence.

Expand Down Expand Up @@ -193,6 +211,7 @@ def __init__(self, items):
"""
super(Sequence, self).__init__([Item(items.pop(0))])
self.__missing = []
self.__NEEDREINDEX = False
while items:
f = Item(items.pop(0))
try:
Expand All @@ -204,6 +223,8 @@ def __init__(self, items):
except KeyboardInterrupt:
log.info("Stopping.")
break



def __attrs__(self):
"""Replaces format directives with values"""
Expand Down Expand Up @@ -294,7 +315,7 @@ def length(self):

def frames(self):
""":return: List of files in sequence."""
if not hasattr(self, '__frames') or not self.__frames:
if not hasattr(self, '__frames') or not self.__frames or self.__NEEDREINDEX:
self.__frames = list(map(int, self._get_frames()))
self.__frames.sort()
return self.__frames
Expand Down Expand Up @@ -405,6 +426,63 @@ def append(self, item):
self.__missing = None
else:
raise SequenceError('Item is not a member of this sequence')

@property
def mtime(self):
"""
returns the latest mtime of all items
"""
maxDate = list()
for i in self:
maxDate.append(i.mtime)
return max(maxDate)

@property
def size(self):
"""
returns the size all items
divide the result by 1024/1024 to get megabytes
"""
tempSize = list()
for i in self:
tempSize.append(i.size)
return sum(tempSize)

def reIndex(self, addSub, padding=None):
"""
renames the files on disk
reIndex the items in the sequence object egg
1001 + 100 = 1101
can change the padding too
@param addSub int
@param padding str
@param run boolean
"""
if not padding:
padding = self.format("%p")

if addSub > 0:
gen = ((image,frame) for (image,frame) in zip(reversed(self),reversed(self.frames())))
else:
gen = ((image,frame) for (image,frame) in zip(self,self.frames()))

for image,frame in gen:
oldName = image.path
newFrame = padding % (frame + addSub)
newFileName ="%s%s%s" % (self.format("%h"), newFrame , self.format("%t") )
newName = os.path.join(image.dirname, newFileName)
try:
import shutil
shutil.move(oldName,newName)
except Exception,e:
log.error(e)
else:
log.info('renaming %s -->' % oldName)
log.info(' %s' % newName)
self.__NEEDREINDEX = True
image.frame = newFrame
else:
self.frames()

def _get_padding(self):
""":return: padding string, e.g. %07d"""
Expand Down