Lint various Python files.

This commit is contained in:
Nicola Soranzo
2015-07-13 00:13:21 +01:00
parent 2c61543d72
commit 43aa5fa482
5 changed files with 130 additions and 131 deletions
@@ -1,40 +1,39 @@
#!/usr/bin/env python
# Script requires handlebars compiler be installed; use node package manager
# Script requires handlebars compiler be installed; use node package manager
# to install handlebars.
"""%prog [options] [ specific .handlebars or .html template file to compile ]
Compiles handlebars templates into javascript for better load times and performance.
Handlebars compilation errors will print to stderr.
NOTE!: you will need node.js, npm, and handlebars (installed from npm) in order to compile templates
See http://handlebarsjs.com/precompilation.html for more info.
Should be called from the $GLX_ROOT/static/scripts/templates directory.
Should be called from the $GLX_ROOT/static/scripts/templates directory.
Compiled templates will be written to the compiled/ directory.
If the --multi-ext option is passed a file extension string, the script will:
* search for *.<extension> in the current directory,
* extract each '<script type="text/template"> from the file and write them into a new handlebars file
named using the id of the script tag (eg. id="undeleteMsg" -> undeleteMsg.handlebars)
* these new handlebars scripts will be compiled along with any other handlebars templates in the dir
additionally for each of those files, the script will:
* extract each '<script type="text/javascript"> from the file and write them ALL into a single js file
named using the prefix 'helpers-' and the basename of the enclosing file
(eg. common-templates.html -> helpers-common-templates.js )
* these new handlebars scripts will be compiled along with any other handlebars templates in the dir
These files can be included as if they were templates (eg. h.templates( 'helpers-common-templates' ) )
This hopefully will allow both multiple templates and their associated Handlebars helper functions to be
included in one file.
Currently, this works (best?) if:
* the multiple templates are within one html file
* each template is enclosed in a <script> tag with type="text/template" and some id attribute
* each helper fn is enclosed in a <script> tag with type="text/javascript" and some id attribute
e.g. <script type="text/template" id="history-deletedMsg" class="template">Deleted {{ msg }}</script>
"""
# ------------------------------------------------------------------------------
@@ -52,8 +51,8 @@ import re
import logging as log
log.basicConfig(
#level = log.DEBUG,
name = __name__
# level=log.DEBUG,
name=__name__
)
COMPILED_DIR = 'compiled'
@@ -67,7 +66,8 @@ HELPER_TYPE = 'text/javascript'
# both of these are off by default for backward compat
DEFAULT_MINIMIZATION = False
DEFAULT_MULTI_EXT = None #'.html'
DEFAULT_MULTI_EXT = None # '.html'
# ------------------------------------------------------------------------------
def parse_html_tag_attrs( string ):
@@ -79,15 +79,17 @@ def parse_html_tag_attrs( string ):
attrs[ key ] = val
return attrs
def split_on_html_tag( string, tag ):
tag_pattern = r'<%s\s*(?P<attrs>.*?)>(?P<body>.*?)</%s>' % ( tag, tag )
log.debug( tag_pattern )
tag_pattern = re.compile( tag_pattern, re.MULTILINE | re.DOTALL )
found_list = re.findall( tag_pattern, string )
for attrs, body in found_list:
yield ( parse_html_tag_attrs( attrs ), body )
def filter_on_tag_type( generator, type_attr_to_match ):
for attrs, body in generator:
log.debug( 'attrs: %s', str( attrs ) )
@@ -95,41 +97,41 @@ def filter_on_tag_type( generator, type_attr_to_match ):
and ( attrs[ 'type' ] == type_attr_to_match ) ):
yield attrs, body
# ------------------------------------------------------------------------------
def break_multi_template( multi_template_filename ):
"""parse the multi template, writing each template into a new handlebars tmpl and returning their names"""
template_filenames = []
# parse the multi template
print "\nBreaking multi-template file %s into individual templates and helpers:" % ( multi_template_filename )
with open( multi_template_filename, 'r' ) as multi_template_file:
multi_template_file_text = multi_template_file.read()
# write a template file for each template (name based on id in tag)
tag_generator = split_on_html_tag( multi_template_file_text, TEMPLATE_TAG )
for attrs, template_text in filter_on_tag_type( tag_generator, TEMPLATE_TYPE ):
if( 'id' not in attrs ):
log.warning( 'Template has no "id". attrs: %s' %( str( attrs ) ) )
log.warning( 'Template has no "id". attrs: %s' % ( str( attrs ) ) )
continue
template_id = attrs[ 'id' ]
template_text = template_text.strip()
handlebar_template_filename = template_id + '.handlebars'
with open( handlebar_template_filename, 'w' ) as handlebar_template_file:
handlebar_template_file.write( template_text )
log.debug( "%s\n%s\n", template_id, template_text )
template_filenames.append( handlebar_template_filename )
## write all helpers to a single 'helper-' prefixed js file in the compilation dir
# write all helpers to a single 'helper-' prefixed js file in the compilation dir
helper_fns = []
# same tag, different type
tag_generator = split_on_html_tag( multi_template_file_text, TEMPLATE_TAG )
for attrs, helper_text in filter_on_tag_type( tag_generator, HELPER_TYPE ):
helper_text = helper_text.strip()
print '(helper):', ( attrs[ 'id' ] if 'id' in attrs else '(No id)' )
helper_fns.append( helper_text )
if helper_fns:
@@ -139,7 +141,7 @@ def break_multi_template( multi_template_filename ):
with open( helper_filename, 'w' ) as helper_file:
helper_file.write( '\n'.join( helper_fns ) )
print '(helper functions written to %s)' % helper_filename
print '\n'.join( template_filenames )
return template_filenames
@@ -147,12 +149,12 @@ def break_multi_template( multi_template_filename ):
# ------------------------------------------------------------------------------
def compile_template( template_filename, minimize=False ):
"""compile the given template file (optionally minimizing the js) using subprocess.
Use the basename of the template file for the outputed js.
"""
template_basename = os.path.splitext( os.path.split( template_filename )[1] )[0]
compiled_filename = os.path.join( COMPILED_DIR, template_basename + COMPILED_EXT )
command_string = COMPILE_CMD_STR % ( template_filename, compiled_filename )
if minimize:
command_string += COMPILE_MINIMIZE_SWITCH
@@ -163,16 +165,16 @@ def compile_template( template_filename, minimize=False ):
# ------------------------------------------------------------------------------
def main( options, args ):
"""Break multi template files into single templates, compile all single templates.
If args, compile that as a list of specific handlebars and/or multi template files.
"""
print "(Call this script with the '-h' option for more help)"
handlebars_templates = []
# If specific scripts specified on command line, just compile them,
if len( args ) >= 1:
handlebars_templates = filter( lambda( x ): x.endswith( '.handlebars' ), args )
# otherwise compile all in the current dir
else:
handlebars_templates = glob( '*.handlebars' )
@@ -186,13 +188,13 @@ def main( options, args ):
multi_templates = filter( lambda( x ): x.endswith( options.multi_ext ), args )
else:
multi_templates = glob( '*' + options.multi_ext )
for multi_template_filename in multi_templates:
multi_template_template_filenames.extend( break_multi_template( multi_template_filename ) )
# unique filenames only (Q&D)
handlebars_templates = list( set( handlebars_templates + multi_template_template_filenames ) )
# compile the templates
print "\nCompiling templates:"
filenames_w_possible_errors = []
@@ -200,7 +202,7 @@ def main( options, args ):
shell_ret = compile_template( handlebars_template, options.minimize )
if shell_ret:
filenames_w_possible_errors.append( handlebars_template )
# report any possible errors
if filenames_w_possible_errors:
print "\nThere may have been errors on the following files:"
@@ -234,4 +236,3 @@ if __name__ == '__main__':
( options, args ) = optparser.parse_args()
sys.exit( main( options, args ) )
@@ -12,6 +12,7 @@ from math import pi
assert sys.version_info[:2] >= ( 2, 4 )
def parse_css_color( color ):
if color.startswith( '#' ):
color = color[1:]
@@ -25,11 +26,10 @@ def parse_css_color( color ):
b = int( color[4:6], 16 )
else:
raise Exception( "Color should be 3 hex numbers" )
return r/256, g/256, b/256
return r / 256, g / 256, b / 256
size = int( sys.argv[1] )
surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, size, size )
c = cairo.Context( surface )
@@ -48,8 +48,8 @@ t = size / 4.0
arrow = sys.argv[4]
if arrow == 'right':
c.move_to( t + 1, t )
c.line_to( 3*t - 1, 2*t )
c.line_to( t + 1, 3*t )
c.line_to( 3 * t - 1, 2 * t )
c.line_to( t + 1, 3 * t )
c.stroke()
surface.write_to_png( "/dev/stdout" )
-2
View File
@@ -27,8 +27,6 @@ assert sys.version_info[:2] >= ( 2, 6 ) and sys.version_info[:2] <= ( 2, 7 ), 'P
new_path = os.path.abspath( os.path.join( os.path.dirname( __file__ ), os.pardir, os.pardir ) )
sys.path.insert( 0, new_path )
from galaxy import eggs
import pkg_resources
import galaxy.model.mapping # need to load this before we unpickle, in order to setup properties assigned by the mappers
from galaxy.model.custom_types import total_size
+34 -33
View File
@@ -1,40 +1,39 @@
#!/usr/bin/env python
# Script requires handlebars compiler be installed; use node package manager
# Script requires handlebars compiler be installed; use node package manager
# to install handlebars.
"""%prog [options] [ specific .handlebars or .html template file to compile ]
Compiles handlebars templates into javascript for better load times and performance.
Handlebars compilation errors will print to stderr.
NOTE!: you will need node.js, npm, and handlebars (installed from npm) in order to compile templates
See http://handlebarsjs.com/precompilation.html for more info.
Should be called from the $GLX_ROOT/static/scripts/templates directory.
Should be called from the $GLX_ROOT/static/scripts/templates directory.
Compiled templates will be written to the compiled/ directory.
If the --multi-ext option is passed a file extension string, the script will:
* search for *.<extension> in the current directory,
* extract each '<script type="text/template"> from the file and write them into a new handlebars file
named using the id of the script tag (eg. id="undeleteMsg" -> undeleteMsg.handlebars)
* these new handlebars scripts will be compiled along with any other handlebars templates in the dir
additionally for each of those files, the script will:
* extract each '<script type="text/javascript"> from the file and write them ALL into a single js file
named using the prefix 'helpers-' and the basename of the enclosing file
(eg. common-templates.html -> helpers-common-templates.js )
* these new handlebars scripts will be compiled along with any other handlebars templates in the dir
These files can be included as if they were templates (eg. h.templates( 'helpers-common-templates' ) )
This hopefully will allow both multiple templates and their associated Handlebars helper functions to be
included in one file.
Currently, this works (best?) if:
* the multiple templates are within one html file
* each template is enclosed in a <script> tag with type="text/template" and some id attribute
* each helper fn is enclosed in a <script> tag with type="text/javascript" and some id attribute
e.g. <script type="text/template" id="history-deletedMsg" class="template">Deleted {{ msg }}</script>
"""
# ------------------------------------------------------------------------------
@@ -52,8 +51,8 @@ import re
import logging as log
log.basicConfig(
#level = log.DEBUG,
name = __name__
# level=log.DEBUG,
name=__name__
)
COMPILED_DIR = 'compiled'
@@ -67,7 +66,8 @@ HELPER_TYPE = 'text/javascript'
# both of these are off by default for backward compat
DEFAULT_MINIMIZATION = False
DEFAULT_MULTI_EXT = None #'.html'
DEFAULT_MULTI_EXT = None # '.html'
# ------------------------------------------------------------------------------
def parse_html_tag_attrs( string ):
@@ -79,15 +79,17 @@ def parse_html_tag_attrs( string ):
attrs[ key ] = val
return attrs
def split_on_html_tag( string, tag ):
tag_pattern = r'<%s\s*(?P<attrs>.*?)>(?P<body>.*?)</%s>' % ( tag, tag )
log.debug( tag_pattern )
tag_pattern = re.compile( tag_pattern, re.MULTILINE | re.DOTALL )
found_list = re.findall( tag_pattern, string )
for attrs, body in found_list:
yield ( parse_html_tag_attrs( attrs ), body )
def filter_on_tag_type( generator, type_attr_to_match ):
for attrs, body in generator:
log.debug( 'attrs: %s', str( attrs ) )
@@ -95,41 +97,41 @@ def filter_on_tag_type( generator, type_attr_to_match ):
and ( attrs[ 'type' ] == type_attr_to_match ) ):
yield attrs, body
# ------------------------------------------------------------------------------
def break_multi_template( multi_template_filename ):
"""parse the multi template, writing each template into a new handlebars tmpl and returning their names"""
template_filenames = []
# parse the multi template
print "\nBreaking multi-template file %s into individual templates and helpers:" % ( multi_template_filename )
with open( multi_template_filename, 'r' ) as multi_template_file:
multi_template_file_text = multi_template_file.read()
# write a template file for each template (name based on id in tag)
tag_generator = split_on_html_tag( multi_template_file_text, TEMPLATE_TAG )
for attrs, template_text in filter_on_tag_type( tag_generator, TEMPLATE_TYPE ):
if( 'id' not in attrs ):
log.warning( 'Template has no "id". attrs: %s' %( str( attrs ) ) )
log.warning( 'Template has no "id". attrs: %s' % ( str( attrs ) ) )
continue
template_id = attrs[ 'id' ]
template_text = template_text.strip()
handlebar_template_filename = template_id + '.handlebars'
with open( handlebar_template_filename, 'w' ) as handlebar_template_file:
handlebar_template_file.write( template_text )
log.debug( "%s\n%s\n", template_id, template_text )
template_filenames.append( handlebar_template_filename )
## write all helpers to a single 'helper-' prefixed js file in the compilation dir
# write all helpers to a single 'helper-' prefixed js file in the compilation dir
helper_fns = []
# same tag, different type
tag_generator = split_on_html_tag( multi_template_file_text, TEMPLATE_TAG )
for attrs, helper_text in filter_on_tag_type( tag_generator, HELPER_TYPE ):
helper_text = helper_text.strip()
print '(helper):', ( attrs[ 'id' ] if 'id' in attrs else '(No id)' )
helper_fns.append( helper_text )
if helper_fns:
@@ -139,7 +141,7 @@ def break_multi_template( multi_template_filename ):
with open( helper_filename, 'w' ) as helper_file:
helper_file.write( '\n'.join( helper_fns ) )
print '(helper functions written to %s)' % helper_filename
print '\n'.join( template_filenames )
return template_filenames
@@ -147,12 +149,12 @@ def break_multi_template( multi_template_filename ):
# ------------------------------------------------------------------------------
def compile_template( template_filename, minimize=False ):
"""compile the given template file (optionally minimizing the js) using subprocess.
Use the basename of the template file for the outputed js.
"""
template_basename = os.path.splitext( os.path.split( template_filename )[1] )[0]
compiled_filename = os.path.join( COMPILED_DIR, template_basename + COMPILED_EXT )
command_string = COMPILE_CMD_STR % ( template_filename, compiled_filename )
if minimize:
command_string += COMPILE_MINIMIZE_SWITCH
@@ -163,16 +165,16 @@ def compile_template( template_filename, minimize=False ):
# ------------------------------------------------------------------------------
def main( options, args ):
"""Break multi template files into single templates, compile all single templates.
If args, compile that as a list of specific handlebars and/or multi template files.
"""
print "(Call this script with the '-h' option for more help)"
handlebars_templates = []
# If specific scripts specified on command line, just compile them,
if len( args ) >= 1:
handlebars_templates = filter( lambda( x ): x.endswith( '.handlebars' ), args )
# otherwise compile all in the current dir
else:
handlebars_templates = glob( '*.handlebars' )
@@ -186,13 +188,13 @@ def main( options, args ):
multi_templates = filter( lambda( x ): x.endswith( options.multi_ext ), args )
else:
multi_templates = glob( '*' + options.multi_ext )
for multi_template_filename in multi_templates:
multi_template_template_filenames.extend( break_multi_template( multi_template_filename ) )
# unique filenames only (Q&D)
handlebars_templates = list( set( handlebars_templates + multi_template_template_filenames ) )
# compile the templates
print "\nCompiling templates:"
filenames_w_possible_errors = []
@@ -200,7 +202,7 @@ def main( options, args ):
shell_ret = compile_template( handlebars_template, options.minimize )
if shell_ret:
filenames_w_possible_errors.append( handlebars_template )
# report any possible errors
if filenames_w_possible_errors:
print "\nThere may have been errors on the following files:"
@@ -234,4 +236,3 @@ if __name__ == '__main__':
( options, args ) = optparser.parse_args()
sys.exit( main( options, args ) )
+57 -58
View File
@@ -1,10 +1,10 @@
import os
import sys
#--------read tool_conf.xml to get all the tool xml file names-----------
# read tool_conf.xml to get all the tool xml file names
onoff = 1
tool_list = []
tool_conf_file = os.environ.get( 'GALAXY_TEST_TOOL_CONF', None )
tool_conf_file = os.environ.get( 'GALAXY_TEST_TOOL_CONF', None )
if tool_conf_file is None:
for possible_tool_file in [ 'config/tool_conf.xml', 'tool_conf.xml', 'config/tool_conf.xml.sample' ]:
@@ -17,66 +17,65 @@ if tool_conf_file is None or not os.path.isfile(tool_conf_file):
sys.exit(1)
for line in open(tool_conf_file, "r"):
if line.find("<!--") != -1:
onoff = 0
if line.find("file") != -1 and onoff==1:
strs = line.split('\"')
tool_list.append(strs[1])
if line.find("<section") != -1 and onoff==1:
keys = line.strip().split('\"')
n = 0
strtmp = "section::"
while n < len(keys) :
if keys[n].find("id") != -1 : strtmp = strtmp + keys[n+1]
if keys[n].find("name") != -1 : strtmp = strtmp + keys[n+1] + "-"
n = n + 1
tool_list.append(strtmp.replace(' ', '_'))
if line.find("-->") != -1:
onoff =1
if line.find("<!--") != -1:
onoff = 0
if line.find("file") != -1 and onoff == 1:
strs = line.split('\"')
tool_list.append(strs[1])
if line.find("<section") != -1 and onoff == 1:
keys = line.strip().split('\"')
n = 0
strtmp = "section::"
while n < len(keys):
if keys[n].find("id") != -1:
strtmp = strtmp + keys[n + 1]
if keys[n].find("name") != -1:
strtmp = strtmp + keys[n + 1] + "-"
n = n + 1
tool_list.append(strtmp.replace(' ', '_'))
if line.find("-->") != -1:
onoff = 1
#-------read tool info from every tool xml file--------------------------
# read tool info from every tool xml file
name = []
id = []
desc = []
tool_infos = []
for tool in tool_list :
if tool.find("section")!=-1 :
tool_info = dict()
tool_info["id"] = tool
tool_infos.append(tool_info)
if os.path.exists("tools/"+tool) :
for line in open("tools/"+tool) :
if line.find("<tool ") != -1 and line.find("id") != -1 :
keys = line.strip().split('\"')
tool_info = dict()
tool_info["desc"] = ''
for n in range(len(keys)-1):
if " id=" in keys[n]:
tool_info["id"] = keys[n+1].replace(' ', '_')
if " name=" in keys[n]:
tool_info["name"] = keys[n+1]
if " description=" in keys[n]:
tool_info["desc"] = keys[n+1]
tool_infos.append(tool_info)
break
for tool in tool_list:
if tool.find("section") != -1:
tool_info = dict()
tool_info["id"] = tool
tool_infos.append(tool_info)
if os.path.exists("tools/" + tool):
for line in open("tools/" + tool):
if line.find("<tool ") != -1 and line.find("id") != -1:
keys = line.strip().split('\"')
tool_info = dict()
tool_info["desc"] = ''
for n in range(len(keys) - 1):
if " id=" in keys[n]:
tool_info["id"] = keys[n + 1].replace(' ', '_')
if " name=" in keys[n]:
tool_info["name"] = keys[n + 1]
if " description=" in keys[n]:
tool_info["desc"] = keys[n + 1]
tool_infos.append(tool_info)
break
flag=0
if len(sys.argv) == 1 :
for tool_info in tool_infos:
if tool_info["id"].find("section") != -1 :
print "==========================================================================================================================================="
print "%-45s\t%-40s\t%s" % ("id", "name", tool_info["id"])
print "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
else :
print "%-45s\t%-40s" % (tool_info["id"], tool_info["name"])
flag = 0
if len(sys.argv) == 1:
for tool_info in tool_infos:
if tool_info["id"].find("section") != -1:
print "==========================================================================================================================================="
print "%-45s\t%-40s\t%s" % ("id", "name", tool_info["id"])
print "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
else:
print "%-45s\t%-40s" % (tool_info["id"], tool_info["name"])
else:
for tool_info in tool_infos:
if tool_info["id"].find("section") != -1 :
flag=0
elif flag==1:
print " functional.test_toolbox:TestForTool_%s" % tool_info["id"],
if tool_info["id"].replace('section::', '')==sys.argv[1]:
flag=1
#for key in tool_infos.keys():
# print tool_infos[key]["id"], "\t", tool_infos[key]["name"], "\t", tool_infos[key]["desc"]
for tool_info in tool_infos:
if tool_info["id"].find("section") != -1:
flag = 0
elif flag == 1:
print " functional.test_toolbox:TestForTool_%s" % tool_info["id"],
if tool_info["id"].replace('section::', '') == sys.argv[1]:
flag = 1