def testSetValue(self): """ This method tests :meth:`foundations.environment.Environment.setValue` method. """ environment = Environment() self.assertTrue(environment.setValue("JANE", "DOE")) self.assertIn("JANE", os.environ) self.assertEqual(environment.getValue("JANE"), "DOE")
def testGetValue(self): """ This method tests :meth:`foundations.environment.Environment.getValue` method. """ if platform.system() == "Windows" or platform.system() == "Microsoft": environment = Environment("APPDATA") elif platform.system() == "Darwin": environment = Environment("HOME") elif platform.system() == "Linux": environment = Environment("HOME") self.assertTrue(environment.getValue()) self.assertIsInstance(environment.getValue(), str) environment.setValues(JOHN="DOE") self.assertEqual(environment.getValue("JOHN"), "DOE") self.assertFalse( environment.getValue( "JOHNDOE_IS_FOR_SURE_A_NON_EXISTING_SYSTEM_ENVIRONMENT_VARIABLE" ))
def testSetValues(self): """ This method tests :meth:`foundations.environment.Environment.setValues` method. """ environment = Environment() self.assertTrue(environment.setValues(JOHN="DOE")) self.assertIn("JOHN", os.environ) self.assertTrue(environment.setValues(JOHN="EOD", DOE="JOHN")) self.assertIn("DOE", os.environ) self.assertEqual(environment.getValues()["JOHN"], "EOD")
def getProcessCommand(self, directory, customBrowser=None): """ Gets process command. :param directory: Directory to explore. :type directory: unicode :param customBrowser: Custom browser. :type customBrowser: unicode :return: Process command. :rtype: unicode """ processCommand = None directory = os.path.normpath(directory) if platform.system() == "Windows" or platform.system() == "Microsoft": if customBrowser: processCommand = "\"{0}\" \"{1}\"".format(customBrowser, directory) else: processCommand = "explorer.exe \"{0}\"".format(directory) elif platform.system() == "Darwin": if customBrowser: processCommand = "open -a \"{0}\" \"{1}\"".format(customBrowser, directory) else: processCommand = "open \"{0}\"".format(directory) elif platform.system() == "Linux": if customBrowser: processCommand = "\"{0}\" \"{1}\"".format(customBrowser, directory) else: environmentVariable = Environment("PATH") paths = environmentVariable.getValue().split(":") browserFound = False for browser in self.__linuxBrowsers: if browserFound: break try: for path in paths: if foundations.common.pathExists(os.path.join(path, browser)): processCommand = "\"{0}\" \"{1}\"".format(browser, directory) browserFound = True raise StopIteration except StopIteration: pass if not browserFound: raise Exception("{0} | Exception raised: No suitable Linux browser found!".format( self.__class__.__name__)) return processCommand
def exploreDirectory(self, directory): """ Provides directory exploring capability. :param directory: Folder to explore. :type directory: str :return: Method success. :rtype: bool """ browserCommand = None directory = os.path.normpath(directory) if platform.system() == "Windows" or platform.system() == "Microsoft": LOGGER.info("{0} | Launching 'explorer.exe' with '{1}'.".format(self.__class__.__name__, directory)) browserCommand = "explorer.exe \"{0}\"".format(directory) elif platform.system() == "Darwin": LOGGER.info("{0} | Launching 'Finder' with '{1}'.".format(self.__class__.__name__, directory)) browserCommand = "open \"{0}\"".format(directory) elif platform.system() == "Linux": environmentVariable = Environment("PATH") paths = environmentVariable.getValue().split(":") browserFound = False for browser in self.__linuxBrowsers: if not browserFound: try: for path in paths: if os.path.exists(os.path.join(path, browser)): LOGGER.info("{0} | Launching '{1}' file browser with '{1}'.".format(self.__class__.__name__, browser, directory)) browserCommand = "\"{0}\" \"{1}\"".format(browser, directory) browserFound = True raise StopIteration except StopIteration: pass else: break if browserCommand: LOGGER.debug("> Current browser command: '{0}'.".format(browserCommand)) browserProcess = QProcess() browserProcess.startDetached(browserCommand) return True
def editFile(self, file): """ Provides editing capability. :param file: File to edit. :type file: str :return: Method success. :rtype: bool """ editCommand = None file = os.path.normpath(file) if platform.system() == "Windows" or platform.system() == "Microsoft": LOGGER.info("{0} | Launching 'notepad.exe' with '{1}'.".format(self.__class__.__name__, file)) editCommand = "notepad.exe \"{0}\"".format(file) elif platform.system() == "Darwin": LOGGER.info("{0} | Launching default text editor with '{1}'.".format(self.__class__.__name__, file)) editCommand = "open -e \"{0}\"".format(file) elif platform.system() == "Linux": environmentVariable = Environment("PATH") paths = environmentVariable.getValue().split(":") editorFound = False for editor in self.__linuxTextEditors: if not editorFound: try: for path in paths: if os.path.exists(os.path.join(path, editor)): LOGGER.info("{0} | Launching '{1}' text editor with '{2}'.".format(self.__class__.__name__, editor, file)) editCommand = "\"{0}\" \"{1}\"".format(editor, file) editorFound = True raise StopIteration except StopIteration: pass else: break if editCommand: LOGGER.debug("> Current edit command: '{0}'.".format(editCommand)) editProcess = QProcess() editProcess.startDetached(editCommand) return True
def testGetValues(self): """ This method tests :meth:`foundations.environment.Environment.getValues` method. """ if platform.system() == "Windows" or platform.system() == "Microsoft": variable = "APPDATA" elif platform.system() == "Darwin": variable = "HOME" elif platform.system() == "Linux": variable = "HOME" environment = Environment(variable) self.assertIsInstance(environment.getValues(), dict) self.assertIsInstance(environment.getValues(variable), dict) self.assertIsInstance(environment.getValues().get(variable), str) self.assertEqual(environment.getValues()[variable], os.environ[variable]) environment.getValues( "JOHNDOE_IS_FOR_SURE_A_NON_EXISTING_SYSTEM_ENVIRONMENT_VARIABLE") self.assertFalse( environment.getValues() ["JOHNDOE_IS_FOR_SURE_A_NON_EXISTING_SYSTEM_ENVIRONMENT_VARIABLE"])