Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
allenm committed Nov 22, 2012
1 parent a8c6858 commit 3e77e87
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
live-py-stylus
==============

Convert stylus to css real time. Easily used by any web framwork.
Convert stylus to css real time. Easily used by any web framwork.

For example , in a flask project :
76 changes: 76 additions & 0 deletions live_stylus/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# author Allen.M <http://blog.allenm.me>
# date 2012-11-21
#/

from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from stylus import Stylus
import threading
import sys
import os
import re
import fnmatch


compiler = Stylus()

class FileChange( PatternMatchingEventHandler ):
''' File change event handler , capture the *.styl file change event '''

def __init__(self):
super( FileChange, self ).__init__( patterns = ('*.styl',) )

def on_any_event(self, event):
if event.is_directory is False:
CompileStylus( event.src_path )

class CompileStylus:

def __init__(self, path ):
if os.path.isfile( path ):
stylusFile = open( path, 'r' )
content = stylusFile.read()
stylusFile.close()
result = compiler.compile( content )
print 'Compile the file: %s' % ( path )
resultPath = path[0:-4] + 'css'
cssFile = open( resultPath , 'w' )
cssFile.write( result )
cssFile.close()



class Watch( threading.Thread ):

def __init__(self, path = None ):
threading.Thread.__init__(self)
if path is None:
path = os.path.dirname(os.path.realpath(sys.argv[0]))
self.path = path
self.convertAll()


def run( self ):
event_handler = FileChange()
observer = Observer()
observer.schedule(event_handler, path= self.path, recursive=True)
observer.start()
print 'start watching %s directory stylus file' % (self.path)

def convertAll(self):
''' convert all the sylus file one time in the starting '''
matches = []
for root , dirnames, filenames in os.walk( self.path ):
for filename in fnmatch.filter( filenames , '*.styl'):
matches.append( os.path.join( root , filename ) )

for filename in matches :
CompileStylus( filename )

class ConvStylus:
def __init__(self, path = None ):
self.w = Watch( path )
self.w.start()
26 changes: 26 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python

import os
from setuptools import setup

def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
name = "live_stylus",
version = "0.1",
author = "Allen.M",
author_email = "menghonglun@gmail.com",
description = "Convert stylus to css real time. Easily used by any web framwork.",
licence = "MIT",
keywords = "stylus css",
url = "https://github.com/allenm/live-py-stylus",
packages = ["live_stylus"],
long_description=read('README.md'),
classifiers=[
"Development Status :: 4 - Beta",
"Topic :: Utilities",
"License :: OSI Approved :: BSD License",
],
install_requires=["stylus","watchdog"]
)

0 comments on commit 3e77e87

Please sign in to comment.