# HG changeset patch # User cmlenz # Date 1236809672 0 # Node ID ce5ad2d540b3534aa45912e456d47fb87c1fb830 # Parent 185b9b1784d1643997bca2e87ab0ec777d24104f Get rid of some Python 2.3 legacy that's no longer needed now that 2.4 is the baseline. diff --git a/genshi/core.py b/genshi/core.py --- a/genshi/core.py +++ b/genshi/core.py @@ -468,6 +468,7 @@ return Markup(unicode(self).join([escape(item, quotes=escape_quotes) for item in seq])) + @classmethod def escape(cls, text, quotes=True): """Create a Markup instance from a string and escape special characters it may contain (<, >, & and \"). @@ -501,7 +502,6 @@ if quotes: text = text.replace('"', '"') return cls(text) - escape = classmethod(escape) def unescape(self): """Reverse-escapes &, <, >, and \" and returns a `unicode` object. diff --git a/genshi/output.py b/genshi/output.py --- a/genshi/output.py +++ b/genshi/output.py @@ -129,6 +129,7 @@ ) SVG = SVG_FULL + @classmethod def get(cls, name): """Return the ``(name, pubid, sysid)`` tuple of the ``DOCTYPE`` declaration for the specified name. @@ -164,7 +165,6 @@ 'svg-basic': cls.SVG_BASIC, 'svg-tiny': cls.SVG_TINY }.get(name.lower()) - get = classmethod(get) class XMLSerializer(object): diff --git a/genshi/path.py b/genshi/path.py --- a/genshi/path.py +++ b/genshi/path.py @@ -65,12 +65,12 @@ DESCENDANT_OR_SELF = 'descendant-or-self' SELF = 'self' + @classmethod def forname(cls, name): """Return the axis constant for the given name, or `None` if no such axis was defined. """ return getattr(cls, name.upper().replace('-', '_'), None) - forname = classmethod(forname) ATTRIBUTE = Axis.ATTRIBUTE @@ -674,8 +674,13 @@ # Tokenizer - at_end = property(lambda self: self.pos == len(self.tokens) - 1) - cur_token = property(lambda self: self.tokens[self.pos]) + @property + def at_end(self): + return self.pos == len(self.tokens) - 1 + + @property + def cur_token(self): + return self.tokens[self.pos] def next_token(self): self.pos += 1 diff --git a/genshi/template/base.py b/genshi/template/base.py --- a/genshi/template/base.py +++ b/genshi/template/base.py @@ -13,12 +13,7 @@ """Basic templating functionality.""" -try: - from collections import deque -except ImportError: - class deque(list): - def appendleft(self, x): self.insert(0, x) - def popleft(self): return self.pop(0) +from collections import deque import os from StringIO import StringIO import sys @@ -422,12 +417,12 @@ if self.loader: self.filters.append(self._include) - def _get_stream(self): + @property + def stream(self): if not self._prepared: self._stream = list(self._prepare(self._stream)) self._prepared = True return self._stream - stream = property(_get_stream) def _parse(self, source, encoding): """Parse the template. diff --git a/genshi/template/directives.py b/genshi/template/directives.py --- a/genshi/template/directives.py +++ b/genshi/template/directives.py @@ -60,6 +60,7 @@ offset=-1): self.expr = self._parse_expr(value, template, lineno, offset) + @classmethod def attach(cls, template, stream, value, namespaces, pos): """Called after the template stream has been completely parsed. @@ -80,7 +81,6 @@ stream associated with the directive. """ return cls(value, template, namespaces, *pos[1:]), stream - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): """Apply the directive to the given stream. @@ -100,6 +100,7 @@ expr = ' "%s"' % self.expr.source return '<%s%s>' % (self.__class__.__name__, expr) + @classmethod def _parse_expr(cls, expr, template, lineno=-1, offset=-1): """Parses the given expression, raising a useful error message when a syntax error is encountered. @@ -112,7 +113,6 @@ cls.tagname) raise TemplateSyntaxError(err, template.filepath, lineno, offset + (err.offset or 0)) - _parse_expr = classmethod(_parse_expr) def _assignment(ast): @@ -202,6 +202,7 @@ """ __slots__ = [] + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: raise TemplateSyntaxError('The content directive can not be used ' @@ -209,7 +210,6 @@ *pos[1:]) expr = cls._parse_expr(value, template, *pos[1:]) return None, [stream[0], (EXPR, expr, pos), stream[-1]] - attach = classmethod(attach) class DefDirective(Directive): @@ -279,12 +279,12 @@ else: self.name = ast.id + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('function') return super(DefDirective, cls).attach(template, stream, value, namespaces, pos) - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): stream = list(stream) @@ -348,12 +348,12 @@ self.filename = template.filepath Directive.__init__(self, value, template, namespaces, lineno, offset) + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('each') return super(ForDirective, cls).attach(template, stream, value, namespaces, pos) - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): iterable = _eval_expr(self.expr, ctxt, **vars) @@ -389,12 +389,12 @@ """ __slots__ = [] + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('test') return super(IfDirective, cls).attach(template, stream, value, namespaces, pos) - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): value = _eval_expr(self.expr, ctxt, **vars) @@ -429,6 +429,7 @@ self.namespaces = namespaces or {} self.hints = hints or () + @classmethod def attach(cls, template, stream, value, namespaces, pos): hints = [] if type(value) is dict: @@ -441,7 +442,6 @@ value = value.get('path') return cls(value, template, frozenset(hints), namespaces, *pos[1:]), \ stream - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): ctxt._match_templates.append((self.path.test(ignore_context=True), @@ -481,6 +481,7 @@ """ __slots__ = [] + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('value') @@ -489,7 +490,6 @@ template.filepath, *pos[1:]) expr = cls._parse_expr(value, template, *pos[1:]) return None, [(EXPR, expr, pos)] - attach = classmethod(attach) class StripDirective(Directive): @@ -538,12 +538,12 @@ yield event return _apply_directives(_generate(), directives, ctxt, **vars) + @classmethod def attach(cls, template, stream, value, namespaces, pos): if not value: return None, stream[1:-1] return super(StripDirective, cls).attach(template, stream, value, namespaces, pos) - attach = classmethod(attach) class ChooseDirective(Directive): @@ -587,12 +587,12 @@ """ __slots__ = ['matched', 'value'] + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('test') return super(ChooseDirective, cls).attach(template, stream, value, namespaces, pos) - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): info = [False, bool(self.expr), None] @@ -616,12 +616,12 @@ Directive.__init__(self, value, template, namespaces, lineno, offset) self.filename = template.filepath + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('test') return super(WhenDirective, cls).attach(template, stream, value, namespaces, pos) - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): info = ctxt._choice_stack and ctxt._choice_stack[-1] @@ -710,12 +710,12 @@ raise TemplateSyntaxError(err, template.filepath, lineno, offset + (err.offset or 0)) + @classmethod def attach(cls, template, stream, value, namespaces, pos): if type(value) is dict: value = value.get('vars') return super(WithDirective, cls).attach(template, stream, value, namespaces, pos) - attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): frame = {} diff --git a/genshi/template/eval.py b/genshi/template/eval.py --- a/genshi/template/eval.py +++ b/genshi/template/eval.py @@ -281,6 +281,7 @@ class LookupBase(object): """Abstract base class for variable lookup implementations.""" + @classmethod def globals(cls, data): """Construct the globals dictionary to use as the execution context for the expression or suite. @@ -293,8 +294,8 @@ '_star_import_patch': _star_import_patch, 'UndefinedError': UndefinedError, } - globals = classmethod(globals) + @classmethod def lookup_name(cls, data, name): __traceback_hide__ = True val = data.get(name, UNDEFINED) @@ -303,8 +304,8 @@ if val is UNDEFINED: val = cls.undefined(name) return val - lookup_name = classmethod(lookup_name) + @classmethod def lookup_attr(cls, obj, key): __traceback_hide__ = True try: @@ -318,8 +319,8 @@ except (KeyError, TypeError): val = cls.undefined(key, owner=obj) return val - lookup_attr = classmethod(lookup_attr) + @classmethod def lookup_item(cls, obj, key): __traceback_hide__ = True if len(key) == 1: @@ -333,8 +334,8 @@ val = cls.undefined(key, owner=obj) return val raise - lookup_item = classmethod(lookup_item) + @classmethod def undefined(cls, key, owner=UNDEFINED): """Can be overridden by subclasses to specify behavior when undefined variables are accessed. @@ -343,7 +344,6 @@ :param owner: the owning object, if the variable is accessed as a member """ raise NotImplementedError - undefined = classmethod(undefined) class LenientLookup(LookupBase): @@ -369,11 +369,12 @@ :see: `StrictLookup` """ + + @classmethod def undefined(cls, key, owner=UNDEFINED): """Return an ``Undefined`` object.""" __traceback_hide__ = True return Undefined(key, owner=owner) - undefined = classmethod(undefined) class StrictLookup(LookupBase): @@ -397,11 +398,12 @@ ... UndefinedError: {} has no member named "nil" """ + + @classmethod def undefined(cls, key, owner=UNDEFINED): """Raise an ``UndefinedError`` immediately.""" __traceback_hide__ = True raise UndefinedError(key, owner=owner) - undefined = classmethod(undefined) def _parse(source, mode='eval'): diff --git a/genshi/template/loader.py b/genshi/template/loader.py --- a/genshi/template/loader.py +++ b/genshi/template/loader.py @@ -264,6 +264,7 @@ encoding=encoding, lookup=self.variable_lookup, allow_exec=self.allow_exec) + @staticmethod def directory(path): """Loader factory for loading templates from a local directory. @@ -279,8 +280,8 @@ return mtime == os.path.getmtime(filepath) return filepath, filename, fileobj, _uptodate return _load_from_directory - directory = staticmethod(directory) + @staticmethod def package(name, path): """Loader factory for loading templates from egg package data. @@ -294,8 +295,8 @@ filepath = os.path.join(path, filename) return filepath, filename, resource_stream(name, filepath), None return _load_from_package - package = staticmethod(package) + @staticmethod def prefixed(**delegates): """Factory for a load function that delegates to other loaders depending on the prefix of the requested template path. @@ -327,7 +328,7 @@ return filepath, filename, fileobj, uptodate raise TemplateNotFound(filename, delegates.keys()) return _dispatch_by_prefix - prefixed = staticmethod(prefixed) + directory = TemplateLoader.directory package = TemplateLoader.package