33"""This python module aims to manage
44`DokuWiki <https://www.dokuwiki.org/dokuwiki>`_ wikis by using the
55provided `XML-RPC API <https://www.dokuwiki.org/devel:xmlrpc>`_. It is
6- compatible with python2.7 and python3+.
6+ compatible with python3+.
77
88Installation
99------------
1515Otherwise sources are in `github <https://github.com/fmenabe/python-dokuwiki>`_
1616"""
1717
18- import re
19- import sys
2018import base64
19+ import re
2120import weakref
22- from xml .parsers .expat import ExpatError
23- 24- PY_VERSION = sys .version_info [0 ]
25- if PY_VERSION == 3 :
26- from xmlrpc .client import ServerProxy , Binary , Fault , Transport , SafeTransport , ProtocolError
27- from urllib .parse import quote
28- else :
29- from xmlrpclib import ServerProxy , Binary , Fault , Transport , SafeTransport , ProtocolError
30- from urllib import quote
31- 3221from datetime import datetime , timedelta
22+ from urllib .parse import quote
23+ from xml .parsers .expat import ExpatError
24+ from xmlrpc .client import ServerProxy , Binary , Fault , Transport , SafeTransport , ProtocolError
3325
3426ERR = 'XML or text declaration not at start of entity: line 2, column 0'
3527
3628_URL_RE = re .compile (r'(?P<proto>https?)://(?P<host>[^/]*)(?P<uri>/.*)?' )
3729
3830def date (date ):
39- """DokuWiki returns dates of `xmlrpclib`/` xmlrpc.client` ``DateTime``
31+ """DokuWiki returns dates of `xmlrpc.client` ``DateTime``
4032 type and the format changes between DokuWiki versions ... This function
4133 convert *date* to a `datetime` object.
4234 """
@@ -49,10 +41,7 @@ def utc2local(date):
4941 """DokuWiki returns date with a +0000 timezone. This function convert *date*
5042 to the local time.
5143 """
52- date_offset = (datetime .now () - datetime .utcnow ())
53- # Python < 2.7 don't have the 'total_seconds' method so calculate it by hand!
54- date_offset = (date_offset .microseconds +
55- (date_offset .seconds + date_offset .days * 24 * 3600 ) * 1e6 ) / 1e6
44+ date_offset = (datetime .now () - datetime .utcnow ()).total_seconds ()
5645 date_offset = int (round (date_offset / 60 / 60 ))
5746 return date + timedelta (hours = date_offset )
5847
@@ -88,38 +77,13 @@ def parse_response(self, response):
8877 finally :
8978 return _TransportClass_ .parse_response (self , response )
9079
91- class CookiesTransport2 (_TransportClass_ ):
92- """A Python2 xmlrpclib.Transport subclass that retains cookies."""
93- def __init__ (self ):
94- _TransportClass_ .__init__ (self )
95- self ._cookies = dict ()
96- 97- def send_request (self , connection , handler , request_body ):
98- _TransportClass_ .send_request (self , connection , handler , request_body )
99- # set cookie below handler
100- if self ._cookies :
101- cookies = map (lambda x : x [0 ] + '=' + x [1 ], self ._cookies .items ())
102- connection .putheader ("Cookie" , "; " .join (cookies ))
103- 104- def parse_response (self , response ):
105- """parse and store cookie"""
106- try :
107- for header in response .getheader ("set-cookie" ).split (", " ):
108- # filter 'expire' information
109- if not header .startswith ("D" ):
110- continue
111- cookie = header .split (";" , 1 )[0 ]
112- cookieKey , cookieValue = cookie .split ("=" , 1 )
113- self ._cookies [cookieKey ] = cookieValue
114- finally :
115- return _TransportClass_ .parse_response (self , response )
80+ return CookiesTransport ()
11681
117- return CookiesTransport2 () if PY_VERSION == 2 else CookiesTransport ()
11882
119- class DokuWiki ( object ) :
83+ class DokuWiki :
12084 """Initialize a connection to a DokuWiki wiki. ``url``, ``user`` and
12185 ``password`` are respectively the URL, the login and the password for
122- connecting to the wiki. ``kwargs`` are `xmlrpclib`/` xmlrpc.client`
86+ connecting to the wiki. ``kwargs`` are `xmlrpc.client`
12387 **ServerProxy** parameters.
12488
12589 The exception `DokuWikiError` is raised if the authentication
@@ -256,7 +220,7 @@ def del_acl(self, scope, user):
256220 return self .send ('plugin.acl.delAcl' , scope , user )
257221
258222
259- class _Pages ( object ) :
223+ class _Pages :
260224 """This object regroup methods for managing pages of a DokuWiki. This object
261225 is accessible from the ``pages`` property of an `DokuWiki` instance::
262226
@@ -383,7 +347,7 @@ def backlinks(self, page):
383347 return self ._dokuwiki .send ('wiki.getBackLinks' , page )
384348
385349
386- class _Medias ( object ) :
350+ class _Medias :
387351 """This object regroup methods for managing medias of a DokuWiki. This
388352 object is accessible from the ``medias`` property of an `DokuWiki`
389353 instance::
@@ -464,7 +428,7 @@ def delete(self, media):
464428 return self ._dokuwiki .send ('wiki.deleteAttachment' , media )
465429
466430
467- class _Structs ( object ) :
431+ class _Structs :
468432 def __init__ (self , dokuwiki ):
469433 """Get the structured data of a given page."""
470434 self ._dokuwiki = dokuwiki
@@ -487,7 +451,7 @@ def get_aggregation_data(self, schemas, columns, data_filter=[], sort=''):
487451 'plugin.struct.getAggregationData' , schemas , columns , data_filter , sort )
488452
489453
490- class Dataentry ( object ) :
454+ class Dataentry :
491455 """Object that manage `data entries <https://www.dokuwiki.org/plugin:data>`_."""
492456
493457 @staticmethod
0 commit comments