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

View file

@ -9,6 +9,7 @@ try:
form = cgi.FieldStorage() form = cgi.FieldStorage()
out = '' out = ''
out_buf, str_out = '', '' out_buf, str_out = '', ''
jsdir='../js'
jsonout= {} jsonout= {}
# Traceback # Traceback
@ -25,10 +26,11 @@ try:
def load_js_from_file(module_name): def load_js_from_file(module_name):
global out global out
global jsonout 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 import os
try: try:
f = open(os.path.join('../js/', filename)) f = open(os.path.join(filename)
try: try:
out = f.read() out = f.read()
finally: finally:
@ -45,7 +47,9 @@ try:
load_js_from_file(module_name) load_js_from_file(module_name)
def get_dependencies(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): def compress_string(buf):

View file

@ -1,4 +1,5 @@
class jsDependencyBuilder: class jsDependencyBuilder:
@staticmethod
def read_code(self,js_path): def read_code(self,js_path):
try: try:
f = open(js_path) f = open(js_path)
@ -9,11 +10,16 @@ class jsDependencyBuilder:
except Exception, e: except Exception, e:
raise e raise e
return code return code
@staticmethod
def read_imports(self,code): def read_imports(self,code):
import re import re
p = re.compile('\$import\(\' (?P<name> [^)]*)\' \)', re.VERBOSE) p = re.compile('\$import\(\' (?P<name> [^)]*)\' \)', re.VERBOSE)
return p.findall(code) 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) code = self.read_code(js_path)
curdepend = self.read_imports(code) curdepend = self.read_imports(code)
for i in curdepend: 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