15
15
from . import hamlpy
16
16
from . import nodes as hamlpynodes
17
17
18
- try :
19
- str = unicode
20
- except NameError :
21
- pass
22
18
23
19
class Options (object ):
24
20
CHECK_INTERVAL = 3 # in seconds
@@ -29,8 +25,9 @@ class Options(object):
29
25
# dict of compiled files [fullpath : timestamp]
30
26
compiled = dict ()
31
27
28
+
32
29
class StoreNameValueTagPair (argparse .Action ):
33
- def __call__ (self , parser , namespace , values , option_string = None ):
30
+ def __call__ (self , parser , namespace , values , option_string = None ):
34
31
tags = getattr (namespace , 'tags' , {})
35
32
if tags is None :
36
33
tags = {}
@@ -41,16 +38,24 @@ def __call__(self, parser, namespace, values, option_string = None):
41
38
setattr (namespace , 'tags' , tags )
42
39
43
40
arg_parser = argparse .ArgumentParser ()
44
- arg_parser .add_argument ('-v' , '--verbose' , help = 'Display verbose output' , action = 'store_true' )
45
- arg_parser .add_argument ('-i' , '--input-extension' , metavar = 'EXT' , default = '.hamlpy' , help = 'The file extensions to look for.' , type = str , nargs = '+' )
46
- arg_parser .add_argument ('-ext' , '--extension' , metavar = 'EXT' , default = Options .OUTPUT_EXT , help = 'The output file extension. Default is .html' , type = str )
47
- arg_parser .add_argument ('-r' , '--refresh' , metavar = 'S' , default = Options .CHECK_INTERVAL , help = 'Refresh interval for files. Default is {} seconds. Ignored if the --once flag is set.' .format (Options .CHECK_INTERVAL ), type = int )
48
- arg_parser .add_argument ('input_dir' , help = 'Folder to watch' , type = str )
49
- arg_parser .add_argument ('output_dir' , help = 'Destination folder' , type = str , nargs = '?' )
50
- arg_parser .add_argument ('--tag' , help = 'Add self closing tag. eg. --tag macro:endmacro' , type = str , nargs = 1 , action = StoreNameValueTagPair )
51
- arg_parser .add_argument ('--attr-wrapper' , dest = 'attr_wrapper' , type = str , choices = ('"' , "'" ), default = "'" , action = 'store' , help = "The character that should wrap element attributes. This defaults to ' (an apostrophe)." )
52
- arg_parser .add_argument ('--jinja' , help = 'Makes the necessary changes to be used with Jinja2.' , default = False , action = 'store_true' )
53
- arg_parser .add_argument ('--once' , help = 'Runs the compiler once and exits on completion. Returns a non-zero exit code if there were any compile errors.' , default = False , action = 'store_true' )
41
+ arg_parser .add_argument ('-v' , '--verbose' , help = 'Display verbose output' , action = 'store_true' )
42
+ arg_parser .add_argument ('-i' , '--input-extension' , metavar = 'EXT' , default = '.hamlpy' ,
43
+ help = 'The file extensions to look for.' , type = str , nargs = '+' )
44
+ arg_parser .add_argument ('-ext' , '--extension' , metavar = 'EXT' , default = Options .OUTPUT_EXT ,
45
+ help = 'The output file extension. Default is .html' , type = str )
46
+ arg_parser .add_argument ('-r' , '--refresh' , metavar = 'S' , default = Options .CHECK_INTERVAL , type = int ,
47
+ help = 'Refresh interval for files. Default is {} seconds. Ignored if the --once flag is set.' .format (Options .CHECK_INTERVAL ))
48
+ arg_parser .add_argument ('input_dir' , help = 'Folder to watch' , type = str )
49
+ arg_parser .add_argument ('output_dir' , help = 'Destination folder' , type = str , nargs = '?' )
50
+ arg_parser .add_argument ('--tag' , type = str , nargs = 1 , action = StoreNameValueTagPair ,
51
+ help = 'Add self closing tag. eg. --tag macro:endmacro' )
52
+ arg_parser .add_argument ('--attr-wrapper' , dest = 'attr_wrapper' , type = str , choices = ('"' , "'" ), default = "'" , action = 'store' ,
53
+ help = "The character that should wrap element attributes. This defaults to ' (an apostrophe)." )
54
+ arg_parser .add_argument ('--jinja' , default = False , action = 'store_true' ,
55
+ help = 'Makes the necessary changes to be used with Jinja2.' )
56
+ arg_parser .add_argument ('--once' , default = False , action = 'store_true' ,
57
+ help = 'Runs the compiler once and exits on completion. Returns a non-zero exit code if there were any compile errors.' )
58
+
54
59
55
60
def watched_extension (extension ):
56
61
"""Return True if the given extension is one of the watched extensions"""
@@ -59,9 +64,9 @@ def watched_extension(extension):
59
64
return True
60
65
return False
61
66
67
+
62
68
def watch_folder ():
63
69
"""Main entry point. Expects one or two arguments (the watch folder + optional destination folder)."""
64
- argv = sys .argv [1 :] if len (sys .argv ) > 1 else []
65
70
args = arg_parser .parse_args (sys .argv [1 :])
66
71
compiler_args = {}
67
72
@@ -95,9 +100,9 @@ def watch_folder():
95
100
hamlpynodes .TagNode .may_contain .pop (k , None )
96
101
97
102
hamlpynodes .TagNode .self_closing .update ({
98
- 'macro' : 'endmacro' ,
99
- 'call' : 'endcall' ,
100
- 'raw' : 'endraw'
103
+ 'macro' : 'endmacro' ,
104
+ 'call' : 'endcall' ,
105
+ 'raw' : 'endraw'
101
106
})
102
107
103
108
hamlpynodes .TagNode .may_contain ['for' ] = 'else'
@@ -120,6 +125,7 @@ def watch_folder():
120
125
# allow graceful exit (no stacktrace output)
121
126
sys .exit (0 )
122
127
128
+
123
129
def _watch_folder (folder , destination , compiler_args ):
124
130
"""Compares "modified" timestamps against the "compiled" dict, calls compiler
125
131
if necessary. Returns a tuple of the number of files hit and the number
@@ -140,17 +146,19 @@ def _watch_folder(folder, destination, compiler_args):
140
146
os .makedirs (compiled_folder )
141
147
142
148
compiled_path = _compiled_path (compiled_folder , filename )
143
- if not fullpath in compiled or compiled [fullpath ] < mtime or not os .path .isfile (compiled_path ):
149
+ if fullpath not in compiled or compiled [fullpath ] < mtime or not os .path .isfile (compiled_path ):
144
150
compiled [fullpath ] = mtime
145
151
total_files += 1
146
152
if not compile_file (fullpath , compiled_path , compiler_args ):
147
153
num_failed += 1
148
154
149
- return (total_files , num_failed )
155
+ return total_files , num_failed
156
+
150
157
151
158
def _compiled_path (destination , filename ):
152
159
return os .path .join (destination , filename [:filename .rfind ('.' )] + Options .OUTPUT_EXT )
153
160
161
+
154
162
def compile_file (fullpath , outfile_name , compiler_args ):
155
163
"""Calls HamlPy compiler. Returns True if the file was compiled and
156
164
written successfully."""
@@ -159,10 +167,10 @@ def compile_file(fullpath, outfile_name, compiler_args):
159
167
try :
160
168
if Options .DEBUG :
161
169
print ("Compiling %s -> %s" % (fullpath , outfile_name ))
162
- haml_lines = codecs .open (fullpath , 'r' , encoding = 'utf-8' ).read ().splitlines ()
170
+ haml_lines = codecs .open (fullpath , 'r' , encoding = 'utf-8' ).read ().splitlines ()
163
171
compiler = hamlpy .Compiler (compiler_args )
164
172
output = compiler .process_lines (haml_lines )
165
- outfile = codecs .open (outfile_name , 'w' , encoding = 'utf-8' )
173
+ outfile = codecs .open (outfile_name , 'w' , encoding = 'utf-8' )
166
174
outfile .write (output )
167
175
168
176
return True
@@ -173,5 +181,6 @@ def compile_file(fullpath, outfile_name, compiler_args):
173
181
174
182
return False
175
183
176
- if __name__ == '__main__' :
184
+
185
+ if __name__ == '__main__' : # pragma: no cover
177
186
watch_folder ()
0 commit comments