js namespace related methods in different class & fixed stitching logic

This commit is contained in:
Pratik Vyas 2011-06-12 18:36:36 +05:30
parent 5aacd1ea75
commit c22a25d204
4 changed files with 54 additions and 22 deletions

View file

@ -1,16 +1,15 @@
class wnJSCompiler:
def concate_files_in_subdirs(self,path,dest):
@staticmethod
def concate_files_in_dir(self,path,dest):
"""
Concatenates all files in a subdir (recursive)
Concatenates all files in a directory
"""
import os
allfiles = []
for root, subdir, files in os.walk(path):
for filename in files:
allfiles.append(os.path.join(root,filename))
print path
print allfiles
print '----'
l = os.listdir(path)
for i in l:
if os.path.isfile(os.path.join(dirname,i)):
allfiles.append(os.path.join(dirname,i))
fout = open(dest,'w')
for filename in allfiles:
f = open(filename)
@ -19,6 +18,7 @@ class wnJSCompiler:
fout.close
@staticmethod
def getsubs(self,path):
"""
gets all the sub directories of a directory (recursive)
@ -29,33 +29,30 @@ class wnJSCompiler:
for i in subd:
subs.append(os.path.join(root,i))
return subs
@staticmethod
def compilejs(self,path):
"""
Compiles the js tree for ondemand import
"""
import os
import webnotes.utils.jsnamespace as jsn
subs = self.getsubs(path)
for subdir in subs:
modname = self.getmodname(subdir)
self.concate_files_in_subdirs(subdir,os.path.join(subdir, modname))
modname = jsn.jsNamespace.getmodname(subdir)
self.concate_files_in_dir(subdir,os.path.join(subdir, modname))
self.minifyjs(os.path.join(subdir, modname))
@staticmethod
def minifyjs(self,modpath):
"""
Stub to minify js
"""
pass
def getmodname(self,modpath,ext='.js'):
"""
returns filename for the stiched file
"""
import os
b = modpath.split(os.sep)
modname = b[-1] + ext
return modname
@staticmethod
def gentsfile(self,spath,dpath):
"""
function to generate timestamps of all files in spath

View file

@ -9,6 +9,7 @@ try:
form = cgi.FieldStorage()
out = ''
out_buf, str_out = '', ''
jsdir='../js'
jsonout= {}
# Traceback
@ -25,10 +26,11 @@ try:
def load_js_from_file(module_name):
global out
global jsonout
filename = module_name # TODO replace . by /
import webnotes.utils.jsnamespace as jsn
filename = jsn.jsNamespace.modname_to_filename(module_name,jsdir)
import os
try:
f = open(os.path.join('../js/', filename))
f = open(os.path.join(filename)
try:
out = f.read()
finally:
@ -45,7 +47,9 @@ try:
load_js_from_file(module_name)
def get_dependencies(module_name):
return []
import webnotes.utils.jsdependency as jsd
ret = jsd.jsDependencyBuilder.build_dependency(module_name)
return ret
def compress_string(buf):

View file

@ -1,4 +1,5 @@
class jsDependencyBuilder:
@staticmethod
def read_code(self,js_path):
try:
f = open(js_path)
@ -9,11 +10,16 @@ class jsDependencyBuilder:
except Exception, e:
raise e
return code
@staticmethod
def read_imports(self,code):
import re
p = re.compile('\$import\(\' (?P<name> [^)]*)\' \)', re.VERBOSE)
return p.findall(code)
def build_dependency(self,js_path,depends= set()):
@staticmethod
def build_dependency(self,modname,depends= set()):
import webnotes.utils.jsnamespace as jsn
jsdir = '../js'
js_path = jsn.jsNamespace.modname_to_filename(modname,jsdir)
code = self.read_code(js_path)
curdepend = self.read_imports(code)
for i in curdepend:

View file

@ -0,0 +1,25 @@
class jsNamespace:
@staticmethod
package_prefix = '_'
def modname_to_filename(modname,jsdir, ext='.js'):
import os
path = modname.replace('.',os.sep)
path = os.path.join(jsdir,path)
if os.path.isdir(path):
#package
packagename = self.package_prefix + path.split(os.sep)[-1]
path = os.path.join(path,packagename)
elif os.path.isfile(path + ext):
path = path + ext
else:
path = 'notf' # TODO raise exception that it doesnt exist
return path
def getmodname(self,modpath,ext='.js'):
"""
returns filename for the stiched file
"""
import os
b = modpath.split(os.sep)
modname = package_prefix + b[-1] + ext
return modname