Example #1
0
    def endElement(self, name):
        if self.ignore_depth:
            self.ignore_depth -= 1
            return

        if name == 'part':
            partnode = self.stack[-1]
            partnode.started_inclusion = True
            # Add the contents of the sub-grd file as children of the <part> node.
            partname = os.path.join(self.dir, partnode.GetInputPath())
            # Check the GRDP file exists.
            if not os.path.exists(partname):
                raise exception.FileNotFound(partname)
            # Exceptions propagate to the handler in grd_reader.Parse().
            oldsource = self.source
            try:
                self.source = partname
                xml.sax.parse(partname, GrdPartContentHandler(self))
            finally:
                self.source = oldsource

        if self.debug:
            print("End parsing of element %s" % name)
        self.stack.pop().EndParsing()

        if name == self.stop_after:
            raise StopParsingException()
    def _FindInputFile(self):
        output_context = self.grd_node.GetRoot().output_context
        match = self.split_context_re_.match(output_context)
        if not match:
            raise exception.MissingMandatoryAttribute(
                'All <output> nodes must have an appropriate context attribute'
                ' (e.g. context="touch_200_percent")')
        req_layout, req_scale = match.group(1), int(match.group(2))

        layouts = [req_layout]
        if 'default' not in layouts:
            layouts.append('default')
        scales = [req_scale]
        try_low_res = self.grd_node.FindBooleanAttribute(
            'fallback_to_low_resolution', default=False, skip_self=False)
        if try_low_res and 100 not in scales:
            scales.append(100)
        for layout in layouts:
            for scale in scales:
                dir = '%s_%s_percent' % (layout, scale)
                path = os.path.join(dir, self.rc_file)
                if os.path.exists(self.grd_node.ToRealPath(path)):
                    return path, scale, req_scale
        # If we get here then the file is missing, so fail.
        dir = "%s_%s_percent" % (_MakeBraceGlob(layouts),
                                 _MakeBraceGlob(map(str, scales)))
        raise exception.FileNotFound(
            'Tried ' +
            self.grd_node.ToRealPath(os.path.join(dir, self.rc_file)))
    def _FindInputFile(self):
        output_context = self.grd_node.GetRoot().output_context
        match = self.split_context_re_.match(output_context)
        if not match:
            raise exception.MissingMandatoryAttribute(
                'All <output> nodes must have an appropriate context attribute'
                ' (e.g. context="touch_200_percent")')
        req_layout, req_scale = match.group(1), int(match.group(2))

        layouts = [req_layout]
        try_default_layout = self.grd_node.GetRoot().fallback_to_default_layout
        if try_default_layout and 'default' not in layouts:
            layouts.append('default')

        # TODO(tdanderson): Search in descending order of all image scales
        #                   instead of immediately falling back to 100.
        #                   See crbug.com/503643.
        scales = [req_scale]
        try_low_res = self.grd_node.FindBooleanAttribute(
            'fallback_to_low_resolution', default=False, skip_self=False)
        if try_low_res and 100 not in scales:
            scales.append(100)

        for layout in layouts:
            for scale in scales:
                dir = '%s_%s_percent' % (layout, scale)
                path = os.path.join(dir, self.rc_file)
                if os.path.exists(self.grd_node.ToRealPath(path)):
                    return path, scale, req_scale

        if not try_default_layout:
            # The file was not found in the specified output context and it was
            # explicitly indicated that the default context should not be searched
            # as a fallback, so return an empty path.
            return None, 100, req_scale

        # The file was found in neither the specified context nor the default
        # context, so raise an exception.
        dir = "%s_%s_percent" % (_MakeBraceGlob(layouts),
                                 _MakeBraceGlob(map(str, scales)))
        raise exception.FileNotFound(
            'Tried ' +
            self.grd_node.ToRealPath(os.path.join(dir, self.rc_file)))