pymtt
 All Classes Namespaces Files Functions Variables Groups
LoadClasses.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from builtins import object
3 #!/usr/bin/env python
4 #
5 # Copyright (c) 2015-2018 Intel, Inc. All rights reserved.
6 # $COPYRIGHT$
7 #
8 # Additional copyrights may follow
9 #
10 # $HEADER$
11 #
12 
13 import os
14 import imp
15 import sys
16 import datetime
17 from bisect import *
18 from pathlib import Path
19 
20 class LoadClasses(object):
21  def __init__(self):
22  self.stages = {};
23  self.stageOrder = []
25  self.tools = {};
26  self.utilities = {};
27 
28  def print_name(self):
29  return "LoadClasses"
30 
31  def load(self, directory):
32  # Loop over every python file which has MTT in the
33  # filename in this directory tree
34  for filename in Path(directory).glob("**/*MTT*.py"):
35  # Strip file extension
36  modname = filename.stem
37 
38  # Do this on the stem because it is a string
39  if "Stage" not in modname and "Tool" not in modname and "Utility" not in modname:
40  continue
41 
42  try:
43  # Python 2 requires string cast
44  m = imp.load_source(modname, str(filename))
45  except ImportError:
46  print("ERROR: unable to load " + modname + " from file " + str(filename))
47  exit(1)
48  # add the class to the corresponding category
49  try:
50  cls = getattr(m, modname)
51  a = cls()
52  if "Stage" in modname:
53  # trim the MTTStage from the name - it was included
54  # solely to avoid confusion with global namespaces
55  modname = modname[:-8]
56  self.stages[modname] = a.__class__
57  # get the ordering index of this stage
58  order = a.__class__().ordering()
59  # find the point where it should be inserted
60  i = bisect_left(self.stageOrderIndices, order)
61  # now update both the indices and order
62  self.stageOrder.insert(i, modname)
63  self.stageOrderIndices.insert(i, order)
64  elif "Tool" in modname:
65  # trim the MTTTool from the name - it was included
66  # solely to avoid confusion with global namespaces
67  modname = modname[:-7]
68  self.tools[modname] = a.__class__
69  elif "Utility" in modname:
70  # trim the MTTUtility from the name - it was included
71  # solely to avoid confusion with global namespaces
72  modname = modname[:-10]
73  self.utilities[modname] = a.__class__
74  except AttributeError:
75  # just ignore it
76  continue
tuple cls
Definition: pymtt.py:208