| 1 | # LinkExchange - Universal link exchange service client |
|---|
| 2 | # Copyright (C) 2009 Konstantin Korikov |
|---|
| 3 | # |
|---|
| 4 | # This library is free software; you can redistribute it and/or |
|---|
| 5 | # modify it under the terms of the GNU Lesser General Public |
|---|
| 6 | # License as published by the Free Software Foundation; either |
|---|
| 7 | # version 2.1 of the License, or (at your option) any later version. |
|---|
| 8 | # |
|---|
| 9 | # This library is distributed in the hope that it will be useful, |
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 | # Lesser General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU Lesser General Public |
|---|
| 15 | # License along with this library; if not, write to the Free Software |
|---|
| 16 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 | # |
|---|
| 18 | # NOTE: In the context of the Python environment, I interpret "dynamic |
|---|
| 19 | # linking" as importing -- thus the LGPL applies to the contents of |
|---|
| 20 | # the modules, but make no requirements on code importing these |
|---|
| 21 | # modules. |
|---|
| 22 | |
|---|
| 23 | import urlparse |
|---|
| 24 | import Cookie |
|---|
| 25 | import os |
|---|
| 26 | import os.path |
|---|
| 27 | |
|---|
| 28 | class ClientError(Exception): |
|---|
| 29 | "Generic client error class" |
|---|
| 30 | |
|---|
| 31 | class ClientNetworkError(ClientError): |
|---|
| 32 | "Network error occurred when connecting and retrieving client data" |
|---|
| 33 | |
|---|
| 34 | class ClientDataError(ClientError): |
|---|
| 35 | "Client data parsing/processing error" |
|---|
| 36 | |
|---|
| 37 | class ClientDataAccessError(ClientError): |
|---|
| 38 | "Concurrent data access error" |
|---|
| 39 | |
|---|
| 40 | class PageRequest(object): |
|---|
| 41 | """ |
|---|
| 42 | Represents HTTP request. |
|---|
| 43 | """ |
|---|
| 44 | def __init__(self, url=None, type=None, host=None, uri=None, |
|---|
| 45 | cookies=None, remote_addr=None, meta=None): |
|---|
| 46 | if url: |
|---|
| 47 | type, host, uri, query, fragment = urlparse.urlsplit(url, 'http') |
|---|
| 48 | if not uri: |
|---|
| 49 | uri = '/' |
|---|
| 50 | if query: |
|---|
| 51 | uri += '?' + query |
|---|
| 52 | self.type = type or 'http' |
|---|
| 53 | self.host = host |
|---|
| 54 | self.uri = uri |
|---|
| 55 | if isinstance(cookies, Cookie.BaseCookie): |
|---|
| 56 | cookies = dict([(k, v.value) for k, v in cookies.items()]) |
|---|
| 57 | self.cookies = cookies or {} |
|---|
| 58 | self.remote_addr = remote_addr |
|---|
| 59 | self.meta = meta or {} |
|---|
| 60 | |
|---|
| 61 | def url(self): |
|---|
| 62 | return '%s://%s%s' % (self.type, self.host, self.uri) |
|---|
| 63 | |
|---|
| 64 | class PageResponse(object): |
|---|
| 65 | """ |
|---|
| 66 | Represents HTTP response. |
|---|
| 67 | """ |
|---|
| 68 | def __init__(self, status=200, body='', headers=None): |
|---|
| 69 | self.status = status |
|---|
| 70 | self.body = body |
|---|
| 71 | self.headers = headers or {} |
|---|
| 72 | |
|---|
| 73 | class BaseClient(object): |
|---|
| 74 | """ |
|---|
| 75 | Base class for link exchange service clients. |
|---|
| 76 | """ |
|---|
| 77 | def get_raw_links(self, request): |
|---|
| 78 | """ |
|---|
| 79 | Returns list of raw HTML link codes for a given page or special tags if |
|---|
| 80 | no links available. |
|---|
| 81 | |
|---|
| 82 | @param request: PageRequest object |
|---|
| 83 | @return: list of unicode strings with raw HTML |
|---|
| 84 | """ |
|---|
| 85 | return [] |
|---|
| 86 | |
|---|
| 87 | def get_html_links(self, request): |
|---|
| 88 | """ |
|---|
| 89 | Returns HTML code with links for given page. The result will be |
|---|
| 90 | formatted according to service features and settings. |
|---|
| 91 | |
|---|
| 92 | @param request: PageRequest object |
|---|
| 93 | @return: unicode string with HTML |
|---|
| 94 | """ |
|---|
| 95 | return u'' |
|---|
| 96 | |
|---|
| 97 | def content_filter(self, request, content): |
|---|
| 98 | """ |
|---|
| 99 | Clients that do content filtering implements this method. |
|---|
| 100 | |
|---|
| 101 | @param request: PageRequest object |
|---|
| 102 | @param content: HTML content (full page or fragment) as unicode string |
|---|
| 103 | @return: filtered content as unicode string |
|---|
| 104 | """ |
|---|
| 105 | return content |
|---|
| 106 | |
|---|
| 107 | def handle_request(self, request): |
|---|
| 108 | """ |
|---|
| 109 | Clients that handles HTTP requests implements this method. |
|---|
| 110 | |
|---|
| 111 | @param request: PageRequest object |
|---|
| 112 | @return: PageResponse object |
|---|
| 113 | """ |
|---|
| 114 | return PageResponse(status=404) |
|---|
| 115 | |
|---|
| 116 | def refresh_db(self, request): |
|---|
| 117 | """ |
|---|
| 118 | Force to refresh client database. |
|---|
| 119 | |
|---|
| 120 | @param request: PageRequest object |
|---|
| 121 | """ |
|---|