Skip to content

Commit

Permalink
extracting metadata correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
MattReimer committed Sep 3, 2021
1 parent 2b70ce6 commit 9d1c19b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
38 changes: 31 additions & 7 deletions src/classes/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,23 @@ def _load_project(self):
if os.path.isfile(self.project_xml_path):
self.project = lxml.etree.parse(self.project_xml_path).getroot()

self.meta = {meta.attrib['name']: meta.text for meta in self.project.findall('MetaData/Meta')}
self.warehouse_meta = {meta.attrib['name']: meta.text for meta in self.project.findall('Warehouse/Meta')}
self.meta = self.extract_meta(self.project.findall('MetaData/Meta'))
self.warehouse_meta = self.extract_meta(self.project.findall('Warehouse/Meta'))
self.project_type = self.project.find('ProjectType').text

realizations = self.project.find('Realizations')
if realizations is None:
raise Exception('Could not find the <Realizations> node. Are you sure the xml file you opened is Riverscapes Project? File: {}'.format(self.project_xml_path))

def extract_meta(self, nodelist):
meta = {}
for meta_node in nodelist:
key = meta_node.attrib['name']
value = meta_node.text
type = meta_node.attrib['type'] if 'type' in meta_node.attrib else None
meta[key] = (value, type)
return meta

def _load_businesslogic(self):
if self.project is None or self.project_type is None:
return
Expand Down Expand Up @@ -104,6 +117,11 @@ def _load_businesslogic(self):
else:
raise Exception('Could not find a valid business logic file. Valid paths are: [ {} ]'.format(','.join(hierarchy)))

# Check for a different kind of file
root_node = self.business_logic.find('Node')
if root_node is None:
raise Exception('Could not find the root <Node> element. Are you sure the xml file you opened is Riverscapes Business logic XML? File: {}'.format(self.business_logic_path))

def _build_tree(self, force=False):
"""
Parse the XML and return any basemaps you find
Expand All @@ -115,8 +133,11 @@ def _build_tree(self, force=False):
return

# Parse the XML
self.qproject = self._recurse_tree()
self._build_views()
if self.business_logic is None:
self.settings.log("No business logic file for this project could be found.")
else:
self.qproject = self._recurse_tree()
self._build_views()

def _build_views(self):
if self.business_logic is None or self.qproject is None:
Expand Down Expand Up @@ -153,11 +174,14 @@ def _build_views(self):

def _recurse_tree(self, bl_el=None, proj_el=None, parent: QStandardItem = None):
settings = Settings()
if self.business_logic is None:
return

if bl_el is None:
bl_el = self.business_logic.find('Node')

if bl_el is None:
self.settings.log('No default businesslogic root node could be located in file: {}'.format(self.business_logic_path), Qgis.Critical)
return

is_root = proj_el is None
bl_attr = bl_el.attrib

Expand Down Expand Up @@ -237,7 +261,7 @@ def _recurse_tree(self, bl_el=None, proj_el=None, parent: QStandardItem = None):
curr_item.setIcon(QIcon(':/plugins/qrave_toolbar/RaveAddIn_16px.png'))

# Couldn't find this node. Ignore it.
meta = {meta.attrib['name']: meta.text for meta in new_proj_el.findall('MetaData/Meta')}
meta = self.extract_meta(new_proj_el.findall('MetaData/Meta'))
new_proj_el.find('Path')

layer_name = None
Expand Down
4 changes: 2 additions & 2 deletions src/dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ def get_warehouse_url(self, wh_meta: Dict[str, str]):
if wh_meta is not None:

if 'program' in wh_meta and 'id' in wh_meta:
return '/'.join([CONSTANTS['warehouseUrl'], wh_meta['program'], wh_meta['id']])
return '/'.join([CONSTANTS['warehouseUrl'], wh_meta['program'][0], wh_meta['id'][0]])

elif '_rs_wh_id' in wh_meta and '_rs_wh_program' in wh_meta:
return '/'.join([CONSTANTS['warehouseUrl'], wh_meta['_rs_wh_program'], wh_meta['_rs_wh_id']])
return '/'.join([CONSTANTS['warehouseUrl'], wh_meta['_rs_wh_program'][0], wh_meta['_rs_wh_id'][0]])

return None

Expand Down
58 changes: 39 additions & 19 deletions src/meta_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

from qgis.PyQt import uic
from qgis.core import Qgis
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem, QIcon, QDesktopServices, QGuiApplication
from qgis.PyQt.QtWidgets import QDockWidget, QWidget, QTreeView, QVBoxLayout, QMenu, QAction
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem, QDesktopServices, QGuiApplication, QBrush
from qgis.PyQt.QtWidgets import QDockWidget, QMenu, QMessageBox
from qgis.PyQt.QtCore import pyqtSignal, pyqtSlot, Qt, QModelIndex, QUrl

from .classes.settings import Settings, CONSTANTS
Expand Down Expand Up @@ -66,21 +66,15 @@ def load(self, label: str, meta_type: str, meta: dict, show: bool = False):
proj_meta_font.setBold(True)
proj_meta.setFont(proj_meta_font)
for k, v in meta['project'].items():
proj_meta.appendRow([
QStandardItem(k),
QStandardItem(v)
])
self.appendMetaItem(proj_meta, k, v[0], v[1])
root_item.appendRow(proj_meta)
if 'warehouse' in meta and len(meta['warehouse'].keys()) > 0:
wh_meta = QStandardItem('Warehouse Meta')
wh_meta_font = proj_meta.font()
wh_meta_font.setBold(True)
wh_meta.setFont(wh_meta_font)
for k, v in meta['warehouse'].items():
wh_meta.appendRow([
QStandardItem(k),
QStandardItem(v)
])
self.appendMetaItem(wh_meta, k, v[0], v[1])
root_item.appendRow(wh_meta)

elif meta_type == MetaType.FOLDER:
Expand All @@ -102,10 +96,7 @@ def load(self, label: str, meta_type: str, meta: dict, show: bool = False):
self.treeView.setHeaderHidden(False)
if meta is not None and len(meta.keys()) > 0:
for k, v in meta.items():
root_item.appendRow([
QStandardItem(k),
QStandardItem(v)
])
self.appendMetaItem(root_item, k, v[0], v[1])
else:
self.treeView.setHeaderHidden(True)
self.treeView.setEnabled(False)
Expand Down Expand Up @@ -138,12 +129,36 @@ def load(self, label: str, meta_type: str, meta: dict, show: bool = False):
if show is True:
self.show()

def appendMetaItem(self, root_item: QStandardItem, key: str, value: str, meta_type=None):
val_item = QStandardItem(value)
val_item.setData(meta_type, Qt.UserRole)

# Getting ready for custom meta types
if meta_type == 'url' or meta_type == 'image' or meta_type == 'video' and value.startswith('http'):
val_item.setData(QBrush(Qt.blue), Qt.ForegroundRole)
# val_item.setUnderlineStyle(QTextCharFormat.SingleUnderline)

root_item.appendRow([
QStandardItem(key),
val_item
])

def closeEvent(self, event):
self.hide()

def default_tree_action(self, index):
item = self.model.itemFromIndex(index)
data = item.data(Qt.UserRole)
meta_type = item.data(Qt.UserRole)
text = item.text()

if meta_type is not None and text is not None:
if meta_type == 'url' or meta_type == 'image' or meta_type == 'video' and text.startswith('http'):
qm = QMessageBox
qm.question(self, '', "Visit in browser?", qm.Yes | qm.No)
if qm.Yes:
QDesktopServices.openUrl(QUrl(text))
else:
self.copy(text)

def open_menu(self, position):

Expand All @@ -158,16 +173,21 @@ def open_menu(self, position):
self.menu.clear()
if item_val is not None:
row_text = {item_name.text(): item_val.text()}
self.menu.addAction('Copy Name to Clipboard', lambda: self.copy(item_name.text()))
self.menu.addAction('Copy Value to Clipboard', lambda: self.copy(item_val.text()))
self.menu.addAction('Copy Row to Clipboard (json)', lambda: self.copy(
meta_type = item_val.data(Qt.UserRole)
if meta_type == 'url' or meta_type == 'image' or meta_type == 'video' and item_val.text().startswith('http'):
self.menu.addAction('Visit URL in Browser', lambda: QDesktopServices.openUrl(QUrl(item_val.text())))
self.menu.addSeparator()
self.menu.addAction('Copy name', lambda: self.copy(item_name.text()))
self.menu.addAction('Copy value', lambda: self.copy(item_val.text()))
self.menu.addAction('Copy row (json)', lambda: self.copy(
json.dumps(row_text, indent=4, sort_keys=True)
))
self.menu.addAction('Copy All to Clipboard (json)', lambda: self.copy(json.dumps(self.meta, indent=4, sort_keys=True)))
self.menu.addAction('Copy all rows (json)', lambda: self.copy(json.dumps(self.meta, indent=4, sort_keys=True)))

self.menu.exec_(self.treeView.viewport().mapToGlobal(position))

def copy(self, data: str):
self.settings.msg_bar('Item Copied to clipboard:', data, Qgis.Success)
cb = QGuiApplication.clipboard()
cb.clear(mode=cb.Clipboard)
cb.setText(data, mode=cb.Clipboard)

0 comments on commit 9d1c19b

Please sign in to comment.