@@ -52,45 +52,155 @@ class HTTPResponseError(ResponseError):
5252 def __init__ (self , res ):
5353 ResponseError .__init__ (self ,res , "HTTP" )
5454
55- class PublicShare ():
56- """Public share information"""
55+ class ShareInfo ():
56+ """Share information"""
5757
58- def __init__ (self , share_id , target_file , link , token ):
59- self .share_id = share_id
60- self .target_file = target_file
61- self .link = link
62- self .token = token
58+ def __init__ (self , share_info ):
59+ self .share_info = {}
60+ # remove unneeded attributes
61+ del_attrs = ['item_type' , 'item_source' , 'file_source' , 'parent' , 'storage' , 'mail_send' ]
62+ for k , v in share_info .iteritems ():
63+ if k not in del_attrs :
64+ self .share_info [k ] = v
65+ self .share_id = self .__get_int ('id' )
66+ if 'token' in self .share_info :
67+ self .token = self .share_info ['token' ]
6368
64- def __str__ (self ):
65- return 'PublicShare(id=%i,path=%s,link=%s,token=%s)' % \
66- (self .share_id , self .target_file , self .link , self .token )
69+ def get_id (self ):
70+ """Returns the id of the share
71+
72+ :returns: id of the share
73+ """
74+ return self .__get_int ('id' )
6775
76+ def get_share_type (self ):
77+ """Returns the type of the share.
78+ See OCS_SHARE_TYPE_* constants.
6879
69- class UserShare ():
70- """User share information"""
80+ :returns: share type
81+ """
82+ return self .__get_int ('share_type' )
7183
72- def __init__ (self , share_id , share , perms ):
73- self .share_id = share_id
74- self .share = share
75- self .perms = perms
84+ def get_share_with (self ):
85+ """Returns the share recipient.
86+ If share type is OCS_SHARE_TYPE_USER, then the recipient is the name of the user.
87+ For OCS_SHARE_TYPE_GROUP it is the name of the group.
88+ Otherwise this value is None.
89+
90+ :returns: name of the share recipient
91+ """
92+ if 'share_with' in self .share_info :
93+ return self .share_info ['share_with' ]
94+ return None
95+
96+ def get_share_with_displayname (self ):
97+ """Returns the share recipient displayname.
98+ If share_with_displayname cannot be returned, None is returned instead
99+ :returns: name of the share recipient
100+ """
101+ if 'share_with_displayname' in self .share_info :
102+ return self .share_info ['share_with_displayname' ]
103+ return None
104+
105+ def get_path (self ):
106+ """Returns the path of the shared file/folder relative to the
107+ caller's filesystem.
108+
109+ :returns: path to the shared file/folder
110+ """
111+ if 'path' in self .share_info :
112+ return self .share_info ['path' ]
113+ return None
114+
115+ def get_permissions (self ):
116+ """Returns the share permissions.
117+ See OCS_PERMISSION_* constants.
118+
119+ :returns: share permissions
120+ """
121+ return self .__get_int ('permissions' )
122+
123+ def get_share_time (self ):
124+ """Returns the share time.
125+
126+ :returns: share timestamp
127+ :rtype: datetime object
128+ """
129+ return datetime .datetime .fromtimestamp (
130+ self .__get_int ('stime' )
131+ )
132+
133+ def get_expiration (self ):
134+ """Returns the expiration date.
135+
136+ :returns: expiration date
137+ :rtype: datetime object
138+ """
139+ exp = self .__get_int ('expiration' )
140+ if exp is not None :
141+ return datetime .datetime .fromtimestamp (
142+ exp
143+ )
144+ return None
145+
146+ def get_token (self ):
147+ if 'token' in self .share_info :
148+ return self .share_info ['token' ]
149+ return None
150+
151+ def get_link (self ):
152+ if 'link' in self .share_info :
153+ return self .share_info ['link' ]
154+ return None
155+
156+ def get_uid_owner (self ):
157+ """Returns the user id of the owner.
158+
159+ :returns: owner user id
160+ """
161+ if 'uid_owner' in self .share_info :
162+ return self .share_info ['uid_owner' ]
163+ return None
164+
165+ def get_displayname_owner (self ):
166+ """Returns the display name of the owner.
167+
168+ :returns: display name of owner
169+ """
170+ if 'displayname_owner' in self .share_info :
171+ return self .share_info ['displayname_owner' ]
172+ return None
76173
77174 def __str__ (self ):
78- return "UserShare(id=%i,path='%s',perms=%s)" % \
79- (self .share_id , self .share , self .perms )
175+ info = ''
176+ for k , v in self .share_info .iteritems ():
177+ info += '%s=%s,' % (k , v )
178+ return 'ShareInfo(%s)' % info [:- 1 ]
80179
180+ def __repr__ (self ):
181+ return self .__str__ ()
182+
183+ def __get_int (self , key ):
184+ """Simple wrapper which converts value to Integer
185+ in silently manner (w/o raising exception)"""
186+ try :
187+ value = int (self .share_info [key ])
188+ return value
189+ except :
190+ return None
81191
82- class GroupShare ( ):
83- """Group share information"""
192+ class PublicShare ( ShareInfo ):
193+ """Public share information"""
84194
85- def __init__ (self , share_id , share , perms ):
195+ def __init__ (self , share_id , target_file , link , token ):
86196 self .share_id = share_id
87- self .share = share
88- self .perms = perms
197+ self .target_file = target_file
198+ self .link = link
199+ self .token = token
89200
90201 def __str__ (self ):
91- return "GroupShare(id=%i,path='%s',perms=%s)" % \
92- (self .share_id , self .share , self .perms )
93-
202+ return 'PublicShare(id=%i,path=%s,link=%s,token=%s)' % \
203+ (self .share_id , self .target_file , self .link , self .token )
94204
95205class FileInfo ():
96206 """File information"""
@@ -678,7 +788,7 @@ def share_file_with_link(self, path, **kwargs):
678788 :param public_upload (optional): allows users to upload files or folders
679789 :param password (optional): sets a password
680790 http://doc.owncloud.org/server/6.0/admin_manual/sharing_api/index.html
681- :returns: instance of :class:`PublicShare ` with the share info
791+ :returns: instance of :class:`ShareInfo ` with the share info
682792 or False if the operation failed
683793 :raises: HTTPResponseError in case an HTTP error status was returned
684794 """
@@ -709,11 +819,13 @@ def share_file_with_link(self, path, **kwargs):
709819 tree = ET .fromstring (res .content )
710820 self .__check_ocs_status (tree )
711821 data_el = tree .find ('data' )
712- return PublicShare (
713- int (data_el .find ('id' ).text ),
714- path ,
715- data_el .find ('url' ).text ,
716- data_el .find ('token' ).text
822+ return ShareInfo (
823+ {
824+ 'id' : data_el .find ('id' ).text ,
825+ 'path' :path ,
826+ 'link' : data_el .find ('url' ).text ,
827+ 'token' : data_el .find ('token' ).text
828+ }
717829 )
718830 raise HTTPResponseError (res )
719831
@@ -736,6 +848,27 @@ def is_shared(self, path):
736848 return False
737849 return False
738850
851+ def get_share (self , share_id ):
852+ """Returns share information about known share
853+
854+ :param share_id: id of the share to be checked
855+ :returns: instance of ShareInfo class
856+ :raises: ResponseError in case an HTTP error status was returned
857+ """
858+ if (share_id is None ) or not (isinstance (share_id , int )):
859+ return None
860+
861+ res = self .__make_ocs_request (
862+ 'GET' ,
863+ self .OCS_SERVICE_SHARE ,
864+ 'shares/' + str (share_id )
865+ )
866+ if res .status_code == 200 :
867+ tree = ET .fromstring (res .content )
868+ self .__check_ocs_status (tree )
869+ return self .__get_shareinfo (tree .find ('data' ).find ('element' ))
870+ raise ResponseError (res )
871+
739872 def get_shares (self , path = '' , ** kwargs ):
740873 """Returns array of shares
741874
@@ -744,7 +877,7 @@ def get_shares(self, path='', **kwargs):
744877 the current user but all shares from the given file (default: False)
745878 :param subfiles: (optional, boolean) returns all shares within
746879 a folder, given that path defines a folder (default: False)
747- :returns: array of shares or empty array if the operation failed
880+ :returns: array of shares ShareInfo instances or empty array if the operation failed
748881 :raises: HTTPResponseError in case an HTTP error status was returned
749882 """
750883 if not (isinstance (path , basestring )):
@@ -774,12 +907,13 @@ def get_shares(self, path='', **kwargs):
774907 self .__check_ocs_status (tree )
775908 shares = []
776909 for element in tree .find ('data' ).iter ('element' ):
777- share_attr = {}
910+ ''' share_attr = {}
778911 for child in element:
779912 key = child.tag
780913 value = child.text
781914 share_attr[key] = value
782- shares .append (share_attr )
915+ shares.append(share_attr)'''
916+ shares .append (self .__get_shareinfo (element ))
783917 return shares
784918 raise HTTPResponseError (res )
785919
@@ -1069,7 +1203,7 @@ def share_file_with_user(self, path, user, **kwargs):
10691203 :param perms (optional): permissions of the shared object
10701204 defaults to read only (1)
10711205 http://doc.owncloud.org/server/6.0/admin_manual/sharing_api/index.html
1072- :returns: instance of :class:`UserShare ` with the share info
1206+ :returns: instance of :class:`ShareInfo ` with the share info
10731207 or False if the operation failed
10741208 :raises: HTTPResponseError in case an HTTP error status was returned
10751209 """
@@ -1101,10 +1235,12 @@ def share_file_with_user(self, path, user, **kwargs):
11011235 tree = ET .fromstring (res .content )
11021236 self .__check_ocs_status (tree )
11031237 data_el = tree .find ('data' )
1104- return UserShare (
1105- int (data_el .find ('id' ).text ),
1106- path ,
1107- perms
1238+ return ShareInfo (
1239+ {
1240+ 'id' :data_el .find ('id' ).text ,
1241+ 'path' :path ,
1242+ 'permissions' :perms
1243+ }
11081244 )
11091245 raise HTTPResponseError (res )
11101246
@@ -1187,7 +1323,7 @@ def share_file_with_group(self, path, group, **kwargs):
11871323 :param perms (optional): permissions of the shared object
11881324 defaults to read only (1)
11891325 http://doc.owncloud.org/server/6.0/admin_manual/sharing_api/index.html
1190- :returns: instance of :class:`GroupShare ` with the share info
1326+ :returns: instance of :class:`ShareInfo ` with the share info
11911327 or False if the operation failed
11921328 :raises: HTTPResponseError in case an HTTP error status was returned
11931329 """
@@ -1209,10 +1345,12 @@ def share_file_with_group(self, path, group, **kwargs):
12091345 tree = ET .fromstring (res .text )
12101346 self .__check_ocs_status (tree )
12111347 data_el = tree .find ('data' )
1212- return GroupShare (
1213- int (data_el .find ('id' ).text ),
1214- path ,
1215- perms
1348+ return ShareInfo (
1349+ {
1350+ 'id' : data_el .find ('id' ).text ,
1351+ 'path' :path ,
1352+ 'permissions' : perms
1353+ }
12161354 )
12171355 raise HTTPResponseError (res )
12181356
@@ -1595,4 +1733,13 @@ def __xml_to_dict(self, element):
15951733 else :
15961734 return_dict [el .tag ] = el .text
15971735 return return_dict
1736+
1737+ def __get_shareinfo (self , data_el ):
1738+ """Simple helper which returns instance of ShareInfo class
15981739
1740+ :param data_el: 'data' element extracted from __make_ocs_request
1741+ :returns: instance of ShareInfo class
1742+ """
1743+ if (data_el is None ) or not (isinstance (data_el , ET .Element )):
1744+ return None
1745+ return ShareInfo (self .__xml_to_dict (data_el ))
0 commit comments