<commit_before>
import copy
import os
import re
import types

from quickrelease.config import ConfigSpecError, ConfigSpec

class Deliverable(object):
   DELIVERABLE_SECTION_PREFIX = 'deliverable'
   DELIVERABLE_CONFIG_PREFIX = (DELIVERABLE_SECTION_PREFIX +
    ConfigSpec.CONFIG_SECTION_DELIMETER)

   ERROR_STR_NEED_NAME_OR_REGEX = ("Deliverable class '%s' must define a name "
    "or a regex for the deliverable.")

   _gDeliverablesCache = {}

   ATTRIB_TYPE_CALLBACK = 0
   ATTRIB_TYPE_REGEX = 1
   ATTRIB_TYPE_VALUE = 2

   def __init__(self, deliverableFile, deliverableClass, config, *args,
    **kwargs):
      object.__init__(self, *args, **kwargs)

      if not os.path.isabs(deliverableFile):
         raise ValueError("Must provide absolute path to Deliverable "
          "constructor")
      elif not os.path.isfile(deliverableFile):
         raise ValueError("Non-existent file passed to Deliverable constructor")
      elif not IsDeliverableSection(config, deliverableClass):
         raise ValueError("Non-existent deliverable class passed to "
          "Deliverable constructor: %s" % deliverableClass)

      self.configSection = DeliverableSectionNameFromClass(deliverableClass)
      self.deliverableClass = deliverableClass
      self.file = deliverableFile
      self.regex = None
      self.name = None
      self.filterAttributes = None
      self.attributes = []
      self.attributeHandlers = {}

      deliverableSectionItems = config.GetSectionItems(self.configSection)

      if 'name' in deliverableSectionItems:
         self.name = config.SectionGet(self.configSection, 'name').strip()

      if 'regex' in deliverableSectionItems:
         self.regex = config.SectionGet(self.configSection, 'regex').strip()

      if self.regex is None and self.name is None:
         raise ConfigSpecError(self.ERROR_STR_NEED_NAME_OR_REGEX %
          (deliverableClass))

      if 'attributes' in deliverableSectionItems:
         self.attributes = config.SectionGet(self.configSection, 'attributes',
          list)

      for attr in self.attributes:
         attributeValue = None
         attributeType = None

         if ('attrib_%s_handler' % (attr)) in deliverableSectionItems:
            attributeType = Deliverable.ATTRIB_TYPE_CALLBACK
            attributeValue = config.SectionGet(self.configSection,
             'attrib_%s_handler' % (attr))
         elif ('attrib_%s_regex' % (attr)) in deliverableSectionItems:
            attributeType = Deliverable.ATTRIB_TYPE_REGEX
            attributeValue = config.SectionGet(self.configSection,
             'attrib_%s_regex' % (attr))
         elif ('attrib_%s_value' % (attr)) in deliverableSectionItems:
            attributeType = Deliverable.ATTRIB_TYPE_VALUE
            attributeValue = config.SectionGet(self.configSection,
             'attrib_%s_value' % (attr))
         else:
            raise ConfigSpecError("Deliverable class '%s' defines "
             "attribute '%s', but doesn't define handler for it." %
             (deliverableClass, attr))

         attributeHandlerDescriptor = {}
         attributeHandlerDescriptor['type'] = attributeType

         if attributeType == Deliverable.ATTRIB_TYPE_CALLBACK:
            try:
               attributeHandlerModParts = attributeValue.split('.')
               mod = __import__('.'.join(attributeHandlerModParts[:-1]))
               for comp in attributeHandlerModParts[1:]:
                  mod = getattr(mod, comp)

               attributeHandlerDescriptor['handler'] = mod
            except NameError, ex:
               raise ConfigSpecError("Deliverable class '%s' defines an "
                "attribute callback handler for attribute '%s', but the "
                "callback is undefined: %s" % (self.deliverableClass, attr,
                str(ex)))
         elif (attributeType == Deliverable.ATTRIB_TYPE_REGEX or
          attributeType == Deliverable.ATTRIB_TYPE_VALUE):
            attributeHandlerDescriptor['handler'] = attributeValue
         else:
            assert False, "Unknown attribute handler type: %s" % (attributeType)

         self.attributeHandlers[attr] = attributeHandlerDescriptor

      if 'filter_attributes' in deliverableSectionItems:
         self.filterAttributes = config.SectionGet(self.configSection,
          'filter_attributes', list)

      if self.filterAttributes is not None:
         for fa in self.filterAttributes:
            if fa not in self.attributes:
               raise ConfigSpecError("Deliverable class '%s' defines invalid "
                "filter attribute '%s'" % (deliverableClass, fa))

   def __str__(self):
      return self.GetLocation()

   def GetName(self):
      return self.deliverableClass

   def GetLocation(self):
      return self.file

   def GetFileName(self):
      return os.path.basename(self.GetLocation())

   def GetRegex(self):
      return self.regex

   def GetFilterAttributes(self):
      return tuple(self.filterAttributes)

   def GetAttributes(self):
      return tuple(self.attributes)

   def GetAttribute(self, attribute):
      if attribute not in self.attributes:
         raise ValueError("Deliverable class '%s' has no attribute '%s" %
          (self.GetName(), attribute))

      handlerType = self.attributeHandlers[attribute]['type']

      if handlerType == Deliverable.ATTRIB_TYPE_VALUE:
         return self.attributeHandlers[attribute]['handler']
      elif handlerType == Deliverable.ATTRIB_TYPE_REGEX:
         attribMatch = re.search(self.attributeHandlers[attribute]['handler'],
          self.GetFileName())

         if attribMatch is None:
            return None
         elif len(attribMatch.groups()) == 1:
            return attribMatch.group(1)
         else:
            return attribMatch.groups()

      elif handlerType == Deliverable.ATTRIB_TYPE_CALLBACK:
         return self.attributeHandlers[attribute]['handler'](self)
      else:
         assert False, "Unknown attribute handler type: %s" % (handlerType)

def FindDeliverables(deliverableDir, config):
   if not os.path.isdir(deliverableDir):
      raise ValueError("Invalid deliverable directory: %s" % (deliverableDir))

   if Deliverable._gDeliverablesCache.has_key(deliverableDir):
      return None

   deliverables = []
   deliverableSections = GetDeliverableSections(config)

   ignoreUndefinedDeliverables = True
   try:
      ignoreUndefinedDeliverables = config.Get('ignore_undefined_deliverables',
       bool)
   except ConfigSpecError, ex:
      if ex.GetDetails() != ConfigSpecError.NO_OPTION_ERROR:
         raise ex

   for root, dirs, files in os.walk(deliverableDir):
      for f in files:
         #print "Looking at: %s" % (os.path.join(root, f))
         deliverableDescList = []
         for section in deliverableSections:
            delivRegex = None
            delivName = None
            matchType = None
            subclassType = None

            try:
               delivName = config.SectionGet(section, 'name').strip()
               matchType = 'name'
            except ConfigSpecError, ex:
               if ex.GetDetails() != ConfigSpecError.NO_OPTION_ERROR:
                  raise ex

               try:
                  delivRegex = config.SectionGet(section, 'regex').strip()
                  matchType = 'regex'
               except ConfigSpecError, ex:
                  if ex.GetDetails() == ConfigSpecError.NO_OPTION_ERROR:
                     raise ConfigSpecError(
                      Deliverable.ERROR_STR_NEED_NAME_OR_REGEX %
                      DeliverableClassFromSectionName(section))
                  else:
                     raise ex

            #print "f is %s, name is %s, regex is %s" % (f, delivName, delivRegex)
            if ((delivName is not None and f == delivName) or
             (delivRegex is not None and re.search(delivRegex, f))):
               try:
                 subclassType = config.SectionGet(section,
                  'subclass').strip()
               except ConfigSpecError, ex:
                  if ex.GetDetails() != ConfigSpecError.NO_OPTION_ERROR:
                     raise ex
                  pass

               delivClassDescription = { 'type': matchType,
                                         'subclass' : subclassType,
                                         'class' : DeliverableClassFromSectionName(section),
                                         'file' : os.path.join(root, f),
               }

               deliverableDescList.append(delivClassDescription)

         if len(deliverableDescList) == 0:
            if not ignoreUndefinedDeliverables:
               assert False, "Should be a release framework error."
            else:
               continue

         if len(deliverableDescList) == 1:
            delivDesc = deliverableDescList[0]

            if delivDesc['subclass'] is not None:
               try:
                  subclassParts = delivDesc['subclass'].split('.')
                  mod = __import__('.'.join(subclassParts[:-1]))
                  for comp in subclassParts[1:]:
                     mod = getattr(mod, comp)

                  newDelivObj = mod(delivDesc['file'], delivDesc['class'],
                   config)

               except NameError, ex:
                  raise ConfigSpecError("subclass error %s" % (ex))
            else:
               newDelivObj = Deliverable(delivDesc['file'],
                delivDesc['class'], config)

            deliverables.append(newDelivObj)

         else:
            matchedClassList = []
            fileLoc = deliverableDescList[0]['file']
            for delivDesc in deliverableDescList:
               assert fileLoc == delivDesc['file'], ("Deliverable file name "
                "mismatch (%s vs %s)?" % (fileLoc, delivDesc['file']))

               matchedClassList.append("%s (matched via %s)" % (
                delivDesc['class'], delivDesc['type']))

            raise ConfigSpecError("More than one deliverable class for "
             "the file %s: %s" % (fileLoc, ', '.join(matchedClassList)))

   Deliverable._gDeliverablesCache[deliverableDir] = tuple(deliverables)
   return len(tuple(deliverables))

def GetAllDeliverables(deliverableDir=None):
   if deliverableDir is not None:
      if not Deliverable._gDeliverablesCache.has_key(deliverableDir):
         raise ValueError("Directory %d has not been scanned for deliverables "
          "yet; use FindDeliverables()" % (deliverableDir))

      return tuple(Deliverable._gDeliverablesCache[deliverableDir])
   else:
      cacheKeys = Deliverable._gDeliverablesCache.keys()
      if len(cacheKeys) == 0:
         raise ValueError("No deliverables found yet; prime cache with
          FindDeliverables()" % (deliverableDir))

      allDeliverables = []

      for dDirs in cacheKeys:
         for deliv in Deliverable._gDeliverablesCache[dDirs]:
            allDeliverables.append(deliv)

      return tuple(allDeliverables)

def GetDeliverables(deliverableClass, deliverableDir=None):
   filterValues = deliverableClass.split(':')
   possibleDelivs = []

   for deliv in GetAllDeliverables(deliverableDir):
      if filterValues[0] == deliv.GetName():
         possibleDelivs.append(deliv)

   filteredDeliverableList = []
   for pd in possibleDelivs:
      filterNames = pd.GetFilterAttributes()

      if filterNames is None and len(filterValues) > 1:
         raise ValueError("GetDeliverables passed filter '%s' for a "
          "deliverable class that defines no filter attributes" %
          ':'.join(filterValues[1:]))

      skipThisDeliv = False
      for ndx in range(len(filterValues))[1:]:
         if pd.GetAttribute(filterNames[ndx]) != filterValues[ndx]:
            skipThisDeliv = True
            break

      if skipThisDeliv:
         continue

      filteredDeliverableList.append(pd)

   return filteredDeliverableList

def GetDeliverable(deliverableClass, deliverableDir=None):
   possibleDelivs = GetDeliverables(deliverableClass, deliverableDir)

   if len(possibleDelivs) == 0:
      return None
   elif len(possibleDelivs) > 1:
      raise ConfigSpecError("More than one deliverable matched for "
       "deliverable class %s: %s" % (deliverableClass,
       ', '.join(possibleDelivs)))
   else:
      return possibleDelivs[0]

def FlushDeliverableCache(deliverableDir=None):
   if deliverableDir is None:
      Deliverable._gDeliverablesCache.clear()
   else:
      try:
         del Deliverable._gDeliverablesCache[deliverableDir]
      except KeyError:
         raise ValueError("Deliverable directory %d not in cache" %
          (deliverableDir))

def GetDeliverableSections(config):
   retSections = []

   for section in config.GetSectionList():
      if IsDeliverableSectionName(section):
         retSections.append(section)

   return retSections

def DeliverableClassFromSectionName(sectionName):
   return re.sub('^%s' % (Deliverable.DELIVERABLE_CONFIG_PREFIX), '',
    sectionName)

def DeliverableSectionNameFromClass(delivClass):
   return Deliverable.DELIVERABLE_CONFIG_PREFIX + delivClass

def IsDeliverableSectionName(sectionName):
   return sectionName.startswith(Deliverable.DELIVERABLE_CONFIG_PREFIX)

def IsDeliverableSection(config, delivClass):
   sectionItems = config.GetSectionItems(DeliverableSectionNameFromClass(
    delivClass))
   return 'name' in sectionItems or 'regex' in sectionItems
<commit_msg>Rename a validation function (and callers) to match what it actually does.<commit_after>
import copy
import os
import re
import types

from quickrelease.config import ConfigSpecError, ConfigSpec

class Deliverable(object):
   DELIVERABLE_SECTION_PREFIX = 'deliverable'
   DELIVERABLE_CONFIG_PREFIX = (DELIVERABLE_SECTION_PREFIX +
    ConfigSpec.CONFIG_SECTION_DELIMETER)

   ERROR_STR_NEED_NAME_OR_REGEX = ("Deliverable class '%s' must define a name "
    "or a regex for the deliverable.")

   _gDeliverablesCache = {}

   ATTRIB_TYPE_CALLBACK = 0
   ATTRIB_TYPE_REGEX = 1
   ATTRIB_TYPE_VALUE = 2

   def __init__(self, deliverableFile, deliverableClass, config, *args,
    **kwargs):
      object.__init__(self, *args, **kwargs)

      if not os.path.isabs(deliverableFile):
         raise ValueError("Must provide absolute path to Deliverable "
          "constructor")
      elif not os.path.isfile(deliverableFile):
         raise ValueError("Non-existent file passed to Deliverable constructor")
      elif not IsValidDeliverableClass(config, deliverableClass):
         raise ValueError("Non-existent deliverable class passed to "
          "Deliverable constructor: %s" % deliverableClass)

      self.configSection = DeliverableSectionNameFromClass(deliverableClass)
      self.deliverableClass = deliverableClass
      self.file = deliverableFile
      self.regex = None
      self.name = None
      self.filterAttributes = None
      self.attributes = []
      self.attributeHandlers = {}

      deliverableSectionItems = config.GetSectionItems(self.configSection)

      if 'name' in deliverableSectionItems:
         self.name = config.SectionGet(self.configSection, 'name').strip()

      if 'regex' in deliverableSectionItems:
         self.regex = config.SectionGet(self.configSection, 'regex').strip()

      if self.regex is None and self.name is None:
         raise ConfigSpecError(self.ERROR_STR_NEED_NAME_OR_REGEX %
          (deliverableClass))

      if 'attributes' in deliverableSectionItems:
         self.attributes = config.SectionGet(self.configSection, 'attributes',
          list)

      for attr in self.attributes:
         attributeValue = None
         attributeType = None

         if ('attrib_%s_handler' % (attr)) in deliverableSectionItems:
            attributeType = Deliverable.ATTRIB_TYPE_CALLBACK
            attributeValue = config.SectionGet(self.configSection,
             'attrib_%s_handler' % (attr))
         elif ('attrib_%s_regex' % (attr)) in deliverableSectionItems:
            attributeType = Deliverable.ATTRIB_TYPE_REGEX
            attributeValue = config.SectionGet(self.configSection,
             'attrib_%s_regex' % (attr))
         elif ('attrib_%s_value' % (attr)) in deliverableSectionItems:
            attributeType = Deliverable.ATTRIB_TYPE_VALUE
            attributeValue = config.SectionGet(self.configSection,
             'attrib_%s_value' % (attr))
         else:
            raise ConfigSpecError("Deliverable class '%s' defines "
             "attribute '%s', but doesn't define handler for it." %
             (deliverableClass, attr))

         attributeHandlerDescriptor = {}
         attributeHandlerDescriptor['type'] = attributeType

         if attributeType == Deliverable.ATTRIB_TYPE_CALLBACK:
            try:
               attributeHandlerModParts = attributeValue.split('.')
               mod = __import__('.'.join(attributeHandlerModParts[:-1]))
               for comp in attributeHandlerModParts[1:]:
                  mod = getattr(mod, comp)

               attributeHandlerDescriptor['handler'] = mod
            except NameError, ex:
               raise ConfigSpecError("Deliverable class '%s' defines an "
                "attribute callback handler for attribute '%s', but the "
                "callback is undefined: %s" % (self.deliverableClass, attr,
                str(ex)))
         elif (attributeType == Deliverable.ATTRIB_TYPE_REGEX or
          attributeType == Deliverable.ATTRIB_TYPE_VALUE):
            attributeHandlerDescriptor['handler'] = attributeValue
         else:
            assert False, "Unknown attribute handler type: %s" % (attributeType)

         self.attributeHandlers[attr] = attributeHandlerDescriptor

      if 'filter_attributes' in deliverableSectionItems:
         self.filterAttributes = config.SectionGet(self.configSection,
          'filter_attributes', list)

      if self.filterAttributes is not None:
         for fa in self.filterAttributes:
            if fa not in self.attributes:
               raise ConfigSpecError("Deliverable class '%s' defines invalid "
                "filter attribute '%s'" % (deliverableClass, fa))

   def __str__(self):
      return self.GetLocation()

   def GetName(self):
      return self.deliverableClass

   def GetLocation(self):
      return self.file

   def GetFileName(self):
      return os.path.basename(self.GetLocation())

   def GetRegex(self):
      return self.regex

   def GetFilterAttributes(self):
      return tuple(self.filterAttributes)

   def GetAttributes(self):
      return tuple(self.attributes)

   def GetAttribute(self, attribute):
      if attribute not in self.attributes:
         raise ValueError("Deliverable class '%s' has no attribute '%s" %
          (self.GetName(), attribute))

      handlerType = self.attributeHandlers[attribute]['type']

      if handlerType == Deliverable.ATTRIB_TYPE_VALUE:
         return self.attributeHandlers[attribute]['handler']
      elif handlerType == Deliverable.ATTRIB_TYPE_REGEX:
         attribMatch = re.search(self.attributeHandlers[attribute]['handler'],
          self.GetFileName())

         if attribMatch is None:
            return None
         elif len(attribMatch.groups()) == 1:
            return attribMatch.group(1)
         else:
            return attribMatch.groups()

      elif handlerType == Deliverable.ATTRIB_TYPE_CALLBACK:
         return self.attributeHandlers[attribute]['handler'](self)
      else:
         assert False, "Unknown attribute handler type: %s" % (handlerType)

def FindDeliverables(deliverableDir, config):
   if not os.path.isdir(deliverableDir):
      raise ValueError("Invalid deliverable directory: %s" % (deliverableDir))

   if Deliverable._gDeliverablesCache.has_key(deliverableDir):
      return None

   deliverables = []
   deliverableSections = GetDeliverableSections(config)

   ignoreUndefinedDeliverables = True
   try:
      ignoreUndefinedDeliverables = config.Get('ignore_undefined_deliverables',
       bool)
   except ConfigSpecError, ex:
      if ex.GetDetails() != ConfigSpecError.NO_OPTION_ERROR:
         raise ex

   for root, dirs, files in os.walk(deliverableDir):
      for f in files:
         #print "Looking at: %s" % (os.path.join(root, f))
         deliverableDescList = []
         for section in deliverableSections:
            delivRegex = None
            delivName = None
            matchType = None
            subclassType = None

            try:
               delivName = config.SectionGet(section, 'name').strip()
               matchType = 'name'
            except ConfigSpecError, ex:
               if ex.GetDetails() != ConfigSpecError.NO_OPTION_ERROR:
                  raise ex

               try:
                  delivRegex = config.SectionGet(section, 'regex').strip()
                  matchType = 'regex'
               except ConfigSpecError, ex:
                  if ex.GetDetails() == ConfigSpecError.NO_OPTION_ERROR:
                     raise ConfigSpecError(
                      Deliverable.ERROR_STR_NEED_NAME_OR_REGEX %
                      DeliverableClassFromSectionName(section))
                  else:
                     raise ex

            #print "f is %s, name is %s, regex is %s" % (f, delivName, delivRegex)
            if ((delivName is not None and f == delivName) or
             (delivRegex is not None and re.search(delivRegex, f))):
               try:
                 subclassType = config.SectionGet(section,
                  'subclass').strip()
               except ConfigSpecError, ex:
                  if ex.GetDetails() != ConfigSpecError.NO_OPTION_ERROR:
                     raise ex
                  pass

               delivClassDescription = { 'type': matchType,
                                         'subclass' : subclassType,
                                         'class' : DeliverableClassFromSectionName(section),
                                         'file' : os.path.join(root, f),
               }

               deliverableDescList.append(delivClassDescription)

         if len(deliverableDescList) == 0:
            if not ignoreUndefinedDeliverables:
               assert False, "Should be a release framework error."
            else:
               continue

         if len(deliverableDescList) == 1:
            delivDesc = deliverableDescList[0]

            if delivDesc['subclass'] is not None:
               try:
                  subclassParts = delivDesc['subclass'].split('.')
                  mod = __import__('.'.join(subclassParts[:-1]))
                  for comp in subclassParts[1:]:
                     mod = getattr(mod, comp)

                  newDelivObj = mod(delivDesc['file'], delivDesc['class'],
                   config)

               except NameError, ex:
                  raise ConfigSpecError("subclass error %s" % (ex))
            else:
               newDelivObj = Deliverable(delivDesc['file'],
                delivDesc['class'], config)

            deliverables.append(newDelivObj)

         else:
            matchedClassList = []
            fileLoc = deliverableDescList[0]['file']
            for delivDesc in deliverableDescList:
               assert fileLoc == delivDesc['file'], ("Deliverable file name "
                "mismatch (%s vs %s)?" % (fileLoc, delivDesc['file']))

               matchedClassList.append("%s (matched via %s)" % (
                delivDesc['class'], delivDesc['type']))

            raise ConfigSpecError("More than one deliverable class for "
             "the file %s: %s" % (fileLoc, ', '.join(matchedClassList)))

   Deliverable._gDeliverablesCache[deliverableDir] = tuple(deliverables)
   return len(tuple(deliverables))

def GetAllDeliverables(deliverableDir=None):
   if deliverableDir is not None:
      if not Deliverable._gDeliverablesCache.has_key(deliverableDir):
         raise ValueError("Directory %d has not been scanned for deliverables "
          "yet; use FindDeliverables()" % (deliverableDir))

      return tuple(Deliverable._gDeliverablesCache[deliverableDir])
   else:
      cacheKeys = Deliverable._gDeliverablesCache.keys()
      if len(cacheKeys) == 0:
         raise ValueError("No deliverables found yet; prime cache with
          FindDeliverables()" % (deliverableDir))

      allDeliverables = []

      for dDirs in cacheKeys:
         for deliv in Deliverable._gDeliverablesCache[dDirs]:
            allDeliverables.append(deliv)

      return tuple(allDeliverables)

def GetDeliverables(deliverableClass, deliverableDir=None):
   filterValues = deliverableClass.split(':')
   possibleDelivs = []

   for deliv in GetAllDeliverables(deliverableDir):
      if filterValues[0] == deliv.GetName():
         possibleDelivs.append(deliv)

   filteredDeliverableList = []
   for pd in possibleDelivs:
      filterNames = pd.GetFilterAttributes()

      if filterNames is None and len(filterValues) > 1:
         raise ValueError("GetDeliverables passed filter '%s' for a "
          "deliverable class that defines no filter attributes" %
          ':'.join(filterValues[1:]))

      skipThisDeliv = False
      for ndx in range(len(filterValues))[1:]:
         if pd.GetAttribute(filterNames[ndx]) != filterValues[ndx]:
            skipThisDeliv = True
            break

      if skipThisDeliv:
         continue

      filteredDeliverableList.append(pd)

   return filteredDeliverableList

def GetDeliverable(deliverableClass, deliverableDir=None):
   possibleDelivs = GetDeliverables(deliverableClass, deliverableDir)

   if len(possibleDelivs) == 0:
      return None
   elif len(possibleDelivs) > 1:
      raise ConfigSpecError("More than one deliverable matched for "
       "deliverable class %s: %s" % (deliverableClass,
       ', '.join(possibleDelivs)))
   else:
      return possibleDelivs[0]

def FlushDeliverableCache(deliverableDir=None):
   if deliverableDir is None:
      Deliverable._gDeliverablesCache.clear()
   else:
      try:
         del Deliverable._gDeliverablesCache[deliverableDir]
      except KeyError:
         raise ValueError("Deliverable directory %d not in cache" %
          (deliverableDir))

def GetDeliverableSections(config):
   retSections = []

   for section in config.GetSectionList():
      if IsDeliverableSectionName(section):
         retSections.append(section)

   return retSections

def DeliverableClassFromSectionName(sectionName):
   return re.sub('^%s' % (Deliverable.DELIVERABLE_CONFIG_PREFIX), '',
    sectionName)

def DeliverableSectionNameFromClass(delivClass):
   return Deliverable.DELIVERABLE_CONFIG_PREFIX + delivClass

def IsDeliverableSectionName(sectionName):
   return sectionName.startswith(Deliverable.DELIVERABLE_CONFIG_PREFIX)

def IsValidDeliverableClass(config, delivClass):
   try:
      sectionItems = config.GetSectionItems(DeliverableSectionNameFromClass(
       delivClass))
   except ValueError:
      return False

   return 'name' in sectionItems or 'regex' in sectionItems
<|endoftext|>


<commit_before>from BuildDemos import demos
import argparse, requests, json, subprocess, re

parser = argparse.ArgumentParser()
parser.add_argument("version", type=str, help="Vaadin version that was just built")
parser.add_argument("deployUrl", type=str, help="Base url of the deployment server")
parser.add_argument("buildResultUrl", type=str, help="URL for the build result page")

parser.add_argument("teamcityUser", type=str, help="Teamcity username to use")
parser.add_argument("teamcityPassword", type=str, help="Password for given teamcity username")

parser.add_argument("teamcityUrl", type=str, help="Address to the teamcity server")
parser.add_argument("buildId", type=str, help="ID of the build to generate this report for")

parser.add_argument("frameworkRepoUrl", type=str, help="URL to the framework staging repository")
parser.add_argument("archetypeRepoUrl", type=str, help="URL to the archetype staging repository")
parser.add_argument("pluginRepoUrl", type=str, help="URL to the plugin staging repository")
args = parser.parse_args()

def createTableRow(*columns):
    html = "
"
    for column in columns:
        html += "" + column + "	"
    return html + "
"

def getBuildStatusHtml():
    build_steps_request_string = "http://{}/app/rest/problemOccurrences?locator=build:{}".format(args.teamcityUrl, args.buildId)
    build_steps_request = requests.get(build_steps_request_string, auth=(args.teamcityUser, args.teamcityPassword), headers={'Accept':'application/json'})
    if build_steps_request.status_code != 200:
        return createTableRow(traffic_light.format(color="black"), "Build status: unable to retrieve status of build")
    else:
        build_steps_json = build_steps_request.json()
        if build_steps_json["count"] == 0:
            return createTableRow(traffic_light.format(color="green"), "Build status: all build steps successful")
        else:
            return createTableRow(traffic_light.format(color="red"), "Build status: there are failing build steps, check the build report".format(args.buildResultUrl))

def getTestStatusHtml():
    test_failures_request_string = "http://{}/app/rest/testOccurrences?locator=build:{},status:FAILURE".format(args.teamcityUrl, args.buildId)
    test_failures_request = requests.get(test_failures_request_string, auth=(args.teamcityUser, args.teamcityPassword), headers={'Accept':'application/json'})
    if test_failures_request.status_code != 200:
        return createTableRow(traffic_light.format(color="black"), "Test status: unable to retrieve status of tests")
    else:
        test_failures_json = test_failures_request.json()
        if test_failures_json["count"] == 0:
            return createTableRow(traffic_light.format(color="green"), "Test status: all tests passing")
        else:
            return createTableRow(traffic_light.format(color="red"), "Test status: there are " + str(test_failures_json["count"]) + " failing tests, see report.".format(args.buildId))

def getDemoLinksHtml():
    demos_html = "Try demos"
    demos_html += "
"
    for demo in demos:
        demos_html += "
{demoName}
".format(url=args.deployUrl, demoName=demo, version=args.version)
    return demos_html + "
"

def getDirs(url):
    page = requests.get(url)
    files = re.findall('(.*)', page.text)
    dirs = filter(lambda x: x.endswith('/'), files)
    return list(map(lambda x: x.replace('/', ''), dirs))

def dirTree(url):
    dirs = getDirs(url)
    result = []
    for d in dirs:
        result.append(d)
        subDirs = list(map(lambda x: d + '/' + x, dirTree(url + '/' + d)))
        result.extend(subDirs)
    return result

def getAllowedArtifactPaths(allowedArtifacts):
    result = []
    for artifact in allowedArtifacts:
        parts = artifact.split('/', 1)
        result.append(parts[0])
        if len(parts) > 1:
            subart = getAllowedArtifactPaths([ parts[1] ])
            subArtifacts = list(map(lambda x: parts[0] + '/' + x, subart))
            result.extend(subArtifacts)
    return result

def checkStagingContents(url, allowedArtifacts):
    dirs = dirTree(url)
    allowedDirs = getAllowedArtifactPaths(allowedArtifacts)
    return set(dirs) == set(allowedDirs)

def getStagingContentsHtml(repoUrl, allowedArtifacts, name):
    if checkStagingContents(repoUrl, allowedArtifacts):
        return createTableRow(traffic_light.format(color="green"), "No extra artifacts found in the {} staging repository".format(name))
    else:
        return createTableRow(traffic_light.format(color="red"), "Extra artifacts found in the {} staging repository".format(name))

def completeArtifactName(artifactId, version):
    return 'com/vaadin/' + artifactId + '/' + version

def completeArtifactNames(artifactIds, version):
    return list(map(lambda x: completeArtifactName(x, version), artifactIds))

allowedPluginArtifacts = completeArtifactNames([ 'vaadin-maven-plugin' ], args.version)
allowedArchetypeArtifacts = completeArtifactNames([ 'vaadin-archetype-application', 'vaadin-archetype-application-multimodule', 'vaadin-archetype-application-example', 'vaadin-archetype-widget', 'vaadin-archetype-liferay-portlet' ], args.version)
allowedFrameworkArtifacts = completeArtifactNames([ 'vaadin-root', 'vaadin-bom', 'vaadin-shared', 'vaadin-server', 'vaadin-client', 'vaadin-client-compiler', 'vaadin-client-compiled', 'vaadin-push', 'vaadin-themes', 'vaadin-widgets', 'vaadin-compatibility-shared', 'vaadin-compatibility-server', 'vaadin-compatibility-client', 'vaadin-compatibility-client-compiled', 'vaadin-compatibility-themes' ], args.version)

content = "
"
traffic_light = "<svg width=\"20px\" height=\"20px\" style=\"padding-right:5px\"><circle cx=\"10\" cy=\"10\" r=\"10\" fill=\"{color}\"/></svg>"

# Build step status
content += getBuildStatusHtml()

# Test failures
content += getTestStatusHtml()

# Missing @since tags
try:
    p1 = subprocess.Popen(['find', '.', '-name', '*.java'], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(['xargs', 'egrep', '-n', '@since ?$'], stdin=p1.stdout, stdout=subprocess.PIPE)
    missing = subprocess.check_output(['egrep', '-v', '/(test|tests|target)/'], stdin=p2.stdout)
    content += createTableRow(traffic_light.format(color="red"), "Empty @since:

%s
" % (missing))

except subprocess.CalledProcessError as e:
    if e.returncode == 1:
        content += createTableRow(traffic_light.format(color="green"), "No empty @since")
    else:
        raise e

# check staging repositories don't contain extra artifacts
content += getStagingContentsHtml(args.frameworkRepoUrl, allowedFrameworkArtifacts, "framework")
content += getStagingContentsHtml(args.archetypeRepoUrl, allowedArchetypeArtifacts, "archetype")
content += getStagingContentsHtml(args.pluginRepoUrl, allowedPluginArtifacts, "plugin")

content += createTableRow("", "
Manual checks before publishing
")
# try demos
content += createTableRow("", getDemoLinksHtml())

# link to release notes
content += createTableRow("", "Check release notes".format(args.buildId))
# link to api diff
content += createTableRow("", "Check API diff".format(args.buildId))

# closed fixed tickets without a milestone
content += createTableRow("", "Closed fixed tickets without milestone {version}".format(version=args.version))
# closed tickets with milestone
content += createTableRow("", "Closed tickets with milestone {version}".format(version=args.version))
# pending release tickets with milestone
content += createTableRow("", "Pending-release tickets with milestone {version}".format(version=args.version))
# pending release tickets without milestone
content += createTableRow("", "Pending-release tickets without milestone")

content += createTableRow("", "
Preparations before publishing
")
# create milestone for next release
content += createTableRow("", "Create milestone for nex release")
# close trac milestone
content += createTableRow("", "Close Trac Milestone".format(version=args.version))
# verify pending release tickets still have milestone
content += createTableRow("", "Verify pending release tickets still have milestone {version}".format(version=args.version))
# add version to trac
content += createTableRow("", "Add version {version} to Trac".format(version=args.version))

content += "
"
f = open("result/report.html", 'w')
f.write(content)
<commit_msg>Improve GenerateBuildTestAndStagingReport<commit_after>from BuildDemos import demos
import argparse, requests, json, subprocess, re

parser = argparse.ArgumentParser()
parser.add_argument("version", type=str, help="Vaadin version that was just built")
parser.add_argument("deployUrl", type=str, help="Base url of the deployment server")
parser.add_argument("buildResultUrl", type=str, help="URL for the build result page")

parser.add_argument("teamcityUser", type=str, help="Teamcity username to use")
parser.add_argument("teamcityPassword", type=str, help="Password for given teamcity username")

parser.add_argument("teamcityUrl", type=str, help="Address to the teamcity server")
parser.add_argument("buildId", type=str, help="ID of the build to generate this report for")

parser.add_argument("frameworkRepoUrl", type=str, help="URL to the framework staging repository")
parser.add_argument("archetypeRepoUrl", type=str, help="URL to the archetype staging repository")
parser.add_argument("pluginRepoUrl", type=str, help="URL to the plugin staging repository")
args = parser.parse_args()

def createTableRow(*columns):
    html = "
"
    for column in columns:
        html += "" + column + "	"
    return html + "
"

def getBuildStatusHtml():
    build_steps_request_string = "http://{}/app/rest/problemOccurrences?locator=build:{}".format(args.teamcityUrl, args.buildId)
    build_steps_request = requests.get(build_steps_request_string, auth=(args.teamcityUser, args.teamcityPassword), headers={'Accept':'application/json'})
    if build_steps_request.status_code != 200:
        return createTableRow(traffic_light.format(color="black"), "Build status: unable to retrieve status of build")
    else:
        build_steps_json = build_steps_request.json()
        if build_steps_json["count"] == 0:
            return createTableRow(traffic_light.format(color="green"), "Build status: all build steps successful")
        else:
            return createTableRow(traffic_light.format(color="red"), "Build status: there are failing build steps, check the build report".format(args.buildResultUrl))

def getTestStatusHtml():
    test_failures_request_string = "http://{}/app/rest/testOccurrences?locator=build:{},status:FAILURE".format(args.teamcityUrl, args.buildId)
    test_failures_request = requests.get(test_failures_request_string, auth=(args.teamcityUser, args.teamcityPassword), headers={'Accept':'application/json'})
    if test_failures_request.status_code != 200:
        return createTableRow(traffic_light.format(color="black"), "Test status: unable to retrieve status of tests")
    else:
        test_failures_json = test_failures_request.json()
        if test_failures_json["count"] == 0:
            return createTableRow(traffic_light.format(color="green"), "Test status: all tests passing")
        else:
            return createTableRow(traffic_light.format(color="red"), "Test status: there are " + str(test_failures_json["count"]) + " failing tests, see report.".format(args.buildId))

def getDemoLinksHtml():
    demos_html = "Try demos"
    demos_html += "
"
    for demo in demos:
        demos_html += "
{demoName}
".format(url=args.deployUrl, demoName=demo, version=args.version)
    return demos_html + "
"

def getApiDiffHtml():
    apidiff_html = "Check API diff"
    apidiff_html += "
"
    modules = [
        "client", "client-compiler",
        "compatibility-client",
        "compatibility-server",
        "compatibility-shared",
        "server", "shared"
    ]
    for module in modules:
        apidiff_html += "
{}
".format(args.buildId, module, module)
    return apidiff_html + "
"

def getDirs(url):
    page = requests.get(url)
    files = re.findall('(.*)', page.text)
    dirs = filter(lambda x: x.endswith('/'), files)
    return list(map(lambda x: x.replace('/', ''), dirs))

def dirTree(url):
    dirs = getDirs(url)
    result = []
    for d in dirs:
        result.append(d)
        subDirs = list(map(lambda x: d + '/' + x, dirTree(url + '/' + d)))
        result.extend(subDirs)
    return result

def getAllowedArtifactPaths(allowedArtifacts):
    result = []
    for artifact in allowedArtifacts:
        parts = artifact.split('/', 1)
        result.append(parts[0])
        if len(parts) > 1:
            subart = getAllowedArtifactPaths([ parts[1] ])
            subArtifacts = list(map(lambda x: parts[0] + '/' + x, subart))
            result.extend(subArtifacts)
    return result

def checkStagingContents(url, allowedArtifacts):
    dirs = dirTree(url)
    allowedDirs = getAllowedArtifactPaths(allowedArtifacts)
    return set(dirs) == set(allowedDirs)

def getStagingContentsHtml(repoUrl, allowedArtifacts, name):
    if checkStagingContents(repoUrl, allowedArtifacts):
        return createTableRow(traffic_light.format(color="green"), "No extra artifacts found in the {} staging repository. Link to the repository.".format(name, repoUrl))
    else:
        return createTableRow(traffic_light.format(color="red"), "Extra artifacts found in the {} staging repository. Link to the repository.".format(name, repoUrl))

def completeArtifactName(artifactId, version):
    return 'com/vaadin/' + artifactId + '/' + version

def completeArtifactNames(artifactIds, version):
    return list(map(lambda x: completeArtifactName(x, version), artifactIds))

allowedPluginArtifacts = completeArtifactNames([ 'vaadin-maven-plugin' ], args.version)
allowedArchetypeArtifacts = completeArtifactNames([ 'vaadin-archetype-application', 'vaadin-archetype-application-multimodule', 'vaadin-archetype-application-example', 'vaadin-archetype-widget', 'vaadin-archetype-liferay-portlet' ], args.version)
allowedFrameworkArtifacts = completeArtifactNames([ 'vaadin-root', 'vaadin-bom', 'vaadin-shared', 'vaadin-server', 'vaadin-client', 'vaadin-client-compiler', 'vaadin-client-compiled', 'vaadin-push', 'vaadin-themes', 'vaadin-widgets', 'vaadin-compatibility-shared', 'vaadin-compatibility-server', 'vaadin-compatibility-client', 'vaadin-compatibility-client-compiled', 'vaadin-compatibility-themes' ], args.version)

content = "
"
traffic_light = "<svg width=\"20px\" height=\"20px\" style=\"padding-right:5px\"><circle cx=\"10\" cy=\"10\" r=\"10\" fill=\"{color}\"/></svg>"

# Build step status
content += getBuildStatusHtml()

# Test failures
content += getTestStatusHtml()

# Missing @since tags
try:
    p1 = subprocess.Popen(['find', '.', '-name', '*.java'], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(['xargs', 'egrep', '-n', '@since ?$'], stdin=p1.stdout, stdout=subprocess.PIPE)
    missing = subprocess.check_output(['egrep', '-v', '/(testbench|test|tests|target)/'], stdin=p2.stdout)
    content += createTableRow(traffic_light.format(color="red"), "Empty @since:

%s
" % (missing))

except subprocess.CalledProcessError as e:
    if e.returncode == 1:
        content += createTableRow(traffic_light.format(color="green"), "No empty @since")
    else:
        raise e

# check staging repositories don't contain extra artifacts
content += getStagingContentsHtml(args.frameworkRepoUrl, allowedFrameworkArtifacts, "framework")
content += getStagingContentsHtml(args.archetypeRepoUrl, allowedArchetypeArtifacts, "archetype")
content += getStagingContentsHtml(args.pluginRepoUrl, allowedPluginArtifacts, "plugin")

content += createTableRow("", "
Manual checks before publishing
")
# try demos
content += createTableRow("", getDemoLinksHtml())

# link to release notes
content += createTableRow("", "Check release notes".format(args.buildId))
# link to api diff
content += createTableRow("", getApiDiffHtml())

# closed fixed tickets without a milestone
content += createTableRow("", "Closed fixed tickets without milestone {version}".format(version=args.version))
# closed tickets with milestone
content += createTableRow("", "Closed tickets with milestone {version}".format(version=args.version))
# pending release tickets with milestone
content += createTableRow("", "Pending-release tickets with milestone {version}".format(version=args.version))
# pending release tickets without milestone
content += createTableRow("", "Pending-release tickets without milestone")

content += createTableRow("", "
Preparations before publishing
")
# create milestone for next release
content += createTableRow("", "Create milestone for next release")
# close trac milestone
content += createTableRow("", "Close Trac Milestone".format(version=args.version))
# verify pending release tickets still have milestone
content += createTableRow("", "Verify pending release tickets still have milestone {version}".format(version=args.version))
# add version to trac
content += createTableRow("", "Add version {version} to Trac".format(version=args.version))

content += "
"
f = open("result/report.html", 'w')
f.write(content)
<|endoftext|>


<commit_before>import rcpy
from rcpy._mpu9250 import *

ACCEL_FSR_2G = 0
ACCEL_FSR_4G = 1
ACCEL_FSR_8G = 2
ACCEL_FSR_16G = 3

GYRO_FSR_250DPS = 0
GYRO_FSR_500DPS = 1
GYRO_FSR_1000DPS = 2
GYRO_FSR_2000DPS = 3

ACCEL_DLPF_OFF = 0
ACCEL_DLPF_184 = 1
ACCEL_DLPF_92 = 2
ACCEL_DLPF_41 = 3
ACCEL_DLPF_20 = 4
ACCEL_DLPF_10 = 5
ACCEL_DLPF_5 = 6

GYRO_DLPF_OFF = 0
GYRO_DLPF_184 = 1
GYRO_DLPF_92 = 2
GYRO_DLPF_41 = 3
GYRO_DLPF_20 = 4
GYRO_DLPF_10 = 5
GYRO_DLPF_5 = 6

ORIENTATION_Z_UP	= 136,
ORIENTATION_Z_DOWN	= 396,
ORIENTATION_X_UP	= 14,
ORIENTATION_X_DOWN	= 266,
ORIENTATION_Y_UP	= 112,
ORIENTATION_Y_DOWN	= 336,
ORIENTATION_X_FORWARD	= 133,
ORIENTATION_X_BACK	= 161

# Uses Alex Martelli's Borg for making MPU9250 a singleton

class IMU():

    _shared_state = {}

    def __init__(**kwargs):

        # Makes sure clock is a singleton
        self.__dict__ = self._shared_state

        # Do not initialize if already initialized
        if not self.__dict__ == {}:
            return self.set(**kwargs)

        # get defaults
        defaults = get()

        # accel_fsr
        self.accel_fsr = kwargs.pop('accel_fsr', defaults['accel_fsr'])

        # gyro_fsr
        self.gyro_fsr = kwargs.pop('gyro_fsr', defaults['gyro_fsr'])

        # accel_dlpf
        self.accel_dlpf = kwargs.pop('accel_dlpf', defaults['accel_dlpf'])

        # gyro_dlpf
        self.gyro_dlpf = kwargs.pop('gyro_dlpf', defaults['gyro_dlpf'])

        # enable_magnetometer
        self.enable_magnetometer = kwargs.pop('enable_magnetometer',
                                              defaults['enable_magnetometer'])

        # orientation
        self.orientation = kwargs.pop('orientation', defaults['orientation'])

        # compass_time_constant
        self.compass_time_constant = kwargs.pop('compass_time_constant',
                                                defaults['compass_time_constant'])

        # dmp_interrupt_priority
        self.dmp_interrupt_priority = kwargs.pop('dmp_interrupt_priority',
                                                 defaults['dmp_interrupt_priority'])

        # dmp_sample_rate
        self.dmp_sample_rate = kwargs.pop('dmp_sample_rate',
                                          defaults['dmp_sample_rate'])

        # show_warnings
        self.show_warnings = kwargs.pop('show_warnings',
                                        defaults['show_warnings'])

        # enable_dmp
        self.enable_dmp = kwargs.pop('enable_dmp',
                                     True)

        # enable_fusion
        self.enable_fusion = kwargs.pop('enable_fusion',
                                        defaults['enable_fusion'])

        # call mpu9250 initialize
        initialize(accel_fsr = self.accel_fsr,
                   gyro_fsr = self.gyro_fsr,
                   accel_dlpf = self.accel_dlpf,
                   gyro_dlpf = self.gyro_dlpf,
                   enable_magnetometer = self.enable_magnetometer,
                   orientation = self.orientation,
                   compass_time_constant = self.compass_time_constant,
                   dmp_interrupt_priority = self.dmp_interrupt_priority,
                   dmp_sample_rate = self.dmp_sample_rate,
                   show_warnings = self.show_warnings,
                   enable_dmp = self.enable_dmp,
                   enable_fusion = self.enable_fusion)

        # initialize data
        self.data = {}

    def set(self, **kwargs):

        # accel_fsr
        if 'accel_fsr' in kwargs:
            self.accel_fsr = kwargs.pop('accel_fsr')

        # gyro_fsr
        if 'gyro_fsr' in kwargs:
            self.gyro_fsr = kwargs.pop('gyro_fsr')

        # accel_dlpf
        if 'accel_dlpf' in kwargs:
            self.accel_dlpf = kwargs.pop('accel_dlpf')

        # gyro_dlpf
        if 'gyro_dlpf' in kwargs:
            self.gyro_dlpf = kwargs.pop('gyro_dlpf')

        # enable_magnetometer
        if 'enable_magnetometer' in kwargs:
            self.enable_magnetometer = kwargs.pop('enable_magnetometer')

        # orientation
        if 'orientation' in kwargs:
            self.orientation = kwargs.pop('orientation')

        # compass_time_constant
        if 'compass_time_constant' in kwargs:
            self.compass_time_constant = kwargs.pop('compass_time_constant')

        # dmp_interrupt_priority
        if 'dmp_interrupt_priority' in kwargs:
            self.dmp_interrupt_priority = kwargs.pop('dmp_interrupt_priority')

        # dmp_sample_rate
        if 'dmp_sample_rate' in kwargs:
            self.dmp_sample_rate = kwargs.pop('dmp_sample_rate')

        # show_warnings
        if 'show_warnings' in kwargs:
            self.show_warnings = kwargs.pop('show_warnings')

        # enable_dmp
        if 'enable_dmp' in kwargs:
            self.enable_dmp = kwargs.pop('enable_dmp')

        # enable_fusion
        if 'enable_fusion' in kwargs:
            self.enable_fusion = kwargs.pop('enable_fusion')

        # call mpu9250 initialize
        initialize(accel_fsr = self.accel_fsr,
                   gyro_fsr = self.gyro_fsr,
                   accel_dlpf = self.accel_dlpf,
                   gyro_dlpf = self.gyro_dlpf,
                   enable_magnetometer = self.enable_magnetometer,
                   orientation = self.orientation,
                   compass_time_constant = self.compass_time_constant,
                   dmp_interrupt_priority = self.dmp_interrupt_priority,
                   dmp_sample_rate = self.dmp_sample_rate,
                   show_warnings = self.show_warnings,
                   enable_dmp = self.enable_dmp,
                   enable_fusion = self.enable_fusion)

    def read(self):

        # read mpu9250
        return read()

imu = IMU()
<commit_msg>mpu9250 class<commit_after>import rcpy
from rcpy._mpu9250 import *

ACCEL_FSR_2G = 0
ACCEL_FSR_4G = 1
ACCEL_FSR_8G = 2
ACCEL_FSR_16G = 3

GYRO_FSR_250DPS = 0
GYRO_FSR_500DPS = 1
GYRO_FSR_1000DPS = 2
GYRO_FSR_2000DPS = 3

ACCEL_DLPF_OFF = 0
ACCEL_DLPF_184 = 1
ACCEL_DLPF_92 = 2
ACCEL_DLPF_41 = 3
ACCEL_DLPF_20 = 4
ACCEL_DLPF_10 = 5
ACCEL_DLPF_5 = 6

GYRO_DLPF_OFF = 0
GYRO_DLPF_184 = 1
GYRO_DLPF_92 = 2
GYRO_DLPF_41 = 3
GYRO_DLPF_20 = 4
GYRO_DLPF_10 = 5
GYRO_DLPF_5 = 6

ORIENTATION_Z_UP	= 136,
ORIENTATION_Z_DOWN	= 396,
ORIENTATION_X_UP	= 14,
ORIENTATION_X_DOWN	= 266,
ORIENTATION_Y_UP	= 112,
ORIENTATION_Y_DOWN	= 336,
ORIENTATION_X_FORWARD	= 133,
ORIENTATION_X_BACK	= 161

# Uses Alex Martelli's Borg for making MPU9250 a singleton

class IMU():

    _shared_state = {}

    def __init__(self, **kwargs):

        # Makes sure clock is a singleton
        self.__dict__ = self._shared_state

        # Do not initialize if already initialized
        if not self.__dict__ == {}:
            return self.set(**kwargs)

        # get defaults
        defaults = get()

        # accel_fsr
        self.accel_fsr = kwargs.pop('accel_fsr', defaults['accel_fsr'])

        # gyro_fsr
        self.gyro_fsr = kwargs.pop('gyro_fsr', defaults['gyro_fsr'])

        # accel_dlpf
        self.accel_dlpf = kwargs.pop('accel_dlpf', defaults['accel_dlpf'])

        # gyro_dlpf
        self.gyro_dlpf = kwargs.pop('gyro_dlpf', defaults['gyro_dlpf'])

        # enable_magnetometer
        self.enable_magnetometer = kwargs.pop('enable_magnetometer',
                                              defaults['enable_magnetometer'])

        # orientation
        self.orientation = kwargs.pop('orientation', defaults['orientation'])

        # compass_time_constant
        self.compass_time_constant = kwargs.pop('compass_time_constant',
                                                defaults['compass_time_constant'])

        # dmp_interrupt_priority
        self.dmp_interrupt_priority = kwargs.pop('dmp_interrupt_priority',
                                                 defaults['dmp_interrupt_priority'])

        # dmp_sample_rate
        self.dmp_sample_rate = kwargs.pop('dmp_sample_rate',
                                          defaults['dmp_sample_rate'])

        # show_warnings
        self.show_warnings = kwargs.pop('show_warnings',
                                        defaults['show_warnings'])

        # enable_dmp
        self.enable_dmp = kwargs.pop('enable_dmp',
                                     True)

        # enable_fusion
        self.enable_fusion = kwargs.pop('enable_fusion',
                                        defaults['enable_fusion'])

        # call mpu9250 initialize
        initialize(accel_fsr = self.accel_fsr,
                   gyro_fsr = self.gyro_fsr,
                   accel_dlpf = self.accel_dlpf,
                   gyro_dlpf = self.gyro_dlpf,
                   enable_magnetometer = self.enable_magnetometer,
                   orientation = self.orientation,
                   compass_time_constant = self.compass_time_constant,
                   dmp_interrupt_priority = self.dmp_interrupt_priority,
                   dmp_sample_rate = self.dmp_sample_rate,
                   show_warnings = self.show_warnings,
                   enable_dmp = self.enable_dmp,
                   enable_fusion = self.enable_fusion)

        # initialize data
        self.data = {}

    def set(self, **kwargs):

        # accel_fsr
        if 'accel_fsr' in kwargs:
            self.accel_fsr = kwargs.pop('accel_fsr')

        # gyro_fsr
        if 'gyro_fsr' in kwargs:
            self.gyro_fsr = kwargs.pop('gyro_fsr')

        # accel_dlpf
        if 'accel_dlpf' in kwargs:
            self.accel_dlpf = kwargs.pop('accel_dlpf')

        # gyro_dlpf
        if 'gyro_dlpf' in kwargs:
            self.gyro_dlpf = kwargs.pop('gyro_dlpf')

        # enable_magnetometer
        if 'enable_magnetometer' in kwargs:
            self.enable_magnetometer = kwargs.pop('enable_magnetometer')

        # orientation
        if 'orientation' in kwargs:
            self.orientation = kwargs.pop('orientation')

        # compass_time_constant
        if 'compass_time_constant' in kwargs:
            self.compass_time_constant = kwargs.pop('compass_time_constant')

        # dmp_interrupt_priority
        if 'dmp_interrupt_priority' in kwargs:
            self.dmp_interrupt_priority = kwargs.pop('dmp_interrupt_priority')

        # dmp_sample_rate
        if 'dmp_sample_rate' in kwargs:
            self.dmp_sample_rate = kwargs.pop('dmp_sample_rate')

        # show_warnings
        if 'show_warnings' in kwargs:
            self.show_warnings = kwargs.pop('show_warnings')

        # enable_dmp
        if 'enable_dmp' in kwargs:
            self.enable_dmp = kwargs.pop('enable_dmp')

        # enable_fusion
        if 'enable_fusion' in kwargs:
            self.enable_fusion = kwargs.pop('enable_fusion')

        # call mpu9250 initialize
        initialize(accel_fsr = self.accel_fsr,
                   gyro_fsr = self.gyro_fsr,
                   accel_dlpf = self.accel_dlpf,
                   gyro_dlpf = self.gyro_dlpf,
                   enable_magnetometer = self.enable_magnetometer,
                   orientation = self.orientation,
                   compass_time_constant = self.compass_time_constant,
                   dmp_interrupt_priority = self.dmp_interrupt_priority,
                   dmp_sample_rate = self.dmp_sample_rate,
                   show_warnings = self.show_warnings,
                   enable_dmp = self.enable_dmp,
                   enable_fusion = self.enable_fusion)

    def read(self):

        # read mpu9250
        return read()

imu = IMU()
<|endoftext|>


<commit_before>
import copy
import os
import re
import types

from quickrelease.config import ConfigSpecError, ConfigSpec
from quickrelease.utils import ImportModule

class Deliverable(object):
    DELIVERABLE_SECTION_PREFIX = 'deliverable'
    DELIVERABLE_CONFIG_PREFIX = (DELIVERABLE_SECTION_PREFIX +
     ConfigSpec.CONFIG_SECTION_DELIMETER)

    ERROR_STR_NEED_NAME_OR_REGEX = ("Deliverable class '%s' must define a name "
     "or a regex for the deliverable.")

    _gDeliverablesCache = {}

    ATTRIB_TYPE_CALLBACK = 0
    ATTRIB_TYPE_REGEX = 1
    ATTRIB_TYPE_VALUE = 2

    def __init__(self, deliverableFile, deliverableClass, config, *args,
     **kwargs):
        object.__init__(self, *args, **kwargs)

        if not os.path.isabs(deliverableFile):
            raise ValueError("Must provide absolute path to Deliverable "
             "constructor")
        elif not os.path.isfile(deliverableFile):
            raise ValueError("Non-existent file passed to Deliverable "
             "constructor")
        elif not IsValidDeliverableClass(config, deliverableClass):
            raise ValueError("Non-existent deliverable class passed to "
             "Deliverable constructor: %s" % deliverableClass)

        self.configSection = DeliverableSectionNameFromClass(deliverableClass)
        self.deliverableClass = deliverableClass
        self.file = deliverableFile
        self.regex = None
        self.name = None
        self.filterAttributes = None
        self.attributes = []
        self.attributeHandlers = {}

        deliverableSectionItems = config.GetSectionItems(self.configSection)

        if 'name' in deliverableSectionItems:
            self.name = config.SectionGet(self.configSection, 'name').strip()

        if 'regex' in deliverableSectionItems:
            self.regex = config.SectionGet(self.configSection, 'regex').strip()

        if self.regex is None and self.name is None:
            raise ConfigSpecError(self.ERROR_STR_NEED_NAME_OR_REGEX %
             (deliverableClass))

        if 'attributes' in deliverableSectionItems:
            self.attributes = config.SectionGet(self.configSection,
             'attributes', list)

        for attr in self.attributes:
            attributeValue = None
            attributeType = None

            if ('attrib_%s_handler' % (attr)) in deliverableSectionItems:
                attributeType = Deliverable.ATTRIB_TYPE_CALLBACK
                attributeValue = config.SectionGet(self.configSection,
                 'attrib_%s_handler' % (attr))
            elif ('attrib_%s_regex' % (attr)) in deliverableSectionItems:
                attributeType = Deliverable.ATTRIB_TYPE_REGEX
                attributeValue = config.SectionGet(self.configSection,
                 'attrib_%s_regex' % (attr))
            elif ('attrib_%s_value' % (attr)) in deliverableSectionItems:
                attributeType = Deliverable.ATTRIB_TYPE_VALUE
                attributeValue = config.SectionGet(self.configSection,
                 'attrib_%s_value' % (attr))
            else:
                raise ConfigSpecError("Deliverable class '%s' defines "
                 "attribute '%s', but doesn't define handler for it." %
                 (deliverableClass, attr))

            attributeHandlerDescriptor = {}
            attributeHandlerDescriptor['type'] = attributeType

            if attributeType == Deliverable.ATTRIB_TYPE_CALLBACK:
                try:
                    attributeHandlerDescriptor['handler'] = ImportModule(
                     attributeValue)
                except NameError, ex:
                    raise ConfigSpecError("Deliverable class '%s' defines an "
                     "attribute callback handler for attribute '%s', but the "
                     "callback is undefined: %s" % (self.deliverableClass, attr,
                     str(ex)))
            elif (attributeType == Deliverable.ATTRIB_TYPE_REGEX or
             attributeType == Deliverable.ATTRIB_TYPE_VALUE):
                attributeHandlerDescriptor['handler'] = attributeValue
            else:
                assert False, "Unknown attribute handler type: %s" % (
                 attributeType)

            self.attributeHandlers[attr] = attributeHandlerDescriptor

        if 'filter_attributes' in deliverableSectionItems:
            self.filterAttributes = config.SectionGet(self.configSection,
             'filter_attributes', list)

        if self.filterAttributes is not None:
            for fa in self.filterAttributes:
                if fa not in self.attributes:
                    raise ConfigSpecError("Deliverable class '%s' defines "
                     "invalid filter attribute '%s'" % (deliverableClass, fa))

    def __rep__(self):
        return "<class %s: %s (%s)>" % (self.__class__, self.GetName(),
         self.GetLocation())

    def __str__(self):
        return self.GetLocation()

    def GetName(self):
        return self.deliverableClass

    def GetLocation(self):
        return self.file

    def GetFileName(self):
        return os.path.basename(self.GetLocation())

    def GetRegex(self):
        return self.regex

    def GetFilterAttributes(self):
        if self.filterAttributes is None:
            return None
        else:
            return tuple(self.filterAttributes)

    def GetAttributes(self):
        return tuple(self.attributes)

    def GetAttribute(self, attribute):
        if attribute not in self.attributes:
            raise ValueError("Deliverable class '%s' has no attribute '%s" %
             (self.GetName(), attribute))

        handlerType = self.attributeHandlers[attribute]['type']

        if handlerType == Deliverable.ATTRIB_TYPE_VALUE:
            return self.attributeHandlers[attribute]['handler']
        elif handlerType == Deliverable.ATTRIB_TYPE_REGEX:
            attribMatch = re.search(
             self.attributeHandlers[attribute]['handler'], self.GetFileName())

            if attribMatch is None:
                return None
            elif len(attribMatch.groups()) == 1:
                return attribMatch.group(1)
            else:
                return attribMatch.groups()

        elif handlerType == Deliverable.ATTRIB_TYPE_CALLBACK:
            return self.attributeHandlers[attribute]['handler'](self)
        else:
            assert False, "Unknown attribute handler type: %s" % (handlerType)

def FindDeliverables(deliverableDir, config):
    if not os.path.isdir(deliverableDir):
        raise ValueError("Invalid deliverable directory: %s" % (deliverableDir))

    if Deliverable._gDeliverablesCache.has_key(deliverableDir):
        return None

    deliverables = []
    deliverableSections = GetDeliverableSections(config)

    ignoreUndefinedDeliverables = True
    try:
        ignoreUndefinedDeliverables = config.Get(
         'ignore_undefined_deliverables', bool)
    except ConfigSpecError, ex:
        if ex.GetDetails() != ConfigSpecError.NO_OPTION_ERROR:
            raise ex

    for root, dirs, files in os.walk(deliverableDir):
        for f in files:
            #print "Looking at: %s" % (os.path.join(root, f))
            deliverableDescList = []
            for section in deliverableSections:
                delivRegex = None
                delivName = None
                matchType = None
                subclassType = None

                sectionItems = config.GetSectionItems(section)

                if 'name' in sectionItems:
                    delivName = config.SectionGet(section, 'name').strip()
                    matchType = 'name'
                elif 'regex' in sectionItems:
                    delivRegex = config.SectionGet(section, 'regex').strip()
                    matchType = 'regex'
                else:
                    raise ConfigSpecError(
                     Deliverable.ERROR_STR_NEED_NAME_OR_REGEX %
                     DeliverableClassFromSectionName(section))

                #print "f is %s, name is %s, regex is %s" % (f, delivName, delivRegex)
                if ((delivName is not None and f == delivName) or
                 (delivRegex is not None and re.search(delivRegex, f))):

                    if 'subclass' in sectionItems:
                      subclassType = config.SectionGet(section,
                        'subclass').strip()

                    delivClassDescription = { 'type': matchType,
                                              'subclass' : subclassType,
                                              'class' : DeliverableClassFromSectionName(section),
                                              'file' : os.path.join(root, f),
                    }

                    deliverableDescList.append(delivClassDescription)

            if len(deliverableDescList) == 0:
                if not ignoreUndefinedDeliverables:
                    assert False, "Should be a release framework error."
                else:
                    continue

            if len(deliverableDescList) == 1:
                delivDesc = deliverableDescList[0]

                if delivDesc['subclass'] is not None:
                    try:
                        subclassParts = delivDesc['subclass'].split('.')
                        mod = __import__('.'.join(subclassParts[:-1]))
                        for comp in subclassParts[1:]:
                            mod = getattr(mod, comp)

                        newDelivObj = mod(delivDesc['file'], delivDesc['class'],
                         config)

                    except NameError, ex:
                        raise ConfigSpecError("subclass error %s" % (ex))
                else:
                    newDelivObj = Deliverable(delivDesc['file'],
                     delivDesc['class'], config)

                deliverables.append(newDelivObj)

            else:
                matchedClassList = []
                fileLoc = deliverableDescList[0]['file']
                for delivDesc in deliverableDescList:
                    assert fileLoc == delivDesc['file'], ("Deliverable file "
                     "name mismatch (%s vs %s)?" % (fileLoc, delivDesc['file']))

                    matchedClassList.append("%s (matched via %s)" % (
                     delivDesc['class'], delivDesc['type']))

                raise ConfigSpecError("More than one deliverable class for "
                 "the file %s: %s" % (fileLoc, ', '.join(matchedClassList)))

    Deliverable._gDeliverablesCache[deliverableDir] = tuple(deliverables)
    return len(tuple(deliverables))

def GetAllDeliverables(deliverableDir=None):
    if deliverableDir is not None:
        if not Deliverable._gDeliverablesCache.has_key(deliverableDir):
            raise ValueError("Directory %s has not been scanned for "
             "deliverables yet; use FindDeliverables()" % (deliverableDir))

        return tuple(Deliverable._gDeliverablesCache[deliverableDir])
    else:
        cacheKeys = Deliverable._gDeliverablesCache.keys()
        if len(cacheKeys) == 0:
            raise ValueError("No deliverables found yet; prime cache with "
             "FindDeliverables()")

        allDeliverables = []

        for dDirs in cacheKeys:
            for deliv in Deliverable._gDeliverablesCache[dDirs]:
                allDeliverables.append(deliv)

        return tuple(allDeliverables)

def GetDeliverables(deliverableClass, deliverableDir=None):
    filterArgs = deliverableClass.split(':')
    filterArgsLen = len(filterArgs)
    filteredDeliverableList = []

    # Process the static filters, given in the config file
    for deliv in GetAllDeliverables(deliverableDir):
        staticFilters = deliv.GetName().split(':')
        staticFilterLen = len(staticFilters)

        filterNdx = 0

        skipThisDeliv = False
        while filterNdx < filterArgsLen and filterNdx < staticFilterLen:
            if filterArgs[filterNdx] != staticFilters[filterNdx]:
                skipThisDeliv = True
                break

            filterNdx += 1

        if skipThisDeliv:
            continue

        # If we've parsed all of the filter arguments, we're done
        if filterNdx == filterArgsLen:
            filteredDeliverableList.append(deliv)
            continue

        dynamicFilters = deliv.GetFilterAttributes()

        if dynamicFilters is None and filterNdx < filterArgsLen:
            raise ValueError("GetDeliverables passed filter '%s' for a "
             "deliverable class that defines no filter attributes" %
             ':'.join(filterArgs[filterNdx:]))

        dynamicFilterLen = len(dynamicFilters)

        while filterNdx < filterArgsLen:
            dynNdx = filterNdx - staticFilterLen
            assert dynNdx >= 0, "Invalid (negative) dynamic filter index."
            if dynNdx >= dynamicFilterLen:
                availableFilters = staticFilters[1:] + list(dynamicFilters)
                availableFiltersStr = ', '.join(availableFilters)
                filterCount = len(availableFilters)
                if filterCount > 1:
                    pluralFilters = "s"
                else:
                    pluralFilters = ""

                raise ValueError("GetDeliverables passed extra filter '%s' "
                 "for deliverable %s; %s defines %d filter%s: %s." % (
                 ':'.join(filterArgs[filterNdx:]), deliv.GetName(),
                 deliv.GetName(), filterCount, pluralFilters,
                 availableFiltersStr))

            if (deliv.GetAttribute(dynamicFilters[dynNdx]) !=
             filterArgs[filterNdx]):
                skipThisDeliv = True
                break

            filterNdx += 1

        if skipThisDeliv:
            continue

        filteredDeliverableList.append(deliv)

    return filteredDeliverableList

def GetDeliverable(deliverableClass, deliverableDir=None):
    possibleDelivs = GetDeliverables(deliverableClass, deliverableDir)

    if len(possibleDelivs) == 0:
        return None
    elif len(possibleDelivs) > 1:
        raise ConfigSpecError("More than one deliverable matched for "
         "deliverable class %s: %s" % (deliverableClass,
         ', '.join(list(x.GetLocation() for x in possibleDelivs))))
    else:
        return possibleDelivs[0]

def FlushDeliverableCache(deliverableDir=None):
    if deliverableDir is None:
        Deliverable._gDeliverablesCache.clear()
    else:
        try:
            del Deliverable._gDeliverablesCache[deliverableDir]
        except KeyError:
            raise ValueError("Deliverable directory %s not in cache" %
             (deliverableDir))

def GetDeliverableSections(config):
    retSections = []

    for section in config.GetSectionList():
        if IsDeliverableSectionName(section):
            retSections.append(section)

    return retSections

def DeliverableClassFromSectionName(sectionName):
    return re.sub('^%s' % (Deliverable.DELIVERABLE_CONFIG_PREFIX), '',
     sectionName)

def DeliverableSectionNameFromClass(delivClass):
    return Deliverable.DELIVERABLE_CONFIG_PREFIX + delivClass

def IsDeliverableSectionName(sectionName):
    return sectionName.startswith(Deliverable.DELIVERABLE_CONFIG_PREFIX)

def IsValidDeliverableClass(config, delivClass):
    try:
        sectionItems = config.GetSectionItems(DeliverableSectionNameFromClass(
         delivClass))
    except ValueError:
        return False

    return 'name' in sectionItems or 'regex' in sectionItems
<commit_msg>Exception details were converted to a property.<commit_after>
import copy
import os
import re
import types

from quickrelease.config import ConfigSpecError, ConfigSpec
from quickrelease.utils import ImportModule

class Deliverable(object):
    DELIVERABLE_SECTION_PREFIX = 'deliverable'
    DELIVERABLE_CONFIG_PREFIX = (DELIVERABLE_SECTION_PREFIX +
     ConfigSpec.CONFIG_SECTION_DELIMETER)

    ERROR_STR_NEED_NAME_OR_REGEX = ("Deliverable class '%s' must define a name "
     "or a regex for the deliverable.")

    _gDeliverablesCache = {}

    ATTRIB_TYPE_CALLBACK = 0
    ATTRIB_TYPE_REGEX = 1
    ATTRIB_TYPE_VALUE = 2

    def __init__(self, deliverableFile, deliverableClass, config, *args,
     **kwargs):
        object.__init__(self, *args, **kwargs)

        if not os.path.isabs(deliverableFile):
            raise ValueError("Must provide absolute path to Deliverable "
             "constructor")
        elif not os.path.isfile(deliverableFile):
            raise ValueError("Non-existent file passed to Deliverable "
             "constructor")
        elif not IsValidDeliverableClass(config, deliverableClass):
            raise ValueError("Non-existent deliverable class passed to "
             "Deliverable constructor: %s" % deliverableClass)

        self.configSection = DeliverableSectionNameFromClass(deliverableClass)
        self.deliverableClass = deliverableClass
        self.file = deliverableFile
        self.regex = None
        self.name = None
        self.filterAttributes = None
        self.attributes = []
        self.attributeHandlers = {}

        deliverableSectionItems = config.GetSectionItems(self.configSection)

        if 'name' in deliverableSectionItems:
            self.name = config.SectionGet(self.configSection, 'name').strip()

        if 'regex' in deliverableSectionItems:
            self.regex = config.SectionGet(self.configSection, 'regex').strip()

        if self.regex is None and self.name is None:
            raise ConfigSpecError(self.ERROR_STR_NEED_NAME_OR_REGEX %
             (deliverableClass))

        if 'attributes' in deliverableSectionItems:
            self.attributes = config.SectionGet(self.configSection,
             'attributes', list)

        for attr in self.attributes:
            attributeValue = None
            attributeType = None

            if ('attrib_%s_handler' % (attr)) in deliverableSectionItems:
                attributeType = Deliverable.ATTRIB_TYPE_CALLBACK
                attributeValue = config.SectionGet(self.configSection,
                 'attrib_%s_handler' % (attr))
            elif ('attrib_%s_regex' % (attr)) in deliverableSectionItems:
                attributeType = Deliverable.ATTRIB_TYPE_REGEX
                attributeValue = config.SectionGet(self.configSection,
                 'attrib_%s_regex' % (attr))
            elif ('attrib_%s_value' % (attr)) in deliverableSectionItems:
                attributeType = Deliverable.ATTRIB_TYPE_VALUE
                attributeValue = config.SectionGet(self.configSection,
                 'attrib_%s_value' % (attr))
            else:
                raise ConfigSpecError("Deliverable class '%s' defines "
                 "attribute '%s', but doesn't define handler for it." %
                 (deliverableClass, attr))

            attributeHandlerDescriptor = {}
            attributeHandlerDescriptor['type'] = attributeType

            if attributeType == Deliverable.ATTRIB_TYPE_CALLBACK:
                try:
                    attributeHandlerDescriptor['handler'] = ImportModule(
                     attributeValue)
                except NameError, ex:
                    raise ConfigSpecError("Deliverable class '%s' defines an "
                     "attribute callback handler for attribute '%s', but the "
                     "callback is undefined: %s" % (self.deliverableClass, attr,
                     str(ex)))
            elif (attributeType == Deliverable.ATTRIB_TYPE_REGEX or
             attributeType == Deliverable.ATTRIB_TYPE_VALUE):
                attributeHandlerDescriptor['handler'] = attributeValue
            else:
                assert False, "Unknown attribute handler type: %s" % (
                 attributeType)

            self.attributeHandlers[attr] = attributeHandlerDescriptor

        if 'filter_attributes' in deliverableSectionItems:
            self.filterAttributes = config.SectionGet(self.configSection,
             'filter_attributes', list)

        if self.filterAttributes is not None:
            for fa in self.filterAttributes:
                if fa not in self.attributes:
                    raise ConfigSpecError("Deliverable class '%s' defines "
                     "invalid filter attribute '%s'" % (deliverableClass, fa))

    def __rep__(self):
        return "<class %s: %s (%s)>" % (self.__class__, self.GetName(),
         self.GetLocation())

    def __str__(self):
        return self.GetLocation()

    def GetName(self):
        return self.deliverableClass

    def GetLocation(self):
        return self.file

    def GetFileName(self):
        return os.path.basename(self.GetLocation())

    def GetRegex(self):
        return self.regex

    def GetFilterAttributes(self):
        if self.filterAttributes is None:
            return None
        else:
            return tuple(self.filterAttributes)

    def GetAttributes(self):
        return tuple(self.attributes)

    def GetAttribute(self, attribute):
        if attribute not in self.attributes:
            raise ValueError("Deliverable class '%s' has no attribute '%s" %
             (self.GetName(), attribute))

        handlerType = self.attributeHandlers[attribute]['type']

        if handlerType == Deliverable.ATTRIB_TYPE_VALUE:
            return self.attributeHandlers[attribute]['handler']
        elif handlerType == Deliverable.ATTRIB_TYPE_REGEX:
            attribMatch = re.search(
             self.attributeHandlers[attribute]['handler'], self.GetFileName())

            if attribMatch is None:
                return None
            elif len(attribMatch.groups()) == 1:
                return attribMatch.group(1)
            else:
                return attribMatch.groups()

        elif handlerType == Deliverable.ATTRIB_TYPE_CALLBACK:
            return self.attributeHandlers[attribute]['handler'](self)
        else:
            assert False, "Unknown attribute handler type: %s" % (handlerType)

def FindDeliverables(deliverableDir, config):
    if not os.path.isdir(deliverableDir):
        raise ValueError("Invalid deliverable directory: %s" % (deliverableDir))

    if Deliverable._gDeliverablesCache.has_key(deliverableDir):
        return None

    deliverables = []
    deliverableSections = GetDeliverableSections(config)

    ignoreUndefinedDeliverables = True
    try:
        ignoreUndefinedDeliverables = config.Get(
         'ignore_undefined_deliverables', bool)
    except ConfigSpecError, ex:
        if ex.details != ConfigSpecError.NO_OPTION_ERROR:
            raise ex

    for root, dirs, files in os.walk(deliverableDir):
        for f in files:
            #print "Looking at: %s" % (os.path.join(root, f))
            deliverableDescList = []
            for section in deliverableSections:
                delivRegex = None
                delivName = None
                matchType = None
                subclassType = None

                sectionItems = config.GetSectionItems(section)

                if 'name' in sectionItems:
                    delivName = config.SectionGet(section, 'name').strip()
                    matchType = 'name'
                elif 'regex' in sectionItems:
                    delivRegex = config.SectionGet(section, 'regex').strip()
                    matchType = 'regex'
                else:
                    raise ConfigSpecError(
                     Deliverable.ERROR_STR_NEED_NAME_OR_REGEX %
                     DeliverableClassFromSectionName(section))

                #print "f is %s, name is %s, regex is %s" % (f, delivName, delivRegex)
                if ((delivName is not None and f == delivName) or
                 (delivRegex is not None and re.search(delivRegex, f))):

                    if 'subclass' in sectionItems:
                      subclassType = config.SectionGet(section,
                        'subclass').strip()

                    delivClassDescription = { 'type': matchType,
                                              'subclass' : subclassType,
                                              'class' : DeliverableClassFromSectionName(section),
                                              'file' : os.path.join(root, f),
                    }

                    deliverableDescList.append(delivClassDescription)

            if len(deliverableDescList) == 0:
                if not ignoreUndefinedDeliverables:
                    assert False, "Should be a release framework error."
                else:
                    continue

            if len(deliverableDescList) == 1:
                delivDesc = deliverableDescList[0]

                if delivDesc['subclass'] is not None:
                    try:
                        subclassParts = delivDesc['subclass'].split('.')
                        mod = __import__('.'.join(subclassParts[:-1]))
                        for comp in subclassParts[1:]:
                            mod = getattr(mod, comp)

                        newDelivObj = mod(delivDesc['file'], delivDesc['class'],
                         config)

                    except NameError, ex:
                        raise ConfigSpecError("subclass error %s" % (ex))
                else:
                    newDelivObj = Deliverable(delivDesc['file'],
                     delivDesc['class'], config)

                deliverables.append(newDelivObj)

            else:
                matchedClassList = []
                fileLoc = deliverableDescList[0]['file']
                for delivDesc in deliverableDescList:
                    assert fileLoc == delivDesc['file'], ("Deliverable file "
                     "name mismatch (%s vs %s)?" % (fileLoc, delivDesc['file']))

                    matchedClassList.append("%s (matched via %s)" % (
                     delivDesc['class'], delivDesc['type']))

                raise ConfigSpecError("More than one deliverable class for "
                 "the file %s: %s" % (fileLoc, ', '.join(matchedClassList)))

    Deliverable._gDeliverablesCache[deliverableDir] = tuple(deliverables)
    return len(tuple(deliverables))

def GetAllDeliverables(deliverableDir=None):
    if deliverableDir is not None:
        if not Deliverable._gDeliverablesCache.has_key(deliverableDir):
            raise ValueError("Directory %s has not been scanned for "
             "deliverables yet; use FindDeliverables()" % (deliverableDir))

        return tuple(Deliverable._gDeliverablesCache[deliverableDir])
    else:
        cacheKeys = Deliverable._gDeliverablesCache.keys()
        if len(cacheKeys) == 0:
            raise ValueError("No deliverables found yet; prime cache with "
             "FindDeliverables()")

        allDeliverables = []

        for dDirs in cacheKeys:
            for deliv in Deliverable._gDeliverablesCache[dDirs]:
                allDeliverables.append(deliv)

        return tuple(allDeliverables)

def GetDeliverables(deliverableClass, deliverableDir=None):
    filterArgs = deliverableClass.split(':')
    filterArgsLen = len(filterArgs)
    filteredDeliverableList = []

    # Process the static filters, given in the config file
    for deliv in GetAllDeliverables(deliverableDir):
        staticFilters = deliv.GetName().split(':')
        staticFilterLen = len(staticFilters)

        filterNdx = 0

        skipThisDeliv = False
        while filterNdx < filterArgsLen and filterNdx < staticFilterLen:
            if filterArgs[filterNdx] != staticFilters[filterNdx]:
                skipThisDeliv = True
                break

            filterNdx += 1

        if skipThisDeliv:
            continue

        # If we've parsed all of the filter arguments, we're done
        if filterNdx == filterArgsLen:
            filteredDeliverableList.append(deliv)
            continue

        dynamicFilters = deliv.GetFilterAttributes()

        if dynamicFilters is None and filterNdx < filterArgsLen:
            raise ValueError("GetDeliverables passed filter '%s' for a "
             "deliverable class that defines no filter attributes" %
             ':'.join(filterArgs[filterNdx:]))

        dynamicFilterLen = len(dynamicFilters)

        while filterNdx < filterArgsLen:
            dynNdx = filterNdx - staticFilterLen
            assert dynNdx >= 0, "Invalid (negative) dynamic filter index."
            if dynNdx >= dynamicFilterLen:
                availableFilters = staticFilters[1:] + list(dynamicFilters)
                availableFiltersStr = ', '.join(availableFilters)
                filterCount = len(availableFilters)
                if filterCount > 1:
                    pluralFilters = "s"
                else:
                    pluralFilters = ""

                raise ValueError("GetDeliverables passed extra filter '%s' "
                 "for deliverable %s; %s defines %d filter%s: %s." % (
                 ':'.join(filterArgs[filterNdx:]), deliv.GetName(),
                 deliv.GetName(), filterCount, pluralFilters,
                 availableFiltersStr))

            if (deliv.GetAttribute(dynamicFilters[dynNdx]) !=
             filterArgs[filterNdx]):
                skipThisDeliv = True
                break

            filterNdx += 1

        if skipThisDeliv:
            continue

        filteredDeliverableList.append(deliv)

    return filteredDeliverableList

def GetDeliverable(deliverableClass, deliverableDir=None):
    possibleDelivs = GetDeliverables(deliverableClass, deliverableDir)

    if len(possibleDelivs) == 0:
        return None
    elif len(possibleDelivs) > 1:
        raise ConfigSpecError("More than one deliverable matched for "
         "deliverable class %s: %s" % (deliverableClass,
         ', '.join(list(x.GetLocation() for x in possibleDelivs))))
    else:
        return possibleDelivs[0]

def FlushDeliverableCache(deliverableDir=None):
    if deliverableDir is None:
        Deliverable._gDeliverablesCache.clear()
    else:
        try:
            del Deliverable._gDeliverablesCache[deliverableDir]
        except KeyError:
            raise ValueError("Deliverable directory %s not in cache" %
             (deliverableDir))

def GetDeliverableSections(config):
    retSections = []

    for section in config.GetSectionList():
        if IsDeliverableSectionName(section):
            retSections.append(section)

    return retSections

def DeliverableClassFromSectionName(sectionName):
    return re.sub('^%s' % (Deliverable.DELIVERABLE_CONFIG_PREFIX), '',
     sectionName)

def DeliverableSectionNameFromClass(delivClass):
    return Deliverable.DELIVERABLE_CONFIG_PREFIX + delivClass

def IsDeliverableSectionName(sectionName):
    return sectionName.startswith(Deliverable.DELIVERABLE_CONFIG_PREFIX)

def IsValidDeliverableClass(config, delivClass):
    try:
        sectionItems = config.GetSectionItems(DeliverableSectionNameFromClass(
         delivClass))
    except ValueError:
        return False

    return 'name' in sectionItems or 'regex' in sectionItems
<|endoftext|>


<commit_before>from django.conf.urls.defaults import patterns, include, url
from armstrong.core.arm_wells.views import QuerySetBackedWellView
from armstrong.core.arm_sections.views import SimpleSectionView, SectionFeed
from armstrong.apps.articles.models import Article
from armstrong.apps.articles.views import ArticleFeed
from django.views.generic.list_detail import object_detail
from django.views.generic import TemplateView
# ADMIN_BASE is the base URL for your Armstrong admin.  It is highly
# recommended that you change this to a different URL unless you enforce a
# strict password-strength policy for your users.
ADMIN_BASE = "admin"

# Comment the next two lines out to disable the admin:
from armstrong import hatband as admin
admin.autodiscover()

from .utils import get_url_for_model

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', '{{ project_name }}.views.home', name='home'),
    # url(r'^{{ project_name }}/', include('{{ project_name }}.foo.urls')),

    # Comment the admin/doc line below to disable admin documentation:
    url(r'^%s/doc/' % ADMIN_BASE, include('django.contrib.admindocs.urls')),

    # Comment the next line to disable the admin:
    url(r'^%s/' % ADMIN_BASE, include(admin.site.urls)),

    # {% if demo %}
    url(r'^$',
            QuerySetBackedWellView.as_view(well_title='front_page',
                                           template_name="front_page.html",
                                           queryset=Article.published.all(),
                                    ),
            name='front_page'),
    # {% else %}
    # Below is an example well view that might be used to display a well named
    # 'front_page' allowing for placement of content on the home page. The view
    # will work after # running `armstrong loaddata fixtures/initial_data.json`
    #
    #url(r'^$', QuerySetBackedWellView.as_view(well_title='front_page',
    #                                          template_name="front_page.html",
    #                                          queryset=Article.published.all()),
    #        name='front_page'),
    #
    # Load the Armstrong "success" page by default
    url(r'^$', TemplateView.as_view(template_name="index.html")),
    # {% endif %}

    url(r'^section/(?P<full_slug>[-\w/]+)',
            SimpleSectionView.as_view(template_name='section.html'),
            name='section_view'),
    url(r'^feed/section/(?P<full_slug>[-\w/]+)',
            SectionFeed(section_view='section_view'),
            name='section_feed'),

    url(r'^feed/all',
            ArticleFeed(title='Demo site articles',
                        link='/',
                        queryset=Article.objects.all()),
            name='all_articles_feed'),

    url(r'^article/(?P<slug>[-\w]+)/', object_detail, {
                        'queryset':Article.published.all().select_subclasses(),
                        'template_name':'article.html',
                        'slug_field':'slug',
                    },
            name='article_detail'),
)

# Uncomment the following two lines if you want to expose our default API
# from .api import urlpatterns as api_urlpatterns
# urlpatterns += api_urlpatterns
<commit_msg>serve media root<commit_after>from django.conf.urls.defaults import patterns, include, url
from armstrong.core.arm_wells.views import QuerySetBackedWellView
from armstrong.core.arm_sections.views import SimpleSectionView, SectionFeed
from armstrong.apps.articles.models import Article
from armstrong.apps.articles.views import ArticleFeed
from django.views.generic.list_detail import object_detail
from django.views.generic import TemplateView
from django.conf import settings
# ADMIN_BASE is the base URL for your Armstrong admin.  It is highly
# recommended that you change this to a different URL unless you enforce a
# strict password-strength policy for your users.
ADMIN_BASE = "admin"

# Comment the next two lines out to disable the admin:
from armstrong import hatband as admin
admin.autodiscover()

from .utils import get_url_for_model

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', '{{ project_name }}.views.home', name='home'),
    # url(r'^{{ project_name }}/', include('{{ project_name }}.foo.urls')),

    # Comment the admin/doc line below to disable admin documentation:
    url(r'^%s/doc/' % ADMIN_BASE, include('django.contrib.admindocs.urls')),

    # Comment the next line to disable the admin:
    url(r'^%s/' % ADMIN_BASE, include(admin.site.urls)),

    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT}),
    # {% if demo %}
    url(r'^$',
            QuerySetBackedWellView.as_view(well_title='front_page',
                                           template_name="front_page.html",
                                           queryset=Article.published.all(),
                                    ),
            name='front_page'),
    # {% else %}
    # Below is an example well view that might be used to display a well named
    # 'front_page' allowing for placement of content on the home page. The view
    # will work after # running `armstrong loaddata fixtures/initial_data.json`
    #
    #url(r'^$', QuerySetBackedWellView.as_view(well_title='front_page',
    #                                          template_name="front_page.html",
    #                                          queryset=Article.published.all()),
    #        name='front_page'),
    #
    # Load the Armstrong "success" page by default
    url(r'^$', TemplateView.as_view(template_name="index.html")),
    # {% endif %}

    url(r'^section/(?P<full_slug>[-\w/]+)',
            SimpleSectionView.as_view(template_name='section.html'),
            name='section_view'),
    url(r'^feed/section/(?P<full_slug>[-\w/]+)',
            SectionFeed(section_view='section_view'),
            name='section_feed'),

    url(r'^feed/all',
            ArticleFeed(title='Demo site articles',
                        link='/',
                        queryset=Article.objects.all()),
            name='all_articles_feed'),

    url(r'^article/(?P<slug>[-\w]+)/', object_detail, {
                        'queryset':Article.published.all().select_subclasses(),
                        'template_name':'article.html',
                        'slug_field':'slug',
                    },
            name='article_detail'),
)

# Uncomment the following two lines if you want to expose our default API
# from .api import urlpatterns as api_urlpatterns
# urlpatterns += api_urlpatterns
<|endoftext|>


<commit_before>import itertools
from dune.xt.codegen import typeid_to_typedef_name, la_backends

grids = ['Yasp2Grid']
try:
    if cache['dune-alugrid']:
        grids.append('AluSimplex2dGridType')
        # 'AluConform2dGridType', diabled due to https://gitlab.dune-project.org/extensions/dune-alugrid/issues/17
except KeyError:
    pass
casenames = ['AO2013TestCase', 'ER2007TestCase', 'ESV2007TestCase', 'MixedBoundaryTestCase']
try:
    if bool(cache['DXT_ENABLE_LARGE_TESTS']):
        casenames.append('Spe10Model1TestCase')
except KeyError:
    pass

space_backends = ['fem', 'pdelab']

testcases = ['Dune::GDT::LinearElliptic::{}<{}>'.format(c, g) for c, g in itertools.product(casenames, grids)]

def filter(casename):
    if 'Spe10Model1TestCase' not in casename:
        return True
    return 'YaspGrid' in casename
permutations = itertools.product(testcases, space_backends, la_backends(cache))
permutations = [(t, s, l, typeid_to_typedef_name('{}_{}_{}'.format(t, s, l))) for t, s, l in permutations if filter(t)]
<commit_msg>[test.linearelliptic.swipdg] drop useless filter<commit_after>import itertools
from dune.xt.codegen import typeid_to_typedef_name, la_backends

grids = ['Yasp2Grid']
try:
    if cache['dune-alugrid']:
        grids.append('AluSimplex2dGridType')
        # 'AluConform2dGridType', diabled due to https://gitlab.dune-project.org/extensions/dune-alugrid/issues/17
except KeyError:
    pass
casenames = ['AO2013TestCase', 'ER2007TestCase', 'ESV2007TestCase', 'MixedBoundaryTestCase']
try:
    if bool(cache['DXT_ENABLE_LARGE_TESTS']):
        casenames.append('Spe10Model1TestCase')
except KeyError:
    pass

space_backends = ['fem', 'pdelab']

testcases = ['Dune::GDT::LinearElliptic::{}<{}>'.format(c, g) for c, g in itertools.product(casenames, grids)]

def filter(casename):
    return True
permutations = itertools.product(testcases, space_backends, la_backends(cache))
permutations = [(t, s, l, typeid_to_typedef_name('{}_{}_{}'.format(t, s, l))) for t, s, l in permutations if filter(t)]
<|endoftext|>