def compare_by_template(a, b, template=None): """ Compares a and b as strings, first for equality, than using the template list. The one that comes first in the list is smaller. If no template list is given, or no match is achieved, string comparision is returned. Note that per default string comparision is case sensitive. If a == b, returns 0 If a > b, returns 1 If a < b, returns -1 Examples: >>> compare_by_template(4, 2, [1, 2, 3, 4, 5]) # 4 is bigger than 2 in the ordered template 1 >>> compare_by_template(2, 4, [5, 4, 3, 2, 1]) # 2 is bigger than 4 in the reversed template 1 >>> compare_by_template('a', 'a', ['whatever']) # identical 0 >>> compare_by_template('a', 'b', [1, 2, 3]) # not found in template: string comparision -1 >>> compare_by_template('layout', 'animation', ['layout', 'animation', 'export', 'render']) # 'animation' comes later (is bigger) -1 >>> compare_by_template('layout', 'animation') # by string comparision 'animation' comes first (is smaller) 1 >>> compare_by_template('layout', 'x-nonsense', ['layout', 'animation', 'render']) # partially incompatible (one is not in list): string comparision -1 :param a: string to compare :param b: string to compare :param template: list of strings :return: """ if not a and b: return 0 # debug('start {} - {}'.format(a, b)) a = unicode(a) b = unicode(b) if a == b: return 0 if not (a and b): return 1 if a > b else -1 if template: template = [str(i) for i in template] if not set([a, b]) <= set(template): info('Not all of "{}" and "{}" are included in template "{}". Returning string order.'.format(a, b, str(template))) template = [] for item in template: if item == a: return -1 if item == b: return 1 if template: info('Unable to order "{}" vs "{}" (using "{}"). Returning string order.'.format(a, b, template)) return 1 if a > b else -1
def path(self): """ return the sid as a path """ result = None try: result = dict_to_path(self.asdict()) except SpilException as e: info('This Sid has no path. ({})'.format(e)) return result
def parent(self, set_default=False): """ Returns the parent sid. Does not return the parent value, but the full parent Sid. Sets defaults if set_default is True. Useful because the parent may have its last key empty, which might be unexpected. Example: Sid('demo|s|010|120|animation').parent() # Will return the Shot Sid 'demo|s|010|120' sid.parent() Uses get_as(). :param set_default: :return: """ if len(self) <= 1: info('Sid {} has no parent. Copy is returned.'.format(self)) return self.copy() parent_key = self.data.keys()[len(self) - 2] if set_default: self.set_defaults(parent_key) return self.get_as(parent_key)
def by_path(tests): for test in tests: info('Testing : {}'.format(test)) info('') _type, _dict = path_to_dict(test) if _type: info('Match!') else: info('No match.') continue info('Resolved basetype : {}'.format(_type)) info('Resolved dict : {}'.format(_dict)) info('') if _type in ['shot_version']: # FIXME : explicit conf info( 'This type cannot be resolved to a path : {}. Skipped'.format( _type)) continue retour = dict_to_path(_dict) info('Retour path : {}'.format(retour)) assert (test == retour) info('') info('*' * 30) info('')
def by_sids(tests): for test in tests: info('Testing : {}'.format(test)) info('') test = Sid(test) _dict = test.asdict() _type = test.sidtype() info('Initial sid dict : {}'.format(_dict)) info('Initial sid basetype : {}'.format(_type)) path = None try: path = dict_to_path(_dict) info('Resolved to path : {}'.format(path)) except SpilException as e: info('Unable to resolve path because : {}'.format(e)) test = test.parent(set_default=True) # we try to level one up if test: info('Sid up one level (parent)...') warn('Sid is now : {}'.format(test)) _dict = test.asdict() _type = test.sidtype() info('Sid dict is now : {}'.format(_dict)) path = dict_to_path( test.asdict()) # do not catch error if this doesnt work info('Resolved to path : {}'.format(path)) _type, retour = path_to_dict(path) if _type: info('Retour resolved') else: info('No match.') continue info('Resolved retour dict : {}'.format(retour)) info('') # assert (_dict == retour) # dicts do not need to be identical. retour = Sid(data=retour) info('Retour sid : {}'.format(retour)) info('') warn('test ' + str(test)) warn('test ' + str(retour)) assert (test == retour) info('') info('*' * 30) info('')
retour = Sid(data=retour) info('Retour sid : {}'.format(retour)) info('') warn('test ' + str(test)) warn('test ' + str(retour)) assert (test == retour) info('') info('*' * 30) info('') if __name__ == '__main__': info('') info('*' * 30) info('') from spil.libs.util.log import setLevel, DEBUG, INFO, info, warn setLevel(INFO) setLevel(DEBUG) # In case of problems, use DEBUG mode info('') info('Tests start') info('') # by paths from spil.conf.fs_conf import test_paths as tests #by_path(tests)
from spil.libs.sid import Sid if __name__ == '__main__': """ This tests Sid -> File This tests gets the test_sids, generates paths and back to sids again. """ from spil.libs.util.log import setLevel, DEBUG, INFO, info info('') info('Tests start') info('') setLevel(INFO) # setLevel(DEBUG) # In case of problems, use DEBUG mode from spil.conf.sid_conf import test_sids as tests for test in tests: info('Testing : {}'.format(test)) info('') sid = Sid(test) if not sid: info('Sid not correct, skipping : {}'.format(test)) continue
template = [str(i) for i in template] if not set([a, b]) <= set(template): info('Not all of "{}" and "{}" are included in template "{}". Returning string order.'.format(a, b, str(template))) template = [] for item in template: if item == a: return -1 if item == b: return 1 if template: info('Unable to order "{}" vs "{}" (using "{}"). Returning string order.'.format(a, b, template)) return 1 if a > b else -1 if __name__ == '__main__': """ Test block. Launches doc test (test in the doc). """ from spil.libs.util.log import setLevel, INFO, ERROR setLevel(INFO) info('Tests start') import doctest # info(doctest) doctest.testmod() info('Tests done.')
data_tmp = data.copy() # self.data = data_tmp self.init_data(data_tmp) # Conform Project for key, value in path_mapping['project'].items(): if key == self.data['project']: self.data['project'] = value # print 'Init done:', self if __name__ == '__main__': import sys sid = Sid('demo/s/s010/p010/fx/main/v001/p/hip') info(sid) mov = sid.get_with(ext='mov') print "SID MOV : ", sid info(mov.path) exr = sid.get_with(ext='exr') exr.set('frame', '*') print "SID EXR : ", exr exr = exr.get_filled('*', True) print "SID EXR : ", exr sys.exit() info('Most tests are in the spil.tests package') from spil.libs.util.log import setLevel, DEBUG, INFO
def get(search_sid): """ Finds Sids based on the given search Sid, using the Glob syntax. Returns a sorted, uniqued list :param search_sid: :return: result list """ search = Sid(search_sid) # filling intermediate values with * last_key = search.last_key() search = search.get_filled(by='*', until=last_key) debug('Search : ' + str(search)) info('PATH : {}'.format(search.path)) path = search.path if not path: warn('Search sid {} did not resolve to a path. Cancelled.'.format( search)) return [] debug('Search path : ' + str(path)) project = Sid(search.get('project')) # TODO need a way to find a root path depending on other sid parts (eg: fx caches) project_path = project.path pattern = path.split(project_path + '/')[-1] for key, value in six.iteritems(search_path_mapping): pattern = pattern.replace(key, value) debug('pattern : ' + str(pattern)) debug('project_path : ' + str(project_path)) if str(pattern) == str(project_path): warn('No valid search pattern') return [] """ found = [] for ext in pattern.split('.')[-1].split(','): new_pattern = pattern.split('.')[0] + '.' + ext found.extend(glob.glob(os.path.join(project_path, new_pattern))) """ found = glob.glob(os.path.join(project_path, pattern)) result = [] for path in found: path = str(path).replace(os.sep, '/') try: sid = Sid(path=path) except SpilException as e: debug('Path did not generate sid : {}'.format(path)) continue if not sid: warn('Path did not generate sid : {}'.format(path)) continue result.append(sid) result = sorted(list(set(result))) return result
from spil.libs.fs.fs import FS from spil.libs.sid import Sid if __name__ == '__main__': from spil.libs.util.log import setLevel, DEBUG, INFO, info info('') info('Search Tests start') info('') setLevel(INFO) # setLevel(DEBUG) # In case of problems, use DEBUG mode from spil.conf.sid_conf import test_sids for test in test_sids[0:]: sid = Sid(test).get_with('subtask', '*') # FIXME info('Sid : {}'.format(sid)) for sid in FS.get_children(sid): # info('{} ({})'.format(sid, sid.sidtype())) info('\t\tChild {}'.format(sid)) search = Sid(test).get_with('task', '*').get_with( 'state', '*').get_with('version', '*').get_with('subtask', '*') if not search: info('Search not matching a sid, skipping : {}'.format(test)) continue
template = lucidity.Template(_type, pattern) if not template: raise SpilException('toe') keys = values_sorted.get(basetype).get('keys') keys = filter(lambda x: x in template.keys(), keys) return keys if __name__ == '__main__': from spil.libs.util.log import setLevel, DEBUG, INFO, info info('Tests start') setLevel(INFO) # setLevel(DEBUG) # In case of problems, use DEBUG mode from spil.conf.sid_conf import test_sids as tests # tests = ['demo/*'] for test in tests: info('Testing : {}'.format(test)) _dict = sid_to_dict(test) info('sid {} ---> \n{}'.format(test, _dict))
from spil.libs.sid import Sid if __name__ == '__main__': """ This tests File -> Sid This tests gets the test_paths, generates sids and back to path again. """ from spil.libs.util.log import setLevel, DEBUG, INFO, info info('') info('Tests start') info('') setLevel(INFO) # setLevel(DEBUG) # In case of problems, use DEBUG mode from spil.conf.fs_conf import test_paths as tests test_paths = [] for test in tests: info('Testing : {}'.format(test)) info('') sid = Sid(path=test) if not sid: info('Not matching, skipping : {}'.format(test))
#setLevel(DEBUG) # In case of problems, use DEBUG mode sid = Sid('demo/s/010/0200/animation/*/001/w/ma') print((sid + 'bla')) sid = Sid('demo/s/010/0200/layout/*/002/p') print(sid) sid = Sid('demo/s/010/0200') print(sid + 'layout' + '*' + '002' + 'p') debug('Tests are in the tests.sid_tests module') sids = test_sids for sid in sids: # sids[-1:]: info('') info('Testing {}'.format(sid)) info('') sid = Sid(sid) if sid.basetype() == 'project': continue if sid.is_shot(): info('Copy until Shot: {}'.format(sid.copy(until='shot'))) info('As shot : {}'.format(sid.get_as('shot'))) info('As state : {}'.format(sid.get_as('state'))) new = sid.copy() new.set_defaults() info('With defaults {}'.format(new)) info('Task : {}'.format(sid.get('task'))) info('Task : {}'.format(sid.task))