Beispiel #1
0
    def helper_resolve_all_files(self, labbook, kwargs):
        """Helper method to populate the LabbookFileConnection"""
        # Get all files and directories, with the exception of anything in .git or .gigantum
        edges = labbook.walkdir(section=self.section, show_hidden=False)
        # Generate naive cursors
        cursors = [
            base64.b64encode("{}".format(cnt).encode("UTF-8")).decode("UTF-8")
            for cnt, x in enumerate(edges)
        ]

        # Process slicing and cursor args
        lbc = ListBasedConnection(edges, cursors, kwargs)
        lbc.apply()

        edge_objs = []
        for edge, cursor in zip(lbc.edges, lbc.cursors):
            create_data = {
                "owner": self.owner,
                "section": self.section,
                "name": self.name,
                "key": edge['key'],
                "_file_info": edge
            }
            edge_objs.append(
                LabbookFileConnection.Edge(node=LabbookFile(**create_data),
                                           cursor=cursor))

        return LabbookFileConnection(edges=edge_objs, page_info=lbc.page_info)
Beispiel #2
0
    def mutate_and_get_payload(cls, root, info, owner, labbook_name, section, directory,
                               client_mutation_id=None):
        username = get_logged_in_username()
        lb = InventoryManager().load_labbook(username, owner, labbook_name,
                                             author=get_logged_in_author())
        with lb.lock():
            FileOperations.makedir(lb, os.path.join(section, directory), create_activity_record=True)

        # Prime dataloader with labbook you already loaded
        dataloader = LabBookLoader()
        dataloader.prime(f"{owner}&{labbook_name}&{lb.name}", lb)

        # Create data to populate edge
        file_info = FileOperations.get_file_info(lb, section, directory)
        create_data = {'owner': owner,
                       'name': labbook_name,
                       'section': section,
                       'key': file_info['key'],
                       '_file_info': file_info}

        # TODO: Fix cursor implementation, this currently doesn't make sense
        cursor = base64.b64encode(f"{0}".encode('utf-8'))

        return MakeLabbookDirectory(
            new_labbook_file_edge=LabbookFileConnection.Edge(
                node=LabbookFile(**create_data),
                cursor=cursor))
Beispiel #3
0
    def mutate_and_get_payload(cls, root, info, owner, labbook_name, section, src_path, dst_path,
                               client_mutation_id=None, **kwargs):
        username = get_logged_in_username()
        lb = InventoryManager().load_labbook(username, owner, labbook_name,
                                             author=get_logged_in_author())

        with lb.lock():
            mv_results = FileOperations.move_file(lb, section, src_path, dst_path)

        file_edges = list()
        for file_dict in mv_results:
            file_edges.append(LabbookFile(owner=owner,
                                          name=labbook_name,
                                          section=section,
                                          key=file_dict['key'],
                                          is_dir=file_dict['is_dir'],
                                          is_favorite=file_dict['is_favorite'],
                                          modified_at=file_dict['modified_at'],
                                          size=str(file_dict['size'])))

        cursors = [base64.b64encode("{}".format(cnt).encode("UTF-8")).decode("UTF-8")
                   for cnt, x in enumerate(file_edges)]

        edge_objs = [LabbookFileConnection.Edge(node=e, cursor=c) for e, c in zip(file_edges, cursors)]

        return MoveLabbookFile(updated_edges=edge_objs)
    def helper_resolve_all_files(self, labbook, kwargs):
        """Helper method to populate the LabbookFileConnection"""
        # Check if file info has been cached
        redis_conn = redis.Redis(db=5)
        cache_key = f"FILE_LIST_CACHE|{labbook.key}|{self.section}"
        if redis_conn.exists(cache_key):
            # Load from cache
            edges = json.loads(redis_conn.get(cache_key).decode("utf-8"))
            redis_conn.expire(cache_key, 5)
        else:
            # Load from disk and cache
            # Get all files and directories, with the exception of anything in .git or .gigantum
            edges = FileOperations.walkdir(labbook,
                                           section=self.section,
                                           show_hidden=False)
            redis_conn.set(cache_key, json.dumps(edges))
            redis_conn.expire(cache_key, 5)

        # Generate naive cursors
        cursors = [
            base64.b64encode("{}".format(cnt).encode("UTF-8")).decode("UTF-8")
            for cnt, x in enumerate(edges)
        ]

        # Process slicing and cursor args
        lbc = ListBasedConnection(edges, cursors, kwargs)
        lbc.apply()

        edge_objs = []
        for edge, cursor in zip(lbc.edges, lbc.cursors):
            create_data = {
                "owner": self.owner,
                "section": self.section,
                "name": self.name,
                "key": edge['key'],
                "_file_info": edge
            }
            edge_objs.append(
                LabbookFileConnection.Edge(node=LabbookFile(**create_data),
                                           cursor=cursor))

        return LabbookFileConnection(edges=edge_objs, page_info=lbc.page_info)
    def helper_resolve_files(self, labbook, kwargs):
        """Helper method to populate the LabbookFileConnection"""
        base_dir = None
        if 'root_dir' in kwargs:
            if kwargs['root_dir']:
                base_dir = kwargs['root_dir'] + os.path.sep
                base_dir = base_dir.replace(os.path.sep + os.path.sep,
                                            os.path.sep)

        # Get all files and directories, with the exception of anything in .git or .gigantum
        edges = FileOperations.listdir(labbook,
                                       self.section,
                                       base_path=base_dir,
                                       show_hidden=False)

        # Generate naive cursors
        cursors = [
            base64.b64encode("{}".format(cnt).encode("UTF-8")).decode("UTF-8")
            for cnt, x in enumerate(edges)
        ]

        # Process slicing and cursor args
        lbc = ListBasedConnection(edges, cursors, kwargs)
        lbc.apply()

        edge_objs = []
        for edge, cursor in zip(lbc.edges, lbc.cursors):
            create_data = {
                "owner": self.owner,
                "section": self.section,
                "name": self.name,
                "key": edge['key'],
                "_file_info": edge
            }
            edge_objs.append(
                LabbookFileConnection.Edge(node=LabbookFile(**create_data),
                                           cursor=cursor))

        return LabbookFileConnection(edges=edge_objs, page_info=lbc.page_info)
    def mutate_and_process_upload(cls,
                                  info,
                                  owner,
                                  labbook_name,
                                  section,
                                  file_path,
                                  chunk_upload_params,
                                  transaction_id,
                                  client_mutation_id=None):
        if not cls.upload_file_path:
            logger.error('No file uploaded')
            raise ValueError('No file uploaded')

        try:
            username = get_logged_in_username()
            working_directory = Configuration().config['git'] \
                ['working_directory']
            inferred_lb_directory = os.path.join(working_directory, username,
                                                 owner, 'labbooks',
                                                 labbook_name)
            lb = LabBook(author=get_logged_in_author())
            lb.from_directory(inferred_lb_directory)
            dstpath = os.path.join(os.path.dirname(file_path), cls.filename)

            fops = FileOperations.put_file(labbook=lb,
                                           section=section,
                                           src_file=cls.upload_file_path,
                                           dst_path=dstpath,
                                           txid=transaction_id)
        finally:
            try:
                logger.debug(f"Removing temp file {cls.upload_file_path}")
                os.remove(cls.upload_file_path)
            except FileNotFoundError:
                pass

        # Create data to populate edge
        create_data = {
            'owner': owner,
            'name': labbook_name,
            'section': section,
            'key': fops['key'],
            '_file_info': fops
        }

        # TODO: Fix cursor implementation..
        # this currently doesn't make sense when adding edges
        cursor = base64.b64encode(f"{0}".encode('utf-8'))
        return AddLabbookFile(new_labbook_file_edge=LabbookFileConnection.Edge(
            node=LabbookFile(**create_data), cursor=cursor))
Beispiel #7
0
    def mutate_and_process_upload(cls, info, upload_file_path, upload_filename,
                                  **kwargs):
        if not upload_file_path:
            logger.error('No file uploaded')
            raise ValueError('No file uploaded')

        owner = kwargs.get('owner')
        labbook_name = kwargs.get('labbook_name')
        section = kwargs.get('section')
        transaction_id = kwargs.get('transaction_id')
        file_path = kwargs.get('file_path')

        try:
            username = get_logged_in_username()
            lb = InventoryManager().load_labbook(username,
                                                 owner,
                                                 labbook_name,
                                                 author=get_logged_in_author())
            dst_path = os.path.join(os.path.dirname(file_path),
                                    upload_filename)
            with lb.lock():
                fops = FileOperations.put_file(labbook=lb,
                                               section=section,
                                               src_file=upload_file_path,
                                               dst_path=dst_path,
                                               txid=transaction_id)
        finally:
            try:
                logger.debug(f"Removing temp file {upload_file_path}")
                os.remove(upload_file_path)
            except FileNotFoundError:
                pass

        # Create data to populate edge
        create_data = {
            'owner': owner,
            'name': labbook_name,
            'section': section,
            'key': fops['key'],
            '_file_info': fops
        }

        # TODO: Fix cursor implementation..this currently doesn't make sense when adding edges without a refresh
        cursor = base64.b64encode(f"{0}".encode('utf-8'))
        return AddLabbookFile(new_labbook_file_edge=LabbookFileConnection.Edge(
            node=LabbookFile(**create_data), cursor=cursor))
    def mutate_and_get_payload(cls,
                               root,
                               info,
                               owner,
                               labbook_name,
                               section,
                               directory,
                               client_mutation_id=None):
        username = get_logged_in_username()

        working_directory = Configuration().config['git']['working_directory']
        inferred_lb_directory = os.path.join(working_directory, username,
                                             owner, 'labbooks', labbook_name)
        lb = LabBook(author=get_logged_in_author())
        lb.from_directory(inferred_lb_directory)
        lb.makedir(os.path.join(section, directory),
                   create_activity_record=True)
        logger.info(f"Made new directory in `{directory}`")

        # Prime dataloader with labbook you already loaded
        dataloader = LabBookLoader()
        dataloader.prime(f"{owner}&{labbook_name}&{lb.name}", lb)

        # Create data to populate edge
        file_info = lb.get_file_info(section, directory)
        create_data = {
            'owner': owner,
            'name': labbook_name,
            'section': section,
            'key': file_info['key'],
            '_file_info': file_info
        }

        # TODO: Fix cursor implementation, this currently doesn't make sense
        cursor = base64.b64encode(f"{0}".encode('utf-8'))

        return MakeLabbookDirectory(
            new_labbook_file_edge=LabbookFileConnection.Edge(
                node=LabbookFile(**create_data), cursor=cursor))
Beispiel #9
0
 def mutate_and_wait_for_chunks(cls, info, **kwargs):
     return AddLabbookFile(new_labbook_file_edge=LabbookFileConnection.Edge(
         node=None, cursor="null"))