def Process(self, rctext, rc_path): '''Processes 'rctext' and returns a resource tree corresponding to it. Args: rctext: complete text of the rc file rc_path: 'resource\resource.rc' Return: grit.node.base.Node subclass ''' if self.pre_process: preprocess_class = util.NewClassInstance( self.pre_process, preprocess_interface.PreProcessor) if preprocess_class: rctext = preprocess_class.Process(rctext, rc_path) else: self.Out( 'PreProcessing class could not be found. Skipping preprocessing.\n' ) # Start with a basic skeleton for the .grd file root = grd_reader.Parse( StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> <grit base_dir="." latest_public_release="0" current_release="1" source_lang_id="en"> <outputs /> <translations /> <release seq="1"> <includes /> <structures /> <messages /> </release> </grit>'''), util.dirname(rc_path)) includes = root.children[2].children[0] structures = root.children[2].children[1] messages = root.children[2].children[2] assert (isinstance(includes, grit.node.empty.IncludesNode) and isinstance(structures, grit.node.empty.StructuresNode) and isinstance(messages, grit.node.empty.MessagesNode)) self.AddIncludes(rctext, includes) self.AddStructures(rctext, structures, os.path.basename(rc_path)) self.AddMessages(rctext, messages) self.VerboseOut('Validating that all IDs are unique...\n') root.ValidateUniqueIds() self.ExtraVerboseOut('Done validating that all IDs are unique.\n') if self.post_process: postprocess_class = util.NewClassInstance( self.post_process, postprocess_interface.PostProcessor) if postprocess_class: root = postprocess_class.Process(rctext, rc_path, root) else: self.Out( 'PostProcessing class could not be found. Skipping postprocessing.\n' ) return root
def EndParsing(self): super(type(self), self).EndParsing() # Make the text (including placeholder references) and list of placeholders, # then strip and store leading and trailing whitespace and create the # tclib.Message() and a clique to contain it. text = '' placeholders = [] for item in self.mixed_content: if isinstance(item, types.StringTypes): text += item else: presentation = item.attrs['name'].upper() text += presentation ex = ' ' if len(item.children): ex = item.children[0].GetCdata() original = item.GetCdata() placeholders.append( tclib.Placeholder(presentation, original, ex)) m = _WHITESPACE.match(text) if m: self.ws_at_start = m.group('start') self.ws_at_end = m.group('end') text = m.group('body') self.shortcut_groups_ = self._SPLIT_RE.split( self.attrs['shortcut_groups']) self.shortcut_groups_ = [i for i in self.shortcut_groups_ if i != ''] description_or_id = self.attrs['desc'] if description_or_id == '' and 'name' in self.attrs: description_or_id = 'ID: %s' % self.attrs['name'] assigned_id = None if (self.attrs['use_name_for_id'] == 'true' and self.SatisfiesOutputCondition()): assigned_id = self.attrs['name'] message = tclib.Message(text=text, placeholders=placeholders, description=description_or_id, meaning=self.attrs['meaning'], assigned_id=assigned_id) self.clique = self.UberClique().MakeClique(message, self.IsTranslateable()) for group in self.shortcut_groups_: self.clique.AddToShortcutGroup(group) if self.attrs['custom_type'] != '': self.clique.SetCustomType( util.NewClassInstance(self.attrs['custom_type'], clique.CustomType)) elif self.attrs['validation_expr'] != '': self.clique.SetCustomType( clique.OneOffCustomType(self.attrs['validation_expr']))
def testNewClassInstance(self): # Test short class name with no fully qualified package name # Should fail, it is not supported by the function now (as documented) cls = util.NewClassInstance('grit.util.TestClassToLoad', TestBaseClassToLoad) self.failUnless(cls == None) # Test non existent class name cls = util.NewClassInstance('grit.util_unittest.NotExistingClass', TestBaseClassToLoad) self.failUnless(cls == None) # Test valid class name and valid base class cls = util.NewClassInstance('grit.util_unittest.TestClassToLoad', TestBaseClassToLoad) self.failUnless(isinstance(cls, TestBaseClassToLoad)) # Test valid class name with wrong hierarchy cls = util.NewClassInstance('grit.util_unittest.TestClassNoBase', TestBaseClassToLoad) self.failUnless(cls == None)
def InstallMessage(self, message): '''Sets this node's clique from a tclib.Message instance. Args: message: A tclib.Message. ''' self.clique = self.UberClique().MakeClique(message, self.IsTranslateable()) for group in self.shortcut_groups_: self.clique.AddToShortcutGroup(group) if self.attrs['custom_type'] != '': self.clique.SetCustomType(util.NewClassInstance(self.attrs['custom_type'], clique.CustomType)) elif self.attrs['validation_expr'] != '': self.clique.SetCustomType( clique.OneOffCustomType(self.attrs['validation_expr']))
def testCustomTypes(self): factory = clique.UberClique() message = tclib.Message(text='Bingo bongo') c = factory.MakeClique(message) try: c.SetCustomType(DummyCustomType()) self.fail() except: pass # expected case - 'Bingo bongo' does not start with 'jjj' message = tclib.Message(text='jjjBingo bongo') c = factory.MakeClique(message) c.SetCustomType(util.NewClassInstance( 'grit.clique_unittest.DummyCustomType', clique.CustomType)) translation = tclib.Translation(id=message.GetId(), text='Bilingo bolongo') c.AddTranslation(translation, 'fr') self.failUnless(c.MessageForLanguage('fr').GetRealContent().startswith('jjj'))