Commits

Anonymous committed 69c7802

import initial

  • Participants

Comments (0)

Files changed (13)

+syntax: glob
+*.orig
+*.rej
+*~
+*.o
+*.pyc
+*.pyo
+tests/*.err
+*.swp
+
+syntax: regexp
+.*\#.*\#$
+
+import friendpaste
+
+if __name__ == "__main__":
+    friendpaste.run()

friendpaste/__init__.py

+import web
+import sys
+
+import config
+from snippets.views import Snippet, Copy
+
+render = web.template.render(config.TEMPLATES_DIR)
+
+urls = (
+        '/(?P<snippetid>\d*)', 'Snippet',
+        '/(?P<snippetid>\d+)/copy/', 'Copy',
+)
+
+_actions = []
+def action(f):
+    """Decorator to register an infogami action."""
+    _actions.append(f)
+    return f
+
+_install_hooks = []
+def install_hook(f):
+    """Decorator to register install hook."""
+    _install_hooks.append(f)
+    return f
+
+def find_action(name):
+    for a in _actions:
+        if a.__name__ == name:
+            return a
+
+def run_action(name, args=[]):
+    a = find_action(name)
+    if a:
+        a(*args)
+    else:
+        print >> sys.stderr, 'unknown command', name
+        help()
+
+@action
+def help(name=None):
+    """Show this help."""
+    
+    a = name and find_action(name)
+
+    print "Friendpaste Help"
+    print ""
+
+    if a:
+        print "    %s\t%s" %  (a.__name__, a.__doc__)
+    else:
+        print "Available actions"
+        for a in _actions:
+            print "    %s\t%s" %  (a.__name__, a.__doc__)
+
+
+
+@action
+def install():
+    for a in _install_hooks:
+        print >> web.debug, a.__name__
+        a()
+
+
+@action
+def startserver():
+    web.run(urls, globals(), *config.middleware)
+
+
+def _load():
+    import friendpaste.snippets.models
+
+
+def run():
+    _load()
+    if len(sys.argv) == 1:
+        run_action("startserver")
+    else:
+        run_action(sys.argv[1], sys.argv[2:])

friendpaste/code.py

+# -*- coding: utf-8 -*-
+
+_snippet_tpl = """
+title = %(title)s
+content = %(content)s
+"""
+
+def save_snippet(args):
+    snippet = _snippet_tpl % { 
+            'title': args['title'].value,
+            'content': args['content'].value
+    )
+
+    print snippet
+

friendpaste/config.py

+# -*- coding: utf-8 -*-
+import os.path
+PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
+
+DB_URI="mysql://work:test@localhost/friendpaste"
+
+middleware=[]
+
+MEDIA_URL = "/media"
+
+MEDIA_PATH=PROJECT_PATH + "/media"
+
+TEMPLATES_DIR = PROJECT_PATH + "/templates"

friendpaste/core/__init__.py

Empty file added.

friendpaste/core/db.py

+# -*- coding: utf-8 -*-
+from friendpaste import config
+
+from storm.locals import create_database, Store
+
+database = create_database(config.DB_URI)
+store = Store(database)
+

friendpaste/core/lexers.py

+from pygments.lexers import get_all_lexers
+
+lexers = get_all_lexers()
+for l in lexers:
+    print "('%s','%s')," % (l[1][0],l[0])
+
+
+all_lexers = (
+        ('text', 'None'),
+        ('bash','Bash'),
+        ('c','C/C++'),
+        ('css','CSS'),
+        ('diff','Diff'),
+        ('html','HTML'),
+        ('java','Java'),
+        ('js','JavaScript'),
+        ('lua','Lua'),
+        ('perl','Perl'),
+        ('php', 'PHP'),
+        ('python','Python'),
+        ('rb','Ruby'),
+
+)

friendpaste/snippets/__init__.py

Empty file added.

friendpaste/snippets/code.py

+# -*- coding: utf-8 -*-
+from pygments.lexers import get_all_lexers
+
+
+# some lexers we would like to present first
+main_lexers = (
+        ('text', 'None'),
+        ('bash','Bash'),
+        ('c','C/C++'),
+        ('css','CSS'),
+        ('diff','Diff'),
+        ('html','HTML'),
+        ('java','Java'),
+        ('js','JavaScript'),
+        ('lua','Lua'),
+        ('perl','Perl'),
+        ('php', 'PHP'),
+        ('python','Python'),
+        ('rb','Ruby'),
+
+)
+
+_snippet_tpl = """
+title = %(title)s
+#content 
+%(content)s
+"""
+
+def get_lexers():
+    lexers = get_all_lexers()
+    ret = [] 
+    for l in lexers:
+        ret.append((l[1][0],l[0]))
+        
+    return ret
+
+
+def save_snippet(args):
+    snippet = _snippet_tpl % { 
+            'title': args['title'].value,
+            'content': args['content'].value
+    }
+
+    print snippet

friendpaste/snippets/models.py

+# -*- coding: utf-8 -*-
+from storm.locals import *
+from storm.exceptions import StoreError,DatabaseError
+
+import friendpaste
+from friendpaste.core.db import store
+
+class Snippet(object):
+   __storm_table__ = "paste"
+   __storm_primary__ = "id"
+   id = Int(primary=True)
+   title = Unicode()
+   content = Unicode()
+   lang = Unicode()
+   original_id = Int()
+   original = Reference(original_id, id)
+   created = DateTime()
+   expire = DateTime()
+   password = Unicode()
+   expired = Bool()
+   private = Bool()
+
+   def __init__(self, title, content, original_id=0):
+       self.title = title
+       self.content = content
+       self.original_id = original_id
+
+
+@friendpaste.install_hook
+def create_model():
+    try: 
+        store.execute("CREATE TABLE snippet "
+            "(id INTEGER PRIMARY KEY, title VARCHAR(100), "
+            "content LONGTEXT, original_id INTEGER, "
+            "created DATETIME, expire DATETIME, "
+            "password VARCHAR(32), "
+            "expired TINYINT(1), private TINYINT(1), "
+            "FULLTEXT (title,content))")
+    except DatabaseError, err:
+        print err[1]
+

friendpaste/snippets/views.py

+# -*- coding: utf-8 -*-
+import web
+from web.request import autodelegate
+from web import form
+
+import friendpaste
+
+from code import save_snippet, get_lexers, main_lexers
+
+snippet_form = form.Form(
+        form.Textbox("title"),
+        form.Textarea("content"),
+        form.Dropdown("language", get_lexers()),
+        )
+
+class Snippet:
+    def GET(self, snippetid):
+        if snippetid:
+            print "Snippet %s" % (snippetid)
+        else:
+            form = snippet_form()
+            print friendpaste.render.snippet_form(form, main_lexers)
+
+    def POST(self, snippetid):
+        form = snippet_form()
+        if not form.validates(): 
+            print friendpaste.render.snippet_form(form)
+        else:
+            save_snippet(form)
+
+class Copy:
+    def GET(self, snippetid):
+        print "copy 555"

friendpaste/templates/snippet_form.html

+$def with (form, main_lexers)
+
+<form name="main" method="post" action="."> 
+    $if not form.valid: <p class="error">Try again :</p>
+    <ul>
+    $for l in main_lexers:
+        <li>$l[0]</li>
+    </ul>
+
+    <div class="form-row"><label for="language">Syntax hilighting</label>$:form['language'].render()</div>
+    <p>To highlight particular lines, prefix each line with @@</p>
+    <div class="form-row">$:form['content'].render()</div>
+    <div class="form-row"><label for="title">Title/ Name</label>$:form['title'].render()</div>
+    <input type="submit" />    
+</form>