-
Notifications
You must be signed in to change notification settings - Fork 4
/
envmanager.py
109 lines (90 loc) · 3.95 KB
/
envmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
"""
/***************************************************************************
envmanager
A class for managing environment stuff
FSC QGIS plugin for biological recorders
-------------------
begin : 2014-02-17
copyright : (C) 2014 by Rich Burkmar, Field Studies Council
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import os
from shutil import *
class envManager():
def __init__(self):
self.iniFileInternal = os.path.join(os.path.dirname( __file__ ), "iniBioRec.txt")
self.iniFileDefault = os.path.join(os.path.dirname( __file__ ), "iniBioRecDefault.txt")
self.iniExternal = ""
#If the internal iniFileInternal exists, see if it references an external file
if os.path.exists(self.iniFileInternal):
self.iniFile = self.iniFileInternal
self.loadEnvironment()
self.iniExternal = self.getEnvValue("external-env-file")
if os.path.exists(self.iniExternal):
self.iniFile = self.iniExternal
else:
if os.path.exists(self.iniFileDefault):
copyfile(self.iniFileDefault, self.iniFileInternal)
self.iniFile = self.iniFileInternal
self.loadEnvironment()
def loadEnvironment(self):
if os.path.isfile(self.iniFile):
self.textEnv = open(self.iniFile).read()
else:
self.textEnv = "Environment File '" self.iniFile "'not found."
self.textEnv = "#Environment File"
def saveEnvironment(self):
f = open(self.iniFile, 'w')
f.write(self.textEnv)
f.close()
if self.iniFile != self.iniFileInternal:
f = open(self.iniFileInternal, 'w')
f.write("external-env-file: " self.iniFile)
f.close()
def getTextEnv(self):
return self.textEnv
def getTextExample(self):
if os.path.isfile(self.iniFileDefault):
return open(self.iniFileDefault).read()
def setTextEnv(self, textEnv):
self.textEnv = textEnv
def getEnvValue(self, envLabel):
# Returns the first matching value
search = envLabel ": "
lines = self.textEnv.split('\n' )
for line in lines:
if line.startswith(search):
return line[len(search):].strip()
return ""
def getEnvValues(self, envLabel):
# Returns a list of all matching values
ret = []
search = envLabel ": "
lines = self.textEnv.split('\n' )
for line in lines:
if line.startswith(search):
ret.append(line[len(search):].strip())
return ret
def getExternalFilePath(self):
return self.iniExternal
def isValueAssignedToLabel(self, envLabel, envValue):
# Returns True if the value is assigned to the label
pass
def setExternalEnvFile(self, envFile, bBrowse):
self.iniFile = envFile
if bBrowse:
self.loadEnvironment()
else:
self.saveEnvironment()