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