import os
import yaml
import logging
from django.utils.translation import ungettext_lazy
from django.conf import settings


def help_max_length(max_length):
    return ungettext_lazy(
        u"Maximum length: {0} character",
        u"Maximum length: {0} characters",
        max_length).format(max_length)


class StreamEcho(object):
    def write(self, value):
        return value


def description_filename(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = os.path.join(settings.MEDIA_ROOT, 'job-output', 'job-%s' % job_id, 'description.yaml')
    if not os.path.exists(filename):
        logger.error("No description.yaml for job %s" % job_id)
        return None
    return filename


def description_data(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = description_filename(job_id)
    if not filename:
        return {}
    try:
        data = yaml.load(open(filename, 'r'))
    except yaml.YAMLError:
        logger.error("Unable to parse description for %s" % job_id)
        return {}
    return data

import os
import yaml
import logging
from django.utils.translation import ungettext_lazy
from django.conf import settings


def help_max_length(max_length):
    return ungettext_lazy(
        u"Maximum length: {0} character",
        u"Maximum length: {0} characters",
        max_length).format(max_length)


class StreamEcho(object):
    def write(self, value):
        return value


def description_filename(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = os.path.join(settings.MEDIA_ROOT, 'job-output', 'job-%s' % job_id, 'description.yaml')
    if not os.path.exists(filename):
        logger.error("No description.yaml for job %s" % job_id)
        return None
    return filename


def description_data(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = description_filename(job_id)
    if not filename:
        return {}
    try:
        data = yaml.load(open(filename, 'r'))
    except yaml.YAMLError:
        logger.error("Unable to parse description for %s" % job_id)
        return {}
    if not data:
        return {}
    return data

Return an empty dict if no data
Return an empty dict if no data

Avoids a HTTP500 on slow instances where the file
may be created before data is written, causing the
YAML parser to return None.

Change-Id: I13b92941f3e368839a9665fe3197c706babd9335

  import os
  import yaml
  import logging
  from django.utils.translation import ungettext_lazy
  from django.conf import settings
  
  
  def help_max_length(max_length):
      return ungettext_lazy(
          u"Maximum length: {0} character",
          u"Maximum length: {0} characters",
          max_length).format(max_length)
  
  
  class StreamEcho(object):
      def write(self, value):
          return value
  
  
  def description_filename(job_id):
      logger = logging.getLogger('lava_results_app')
      filename = os.path.join(settings.MEDIA_ROOT, 'job-output', 'job-%s' % job_id, 'description.yaml')
      if not os.path.exists(filename):
          logger.error("No description.yaml for job %s" % job_id)
          return None
      return filename
  
  
  def description_data(job_id):
      logger = logging.getLogger('lava_results_app')
      filename = description_filename(job_id)
      if not filename:
          return {}
      try:
          data = yaml.load(open(filename, 'r'))
      except yaml.YAMLError:
          logger.error("Unable to parse description for %s" % job_id)
          return {}
+     if not data:
+         return {}
      return data
  
Return an empty dict if no data
## Code Before:
import os
import yaml
import logging
from django.utils.translation import ungettext_lazy
from django.conf import settings


def help_max_length(max_length):
    return ungettext_lazy(
        u"Maximum length: {0} character",
        u"Maximum length: {0} characters",
        max_length).format(max_length)


class StreamEcho(object):
    def write(self, value):
        return value


def description_filename(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = os.path.join(settings.MEDIA_ROOT, 'job-output', 'job-%s' % job_id, 'description.yaml')
    if not os.path.exists(filename):
        logger.error("No description.yaml for job %s" % job_id)
        return None
    return filename


def description_data(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = description_filename(job_id)
    if not filename:
        return {}
    try:
        data = yaml.load(open(filename, 'r'))
    except yaml.YAMLError:
        logger.error("Unable to parse description for %s" % job_id)
        return {}
    return data

## Instruction:
Return an empty dict if no data
## Code After:
import os
import yaml
import logging
from django.utils.translation import ungettext_lazy
from django.conf import settings


def help_max_length(max_length):
    return ungettext_lazy(
        u"Maximum length: {0} character",
        u"Maximum length: {0} characters",
        max_length).format(max_length)


class StreamEcho(object):
    def write(self, value):
        return value


def description_filename(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = os.path.join(settings.MEDIA_ROOT, 'job-output', 'job-%s' % job_id, 'description.yaml')
    if not os.path.exists(filename):
        logger.error("No description.yaml for job %s" % job_id)
        return None
    return filename


def description_data(job_id):
    logger = logging.getLogger('lava_results_app')
    filename = description_filename(job_id)
    if not filename:
        return {}
    try:
        data = yaml.load(open(filename, 'r'))
    except yaml.YAMLError:
        logger.error("Unable to parse description for %s" % job_id)
        return {}
    if not data:
        return {}
    return data
