def Run(self, globopt, args): args = self.Setup(globopt, args) if len(args) != 3: self.Out('This tool takes exactly three arguments:\n' ' 1. The path to the original RC file\n' ' 2. The path to the translated RC file\n' ' 3. The output file path.\n') return 2 grd = grd_reader.Parse(self.o.input, debug=self.o.extra_verbose) grd.RunGatherers(recursive=True) source_rc = util.WrapInputStream(file(args[0], 'r'), self.rc2grd.input_encoding) transl_rc = util.WrapInputStream(file(args[1], 'r'), self.rc2grd.input_encoding) translations = self.ExtractTranslations(grd, source_rc.read(), args[0], transl_rc.read(), args[1]) transl_rc.close() source_rc.close() output_file = util.WrapOutputStream(file(args[2], 'w')) self.WriteTranslations(output_file, translations.items()) output_file.close() self.Out('Wrote output file %s' % args[2])
def testAllInUberClique(self): resources = grd_reader.Parse( util.WrapInputStream( StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_dir="."> <release seq="3"> <messages> <message name="IDS_GREETING" desc="Printed to greet the currently logged in user"> Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing today? </message> </messages> <structures> <structure type="dialog" name="IDD_ABOUTBOX" encoding="utf-16" file="grit/testdata/klonk.rc" /> <structure type="tr_html" name="ID_HTML" file="grit/testdata/simple.html" /> </structures> </release> </grit>''')), util.PathFromRoot('.')) resources.SetOutputContext('en', {}) resources.RunGatherers(True) content_list = [] for clique_list in resources.UberClique().cliques_.values(): for clique in clique_list: content_list.append(clique.GetMessage().GetRealContent()) self.failUnless('Hello %s, how are you doing today?' in content_list) self.failUnless('Jack "Black" Daniels' in content_list) self.failUnless('Hello!' in content_list)
def Run(self, opts, args): args = self.ParseOptions(args) if len(args) != 1: print( 'This tool takes a single tool-specific argument, the path to the\n' '.rc file to process.') return 2 self.SetOptions(opts) path = args[0] out_path = os.path.join( util.dirname(path), os.path.splitext(os.path.basename(path))[0] + '.grd') rcfile = util.WrapInputStream(file(path, 'r'), self.input_encoding) rctext = rcfile.read() grd_text = unicode(self.Process(rctext, path)) rcfile.close() outfile = util.WrapOutputStream(file(out_path, 'w'), 'utf-8') outfile.write(grd_text) outfile.close() print 'Wrote output file %s.\nPlease check for TODO items in the file.' % out_path
def FromFileImpl(rc_file, extkey, encoding, type): '''Implementation of FromFile. Need to keep separate so we can have a FromFile in this class that has its type set to Section by default. ''' if isinstance(rc_file, types.StringTypes): rc_file = util.WrapInputStream(file(rc_file, 'r'), encoding) out = '' begin_count = 0 for line in rc_file.readlines(): if len(out) > 0 or (line.strip().startswith(extkey) and line.strip().split()[0] == extkey): out += line # we stop once we reach the END for the outermost block. begin_count_was = begin_count if len(out) > 0 and line.strip() == 'BEGIN': begin_count += 1 elif len(out) > 0 and line.strip() == 'END': begin_count -= 1 if begin_count_was == 1 and begin_count == 0: break if len(out) == 0: raise exception.SectionNotFound('%s in file %s' % (extkey, rc_file)) return type(out)
def FromFile(rc_file, extkey, encoding = 'cp1252'): '''Implementation of FromFile for resource types w/braces (not BEGIN/END) ''' if isinstance(rc_file, types.StringTypes): rc_file = util.WrapInputStream(file(rc_file, 'r'), encoding) out = '' begin_count = 0 openbrace_count = 0 for line in rc_file.readlines(): if len(out) > 0 or line.strip().startswith(extkey): out += line # we stop once balance the braces (could happen on one line) begin_count_was = begin_count if len(out) > 0: openbrace_count += line.count('{') begin_count += line.count('{') begin_count -= line.count('}') if ((begin_count_was == 1 and begin_count == 0) or (openbrace_count > 0 and begin_count == 0)): break if len(out) == 0: raise exception.SectionNotFound('%s in file %s' % (extkey, rc_file)) return RCData(out)
def FromFile(filename_or_stream, extkey=None, encoding='cp1252'): if isinstance(filename_or_stream, types.StringTypes): if util.IsVerbose(): print "MuppetStrings reading file %s, encoding %s" % ( filename_or_stream, encoding) filename_or_stream = util.WrapInputStream( file(filename_or_stream, 'r'), encoding) return MuppetStrings(filename_or_stream.read())
def testCorrectExceptionIfWrongEncodingOnResourceFile(self): '''This doesn't really belong in this unittest file, but what the heck.''' resources = grd_reader.Parse( util.WrapInputStream( StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_dir="."> <release seq="3"> <structures> <structure type="dialog" name="IDD_ABOUTBOX" file="grit/testdata/klonk.rc" /> </structures> </release> </grit>''')), util.PathFromRoot('.')) self.assertRaises(exception.SectionNotFound, resources.RunGatherers, True)
def FromFile(filename_or_stream, extkey, encoding): '''Creates a JSONLoader instance from a file or stream. Args: filename_or_stream: The source of JSON data. extkey: Unused, see interface.py. encoding: The encoding used in the JSON file. (Note that it should not contain localized strings.) Returns: The JSONLoader instance holding the JSON data unparsed. ''' if isinstance(filename_or_stream, types.StringTypes): filename_or_stream = \ util.WrapInputStream(file(filename_or_stream, 'rU'), encoding) return JsonLoader(filename_or_stream.read())
def FromFile(adm_file, ext_key=None, encoding='cp1252'): '''Loads the contents of 'adm_file' in encoding 'encoding' and creates an AdmGatherer instance that gathers from those contents. The 'ext_key' parameter is ignored. Args: adm_file: file('bingo.rc') | 'filename.rc' encoding: 'utf-8' Return: AdmGatherer(contents_of_file) ''' if isinstance(adm_file, types.StringTypes): adm_file = util.WrapInputStream(file(adm_file, 'r'), encoding) return AdmGatherer(adm_file.read())
def testStopAfter(self): input = u'''<?xml version="1.0" encoding="UTF-8"?> <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_dir="."> <outputs> <output filename="resource.h" type="rc_header" /> <output filename="resource.rc" lang="en-US" type="rc_all" /> </outputs> <release seq="3"> <includes> <include type="gif" name="ID_LOGO" file="images/logo.gif"/> </includes> </release> </grit>''' pseudo_file = util.WrapInputStream(StringIO.StringIO(input)) tree = grd_reader.Parse(pseudo_file, '.', stop_after='outputs') # only an <outputs> child self.failUnless(len(tree.children) == 1) self.failUnless(tree.children[0].name == 'outputs')
def FromFile(html, extkey=None, encoding='utf-8'): '''Creates a TrHtml object from the contents of 'html' which are decoded using 'encoding'. Returns a new TrHtml object, upon which Parse() has not been called. Args: html: file('') | 'filename.html' extkey: ignored encoding: 'utf-8' (note that encoding is ignored if 'html' is not a file name but instead an open file or file-like object) Return: TrHtml(text_of_file) ''' if isinstance(html, types.StringTypes): html = util.WrapInputStream(file(html, 'r'), encoding) doc = html.read() # Ignore the BOM character if the document starts with one. if len(doc) and doc[0] == u'\ufeff': doc = doc[1:] return TrHtml(doc)
def FromFile(filename_or_stream, extkey=None, encoding='cp1252'): if isinstance(filename_or_stream, types.StringTypes): print "IgoogleStrings %s %s" % (filename_or_stream, encoding) filename_or_stream = util.WrapInputStream( file(filename_or_stream, 'r'), encoding) return IgoogleStrings(filename_or_stream.read())
def FromFile(filename_or_stream, extkey=None, encoding='cp1252'): if isinstance(filename_or_stream, types.StringTypes): filename_or_stream = util.WrapInputStream( file(filename_or_stream, 'rb'), encoding) return TxtFile(filename_or_stream.read())