Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import os 

2import re 

3import tempfile 

4 

5from mako import exceptions 

6from mako.template import Template 

7 

8from .compat import get_current_bytecode_suffixes 

9from .compat import has_pep3147 

10from .compat import load_module_py 

11from .compat import load_module_pyc 

12from .compat import py35 

13from .exc import CommandError 

14 

15 

16def template_to_file(template_file, dest, output_encoding, **kw): 

17 template = Template(filename=template_file) 

18 try: 

19 output = template.render_unicode(**kw).encode(output_encoding) 

20 except: 

21 with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as ntf: 

22 ntf.write( 

23 exceptions.text_error_template() 

24 .render_unicode() 

25 .encode(output_encoding) 

26 ) 

27 fname = ntf.name 

28 raise CommandError( 

29 "Template rendering failed; see %s for a " 

30 "template-oriented traceback." % fname 

31 ) 

32 else: 

33 with open(dest, "wb") as f: 

34 f.write(output) 

35 

36 

37def coerce_resource_to_filename(fname): 

38 """Interpret a filename as either a filesystem location or as a package 

39 resource. 

40 

41 Names that are non absolute paths and contain a colon 

42 are interpreted as resources and coerced to a file location. 

43 

44 """ 

45 if not os.path.isabs(fname) and ":" in fname: 

46 import pkg_resources 

47 

48 fname = pkg_resources.resource_filename(*fname.split(":")) 

49 return fname 

50 

51 

52def pyc_file_from_path(path): 

53 """Given a python source path, locate the .pyc. 

54 

55 """ 

56 

57 if has_pep3147(): 

58 if py35: 

59 import importlib 

60 

61 candidate = importlib.util.cache_from_source(path) 

62 else: 

63 import imp 

64 

65 candidate = imp.cache_from_source(path) 

66 if os.path.exists(candidate): 

67 return candidate 

68 

69 # even for pep3147, fall back to the old way of finding .pyc files, 

70 # to support sourceless operation 

71 filepath, ext = os.path.splitext(path) 

72 for ext in get_current_bytecode_suffixes(): 

73 if os.path.exists(filepath + ext): 

74 return filepath + ext 

75 else: 

76 return None 

77 

78 

79def edit(path): 

80 """Given a source path, run the EDITOR for it""" 

81 

82 import editor 

83 

84 try: 

85 editor.edit(path) 

86 except Exception as exc: 

87 raise CommandError("Error executing editor (%s)" % (exc,)) 

88 

89 

90def load_python_file(dir_, filename): 

91 """Load a file from the given path as a Python module.""" 

92 

93 module_id = re.sub(r"\W", "_", filename) 

94 path = os.path.join(dir_, filename) 

95 _, ext = os.path.splitext(filename) 

96 if ext == ".py": 

97 if os.path.exists(path): 

98 module = load_module_py(module_id, path) 

99 else: 

100 pyc_path = pyc_file_from_path(path) 

101 if pyc_path is None: 

102 raise ImportError("Can't find Python file %s" % path) 

103 else: 

104 module = load_module_pyc(module_id, pyc_path) 

105 elif ext in (".pyc", ".pyo"): 

106 module = load_module_pyc(module_id, path) 

107 return module