def castFromUi(self, value):

        if self.isMulti():
            if value and isinstance(value, basestring):
                values = _STR_TO_LIST_REXP.findall(value)
            else:
                values = argToList(value)

            _castFromUi = self._castFromUi
            return list(_castFromUi(v) for v in values)

        return value
    def castFromUi(self, value):

        if self.isMulti():
            if value and isinstance(value, basestring):
                values = _STR_TO_LIST_REXP.findall(value)
            else:
                values = argToList(value)

            _castFromUi = self._castFromUi
            return list(_castFromUi(v) for v in values)

        return value
Example #3
0
    def castToWrite(self, in_value):
        value = DbBaseProperty.castToWrite(self, in_value)

        if self.isMulti():

            if value and isinstance(value, basestring):
                values = _STR_TO_LIST_REXP.findall(value)
            else:
                values = argToList(value)

            value = u",".join(toUnicode(v) for v in values) if value else None
        else:
            value = toUnicode(value)

        if value:
            value = value.replace(u"%", u"percent")

        return value
def distribTree(in_sSrcRootDir, in_sDestRootDir, **kwargs):

    bDryRun = kwargs.get("dry_run", False)

    bPrintSrcOnly = kwargs.pop("printSourceOnly", False)
    sFilePathList = kwargs.pop("filePaths", "NoEntry")

    sReplaceExtDct = kwargs.pop("replaceExtensions", kwargs.pop("replaceExts", {}))
    if not isinstance(sReplaceExtDct, dict):
        raise TypeError('"replaceExtensions" kwarg expects {0} but gets {1}.'
                        .format(dict, type(sReplaceExtDct)))

    sEncryptExtList = kwargs.pop("encryptExtensions", kwargs.pop("encryptExts", []))
    if not isinstance(sEncryptExtList, list):
        raise TypeError('"encryptExtensions" kwarg expects {0} but gets {1}.'
                        .format(list, type(sEncryptExtList)))

    if sEncryptExtList:
        raise NotImplementedError, "Sorry, feature has been removed."
        # import cryptUtil
        sEncryptExtList = list(e.strip(".") for e in sEncryptExtList)

    sSrcRootDir = addEndSlash(pathNorm(in_sSrcRootDir))
    sDestRootDir = addEndSlash(pathNorm(in_sDestRootDir))

    if not osp.isdir(sSrcRootDir):
        raise ValueError, 'No such directory found: "{0}"'.format(sSrcRootDir)

    if not osp.isdir(sDestRootDir):
        print 'Creating destination directory: "{0}"'.format(sDestRootDir)
        if not bDryRun:
            os.makedirs(sDestRootDir)

    sCopiedFileList = []

    if sFilePathList == "NoEntry":
        sMsg = "Sorry, but for now, you must provide a list of file paths to copy."
        raise NotImplementedError(sMsg)
    else:
        sFilePathList = argToList(sFilePathList)
        sFilePathList.sort()

        srcRootDirRexp = re.compile("^" + sSrcRootDir, re.I)
        destRootDirRexp = re.compile("^" + sDestRootDir, re.I)

        # building destination directories
        sDestDirList = sFilePathList[:]

        iMaxPathLen = 0
        for i, sFilePath in enumerate(sFilePathList):

            sSrcDir = addEndSlash(pathNorm(osp.dirname(sFilePath)))

            sRexpList = srcRootDirRexp.findall(sSrcDir)
            if not sRexpList:
                raise RuntimeError, "File outside of source directory: {0}.".format(sSrcDir)

            sDestDirList[i] = sSrcDir.replace(sRexpList[0], sDestRootDir)

            iPathLen = len(srcRootDirRexp.split(sFilePath, 1)[1])
            if iPathLen > iMaxPathLen:
                iMaxPathLen = iPathLen

        iNumFiles = len(sFilePathList)
        iDoneFileCount = 0
        iCountLen = len(str(iNumFiles)) * 2 + 5

        sPrintFormat = "{0:^{width1}} {1:<{width2}} >> {2}"
        sPrintFormat = sPrintFormat if not bPrintSrcOnly else sPrintFormat.split(">>", 1)[0]

        def endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount):

            iDoneFileCount += 1

            if bCopied:
                sCount = "{0}/{1}".format(iDoneFileCount, iNumFiles)
                print sPrintFormat.format(sCount,
                                          srcRootDirRexp.split(sFilePath, 1)[1],
                                          destRootDirRexp.split(sDestPath, 1)[1],
                                          width1=iCountLen,
                                          width2=iMaxPathLen)

                sCopiedFileList.append(sDestPath)

            return iDoneFileCount

        print '{0} files to copy from "{1}" to "{2}":'.format(iNumFiles, sSrcRootDir, sDestRootDir)

        # creating directories
        for sDestDir in sorted(set(sDestDirList)):
            if (not osp.isdir(sDestDir)) and (not bDryRun):
                os.makedirs(sDestDir)

        # copying files
        if sReplaceExtDct:

            for sFilePath, sDestDir in zip(sFilePathList, sDestDirList):

                sPath, sExt = osp.splitext(sFilePath); sExt = sExt.strip(".")
                sNewExt = sReplaceExtDct.get(sExt, "")
                if sNewExt:
                    sDestPath = pathJoin(sDestDir, osp.basename(sPath)) + "." + sNewExt.strip(".")
                else:
                    sDestPath = pathJoin(sDestDir, osp.basename(sFilePath))

                bCopied = True
                if sExt in sEncryptExtList:
                    pass# bCopied = cryptUtil.encryptFile(sFilePath, sDestPath, **kwargs)
                else:
                    sDestPath, bCopied = copyFile(sFilePath, sDestPath, **kwargs)

                iDoneFileCount = endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount)

        elif sEncryptExtList:

            for sFilePath, sDestDir in zip(sFilePathList, sDestDirList):

                sExt = osp.splitext(sFilePath)[1].strip(".")

                # print "\t{0} >> {1}".format( srcRootDirRexp.split( sFilePath, 1 )[1], destRootDirRexp.split( sDestDir, 1 )[1] )

                sDestPath = pathJoin(sDestDir, osp.basename(sFilePath))

                bCopied = True
                if sExt in sEncryptExtList:
                    pass# bCopied = cryptUtil.encryptFile(sFilePath, sDestPath, **kwargs)
                else:
                    _, bCopied = copyFile(sFilePath, sDestPath, **kwargs)

                iDoneFileCount = endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount)

        else:

            for sFilePath, sDestDir in zip(sFilePathList, sDestDirList):

                sDestPath = pathJoin(sDestDir, osp.basename(sFilePath))

                _, bCopied = copyFile(sFilePath, sDestPath, **kwargs)

                iDoneFileCount = endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount)

    return sCopiedFileList
    def defaultValue(self):

        value = copy(self.propertyDct.get("default", "undefined"))
        return argToList(value) if self.__isMulti else value
def distribTree(in_sSrcRootDir, in_sDestRootDir, **kwargs):

    bDryRun = kwargs.get("dry_run", False)

    bPrintSrcOnly = kwargs.pop("printSourceOnly", False)
    sFilePathList = kwargs.pop("filePaths", "NoEntry")

    sReplaceExtDct = kwargs.pop("replaceExtensions", kwargs.pop("replaceExts", {}))
    if not isinstance(sReplaceExtDct, dict):
        raise TypeError('"replaceExtensions" kwarg expects {0} but gets {1}.'
                        .format(dict, type(sReplaceExtDct)))

    sEncryptExtList = kwargs.pop("encryptExtensions", kwargs.pop("encryptExts", []))
    if not isinstance(sEncryptExtList, list):
        raise TypeError('"encryptExtensions" kwarg expects {0} but gets {1}.'
                        .format(list, type(sEncryptExtList)))

    if sEncryptExtList:
        raise NotImplementedError, "Sorry, feature has been removed."
        # import cryptUtil
        sEncryptExtList = list(e.strip(".") for e in sEncryptExtList)

    sSrcRootDir = addEndSlash(pathNorm(in_sSrcRootDir))
    sDestRootDir = addEndSlash(pathNorm(in_sDestRootDir))

    if not osp.isdir(sSrcRootDir):
        raise ValueError, 'No such directory found: "{0}"'.format(sSrcRootDir)

    if not osp.isdir(sDestRootDir):
        print 'Creating destination directory: "{0}"'.format(sDestRootDir)
        if not bDryRun:
            os.makedirs(sDestRootDir)

    sCopiedFileList = []

    if sFilePathList == "NoEntry":
        sMsg = "Sorry, but for now, you must provide a list of file paths to copy."
        raise NotImplementedError(sMsg)
    else:
        sFilePathList = argToList(sFilePathList)
        sFilePathList.sort()

        srcRootDirRexp = re.compile("^" + sSrcRootDir, re.I)
        destRootDirRexp = re.compile("^" + sDestRootDir, re.I)

        # building destination directories
        sDestDirList = sFilePathList[:]

        iMaxPathLen = 0
        for i, sFilePath in enumerate(sFilePathList):

            sSrcDir = addEndSlash(pathNorm(osp.dirname(sFilePath)))

            sRexpList = srcRootDirRexp.findall(sSrcDir)
            if not sRexpList:
                raise RuntimeError, "File outside of source directory: {0}.".format(sSrcDir)

            sDestDirList[i] = sSrcDir.replace(sRexpList[0], sDestRootDir)

            iPathLen = len(srcRootDirRexp.split(sFilePath, 1)[1])
            if iPathLen > iMaxPathLen:
                iMaxPathLen = iPathLen

        iNumFiles = len(sFilePathList)
        iDoneFileCount = 0
        iCountLen = len(str(iNumFiles)) * 2 + 5

        sPrintFormat = "{0:^{width1}} {1:<{width2}} >> {2}"
        sPrintFormat = sPrintFormat if not bPrintSrcOnly else sPrintFormat.split(">>", 1)[0]

        def endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount):

            iDoneFileCount += 1

            if bCopied:
                sCount = "{0}/{1}".format(iDoneFileCount, iNumFiles)
                print sPrintFormat.format(sCount,
                                          srcRootDirRexp.split(sFilePath, 1)[1],
                                          destRootDirRexp.split(sDestPath, 1)[1],
                                          width1=iCountLen,
                                          width2=iMaxPathLen)

                sCopiedFileList.append(sDestPath)

            return iDoneFileCount

        print '{0} files to copy from "{1}" to "{2}":'.format(iNumFiles, sSrcRootDir, sDestRootDir)

        # creating directories
        for sDestDir in sorted(set(sDestDirList)):
            if (not osp.isdir(sDestDir)) and (not bDryRun):
                os.makedirs(sDestDir)

        # copying files
        if sReplaceExtDct:

            for sFilePath, sDestDir in zip(sFilePathList, sDestDirList):

                sPath, sExt = osp.splitext(sFilePath); sExt = sExt.strip(".")
                sNewExt = sReplaceExtDct.get(sExt, "")
                if sNewExt:
                    sDestPath = pathJoin(sDestDir, osp.basename(sPath)) + "." + sNewExt.strip(".")
                else:
                    sDestPath = pathJoin(sDestDir, osp.basename(sFilePath))

                bCopied = True
                if sExt in sEncryptExtList:
                    pass# bCopied = cryptUtil.encryptFile(sFilePath, sDestPath, **kwargs)
                else:
                    sDestPath, bCopied = copyFile(sFilePath, sDestPath, **kwargs)

                iDoneFileCount = endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount)

        elif sEncryptExtList:

            for sFilePath, sDestDir in zip(sFilePathList, sDestDirList):

                sExt = osp.splitext(sFilePath)[1].strip(".")

                # print "\t{0} >> {1}".format( srcRootDirRexp.split( sFilePath, 1 )[1], destRootDirRexp.split( sDestDir, 1 )[1] )

                sDestPath = pathJoin(sDestDir, osp.basename(sFilePath))

                bCopied = True
                if sExt in sEncryptExtList:
                    pass# bCopied = cryptUtil.encryptFile(sFilePath, sDestPath, **kwargs)
                else:
                    _, bCopied = copyFile(sFilePath, sDestPath, **kwargs)

                iDoneFileCount = endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount)

        else:

            for sFilePath, sDestDir in zip(sFilePathList, sDestDirList):

                sDestPath = pathJoin(sDestDir, osp.basename(sFilePath))

                _, bCopied = copyFile(sFilePath, sDestPath, **kwargs)

                iDoneFileCount = endCopy(sFilePath, sDestPath, bCopied, iDoneFileCount)

    return sCopiedFileList
    def defaultValue(self):

        value = copy(self.propertyDct.get("default", "undefined"))
        return argToList(value) if self.__isMulti else value