-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
52 lines (43 loc) · 1.27 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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
" Provides some helper functions. "
import re
def remove_anchor(text):
" remove anchor from URL"
if not text:
return text
pos = text.find(u'#')
if pos != -1:
return text[0:pos]
else:
return text
def CachedProperty(func):
""" Returns a cached property that is calculated by function func"""
def get(self):
try:
return self._property_cache[func]
except AttributeError:
self._property_cache = { }
x = self._property_cache[func] = func(self)
return x
except KeyError:
x = self._property_cache[func] = func(self)
return x
return property(get)
def parseChmURL(url):
'''
url:unicode
return: tuple(bool,unicode,unicode)
first item tell if it's a url pointing to another .CHM
second item is chm file,
third item is page.
'''
assert isinstance(url, unicode)
# [scheme] ms-its:[chmpath]::[pagepath]
pattern = re.compile(u'^ms-its:(.*)::(.*)$', re.I)
match = pattern.search(url)
if match:
newchmfile = match.group(1)
page = match.group(2)
return (True, newchmfile, page)
return (False, None, None)