This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
/
Utils.py
885 lines (713 loc) · 25.1 KB
/
Utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# -*- coding: iso-8859-1 -*
# python
import string
import logging
import re
import cgi
from cgi import escape
from pprint import pprint
from random import shuffle
from math import floor
from urllib import unquote
import StringIO
import itertools
try:
from zLOG import LOG, INFO, ERROR
except:
INFO = "INFO"; ERROR = "ERROR"
def LOG(s, *a, **k):
print s, a, k
try:
from Products.PythonScripts.standard import html_quote, newline_to_br, \
structured_text, url_quote, url_quote_plus
except ImportError:
print "Could not import from Zope"
try:
from stripogram import html2text, html2safehtml
except ImportError:
try:
from Products.stripogram import html2text, html2safehtml
except ImportError:
html2text = html2safehtml = None
try:
import SilverCity
except ImportError:
SilverCity = None
try:
import SilvercityOptimizer
except ImportError:
SilvercityOptimizer = None
try:
from addhrefs import addhrefs
except ImportError:
LOG("Peterbecom.Utils", ERROR, "addhrefs not installed!")
def addhrefs(s, *a, **k):
return s
def niceboolean(value):
if type(value) is bool:
return value
falseness = ('','no','off','false','none','0', 'f')
return str(value).lower().strip() not in falseness
from subprocess import Popen, PIPE
def command(s):
proc = Popen(s, shell=True, stdout=PIPE, stderr=PIPE)
return proc.communicate()
SplitRegEx = re.compile(r'<!--\s*split\s*-->', re.I)
tags_start = re.compile(r'<texmix>|<texmix inline=".*?">', re.I)
tags_end = re.compile(r'</texmix>', re.I)
texmix_reg = re.compile(r'(<texmix>|<texmix inline=".*?">)(.*?)</texmix>', re.I|re.DOTALL)
pre_reg = re.compile(r'<pre>(.*?)</pre>', re.I|re.DOTALL)
starttag_regex = re.compile('<.*?>', re.MULTILINE|re.DOTALL)
singletontag_regex = re.compile('<.*?/>', re.MULTILINE|re.DOTALL)
endtag_regex = re.compile('</.*?>', re.MULTILINE|re.DOTALL)
multiple_linebreaks = re.compile('\n\s+\n|\n\n')
def dehtmlify(html):
""" Try to convert an HTML chunk to text """
li_tags = re.compile('<li>\s*',re.I)
html = li_tags.sub(' * ', html)
html = multiple_linebreaks.sub('\n', html)
html = starttag_regex.sub('',html)
html = singletontag_regex.sub('', html)
html = endtag_regex.sub('\n', html)
return html.strip()
def _filterPasswordFields(items):
result = []
for k, v in items:
if k.lower().find('passw') >-1:
v = '<password obscured>'
result.append((k, v))
return result
hide_key={'HTTP_AUTHORIZATION':1,
'HTTP_CGI_AUTHORIZATION': 1,
}.has_key
def REQUEST2String(REQUEST):
result="form\n"
row = "\t%s: %s\n"
for k,v in _filterPasswordFields(REQUEST.form.items()):
result=result + row % (escape(k), escape(repr(v)))
result += "cookies\n"
for k,v in _filterPasswordFields(REQUEST.cookies.items()):
result=result + row % (escape(k), escape(repr(v)))
result += "lazy items\n"
for k,v in _filterPasswordFields(REQUEST._lazies.items()):
result=result + row % (escape(k), escape(repr(v)))
result += "other\n"
for k,v in _filterPasswordFields(REQUEST.other.items()):
if k in ('PARENTS','RESPONSE'): continue
result=result + row % (escape(k), escape(repr(v)))
for n in "0123456789":
key = "URL%s"%n
try: result=result + row % (key, escape(REQUEST[key]))
except KeyError: pass
for n in "0123456789":
key = "BASE%s"%n
try: result=result + row % (key, escape(REQUEST[key]))
except KeyError: pass
result=result+"environ\n"
for k,v in REQUEST.environ.items():
if not hide_key(k):
result=result + row % (escape(k), escape(repr(v)))
return result
def cookId(id, title, file):
if not id and hasattr(file,'filename'):
filename=file.filename
title=title or filename
id=filename[max(filename.rfind('/'),
filename.rfind('\\'),
filename.rfind(':'),
)+1:]
return id, title
def ss(s):
return s.lower().strip()
def same_type(obj1, obj2):
return type(obj1)==type(obj2)
def anyTrue(pred, seq):
""" http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/212959 """
return True in itertools.imap(pred,seq)
def uniqify(seq, idfun=None): # Alex Martelli ******* order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
if seen.has_key(marker): continue
seen[marker] = 1
result.append(item)
return result
def iuniqify(seq):
""" return a list of strings unique case insensitively.
If the input is ['foo','bar','Foo']
return ['foo','bar']
"""
def idfunction(x):
if isinstance(x, basestring):
return x.lower()
else:
return x
return uniqify(seq, idfunction)
def moveUpListelement(element, list):
""" move an element in a _mutable_ list up one position
if possible. If the element is a list, then the function
is self recursivly called for each subelement.
"""
if type(element)==type([]):
for subelement in element:
moveUpListelement(subelement, list)
if element==list[0]:
pass
elif element in list:
i=list.index(element)
list[i], list[i-1] = list[i-1], list[i]
def getDomain(findlist):
_all = '(com|co.uk|de|se|it|fr|com.br|fi|be|nl|com.au|co.in|es|'
_all += 'com.ar|dk|pl|ca|ie|ch|cl|com.tr|ae|co.nz|as|at|com.my|'
_all += 'com.mx|pt|co.jp|co.hu|net|com.ru|lt|sk|com.uy|com.sg|'
_all += 'com.co|com.pk|com.mt|com.tw|co.th|com.gr|com.hk|mu|'
_all += 'com.ni|com.pa|ru|no|ro|co.za|co.cr|com.ph|com.bo|'
_all += 'com.vn|hn|lu|co.ug|com.ua|com.vn|com.gt|'
_all += 'com.pe|co.kr|com.sa|co.il|lv|co.id|com.do|co.ve)'
_domains = '(yahoo|google|earthlink|msn|alltheweb|aol|comcast|'
_domains += 'blueyonder|gogole|mysearch|mysearch.myway|imdb)'
_transl = {'gogole':'google', 'mysearch.myway':'mysearch',
'imdb':'google'}
f=list(findlist[0])
while '' in f:
f.remove('')
f = ':'+':'.join(f)+':'
re_top = re.compile(r":%s:"%_all, re.I)
try:
top = re_top.findall(f)[0]
except IndexError:
raise IndexError, "No top domain found in %s from %s"%(f, r":%s:"%_all)
re_domain = re.compile(r":%s:"%_domains, re.I)
domain = re_domain.findall(f)[0]
domain = _transl.get(domain,domain)
return domain, top
## Searchterms
def _translateQstr(s):
""" extra massage to a query string """
return unquote(s)
def generalQstr(s, bracketted=0):
""" those that use 'q=' """
if len(s.split('?')) > 1:
s = s.split('?')[1]
parsed = cgi.parse_qs(s)
if parsed.get('q'):
s = parsed.get('q')[0]
else:
return ""
s = _translateQstr(s)
if s and bracketted:
s = '(%s)'%s
return s
def queryQstr(s, bracketted=0):
""" those that use 'query=' """
if len(s.split('?')) > 1:
s = s.split('?')[1]
parsed = cgi.parse_qs(s)
if parsed.get('query'):
s = parsed.get('query')[0]
else:
return ""
s = _translateQstr(s)
if s and bracketted:
s = '(%s)'%s
return s
def googleQstr(s, bracketted=0):
return generalQstr(s, bracketted)
def yahooQstr(s, bracketted=0):
if len(s.split('?')) > 1:
s = s.split('?')[1]
parsed = cgi.parse_qs(s)
if parsed.get('p'):
s = parsed.get('p')[0]
elif parsed.get('va'):
s = parsed.get('va')[0]
else:
return ""
s = _translateQstr(s)
if s and bracketted:
s = '(%s)'%s
return s
def earthlinkQstr(s, bracketted=0):
return generalQstr(s, bracketted)
## /Searchterms
def renderSQLHTML(code):
""" make HTML representation of SQL statement """
return code
def timeSince(firstdate, seconddate, afterword=None):
"""
Use two date objects to return in plain english the difference between them.
E.g. "3 years and 2 days"
or "1 year and 3 months and 1 day"
If less than 1 day, return number of hours.
If there is "no difference" between them, return false.
"""
# Language constants
HOUR = 'hour'
HOURS = 'hours'
YEAR = 'year'
YEARS = 'years'
MONTH = 'month'
MONTHS = 'months'
DAY = 'day'
DAYS = 'days'
AND = 'and'
fdo = firstdate
sdo = seconddate
day_difference = int(abs(sdo-fdo))
years = day_difference/365
months = (day_difference % 365)/30
days = (day_difference % 365) % 30
if days == 0 and months == 0:
# use hours
hours=int(abs(sdo-fdo)*24)
if hours == 1:
if afterword is None:
return "1 %s" % (HOUR)
else:
return "1 %s %s" % (HOUR, afterword)
elif hours > 0:
if afterword is None:
return "%s %s" % (hours, HOURS)
else:
return "%s %s %s" % (hours, HOURS, afterword)
else:
# if the difference is smaller than 1 hour,
# return it false
return 0
else:
s = []
if years == 1:
s.append('1 %s'%(YEAR))
elif years > 1:
s.append('%s %s'%(years,YEARS))
if months == 1:
s.append('1 %s'%MONTH)
elif months > 1:
s.append('%s %s'%(months,MONTHS))
if days == 1:
s.append('1 %s'%DAY)
elif days > 1:
s.append('%s %s'%(days,DAYS))
if len(s)>1:
if afterword is None:
return "%s" % (string.join(s,' %s '%AND))
else:
return "%s %s" % (string.join(s,' %s '%AND), afterword)
else:
if afterword is None:
return "%s" % (s[0])
else:
return "%s %s" % (s[0], afterword)
#def LineIndent(text, indent, maxwidth=None):
# """ indent each new line with 'indent' """
# text = indent+text
# text = text.replace('\n','\n%s'%indent)
# return text
def LineIndent(text, indent, maxwidth=None):
""" indent each new line with 'indent' """
if maxwidth:
words = text.split(' ')
lines = []
tmpline = ''
for word in words:
if len(tmpline+' '+word) > maxwidth:
lines.append(tmpline.strip())
tmpline = word
else:
tmpline += ' ' + word
lines.append(tmpline.strip())
start = "\n%s"%indent
return indent + start.join(lines)
else:
text = indent+text
text = text.replace('\n','\n%s'%indent)
return text
def ordinalth(n, html=0):
t = 'th st nd rd th th th th th th'.split()
if n % 100 in (11, 12, 13): #special case
if html:
return '%d'%n+'<sup>th</sup>'
else:
return '%dth' % n
if html:
return str(n) + '<sup>%s</sup>'% t[n % 10]
else:
return str(n) + t[n % 10]
def replace_special_chars(text, simplify=1, html_encoding=0):
""" Replace special characters with placeholder keys back and forth.
The reason for doing this is that structured_text() doesn't support
special characters such as åäö.
"""
reps = {'Aring':'Å', 'Auml':'Ä', 'Ouml':'Ö',
'aring':'å', 'auml':'ä', 'ouml':'ö',
'Egrave':'È', 'Eacute':'É',
'egrave':'è', 'eacute':'é',
}
if simplify:
for k, v in reps.items():
if html_encoding:
k='&%s;'%k
else:
k='__%s__'%k
text = text.replace(v,k)
else:
for k, v in reps.items():
k='__%s__'%k
text = text.replace(k,v)
return text
def getTexes(text, allinfo=0):
""" return the TeX chunks """
nr_tags_start = len(tags_start.findall(text))
nr_tags_end = len(tags_end.findall(text))
if nr_tags_start != nr_tags_end:
m = "%s start tags and %s end tags"%(nr_tags_start, nr_tags_end)
raise "TagMismatch", m
if allinfo:
return [x for x in texmix_reg.findall(text)]
else:
return [x[1].strip() for x in texmix_reg.findall(text)]
PYTHON_SYNTAX = 1
CPP_SYNTAX = 2
SQL_SYNTAX = 3
XML_SYNTAX = 4
CSS_SYNTAX = 5
def SimpleSyntaxHighlight(text, syntax):
""" just go straigt at it """
strconverters = {'python':PYTHON_SYNTAX, 'c++':CPP_SYNTAX,
'sql':SQL_SYNTAX, 'xml':XML_SYNTAX,
'css':CSS_SYNTAX}
syntax = strconverters.get(str(syntax).lower().strip(), syntax)
if syntax == CPP_SYNTAX:
g = SilverCity.CPP.CPPHTMLGenerator()
elif syntax == PYTHON_SYNTAX:
g = SilverCity.Python.PythonHTMLGenerator()
elif syntax == SQL_SYNTAX:
g = SilverCity.SQL.SQLHTMLGenerator()
elif syntax == XML_SYNTAX:
g = SilverCity.XML.XMLHTMLGenerator()
elif syntax == CSS_SYNTAX:
g = SilverCity.CSS.CSSHTMLGenerator()
else:
m = "Dont know what to do with %s"%syntax
raise "UnrecognizedSyntax", m
text = text.replace('<','<').replace('>','>')
text = text.replace('&','&').replace('"','"')
file = StringIO.StringIO()
g.generate_html(file, text)
html = file.getvalue().replace('<br />','').replace('<br/>','')
#html = html.replace('\n','', 1)
file.close()
html = html.replace('\n','<br>')
better = '<br><div class="my_code_default">%s</div>'%html
# remove the following line to not optimize the silvercity output
if SilvercityOptimizer:
better = SilvercityOptimizer.OptimizeSilverCityHTMLOutput(better, xhtml=True)
return better
#text = text.replace('<pre>%s</pre>'%chunk, better)
def SyntaxHighlight(text, syntax):
if not SilverCity:
return text
pre_chunks = pre_reg.findall(text)
if not pre_chunks:
return text
if syntax == CPP_SYNTAX:
g = SilverCity.CPP.CPPHTMLGenerator()
elif syntax == PYTHON_SYNTAX:
g = SilverCity.Python.PythonHTMLGenerator()
elif syntax == SQL_SYNTAX:
g = SilverCity.SQL.SQLHTMLGenerator()
elif syntax == XML_SYNTAX:
g = SilverCity.XML.XMLHTMLGenerator()
elif syntax == CSS_SYNTAX:
g = SilverCity.CSS.CSSHTMLGenerator()
else:
m = "Dont know what to do with %s"%syntax
raise "UnrecognizedSyntax", m
for chunk in pre_chunks:
chunk_norm = chunk.replace('<','<').replace('>','>')
chunk_norm = chunk_norm.replace('&','&').replace('"','"')
file = StringIO.StringIO()
g.generate_html(file, chunk_norm)
html = file.getvalue().replace('<br />','').replace('<br/>','')
html = html.replace('\n','', 1)
file.close()
html = html.replace('\n','<br />')
better = '<br /><div class="my_code_default">%s</div>'%html
# remove the following line to not optimize the silvercity output
if SilvercityOptimizer:
better = SilvercityOptimizer.OptimizeSilverCityHTMLOutput(better, xhtml=True)
text = text.replace('<pre>%s</pre>'%chunk, better)
del g
return text
def nice_structured_text(st):
""" run structured_text() but recognized text like:
"google"n:http://google.com
and attached style="text-decoration:none" on those.
"""
letters = string.letters
hrefn = re.compile("(\"[ %s0-9\n\-\.\,\;\(\)\/\:\/\*\']+\")n(:)([a-zA-Z0-9\@\.\,\?\!\/\:\;\=\&\%%\-\#\~]+)([,]*\s*)" % letters)
found_ununderlined = hrefn.findall(st)
_href_mem = {}
for each in found_ununderlined:
randstr = getRandomString(5)
_href_mem[randstr] = each[0]
for each in found_ununderlined:
each = list(each)
basiceach = [each[0],'n'] + list(each[1:-1])
for mem, title in _href_mem.items():
if title == each[0]:
each[0] = '"%s"'%mem
st = st.replace(''.join(basiceach), ''.join(each))
st = structured_text(st)
for each in found_ununderlined:
e2 = each[2]
if e2[-1] in ['.',',']:
e2 = e2[:-1]
e0 = each[0] # title
for mem, title in _href_mem.items():
if title == each[0]:
e0 = mem
orig_href = '<a href="%s">%s</a>'%(e2, e0)
_good = _href_mem[e0].replace('"','')
new_href = '<a href="%s" style="text-decoration:none">%s</a>'
new_href = new_href%(e2, _good)
st = st.replace(orig_href, new_href)
return st
def mylinker(url):
template = '<a href="%s" rel="nofollow">%s'
return template % (url, url)
sole_ampersand_regex = re.compile(r'\B&\B')
def ShowDescription(text, display_format='', nofollow_rel=False):
"""
Display text, using harmless HTML
"""
text = SplitRegEx.sub('<!--split-->', text)
codesyntax = ''
if same_type(display_format, ()) or same_type(display_format, []):
display_format, codesyntax = display_format
if display_format == 'structuredtext':
#st=_replace_special_chars(text)
st=text
for k,v in {#'<':'<', '>':'>',
'[':'|[|'}.items():
st = st.replace(k,v)
try:
# my structured text
st = nice_structured_text(st)
except:
st = structured_text(st)
for k,v in {'&lt;':'<', '&gt;':'>',
'|[|':'['}.items():
st = st.replace(k,v)
# BUG in structured_text in Zope 2.4.0
# it appends these annoying tags.
for tag in ['<html>','<body>','</body>','</html>']:
st = st.replace(tag, '')
pre_whole_tags = re.compile(r'<pre>.*?</pre>', re.I|re.DOTALL)
pre_tags = pre_whole_tags.findall(st)
mem = {}
for pre_tag in pre_tags:
randstr = '__%s__'%getRandomString()
mem[randstr] = pre_tag
st = st.replace(pre_tag, randstr)
### NEEDS TO BE FIXED!
#st = addhrefs(st, urllinkfunction=mylinker)
for key, tag in mem.items():
st = st.replace(key, tag)
# preserve look of '<!--split-->'
st = st.replace('<p><!--split--></p>','<!--split-->')
# syntax highlighting of code
if str(codesyntax).lower() in ['c++','cpp']:
st = SyntaxHighlight(st, CPP_SYNTAX)
elif str(codesyntax).lower() in ['py','python']:
st = SyntaxHighlight(st, PYTHON_SYNTAX)
elif str(codesyntax).lower() in ['sql']:
st = SyntaxHighlight(st, SQL_SYNTAX)
elif str(codesyntax).lower() in ['xml/html','xml','html']:
st = SyntaxHighlight(st, XML_SYNTAX)
elif str(codesyntax).lower() in ['css','stylesheet']:
st = SyntaxHighlight(st, CSS_SYNTAX)
st = sole_ampersand_regex.sub('&', st)
return st
elif display_format == 'html':
return text
elif display_format == 'texmix':
texes = getTexes(text, 1)
count = 1
for tagused, texstring in texes:
imageid = 'texjpeg-%s.jpg'%count
imagepath = 'texcache/%s'%imageid
imagetag = '<img src="%s" alt="%s" '%(imagepath, "*generated jpeg*")
if tagused.lower().find('inline') > -1:
imagetag += 'class="texmix-inline"'
else:
imagetag += 'class="texmix"'
imagetag += ' />'
text = text.replace(texstring, imagetag)
count += 1
text = text.replace('<texmix>','<span class="texmix">')
text = text.replace('<texmix inline="1">','<span class="texmix-inline">')
text = text.replace('</texmix>','</span>')
format = 'structuredtext'
if codesyntax:
format = [format, codesyntax]
return ShowDescription(text, format)
else:
t = html_quote(text)
t = t.replace('&lt;','<').replace('&gt;','>')
t = t.replace('<!--split-->', '<!--split-->')
if nofollow_rel:
def nofollower(url):
template = '<a href="%s" rel="nofollow">%s</a>'
return template % (url, url)
t = addhrefs(t, urllinkfunction=nofollower)
else:
t = addhrefs(t)
t = newline_to_br(t)
return t
def _ShouldBeNone(result): return result is not None
def _ShouldNotBeNone(result): return result is None
tests = (
# Thank you Bruce Eckels for these
(re.compile("^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$"), _ShouldNotBeNone, "Failed a"),
(re.compile("^[^0-9a-zA-Z]|[^0-9a-zA-Z]$"), _ShouldBeNone, "Failed b"),
(re.compile("([0-9a-zA-Z]{1})\@."), _ShouldNotBeNone, "Failed c"),
(re.compile(".\@([0-9a-zA-Z]{1})"), _ShouldNotBeNone, "Failed d"),
(re.compile(".\.\-.|.\-\..|.\.\..|.\-\-."), _ShouldBeNone, "Failed e"),
(re.compile(".\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_."), _ShouldBeNone, "Failed f"),
(re.compile(".\.([a-zA-Z]{2,3})$|.\.([a-zA-Z]{2,4})$"), _ShouldNotBeNone, "Failed g"),
)
def ValidEmailAddress(address, debug=None):
for test in tests:
if test[1](test[0].search(address)):
if debug: return test[2]
return 0
return 1
def safeId(id, nospaces=0):
""" Just make sure it contains no dodgy characters """
id = _replace_special_chars(id)
lowercase = 'abcdefghijklmnopqrstuvwxyz'
digits = '0123456789'
specials = '_-.'
allowed = lowercase + lowercase.upper() + digits + specials
if not nospaces:
allowed = ' ' + allowed
n_id=[]
allowed_list = list(allowed)
for letter in list(id):
if letter in allowed_list:
n_id.append(letter)
return ''.join(n_id)
def _replace_special_chars(id):
""" be kind to make some replacements """
d={'Ä':'A', 'Å':'A', 'Ö':'O', 'å':'a', 'ä':'a', 'ö':'o',
'é':'e', 'è':'e', 'à':'a'}
for bad, good in d.items():
id = id.replace(bad, good)
return id
def list_makesure(item):
""" make sure item is list """
if type(item) != type([]):
return [item]
else:
return item
def getRandomString(length=10, loweronly=1, numbersonly=0,
lettersonly=0):
""" return a very random string """
_letters = 'abcdefghijklmnopqrstuvwxyz'
if numbersonly:
l = list('0123456789')
elif lettersonly:
l = list(_letters + _letters.upper())
else:
lowercase = _letters+'0123456789'*2
l = list(lowercase + lowercase.upper())
shuffle(l)
s = string.join(l,'')
if len(s) < length:
s = s + getRandomString(loweronly=1)
s = s[:length]
if loweronly:
return s.lower()
else:
return s
def encodeEmailString(email, title=None, nolink=0):
""" just write the email like
<span class="aeh">peter_AT_peterbe_._com</span>
and a 'onLoad' Javascript will convert it to look nice.
The way we show the email address must match the Javascript
that converts it on the fly. """
methods = ['_dot_',
'&#%s;dot_' % ord(' '),
'_dot&#%s;' % ord(' '),
'_._',
'-.-']
shuffle(methods)
# replace . after @
if '@' in list(email):
afterbit = email.split('@')[1]
newbit = afterbit.replace('.', methods[0])
email = email.replace(afterbit, newbit)
methods = ['_', '~']
shuffle(methods)
atsigns = ['at','AT', '&#%s;' % ord('@')]
shuffle(atsigns)
# replace @ with *AT*
email = email.replace('@','%s%s%s'%(methods[0], atsigns[0], methods[0]))
if title is None or title == email:
title = email
spantag = '<span class="aeh">%s</span>'
spantag_link = '<span class="aeh"><a href="mailto:%s">%s</a></span>'
if nolink:
return spantag%email
else:
return spantag_link%(email, title)
def encodeEmailString_OLD(email, title=None):
""" if encode_emaildisplay then use JavaScript to encode it """
if title is None:
title = email
basic = email.replace('@','(at)').replace('.',' dot ')
if title != email:
basic = "%s, %s"%(title, basic)
js_string = """document.write('<a href="mailto:%s">"""%email
js_string += """%s</a>')"""%title
hexed = _hex_string(js_string)
js_script = """<script language="JavaScript">eval(unescape('"""
js_script += hexed + """'))</script>"""
js_script += "<noscript>%s</noscript>"%basic
return js_script
def _hex_string(oldstring):
""" hexify a string """
# Taken from http://www.happysnax.com.au/testemail.php
# See Credits
def _tohex(n):
hs='0123456789ABCDEF'
return hs[int(floor(n/16))]+hs[n%16]
newstring=''
length=len(oldstring)
for i in range(length):
newstring=newstring+'%'+_tohex(ord(oldstring[i]))
return newstring
def emailtesting():
def T(s):
print s, "->", ValidEmailAddress(s)
T("mail@peterbe.com")
T("mail @peterbe.com")
def addhrefstesting():
x='''http://www.frosp.com/pin555.gif
Boo Yah....555 ft'''
print addhrefs(x)
def querystringtesting():
q="va=peter+bengtsson&sourceid=mozilla-search&start=0&start=0&ie=utf-8&oe=utf-8"
print yahooQstr(q)
def runtests():
""" just some misc testing """
#print getRandomString(2, lettersonly=1)
#emailtesting()
#addhrefstesting()
querystringtesting()
if __name__=='__main__':
runtests()