def _deployResources(cls): """ On windows the resource folder data is stored within the application install directory. However, due to permissions issues, certain file types cannot be accessed from that directory without causing the program to crash. Therefore, the stored resources must be expanded into the user's AppData/Local folder. The method checks the currently deployed resources folder and deploys the stored resources if the existing resources either don't exist or don't match the currently installed version of the program. """ if not OsUtils.isWindows() or not PyGlassEnvironment.isDeployed: return False storagePath = PyGlassEnvironment.getInstallationPath('resource_storage', isDir=True) storageStampPath = FileUtils.makeFilePath(storagePath, 'install.stamp') resourcePath = PyGlassEnvironment.getRootResourcePath(isDir=True) resourceStampPath = FileUtils.makeFilePath(resourcePath, 'install.stamp') try: resousrceData = JSON.fromFile(resourceStampPath) storageData = JSON.fromFile(storageStampPath) if resousrceData['CTS'] == storageData['CTS']: return False except Exception as err: pass SystemUtils.remove(resourcePath) FileUtils.mergeCopy(storagePath, resourcePath) return True
def run(self): """Doc...""" resources = self._compiler.resources #------------------------------------------------------------------------------------------- # RESOURCES # If no resource folders were specified copy the entire contents of the resources # folder. Make sure to skip the local resources path in the process. if not resources: for item in os.listdir(PyGlassEnvironment.getRootResourcePath(isDir=True)): itemPath = PyGlassEnvironment.getRootResourcePath(item) if os.path.isdir(itemPath) and not item in ['local', 'apps']: resources.append(item) for container in resources: parts = container.replace('\\', '/').split('/') self._copyResourceFolder( PyGlassEnvironment.getRootResourcePath(*parts, isDir=True), parts) #------------------------------------------------------------------------------------------- # APP RESOURCES appResources = self._compiler.resourceAppIds if not appResources: appResources = [] for appResource in appResources: itemPath = PyGlassEnvironment.getRootResourcePath('apps', appResource, isDir=True) if not os.path.exists(itemPath): self._log.write('[WARNING]: No such app resource path found: %s' % appResource) continue self._copyResourceFolder(itemPath, ['apps', appResource]) #------------------------------------------------------------------------------------------- # PYGLASS RESOURCES # Copy the resources from the PyGlass resources = [] for item in os.listdir(PyGlassEnvironment.getPyGlassResourcePath('..', isDir=True)): itemPath = PyGlassEnvironment.getPyGlassResourcePath('..', item) if os.path.isdir(itemPath): resources.append(item) for container in resources: self._copyResourceFolder( PyGlassEnvironment.getPyGlassResourcePath('..', container), [container]) # Create a stamp file in resources for comparing on future installations creationStampFile = FileUtils.makeFilePath(self._targetPath, 'install.stamp') JSON.toFile(creationStampFile, {'CTS':TimeUtils.toZuluPreciseTimestamp()}) #------------------------------------------------------------------------------------------- # CLEANUP if self._verbose: self._log.write('CLEANUP: Removing unwanted destination files.') self._cleanupFiles(self._targetPath) self._copyPythonStaticResources() if self._verbose: self._log.write('COMPLETE: Resource Collection') return True
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)
def run(self): """Doc...""" # Create the bin directory where the output will be stored if it does not already exist binPath = self.getBinPath(isDir=True) if not os.path.exists(binPath): os.makedirs(binPath) # Remove any folders created by previous build attempts for d in self._CLEANUP_FOLDERS: path = os.path.join(binPath, d) if os.path.exists(path): shutil.rmtree(path) os.chdir(binPath) ResourceCollector(self, verbose=True).run() cmd = [ FileUtils.makeFilePath(sys.prefix, 'bin', 'python'), '"%s"' % self._createSetupFile(binPath), OsUtils.getPerOsValue('py2exe', 'py2app'), '>', '"%s"' % self.getBinPath('setup.log', isFile=True)] print('[COMPILING]: Executing %s' % OsUtils.getPerOsValue('py2exe', 'py2app')) print('[COMMAND]: %s' % ' '.join(cmd)) result = SystemUtils.executeCommand(cmd, remote=False, wait=True) if result['code']: print('COMPILATION ERROR:') print(result['out']) print(result['error']) return False if self.appFilename and OsUtils.isWindows(): name = self.applicationClass.__name__ source = FileUtils.createPath(binPath, 'dist', name + '.exe', isFile=True) dest = FileUtils.createPath(binPath, 'dist', self.appFilename + '.exe', isFile=True) os.rename(source, dest) if OsUtils.isWindows() and not self._createWindowsInstaller(binPath): print('Installer Creation Failed') return False elif OsUtils.isMac() and not self._createMacDmg(binPath): print('DMG Creation Failed') return False # Remove the resources path once compilation is complete resourcePath = FileUtils.createPath(binPath, 'resources', isDir=True) SystemUtils.remove(resourcePath) buildPath = FileUtils.createPath(binPath, 'build', isDir=True) SystemUtils.remove(buildPath) FileUtils.openFolderInSystemDisplay(binPath) return True
def _analyzeTrackway(self, trackway, sitemap): """ @param trackway: @param sitemap: @return: """ self.entries = dict(lp=[], rp=[], lm=[], rm=[]) super(SimulationCsvExporterStage, self)._analyzeTrackway( trackway=trackway, sitemap=sitemap ) csv = CsvWriter( autoIndexFieldName='Index', fields=[ 'lp_name', 'lp_uid', 'lp_x', 'lp_dx', 'lp_y', 'lp_dy', 'rp_name', 'rp_uid', 'rp_x', 'rp_dx', 'rp_y', 'rp_dy', 'lm_name', 'lm_uid', 'lm_x', 'lm_dx', 'lm_y', 'lm_dy', 'rm_name', 'rm_uid', 'rm_x', 'rm_dx', 'rm_y', 'rm_dy' ] ) length = max( len(self.entries['lp']), len(self.entries['rp']), len(self.entries['lm']), len(self.entries['rm']), ) for index in range(length): items = [] for limb_id, entries in self.entries.items(): if index < len(entries): items += entries[index].items() else: items += self._create_entry(limb_id).items() csv.addRow(dict(items)) path = self.owner.settings.fetch('EXPORT_DATA_PATH') if path is None: path = self.owner.getLocalPath('Simulation', 'data', isFile=True) path = FileUtils.makeFilePath(path, trackway.name, 'source.csv') directory = os.path.dirname(path) if not os.path.exists(directory): os.makedirs(directory) if csv.save(path): print('[SAVED]:', path) else: print('[ERROR]: Unable to save CSV at "{}"'.format(path))
def getAnalysisSettings(): """ Retrieves the analysis configuration settings file as a SettingsConfig instance that can be modified and saved as needed. @return: SettingsConfig """ if not __LOCALS__.SETTINGS_CONFIG: __LOCALS__.SETTINGS_CONFIG = SettingsConfig( FileUtils.makeFilePath( PyGlassEnvironment.getRootLocalResourcePath( 'analysis', isDir=True), 'analysis.json'), pretty=True) return __LOCALS__.SETTINGS_CONFIG
def redirectLogOutputs(self, prefix =None, logFolderPath =None): """ Sets a temporary standard out and error for deployed applications in a write allowed location to prevent failed write results. """ if not PyGlassEnvironment.isDeployed: return if not prefix: prefix = self.appID if not prefix.endswith('_'): prefix += '_' if logFolderPath: logPath = logFolderPath elif PyGlassEnvironment.isInitialized: logPath = PyGlassEnvironment.getRootLocalResourcePath('logs', isDir=True) else: prefix += 'init_' if appdirs: logPath = appdirs.user_data_dir(self.appID, self.appGroupID) else: logPath = FileUtils.createPath( os.path.expanduser('~'), '.pyglass', self.appGroupID, self.appID, isDir=True) FileUtils.getDirectoryOf(logPath, createIfMissing=True) try: sys.stdout.flush() sys.stdout.close() except Exception as err: pass sys.stdout = open(FileUtils.makeFilePath(logPath, prefix + 'out.log'), 'w+') try: sys.stderr.flush() sys.stderr.close() except Exception as err: pass sys.stderr = open(FileUtils.makeFilePath(logPath, prefix + 'error.log'), 'w+') return True
def loadWidgetFile(cls, widget, names =None, loadAsWidget =True, target =None): """ Loads the UI file for the widget from its resource path. """ widgetName = widget.__class__.__name__ lookups = ListUtils.asList(names, allowTuples=False) + [widgetName, 'widget', 'main', 'gui'] if not target: target = widget try: path = widget.getResourcePath(isDir=True, inApp=True) except Exception: raise IOError('[ERROR]: No widget resource path found for "%s" widget' % (widgetName)) if not os.path.exists(path): try: path = widget.getResourcePath(isDir=True) except Exception: raise IOError( '[ERROR]: No widget resource path found for "%s" widget' % (widgetName)) if not os.path.exists(path): raise IOError( '[ERROR]: Missing widget resource path [%s]: %s' % (widgetName, path)) result = None for item in lookups: result = cls.loadUiFile( target=target, pathNoExtension=FileUtils.makeFilePath(path, item, isFile=True), loadAsWidget=loadAsWidget) if result: break if not result: raise Exception('[ERROR]: No UI file found at: ' + path) if result is not target: layout = QtGui.QVBoxLayout() layout.addWidget(result) target.setLayout(layout) elements = [] for item in dir(result): item = getattr(result, item) if isinstance(item, QtGui.QWidget): elements.append(item) else: layout = target.layout() elements = None return {'widget':result, 'layout':layout, 'elements':elements}
def getPathFromDatabaseUrl(cls, databaseUrl, localResourcesPath=None): urlParts = databaseUrl.split("://") # Determine the sqlite database path if urlParts[0].lower() == "shared": path = ["shared", "data"] else: path = ["apps", urlParts[0], "data"] path += urlParts[1].strip("/").split("/") if not path[-1].endswith(".vdb"): path[-1] += ".vdb" if localResourcesPath: return FileUtils.makeFilePath(localResourcesPath, *path) return PyGlassEnvironment.getRootLocalResourcePath(*path, isFile=True)
def hasMigrationEnvironment(cls, databaseUrl, resourcesPath =None): """ Determines whether or not the specified application database currently has a migration environment setup :param databaseUrl: :param resourcesPath: :return: True or false depending on the presence of a migration environment """ if not resourcesPath: resourcesPath = PyGlassEnvironment.getRootResourcePath(isDir=True) migrationPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl( databaseUrl=databaseUrl, resourcesPath=resourcesPath) if not os.path.exists(migrationPath): return False if not os.path.exists(FileUtils.makeFilePath(migrationPath, 'alembic.ini')): return False if not os.path.exists(FileUtils.makeFolderPath(migrationPath, 'versions')): return False return True
def generateData(tracks): trackway_number = tracks.iloc[0]["trackwayNumber"] metadata = {"key": "/trackway/tw_{}".format(trackway_number), "count": len(tracks)} df = tracks.sort_values(["number", "pes", "left"], ascending=[1, 0, 0]) df = df.copy() df["number"] = df["number"].str.lstrip("0") fields = [] for entry in CONVERSIONS: fields.append((get_key(entry.src), entry.label)) csv = CsvWriter(path=FileUtils.makeFilePath(DATA_DIR, "BEB-500 S-{}.csv".format(trackway_number)), fields=fields) for index, track in df.iterrows(): csv.addRow(create_export_entry(index, track, tracks)) csv.save() df.to_hdf(OUT_PATH, key=metadata["key"], mode="a") return metadata
def initializeDatabase(cls, databaseUrl, resourcesPath =None, localResourcesPath =None): migrationRootPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl( databaseUrl=databaseUrl, root=True, resourcesPath=resourcesPath) migrationPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl( databaseUrl=databaseUrl, resourcesPath=resourcesPath) configPath = FileUtils.makeFilePath(migrationPath, 'alembic.ini') if not os.path.exists(migrationRootPath): os.makedirs(migrationRootPath) if os.path.exists(migrationPath + 'alembic.ini'): return False os.chdir(migrationRootPath) config = alembicConfig.Config() config.config_file_name = configPath alembicCmd.init(config=config, directory=migrationPath) # Refresh config with proper settings cp = configparser.ConfigParser() cp.read(configPath) cp.set('alembic', 'sqlalchemy.url', PyGlassModelUtils.getEngineUrl( databaseUrl=databaseUrl, localResourcesPath=localResourcesPath)) f = open(configPath, 'w+') cp.write(f) f.close() return True
def getAnalysisData( analyzerClass, filename, analysisRootPath =None, renames =None ): """ Using the analyzer class and CSV filename, return a Pandas DataFrame containing that data. @param analyzerClass: Class @param filename: str @param analysisRootPath: str @param renames: dict A dictionary containing columns to rename. The old column names are the keys and the new column names the values. @return: DataFrame """ folderName = analyzerClass.__name__ path = FileUtils.makeFilePath(analysisRootPath, folderName, filename) \ if analysisRootPath \ else getAnalysisPath(folderName, filename, isFile=True) df = pd.read_csv(path) pops.rename.columns(df, renames) return df
from __future__ import print_function, absolute_import, unicode_literals, division import os import sys from pyaid.file.FileUtils import FileUtils from pyaid.json.JSON import JSON from pyaid.system.SystemUtils import SystemUtils FOLDER_NAME = 'Statistical-Results' #--------------------------------------------------------------------------------------------------- rootPath = FileUtils.getDirectoryOf(__file__) localAnalysisPath = FileUtils.makeFolderPath(rootPath, '..', 'resources', 'local', 'analysis') analysisConfigPath = FileUtils.makeFilePath(localAnalysisPath, 'analysis.json') config = JSON.fromFile(analysisConfigPath) if 'OUTPUT_PATH' not in config: rootTargetPath = localAnalysisPath else: rootTargetPath = FileUtils.cleanupPath(config['OUTPUT_PATH'], isDir=True) targetPath = FileUtils.makeFolderPath(rootTargetPath, FOLDER_NAME) if os.path.exists(targetPath): SystemUtils.remove(targetPath) outputPath = FileUtils.makeFolderPath(rootPath, 'output')
from pyglass.app.PyGlassEnvironment import PyGlassEnvironment from cadence.svg.CadenceDrawing import CadenceDrawing PyGlassEnvironment.initializeFromInternalPath(__file__) from cadence.models.tracks.Tracks_SiteMap import Tracks_SiteMap #--------------------------------------------------------------------------------------------------- model = Tracks_SiteMap.MASTER session = model.createSession() sitemap = session.query(model).filter(model.name == 'BEB').filter(model.level == '515').first() print('SITEMAP[%s]: %s' % (sitemap.index, sitemap.name)) output = FileUtils.makeFilePath(OsUtils.getHomePath(), 'Desktop', '%s.svg' % sitemap.name) drawing = CadenceDrawing(output, sitemap) trackways = sitemap.getTrackways() print('TRACKWAY COUNT: %s' % len(trackways)) for trackway in trackways: print('TRACKWAY[%s]: %s' % (trackway.index, trackway.name)) for key, series in DictUtils.iter(trackway.getTrackwaySeriesBundle()): for track in series.tracks: print(' * %s' % track.fingerprint) drawing.circle( track.positionValue.toMayaTuple(), 5, stroke='none', fill='#003300', fill_opacity='0.1')
import numpy as np import pandas as pd import plotly.graph_objs as plotlyGraph from cadence.analysis.shared import DataLoadUtils, PlotConfigs from cadence.analysis.shared.plotting import PlotlyUtils from plotly import plotly ################################################################################ locale.setlocale(locale.LC_ALL, ('en_US', 'utf8')) PLOTLY_FOLDER = 'Comparison' MY_DIR = FileUtils.getDirectoryOf(__file__) DATA_DIR = FileUtils.makeFolderPath(MY_DIR, 'data') OUT_PATH = FileUtils.makeFilePath(DATA_DIR, 'deviation.h5') METADATA_FILE = FileUtils.makeFilePath(DATA_DIR, 'deviation.metadata.json') #_______________________________________________________________________________ def _getLayout( metadata, title, fixed = False, xAxis =None, yAxis =None, **kwargs ): if not xAxis: xAxis = {} xAxis.setdefault('title', 'Deviation (%)') if not yAxis: yAxis = {} yAxis.setdefault('title', 'Frequency') yAxis.setdefault('autorange', True)
from __future__ import unicode_literals from collections import namedtuple from pyaid.file.CsvWriter import CsvWriter from pyaid.file.FileUtils import FileUtils import six from cadence import reporting from cadence.analysis.shared import DataLoadUtils from cadence.enums.TrackCsvColumnEnum import TrackCsvColumnEnum from cadence.enums.TrackPropEnum import TrackPropEnum MY_DIR = FileUtils.getDirectoryOf(__file__) DATA_DIR = FileUtils.makeFolderPath(MY_DIR, "data") OUT_PATH = FileUtils.makeFilePath(DATA_DIR, "beb500.h5") METADATA_FILE = FileUtils.makeFilePath(DATA_DIR, "beb500.metadata.json") TCCE = TrackCsvColumnEnum TPE = TrackPropEnum ENTRY = namedtuple("ENTRY", ["label", "src", "dest"]) CONVERSIONS = [ ENTRY("Index", TCCE.INDEX, None), ENTRY("Tracksite", TCCE.TRACKSITE, TPE.SITE), ENTRY("Level", TCCE.LEVEL, TPE.LEVEL), ENTRY("Trackway", TCCE.TRACKWAY, None), ENTRY("Excavation Area", TCCE.SECTOR, TPE.SECTOR), ENTRY("E", TCCE.ENTRY_AZIMUTH, ""), ENTRY("S", TCCE.EXIT_AZIMUTH, ""),