Changeset 276:080785bf7633 in hatta-dev
Legend:
- Unmodified
- Added
- Removed
-
hatta.py
r274 r276 1 1 #!/usr/bin/python 2 2 # -*- coding: utf-8 -*- 3 4 # @copyright: 2008-2009 Radomir Dopieralski <hatta@sheep.art.pl> 5 # @license: GNU GPL, see COPYING for details. 3 6 4 7 """ … … 176 179 177 180 def _parse_environ(self): 181 """Check the environment variables for options.""" 182 178 183 prefix = 'HATTA_' 179 184 settings = {} … … 185 190 186 191 def _parse_args(self): 192 """Check the commandline arguments for options.""" 193 187 194 import optparse 188 195 parser = optparse.OptionParser() … … 222 229 223 230 def _parse_files(self, files=()): 231 """Check the config files for options.""" 232 224 233 import ConfigParser 234 # XXX TODO 225 235 226 236 class WikiStorage(object): … … 262 272 263 273 def _find_repo_path(self, path): 274 """Go up the directory tree looking for a repository.""" 275 264 276 while not os.path.isdir(os.path.join(path, ".hg")): 265 277 old_path, path = path, os.path.dirname(path) … … 281 293 282 294 def save_file(self, title, file_name, author=u'', comment=u'', parent=None): 295 """Save an existing file as specified page.""" 296 283 297 user = author.encode('utf-8') or _(u'anon').encode('utf-8') 284 298 text = comment.encode('utf-8') or _(u'comment').encode('utf-8') … … 331 345 332 346 def save_text(self, title, text, author=u'', comment=u'', parent=None): 347 """Save text or data as specified page.""" 348 333 349 try: 334 350 temp_path = tempfile.mkdtemp(dir=self.path) … … 372 388 373 389 def page_file_meta(self, title): 390 """Get page's inode number, size and last modification time.""" 391 374 392 try: 375 393 (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, … … 380 398 381 399 def page_meta(self, title): 400 """Get page's revision, date, last editor and his edit comment.""" 401 382 402 filectx_tip = self._find_filectx(title) 383 403 if filectx_tip is None: … … 398 418 399 419 def page_mime(self, title): 420 """Guess page's mime type ased on corresponding file name.""" 421 400 422 file_path = self._file_path(title) 401 423 return page_mime(file_path) 402 424 403 425 def _find_filectx(self, title): 426 """Find the last revision in which the file existed.""" 427 404 428 repo_file = self._title_to_file(title) 405 429 changectx = self.repo.changectx('tip') … … 415 439 416 440 def page_history(self, title): 441 """Iterate over the page's history.""" 442 417 443 filectx_tip = self._find_filectx(title) 418 444 if filectx_tip is None: … … 429 455 430 456 def page_revision(self, title, rev): 457 """Get the contents of specified revision of the page.""" 458 431 459 filectx_tip = self._find_filectx(title) 432 460 if filectx_tip is None: … … 438 466 439 467 def history(self): 468 """Iterate over the history of entire wiki.""" 469 440 470 changectx = self.repo.changectx('tip') 441 471 maxrev = changectx.rev() … … 457 487 458 488 def all_pages(self): 489 """Iterate over the titles of all pages in the wiki.""" 490 459 491 for filename in os.listdir(self.path): 460 492 if (os.path.isfile(os.path.join(self.path, filename)) … … 748 780 749 781 def parse_line(self, line): 782 """Find all the line-level markup and return HTML for it.""" 783 750 784 for m in self.markup_re.finditer(line): 751 785 func = getattr(self, "line_%s" % m.lastgroup) … … 754 788 def parse(self, lines, wiki_link, wiki_image, wiki_syntax=None, 755 789 wiki_math=None): 790 """Parse a list of lines of wiki markup, yielding HTML for it.""" 791 756 792 def key(line): 757 793 match = self.block_re.match(line) … … 844 880 845 881 def split_text(self, text): 882 """Split text into words. Uses Japanese splitter if available.""" 883 846 884 for match in self.word_pattern.finditer(text): 847 885 word = match.group(0) … … 861 899 862 900 def filter_words(self, words): 901 """Filter out stop words, numbers, too long words, etc.""" 863 902 for word in words: 864 903 if len(word) >= 25: … … 870 909 871 910 def count_words(self, words): 911 """Check how many times each word appears in the list.""" 912 872 913 count = {} 873 914 for word in words: … … 876 917 877 918 def add_words(self, title, text): 919 """Add words to the word index for specified page.""" 920 878 921 encoded_title = title.encode('utf-8', 'backslashreplace') 879 922 if text: … … 896 939 897 940 def add_links(self, title, links_and_labels): 941 """Add links to the link index for specified page.""" 942 898 943 links, labels = links_and_labels 899 944 self.links.sync() … … 905 950 906 951 def regenerate_backlinks(self): 952 """Create backlinks indices based on the links indices.""" 953 907 954 self.links.sync() 908 955 for key in self.backlinks: … … 918 965 919 966 def update_backlinks(self, title, old_links, new_links): 967 """Updates backlinks index for specified page.""" 968 920 969 encoded_title = title.encode('utf-8', 'backslashreplace') 921 970 self.backlinks.sync() … … 938 987 939 988 def page_backlinks(self, title): 989 """Get the backlinks to specified page.""" 990 940 991 timestamp = os.stat(self.backlinks_file).st_mtime 941 992 if timestamp > self.backlinks_timestamp: … … 947 998 948 999 def page_links(self, title): 1000 """Get link targets from specified page.""" 1001 949 1002 timestamp = os.stat(self.links_file).st_mtime 950 1003 if timestamp > self.links_timestamp: … … 955 1008 956 1009 def page_labels(self, title): 1010 """Get link labels from specified page.""" 1011 957 1012 encoded_title = title.encode('utf-8', 'backslashreplace') 958 1013 return self.labels.get(encoded_title, []) 959 1014 960 1015 def find(self, words): 1016 """Find words in the pages.""" 1017 961 1018 first = words[0] 962 1019 rest = words[1:] … … 1008 1065 1009 1066 def wiki_link(self, addr, label, class_='wiki', image=None): 1067 """Create HTML for a wiki link.""" 1068 1010 1069 text = werkzeug.escape(label) 1011 1070 if external_link(addr): … … 1035 1094 1036 1095 def wiki_image(self, addr, alt, class_='wiki'): 1096 """Create HTML for a wiki image.""" 1097 1037 1098 chunk = '' 1038 1099 if external_link(addr): … … 1056 1117 1057 1118 def get_author(self): 1119 """Try to guess the author name. Use IP address as last resort.""" 1120 1058 1121 author = (self.form.get("author") 1059 1122 or werkzeug.url_unquote(self.cookies.get("author", "")) … … 1063 1126 1064 1127 def _get_file_stream(self): 1128 """Save all the POSTs to temporary files.""" 1129 1065 1130 class FileWrapper(file): 1066 1131 def __init__(self, f): … … 1088 1153 1089 1154 def cleanup(self): 1155 """Clean up the temporary files created by POSTs.""" 1156 1090 1157 for temp_path in self.tmpfiles: 1091 1158 try: … … 1169 1236 1170 1237 def html_page(self, request, title, content, page_title=u''): 1238 """The main page template.""" 1239 1171 1240 rss = request.adapter.build(self.rss, method='GET') 1172 1241 atom = request.adapter.build(self.atom, method='GET')
Note: See TracChangeset
for help on using the changeset viewer.
