Commits

Matt Joiner committed ea7179c

Rename lib.data and fix imports.

Comments (0)

Files changed (2)

data.py

-import binascii
-import bisect
-import collections
-import errno
-import hashlib
-import io
-import itertools
-import logging
-import os
-import pdb
-import random
-import select
-import signal
-import socket
-import struct
-import sys
-import threading
-import time
-
-
-class SingleFileTorrentData:
-
-    def __init__(self, destdir, name, length):
-        self.destdir = destdir
-        self.name = name
-        self.length = length
-        self.total_bytes = length
-        self.path = os.path.join(self.destdir, self.name)
-        self.f = os.fdopen(os.open(self.path, os.O_RDWR|os.O_CREAT), 'r+b')
-
-    def pwrite(self, buf, offset):
-        assert offset + len(buf) <= self.length
-        self.f.seek(offset)
-        return self.f.write(buf)
-
-    def pread(self, count, offset):
-        try:
-            self.f.seek(offset)
-            return self.f.read(count)
-        except IOError as exc:
-            if exc.errno == errno.ENOENT:
-                return b''
-            raise
-
-
-class MultipleFileTorrentData:
-
-    def __init__(self, destdir, name, files):
-        self.destdir = destdir
-        self.name = name
-        self.files = files
-        self.total_bytes = sum(file['length'] for file in self.files)
-
-    def pwrite(self, buf, offset):
-        written = 0
-        for file in self.files:
-            if offset < file['length']:
-                expected = min(len(buf), file['length'] - offset)
-                file_path = self.destdir
-                for name in [self.name] + [n.decode() for n in file['path'][:-1]]:
-                    file_path = os.path.join(file_path, name)
-                    try:
-                        os.mkdir(file_path)
-                    except OSError as exc:
-                        if exc.errno not in [errno.EEXIST]:
-                            raise
-                file_path = os.path.join(file_path, file['path'][-1].decode())
-                with os.fdopen(os.open(file_path, os.O_WRONLY|os.O_CREAT), 'wb') as fp:
-                    fp.seek(offset)
-                    actual = fp.write(buf[:expected])
-                if actual != expected:
-                    assert False
-                    break
-                buf = buf[expected:]
-                if not buf:
-                    break
-                offset = 0
-            else:
-                offset -= file['length']
-        return written
-
-    def pread(self, count, offset):
-        buf = b''
-        if self.destdir is None:
-            return buf
-        for file in self.files:
-            if offset < file['length']:
-                file_path = os.path.join(
-                    self.destdir,
-                    self.name,
-                    *(n.decode() for n in file['path']))
-                try:
-                    fp = open(file_path, 'rb')
-                except IOError as exc:
-                    if exc.errno in [errno.ENOENT]:
-                        break
-                    raise
-                fp.seek(offset)
-                read_size = min(file['length'] - offset, count)
-                read_buf = fp.read(read_size)
-                buf += read_buf
-                if len(read_buf) == read_size:
-                    count -= read_size
-                else:
-                    break
-                offset = 0
-            else:
-                offset -= file['length']
-            if count == 0:
-                break
-        return buf
+import os, errno
+
+
+class SingleFileTorrentData:
+
+    def __init__(self, destdir, name, length):
+        self.destdir = destdir
+        self.name = name
+        self.length = length
+        self.total_bytes = length
+        self.path = os.path.join(self.destdir, self.name)
+        self.f = os.fdopen(os.open(self.path, os.O_RDWR|os.O_CREAT), 'r+b')
+
+    def pwrite(self, buf, offset):
+        assert offset + len(buf) <= self.length
+        self.f.seek(offset)
+        return self.f.write(buf)
+
+    def pread(self, count, offset):
+        try:
+            self.f.seek(offset)
+            return self.f.read(count)
+        except IOError as exc:
+            if exc.errno == errno.ENOENT:
+                return b''
+            raise
+
+
+class MultipleFileTorrentData:
+
+    def __init__(self, destdir, name, files):
+        self.destdir = destdir
+        self.name = name
+        self.files = files
+        self.total_bytes = sum(file['length'] for file in self.files)
+
+    def pwrite(self, buf, offset):
+        written = 0
+        for file in self.files:
+            if offset < file['length']:
+                expected = min(len(buf), file['length'] - offset)
+                file_path = self.destdir
+                for name in [self.name] + [n.decode() for n in file['path'][:-1]]:
+                    file_path = os.path.join(file_path, name)
+                    try:
+                        os.mkdir(file_path)
+                    except OSError as exc:
+                        if exc.errno not in [errno.EEXIST]:
+                            raise
+                file_path = os.path.join(file_path, file['path'][-1].decode())
+                with os.fdopen(os.open(file_path, os.O_WRONLY|os.O_CREAT), 'wb') as fp:
+                    fp.seek(offset)
+                    actual = fp.write(buf[:expected])
+                if actual != expected:
+                    assert False
+                    break
+                buf = buf[expected:]
+                if not buf:
+                    break
+                offset = 0
+            else:
+                offset -= file['length']
+        return written
+
+    def pread(self, count, offset):
+        buf = b''
+        if self.destdir is None:
+            return buf
+        for file in self.files:
+            if offset < file['length']:
+                file_path = os.path.join(
+                    self.destdir,
+                    self.name,
+                    *(n.decode() for n in file['path']))
+                try:
+                    fp = open(file_path, 'rb')
+                except IOError as exc:
+                    if exc.errno in [errno.ENOENT]:
+                        break
+                    raise
+                fp.seek(offset)
+                read_size = min(file['length'] - offset, count)
+                read_buf = fp.read(read_size)
+                buf += read_buf
+                if len(read_buf) == read_size:
+                    count -= read_size
+                else:
+                    break
+                offset = 0
+            else:
+                offset -= file['length']
+            if count == 0:
+                break
+        return buf
+