Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 436b1f3fefd1d1a2741474617ee635b65fe491d9 0 parents
@akheron authored
2  .gitignore
@@ -0,0 +1,2 @@
+*.pyc
+*.pyo
58 bin/stango-init
@@ -0,0 +1,58 @@
+#!/usr/bin/python
+
+import os
+import sys
+
+manage_code = '''\
+#!/usr/bin/python
+if __name__ == '__main__':
+ import os, stango
+ try:
+ import files
+ except ImportError:
+ import sys
+ print >>sys.stderr, "Error: Unable to import 'files.py' in the directory containing %r" % __file__
+ sys.exit(1)
+ stango.manage.run(files)
+'''
+
+files_code = '''\
+import stango
+
+files = stango.files(
+ # Example:
+ # ('index.html', stango.views.template, {'template': 'index.html'}),
+)
+'''
+
+init_files = [
+ ('__init__.py', '', 0644),
+ ('manage.py', manage_code, 0755),
+ ('files.py', files_code, 0644),
+]
+
+
+if len(sys.argv) != 2 or sys.argv[1] in ['-h', '--help', 'help']:
+ print '''\
+usage: stango-init <dir>
+
+Start a new Stango project in directory <dir>'''
+ sys.exit(2)
+
+project_dir = sys.argv[1]
+if os.path.exists(project_dir):
+ print >>sys.stderr, '%r already exists' % project_dir
+ sys.exit(1)
+
+try:
+ os.mkdir(project_dir)
+except OSError, err:
+ print >>sys.stderr, 'Unable to mkdir %r: %s' % (project_dir, err.strerror)
+ sys.exit(1)
+
+for filename, contents, mode in init_files:
+ path = os.path.join(project_dir, filename)
+ fobj = open(path, 'w')
+ fobj.write(contents)
+ fobj.close()
+ os.chmod(path, mode)
38 lib/stango/__init__.py
@@ -0,0 +1,38 @@
+import os
+import manage
+import views
+
+class File(object):
+ def __init__(self, path, view, kwargs):
+ self.path = path
+ self.realpath = path
+ self.view = view
+ self.kwargs = kwargs
+
+ def complete(self, index_file):
+ if not self.path or self.path.endswith('/'):
+ self.realpath = os.path.join(self.path, index_file)
+
+
+def files(*args):
+ import os
+
+ def _files(*args):
+ for i, arg in enumerate(args):
+ if not isinstance(arg, tuple):
+ raise ValueError('argument %d is not a tuple' % i)
+ if len(arg) < 2 or len(arg) > 3:
+ raise ValueError('argument %d is not a 2-tuple or 3-tuple' % i)
+
+ if len(arg) == 2:
+ path, view = arg
+ kwargs = {}
+ else:
+ path, view, kwargs = arg
+
+ if not isinstance(path, basestring) or path.startswith('/'):
+ raise ValueError('invalid path %r in arg %d' % (path, i))
+
+ yield File(path, view, kwargs)
+
+ return list(_files(*args))
98 lib/stango/manage.py
@@ -0,0 +1,98 @@
+import os
+import shutil
+import sys
+import traceback
+
+def serve(files, port):
+ if port < 0 or port > 65535:
+ print >>sys.stderr, 'Unable to serve on invalid port %r' % port
+ return 1
+
+ print >>sys.stderr, 'Not implemented'
+ return 1
+
+
+def render(files, outdir):
+ if os.path.exists(outdir):
+ if os.path.isdir(outdir):
+ shutil.rmtree(outdir)
+ else:
+ print >>sys.stderr, '%r is not a directory' % outdir
+ return 1
+
+ if os.path.isdir('static'):
+ shutil.copytree('static', 'out')
+
+ for file in files:
+ path = os.path.join(outdir, file.realpath)
+
+ if os.path.exists(path):
+ print >>sys.stderr, '%r exists in both static/ and files.py' % \
+ file.realpath
+
+ if not os.path.exists(os.path.dirname(path)):
+ os.makedirs(os.path.dirname(path))
+
+ fobj = open(path, 'w')
+ try:
+ fobj.write(file.view(**file.kwargs))
+ finally:
+ fobj.close()
+
+
+def print_help():
+ print '''\
+usage: %s subcmd [args...]
+
+subcommands:
+ serve [port]
+
+ Serve the pages on http://localhost:<port>/. <port> defaults
+ to 8080.
+
+ render [outdir]
+
+ Render the pages as flat files to directory <outdir>, which
+ defaults to 'out'.
+''' % sys.argv[0]
+ sys.exit(2)
+
+
+def run(files_module):
+ if not hasattr(files_module, 'files'):
+ print >>sys.stderr, "'files.py' doesn't define the 'files' variable"
+ sys.exit(1)
+ files = files_module.files
+
+ if hasattr(files_module, 'index_file'):
+ for file in files:
+ file.complete(files_module.index_file)
+
+ if len(sys.argv) < 2 or sys.argv[1] not in ['serve', 'render']:
+ print_help()
+
+ if sys.argv[1] == 'serve':
+ if len(sys.argv) == 2:
+ port = 8080
+ elif len(sys.argv) == 3:
+ try:
+ port = int(sys.argv[2])
+ except ValueError:
+ print_help()
+ else:
+ print_help()
+
+ sys.exit(serve(files, port) or 0)
+
+ elif sys.argv[1] == 'render':
+ if len(sys.argv) == 2:
+ outdir = 'out'
+ elif len(sys.argv) == 3:
+ outdir = sys.argv[2]
+ else:
+ print_help()
+
+ sys.exit(render(files, outdir) or 0)
+
+ else:
+ print_usage()
9 lib/stango/shortcuts.py
@@ -0,0 +1,9 @@
+import os
+
+def render_to_template(template_name, **kwargs):
+ from jinja2 import Environment, FileSystemLoader
+
+ env = Environment(loader=FileSystemLoader('templates'))
+ tmpl = env.get_template(template_name)
+
+ return tmpl.render(**kwargs)
Please sign in to comment.
Something went wrong with that request. Please try again.