# Path: management/shell.py
# def cpR(src,dst,pattern='.*',create_parents=False,filters=[]):
#     for p in ls(src,pattern,True):
#         if os.path.exists(p) and not os.path.isdir(p):
#             dest_path = dst+'/'+p[len(src):]
#             cp(p,dest_path,create_parents=create_parents,filters=filters)
# 
# class stream(object):
#     def __init__(self,data,program=False,cwd=None):
#         if program:
#             p = subprocess.Popen(data, stdout=subprocess.PIPE,stdin=subprocess.PIPE,shell=True,cwd=cwd)
#             self.data = p.communicate("")[0]
#         else:
#             self.data = data
# 
#     def pipe(self,program,cwd=None,stdout=None):
#         if callable(program):
#             if cwd is not None:
#                 cd = os.getcwd()
#                 os.chdir(cwd)
#                 ret = stream(program(self.data))
#                 os.chdir(cd)
#                 return ret
#         else:
#             if stdout is None:
#                 stdout=subprocess.PIPE
#             p = subprocess.Popen(program, stdout=stdout,stdin=subprocess.PIPE,shell=True,cwd=cwd)
#             return stream(p.communicate(self.data)[0])
# 
#     def append(self,data):
#         if isinstance(data,stream):
#             self.data = self.data + data.data
#         else:
#             self.data = self.data + data
# 
#     def save(self,path,append=False):
#         if append:
#             out = open(path,'a')
#         else:
#             out = open(path,'w')
#         out.write(self.data)
# 
# Path: management/settings.py
# class settings(object):
#     def __init__(self,package,strip_leading=0):
#         if strip_leading > 0:
#             self._scope = '.'.join(package.split('.')[strip_leading:])
#         else:
#             self._scope = package
# 
#     def default(self, keys):
#         update_default(self._scope,keys)
# 
#     def __getattr__(self, name):
#         return get_key(self._scope,name)
# 
#     def __setattr__(self, name, value):
#         if name == '_scope':
#             super(settings,self).__setattr__(name,value)
#         else:
#             set_key(self._scope,name,value)
#             
#     def __str__(self):
#         s = _settings.get(self._scope,None)
#         return str(s)
# 
# Path: management/web/stylesheets.py
# @task
# def buildcss(target_dir=None):
#     if target_dir is None:
#         target_dir = conf.css_asset_dir
#     css_stream = cat(conf.css_files)
#     for s in conf.sass_files:
#         css_stream.append(cat(s).pipe(sass,cwd=os.path.dirname(s)))
#     css_stream.pipe("uglifycss").save(os.path.join(target_dir,"base.css"))


import os, sys
    import SimpleHTTPServer
    import SocketServer
from fabric.api import task
from management.shell import cpR, stream
from management.settings import settings
from .stylesheets import buildcss
#def compile_js(src,dst=None):
    #if dst is not None:
        ##stream("browserify "+src,program=True).pipe("babel --presets es2015").save(dst)
        ##stream("browserify -t babelify --presets es2015 "+src,program=True).save(dst)
        #stream("browserify "+src,program=True).save(dst)
    #else:
        ##return stream("browserify "+src,program=True).pipe("babel --presets es2015")
        ##return stream("browserify -t babelify --presets es2015 "+src,program=True)
        #return stream("browserify "+src,program=True)



#import jinja2
#from jinja2.filters import environmentfilter
#jinja_env=jinja2.Environment(extensions=['jinja2.ext.autoescape'])
#jinja_env.loader=jinja2.FileSystemLoader(["."])
#def render_tpl(tpl,context,dst):
    #stream(jinja_env.get_template(tpl).render(context)).save(dst)


@task
def copy_assets():
    assets = conf.assets
    for asset in assets.values():
        cpR(asset['source'],asset['target'],pattern=asset['pattern'],create_parents=True)


@task
def deploy():
    buildcss()

# Path: management/shell.py
# def cpR(src,dst,pattern='.*',create_parents=False,filters=[]):
#     for p in ls(src,pattern,True):
#         if os.path.exists(p) and not os.path.isdir(p):
#             dest_path = dst+'/'+p[len(src):]
#             cp(p,dest_path,create_parents=create_parents,filters=filters)
# 
# class stream(object):
#     def __init__(self,data,program=False,cwd=None):
#         if program:
#             p = subprocess.Popen(data, stdout=subprocess.PIPE,stdin=subprocess.PIPE,shell=True,cwd=cwd)
#             self.data = p.communicate("")[0]
#         else:
#             self.data = data
# 
#     def pipe(self,program,cwd=None,stdout=None):
#         if callable(program):
#             if cwd is not None:
#                 cd = os.getcwd()
#                 os.chdir(cwd)
#                 ret = stream(program(self.data))
#                 os.chdir(cd)
#                 return ret
#         else:
#             if stdout is None:
#                 stdout=subprocess.PIPE
#             p = subprocess.Popen(program, stdout=stdout,stdin=subprocess.PIPE,shell=True,cwd=cwd)
#             return stream(p.communicate(self.data)[0])
# 
#     def append(self,data):
#         if isinstance(data,stream):
#             self.data = self.data + data.data
#         else:
#             self.data = self.data + data
# 
#     def save(self,path,append=False):
#         if append:
#             out = open(path,'a')
#         else:
#             out = open(path,'w')
#         out.write(self.data)
# 
# Path: management/settings.py
# class settings(object):
#     def __init__(self,package,strip_leading=0):
#         if strip_leading > 0:
#             self._scope = '.'.join(package.split('.')[strip_leading:])
#         else:
#             self._scope = package
# 
#     def default(self, keys):
#         update_default(self._scope,keys)
# 
#     def __getattr__(self, name):
#         return get_key(self._scope,name)
# 
#     def __setattr__(self, name, value):
#         if name == '_scope':
#             super(settings,self).__setattr__(name,value)
#         else:
#             set_key(self._scope,name,value)
#             
#     def __str__(self):
#         s = _settings.get(self._scope,None)
#         return str(s)
# 
# Path: management/web/stylesheets.py
# @task
# def buildcss(target_dir=None):
#     if target_dir is None:
#         target_dir = conf.css_asset_dir
#     css_stream = cat(conf.css_files)
#     for s in conf.sass_files:
#         css_stream.append(cat(s).pipe(sass,cwd=os.path.dirname(s)))
#     css_stream.pipe("uglifycss").save(os.path.join(target_dir,"base.css"))


# Path: management/web/deploy.py
import os, sys
    import SimpleHTTPServer
    import SocketServer
from fabric.api import task
from management.shell import cpR, stream
from management.settings import settings
from .stylesheets import buildcss

#def compile_js(src,dst=None):
    #if dst is not None:
        ##stream("browserify "+src,program=True).pipe("babel --presets es2015").save(dst)
        ##stream("browserify -t babelify --presets es2015 "+src,program=True).save(dst)
        #stream("browserify "+src,program=True).save(dst)
    #else:
        ##return stream("browserify "+src,program=True).pipe("babel --presets es2015")
        ##return stream("browserify -t babelify --presets es2015 "+src,program=True)
        #return stream("browserify "+src,program=True)



#import jinja2
#from jinja2.filters import environmentfilter
#jinja_env=jinja2.Environment(extensions=['jinja2.ext.autoescape'])
#jinja_env.loader=jinja2.FileSystemLoader(["."])
#def render_tpl(tpl,context,dst):
    #stream(jinja_env.get_template(tpl).render(context)).save(dst)


@task
def copy_assets():
    assets = conf.assets
    for asset in assets.values():
        cpR(asset['source'],asset['target'],pattern=asset['pattern'],create_parents=True)


@task
def deploy():
    buildcss()

    copy_assets()