Ejemplo n.º 1
0
    def get(self):
        esta_manufacturer = common.GetManufacturer(self.ESTA_ID)
        self.response.headers['Content-Type'] = 'text/plain'

        # Can be '', 'esta', 'esta-draft', 'manufacturers' or 'manufacturer-names'
        pid_selection = self.request.get('pids')
        if pid_selection == 'manufacturer-names':
            manufacturers = []
            query = Manufacturer.all()
            query.order('name')
            for manufacturer in query:
                if manufacturer.esta_id in [self.ESTA_ID, 0xffff]:
                    continue

                self.WriteManufacturer(manufacturer, [])
        else:
            pids = Pid.all()

            if pid_selection == 'esta':
                pids.filter('draft =', False)
                pids.filter('manufacturer = ', esta_manufacturer)
            elif pid_selection == 'esta-draft':
                pids.filter('draft =', True)
                pids.filter('manufacturer = ', esta_manufacturer)
            elif pid_selection == "manufacturers":
                pids.filter('manufacturer != ', esta_manufacturer)

            manufacturers = {}
            esta_pids = []
            for pid in pids:
                if pid.manufacturer.esta_id == self.ESTA_ID:
                    esta_pids.append(pid)
                else:
                    # Build the hash of manufacturer pids by manufacturer
                    manufacturers.setdefault(pid.manufacturer.esta_id,
                                             []).append(pid)

            esta_pids.sort(key=lambda p: p.pid_id)
            for pid in esta_pids:
                self.WritePid(pid)

            for manufacturer_id in sorted(manufacturers):
                manufacturer_pids = manufacturers[manufacturer_id]
                manufacturer_pids.sort(key=lambda p: p.pid_id)
                self.WriteManufacturer(
                    manufacturers[manufacturer_id][0].manufacturer,
                    manufacturer_pids)

        query = LastUpdateTime.all()
        if pid_selection == 'manufacturer-names':
            query.filter('name = ', timestamp_keys.MANUFACTURERS)
        else:
            query.filter('name = ', timestamp_keys.PIDS)
        update_time = query.fetch(1)
        if update_time:
            timestamp = TimestampToInt(update_time[0].update_time)
            self.Write('version: %d' % timestamp)
Ejemplo n.º 2
0
    def _LookupManufacturer(self, manufacturer_id):
        """Lookup a Manufacturer entity by id and cache the result.

    Returns:
      The entity object, or None if not found.
    """
        if manufacturer_id not in self._manufacturers:
            self._manufacturers[manufacturer_id] = common.GetManufacturer(
                manufacturer_id)
        return self._manufacturers[manufacturer_id]
Ejemplo n.º 3
0
    def FilterByManufacturer(self, page, manufacturer):
        manufacturer_id = StringToInt(manufacturer)
        manufacturer = common.GetManufacturer(manufacturer_id)
        if manufacturer is None:
            return 0, []

        query = self.ProductType().all()
        query.filter('manufacturer = ', manufacturer.key())
        query.order('name')
        total = query.count()
        return total, query.fetch(None)
Ejemplo n.º 4
0
    def get(self):
        manufacturer = common.GetManufacturer(self.request.get('manufacturer'))
        if manufacturer is None:
            self.error(404)
            return

        output = {
            'name': manufacturer.name,
            'esta_id': manufacturer.esta_id,
        }
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Cache-Control'] = 'public; max-age=300;'
        self.response.out.write(json.dumps(output))
Ejemplo n.º 5
0
    def ApplyChanges(self, key, fields):
        responder_info = UploadedResponderInfo.get(key)
        if not responder_info:
            return 'Invalid key'
        logging.info(responder_info)

        fields_to_update = set(fields.split(','))
        logging.info(fields_to_update)
        data_dict = self.EvalData(responder_info.info)

        # same format as in data/model_data.py
        model_data = {'device_model': responder_info.device_model_id}

        if ('model_description' in fields_to_update
                and 'model_description' in data_dict):
            model_data['model_description'] = data_dict['model_description']
        if 'image_url' in fields_to_update and responder_info.image_url:
            model_data['image_url'] = responder_info.image_url
        if 'url' in fields_to_update and responder_info.link_url:
            model_data['link'] = responder_info.link_url
        if ('product_category' in fields_to_update
                and 'product_category' in data_dict):
            model_data['product_category'] = data_dict['product_category']

        if 'software_versions' in data_dict:
            for version_id, version_data in data_dict[
                    'software_versions'].iteritems():
                if type(version_id) in (int, long):
                    version_dict = self.BuildVersionDict(
                        version_id, version_data, fields_to_update)
                    if version_dict:
                        versions = model_data.setdefault(
                            'software_versions', {})
                        versions[version_id] = version_dict

        logging.info(model_data)

        manufacturer = common.GetManufacturer(responder_info.manufacturer_id)
        if manufacturer is None:
            return 'Invalid manufacturer_id %d' % responder_info.manufacturer_id

        updater = model_loader.ModelUpdater()
        was_added, was_changed = updater.UpdateResponder(
            manufacturer, model_data)
        logging.info('Was added %s' % was_added)
        logging.info('Was changed %s' % was_changed)

        # finally mark this one as done
        responder_info.processed = True
        responder_info.put()
        return ''
Ejemplo n.º 6
0
  def ClearManufacturerPids(self):
    manufacturer_id = StringToInt(self.request.get('manufacturer'))
    manufacturer = common.GetManufacturer(manufacturer_id)

    count = 0
    if manufacturer is not None:
      memcache.delete(memcache_keys.MANUFACTURER_PID_COUNT_KEY)
      memcache.delete(memcache_keys.MANUFACTURER_PID_COUNTS)

      for pid in manufacturer.pid_set:
        if pid.discovery_command:
          pid.discovery_command.delete()
        if pid.get_command:
          pid.get_command.delete()
        if pid.set_command:
          pid.set_command.delete()
        pid.delete()
        count += 1
    return 'Deleted %d PIDs' % count
Ejemplo n.º 7
0
    def LookupPid(self, pid_id, manufacturer_id):
        """
    Lookup a PID

    Returns:
      A tuple of (manufacturer, pid) data objects.
    """
        manufacturer = common.GetManufacturer(manufacturer_id)
        if manufacturer is None:
            raise UnknownManufacturerException(manufacturer_id)

        pid_query = Pid.all()
        pid_query.filter('pid_id = ', pid_id)
        pid_query.filter('manufacturer = ', manufacturer.key())

        result_set = pid_query.fetch(1)
        if result_set:
            return manufacturer, result_set[0]
        else:
            return manufacturer, None
Ejemplo n.º 8
0
    def LookupPIDFromRequest(self):
        pid_id = self.request.get('pid')
        try:
            pid_id = int(pid_id)
        except ValueError:
            return None

        manufacturer = common.GetManufacturer(self.request.get('manufacturer'))
        if manufacturer is None or pid_id is None:
            return None

        models = Pid.all()
        models.filter('pid_id = ', pid_id)
        models.filter('manufacturer = ', manufacturer.key())

        pid_data = models.fetch(1)
        if pid_data:
            return pid_data[0]
        else:
            return None
Ejemplo n.º 9
0
    def GetTemplateData(self):
        manufacturer = common.GetManufacturer(self.request.get('manufacturer'))
        if not manufacturer:
            self.error(404)
            return

        device_query = manufacturer.responder_set
        device_query.order('model_description')
        pid_query = manufacturer.pid_set
        pid_query.order('name')

        data = {
            'manufacturer': manufacturer.name,
            'id': manufacturer.esta_id,
            'url': manufacturer.link,
            'image_url': manufacturer.image_serving_url,
            'devices': device_query.fetch(None),
            'pids': pid_query.fetch(None),
        }
        for product in manufacturer.product_set:
            data.setdefault(product.class_name().lower(), []).append(product)
        return data
Ejemplo n.º 10
0
  def GetTemplateData(self):
    model = common.LookupModelFromRequest(self.request)
    if not model:
      self.error(404)
      return

    esta_manufacturer = common.GetManufacturer(0)
    if not esta_manufacturer:
      logging.error("Can't find ESTA manufacturer!")
      # 404 early here
      self.error(404)
      return

    self.response.headers['Content-Type'] = 'text/plain'

    # software version info
    software_versions = []
    for version_info in model.software_version_set:
      version_output = {
          'version_id': version_info.version_id,
          'label': version_info.label,
      }

      supported_parameters = version_info.supported_parameters
      if supported_parameters is not None:
        param_output = []
        for param in supported_parameters:
          param_dict = { 'id': param, }

          query = Pid.all()
          query.filter('pid_id =' , param)
          if param >= 0x8000:
            query.filter('manufacturer = ', model.manufacturer)
            param_dict['manufacturer_id'] = model.manufacturer.esta_id
          else:
            query.filter('manufacturer = ', esta_manufacturer)
            param_dict['manufacturer_id'] = esta_manufacturer.esta_id

          results = query.fetch(1)
          if results:
            param_dict['name'] = results[0].name
          param_output.append(param_dict)

        version_output['supported_parameters'] = sorted(
            param_output,
            key=lambda x: x['id'])
      software_versions.append(version_output)

      personalities = []
      for personality in version_info.personality_set:
        personality_info = {
          'description': personality.description,
          'index': personality.index,
        }
        if personality.slot_count is not None:
          personality_info['slot_count'] = personality.slot_count

        personalities.append(personality_info)

      if personalities:
        personalities.sort(key=lambda x: x['index'])
        version_output['personalities'] = personalities;

      sensors = []
      for sensor in version_info.sensor_set:
        sensor_info = {
            'description': sensor.description,
            'index': sensor.index,
            'type': sensor.type,
            'supports_recording': sensor.supports_recording,
            'supports_min_max': sensor.supports_min_max_recording,
        }
        type_str = SENSOR_TYPES.get(sensor.type)
        if type_str is not None:
          sensor_info['type_str'] = type_str
        sensors.append(sensor_info)

      if sensors:
        sensors.sort(key=lambda x: x['index'])
        version_output['sensors'] = sensors

    output = {
      'description': model.model_description,
      'manufacturer': model.manufacturer.name,
      'manufacturer_id': model.manufacturer.esta_id,
      'model_id': model.device_model_id,
      'software_versions': software_versions,
      'software_versions_json': json.dumps(software_versions),
    }
    # link and product_category are optional
    if model.link:
      output['link'] = model.link
    category = model.product_category
    if category:
      output['product_category'] = category.name
      output['product_category_id'] = category.id

    # tags
    for tag in model.tag_set:
      tags = output.setdefault('tags', [])
      tags.append(tag.tag.label)

    if model.image_data:
      serving_url = model.image_serving_url
      if not serving_url:
        serving_url = images.get_serving_url(model.image_data.key())
        model.image_serving_url = serving_url
        model.put()
      output['image_key'] = serving_url
    return output
Ejemplo n.º 11
0
 def Init(self):
   self._manufacturer_id = StringToInt(self.request.get('manufacturer'))
   self._manufacturer = common.GetManufacturer(self._manufacturer_id)
Ejemplo n.º 12
0
  def DiffResponder(self, responder, template_data):
    errors = []

    template_data['device_id'] = responder.device_model_id
    template_data['manufacturer_id'] = responder.manufacturer_id

    if responder.email_or_name:
      template_data['contact'] = responder.email_or_name

    manufacturer = common.GetManufacturer(responder.manufacturer_id)
    template_data['manufacturer'] = manufacturer
    if not manufacturer:
      return

    template_data['manufacturer_name'] = manufacturer.name
    existing_model = common.LookupModel(responder.manufacturer_id,
                                        responder.device_model_id)

    # build a dict for the existing responder
    existing_responder_dict = {}
    if existing_model is not None:
      existing_responder_dict = {
          'model_description': existing_model.model_description,
          'image_url': existing_model.image_url,
          'url': existing_model.link,
      }
      category = existing_model.product_category
      if category:
        existing_responder_dict['product_category'] = category.name

    # Build a dict for the new responder
    new_responder_dict = self.EvalData(responder.info)
    new_responder_dict['image_url'] = responder.image_url or None
    new_responder_dict['url'] = responder.link_url or None
    if 'product_category' in new_responder_dict:
      category = common.LookupProductCategory(
          new_responder_dict['product_category'])
      if category:
        new_responder_dict['product_category'] = category.name
      else:
        errors.append('Unknown product category %d' %
          new_responder_dict['product_category'])

    fields = [
        ('Model Description', 'model_description'),
        ('Image URL', 'image_url'),
        ('URL', 'url'),
        ('Product Category', 'product_category'),
    ]

    changed_fields, unchanged_fields = self.DiffProperties(fields,
        new_responder_dict, existing_responder_dict)

    template_data['changed_fields'] = changed_fields
    template_data['unchanged_fields'] = unchanged_fields

    # populate the model_description
    template_data['model_description'] = new_responder_dict.get(
        'model_description')
    if existing_model:
      template_data['model_description'] = existing_model.model_description

    # now work on the software versions
    new_software_versions = new_responder_dict.get('software_versions', {})
    if new_software_versions:
      versions = self.DiffVersions(new_software_versions, existing_model)
      template_data['versions'] = versions

    template_data.setdefault('errors', []).extend(errors)
Ejemplo n.º 13
0
 def __init__(self):
   self._pid_cache = {}
   self._esta = common.GetManufacturer(0)