Skip to content

Commit

Permalink
Use relative imports
Browse files Browse the repository at this point in the history
This makes it possible to embed Cheetah in other packages without
risking conflict with other versions.
  • Loading branch information
CendioOssman committed Aug 25, 2021
1 parent 856329c commit b1c8cdc
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Cheetah/CacheRegion.py
Expand Up @@ -25,7 +25,7 @@
from md5 import md5

import time
import Cheetah.CacheStore
from . import CacheStore


class CacheItem(object):
Expand Down Expand Up @@ -105,7 +105,7 @@ def __init__(self, regionID, templateCacheIdPrefix='', cacheStore=None):
self._regionID = regionID
self._templateCacheIdPrefix = templateCacheIdPrefix
if not cacheStore:
cacheStore = Cheetah.CacheStore.MemoryCacheStore()
cacheStore = CacheStore.MemoryCacheStore()
self._cacheStore = cacheStore
self._wrappedCacheDataStore = _CacheDataStoreWrapper(
cacheStore, keyPrefix=templateCacheIdPrefix + ':' + regionID + ':')
Expand Down
16 changes: 8 additions & 8 deletions Cheetah/CheetahWrapper.py
Expand Up @@ -14,11 +14,11 @@
import pickle as pickle
from optparse import OptionParser

from Cheetah.Compiler import DEFAULT_COMPILER_SETTINGS
from Cheetah.Template import Template
from Cheetah.Utils.Misc import mkdirsWithPyInitFiles
from Cheetah.Version import Version
from Cheetah.compat import PY2
from .Compiler import DEFAULT_COMPILER_SETTINGS
from .Template import Template
from .Utils.Misc import mkdirsWithPyInitFiles
from .Version import Version
from .compat import PY2

optionDashesRE = re.compile(R"^-{1,2}")
moduleNameRE = re.compile(R"^[a-zA-Z_][a-zA-Z_0-9]*$")
Expand Down Expand Up @@ -227,7 +227,7 @@ def parseOpts(self, args):
if opts.print_settings:
print()
print('>> Available Cheetah compiler settings:')
from Cheetah.Compiler import _DEFAULT_COMPILER_SETTINGS
from .Compiler import _DEFAULT_COMPILER_SETTINGS
listing = _DEFAULT_COMPILER_SETTINGS
listing.sort(key=lambda _l: _l[0][0].lower())

Expand Down Expand Up @@ -266,7 +266,7 @@ def compile(self):
self._compileOrFill()

def fill(self):
from Cheetah.ImportHooks import install
from .ImportHooks import install
install()
self._compileOrFill()

Expand All @@ -291,7 +291,7 @@ def test(self):
os.remove(TEST_WRITE_FILENAME)
# @@MO: End ugly kludge.
import unittest
from Cheetah.Tests import Test
from .Tests import Test
verbosity = 1
if '-q' in self.testOpts:
verbosity = 0
Expand Down
44 changes: 25 additions & 19 deletions Cheetah/Compiler.py
Expand Up @@ -19,16 +19,16 @@
import copy
import codecs

from Cheetah.Version import Version, VersionTuple
from Cheetah.SettingsManager import SettingsManager
from Cheetah.Utils.Indenter import indentize # an undocumented preprocessor
from Cheetah import NameMapper
from Cheetah.Parser import Parser, ParseError, specialVarRE, \
from .Version import Version, VersionTuple
from .SettingsManager import SettingsManager
from .Utils.Indenter import indentize # an undocumented preprocessor
from . import NameMapper
from .Parser import Parser, ParseError, specialVarRE, \
STATIC_CACHE, REFRESH_CACHE, SET_GLOBAL, SET_MODULE, \
unicodeDirectiveRE, encodingDirectiveRE, escapedNewlineRE
from Cheetah.compat import PY2, string_type, unicode
from .compat import PY2, string_type, unicode

from Cheetah.NameMapper import valueForName, valueFromSearchList, \
from .NameMapper import valueForName, valueFromSearchList, \
valueFromFrameOrSearchList
VFFSL = valueFromFrameOrSearchList
VFSL = valueFromSearchList
Expand Down Expand Up @@ -1749,6 +1749,11 @@ def _setupCompilerState(self):
self._moduleHeaderLines = []
self._moduleDocStringLines = []
self._specialVars = {}
# we might have been moved to be a sub-package
if "__spec__" in globals():
package = __spec__.parent # noqa: F821 undefined name '__spec__'
else:
package = __package__
self._importStatements = [
"import sys",
"import os",
Expand All @@ -1760,18 +1765,19 @@ def _setupCompilerState(self):
"from os.path import getmtime, exists",
"import time",
"import types",
"from Cheetah.Version import MinCompatibleVersion as "
"RequiredCheetahVersion",
"from Cheetah.Version import MinCompatibleVersionTuple "
"as RequiredCheetahVersionTuple",
"from Cheetah.Template import Template",
"from Cheetah.DummyTransaction import *",
"from Cheetah.NameMapper import NotFound, "
"valueForName, valueFromSearchList, valueFromFrameOrSearchList",
"from Cheetah.CacheRegion import CacheRegion",
"import Cheetah.Filters as Filters",
"import Cheetah.ErrorCatchers as ErrorCatchers",
"from Cheetah.compat import unicode",
"from %s.Version import MinCompatibleVersion as "
"RequiredCheetahVersion" % package,
"from %s.Version import MinCompatibleVersionTuple "
"as RequiredCheetahVersionTuple" % package,
"from %s.Template import Template" % package,
"from %s.DummyTransaction import *" % package,
"from %s.NameMapper import NotFound, "
"valueForName, valueFromSearchList, "
"valueFromFrameOrSearchList" % package,
"from %s.CacheRegion import CacheRegion" % package,
"import %s.Filters as Filters" % package,
"import %s.ErrorCatchers as ErrorCatchers" % package,
"from %s.compat import unicode" % package,
]

self._importedVarNames = ['sys',
Expand Down
6 changes: 3 additions & 3 deletions Cheetah/DirectiveAnalyzer.py
Expand Up @@ -3,9 +3,9 @@
import os
import pprint

from Cheetah import Parser
from Cheetah import Compiler
from Cheetah import Template
from . import Parser
from . import Compiler
from . import Template


class Analyzer(Parser.Parser):
Expand Down
4 changes: 2 additions & 2 deletions Cheetah/Django.py
@@ -1,4 +1,4 @@
import Cheetah.Template
from . import Template


def render(template_file, **kwargs):
Expand All @@ -13,5 +13,5 @@ def render(template_file, **kwargs):
import django.http
import django.template.loader
source, loader = django.template.loader.find_template_source(template_file)
t = Cheetah.Template.Template(source, searchList=[kwargs])
t = Template.Template(source, searchList=[kwargs])
return django.http.HttpResponse(t.__str__())
2 changes: 1 addition & 1 deletion Cheetah/DummyTransaction.py
Expand Up @@ -9,7 +9,7 @@
'''

import logging
from Cheetah.compat import unicode
from .compat import unicode


class DummyResponseFailure(Exception):
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/ErrorCatchers.py
@@ -1,6 +1,6 @@

import time
from Cheetah.NameMapper import NotFound
from .NameMapper import NotFound


class Error(Exception):
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/FileUtils.py
Expand Up @@ -4,7 +4,7 @@
import os.path
import re
from tempfile import NamedTemporaryFile
from Cheetah.compat import string_type
from .compat import string_type


def _escapeRegexChars(
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/Filters.py
Expand Up @@ -6,7 +6,7 @@
#filter results in output filters Cheetah's $placeholders .
#transform results in a filter on the entirety of the output
'''
from Cheetah.compat import unicode
from .compat import unicode

# Additional entities WebSafe knows how to transform. No need to include
# '<', '>' or '&' since those will have been done already.
Expand Down
8 changes: 4 additions & 4 deletions Cheetah/ImportHooks.py
Expand Up @@ -22,10 +22,10 @@
from threading import RLock
import traceback

from Cheetah import ImportManager
from Cheetah.ImportManager import DirOwner
from Cheetah.Compiler import Compiler
from Cheetah.convertTmplPathToModuleName import convertTmplPathToModuleName
from . import ImportManager
from .ImportManager import DirOwner
from .Compiler import Compiler
from .convertTmplPathToModuleName import convertTmplPathToModuleName

_installed = False

Expand Down
2 changes: 1 addition & 1 deletion Cheetah/ImportManager.py
Expand Up @@ -20,7 +20,7 @@
import marshal
import py_compile
import sys
from Cheetah.compat import PY2, string_type, new_module, get_suffixes, \
from .compat import PY2, string_type, new_module, get_suffixes, \
load_module_from_file, RecursionError
if PY2:
import imp
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/LoadTemplate.py
@@ -1,6 +1,6 @@
import os
import sys
from Cheetah.ImportHooks import CheetahDirOwner
from .ImportHooks import CheetahDirOwner


def loadTemplateModule(templatePath, debuglevel=0):
Expand Down
4 changes: 2 additions & 2 deletions Cheetah/NameMapper.py
Expand Up @@ -143,7 +143,7 @@
import inspect
from pprint import pformat

from Cheetah.compat import PY2
from .compat import PY2
if PY2:
from collections import Mapping
else:
Expand Down Expand Up @@ -263,7 +263,7 @@ def hasName(obj, name):


try:
from Cheetah._namemapper import NotFound, valueForKey, valueForName, \
from ._namemapper import NotFound, valueForKey, valueForName, \
valueFromSearchList, valueFromFrameOrSearchList, valueFromFrame
C_VERSION = True
except Exception:
Expand Down
10 changes: 5 additions & 5 deletions Cheetah/Parser.py
Expand Up @@ -13,10 +13,10 @@
import types
import inspect

from Cheetah.SourceReader import SourceReader
from Cheetah.Unspecified import Unspecified
from Cheetah.Macros.I18n import I18n
from Cheetah.compat import PY2, string_type, unicode
from .SourceReader import SourceReader
from .Unspecified import Unspecified
from .Macros.I18n import I18n
from .compat import PY2, string_type, unicode
if PY2:
from tokenize import pseudoprog
else:
Expand Down Expand Up @@ -2390,7 +2390,7 @@ def eatDefMacro(self):
macroSrc,
'%end def'])

from Cheetah.Template import Template
from .Template import Template
templateAPIClass = self.setting('templateAPIClassForDefMacro',
default=Template)
compilerSettings = self.setting('compilerSettingsForDefMacro',
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/SettingsManager.py
Expand Up @@ -11,7 +11,7 @@
from StringIO import StringIO
except ImportError:
from io import StringIO
from Cheetah.compat import PY2
from .compat import PY2


numberRE = re.compile(Number)
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/SourceReader.py
@@ -1,7 +1,7 @@
"""SourceReader class for Cheetah's Parser and CodeGenerator
"""
import re
from Cheetah.compat import unicode
from .compat import unicode

EOLre = re.compile(r'[ \f\t]*(?:\r\n|\r|\n)')
EOLZre = re.compile(r'(?:\r\n|\r|\n|\Z)')
Expand Down
32 changes: 16 additions & 16 deletions Cheetah/Template.py
Expand Up @@ -24,24 +24,24 @@
import cgi # Used by .webInput() if the template is a CGI script.
import types

from Cheetah import ErrorCatchers # for placeholder tags
from Cheetah import Filters # the output filters
from Cheetah.CacheRegion import CacheRegion
from Cheetah.CacheStore import MemoryCacheStore # , MemcachedCacheStore
from Cheetah.Compiler import Compiler
from Cheetah.NameMapper import NotFound, valueFromSearchList
from Cheetah.Parser import ParseError, SourceReader
from . import ErrorCatchers # for placeholder tags
from . import Filters # the output filters
from .CacheRegion import CacheRegion
from .CacheStore import MemoryCacheStore # , MemcachedCacheStore
from .Compiler import Compiler
from .NameMapper import NotFound, valueFromSearchList
from .Parser import ParseError, SourceReader
# Base classes for Template
from Cheetah.Servlet import Servlet
from Cheetah.Unspecified import Unspecified
from Cheetah.Utils.Indenter import Indenter # and for placeholders
from Cheetah.Utils.WebInputMixin import _Converter, _lookup, \
from .Servlet import Servlet
from .Unspecified import Unspecified
from .Utils.Indenter import Indenter # and for placeholders
from .Utils.WebInputMixin import _Converter, _lookup, \
NonNumericInputError
from Cheetah.Version import MinCompatibleVersion
from Cheetah.Version import convertVersionStringToTuple, \
from .Version import MinCompatibleVersion
from .Version import convertVersionStringToTuple, \
MinCompatibleVersionTuple
from Cheetah.compat import PY2, string_type, unicode
from Cheetah.convertTmplPathToModuleName import convertTmplPathToModuleName
from .compat import PY2, string_type, unicode
from .convertTmplPathToModuleName import convertTmplPathToModuleName

try:
from threading import Lock
Expand Down Expand Up @@ -1514,7 +1514,7 @@ def runAsMainProgram(self):
Type 'python yourtemplate.py --help to see what it's capabable of.
"""

from Cheetah.TemplateCmdLineIface import CmdLineIface
from .TemplateCmdLineIface import CmdLineIface
CmdLineIface(templateObj=self).run()

##################################################
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/TemplateCmdLineIface.py
Expand Up @@ -9,7 +9,7 @@
from pickle import load
from json import load as jsonload

from Cheetah.Version import Version
from .Version import Version


class Error(Exception):
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/Utils/WebInputMixin.py
Expand Up @@ -2,7 +2,7 @@
transaction variables in bulk. See the docstring of webInput for full details.
"""

from Cheetah.Utils.Misc import useOrRaise
from .Misc import useOrRaise


class NonNumericInputError(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/Utils/htmlDecode.py
Expand Up @@ -4,7 +4,7 @@
"""

from Cheetah.Utils.htmlEncode import htmlCodesReversed
from .htmlEncode import htmlCodesReversed


def htmlDecode(s, codes=htmlCodesReversed):
Expand Down
2 changes: 1 addition & 1 deletion Cheetah/__init__.py
Expand Up @@ -13,4 +13,4 @@
https://cheetahtemplate.org/
'''

from Cheetah.Version import * # noqa
from .Version import * # noqa
2 changes: 1 addition & 1 deletion Cheetah/convertTmplPathToModuleName.py
@@ -1,6 +1,6 @@
import os.path
import string
from Cheetah.compat import unicode
from .compat import unicode

letters = None
try:
Expand Down

0 comments on commit b1c8cdc

Please sign in to comment.