Exemplo n.º 1
0
    def __init__(self, parser, path, source_resource=None):
        self.parser = parser
        self.path = path
        self.source_resource = source_resource
        self.entities = OrderedDict()  # Preserve entity order.

        # Bug 1193860: unescape quotes in some files
        self.escape_quotes_on = "mobile/android/base" in path and parser is DTDParser

        # Copy entities from the source_resource if it's available.
        if source_resource:
            for key, entity in source_resource.entities.items():
                self.entities[key] = copy_source_entity(entity)

        try:
            # Only uncomment MOZ_LANGPACK_CONTRIBUTORS if this is a .inc
            # file and a source resource (i.e. it has no source resource
            # itself).
            self.structure = parser.get_structure(
                read_file(
                    path,
                    uncomment_moz_langpack=parser is IncParser
                    and not source_resource,
                ))
        # Parse errors are handled gracefully by silme
        # No need to catch them here
        except OSError as err:
            # If the file doesn't exist, but we have a source resource,
            # we can keep going, we'll just not have any translations.
            if source_resource:
                return
            else:
                raise ParseError(err)

        comments = []
        current_order = 0
        for obj in self.structure:
            if isinstance(obj, silme.core.entity.Entity):

                if self.escape_quotes_on:
                    obj.value = unescape_quotes(obj.value)

                entity = SilmeEntity(obj, comments, current_order)
                self.entities[entity.key] = entity
                current_order += 1
                comments = []
            elif isinstance(obj, silme.core.structure.Comment):
                for comment in obj:
                    # Silme groups comments together, so we strip
                    # whitespace and split them up.
                    lines = str(comment).strip().split("\n")
                    comments += [line.strip() for line in lines]
Exemplo n.º 2
0
    def __init__(self, parser, path, source_resource=None):
        self.parser = parser
        self.path = path
        self.source_resource = source_resource
        self.entities = OrderedDict()  # Preserve entity order.

        # Bug 1193860: unescape quotes in some files
        self.escape_quotes_on = 'mobile/android/base' in path and parser is DTDParser

        # Copy entities from the source_resource if it's available.
        if source_resource:
            for key, entity in source_resource.entities.items():
                self.entities[key] = copy_source_entity(entity)

        try:
            # Only uncomment MOZ_LANGPACK_CONTRIBUTORS if this is a .inc
            # file and a source resource (i.e. it has no source resource
            # itself).
            self.structure = parser.get_structure(read_file(
                path,
                uncomment_moz_langpack=parser is IncParser and not source_resource
            ))
        except IOError:
            # If the file doesn't exist, but we have a source resource,
            # we can keep going, we'll just not have any translations.
            if source_resource:
                return
            else:
                raise

        comments = []
        current_order = 0
        for obj in self.structure:
            if isinstance(obj, silme.core.entity.Entity):

                if self.escape_quotes_on:
                    obj.value = unescape_quotes(obj.value)

                entity = SilmeEntity(obj, comments, current_order)
                self.entities[entity.key] = entity
                current_order += 1
                comments = []
            elif isinstance(obj, silme.core.structure.Comment):
                for comment in obj:
                    # Silme groups comments together, so we strip
                    # whitespace and split them up.
                    lines = text_type(comment).strip().split('\n')
                    comments += [line.strip() for line in lines]