def list_contents(
            self,
            directoryToList):
        """
        *list directory contents*

        **Key Arguments:**
            - ``directoryToList`` -- path to the dropbox folder to list contents of

        **Return:**
            - ``filePaths`` -- list of the content filepaths
            - ``fileNames`` -- list of the content filenames
        """
        self.log.info('starting the ``list_contents`` method')

        # GRAB THE FILEPATHS
        listing = self.client.metadata(path=directoryToList)
        listing = listing["contents"]

        filePaths = []
        filePaths[:] = [f["path"] for f in listing]
        meta = []
        meta[:] = [f for f in listing]

        shareUrls = []
        if self.shareUrls:
            for p in filePaths:
                url = share_file.share_file(
                    log=self.log,
                    settings=self.settings,
                    filePath=p,
                    short=False
                ).get()
                shareUrls.append(url)
        else:
            for p in filePaths:
                shareUrls.append(None)

        # SPLICE OFF FILENAMES FROM PATHS
        fileNames = []
        fileNames[:] = [f.split("/")[-1] for f in filePaths]

        self.log.info('completed the ``list_contents`` method')
        return filePaths, fileNames, shareUrls, meta
Ejemplo n.º 2
0
def main(arguments=None):
    """
    *The main function used when ``cl_utils.py`` is run as a single script from the cl, or when installed as a cl command*
    """
    # setup the command-line util settings
    su = tools(
        arguments=arguments,
        docString=__doc__,
        logLevel="WARNING",
        options_first=False,
        projectName="dropterm"
    )
    arguments, settings, log, dbConn = su.setup()

    # tab completion for raw_input
    readline.set_completer_delims(' \t\n;')
    readline.parse_and_bind("tab: complete")
    readline.set_completer(tab_complete)

    # unpack remaining cl arguments using `exec` to setup the variable names
    # automatically
    for arg, val in arguments.iteritems():
        if arg[0] == "-":
            varname = arg.replace("-", "") + "Flag"
        else:
            varname = arg.replace("<", "").replace(">", "")
        if isinstance(val, str) or isinstance(val, unicode):
            val = val.replace("'", "\\'")
            exec(varname + " = '%s'" % (val,))
        else:
            exec(varname + " = %s" % (val,))
        if arg == "--dbConn":
            dbConn = val
        log.debug('%s = %s' % (varname, val,))

    ## START LOGGING ##
    startTime = times.get_now_sql_datetime()
    log.info(
        '--- STARTING TO RUN THE cl_utils.py AT %s' %
        (startTime,))

    # set options interactively if user requests
    if "interactiveFlag" in locals() and interactiveFlag:

        # load previous settings
        moduleDirectory = os.path.dirname(__file__) + "/resources"
        pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
        try:
            with open(pathToPickleFile):
                pass
            previousSettingsExist = True
        except:
            previousSettingsExist = False
        previousSettings = {}
        if previousSettingsExist:
            previousSettings = pickle.load(open(pathToPickleFile, "rb"))

        # x-raw-input
        # x-boolean-raw-input
        # x-raw-input-with-default-value-from-previous-settings

        # save the most recently used requests
        pickleMeObjects = []
        pickleMe = {}
        theseLocals = locals()
        for k in pickleMeObjects:
            pickleMe[k] = theseLocals[k]
        pickle.dump(pickleMe, open(pathToPickleFile, "wb"))

    # call the worker function
    if authenticate == True:
        this = authenticate_dropterm.authenticate_dropterm(
            log=log,
            settings=settings,
            pathToSettings=pathToSettingsFile
        )
    if move == True and contents == False:
        this = move_files_or_folders.move_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath
        )
        this.move()
    if move == True and contents == True:
        this = move_files_or_folders.move_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath,
            moveFolderContents=True
        )
        this.move()
    if copy == True and contents == False:

        this = copy_files_or_folders.copy_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath
        )
        this.copy()
    if copy == True and contents == True:
        this = copy_files_or_folders.copy_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath,
            copyFolderContents=True
        )
        this.copy()
    if ls == True:
        this = list_directory_contents(
            log=log,
            settings=settings,
            directoryToList=directoryPath
        )
        filePaths, fileNames, shareUrls, meta = this.get()
        for f in fileNames:
            print f
    if share == True:
        if lFlag == False:
            lFlag = True
        else:
            lFlag = False
        this = share_file.share_file(
            log=log,
            settings=settings,
            filePath=filePath,
            short=lFlag
        )
        url = this.get()
        print url
    if mkdir == True:
        this = mkdirectory.mkdir(
            log=log,
            settings=settings,
            dropboxPath=dirPath
        ).get()
    if touch == True:
        this = touch_file.touch_file(
            log=log,
            settings=settings,
            destination=destinationPath
        ).touch()

    if "dbConn" in locals() and dbConn:
        dbConn.commit()
        dbConn.close()
    ## FINISH LOGGING ##
    endTime = times.get_now_sql_datetime()
    runningTime = times.calculate_time_difference(startTime, endTime)
    log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' %
             (endTime, runningTime, ))

    return