demo.py
changeset 117 8680c2333546
equal deleted inserted replaced
116:165b5d65e1cb 117:8680c2333546
       
     1 #!/usr/bin/env python3
       
     2 
       
     3 from demos.base import setup
       
     4 import sys
       
     5 import os
       
     6 import re
       
     7 
       
     8 
       
     9 def list_demos():
       
    10     return sorted(fname for fname in os.listdir('demos')
       
    11                   if re.match(r'[0-9]+_.*\.py', fname))
       
    12 
       
    13 
       
    14 def print_usage(names):
       
    15     print("Usage: %s <name>\n" % sys.argv[0])
       
    16     print("Where <name> is one of (or part of):")
       
    17     print('\n'.join(['    '+name for name in names]))
       
    18 
       
    19 
       
    20 if __name__ == '__main__':
       
    21     setup()
       
    22     names = list_demos()
       
    23 
       
    24     if len(sys.argv) != 2:
       
    25         print_usage(names)
       
    26         sys.exit(0)
       
    27 
       
    28     arg = sys.argv[1]
       
    29     name = None
       
    30     for name in names:
       
    31         if arg in name:
       
    32             break
       
    33     else:
       
    34         print("Demo '%s' not found." % arg)
       
    35         sys.exit(1)
       
    36 
       
    37     print("Executing %s..." % name)
       
    38     with open('demos/' + name, 'r') as f:
       
    39         exec(f.read())