FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python-CoffeeScript/coffeescript/__init__.py at master · doloopwhile/Python-CoffeeScript · GitHub
This repository was archived by the owner on Jan 1, 2021. It is now read-only.
doloopwhile
/
Python-CoffeeScript
Public archive
Notifications
You must be signed in to change notification settings
Fork
22
Star
82
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Python-CoffeeScript
/
coffeescript
/
__init__.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
140 lines (104 loc) · 3.89 KB
Breadcrumbs
Python-CoffeeScript
/
coffeescript
/
__init__.py
Copy path
File metadata and controls
executable file
·
140 lines (104 loc) · 3.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
# -*- coding: ascii -*-
from
__future__
import
division
,
unicode_literals
,
print_function
import
pkg_resources
# Copyright (c) 2011 Omoto Kenji
# Released under the MIT license. See `LICENSE` for details.
'''
Python CoffeeScript is a bridge to the JS CoffeeScript compiler.
A short example:
>>> import coffeescript
>>> print(coffeescript.compile('add = (a, b) -> a + b'))
(function() {
var add;
add = function(a, b) {
return a + b;
};
}).call(this);
'''
# A value of __spam__ have to be wrapped with str().
# It is because that __spam__ must be a instance of str() in 2.x and
# "xxxx" is a instance of unicode() due to unicode_literals.
__license__
=
str
(
"MIT License"
)
# The following __version__ is not DRY.
# Same information can be got from setup.py.
# However, __version__ should provited by __init__.py for user-friendliness.
# On the other hand, it is impossible that `from coffeescript import __version__`
# in spite of a dependency on execjs.
# i.e. the import in setup.py fails if execjs has not been installed yet.
__version__
=
str
(
"2.0.3"
)
__all__
=
str
(
'''
compile
compile_file
Compiler
EngineError
CompilationError
get_runtime
get_compiler_script
'''
).
split
()
import
io
import
execjs
EngineError
=
execjs
.
RuntimeError
CompilationError
=
execjs
.
ProgramError
try
:
_BaseString
=
basestring
except
NameError
:
_BaseString
=
(
str
,
bytes
)
class
Compiler
:
'''Wrapper of execution of CoffeeScript compiler script'''
def
__init__
(
self
,
compiler_script
,
runtime
):
'''compiler_script is a CoffeeScript compiler script in JavaScript.
runtime is a instance of execjs.Runtime.
'''
self
.
_compiler_script
=
compiler_script
self
.
_runtime
=
runtime
def
compile
(
self
,
script
,
bare
=
False
):
'''compile a CoffeeScript code to a JavaScript code.
if bare is True, then compile the JavaScript without the top-level
function safety wrapper (like the coffee command).
'''
if
not
hasattr
(
self
,
'_context'
):
self
.
_context
=
self
.
_runtime
.
compile
(
self
.
_compiler_script
)
return
self
.
_context
.
call
(
"CoffeeScript.compile"
,
script
, {
'bare'
:
bare
})
def
compile_file
(
self
,
filename
,
encoding
=
"utf-8"
,
bare
=
False
):
'''compile a CoffeeScript script file to a JavaScript code.
filename can be a list or tuple of filenames,
then contents of files are concatenated with line feeds.
if bare is True, then compile the JavaScript without the top-level
function safety wrapper (like the coffee command).
'''
if
isinstance
(
filename
,
_BaseString
):
filename
=
[
filename
]
scripts
=
[]
for
f
in
filename
:
with
io
.
open
(
f
,
encoding
=
encoding
)
as
fp
:
scripts
.
append
(
fp
.
read
())
return
self
.
compile
(
'
\n
\n
'
.
join
(
scripts
),
bare
=
bare
)
def
compile
(
script
,
bare
=
False
):
return
_default_compiler
().
compile
(
script
,
bare
=
bare
)
compile
.
__doc__
=
Compiler
.
compile
.
__doc__
def
compile_file
(
filename
,
encoding
=
"utf-8"
,
bare
=
False
):
return
_default_compiler
().
compile_file
(
filename
,
encoding
=
encoding
,
bare
=
bare
)
compile_file
.
__doc__
=
Compiler
.
compile_file
.
__doc__
def
get_compiler_script
():
'''returns a CoffeeScript compiler script in JavaScript.
which is used in coffeescript.compile() and coffeescript.compile_file()
'''
return
pkg_resources
.
resource_string
(
'coffeescript'
,
'coffeescript.js'
).
decode
(
'utf-8'
)
def
get_runtime
():
'''returns an appropriate instance of execjs.Runtime
which is used in coffeescript.compile() and coffeescript.compile_file()
'''
return
execjs
.
get
()
_compiler
=
None
def
_default_compiler
():
global
_compiler
if
_compiler
is
None
:
_compiler
=
Compiler
(
get_compiler_script
(),
get_runtime
()
)
return
_compiler
Back
|
FazBrowse Home
|
New Git URL