[Da-tools-commits] ./da-tools/userdir-ldap-cgi-python r14: Deploying a new libformat.
Robin Wittler
real at geek-at-work.org
Sun Jan 25 18:00:02 UTC 2009
------------------------------------------------------------
revno: 14
committer: Robin Wittler <real at geek-at-work.org>
branch nick: userdir-ldap-cgi-python
timestamp: Sun 2009-01-25 19:00:02 +0100
message:
Deploying a new libformat.
changing the controler to do some sorting (its just a prototype).
adjusting the machine template to call the new libformat crap.
modified:
ud_ldap_ng/controllers/udldapng.py
ud_ldap_ng/lib/libformat.py
ud_ldap_ng/lib/libldap.py
ud_ldap_ng/templates/machines.html
-------------- next part --------------
=== modified file 'ud_ldap_ng/controllers/udldapng.py'
--- a/ud_ldap_ng/controllers/udldapng.py 2009-01-22 21:50:03 +0000
+++ b/ud_ldap_ng/controllers/udldapng.py 2009-01-25 18:00:02 +0000
@@ -21,4 +21,7 @@
con = AnonymousDebianLDAP()
c.attributes = ['hostname', 'architecture', 'purpose', 'sponsor', 'status']
c.result = con.getDebianHosts(attrlist=c.attributes, adjust=True, default_value='')
+ sortby = request.params.get('sortby', None)
+ if not sortby or sortby not in c.attributes:
+ c.result = c.result.sortResults('hostname')
return render('/machines.html')
=== modified file 'ud_ldap_ng/lib/libformat.py'
--- a/ud_ldap_ng/lib/libformat.py 2009-01-22 21:50:03 +0000
+++ b/ud_ldap_ng/lib/libformat.py 2009-01-25 18:00:02 +0000
@@ -1,20 +1,85 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
-__version__ = '0.0.1'
+__version__ = '0.0.2'
__author__ = 'Robin Wittler <real at geek-at-work.org>'
__license__ = 'GPL3'
-def formattxt(value):
- for i in ('*', '@'):
- value = value.replace (i, '')
- if '[[' in value:
- value = value.replace('[[', '')
- if '|' in value:
- value = value.split('|')
- value.extend([i for i in value.pop().split(']]') if i])
- else:
- value = [i for i in value.split(']]') if i]
- return value
+
+import re
+
+compiled_regex_type = type(re.compile(''))
+re_debian_url_markup = re.compile('^(?:\[\[)(.*?)(?:\|(.*?))?(?:\]\])(.*?)$')
+
+map_dict = {'*': lambda x: x.replace('*', '', 1),
+ '@': lambda x: x.replace('@', '', 1),
+ 'www.d.o': lambda x: x.replace('www.d.o', 'www.debian.org'),
+ '~': lambda x: x.replace('~', '', 1)}
+
+def is_markup_string(txt, markup_regex=re_debian_url_markup):
+ '''checks if a string matches the given markup_regex. If so
+ it returns the found markups as tuple or returns False if not.
+
+ @param txt the string to check
+ @type txt str
+ @param markup_regex the markup scheme to use
+ @type markup_regex compiled regex
+ @return tuple or False
+ '''
+ if not type(txt) == str:
+ raise TypeError('txt must be type str')
+ if not type(markup_regex) == compiled_regex_type:
+ raise TypeError('markup_regex must be type compiled regex')
+ result = markup_regex.findall(txt)
+ if result:
+ return result[0]
+ else:
+ return False
+
+def is_public(txt, hidden_symbol='-'):
+ '''checks if a given string should be public visible.
+ If first char matches the hidden_symbol, then it should propably not
+ visible. (I think the better way is to store such informations direct in the ldap,
+ because thats why ldap exists (do it e.g. with attributes or with acl).
+ Returns True or False.
+
+ @param txt the string to check
+ @type txt str
+ @param hidden_symbol the char that should mark the string as public unvisible
+ @type txt str with len of 1
+ @return bool
+ '''
+ if not type(txt) == str:
+ raise TypeError('txt must be type str')
+ if not type(hidden_symbol) == str:
+ raise TypeError('hidden_symbol must be type str')
+ if not len(hidden_symbol) == 1:
+ raise ValueError('hidden_symbol must have len of 1')
+ if txt.startswith(hidden_symbol):
+ return False
+ else:
+ return True
+
+def map_chars(txt, mapdict=map_dict):
+ '''maps the string txt with the given mapdict.
+ The values of the dict must be callable - they will be called with
+ the string as argument and they should return a string - with the mapped
+ chars.
+
+ @param txt the string to map
+ @type txt str
+ @param mapdict a dict with the functions
+ @type mapdict dict
+ @return str
+ '''
+ if not type(txt) == str:
+ raise TypeError('txt must be type str')
+ if not type(mapdict) == dict:
+ raise TypeError('mapdict must be type dict')
+ for key, value in mapdict.items():
+ if not callable(value):
+ raise TypeError('value of item %s in mapdict must be callable' (key))
+ txt = value(txt)
+ return txt
if __name__ == '__main__':
- pass
+ pass
=== modified file 'ud_ldap_ng/lib/libldap.py'
--- a/ud_ldap_ng/lib/libldap.py 2009-01-22 21:50:03 +0000
+++ b/ud_ldap_ng/lib/libldap.py 2009-01-25 18:00:02 +0000
@@ -141,7 +141,7 @@
@type reverse bool
@return iterator
'''
- #TODO: This is all a bit ugly, please show me a better way to sort many dicts. ;)
+ #TODO: This is all a bit ugly, please show me a better way to sort many dicts by one key. ;)
if not type(keyword) == str:
raise TypeError('keyword must be type str, None or False')
if not keyword in cls.getAllAttrFromResults(cls.__results):
=== modified file 'ud_ldap_ng/templates/machines.html'
--- a/ud_ldap_ng/templates/machines.html 2009-01-22 21:50:03 +0000
+++ b/ud_ldap_ng/templates/machines.html 2009-01-25 18:00:02 +0000
@@ -48,27 +48,29 @@
<th>Sponsor</th>
<th>Status</th>
</tr>
+ <%! from ud_ldap_ng.lib.libformat import is_markup_string, is_public, map_chars %>
% for result in c.result:
<tr>
<td>
% for hostname in result.get('hostname', ['Unkown',]):
- ${hostname}
+ ${hostname}
% endfor
</td>
- <%! from ud_ldap_ng.lib.libformat import formattxt %>
% for key in c.attributes[1:]:
<td>
<ul>
% for value in result.get(key, ['Unknown',]):
- <% value = formattxt(value) %>
- % if type(value) == str:
- <li>${value}</li>
- % elif len(value) == 1:
- <li><a href="http://${value[0]}">${value[0]}</a></li>
- % elif len(value) == 2:
- <li><a href="http://${value[0]}">${value[1]}</a></li>
- % else:
- <li><a href="http://${value[0]}">${value[1]}</a>${value[2]}</li>
+ <% results = is_markup_string(value) %>
+ % if not results:
+ % if is_public(value):
+ <li>${map_chars(value)}</li>
+ % endif
+ % elif is_public(results[0]):
+ % if not results[1]:
+ <li><a href="http://${map_chars(results[0])}">${map_chars(results[0])}</a>${results[2]}</li>
+ % else:
+ <li><a href="http://${map_chars(results[0])}">${results[1]}</a>${results[2]}</li>
+ % endif
% endif
% endfor
</ul>
More information about the Da-tools-commits
mailing list