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

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

# -*- coding: utf-8 -*- 

# 

# Copyright (C) Damien Churchill 2008-2009 <damoxc@gmail.com> 

# Copyright (C) Andrew Resch 2009 <andrewresch@gmail.com> 

# 

# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with 

# the additional special exception to link portions of this program with the OpenSSL library. 

# See LICENSE for more details. 

# 

 

""" 

The ui common module contains methods and classes that are deemed useful for all the interfaces. 

""" 

 

import logging 

import os 

from hashlib import sha1 as sha 

 

import deluge.configmanager 

from deluge import bencode 

from deluge.common import path_join, utf8_encoded 

 

log = logging.getLogger(__name__) 

 

 

# Dummy tranlation dict so Torrent states text is available for Translators 

# All entries in deluge.common.TORRENT_STATE should be here. It does not need importing 

# as the string matches the translation text so using the _() function is enough. 

def _(message): 

    return message 

STATE_TRANSLATION = { 

    "All": _("All"), 

    "Active": _("Active"), 

    "Allocating": _("Allocating"), 

    "Checking": _("Checking"), 

    "Downloading": _("Downloading"), 

    "Seeding": _("Seeding"), 

    "Paused": _("Paused"), 

    "Checking": _("Checking"), 

    "Queued": _("Queued"), 

    "Error": _("Error"), 

} 

 

TRACKER_STATUS_TRANSLATION = { 

    "Error": _("Error"), 

    "Warning": _("Warning"), 

    "Announce OK": _("Announce OK"), 

    "Announce Sent": _("Announce Sent") 

} 

del _ 

 

 

class TorrentInfo(object): 

    """ 

    Collects information about a torrent file. 

 

    :param filename: The path to the torrent 

    :type filename: string 

 

    """ 

    def __init__(self, filename, filetree=1): 

        # Get the torrent data from the torrent file 

        try: 

            log.debug("Attempting to open %s.", filename) 

            self.__m_filedata = open(filename, "rb").read() 

            self.__m_metadata = bencode.bdecode(self.__m_filedata) 

        except Exception as ex: 

            log.warning("Unable to open %s: %s", filename, ex) 

            raise ex 

 

        self.__m_info_hash = sha(bencode.bencode(self.__m_metadata["info"])).hexdigest() 

 

        # Get encoding from torrent file if available 

        self.encoding = None 

        if "encoding" in self.__m_metadata: 

            self.encoding = self.__m_metadata["encoding"] 

78        elif "codepage" in self.__m_metadata: 

            self.encoding = str(self.__m_metadata["codepage"]) 

        if not self.encoding: 

            self.encoding = "UTF-8" 

 

        # Check if 'name.utf-8' is in the torrent and if not try to decode the string 

        # using the encoding found. 

        if "name.utf-8" in self.__m_metadata["info"]: 

            self.__m_name = utf8_encoded(self.__m_metadata["info"]["name.utf-8"]) 

        else: 

            self.__m_name = utf8_encoded(self.__m_metadata["info"]["name"], self.encoding) 

 

        # Get list of files from torrent info 

        paths = {} 

        dirs = {} 

        if "files" in self.__m_metadata["info"]: 

            prefix = "" 

97            if len(self.__m_metadata["info"]["files"]) > 1: 

                prefix = self.__m_name 

 

            for index, f in enumerate(self.__m_metadata["info"]["files"]): 

99                if "path.utf-8" in f: 

                    path = os.path.join(prefix, *f["path.utf-8"]) 

                    del f["path.utf-8"] 

                else: 

                    path = utf8_encoded(os.path.join(prefix, utf8_encoded(os.path.join(*f["path"]), 

                                                                          self.encoding)), self.encoding) 

                f["path"] = path 

                f["index"] = index 

107                if "sha1" in f and len(f["sha1"]) == 20: 

                        f["sha1"] = f["sha1"].encode('hex') 

109                if "ed2k" in f and len(f["ed2k"]) == 16: 

                        f["ed2k"] = f["ed2k"].encode('hex') 

                paths[path] = f 

                dirname = os.path.dirname(path) 

                while dirname: 

                    dirinfo = dirs.setdefault(dirname, {}) 

                    dirinfo["length"] = dirinfo.get("length", 0) + f["length"] 

                    dirname = os.path.dirname(dirname) 

 

118            if filetree == 2: 

                def walk(path, item): 

                    if item["type"] == "dir": 

                        item.update(dirs[path]) 

                    else: 

                        item.update(paths[path]) 

                    item["download"] = True 

 

                file_tree = FileTree2(paths.keys()) 

                file_tree.walk(walk) 

            else: 

                def walk(path, item): 

                    if type(item) is dict: 

                        return item 

                    return [paths[path]["index"], paths[path]["length"], True] 

 

                file_tree = FileTree(paths) 

                file_tree.walk(walk) 

            self.__m_files_tree = file_tree.get_tree() 

        else: 

138            if filetree == 2: 

                self.__m_files_tree = { 

                    "contents": { 

                        self.__m_name: { 

                            "type": "file", 

                            "index": 0, 

                            "length": self.__m_metadata["info"]["length"], 

                            "download": True 

                        } 

                    } 

                } 

            else: 

                self.__m_files_tree = { 

                    self.__m_name: (0, self.__m_metadata["info"]["length"], True) 

                } 

 

        self.__m_files = [] 

        if "files" in self.__m_metadata["info"]: 

            prefix = "" 

159            if len(self.__m_metadata["info"]["files"]) > 1: 

                prefix = self.__m_name 

 

            for f in self.__m_metadata["info"]["files"]: 

                self.__m_files.append({ 

                    'path': f["path"], 

                    'size': f["length"], 

                    'download': True 

                }) 

        else: 

            self.__m_files.append({ 

                "path": self.__m_name, 

                "size": self.__m_metadata["info"]["length"], 

                "download": True 

            }) 

 

    def as_dict(self, *keys): 

        """ 

        Return the torrent info as a dictionary, only including the passed in 

        keys. 

 

        :param keys: a number of key strings 

        :type keys: string 

        """ 

        return dict([(key, getattr(self, key)) for key in keys]) 

 

    @property 

    def name(self): 

        """ 

        The name of the torrent. 

 

        :rtype: string 

        """ 

        return self.__m_name 

 

    @property 

    def info_hash(self): 

        """ 

        The torrents info_hash 

 

        :rtype: string 

        """ 

        return self.__m_info_hash 

 

    @property 

    def files(self): 

        """ 

        A list of the files that the torrent contains. 

 

        :rtype: list 

        """ 

        return self.__m_files 

 

    @property 

    def files_tree(self): 

        """ 

        A dictionary based tree of the files. 

 

        :: 

 

            { 

                "some_directory": { 

                    "some_file": (index, size, download) 

                } 

            } 

 

        :rtype: dictionary 

        """ 

        return self.__m_files_tree 

 

    @property 

    def metadata(self): 

        """ 

        The torrents metadata. 

 

        :rtype: dictionary 

        """ 

        return self.__m_metadata 

 

    @property 

    def filedata(self): 

        """ 

        The torrents file data.  This will be the bencoded dictionary read 

        from the torrent file. 

 

        :rtype: string 

        """ 

        return self.__m_filedata 

 

 

class FileTree2(object): 

    """ 

    Converts a list of paths in to a file tree. 

 

    :param paths: The paths to be converted 

    :type paths: list 

    """ 

 

    def __init__(self, paths): 

        self.tree = {"contents": {}, "type": "dir"} 

 

        def get_parent(path): 

            parent = self.tree 

            while "/" in path: 

                directory, path = path.split("/", 1) 

                child = parent["contents"].get(directory) 

                if child is None: 

                    parent["contents"][directory] = { 

                        "type": "dir", 

                        "contents": {} 

                    } 

                parent = parent["contents"][directory] 

            return parent, path 

 

        for path in paths: 

            if path[-1] == "/": 

                path = path[:-1] 

                parent, path = get_parent(path) 

                parent["contents"][path] = { 

                    "type": "dir", 

                    "contents": {} 

                } 

            else: 

                parent, path = get_parent(path) 

                parent["contents"][path] = { 

                    "type": "file" 

                } 

 

    def get_tree(self): 

        """ 

        Return the tree. 

 

        :returns: the file tree. 

        :rtype: dictionary 

        """ 

        return self.tree 

 

    def walk(self, callback): 

        """ 

        Walk through the file tree calling the callback function on each item 

        contained. 

 

        :param callback: The function to be used as a callback, it should have 

            the signature func(item, path) where item is a `tuple` for a file 

            and `dict` for a directory. 

        :type callback: function 

        """ 

        def walk(directory, parent_path): 

            for path in directory["contents"].keys(): 

                full_path = path_join(parent_path, path) 

                if directory["contents"][path]["type"] == "dir": 

                    directory["contents"][path] = callback(full_path, directory["contents"][path] 

                                                           ) or directory["contents"][path] 

                    walk(directory["contents"][path], full_path) 

                else: 

                    directory["contents"][path] = callback(full_path, directory["contents"][path] 

                                                           ) or directory["contents"][path] 

        walk(self.tree, "") 

 

    def __str__(self): 

        lines = [] 

 

        def write(path, item): 

            depth = path.count("/") 

            path = os.path.basename(path) 

            path = path + "/" if item["type"] == "dir" else path 

            lines.append("  " * depth + path) 

        self.walk(write) 

        return "\n".join(lines) 

 

 

class FileTree(object): 

    """ 

    Convert a list of paths in a file tree. 

 

    :param paths: The paths to be converted. 

    :type paths: list 

    """ 

 

    def __init__(self, paths): 

        self.tree = {} 

 

        def get_parent(path): 

            parent = self.tree 

            while "/" in path: 

                directory, path = path.split("/", 1) 

                child = parent.get(directory) 

                if child is None: 

                    parent[directory] = {} 

                parent = parent[directory] 

            return parent, path 

 

        for path in paths: 

350            if path[-1] == "/": 

                path = path[:-1] 

                parent, path = get_parent(path) 

                parent[path] = {} 

            else: 

                parent, path = get_parent(path) 

                parent[path] = [] 

 

    def get_tree(self): 

        """ 

        Return the tree, after first converting all file lists to a tuple. 

 

        :returns: the file tree. 

        :rtype: dictionary 

        """ 

        def to_tuple(path, item): 

            if type(item) is dict: 

                return item 

            return tuple(item) 

        self.walk(to_tuple) 

        return self.tree 

 

    def walk(self, callback): 

        """ 

        Walk through the file tree calling the callback function on each item 

        contained. 

 

        :param callback: The function to be used as a callback, it should have 

            the signature func(item, path) where item is a `tuple` for a file 

            and `dict` for a directory. 

        :type callback: function 

        """ 

        def walk(directory, parent_path): 

            for path in directory.keys(): 

                full_path = os.path.join(parent_path, path) 

                if type(directory[path]) is dict: 

                    directory[path] = callback(full_path, directory[path]) or directory[path] 

                    walk(directory[path], full_path) 

                else: 

                    directory[path] = callback(full_path, directory[path]) or directory[path] 

        walk(self.tree, "") 

 

    def __str__(self): 

        lines = [] 

 

        def write(path, item): 

            depth = path.count("/") 

            path = os.path.basename(path) 

            path = type(item) is dict and path + "/" or path 

            lines.append("  " * depth + path) 

        self.walk(write) 

        return "\n".join(lines) 

 

 

def get_localhost_auth(): 

    """ 

    Grabs the localclient auth line from the 'auth' file and creates a localhost uri 

 

    :returns: with the username and password to login as 

    :rtype: tuple 

    """ 

    auth_file = deluge.configmanager.get_config_dir("auth") 

412    if not os.path.exists(auth_file): 

        from deluge.common import create_localclient_account 

        create_localclient_account() 

 

    with open(auth_file) as auth: 

exit        for line in auth: 

419            if line.startswith("#"): 

                # This is a comment line 

                continue 

 

            lsplit = line.strip().split(":") 

 

424            if len(lsplit) == 2: 

                username, password = lsplit 

428            elif len(lsplit) == 3: 

                username, password, level = lsplit 

            else: 

                log.error("Your auth file is malformed: Incorrect number of fields!") 

                continue 

 

416            if username == "localclient": 

                return (username, password)