Example #1
0
 def _reset(self):
     env.sos_dict = WorkflowDict()
     SoS_exec('from sos.runtime import *', None)
     env.sos_dict.set('__interactive__', True)
     self.original_keys = set(env.sos_dict._dict.keys())
     self.original_keys.add('__builtins__')
     self.options = ''
Example #2
0
 def testPatternMatch(self):
     '''Test snake match's pattern match facility'''
     res = extract_pattern('{a}-{b}.txt', ['file-1.txt', 'file-ab.txt'])
     self.assertEqual(res['a'], ['file', 'file'])
     self.assertEqual(res['b'], ['1', 'ab'])
     res = extract_pattern('{a}-{b}.txt', ['file--ab--cd.txt'])
     self.assertEqual(res['a'], ['file--ab-'])
     self.assertEqual(res['b'], ['cd'])
     res = extract_pattern('{path}/{to}/{file}.txt', ['/tmp//1.txt'])
     self.assertEqual(res['path'], [None])
     self.assertEqual(res['to'], [None])
     self.assertEqual(res['file'], [None])
     res = extract_pattern('{path}/{to}/{file}.txt',
                           ['/tmp/test/1.txt.txt'])
     self.assertEqual(res['path'], ['/tmp'])
     self.assertEqual(res['to'], ['test'])
     self.assertEqual(res['file'], ['1.txt'])
     # expand_pattern
     env.sos_dict = WorkflowDict({
         'os': os,
         'a': 100,
         'b': 'file name',
         'c': ['file1', 'file2', 'file 3'],
         'd': {'a': 'file1', 'b': 'file2'},
     })
     self.assertEqual(expand_pattern('{b}.txt'), ['file name.txt'])
     self.assertEqual(expand_pattern('{c}.txt'), [
                      'file1.txt', 'file2.txt', 'file 3.txt'])
     self.assertEqual(expand_pattern('{a}_{c}.txt'), [
                      '100_file1.txt', '100_file2.txt', '100_file 3.txt'])
Example #3
0
 def testEval(self):
     '''Test the evaluation of SoS expression'''
     env.sos_dict = WorkflowDict({
         'interpolate': interpolate,
         'a': 100,
         'b': 'file name',
         'c': ['file1', 'file2', 'file 3'],
         'd': {'a': 'file1', 'b':'file2'},
     })
     for expression, result in [
         ('''"This is ${a+100}"''', 'This is 200'),
         ('''"${a+100}" + "${a//100}"''', "2001"),
         ('''"${c[1]}"''', 'file2'),
         ('''"${c[1:]}"''', 'file2 file 3'),
         ('''"${d}"''', ['a b', 'b a']),
         ('''"${d}"*2''', ['a ba b', 'b ab a']),
         ('''"${d['a']}"''', 'file1'),
         ('''"${b!q}"''', "'file name'"),
         ('''"${b!r}"''', "'file name'"),
         ('''"${c!q}"''', "file1 file2 'file 3'"),
         ]:
         if isinstance(result, str):
             self.assertEqual(SoS_eval(expression, '${ }'), result)
         else:
             self.assertTrue(SoS_eval(expression, '${ }') in result)
     #
     # interpolation will only happen in string
     self.assertRaises(SyntaxError, SoS_eval, '''${a}''', '${ }')
Example #4
0
 def testWorkflowDict(self):
     '''Test workflow dict with attribute access'''
     env.reset()
     d = WorkflowDict()
     d['a'] = 1
     self.assertEqual(d['a'], 1)
     #
     d['a'] += 1
     self.assertEqual(d['a'], 2)
Example #5
0
 def test_on_demand_options(self):
     '''Test options that are evaluated upon request.'''
     options = on_demand_options({'a': '"est"', 'b': 'c', 'c': 'e + 2'})
     env.sos_dict = WorkflowDict({
         'e': 10,
     })
     self.assertEqual(options['a'], 'est')
     self.assertRaises(KeyError, options.__getitem__, 'd')
     self.assertRaises(ValueError, options.__getitem__, 'b')
     self.assertEqual(options['c'], 12)
     #
     env.sos_dict.set('e', 20)
     self.assertEqual(options['c'], 22)
     env.sos_dict.set('c', 200)
     self.assertEqual(options['b'], 200)
Example #6
0
 def testInterpolation(self):
     '''Test string interpolation'''
     env.sos_dict = WorkflowDict({
         'os': os,
         'a': 100,
         'b': 20,
         'c': ['file1', 'file2', 'file3'],
         'd': {'a': 'file1', 'b':'file2'},
         'e': set([1, 'a']),
         'var1': 1/2.,
         'var2': [1, 2, 3.1],
         'file': 'a/b.txt',
         'files2': ['a/b.txt', 'c.d.txt'],
         'file_ws': ['d i r/f .txt']
         })
     for sigil in ('${ }', '{ }', '[ ]', '%( )', '[[ ]]', '%( )s', '# #', '` `'):
         l, r = sigil.split(' ')
         for expr, result, nested, exclude in [
             ('{0}1{1}', '1', False, []),
             ('{0}a{1}', '100', False, []),
             ('{0}a+b{1}', '120', False, []),
             ('{0}a+b*5{1}', '200', False, []),
             ('{0}a+b*5{1} is 200', '200 is 200', False, []),
             ('{0}a+b*5{1} and {0}a{1}', '200 and 100', False, []),
             ('Pre {0}a+b*5{1} and {0}a{1} after', 'Pre 200 and 100 after', False, []),
             ('Nested {0}a+b*{0}b//2{1}{1}', 'Nested 300', True, []),
             ('Format {0}a:.5f{1}', 'Format 100.00000', False, []),
             ('{0}var2[:2]{1}', '1 2', False, []),
             ('{0}var2[1:]{1}', '2 3.1', False, []),
             # nested
             ('Nested {0}a:.{0}4+1{1}f{1}', 'Nested 100.00000', True, []),
             # deep nested
             ('Triple Nested {0}a:.{0}4+{0}5//5{1}{1}f{1}', 'Triple Nested 100.00000', True, []),
             # nested invalid
             ('Nested invalid {0}"{0}a-{1}"{1}', 'Nested invalid {}a-{}'.format(l, r), True, []),
             ('Nested valid {0}"{0}a{1}-"{1}', 'Nested valid 100-', True, []),
             #
             ('Dict {0}d{1}', ['Dict a b', 'Dict b a'], False, []),
             ('set {0}e{1}', ['set 1 a', 'set a 1'], False, []),
             ('Fmt {0}var1:.2f{1}', 'Fmt 0.50', False, []),
             ('Fmt {0}var2:.2f{1}', 'Fmt 1.00 2.00 3.10', False, []),
             ('LC {0}[x*2 for x in var2]{1}', 'LC 2 4 6.2', False, []),
             ('LC {0}[x*2 for x in var2]:.2f{1}', 'LC 2.00 4.00 6.20', False, []),
             #
             # [['a':'b', 'c':'d']['a']] works because
             #     ['a':'b', 'c':'d']a
             # is invalid so SoS does not consider ['a'] as nested expression
             #
             ('Newline {0}{{"a": "b", \n"c": "d"}}["a"]{1}', 'Newline b', False, []),
             #
             # string literal within interpolated expression
             ('Literal {0}"{0} {1}"{1}', 'Literal ' + sigil, True, []),
             # this case does not work because {} would become '' with sigil {}
             ("{0}' {{}} '.format(a){1}", ' 100 ', False, ['{ }']),
             #
             ("{0}os.path.basename(file){1}", 'b.txt', False, []),
             ('{0}os.path.basename(file_ws[0]){1}', 'f .txt', False, []),
             #
             # ! conversion
             ('{0}file!r{1}', "'a/b.txt'", False, []),
             ('{0}file!s{1}', "a/b.txt", False, []),
             ('''{0}"a'b"!r{1}''', '"a\'b"', False, []),
             ('''{0}'a"b'!r{1}''', "'a\"b'", False, []),
             #
             # !q conversion (added by SoS)
             ('{0}file_ws[0]!q{1}', "'d i r/f .txt'", False, []),
             #
             # !, joined by ,
             ('{0}var2!r,{1}', "1, 2, 3.1", False, []),
             ('{0}c!r,{1}', "'file1', 'file2', 'file3'", False, []),
             ('{0}c!,{1}', "file1, file2, file3", False, []),
             #
             ('{0}10000:,{1}', "10,000", False, []),
             #
             # full name by 'a'
             ('{0}"test_utils.py"!a{1}', os.path.abspath('test_utils.py'), False, []),
             ('{0}"a/b/c/test_utils.py"!b{1}', 'test_utils.py', False, []),
             ('{0}"a/b/c/test_utils.py"!d{1}', 'a/b/c', False, []),
             ('{0}"a/b/c/test_utils.py"!dd{1}', 'a/b', False, []),
             ('{0}"a/b/c/test_utils.py"!ddb{1}', 'b', False, []),
             ('{0}"a/b/c/test_utils.py"!n{1}', 'a/b/c/test_utils', False, []),
             ('{0}"a/b/c/test_utils.py"!bn{1}', 'test_utils', False, []),
             ('{0}"~/test_utils.py"!a{1}', os.path.expanduser('~/test_utils.py'), False, []),
             ('{0}"~/test_utils.py"!e{1}', os.path.expanduser('~/test_utils.py'), False, []),
             ('{0}"test/test_utils.py"!b{1}', "test_utils.py", False, []),
             # preceded by \
             (r'\{0}100{1}', '{0}100{1}'.format(l, r), True, []),
             (r'\{0}100{1} {0}100+4{1}', '{0}100{1} 104'.format(l, r), True, []),
             (r'''{0}{1}''', '', False, []),
         ]:
             if l == r and nested:
                 continue
             if sigil in exclude:
                 continue
             if isinstance(result, str):
                 self.assertEqual(interpolate(expr.format(l, r), sigil=sigil), result, 'Failed to interpolate {} with sigil {}'.format(expr, sigil))
             else:
                 # for cases when the order of output is not guaranteed
                 self.assertTrue(interpolate(expr.format(l, r), sigil=sigil) in result, 'Failed to interpolate {} with sigil {}'.format(expr, sigil))
     #
     # locals should be the one passed to the expression
     self.assertTrue('file_ws' in interpolate('${globals().keys()}', '${ }'))
     # 5:.5.0f does not work.
     self.assertRaises(InterpolationError, interpolate, '${5:.${4/2.}f}', '${ }')