Example #1
0
 def cleanFilename(cls, filename):
     if not filename:
         return StringUtils.getRandomString(12)
     out = StringUtils.slugify(filename)
     if not out:
         return StringUtils.getRandomString(12)
     return out
Example #2
0
 def cleanFilename(cls, filename):
     if not filename:
         return StringUtils.getRandomString(12)
     out = StringUtils.slugify(filename)
     if not out:
         return StringUtils.getRandomString(12)
     return out
Example #3
0
 def __init__(self):
     """Creates a new instance of UniqueObject."""
     self._INSTANCE_INDEX += 1
     self._instanceUid = TimeUtils.getUidTimecode(
         prefix=self.__class__.__name__,
         suffix=StringUtils.toUnicode(self._INSTANCE_INDEX) + '-' +
         StringUtils.getRandomString(8))
Example #4
0
    def _makePlot(self, label, data, color ='b', isLog =False, histRange =None):
        """_makePlot doc..."""

        pl = self.plot
        self.owner.createFigure('makePlot')

        pl.hist(
            data, 31,
            range=histRange,
            log=isLog,
            facecolor=color,
            alpha=0.75)
        pl.title('%s Distribution%s' % (label, ' (log)' if isLog else ''))
        pl.xlabel('Fractional Deviation')
        pl.ylabel('Frequency')
        pl.grid(True)

        axis = pl.gca()
        xlims = axis.get_xlim()
        pl.xlim((max(histRange[0], xlims[0]), min(histRange[1], xlims[1])))

        path = self.getTempPath(
            '%s.pdf' % StringUtils.getRandomString(16), isFile=True)
        self.owner.saveFigure('makePlot', path)
        return path
Example #5
0
 def __init__(self):
     """Creates a new instance of UniqueObject."""
     self._INSTANCE_INDEX += 1
     self._instanceUid = TimeUtils.getUidTimecode(
         prefix=self.__class__.__name__,
         suffix=StringUtils.toUnicode(
             self._INSTANCE_INDEX) + '-' + StringUtils.getRandomString(8) )
Example #6
0
 def __init__(self, parent=None, **kwargs):
     """Creates a new instance of VisibilityElement."""
     super(VisibilityElement, self).__init__(parent=parent)
     self._instanceUid = TimeUtils.getUidTimecode(
         prefix=self.__class__.__name__, suffix=StringUtils.getRandomString(8)
     )
     self._visibility = VisibilityManager(target=self)
Example #7
0
    def saveFigure(self, key, path =None, close =True, **kwargs):
        """ Saves the specified figure to a file at teh specified path.

            key :: String
                The key for the figure to be saved. If no such key exists, the method will return
                false.

            path :: String :: None
                The absolute file location to where the figure should be saved. If no path is
                specified the file will be saved as a pdf in this Analyzer's temporary folder.

            close :: Boolean :: True
                If true, the figure will be closed upon successful completion of the save process.

            [kwargs]
                Data to be passed directly to the PyPlot Figure.savefig() method, which can be
                used to further customize how the figure is saved. """

        if not plt or key not in self._plotFigures:
            return False

        if not path:
            path = self.getTempPath('%s-%s.pdf' % (
                key, TimeUtils.getUidTimecode(suffix=StringUtils.getRandomString(16))), isFile=True)

        figure = self._plotFigures[key]

        if 'orientation' not in kwargs:
            kwargs['orientation'] = 'landscape'

        figure.savefig(path, **kwargs)
        if close:
            self.closeFigure(key)
        return path
Example #8
0
    def getTempFilePath(self, name =None, extension =None, *args):
        """ Used to create a temporary file path within this instance's temporary folder.
            Any file on this path will be automatically removed at the end of the analysis
            process.

            [name] :: String :: None
                The desired file name for the desired file within the temporary directory. If no
                name is specified, a name will be created automatically using the current time
                (microsecond) and a 16 digit random code for a very low probability of collisions.

            [extension] :: String :: None
                Specifies the extension to add to this file. The file name is not altered if no
                extension is specified.

            [*args] :: [String] :: []
                A list of relative folder prefixes in which the file should reside. For example,
                if you wish to have a file 'bar' in a folder 'foo' then you would specify 'foo' as
                the single arg to get a file located at 'foo/bar' within the temporary file. No
                directory prefixes will be created within this method. """

        if not name:
            name = TimeUtils.getUidTimecode(suffix=StringUtils.getRandomString(16))
        if extension:
            extension = '.' + extension.strip('.')
            if not name.endswith(extension):
                name += extension

        args = list(args) + [name]
        return FileUtils.makeFilePath(self.tempPath, *args)
Example #9
0
 def _addCRSFToken(self, tokenName =None):
     name  = tokenName if tokenName else ZigguratDisplayView.DEFAULT_CSRF_TOKEN_NAME
     value = StringUtils.getRandomString(16)
     self._request.response.set_cookie(name, value, overwrite=True)
     self._request.response.set_cookie(
         name + '-secure',
         value,
         secure=True,
         httponly=True,
         overwrite=True)
Example #10
0
 def createUniqueId(cls, prefix = u''):
     """ Creates a universally unique identifier string based on current
         time, active application instance state, and a randomized hash
     """
     cls._UID_INDEX += 1
     return '%s%s-%s-%s' % (
         prefix,
         TimeUtils.getNowTimecode(cls.BASE_UNIX_TIME),
         Base64.to64(cls._UID_INDEX),
         StringUtils.getRandomString(12))
Example #11
0
    def __init__(self, source, **kwargs):

        self.footerDom  = u''
        self.page       = ArgsUtils.get('page', None, kwargs)
        self.site       = ArgsUtils.get('site', None, kwargs)

        self.filePath = ArgsUtils.get('path', None, kwargs)
        self.filename = ArgsUtils.get(
            'filename',
            os.path.basename(self.filePath) if self.filePath else None,
            kwargs)

        debugData = ArgsUtils.extract('debugData', None, kwargs)
        blocks    = {
            'root':[
                MarkupTextBlockUtils.createMarkupCommentDef(BlockDefinition.BLOCKED),
                MarkupTextBlockUtils.createMarkupOpenDef('quote'),
                MarkupTextBlockUtils.createMarkupCloseDef(BlockDefinition.BLOCKED) ],
            'quote':[
                BlockDefinition.createQuoteDef(BlockDefinition.BLOCKED),
                BlockDefinition.createLiteralDef(BlockDefinition.BLOCKED) ]}

        self._renderErrors = []
        self._tagIndex     = -1

        super(MarkupProcessor, self).__init__(
            source,
            ArgsUtils.extract('debug', False, kwargs),
            blocks,
            debugData,
            stripSource=False,
            **kwargs)

        self.logger.trace       = True
        self._result            = None
        self._anchors           = []
        self._tags              = []
        self._id                = StringUtils.getRandomString(8)
        self._css               = []
        self._js                = []
        self._radioArrays       = dict()
        self._patterns          = dict()
        self._groups            = dict()
        self._metadata          = ArgsUtils.getAsDict('metadata', kwargs)
        self._libraryIDs        = []
        self._autoTitle         = u''
        self._autoDescription   = u''
        self._allowModelCaching = False

        self.privateView = False
Example #12
0
def pingTest():

    from pyaid.string.StringUtils import StringUtils

    import nimble

    nimble.changeKeepAlive(keepAlive)

    conn = nimble.getConnection()

    #-----------------------------------------------------------------------------------------------
    # PING
    #       An empty call that tests that the Nimble connection is able to complete a request and
    #       response loop and prints the response object when complete.
    for i in range(iterations):
        result = conn.ping()
        if not runSilent:
            print(u'PING:', result.echo(True, True))

    #-----------------------------------------------------------------------------------------------
    # ECHO
    #       A basic call that sends an echo message, which the remote Nimble server returns in the
    #       response. Confirms that data can be set, read, and returned through the Nimble
    #       connection.
    smallMessage = 'This is a test'
    for i in range(iterations):
        result = conn.echo(smallMessage)
        if not runSilent:
            print(u'ECHO:', result.echo(True, True))

    #-----------------------------------------------------------------------------------------------
    # LONG ECHO
    #       A repeat of the echo test, but with a very long echo message that tests the ability for
    #       the Nimble socket protocols to chunk and stream large messages without losing data. The
    #       test is also repeated numerous times to confirm that the nimble socket connection
    #       survives through high-intensity multi-stage communication.
    largeMessage = StringUtils.getRandomString(64000)
    for i in range(iterations):
        result = conn.echo(largeMessage)
        if not runSilent:
            print(u'LARGE ECHO[#%s]:' % i, result.echo(True, True))

    print(u'\nTests Complete')
Example #13
0
def pingTest():

    from pyaid.string.StringUtils import StringUtils

    import nimble

    nimble.changeKeepAlive(keepAlive)

    conn = nimble.getConnection()

    #-----------------------------------------------------------------------------------------------
    # PING
    #       An empty call that tests that the Nimble connection is able to complete a request and
    #       response loop and prints the response object when complete.
    for i in range(iterations):
        result = conn.ping()
        if not runSilent:
            print(u'PING:', result.echo(True, True))

    #-----------------------------------------------------------------------------------------------
    # ECHO
    #       A basic call that sends an echo message, which the remote Nimble server returns in the
    #       response. Confirms that data can be set, read, and returned through the Nimble
    #       connection.
    smallMessage = 'This is a test'
    for i in range(iterations):
        result = conn.echo(smallMessage)
        if not runSilent:
            print(u'ECHO:', result.echo(True, True))

    #-----------------------------------------------------------------------------------------------
    # LONG ECHO
    #       A repeat of the echo test, but with a very long echo message that tests the ability for
    #       the Nimble socket protocols to chunk and stream large messages without losing data. The
    #       test is also repeated numerous times to confirm that the nimble socket connection
    #       survives through high-intensity multi-stage communication.
    largeMessage = StringUtils.getRandomString(64000)
    for i in range(iterations):
        result = conn.echo(largeMessage)
        if not runSilent:
            print(u'LARGE ECHO[#%s]:' % i, result.echo(True, True))

    print(u'\nTests Complete')
Example #14
0
    def _process(
            self, label, widthKey, lengthKey, trackDeviations,
            absoluteOnly =False
    ):
        """_process doc..."""
        pl  = self.plot
        ws  = []
        ls  = []
        w2D = []
        l2D = []

        for entry in self.entries:
            if widthKey in entry:
                ws.append(entry[widthKey])
                if lengthKey in entry:
                    w2D.append(entry[widthKey])

            if lengthKey in entry:
                ls.append(entry[lengthKey])
                if widthKey in entry:
                    l2D.append(entry[lengthKey])

        plotList = [
            ('widths', ws, 'Width', 'b'),
            ('lengths', ls, 'Length', 'r')]

        wRes = NumericUtils.getMeanAndDeviation(ws)
        self.logger.write('Width %ss' % wRes.label)
        lRes = NumericUtils.getMeanAndDeviation(ls)
        self.logger.write('Length %ss' % lRes.label)

        for data in plotList:
            if not absoluteOnly:
                d = data[1]
                self._paths.append(
                    self._makePlot(
                        label, d, data,
                        histRange=(-1.0, 1.0)))
                self._paths.append(
                    self._makePlot(
                        label, d, data,
                        isLog=True,
                        histRange=(-1.0, 1.0)))

            # noinspection PyUnresolvedReferences
            d = np.absolute(np.array(data[1]))
            self._paths.append(
                self._makePlot(
                    'Absolute ' + label, d, data,
                    histRange=(0.0, 1.0)))
            self._paths.append(
                self._makePlot(
                    'Absolute ' + label, d, data,
                    isLog=True,
                    histRange=(0.0, 1.0)))

        self.owner.createFigure('twoD')
        pl.hist2d(w2D, l2D, bins=20, range=([-1, 1], [-1, 1]))
        pl.title('2D %s Distribution' % label)
        pl.xlabel('Width %s' % label)
        pl.ylabel('Length %s' % label)
        pl.xlim(-1.0, 1.0)
        pl.ylim(-1.0, 1.0)
        path = self.getTempPath(
            '%s.pdf' % StringUtils.getRandomString(16),
            isFile=True)
        self.owner.saveFigure('twoD', path)
        self._paths.append(path)

        csv = CsvWriter()
        csv.path = self.getPath(
            '%s-Deviations.csv' % label.replace(' ', '-'),
            isFile=True)
        csv.addFields(
            ('uid', 'UID'),
            ('fingerprint', 'Fingerprint'),
            ('wSigma', 'Width Deviation'),
            ('lSigma', 'Length Deviation') )

        count = 0
        for entry in self.entries:
            widthDevSigma  = NumericUtils.roundToOrder(
                abs(entry.get(widthKey, 0.0)/wRes.uncertainty), -2)
            lengthDevSigma = NumericUtils.roundToOrder(
                abs(entry.get(lengthKey, 0.0)/lRes.uncertainty), -1)
            if widthDevSigma > 2.0 or lengthDevSigma > 2.0:
                count += 1
                track = entry['track']
                data = dict(
                    wSigma=widthDevSigma,
                    lSigma=lengthDevSigma)

                if trackDeviations is not None:
                    trackDeviations[track.uid] = data

                csv.createRow(
                    uid=track.uid,
                    fingerprint=track.fingerprint,
                    **data)

        if not csv.save():
            self.logger.write(
                '[ERROR]: Failed to save CSV file to %s' % csv.path)

        percentage = NumericUtils.roundToOrder(
            100.0*float(count)/float(len(self.entries)), -2)
        self.logger.write('%s significant %ss (%s%%)' % (
            count, label.lower(), percentage))
        if percentage > (100.0 - 95.45):
            self.logger.write(
                '[WARNING]: Large deviation count exceeds' +
                'normal distribution expectations.')
Example #15
0
    def __init__(self, **kwargs):
        """Creates a new instance of PyGlassWindow."""
        parent                  = ArgsUtils.extract('parent', None, kwargs)
        self._application       = ArgsUtils.extract('pyGlassApp', None, kwargs)
        self._qApplication      = ArgsUtils.extract('qApp', None, kwargs)
        self._isMainWindow      = ArgsUtils.extract('isMainWindow', bool(parent is None), kwargs)
        self._mainWindow        = ArgsUtils.extract('mainWindow', None, kwargs)
        self._appWrappingWidget = None
        self._centerWidget      = None
        self._hasShown          = False
        self._isHighDpi         = OsUtils.isHighDpiScaledScreen()
        self._settings          = ConfigsDict()

        self._appLevelWidgets              = dict()
        self._appLevelWidgetDisplayHistory = []

        self._keyboardCallback = ArgsUtils.extract('keyboardCallback', None, kwargs)

        if not self._mainWindow:
            if self._isMainWindow:
                self._mainWindow = self
            elif self._application:
                self._mainWindow = self._application.mainWindow

        self._dependentWindows = []
        self._currentWidget    = None

        QtGui.QMainWindow.__init__(self, parent, ArgsUtils.extract('flags', 0, kwargs))

        self._instanceUid = TimeUtils.getUidTimecode(
            prefix=self.__class__.__name__,
            suffix=StringUtils.getRandomString(8))

        self._styleSheet = kwargs.get('styleSheet', None)
        if self._styleSheet:
            self.setStyleSheet(self.styleSheetPath)

        if self._keyboardCallback is not None:
            self.setFocusPolicy(QtCore.Qt.StrongFocus)

        if self._isMainWindow:
            self._log                 = Logger(self, printOut=True)
            self._config              = ApplicationConfig(self)
            self._commonConfig        = ApplicationConfig(self, common=True)
            self._resourceFolderParts = PyGlassGuiUtils.getResourceFolderParts(self)

            icon = PyGlassGuiUtils.createIcon(
                kwargs.get('iconsPath', self.getAppResourcePath('icons', isDir=True)) )
            if icon:
                self.setWindowIcon(icon)

        elif self._mainWindow:
            icon = self._mainWindow.windowIcon()
            if icon:
                self.setWindowIcon(icon)

        self._appWrappingWidget = VisibilityElement(self)
        layout = QtGui.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self._appWrappingWidget.setLayout(layout)

        self._contentWrappingWidget = self.addApplicationLevelWidget('main')
        layout = QtGui.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self._contentWrappingWidget.setLayout(layout)

        # Loads the ui file if it exists
        externalCentralParent = None
        hasWindowFile = kwargs.get('mainWindowFile', False)
        if hasWindowFile == self:
            UiFileLoader.loadWidgetFile(self)
            externalCentralParent = getattr(self, 'windowContainer')
            if externalCentralParent:
                externalLayout = externalCentralParent.layout()
                if not externalLayout:
                    externalLayout = QtGui.QVBoxLayout()
                    externalLayout.setContentsMargins(0, 0, 0, 0)
                    externalLayout.setSpacing(0)
                    externalCentralParent.setLayout(externalLayout)

        if externalCentralParent:
            self._appWrappingWidget.setParent(externalCentralParent)
            externalLayout.addWidget(self._appWrappingWidget)
        else:
            self.setCentralWidget(self._appWrappingWidget)

        # Sets a non-standard central widget
        centralWidgetName = kwargs.get('centralWidgetName')
        if centralWidgetName and hasattr(self, centralWidgetName):
            self._centerWidget = getattr(self, centralWidgetName)
        elif hasWindowFile and not hasWindowFile == self:
            if not self._centerWidget:
                self._createCentralWidget()
            UiFileLoader.loadWidgetFile(self, target=self._centerWidget)
        elif not hasWindowFile:
            self._centerWidget = None

        if not self._centerWidget and kwargs.get('defaultCenterWidget', True):
            self._createCentralWidget()

        self._lastChildWidgetID  = None
        self._widgetParent  = None
        self._widgets       = None
        self._widgetFlags   = None

        self._widgetClasses = kwargs.get('widgets', dict())
        if self._widgetClasses:
            self._initializeWidgetChildren()

        self.setWindowTitle(kwargs.get('title', self._createTitleFromClass()))
        self.updateStatusBar()
Example #16
0
from pyaid.string.StringUtils import StringUtils

import nimble

conn = nimble.getConnection()

result = conn.ping()
print 'PING:', result.echo(True, True)

result = conn.echo('This is a test')
print 'ECHO:', result.echo(True, True)

for i in range(100):
    largeMessage = StringUtils.getRandomString(64000)
    result = conn.echo(largeMessage)
    print 'LARGE ECHO[#%s]:' % i, result.echo(True, True)

print 'Operation complete'