Merge pull request #411 from niphlod/docs/te-x

sphinx-compatible docstring (only DAL remains)
This commit is contained in:
mdipierro
2014-03-25 12:16:42 -05:00
6 changed files with 1457 additions and 1386 deletions
+558 -554
View File
File diff suppressed because it is too large Load Diff
+61 -64
View File
@@ -1,15 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
This file is part of the web2py Web Framework | This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
| Created by Vladyslav Kozlovskyy (Ukraine) <dbdevelop©gmail.com>
Created by Vladyslav Kozlovskyy (Ukraine) <dbdevelop©gmail.com> | for Web2py project
for Web2py project
Utilities and class for UTF8 strings managing Utilities and class for UTF8 strings managing
=========================================== ----------------------------------------------
""" """
import __builtin__ import __builtin__
__all__ = ['Utf8'] __all__ = ['Utf8']
@@ -30,18 +29,19 @@ repr_escape_tab2[ord('\'')] = u"\\'"
def sort_key(s): def sort_key(s):
""" Unicode Collation Algorithm (UCA) (http://www.unicode.org/reports/tr10/) """Unicode Collation Algorithm (UCA) (http://www.unicode.org/reports/tr10/)
is used for utf-8 and unicode strings sorting and for utf-8 strings is used for utf-8 and unicode strings sorting and for utf-8 strings
comparison comparison
NOTE: pyuca is a very memory cost module! It loads the whole Note:
"allkey.txt" file (~2mb!) into the memory. But this pyuca is a very memory cost module! It loads the whole
functionality is needed only when sort_key() is called as a "allkey.txt" file (~2mb!) into the memory. But this
part of sort() function or when Utf8 strings are compared. functionality is needed only when sort_key() is called as a
part of sort() function or when Utf8 strings are compared.
So, it is a lazy "sort_key" function which (ONLY ONCE, ON ITS So, it is a lazy "sort_key" function which (ONLY ONCE, ON ITS
FIRST CALL) imports pyuca and replaces itself with a real FIRST CALL) imports pyuca and replaces itself with a real
sort_key() function sort_key() function
""" """
global sort_key global sort_key
try: try:
@@ -56,9 +56,8 @@ def sort_key(s):
def ord(char): def ord(char):
""" returns unicode id for utf8 or unicode *char* character """Returns unicode id for utf8 or unicode *char* character
SUPPOSE that *char* is an utf-8 or unicode character only
SUPPOSE that *char* is an utf-8 or unicode character only
""" """
if isinstance(char, unicode): if isinstance(char, unicode):
return __builtin__.ord(char) return __builtin__.ord(char)
@@ -66,28 +65,29 @@ def ord(char):
def chr(code): def chr(code):
""" return utf8-character with *code* unicode id """ """Returns utf8-character with *code* unicode id """
return Utf8(unichr(code)) return Utf8(unichr(code))
def size(string): def size(string):
""" return length of utf-8 string in bytes """Returns length of utf-8 string in bytes
NOTE! The length of correspondent utf-8
string is returned for unicode string Note:
The length of correspondent utf-8 string is returned for unicode string
""" """
return Utf8(string).__size__() return Utf8(string).__size__()
def truncate(string, length, dots='...'): def truncate(string, length, dots='...'):
""" returns string of length < *length* or truncate """Returns string of length < *length* or truncate string with adding
string with adding *dots* suffix to the string's end *dots* suffix to the string's end
args: Args:
length (int): max length of string length (int): max length of string
dots (str or unicode): string suffix, when string is cutted dots (str or unicode): string suffix, when string is cutted
returns: Returns:
(utf8-str): original or cutted string (utf8-str): original or cutted string
""" """
text = unicode(string, 'utf-8') text = unicode(string, 'utf-8')
dots = unicode(dots, 'utf-8') if isinstance(dots, str) else dots dots = unicode(dots, 'utf-8') if isinstance(dots, str) else dots
@@ -125,31 +125,32 @@ class Utf8(str):
def __repr__(self): def __repr__(self):
r''' # note that we use raw strings to avoid having to use double back slashes below r''' # note that we use raw strings to avoid having to use double back slashes below
NOTE! This function is a clone of web2py:gluon.languages.utf_repl() function NOTE! This function is a clone of web2py:gluon.languages.utf_repl() function::
utf8.__repr__() works same as str.repr() when processing ascii string utf8.__repr__() works same as str.repr() when processing ascii string
>>> repr(Utf8('abc')) == repr(Utf8("abc")) == repr('abc') == repr("abc") == "'abc'" >>> repr(Utf8('abc')) == repr(Utf8("abc")) == repr('abc') == repr("abc") == "'abc'"
True True
>>> repr(Utf8('a"b"c')) == repr('a"b"c') == '\'a"b"c\'' >>> repr(Utf8('a"b"c')) == repr('a"b"c') == '\'a"b"c\''
True True
>>> repr(Utf8("a'b'c")) == repr("a'b'c") == '"a\'b\'c"' >>> repr(Utf8("a'b'c")) == repr("a'b'c") == '"a\'b\'c"'
True True
>>> repr(Utf8('a\'b"c')) == repr('a\'b"c') == repr(Utf8("a'b\"c")) == repr("a'b\"c") == '\'a\\\'b"c\'' >>> repr(Utf8('a\'b"c')) == repr('a\'b"c') == repr(Utf8("a'b\"c")) == repr("a'b\"c") == '\'a\\\'b"c\''
True True
>>> repr(Utf8('a\r\nb')) == repr('a\r\nb') == "'a\\r\\nb'" # Test for \r, \n >>> repr(Utf8('a\r\nb')) == repr('a\r\nb') == "'a\\r\\nb'" # Test for \r, \n
True True
Unlike str.repr(), Utf8.__repr__() remains utf8 content when processing utf8 string Unlike str.repr(), Utf8.__repr__() remains utf8 content when processing utf8 string::
>>> repr(Utf8('中文字')) == repr(Utf8("中文字")) == "'中文字'" != repr('中文字')
True >>> repr(Utf8('中文字')) == repr(Utf8("中文字")) == "'中文字'" != repr('中文字')
>>> repr(Utf8('中"文"字')) == "'中\"文\"字'" != repr('中"文"字') True
True >>> repr(Utf8('中"文"字')) == "'中\"文\"字'" != repr('中"文"字')
>>> repr(Utf8("中'文'字")) == '"中\'文\'字"' != repr("中'文'字") True
True >>> repr(Utf8("中'文'字")) == '"中\'文\'字"' != repr("中'文'字")
>>> repr(Utf8('中\'文"字')) == repr(Utf8("中'文\"字")) == '\'中\\\'文"字\'' != repr('中\'文"字') == repr("中'文\"字") True
True >>> repr(Utf8('中\'文"字')) == repr(Utf8("中'文\"字")) == '\'中\\\'文"字\'' != repr('中\'文"字') == repr("中'文\"字")
>>> repr(Utf8('中\r\n文')) == "'中\\r\\n文'" != repr('中\r\n文') # Test for \r, \n True
True >>> repr(Utf8('中\r\n文')) == "'中\\r\\n文'" != repr('中\r\n文') # Test for \r, \n
True
''' '''
if str.find(self, "'") >= 0 and str.find(self, '"') < 0: # only single quote exists if str.find(self, "'") >= 0 and str.find(self, '"') < 0: # only single quote exists
return '"' + unicode(self, 'utf-8').translate(repr_escape_tab).encode('utf-8') + '"' return '"' + unicode(self, 'utf-8').translate(repr_escape_tab).encode('utf-8') + '"'
@@ -578,11 +579,9 @@ if __name__ == '__main__':
7 7
>>> a=Utf8('а б ц д е а б ц д е а\\tб ц д е') >>> a=Utf8('а б ц д е а б ц д е а\\tб ц д е')
>>> a.split() >>> a.split()
['а', 'б', 'ц', 'д', 'е', 'а', 'б', 'ц', 'д', ['а', 'б', 'ц', 'д', 'е', 'а', 'б', 'ц', 'д', 'е', 'а', 'б', 'ц', 'д', 'е']
'е', 'а', 'б', 'ц', 'д', 'е']
>>> a.rsplit() >>> a.rsplit()
['а', 'б', 'ц', 'д', 'е', 'а', 'б', 'ц', 'д', ['а', 'б', 'ц', 'д', 'е', 'а', 'б', 'ц', 'д', 'е', 'а', 'б', 'ц', 'д', 'е']
'е', 'а', 'б', 'ц', 'д', 'е']
>>> a.expandtabs().split('б') >>> a.expandtabs().split('б')
['а ', ' ц д е а ', ' ц д е а ', ' ц д е'] ['а ', ' ц д е а ', ' ц д е а ', ' ц д е']
>>> a.expandtabs().rsplit('б') >>> a.expandtabs().rsplit('б')
@@ -630,8 +629,7 @@ if __name__ == '__main__':
1 1
>>> s.count('Є', 0, 5) >>> s.count('Є', 0, 5)
0 0
>>> Utf8( >>> Utf8("Parameters: '%(проба)s', %(probe)04d, %(проба2)s") % { u"проба": s,
"Parameters: '%(проба)s', %(probe)04d, %(проба2)s") % { u"проба": s,
... "not used": "???", "probe": 2, "проба2": u"ПРоба Probe" } ... "not used": "???", "probe": 2, "проба2": u"ПРоба Probe" }
"Parameters: 'ПРоба Є PRobe', 0002, ПРоба Probe" "Parameters: 'ПРоба Є PRobe', 0002, ПРоба Probe"
>>> a=Utf8(u"Параметр: (%s)-(%s)-[%s]") >>> a=Utf8(u"Параметр: (%s)-(%s)-[%s]")
@@ -694,8 +692,7 @@ if __name__ == '__main__':
аАбБвВгГґҐдДеЕєЄжЖзЗиИіІїЇйЙкКлЛмМнНоОпПрРсСтТуУфФхХцЦчЧшШщЩьЬюЮяЯ аАбБвВгГґҐдДеЕєЄжЖзЗиИіІїЇйЙкКлЛмМнНоОпПрРсСтТуУфФхХцЦчЧшШщЩьЬюЮяЯ
>>> Utf8().join(sorted(c.decode(), key=sort_key)) # convert to unicode for better performance >>> Utf8().join(sorted(c.decode(), key=sort_key)) # convert to unicode for better performance
'аАбБвВгГґҐдДеЕєЄжЖзЗиИіІїЇйЙкКлЛмМнНоОпПрРсСтТуУфФхХцЦчЧшШщЩьЬюЮяЯ' 'аАбБвВгГґҐдДеЕєЄжЖзЗиИіІїЇйЙкКлЛмМнНоОпПрРсСтТуУфФхХцЦчЧшШщЩьЬюЮяЯ'
>>> for result in sorted( >>> for result in sorted(["Іа", "Астро", u"гала", Utf8("Гоша"), "Єва", "шовк", "аякс", "Їжа",
["Іа", "Астро", u"гала", Utf8("Гоша"), "Єва", "шовк", "аякс", "Їжа",
... "ґанок", Utf8("Дар'я"), "білінг", "веб", u"Жужа", "проба", u"тест", ... "ґанок", Utf8("Дар'я"), "білінг", "веб", u"Жужа", "проба", u"тест",
... "абетка", "яблуко", "Юляся", "Київ", "лимонад", "ложка", "Матриця", ... "абетка", "яблуко", "Юляся", "Київ", "лимонад", "ложка", "Матриця",
... ], key=sort_key): ... ], key=sort_key):
@@ -722,6 +719,7 @@ if __name__ == '__main__':
шовк <type 'str'> шовк <type 'str'>
Юляся <type 'str'> Юляся <type 'str'>
яблуко <type 'str'> яблуко <type 'str'>
>>> a=Utf8("中文字") >>> a=Utf8("中文字")
>>> L=list(a) >>> L=list(a)
>>> L >>> L
@@ -734,8 +732,7 @@ if __name__ == '__main__':
>>> a="中文字" # standard str type >>> a="中文字" # standard str type
>>> L=list(a) >>> L=list(a)
>>> L >>> L
['\\xe4', '\\xb8', '\\xad', '\\xe6', '\\x96', '\\x87', ['\\xe4', '\\xb8', '\\xad', '\\xe6', '\\x96', '\\x87', '\\xe5', '\\xad', '\\x97']
'\\xe5', '\\xad', '\\x97']
>>> from string import maketrans >>> from string import maketrans
>>> str_tab=maketrans('PRobe','12345') >>> str_tab=maketrans('PRobe','12345')
>>> unicode_tab={ord(u'П'):ord(u'Ж'), >>> unicode_tab={ord(u'П'):ord(u'Ж'),
+18 -14
View File
@@ -2,11 +2,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
This file is part of the web2py Web Framework | This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
This file specifically includes utilities for security. This file specifically includes utilities for security.
--------------------------------------------------------
""" """
import threading import threading
@@ -65,7 +66,7 @@ def AES_new(key, IV=None):
def compare(a, b): def compare(a, b):
""" compares two strings and not vulnerable to timing attacks """ """ Compares two strings and not vulnerable to timing attacks """
if len(a) != len(b): if len(a) != len(b):
return False return False
result = 0 result = 0
@@ -75,7 +76,7 @@ def compare(a, b):
def md5_hash(text): def md5_hash(text):
""" Generate a md5 hash with the given text """ """ Generates a md5 hash with the given text """
return md5(text).hexdigest() return md5(text).hexdigest()
def simple_hash(text, key='', salt='', digest_alg='md5'): def simple_hash(text, key='', salt='', digest_alg='md5'):
@@ -177,7 +178,7 @@ def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
def initialize_urandom(): def initialize_urandom():
""" """
This function and the web2py_uuid follow from the following discussion: This function and the web2py_uuid follow from the following discussion:
http://groups.google.com/group/web2py-developers/browse_thread/thread/7fd5789a7da3f09 `http://groups.google.com/group/web2py-developers/browse_thread/thread/7fd5789a7da3f09`
At startup web2py compute a unique ID that identifies the machine by adding At startup web2py compute a unique ID that identifies the machine by adding
uuid.getnode() + int(time.time() * 1e3) uuid.getnode() + int(time.time() * 1e3)
@@ -225,7 +226,7 @@ UNPACKED_CTOKENS, HAVE_URANDOM = initialize_urandom()
def fast_urandom16(urandom=[], locker=threading.RLock()): def fast_urandom16(urandom=[], locker=threading.RLock()):
""" """
this is 4x faster than calling os.urandom(16) and prevents This is 4x faster than calling os.urandom(16) and prevents
the "too many files open" issue with concurrent access to os.urandom() the "too many files open" issue with concurrent access to os.urandom()
""" """
try: try:
@@ -243,7 +244,7 @@ def fast_urandom16(urandom=[], locker=threading.RLock()):
def web2py_uuid(ctokens=UNPACKED_CTOKENS): def web2py_uuid(ctokens=UNPACKED_CTOKENS):
""" """
This function follows from the following discussion: This function follows from the following discussion:
http://groups.google.com/group/web2py-developers/browse_thread/thread/7fd5789a7da3f09 `http://groups.google.com/group/web2py-developers/browse_thread/thread/7fd5789a7da3f09`
It works like uuid.uuid4 except that tries to use os.urandom() if possible It works like uuid.uuid4 except that tries to use os.urandom() if possible
and it XORs the output with the tokens uniquely associated with this machine. and it XORs the output with the tokens uniquely associated with this machine.
@@ -263,12 +264,15 @@ REGEX_IPv4 = re.compile('(\d+)\.(\d+)\.(\d+)\.(\d+)')
def is_valid_ip_address(address): def is_valid_ip_address(address):
""" """
>>> is_valid_ip_address('127.0') Examples:
False Better than a thousand words::
>>> is_valid_ip_address('127.0.0.1')
True >>> is_valid_ip_address('127.0')
>>> is_valid_ip_address('2001:660::1') False
True >>> is_valid_ip_address('127.0.0.1')
True
>>> is_valid_ip_address('2001:660::1')
True
""" """
# deal with special cases # deal with special cases
if address.lower() in ('127.0.0.1', 'localhost', '::1', '::ffff:127.0.0.1'): if address.lower() in ('127.0.0.1', 'localhost', '::1', '::ffff:127.0.0.1'):
+802 -737
View File
File diff suppressed because it is too large Load Diff
+15 -14
View File
@@ -2,11 +2,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
This file is part of the web2py Web Framework | This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
The widget is called from web2py. The widget is called from web2py
----------------------------------
""" """
import datetime import datetime
@@ -140,7 +141,7 @@ class web2pyDialog(object):
bg_color = 'white' bg_color = 'white'
root.withdraw() root.withdraw()
self.root = Tkinter.Toplevel(root, bg=bg_color) self.root = Tkinter.Toplevel(root, bg=bg_color)
self.root.resizable(0,0) self.root.resizable(0,0)
self.root.title(ProgramName) self.root.title(ProgramName)
@@ -379,7 +380,7 @@ class web2pyDialog(object):
t.start() t.start()
def checkTaskBar(self): def checkTaskBar(self):
""" Check taskbar status """ """ Checks taskbar status """
if self.tb.status: if self.tb.status:
if self.tb.status[0] == self.tb.EnumStatus.QUIT: if self.tb.status[0] == self.tb.EnumStatus.QUIT:
@@ -401,7 +402,7 @@ class web2pyDialog(object):
self.root.after(1000, self.checkTaskBar) self.root.after(1000, self.checkTaskBar)
def update(self, text): def update(self, text):
""" Update app text """ """ Updates app text """
try: try:
self.text.configure(state='normal') self.text.configure(state='normal')
@@ -411,7 +412,7 @@ class web2pyDialog(object):
pass # ## this should only happen in case app is destroyed pass # ## this should only happen in case app is destroyed
def connect_pages(self): def connect_pages(self):
""" Connect pages """ """ Connects pages """
#reset the menu #reset the menu
available_apps = [arq for arq in os.listdir('applications/') available_apps = [arq for arq in os.listdir('applications/')
if os.path.exists( if os.path.exists(
@@ -423,7 +424,7 @@ class web2pyDialog(object):
label=url, command=lambda u=url: start_browser(u)) label=url, command=lambda u=url: start_browser(u))
def quit(self, justHide=False): def quit(self, justHide=False):
""" Finish the program execution """ """ Finishes the program execution """
if justHide: if justHide:
self.root.withdraw() self.root.withdraw()
else: else:
@@ -450,13 +451,13 @@ class web2pyDialog(object):
sys.exit(0) sys.exit(0)
def error(self, message): def error(self, message):
""" Show error message """ """ Shows error message """
import tkMessageBox import tkMessageBox
tkMessageBox.showerror('web2py start server', message) tkMessageBox.showerror('web2py start server', message)
def start(self): def start(self):
""" Start web2py server """ """ Starts web2py server """
password = self.password.get() password = self.password.get()
@@ -536,7 +537,7 @@ class web2pyDialog(object):
return False return False
def stop(self): def stop(self):
""" Stop web2py server """ """ Stops web2py server """
self.button_start.configure(state='normal') self.button_start.configure(state='normal')
self.button_stop.configure(state='disabled') self.button_stop.configure(state='disabled')
@@ -549,7 +550,7 @@ class web2pyDialog(object):
self.tb.SetServerStopped() self.tb.SetServerStopped()
def update_canvas(self): def update_canvas(self):
""" Update canvas """ """ Updates canvas """
try: try:
t1 = os.path.getsize('httpserver.log') t1 = os.path.getsize('httpserver.log')
@@ -1054,7 +1055,7 @@ def start_schedulers(options):
def start(cron=True): def start(cron=True):
""" Start server """ """ Starts server """
# ## get command line arguments # ## get command line arguments
+3 -3
View File
@@ -2,9 +2,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
This file is part of the web2py Web Framework | This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
""" """
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher from SimpleXMLRPCServer import SimpleXMLRPCDispatcher