Commit 39c905d0 authored by Cage, Marshall Andrew's avatar Cage, Marshall Andrew
Browse files

Add package update functionality

- Fix behaviour of preference window to not store changes when
  cancel is pressed
parent c6587da2
......@@ -19,6 +19,10 @@ class PreferenceManager(QDialog):
PreferenceManager.ui.buttonBox.accepted.connect(
PreferenceManager.storeForm
)
PreferenceManager.ui.buttonBox.rejected.connect(
lambda: (PreferenceManager.loadPreferences(),
PreferenceManager.updateForm() )
)
PreferenceManager.ui.treeWidget.currentItemChanged.connect(
PreferenceManager.updateForm
)
......@@ -36,14 +40,15 @@ class PreferenceManager(QDialog):
twi.addChild(child)
PreferenceManager.ui.treeWidget.addTopLevelItem(twi)
PreferenceManager.loadPreferences()
PreferenceManager.ui.treeWidget.sortItems(0,0)
PreferenceManager.ui.treeWidget.setCurrentItem(
PreferenceManager.ui.treeWidget.itemAt(0,0)
)
PreferenceManager.__init = True
PreferenceManager.loadPreferences()
PreferenceManager.ui.treeWidget.sortItems(0,0)
PreferenceManager.ui.treeWidget.setCurrentItem(
PreferenceManager.ui.treeWidget.itemAt(0,0)
)
PreferenceManager.updateForm()
@staticmethod
def getPreference(package,name,sub=''):
if (package not in PreferenceManager.preferences or
......@@ -73,7 +78,7 @@ class PreferenceManager(QDialog):
[item.text(0)]
else:
package = PreferenceManager.preferences[item.text(0)]['']
# PreferenceManager.storeForm(False)
PreferenceManager.ui.formWidget.setParent(None)
PreferenceManager.ui.horizontalLayout.removeWidget(
PreferenceManager.ui.formWidget
......@@ -90,44 +95,51 @@ class PreferenceManager(QDialog):
w = QLineEdit()
# print(params)
w.setText(
params['value'] if ('value' in params and params['value'])\
else params['default'] if 'default' in params \
params['value'] if ('value' in params and params['value'])
else params['default'] if 'default' in params
else ''
)
w.setPlaceholderText(
params['placeholder'] if 'placeholder' in params \
params['placeholder'] if 'placeholder' in params
else ''
)
w.textEdited.connect(
lambda: PreferenceManager.storeForm(False))
elif params['type'] == 'check':
w = QCheckBox()
w.setChecked(
bool(params['value'] if ('value' in params and
params['value']) \
else params['default'] if 'default' in params \
params['value'])
else params['default'] if 'default' in params
else False)
)
w.toggled.connect(lambda: PreferenceManager.storeForm(False))
elif params['type'] == 'number':
w = QSpinBox()
w.setValue(
int(params['value'] if ('value' in params and
params['value']) \
else params['default'] if 'default' in params \
params['value'])
else params['default'] if 'default' in params
else 0)
)
w.valueChanged.connect(
lambda: PreferenceManager.storeForm(False))
elif params['type'] == 'choice':
w = QComboBox()
w.setItems(
[
x for x in (params['placeholder'].split(',') \
x for x in (params['placeholder'].split(',')
if 'placeholder' in params else '--')
]
)
w.setIndex(
int(params['value'] if ('value' in params and
params['value']) \
else params['default'] if 'default' in params \
params['value'])
else params['default'] if 'default' in params
else 0)
)
w.currentIndexChanged.connect(
lambda: PreferenceManager.storeForm(False))
w.setToolTip(params['tooltip'] if 'tooltip' in params else '')
w.setSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.Minimum)
......@@ -141,7 +153,7 @@ class PreferenceManager(QDialog):
PreferenceManager.ui.horizontalLayout.update()
@staticmethod
def storeForm():
def storeForm(save=True):
package = None
item = PreferenceManager.ui.treeWidget.currentItem()
child = True if item.parent() else False
......@@ -152,7 +164,10 @@ class PreferenceManager(QDialog):
else:
package = PreferenceManager.preferences[item.text(0)]['']
# print(package)
for k in sorted(package.keys()):
# print(k)
params = package[k]
if 'widget' in params:
params['value'] = {
......@@ -162,7 +177,8 @@ class PreferenceManager(QDialog):
'choice': lambda: params['widget'].currentText()
}[params['type']]()
# print(package)
PreferenceManager.savePreferences()
if save:
PreferenceManager.savePreferences()
@staticmethod
def savePreferences():
......@@ -181,15 +197,12 @@ class PreferenceManager(QDialog):
str(params['label']),
str(params['type']),
str({
'string': lambda: params['widget'].text(),
'check': lambda: int(
params['widget'].isChecked()
),
'number': lambda: params['widget'].value(),
'choice': lambda: params['widget']\
.currentText()
'string': lambda: params['value'],
'check': lambda: int(params['value']),
'number': lambda: params['value'],
'choice': lambda: params['value']
}[params['type']]())\
if 'widget' in params else '',
if 'value' in params else '',
str(params['default'])\
if 'default' in params else '',
str(params['placeholder'])\
......@@ -234,4 +247,4 @@ class PreferenceManager(QDialog):
if data['placeholder'] != '' else None,
'tooltip': data['tooltip']\
if data['tooltip'] != '' else None
}
}
\ No newline at end of file
def num2word(num):
words = ["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","twen","thir","four","fif","six","seven","eigh","nine"]
words = ["zero","one","two","three","four","five",
"six","seven","eight","nine","ten","eleven","twelve",
"twen","thir","four","fif","six","seven","eigh","nine"]
ab_num = abs(int(num))
val = ''
#more
......@@ -41,16 +43,17 @@ def num2word(num):
val += num2word(ab_num // 100) + " hundred"
ab_num = ab_num % 100
if len(val) > 0:
if len(val) > 0 and ab_num > 0:
val += " and "
#tens
if ab_num < 13:
if ab_num == 0:
val += words[ab_num] if len(val) == 0 else ''
elif ab_num < 13:
val += words[ab_num]
elif ab_num < 20:
#i = ab_num % 10 + (3 if ab_num > 12 else 0)
val += words[ab_num + 1] + "teen"
elif ab_num < 100:
val += words[ab_num//10 + 11] + "ty"
val += ("-" + words[ab_num%10]) if ab_num%10 > 0 else ''
return ("negative" if num < 0 else '') + val
\ No newline at end of file
return ("negative " if num < 0 else '') + val
\ No newline at end of file
......@@ -277,7 +277,7 @@ class ARCTool(QMainWindow):
"Automatic Report Profile (*.arp)"
)[0]
if path is None or path[0] == '':
if path is None or path == '':
return
self.profilePath = path
......@@ -618,11 +618,6 @@ class ARCTool(QMainWindow):
return
# def exportReport(self):
# self.ui.statusBar.showMessage("Exporting Report")
# ARCD.pdfExport(self.document)
# self.ui.statusBar.showMessage("Finished Exporting Report")
def openPackageArchive(self,path=None):
path = path or QFileDialog.getOpenFileName(
self,"Open Profile",None,
......@@ -631,27 +626,36 @@ class ARCTool(QMainWindow):
self.installPackage(path)
def installPackage(self,path):
if path == '':
return
pkgName = os.path.basename(path).split('.',1)[0]
msg = QMessageBox(self)
msg.setWindowTitle('Install Package')
msg.setStandardButtons(QMessageBox.Ok)
msg.setStandardButtons(msg.Ok)
with tarfile.open(path,'r:gz') as archive:
path = os.path.join(self.storagePath, "packages", pkgName)
clean = False
try:
os.mkdir(path)
except FileExistsError:
print("A package by this name already exists")
msg.setText('A package by this name already exists.')
msg.setText('A package by this name already exists. Would you'
' like to update it?')
self.ui.statusBar.showMessage(
"A package by this name already exists.")
msg.exec()
# This should actually update the package instead...
return
msg.setStandardButtons(msg.Yes|msg.No)
r = msg.exec()
if r == msg.Yes:
msg.setStandardButtons(msg.Ok)
self.ui.statusBar.showMessage(
'Updating package "%s"...' %pkgName)
clean = True
else:
return
names = archive.getnames()
if ('__init__.py' not in names):
os.rmdir(path)
msg.setText('The package is malformed.')
self.ui.statusBar.showMessage("The package is malformed.")
......@@ -684,13 +688,25 @@ class ARCTool(QMainWindow):
msg.exec()
return
# Clean the plugin directory
if clean:
for name in os.listdir(path):
try:
os.remove(os.path.join(path,name))
except:
pass
for name in names:
# Check for malicious intent? ;p
if re.match(r'^((\.\.)|\.[/\\]|/|[A-Za-z]+:\\)', name):
print('d',name)
return
print(name)
archive.extract(name,path,numeric_owner=True)
msg.setText('The package was succesfully installed')
self.ui.statusBar.showMessage(
"The package was successfully installed.", 5000)
msg.setText('The package was successfully installed.')
msg.exec()
self.loadPackages()
return
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment