def parse(self, input): """Parses the given file or file source string.""" try: if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' tmp_header_added = False # if isinstance(input, str) and '"Content-Type: text/plain; charset=' not in input[:200]: # input = basic_header + input # tmp_header_added = True self.units = [] self._cpo_store = cpo.pofile(input, noheader=True) self._build_self_from_cpo() del self._cpo_store if tmp_header_added: self.units = self.units[1:] except Exception, e: raise base.ParseError(e)
def parse(self, input): """parse the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): src = input.read() input.close() input = src if isinstance(input, bytes): # The JSON files should be UTF-8, but implementations # that parse JSON texts MAY ignore the presence of a byte order mark # rather than treating it as an error, see RFC7159 input, self.encoding = self.detect_encoding(input) try: self._file = json.loads(input, object_pairs_hook=OrderedDict) except ValueError as e: raise base.ParseError(e) for unit in self._extract_units(self._file, stop=self._filter): self.addunit(unit)
def parse(self, input): """parsese the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): tmsrc = input.read() input.close() input = tmsrc try: header_length = self._read_header(input) except: raise base.ParseError("Cannot parse header") lines = csv.DictReader(input.split( UtxDialect.lineterminator)[header_length:], fieldnames=self._fieldnames, dialect="utx") for line in lines: newunit = UtxUnit() newunit.dict = line self.addunit(newunit)
def parse(self, input): """parse the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): src = input.read() input.close() input = src if isinstance(input, bytes): input = input.decode('utf-8') try: self._file = json.loads(input, object_pairs_hook=OrderedDict) except ValueError as e: raise base.ParseError(e.message) for k, data, item, notes in self._extract_translatables( self._file, stop=self._filter): unit = self.UnitClass(data, item, notes) unit.setid(k) self.addunit(unit)
def parse(self, input): """parse the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): src = input.read() input.close() input = src if isinstance(input, str): input = BytesIO(input) try: self._file = json.load(input) except ValueError as e: raise base.ParseError(e.message) for k, data, ref, item in self._extract_translatables( self._file, stop=self._filter): unit = self.UnitClass(data, ref, item) unit.setid(k) self.addunit(unit)
def parse(self, input): """parse the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): src = input.read() input.close() input = src if isinstance(input, bytes): input = input.decode('utf-8') try: self._file = yaml.load(input, OrderedDictYAMLLoader) except yaml.YAMLError as e: raise base.ParseError( e.problem if hasattr(e, 'problem') else e.message) self._file = self.preprocess(self._file) for k, data in self._flatten(self._file): unit = self.UnitClass(data) unit.setid(k) self.addunit(unit)
def parse(self, input): """parses the given file or file source string""" if hasattr(input, "name"): self.filename = input.name elif not getattr(self, "filename", ""): self.filename = "" if hasattr(input, "read"): mosrc = input.read() input.close() input = mosrc (little,) = struct.unpack("<L", input[:4]) (big,) = struct.unpack(">L", input[:4]) if little == MO_MAGIC_NUMBER: endian = "<" elif big == MO_MAGIC_NUMBER: endian = ">" else: raise ValueError("This is not an MO file") ( magic, version_maj, version_min, lenkeys, startkey, startvalue, sizehash, offsethash, ) = struct.unpack("%sLHHiiiii" % endian, input[: (7 * 4)]) if version_maj >= 1: raise base.ParseError( """Unable to process version %d.%d MO files""" % (version_maj, version_min) ) for i in range(lenkeys): nextkey = startkey + (i * 2 * 4) nextvalue = startvalue + (i * 2 * 4) klength, koffset = struct.unpack( "%sii" % endian, input[nextkey : nextkey + (2 * 4)] ) vlength, voffset = struct.unpack( "%sii" % endian, input[nextvalue : nextvalue + (2 * 4)] ) source = input[koffset : koffset + klength] context = None if b"\x04" in source: context, source = source.split(b"\x04") # Still need to handle KDE comments if source == "": charset = re.search( b"charset=([^\\s]+)", input[voffset : voffset + vlength] ) if charset: self.encoding = charset.group(1) source = multistring([s.decode(self.encoding) for s in source.split(b"\0")]) target = multistring( [ s.decode(self.encoding) for s in input[voffset : voffset + vlength].split(b"\0") ] ) newunit = mounit(source) newunit.target = target if context is not None: newunit.msgctxt.append(context.decode(self.encoding)) self.addunit(newunit)