]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
(no commit message)
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Sep 2005 02:15:03 +0000 (02:15 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Sep 2005 02:15:03 +0000 (02:15 +0000)
doc/build/components/formatting.myt
test/rundocs.py [new file with mode: 0644]

index 9fe39286b7700331eb723c4a0ca358e83df650e6..4be0c12cc5487783c45610d7283e03f5f5b2b76d 100644 (file)
     <div class="sectiontext">
 
 <%python>
-    regexp = re.compile(r"__FORMAT:LINK{(.*?)(?:\|(.*?))?(?:\@text=(.+?))?}")
+    regexp = re.compile(r"__FORMAT:LINK{(?:\@path=(.+?))?(?:\@xtra=(.+?))?(?:\@text=(.+?))?(?:\@href=(.+?))?(?:\@class=(.+?))?}")
     def link(matchobj):
         path = matchobj.group(1)
-        text = matchobj.group(3)
         xtra = matchobj.group(2)
-            
-        try:
-            element = item.lookup(path)
-            if xtra is not None:
-                return '<a href="%s_%s">%s</a>' % (element.get_link(includefile), xtra, text or xtra)
-            else:
-                return '<a href="%s">%s</a>' % (element.get_link(includefile), text or element.description)
-        except KeyError:
-            if xtra is not None:
-                return '<b>%s</b>' % (text or xtra)
-            else:
-                return '<b>%s</b>' % text or path
+        text = matchobj.group(3)
+        href = matchobj.group(4)
+        class_ = matchobj.group(5)
         
-    m.write(regexp.sub(link, item.content))
+        if class_ is not None:
+            class_ = 'class="%s"' % class_
+            
+        if href:
+            return '<a href="%s" %s>%s</a>' % (href, class_, text or href)
+        else:
+            try:
+                element = item.lookup(path)
+                if xtra is not None:
+                    return '<a href="%s_%s" %s>%s</a>' % (element.get_link(includefile), xtra, class_, text or xtra)
+                else:
+                    return '<a href="%s" %s>%s</a>' % (element.get_link(includefile), class_, text or element.description)
+            except KeyError:
+                if xtra is not None:
+                    return '<b>%s</b>' % (text or xtra)
+                else:
+                    return '<b>%s</b>' % text or path
+
+    re2 = re.compile(r"'''PYESC(.+?)PYESC'''", re.S)
+    content = regexp.sub(link, item.content)
+    content = re2.sub(lambda m: m.group(1), content)
+    #m.write(item.content)
+    m.write(content)
 </%python>
 
     </div>
     <tr class="<% flipper() %>"><% m.content() %></tr>
 </%method>
 
-
-
 <%method codeline trim="both">
 <span class="codeline"><% m.content() %></span>
 </%method>
 <pre><% content %></pre></div>
 </%method>
 
-
 <%method link trim="both">
     <%args>
         path = None
         method = None
         member = None
         text = None
+        href = None
+        class_ = None
     </%args>
     <%init>
-        if path is None:
+        if href is None and path is None:
             path = m.comp('doclib.myt:current').path
             
         extra = (param or method or member)
     </%init>
-__FORMAT:LINK{<%path%><% extra and "|" + extra or "" %><% text and "@text=" + text or "" %>}
-</%method>
-
-
-<%method uniqueblock>
-<%args>
-        blockname
-        uniquename
-</%args>
-
-<%init>
-        context = m.attributes.setdefault('ubcontext', {})
-        try:
-            writer = context[blockname]
-        except KeyError:
-            context[blockname] = uniquename
-            writer = uniquename
-</%init>
-
-% if writer == uniquename:
-        <% m.content() %>
-%
+__FORMAT:LINK{<% path and "@path=" + path or "" %><% extra and "@xtra=" + extra or "" %><% text and "@text=" + text or "" %><% href and "@href=" + href or "" %><% class_ and "@class=" + class_ or "" %>}
 </%method>
 
 <%method popboxlink trim="both"> 
@@ -378,47 +368,32 @@ __FORMAT:LINK{<%path%><% extra and "|" + extra or "" %><% text and "@text=" + te
     <%init>
         if name is None:
             name = m.attributes.setdefault('popbox_name', 0)
-        m.attributes['popbox_name'] += 1
+        name += 1
+        m.attributes['popbox_name'] = name
         name = "popbox_" + repr(name)
     </%init>
-javascript:togglePopbox('<% name %>', show, hide)
+javascript:togglePopbox('<% name %>', '<% show %>', '<% hide %>')
 </%method>
-<%method popbox>
+
+<%method popbox trim="both">
 <%args>
     name = None
+    class_ = None
 </%args>
 <%init>
     if name is None:
         name = 'popbox_' + repr(m.attributes['popbox_name'])
 </%init>
-<&| SELF:uniqueblock, blockname='popboxscript', uniquename=name &>
-        <script>
-                function togglePopbox(id, show, hide) {
-                        var link = document.getElementById(id + "_link");
-                        var div = document.getElementById(id + "_div");
-                        if (div.style.display == 'block') {
-                                div.style.display = 'none';
-                                if (link) {
-                                    link.firstChild.nodeValue = show;
-                                }
-                        }
-                        else if (div.style.display == 'none') {
-                                div.style.display = 'block';
-                                if (link) {
-                                link.firstChild.nodeValue = hide;
-                                }
-                        }
-                }
-
-        </script>
-</&>
-<div id="<% name %>_div"><% m.content() %></div>
+<div id="<% name %>_div" class="<% class_ %>" style="display:none;"><% m.content().strip() %></div>
 </%method>
 
-<%method codepopper>
+<%method codepopper trim="both">
     <%args>
         link
     </%args>
-    <a href="<& SELF:popboxlink &>">link</a>
-    <&|SELF:popbox&><% m.content() %></&>
+    <%init>
+        href = m.scomp('SELF:popboxlink')
+    </%init>
+    '''PYESC<& SELF:link, href=href, text=link, class_="codepoplink" &>PYESC'''
+    '''PYESC<&|SELF:popbox, class_="codepop" &><% m.content() %></&>PYESC'''
 </%method>
\ No newline at end of file
diff --git a/test/rundocs.py b/test/rundocs.py
new file mode 100644 (file)
index 0000000..0db9cdc
--- /dev/null
@@ -0,0 +1,133 @@
+from sqlalchemy.schema import *
+from sqlalchemy.mapper import *
+import sqlalchemy.databases.sqlite as sqlite
+engine = sqlite.engine(':memory:', {})
+
+engine.echo = True
+
+# table metadata
+users = Table('users', engine, 
+    Column('user_id', INTEGER, primary_key = True),
+    Column('user_name', VARCHAR(16), nullable = False),
+    Column('password', VARCHAR(20), nullable = False)
+)
+users.build()
+users.insert().execute(
+    dict(user_name = 'fred', password='45nfss')
+)
+
+
+# class definition
+class User(object):
+    def __init__(self):
+        pass
+    
+# obtain a Mapper
+m = mapper(User, users)
+
+# select
+user = m.select(users.c.user_name == 'fred')[0]      
+
+
+# modify
+user.user_name = 'fred jones'
+
+# commit
+objectstore.commit()
+
+objectstore.clear()
+
+addresses = Table('email_addresses', engine,
+    Column('address_id', INT, primary_key = True),
+    Column('user_id', INT, foreign_key = ForeignKey(users.c.user_id)),
+    Column('email_address', VARCHAR(20)),
+)
+addresses.build()
+addresses.insert().execute(
+    dict(user_id = user.user_id, email_address='fred@bar.com')
+)
+
+# second class definition
+class Address(object):
+    def __init__(self, email_address = None):
+        self.email_address = email_address
+
+# obtain a Mapper.  "private=True" means deletions of the user
+# will cascade down to the child Address objects
+m = mapper(User, users, properties = dict(
+    addresses = relation(Address, addresses, lazy=True, private=True)
+))
+
+# select
+user = m.select(users.c.user_name == 'fred jones')[0]
+print repr(user.__dict__['addresses'])
+address = user.addresses[0]
+
+# modify
+user.user_name = 'fred'
+user.addresses[0].email_address = 'fredjones@foo.com'
+user.addresses.append(Address('freddy@hi.org'))
+
+# commit
+objectstore.commit()
+
+# going to change tables, etc., start over with a new engine
+objectstore.clear()
+engine = None
+engine = sqlite.engine(':memory:', {})
+engine.echo = True
+
+# a table to store a user's preferences for a site
+prefs = Table('user_prefs', engine,
+    Column('pref_id', INT, primary_key = True),
+    Column('stylename', VARCHAR(20)),
+    Column('save_password', BOOLEAN, nullable = False),
+    Column('timezone', CHAR(3), nullable = False)
+)
+prefs.build()
+prefs.insert().execute(
+    dict(pref_id=1, stylename='green', save_password=1, timezone='EST')
+)
+
+# user table gets 'preference_id' column added
+users = Table('users', engine, 
+    Column('user_id', INTEGER, primary_key = True),
+    Column('user_name', VARCHAR(16), nullable = False),
+    Column('password', VARCHAR(20), nullable = False),
+    Column('preference_id', INTEGER, foreign_key = ForeignKey(prefs.c.pref_id))
+)
+users.drop()
+users.build()
+users.insert().execute(
+    dict(user_name = 'fred', password='45nfss', preference_id=1)
+)
+
+
+addresses = Table('email_addresses', engine,
+    Column('address_id', INT, primary_key = True),
+    Column('user_id', INT, foreign_key = ForeignKey(users.c.user_id)),
+    Column('email_address', VARCHAR(20)),
+)
+addresses.drop()
+addresses.build()
+
+# class definition for preferences
+class UserPrefs(object):
+    pass
+    
+# obtain a Mapper.
+m = mapper(User, users, properties = dict(
+    addresses = relation(Address, addresses, lazy=True, private=True),
+    preferences = relation(UserPrefs, prefs, lazy=False, private=True),
+))
+
+# select
+user = m.select(users.c.user_name == 'fred')[0]
+save_password = user.preferences.save_password
+
+# modify
+user.preferences.stylename = 'bluesteel'
+user.addresses.append(Address('freddy@hi.org'))
+
+# commit
+objectstore.commit()
\ No newline at end of file