Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

several updates to examples

  • Loading branch information...
commit ddb72f2bca0afb1f3e3c11b1fe9af5dd8bafdef7 1 parent 96abbc1
C Anthony Risinger authored
0  examples/.gitignore
View
No changes.
28 examples/__main__.py
View
@@ -6,7 +6,7 @@
import sys
import subprocess
-import _util
+from _assets import util
head = os.path.dirname(__file__)
@@ -18,13 +18,31 @@
]
env = os.environ.copy()
-env.setdefault('PYJS_BIN_PYTHON', _util._find_python())
-env.setdefault('PYJS_BIN_PYJSBUILD', _util._find_pyjsbuild(head))
+# Not ready yet
+#env['PYJS_OPT_PROXYINSTALL'] = '1'
+if 'PYJS_BIN_PYTHON' not in env:
+ env['PYJS_BIN_PYTHON'] = util._find_python()
+if 'PYJS_DIR_PYJAMAS' not in env:
+ env.update(dict([
+ ('PYJS_' + k, v)
+ for k, v in util._process_pyjamas(head).items()
+ ]))
for example in examples:
- header = ''.ljust(10, '-') + ' Building {0} '
- header = header.format(example.upper()).ljust(69, '-') + '\n'
+ header = ''.ljust(10, '-') + '( Building {0} )'
+ header = '\n' + header.format(example.upper()).ljust(69, '-') + '\n\n'
sys.stdout.write(header)
sys.stdout.flush()
e = subprocess.Popen([env['PYJS_BIN_PYTHON'], os.path.join(head, example)] + sys.argv[1:], env=env)
e.wait()
+
+complete = [
+ '\n' + '( Pyjs )'.center(69, '-') + '\n',
+ ' Complete! View examples via:',
+ ' # cd {0}'.format(head),
+ ' # {0} -m SimpleHTTPServer'.format(env['PYJS_BIN_PYTHON']),
+ ' http://localhost:8000',
+ '\n' + '( Pyjs )'.center(69, '-') + '\n\n',
+]
+
+sys.stdout.write('\n'.join(complete))
0  examples/_assets/__init__.py
View
No changes.
2  examples/_assets/download/.gitignore
View
@@ -0,0 +1,2 @@
+*
+!.gitignore
13 examples/_assets/static/examples.css
View
@@ -0,0 +1,13 @@
+body {
+ font-family: "Trebuchet MS", Arial;
+ font-size: 11pt;
+}
+h2 {
+ margin: 20px 0px 0px 0px;
+ padding: 0px;
+ color: #609;
+}
+p {
+ margin: 0px 0px 0px 10px;
+ padding: 0px;
+}
19 examples/_assets/template/index.html.tpl
View
@@ -0,0 +1,19 @@
+<html>
+<head>
+ <title>Pyjamas Examples</title>
+ <meta name="keywords" content="Python Pyjamas Examples, Python Web widgets" />
+ <meta name="description" content="Python Pyjamas Examples" />
+ <meta name="title" content="Python Web Toolkit Examples - Javascript Translator and AJAX Widgets" />
+ <link rel="stylesheet" type="text/css" href="_assets/static/examples.css" />
+</head>
+<body>
+ <h1 id='title'>Pyjamas Examples</h1>
+ <h4 id='head'>Demos are enabled once built.</h4>
+
+{0}
+
+ <hr/>
+ <h4 id='foot'><a href="http://pyjs.org">Pyjs.org</a></h4>
+
+</body>
+</html>
214 examples/_assets/util.py
View
@@ -0,0 +1,214 @@
+# -*- coding: utf-8 -*-
+
+
+import sys
+import os
+import subprocess
+
+
+ENV = None
+PATH = None
+TARGETS = None
+
+
+PACKAGE = {
+ 'title': 'example',
+ 'desc': 'default description',
+}
+
+INDEX = {
+ 'example': r'''
+ <hr/>
+<!-- start {name} {{example.{name}._comment_end}}
+ <h2 class='title'>{{example.{name}.title}}</h2>
+ <h3 class='desc'>{{example.{name}.desc}}</h3>
+ <ul class='demos'>
+ {{example.{name}.demos}}
+ </ul>
+{{example.{name}._comment_start}} -->
+ <h4 class='source'><a href="{name}/">source directory</a> ({name})<h4>
+ ''',
+ 'demo': r'''
+ <li class='demo'>(demo) <a href="{name}/output/{target}.html">{target}</a></li>
+ ''',
+}
+
+
+class _e(object):
+
+ _ident = 'example'
+
+ _special = {
+ '_comment_start': '<!--',
+ '_comment_end': '-->',
+ }
+
+ def __init__(self, examples=None, **kwds):
+ if examples is None:
+ self._examples = kwds
+ else:
+ self._examples = examples
+ self._examples.update(kwds)
+ self._path = [self._ident]
+
+ def __str__(self):
+ try:
+ curr = self._examples
+ for frag in self._path[1:]:
+ curr = curr[frag]
+ except KeyError:
+ if frag in self._special:
+ curr = self._special[frag]
+ else:
+ curr = '{{{0}}}'.format('.'.join(self._path))
+ self._path[1:] = []
+ return curr
+
+ def __getattr__(self, name):
+ self._path.append(name)
+ return self
+
+
+def _find_python():
+ if sys.version_info.major == 2 and sys.executable and os.path.isfile(sys.executable):
+ return sys.executable
+ for python in ('python2', 'python2.7', 'python2.6'):
+ try:
+ subprocess.call([python, '-c', '"raise SystemExit"'])
+ return python
+ except OSError:
+ pass
+ return 'python'
+
+
+def _list_examples():
+ return [
+ example
+ for example in os.listdir(ENV['DIR_EXAMPLES'])
+ if os.path.isfile(os.path.join(ENV['DIR_EXAMPLES'], example, '__main__.py'))
+ and not example.startswith('_')
+ ]
+
+
+def _process_pyjamas(root):
+ lim = 2
+ while lim > 0:
+ root = os.path.join(root, '..')
+ boot = os.path.join(root, 'bootstrap.py')
+ if os.path.isfile(boot):
+ root = os.path.abspath(root)
+ boot = os.path.abspath(boot)
+ pyjsbuild = os.path.join(root, 'bin', 'pyjsbuild')
+ break
+ lim = lim - 1
+ if lim == 0:
+ raise RuntimeError('Unable to locate pyjamas root.')
+ # Bootstrap on test failure; attempts to fix a couple issues at once
+ with open(os.devnull, 'wb') as null:
+ try:
+ if subprocess.call(['python', pyjsbuild], stdout=null, stderr=subprocess.STDOUT) > 0:
+ raise OSError
+ except OSError:
+ subprocess.call(['python', boot], stdout=null, stderr=subprocess.STDOUT)
+ return {
+ 'DIR_PYJAMAS': root,
+ 'DIR_EXAMPLES': os.path.join(root, 'examples'),
+ 'BIN_PYJSBUILD': pyjsbuild,
+ }
+
+
+def _process_environ():
+ return dict([
+ (k[5:], v[:])
+ for k, v in os.environ.items()
+ if k.startswith('PYJS')
+ ])
+
+#FIXME
+def _process_opts(args):
+ opts = {}
+ possible = ('downloads',)
+ for name in possible:
+ for flag in ('yes', 'no'):
+ opt = '--{0}-{1}'.format(name, flag)
+ if opt in args:
+ args.remove(opt)
+ opts[opt] = flag
+ return opts
+
+
+def _process_args(args):
+ return {'ARG_PYJSBUILD': args or ['-O']}
+
+
+def init(path):
+ global ENV, PATH
+ args = sys.argv[1:]
+ PATH = path
+ ENV = {}
+ ENV.update(_process_environ())
+ ENV.update(_process_opts(args))
+ ENV.update(_process_args(args))
+ if 'BIN_PYTHON' not in ENV:
+ ENV['BIN_PYTHON'] = _find_python()
+ if 'DIR_PYJAMAS' not in ENV:
+ ENV.update(_process_pyjamas(path))
+ if 'DIR_EXAMPLES' not in ENV:
+ ENV['DIR_EXAMPLES'] = os.path.dirname(path)
+ ENV['NAME_EXAMPLE'] = os.path.basename(path)
+ ENV['DIR_EXAMPLE'] = path
+
+
+def setup(targets):
+ for target in targets:
+ if not os.path.isfile(os.path.join(PATH, target)):
+ raise TypeError('Target `{0}` does not exist.'.format(target))
+ global TARGETS
+ TARGETS = targets
+
+
+def translate():
+ for target in TARGETS:
+ cmd = [ENV['BIN_PYTHON'], ENV['BIN_PYJSBUILD'], ' '.join(ENV['ARG_PYJSBUILD']), target]
+ e = subprocess.Popen(cmd, cwd=PATH)
+ ret = e.wait()
+
+
+def install(package=None, **packages):
+ if package is not None:
+ PACKAGE.update(package)
+ name = ENV['NAME_EXAMPLE']
+ demos = ''.join([
+ INDEX['demo'].format(name=name, target=target[:-3])
+ for target in TARGETS
+ ])
+ example = {
+ 'name': name,
+ 'title': PACKAGE['title'],
+ 'desc': PACKAGE['desc'],
+ 'demos': demos,
+ }
+ if 'OPT_PROXYINSTALL' in ENV:
+ sys.stdout.write(repr(example))
+ sys.stdout.flush()
+ return
+ packages[name] = example
+ if not packages:
+ raise TypeError('Nothing to install.')
+ index = os.path.join(ENV['DIR_EXAMPLES'], 'index.html')
+ if os.path.isfile(index):
+ idx_out_fd = open(index, 'r+')
+ tpl = idx_out_fd.read()
+ idx_out_fd.seek(0)
+ idx_out_fd.truncate()
+ else:
+ idx_out_fd = open(index, 'w')
+ examples = ''.join([
+ INDEX['example'].format(name=example)
+ for example in _list_examples()
+ ])
+ index_ = os.path.join(ENV['DIR_EXAMPLES'], '_assets', 'template', 'index.html.tpl')
+ with open(index_, 'r') as idx_in_fd:
+ tpl = idx_in_fd.read().format(examples)
+ idx_out_fd.write(tpl.format(example=_e(packages)))
+ idx_out_fd.close()
95 examples/_util.py
View
@@ -1,95 +0,0 @@
-# -*- coding: utf-8 -*-
-
-
-import sys
-import os
-import subprocess
-
-
-ENV = None
-PATH = None
-TARGETS = None
-
-
-def _find_python():
- if sys.version_info.major == 2 and sys.executable and os.path.isfile(sys.executable):
- return sys.executable
- for python in ('python2', 'python2.7', 'python2.6'):
- try:
- subprocess.call([python, '-c', '"raise SystemExit"'])
- return python
- except OSError:
- pass
- return 'python'
-
-
-def _find_pyjsbuild(path):
- lim = 2
- while lim > 0:
- path = os.path.join(path, '..')
- boot = os.path.join(path, 'bootstrap.py')
- if os.path.isfile(boot):
- path = os.path.abspath(path)
- boot = os.path.abspath(boot)
- break
- lim = lim - 1
- if lim == 0:
- raise RuntimeError('Unable to locate pyjamas root.')
- pyjsbuild = os.path.join(path, 'bin', 'pyjsbuild')
- # Bootstrap on test failure; attempts to fix a couple issues at once
- null = open(os.devnull, 'wb')
- try:
- if subprocess.call(['python', pyjsbuild], stdout=null, stderr=subprocess.STDOUT) > 0:
- raise OSError
- except OSError:
- subprocess.call(['python', boot], stdout=null, stderr=subprocess.STDOUT)
- null.close()
- return pyjsbuild
-
-
-def _process_environ():
- return dict([
- (k[5:], v[:])
- for k, v in os.environ.items()
- if k.startswith('PYJS')
- ])
-
-
-def _process_opts(args):
- opts = {}
- possible = ('downloads',)
- for name in possible:
- for flag in ('yes', 'no'):
- opt = '--{0}-{1}'.format(name, flag)
- if opt in args:
- args.remove(opt)
- opts[opt] = flag
- return opts
-
-
-def _process_args(args):
- return {'ARG_PYJSBUILD': args or ['-O']}
-
-
-def setup(path, targets):
- for target in targets:
- if not os.path.isfile(os.path.join(path, target)):
- raise TypeError('Target `{0}` does not exist.'.format(target))
- global ENV, PATH, TARGETS
- args = sys.argv[1:]
- ENV = {}
- ENV.update(_process_environ())
- ENV.update(_process_opts(args))
- ENV.update(_process_args(args))
- ENV.setdefault('BIN_PYTHON', _find_python())
- ENV.setdefault('BIN_PYJSBUILD', _find_pyjsbuild(path))
- ENV['DIR_EXAMPLE'] = path
- PATH = path
- TARGETS = targets
-
-
-def translate():
- for target in TARGETS:
- cmd = [ENV['BIN_PYTHON'], ENV['BIN_PYJSBUILD'], ' '.join(ENV['ARG_PYJSBUILD']), target]
- e = subprocess.Popen(cmd, cwd=PATH)
- ret = e.wait()
35 examples/helloworld/__main__.py
View
@@ -7,24 +7,30 @@
]
-def prepare():
- '''Prepare example for translation before util.setup() is called'''
- pass
+PACKAGE = {
+ 'title': 'helloworld',
+ 'desc': 'Port of GWT hello world example',
+}
+
+
+def setup(targets):
+ '''Setup example for translation, MUST call util.setup(targets).'''
+ util.setup(targets)
def translate():
- '''Translate example'''
+ '''Translate example, MUST call util.translate().'''
util.translate()
-def finalize():
- '''Cleanup example'''
- pass
+def install(package):
+ '''Install and cleanup example module. MUST call util.install(package)'''
+ util.install(package)
-#####################################
-# ---------- DO NOT EDIT ---------- #
-#####################################
+##---------------------------------------##
+# --------- (-: DO NOT EDIT :-) --------- #
+##---------------------------------------##
import sys
@@ -33,10 +39,11 @@ def finalize():
head = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(head, '..'))
-import _util as util
+from _assets import util
sys.path.pop(0)
-prepare()
-util.setup(head, TARGETS)
+util.init(head)
+
+setup(TARGETS)
translate()
-finalize()
+install(PACKAGE)
49 examples/helloworldsplash/__main__.py
View
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+TARGETS = [
+ 'Hello.py',
+]
+
+
+PACKAGE = {
+ 'title': 'helloworld w/bootsplash',
+ 'desc': 'Port of GWT hello world example, with a JS bootsplash',
+}
+
+
+def setup(targets):
+ '''Setup example for translation, MUST call util.setup(targets).'''
+ util.setup(targets)
+
+
+def translate():
+ '''Translate example, MUST call util.translate().'''
+ util.translate()
+
+
+def install(package):
+ '''Install and cleanup example module. MUST call util.install(package)'''
+ util.install(package)
+
+
+##---------------------------------------##
+# --------- (-: DO NOT EDIT :-) --------- #
+##---------------------------------------##
+
+
+import sys
+import os
+
+
+head = os.path.dirname(__file__)
+sys.path.insert(0, os.path.join(head, '..'))
+from _assets import util
+sys.path.pop(0)
+
+util.init(head)
+
+setup(TARGETS)
+translate()
+install(PACKAGE)
Please sign in to comment.
Something went wrong with that request. Please try again.