Skip to content

Commit

Permalink
Add option to setPadStyle to convert zfill (refs #116)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfx committed Jun 12, 2022
1 parent a0c6bdc commit fbd2abd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/fileseq/filesequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,28 @@ def padStyle(self):
"""
return self._pad_style

def setPadStyle(self, pad_style):
def setPadStyle(self, pad_style, set_zfill=False):
"""
Set new padding style for the sequence.
See fileseq.constants.PAD_STYLE_HASH1 and fileseq.constants.PAD_STYLE_HASH4
The default behavior converts only the padding characters representation per the new style,
the same zfill/decimalPlaces value. If ``set_zfill=True``, convert the zfill/decimalPlaces
values to match the meaning of the padding characters per the new style.
Args:
pad_style (`PAD_STYLE_DEFAULT` or `PAD_STYLE_HASH1` or `PAD_STYLE_HASH4`): padding style to set
set_zfill (bool): If True, convert zfill/decimalPlaces value instead of padding chars
"""
if set_zfill:
zfill = self.getPaddingNum(self._frame_pad, pad_style=pad_style)
decimal_places = self.getPaddingNum(self._subframe_pad, pad_style=pad_style)

self._pad_style = pad_style
self._zfill = zfill
self._decimal_places = decimal_places
return

decimal_places = self._decimal_places
frame_pad = self.getPaddingChars(self._zfill, pad_style=pad_style)
if decimal_places:
Expand Down
62 changes: 61 additions & 1 deletion test/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
# For testing compatibility with pickle values from older version of fileseq
PICKLE_TEST_SEQ = "/path/to/file.1-100x2#.exr"
OLD_PICKLE_MAP = {
'1.10.0': b'\x80\x02cfileseq.filesequence\nFileSequence\nq\x01)\x81q\x02}q\x03(U\x04_extq\x04U\x04.exrU\t_frameSetq\x05cfileseq.frameset\nFrameSet\nq\x06)\x81q\x07U\x071-100x2q\x08\x85bU\x04_dirq\tU\t/path/to/U\x04_padq\nU\x01#U\x05_baseq\x0bU\x05file.U\x06_zfillq\x0cK\x04ub.'
'1.10.0': b'\x80\x02cfileseq.filesequence\nFileSequence\nq\x01)\x81q\x02}q\x03(U\x04_extq\x04U\x04.exrU'
b'\t_frameSetq\x05cfileseq.frameset\nFrameSet\nq\x06)\x81q\x07U\x071-100x2q\x08\x85bU\x04_dirq'
b'\tU\t/path/to/U\x04_padq\nU\x01#U\x05_baseq\x0bU\x05file.U\x06_zfillq\x0cK\x04ub.'
}


Expand Down Expand Up @@ -799,6 +801,64 @@ def testSetPadding(self):
seq.setSubframePadding("bad")
self.assertEquals(expect, str(seq))

def testSetPadStyle(self):
Case = namedtuple('Case', ['input', 'to_style', 'expected_pad'])
H1 = fileseq.PAD_STYLE_HASH1
H4 = fileseq.PAD_STYLE_HASH4
FS = FileSequence

table = [
Case(FS('[email protected]', H4), H1, '#'),
Case(FS('foo.1@@@@.exr', H4), H1, '####'),
Case(FS('foo.1#.exr', H4), H1, '####'),
Case(FS('foo.1###@.exr', H4), H1, '#############'),
Case(FS('foo.1-5x0.25#.####.exr', H4, allow_subframes=True), H1, '####.################'),
Case(FS('foo.1-5x0.25###@.#.exr', H4, allow_subframes=True), H1, '#############.####'),

Case(FS('[email protected]', H1), H4, '@'),
Case(FS('foo.1@@@@.exr', H1), H4, '#'),
Case(FS('foo.1#.exr', H1), H4, '@'),
Case(FS('foo.1####.exr', H1), H4, '#'),
Case(FS('foo.1-5x0.25#.####.exr', H1, allow_subframes=True), H4, '@.#'),
Case(FS('foo.1-5x0.25####.#.exr', H1, allow_subframes=True), H4, '#.@'),
]

for case in table:
actual = case.input.copy()
actual.setPadStyle(case.to_style)
self.assertEqual(case.input.zfill(), actual.zfill(), msg=str(case))
self.assertEqual(case.input.decimalPlaces(), actual.decimalPlaces(), msg=str(case))
self.assertEqual(case.expected_pad, actual.padding(), msg=str(case))

def testSetPadStyleSetZfill(self):
Case = namedtuple('Case', ['input', 'start_zfill', 'to_style', 'expected_zfill'])
H1 = fileseq.PAD_STYLE_HASH1
H4 = fileseq.PAD_STYLE_HASH4
FS = FileSequence

table = [
Case(FS('[email protected]', H4), 1, H1, 1),
Case(FS('foo.1@@@@.exr', H4), 4, H1, 4),
Case(FS('foo.1#.exr', H4), 4, H1, 1),
Case(FS('foo.1####.exr', H4), 16, H1, 4),
Case(FS('foo.1-5x0.25#.####.exr', H4, allow_subframes=True), 4, H1, 1),
Case(FS('foo.1-5x0.25####.#.exr', H4, allow_subframes=True), 16, H1, 4),

Case(FS('[email protected]', H1), 1, H4, 1),
Case(FS('foo.1@@@@.exr', H1), 4, H4, 4),
Case(FS('foo.1#.exr', H1), 1, H4, 4),
Case(FS('foo.1####.exr', H1), 4, H4, 16),
Case(FS('foo.1-5x0.25#.####.exr', H1, allow_subframes=True), 1, H4, 4),
Case(FS('foo.1-5x0.25####.#.exr', H1, allow_subframes=True), 4, H4, 16),
]

for case in table:
self.assertEqual(case.start_zfill, case.input.zfill(), msg=str(case))
actual = case.input.copy()
actual.setPadStyle(case.to_style, set_zfill=True)
self.assertEqual(case.input.padding(), actual.padding(), msg=str(case))
self.assertEqual(case.expected_zfill, actual.zfill(), msg=str(case))

def testSetFrameSet(self):
seq = FileSequence("/cheech/chong.1-5#.exr")
seq.setFrameSet(FrameSet("10-20"))
Expand Down

0 comments on commit fbd2abd

Please sign in to comment.