Пример #1
0
    def __init__(self, filename=None, config={}, **kwargs):
        """
        Load a repository metadata structure

        filename:
          the name of a .chunker file to load, containing
          either full state, or a useful subset

        **kwargs:
          a basic .chunker data structure

        config:
          optional dictionary of extra info. Keys:
            username - for change log
            hostname - for change log
        """
        self.notifier = None

        if not filename and not kwargs:
            raise Exception("Repo has no initialisation data")

        struct = {}
        if filename and os.path.exists(filename):
            try:
                data = gzip.open(filename).read()
            except:
                data = open(filename).read()
            struct.update(json.loads(data))
        struct.update(kwargs)

        self.config = config

        self.name = struct.get("name") or os.path.basename(struct.get("root")) or os.path.splitext(os.path.basename(filename or ""))[0]
        self.root = struct.get("root") or os.path.join(os.path.expanduser("~/Downloads"), self.name)
        self.type = struct.get("type", "share")  # static / share
        self.uuid = struct.get("uuid", sha256(uuid.uuid4()))
        self.key = struct.get("key", None)       # for encrypting / decrypting chunks
        self.peers = struct.get("peers", [])
        self.files = dict([
            (filename, File.from_struct(self, filename, data))
            for filename, data
            in struct.get("files", {}).items()
        ])

        # if we're creating a new static chunkfile, then add our local files to the chunkfile
        # should this be in start()?
        if (self.type == "static" and not self.files):
            self.__add_local_files()
Пример #2
0
 def test(self):
     self.assertEqual(
         util.sha256("hello world"),
         "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
     )