Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

# Licensed under the MIT license 

# http://opensource.org/licenses/mit-license.php 

 

# Copyright (C) 2006 Fluendo, S.A. (www.fluendo.com). 

# Copyright 2006, Frank Scholz <coherence@beebits.net> 

 

from twisted.python import log 

 

from coherence.upnp.core import DIDLLite 

 

global work, pending 

work = [] 

pending = {} 

 

 

class ContentDirectoryClient: 

 

    def __init__(self, service): 

        self.service = service 

        self.namespace = service.get_type() 

        self.url = service.get_control_url() 

        self.service.subscribe() 

        self.service.client = self 

        # print "ContentDirectoryClient __init__", self.url 

 

    # def __del__(self): 

    #    print "ContentDirectoryClient deleted" 

    #    pass 

 

    def remove(self): 

        self.service.remove() 

        self.service = None 

        self.namespace = None 

        self.url = None 

        del self 

 

    def subscribe_for_variable(self, var_name, callback, signal=False): 

        self.service.subscribe_for_variable(var_name, instance=0, 

                                            callback=callback, signal=signal) 

 

    def get_search_capabilities(self): 

        action = self.service.get_action('GetSearchCapabilities') 

        return action.call() 

 

    def get_sort_extension_capabilities(self): 

        action = self.service.get_action('GetSortExtensionCapabilities') 

        return action.call() 

 

    def get_feature_list(self): 

        action = self.service.get_action('GetFeatureList') 

        return action.call() 

 

    def get_system_update_id(self): 

        action = self.service.get_action('GetSystemUpdateID') 

        return action.call() 

 

    def browse(self, object_id=0, browse_flag='BrowseDirectChildren', 

               filter='*', sort_criteria='', 

               starting_index=0, requested_count=0, 

               process_result=True, 

               backward_compatibility=False): 

 

        def got_result(results): 

            items = [] 

            if results is not None: 

                elt = DIDLLite.DIDLElement.fromString(results['Result']) 

                items = elt.getItems() 

            return items 

 

        def got_process_result(result): 

            # print result 

            r = {} 

            r['number_returned'] = result['NumberReturned'] 

            r['total_matches'] = result['TotalMatches'] 

            r['update_id'] = result['UpdateID'] 

            r['items'] = {} 

            elt = DIDLLite.DIDLElement.fromString(result['Result']) 

            for item in elt.getItems(): 

                # print "process_result", item 

                i = {} 

                i['upnp_class'] = item.upnp_class 

                i['id'] = item.id 

                i['title'] = item.title 

                i['parent_id'] = item.parentID 

                if hasattr(item, 'childCount'): 

                    i['child_count'] = str(item.childCount) 

                if hasattr(item, 'date') and item.date: 

                    i['date'] = item.date 

                if hasattr(item, 'album') and item.album: 

                    i['album'] = item.album 

                if hasattr(item, 'artist') and item.artist: 

                    i['artist'] = item.artist 

                if hasattr(item, 'albumArtURI') and item.albumArtURI: 

                    i['album_art_uri'] = item.albumArtURI 

                if hasattr(item, 'res'): 

                    resources = {} 

                    for res in item.res: 

                        url = res.data 

                        resources[url] = res.protocolInfo 

                    if len(resources): 

                        i['resources'] = resources 

                r['items'][item.id] = i 

            return r 

 

        action = self.service.get_action('Browse') 

        d = action.call(ObjectID=object_id, 

                        BrowseFlag=browse_flag, 

                        Filter=filter, SortCriteria=sort_criteria, 

                        StartingIndex=str(starting_index), 

                        RequestedCount=str(requested_count)) 

        if process_result in [True, 1, '1', 'true', 'True', 'yes', 'Yes']: 

            d.addCallback(got_process_result) 

        # else: 

        #    d.addCallback(got_result) 

        d.addErrback(self._failure) 

        return d 

 

    def search(self, container_id, criteria, starting_index=0, 

               requested_count=0): 

        # print "search:", criteria 

        starting_index = str(starting_index) 

        requested_count = str(requested_count) 

        action = self.service.get_action('Search') 

        if action is None: 

            return None 

        d = action.call(ContainerID=container_id, 

                        SearchCriteria=criteria, 

                        Filter="*", 

                        StartingIndex=starting_index, 

                        RequestedCount=requested_count, 

                        SortCriteria="") 

        d.addErrback(self._failure) 

 

        def gotResults(results): 

            items = [] 

            if results is not None: 

                elt = DIDLLite.DIDLElement.fromString(results['Result']) 

                items = elt.getItems() 

            return items 

 

        d.addCallback(gotResults) 

        return d 

 

    def dict2item(self, elements): 

        upnp_class = DIDLLite.upnp_classes.get( 

            elements.get('upnp_class', None), None) 

        if upnp_class is None: 

            return None 

 

        del elements['upnp_class'] 

        item = upnp_class(id='', 

                          parentID=elements.get('parentID', None), 

                          title=elements.get('title', None), 

                          restricted=elements.get('restricted', None)) 

        for k, v in list(elements.items()): 

            attribute = getattr(item, k, None) 

            if attribute is None: 

                continue 

            attribute = v 

 

        return item 

 

    def create_object(self, container_id, elements): 

        if isinstance(elements, dict): 

            elements = self.dict2item(elements) 

        if isinstance(elements, DIDLLite.Object): 

            didl = DIDLLite.DIDLElement() 

            didl.addItem(elements) 

            elements = didl.toString() 

        if elements is None: 

            elements = '' 

        action = self.service.get_action('CreateObject') 

        if action:  # optional 

            return action.call(ContainerID=container_id, 

                               Elements=elements) 

        return None 

 

    def destroy_object(self, object_id): 

        action = self.service.get_action('DestroyObject') 

        if action:  # optional 

            return action.call(ObjectID=object_id) 

        return None 

 

    def update_object(self, object_id, current_tag_value, new_tag_value): 

        action = self.service.get_action('UpdateObject') 

        if action:  # optional 

            return action.call(ObjectID=object_id, 

                               CurrentTagValue=current_tag_value, 

                               NewTagValue=new_tag_value) 

        return None 

 

    def move_object(self, object_id, new_parent_id): 

        action = self.service.get_action('MoveObject') 

        if action:  # optional 

            return action.call(ObjectID=object_id, 

                               NewParentID=new_parent_id) 

        return None 

 

    def import_resource(self, source_uri, destination_uri): 

        action = self.service.get_action('ImportResource') 

        if action:  # optional 

            return action.call(SourceURI=source_uri, 

                               DestinationURI=destination_uri) 

        return None 

 

    def export_resource(self, source_uri, destination_uri): 

        action = self.service.get_action('ExportResource') 

        if action:  # optional 

            return action.call(SourceURI=source_uri, 

                               DestinationURI=destination_uri) 

        return None 

 

    def delete_resource(self, resource_uri): 

        action = self.service.get_action('DeleteResource') 

        if action:  # optional 

            return action.call(ResourceURI=resource_uri) 

        return None 

 

    def stop_transfer_resource(self, transfer_id): 

        action = self.service.get_action('StopTransferResource') 

        if action:  # optional 

            return action.call(TransferID=transfer_id) 

        return None 

 

    def get_transfer_progress(self, transfer_id): 

        action = self.service.get_action('GetTransferProgress') 

        if action:  # optional 

            return action.call(TransferID=transfer_id) 

        return None 

 

    def create_reference(self, container_id, object_id): 

        action = self.service.get_action('CreateReference') 

        if action:  # optional 

            return action.call(ContainerID=container_id, 

                               ObjectID=object_id) 

        return None 

 

    def _failure(self, error): 

        log.msg(error.getTraceback(), debug=True) 

        error.trap(Exception)