def locate(self, name): """Look in each directory for the file called 'name'.""" for dir in self.dirs: path = os.path.join(dir, name) if os.path.isfile(path): fr = open(path, 'r') fr = decodeFile(fr, path, self.fileCharEncoding) return fr return None
def testUTF8NoDeclarationWithBOM(self): content = textwrap.dedent( u"""\ // this is a file has a BOM trällölü """) encoding = 'utf-8' buf = StringIO('\xef\xbb\xbf' + content.encode(encoding)) fp = utils.decodeFile(buf, 'test.txt') result = fp.read() self.assert_(isinstance(result, unicode)) self.assertEqual(result, content)
def testUTF8DeclarationFirstLine(self): content = textwrap.dedent( u"""\ // coding: utf-8 trällölü """) encoding = 'utf-8' buf = StringIO(content.encode(encoding)) fp = utils.decodeFile(buf, 'test.txt') result = fp.read() self.assert_(isinstance(result, unicode)) self.assertEqual(result, content)
def loadTemplate(self, name, src): stream = None close_stream = False try: if isinstance(src, basestring): # src is a filename # Make sure the file exists. If it doesn't, return None, # so ST keeps looking in superGroups. If it exists then # subsequence errors should be treated as real errors. if os.path.isfile(src): close_stream = True stream = open(src, 'r') stream = decodeFile(stream, src) elif hasattr(src, 'read'): # src is a filelike object stream = decodeFile(src, '<template %r from buffer>' % name) else: raise TypeError( 'loadTemplate must be called with a file or filename' ) if stream is not None: # strip newlines etc.. from front/back since filesystem # may add newlines etc... template = stream.read().strip() if not template: self.error("no text in template '"+name+"'") return None return self.defineTemplate(name, template) finally: if stream is not None and close_stream: stream.close()
def testUTF8NoDeclarationButDefault(self): content = textwrap.dedent( u"""\ // this is a file has no declaration, // but I know it's utf-8 trällölü """) encoding = 'utf-8' buf = StringIO(content.encode(encoding)) fp = utils.decodeFile(buf, 'test.txt', 'utf-8') result = fp.read() self.assert_(isinstance(result, unicode)) self.assertEqual(result, content)
def testUTF8DeclarationThirdLine(self): content = textwrap.dedent( u"""\ // this is a file // oops, declaration on 3rd line // coding: utf-8 trällölü """) encoding = 'utf-8' buf = StringIO(content.encode(encoding)) fp = utils.decodeFile(buf, 'test.txt') try: fp.read() self.fail() except UnicodeDecodeError: pass
def __init__(self, name=None, rootDir=None, lexer=None, fileName=None, file=None, errors=None, superGroup=None): ## What is the group name # self.name = None ## Maps template name to StringTemplate object # self.templates = {} ## Maps map names to HashMap objects. This is the list of maps # defined by the user like typeInitMap ::= ["int":"0"] # self.maps = {} ## How to pull apart a template into chunks? self._templateLexerClass = None ## Under what directory should I look for templates? If None, # to look into the CLASSPATH for templates as resources. # self.rootDir = None ## Are we derived from another group? Templates not found in this # group will be searched for in the superGroup recursively. self._superGroup = None ## Keep track of all interfaces implemented by this group. self.interfaces = [] ## When templates are files on the disk, the refresh interval is used # to know when to reload. When a Reader is passed to the ctor, # it is a stream full of template definitions. The former is used # for web development, but the latter is most likely used for source # code generation for translators; a refresh is unlikely. Anyway, # I decided to track the source of templates in case such info is useful # in other situations than just turning off refresh interval. I just # found another: don't ever look on the disk for individual templates # if this group is a group file...immediately look into any super group. # If not in the super group, report no such template. # self.templatesDefinedInGroupFile = False ## Normally AutoIndentWriter is used to filter output, but user can # specify a new one. # self.userSpecifiedWriter = None self.debugTemplateOutput = False ## The set of templates to ignore when dumping start/stop debug strings self.noDebugStartStopStrings = None ## A Map<class,object> that allows people to register a renderer for # a particular kind of object to be displayed for any template in this # group. For example, a date should be formatted differently depending # on the locale. You can set Date.class to an object whose # str() method properly formats a Date attribute # according to locale. Or you can have a different renderer object # for each locale. # # These render objects are used way down in the evaluation chain # right before an attribute's str() method would normally be # called in ASTExpr.write(). # self.attributeRenderers = None ## Where to report errors. All string templates in this group # use this error handler by default. if errors is not None: self.listener = errors else: self.listener = DEFAULT_ERROR_LISTENER ## How long before tossing out all templates in seconds. # default: no refreshing from disk # self.refreshInterval = sys.maxint/1000 self.lastCheckedDisk = 0L if name is not None: assert isinstance(name, basestring) self.name = name assert rootDir is None or isinstance(rootDir, basestring) self.rootDir = rootDir self.lastCheckedDisk = time.time() StringTemplateGroup.nameToGroupMap[self.name] = self self.templateLexerClass = lexer assert superGroup is None or isinstance(superGroup, StringTemplateGroup) self.superGroup = superGroup if fileName is not None: if file is None: file = decodeFile(open(fileName, 'rb'), fileName) if file is not None: assert hasattr(file, 'read') self.templatesDefinedInGroupFile = True if lexer is not None: self.templateLexerClass = lexer else: self.templateLexerClass = AngleBracketTemplateLexer.Lexer assert superGroup is None or isinstance(superGroup, StringTemplateGroup) self.superGroup = superGroup self.parseGroup(file) assert self.name is not None StringTemplateGroup.nameToGroupMap[self.name] = self self.verifyInterfaceImplementations()
def testPlainASCIINoDeclaration(self): buf = StringIO("// nothing\n// to\n// see\n") fp = utils.decodeFile(buf, 'test.txt') result = fp.read() self.assert_(isinstance(result, unicode)) self.assertEqual(result, "// nothing\n// to\n// see\n")