Commit 8ceee716 authored by Benjamin Jakimow's avatar Benjamin Jakimow
Browse files

refactoring

parent f92b896e
# -*- coding: utf-8 -*-
"""
***************************************************************************
deploy.py
Script to build the HUB-TimeSeriesViewer from Repository code
---------------------
Date : September 2017
Copyright : (C) 2017 by Benjamin Jakimow
Email : benjamin.jakimow@geo.hu-berlin.de
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
import os, sys, re, shutil, zipfile, datetime
import numpy as np
from pb_tool import pb_tool
from timeseriesviewer import DIR_REPO, jp, file_search
import timeseriesviewer
DIR_BUILD = jp(DIR_REPO, 'build')
DIR_DEPLOY = jp(DIR_REPO, 'deploy')
#list of deploy options:
# ZIP - add zipped plugin to DIR_DEPLOY
# UNZIPPED - add the non-zipped plugin to DIR_DEPLOY
DEPLOY_OPTIONS = ['ZIP', 'UNZIPPED']
ADD_TESTDATA = True
#directories below the <enmapbox-repository> folder whose content is to be copied without filtering
PLAIN_COPY_SUBDIRS = ['site-packages']
########## End of config section
timestamp = ''.join(np.datetime64(datetime.datetime.now()).astype(str).split(':')[0:-1])
buildID = '{}.{}'.format(timeseriesviewer.VERSION, timestamp)
dirBuildPlugin = jp(DIR_BUILD, 'timeseriesviewerplugin')
def rm(p):
"""
Remove files or directory 'p'
:param p: path of file or directory to be removed.
"""
if os.path.isfile(p):
os.remove(p)
elif os.path.isdir(p):
shutil.rmtree(p)
def cleanDir(d):
"""
Remove content from directory 'd'
:param d: directory to be cleaned.
"""
assert os.path.isdir(d)
for root, dirs, files in os.walk(d):
for p in dirs + files: rm(jp(root,p))
break
def mkDir(d, delete=False):
"""
Make directory.
:param d: path of directory to be created
:param delete: set on True to delete the directory contents, in case the directory already existed.
"""
if delete and os.path.isdir(d):
cleanDir(d)
if not os.path.isdir(d):
os.makedirs(d)
def patch_pb_tool(DIR_DEPLOY):
#local pb_tool configuration file.
pathCfg = jp(DIR_REPO, 'pb_tool.cfg')
#required to choose andy DIR_DEPLOY of choice
#issue tracker: https://github.com/g-sherman/plugin_build_tool/issues/4
pb_tool.get_plugin_directory = lambda : DIR_DEPLOY
#pb_tool.cli.command = lambda f:f
#Issue 1.: set pb_tool.cfg directly and do not expect current WDir
def config():
import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.read(pathCfg)
return cfg
pb_tool.config = config
#issue 2: do not expect compiled resource files to end on '_rc.py'
def compiled_resource():
return []
import ConfigParser
cfg = config()
try:
res_files = cfg.get('files', 'resource_files').split()
compiled = []
for res in res_files:
(base, ext) = os.path.splitext(res)
#CHANGED!!!! no '_rc.py'
compiled.append('{}.py'.format(base))
# print "Compiled resource files: {}".format(compiled)
return compiled
except ConfigParser.NoSectionError as oops:
print oops.message
sys.exit(1)
pb_tool.compiled_resource = compiled_resource
#Issues:
#def compiled_ui():
# return []
# files = file_search(jp(DIR_REPO,'timeseriesviewer'), '*.ui', recursive=True)
# return files
#pb_tool.compiled_ui = compiled_ui
#Issues:
_deployOld = pb_tool.deploy
def deploy():
#create target directories
plugin_dir = os.path.join(pb_tool.get_plugin_directory(), pb_tool.config().get('plugin', 'name'))
install_files = pb_tool.get_install_files()
for file in install_files:
d = os.path.dirname(jp(plugin_dir,file))
if not os.path.exists(d):
os.makedirs(d)
_deployOld()
pb_tool.deploy = deploy
#Issue: my 'help' dir is called 'doc'
def build_docs():
""" Build the docs using sphinx"""
import subprocess
helpDir = jp(DIR_REPO, 'doc')
#if os.path.exists('help'):
if os.path.exists(helpDir):
if sys.platform == 'win32':
makeprg = 'make.bat'
else:
makeprg = 'make'
cwd = os.getcwd()
os.chdir(helpDir)
subprocess.check_call([makeprg, 'html'])
os.chdir(cwd)
else:
print "No help directory exists in the current directory"
pb_tool.build_docs = build_docs
if __name__ == "__main__":
#the directory to build the "enmapboxplugin" folder
DIR_DEPLOY = jp(DIR_REPO, 'deploy')
mkDir(DIR_DEPLOY)
import pb_tool
# DIR_DEPLOY = r'E:\_EnMAP\temp\temp_bj\enmapbox_deploys\most_recent_version'
patch_pb_tool(DIR_DEPLOY)
pathCfg = jp(DIR_REPO, 'pb_tool.cfg')
cfg = pb_tool.config()
pluginname = cfg.get('plugin', 'name')
if True:
#1. clean an existing directory = the timeseriesviewer folder
pb_tool.clean_deployment(ask_first=False)
#2. Compile. Basically call pyrcc to create the resources.rc file
#I don't know how to call this from pure python
if False:
import subprocess
import make
os.chdir(DIR_REPO)
subprocess.call(['pb_tool', 'compile'])
make.compile_rc_files(DIR_REPO)
else:
pb_tool.compile_files()
#3. Deploy = write the data to the new enmapboxplugin folder
os.chdir(os.path.dirname(pathCfg))
pb_tool.deploy()
#4. As long as we can not specify in the pb_tool.cfg which file types are not to deploy,
# we need to remove them afterwards.
# issue: https://github.com/g-sherman/plugin_build_tool/issues/5
print('Remove files...')
for f in file_search(DIR_DEPLOY, re.compile('(svg|pyc)$'), recursive=True):
os.remove(f)
#5. create a zip
print('Create zipfile...')
from timeseriesviewer.utils import zipdir
pathZip = jp(DIR_DEPLOY, '{}.{}.zip'.format(pluginname,timestamp))
dirPlugin = jp(DIR_DEPLOY, pluginname)
zipdir(dirPlugin, pathZip)
#os.chdir(dirPlugin)
#shutil.make_archive(pathZip, 'zip', '..', dirPlugin)
# 6. copy to local QGIS user DIR
if True:
import shutil
from os.path import expanduser
pathQGIS = os.path.join(expanduser("~"), *['.qgis2', 'python', 'plugins'])
assert os.path.isdir(pathQGIS)
pathDst = os.path.join(pathQGIS, os.path.basename(dirPlugin))
rm(pathDst)
shutil.copytree(dirPlugin, pathDst)
s = ""
print('Finished')
......@@ -12,7 +12,6 @@ from PyQt5.QtCore import *
from timeseriesviewer import *
from timeseriesviewer.utils import *
from timeseriesviewer import file_search
from timeseriesviewer.timeseries import *
from timeseriesviewer.virtualrasters import VRTRasterInputSourceBand, VRTRasterBand, VRTRaster
......
......@@ -23,7 +23,8 @@
import os, sys, re, shutil, zipfile, datetime
from pb_tool import pb_tool
import numpy as np
from timeseriesviewer import DIR_REPO, jp, file_search
from timeseriesviewer import DIR_REPO
from timeseriesviewer.utils import file_search, jp
import timeseriesviewer
DIR_BUILD = jp(DIR_REPO, 'build')
DIR_DEPLOY = jp(DIR_REPO, 'deploy')
......
......@@ -6,15 +6,15 @@ from qgis.core import *
from qgis.gui import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.Qt import QApplication
from PyQt5.QtSvg import *
from PyQt5.QtXml import *
import gdal
from timeseriesviewer import DIR_UI, DIR_REPO, file_search
jp = os.path.join
from timeseriesviewer import DIR_UI, DIR_REPO
from timeseriesviewer.utils import file_search, jp
def createFilePackage(dirData, recursive=True):
......@@ -94,7 +94,7 @@ def createTestData(dirTestData, pathTS, subsetRectangle, crs, drv=None):
lines = open(pathTS).readlines()
import tempfile, random
from timeseriesviewer.main import TimeSeries, TimeSeriesDatum
from qgis.core import QgsRectangle, QgsPoint, QgsPointV2, QgsCoordinateReferenceSystem
from qgis.core import QgsRectangle, QgsPoint, QgsPointXY, QgsCoordinateReferenceSystem
max_offset = 0 #in %
......@@ -246,7 +246,6 @@ def svg2png(pathDir, overwrite=False, mode='INKSCAPE', filterFile=None):
:return:
"""
assert mode in ['INKSCAPE', 'WEBKIT', 'SVG']
from PyQt5.QtWebKit import QWebPage
svgs = file_search(pathDir, '*.svg')
if filterFile is not None:
......@@ -260,6 +259,7 @@ def svg2png(pathDir, overwrite=False, mode='INKSCAPE', filterFile=None):
if len(svgs) == 0:
print('No SVGs to convert')
return
from PyQt5.Qt import QApplication
app = QApplication([], True)
buggySvg = []
......@@ -280,39 +280,41 @@ def svg2png(pathDir, overwrite=False, mode='INKSCAPE', filterFile=None):
if overwrite or not os.path.exists(pathPng):
img.save(pathPng, quality=100)
del painter, renderer
elif mode == 'WEBKIT':
page = QWebPage()
frame = page.mainFrame()
f = QFile(pathSvg)
if f.open(QFile.ReadOnly | QFile.Text):
textStream = QTextStream(f)
svgData = textStream.readAll()
f.close()
qba = QByteArray(str(svgData))
frame.setContent(qba,"image/svg+xml")
page.setViewportSize(frame.contentsSize())
palette = page.palette()
background_color = QColor(50,0,0,50)
palette.setColor(QPalette.Window, background_color)
brush = QBrush(background_color)
palette.setBrush(QPalette.Window, brush)
page.setPalette(palette)
img = QImage(page.viewportSize(), QImage.Format_ARGB32)
img.fill(background_color) #set transparent background
painter = QPainter(img)
painter.setBackgroundMode(Qt.OpaqueMode)
#print(frame.renderTreeDump())
frame.render(painter)
painter.end()
if overwrite or not os.path.exists(pathPng):
print('Save {}...'.format(pathPng))
img.save(pathPng, quality=100)
del painter, frame, img, page
s =""
"""
elif mode == 'WEBKIT':
page = QWebPage()
frame = page.mainFrame()
f = QFile(pathSvg)
if f.open(QFile.ReadOnly | QFile.Text):
textStream = QTextStream(f)
svgData = textStream.readAll()
f.close()
qba = QByteArray(str(svgData))
frame.setContent(qba,"image/svg+xml")
page.setViewportSize(frame.contentsSize())
palette = page.palette()
background_color = QColor(50,0,0,50)
palette.setColor(QPalette.Window, background_color)
brush = QBrush(background_color)
palette.setBrush(QPalette.Window, brush)
page.setPalette(palette)
img = QImage(page.viewportSize(), QImage.Format_ARGB32)
img.fill(background_color) #set transparent background
painter = QPainter(img)
painter.setBackgroundMode(Qt.OpaqueMode)
#print(frame.renderTreeDump())
frame.render(painter)
painter.end()
if overwrite or not os.path.exists(pathPng):
print('Save {}...'.format(pathPng))
img.save(pathPng, quality=100)
del painter, frame, img, page
s =""
"""
elif mode == 'INKSCAPE':
if fileNeedsUpdate(pathSvg, pathPng):
if sys.platform == 'darwin':
......@@ -432,45 +434,6 @@ def file2qrc(icondir, pathQrc, qrcPrefix='timeseriesviewer', fileExtension='.png
def updateInfoHTML():
import markdown, urllib
import timeseriesviewer
from timeseriesviewer import DIR_REPO, PATH_LICENSE, PATH_CHANGELOG
"""
Keyword arguments:
* input: a file name or readable object.
* output: a file name or writable object.
* encoding: Encoding of input and output.
* Any arguments accepted by the Markdown class.
"""
markdownExtension = [
'markdown.extensions.toc',
'markdown.extensions.tables',
'markdown.extensions.extra'
]
def readUrlTxt(url):
req = urllib.urlopen(url)
enc = req.headers['content-type'].split('charset=')[-1]
txt = req.read()
req.close()
if enc == 'text/plain':
return unicode(txt)
return unicode(txt, enc)
paths = [jp(DIR_REPO, *['LICENSE.md']),
jp(DIR_REPO, *['CHANGES.md'])]
for pathSrc in paths:
pathDst = pathSrc.replace('.md','.html')
markdown.markdownFromFile(input=pathSrc,
extensions=markdownExtension,
output=pathDst, output_format='html5')
def updateMetadataTxt():
#see http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/plugins.html#plugin-metadata
#for required metatags
......
......@@ -17,7 +17,7 @@ import os, sys, pickle
from timeseriesviewer.utils import initQgisApplication
import example.Images
from timeseriesviewer import file_search
from timeseriesviewer.utils import file_search
QGIS_APP = initQgisApplication()
......@@ -31,7 +31,8 @@ class PixelLoaderTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
from timeseriesviewer import file_search, DIR_EXAMPLES
from timeseriesviewer import DIR_EXAMPLES
from timeseriesviewer.utils import file_search
cls.imgs = file_search(DIR_EXAMPLES, '*.tif', recursive=True)
cls.img1 = cls.imgs[0]
ds = gdal.Open(cls.img1)
......
......@@ -15,8 +15,7 @@ __copyright__ = 'Copyright 2017, Benjamin Jakimow'
import unittest
from qgis import *
from PyQt5.QtGui import QIcon
from timeseriesviewer import file_search
from timeseriesviewer.utils import initQgisApplication
from timeseriesviewer.utils import initQgisApplication, file_search
QGIS_APP = initQgisApplication()
......
......@@ -17,7 +17,6 @@ from qgis import *
from qgis.gui import *
from PyQt5.QtGui import QIcon
import example.Images
from timeseriesviewer import file_search
from timeseriesviewer.timeseries import TimeSeries, TimeSeriesDatum
from timeseriesviewer.temporalprofiles2d import TemporalProfile, saveTemporalProfiles
from timeseriesviewer.utils import *
......
......@@ -6,7 +6,7 @@ import unittest
import example
import example.Images
from osgeo import gdal
from timeseriesviewer import file_search
from timeseriesviewer.utils import file_search
from timeseriesviewer.timeseries import TimeSeries, SensorInstrument, TimeSeriesDatum
class TestInit(unittest.TestCase):
......
......@@ -14,10 +14,9 @@ __copyright__ = 'Copyright 2017, Benjamin Jakimow'
import unittest
from qgis import *
from qgis.core import QgsProject
from qgis.gui import *
from PyQt5.QtGui import QIcon
from example.Images import Img_2014_04_21_LC82270652014111LGN00_BOA
from timeseriesviewer import file_search
from timeseriesviewer.utils import *
QGIS_APP = initQgisApplication()
......
......@@ -107,10 +107,6 @@ def icon():
path = os.path.join(os.path.dirname(__file__), 'icon.png')
return QIcon(path)
import timeseriesviewer.utils
file_search = timeseriesviewer.utils.file_search
def getFileAndAttributes(file):
"""
splits a GDAL valid file path into
......
......@@ -45,7 +45,7 @@ if os.path.exists(path):
import qgis.utils
from timeseriesviewer.utils import *
from timeseriesviewer import jp, mkdir, DIR_SITE_PACKAGES, file_search, messageLog
from timeseriesviewer import jp, mkdir, DIR_SITE_PACKAGES, messageLog
from timeseriesviewer.timeseries import *
from timeseriesviewer.profilevisualization import SpectralTemporalVisualization
import numpy as np
......
......@@ -624,7 +624,7 @@ if __name__ == '__main__':
DEBUG = False
import example.Images
from timeseriesviewer import file_search
from timeseriesviewer.utils import file_search
dir = os.path.dirname(example.Images.__file__)
#files = file_search(dir, '*.tif')
files = [example.Images.Img_2014_05_07_LC82270652014127LGN00_BOA]
......
......@@ -2161,7 +2161,7 @@ if __name__ == '__main__':
STVis.setTimeSeries(TS)
import example.Images
from timeseriesviewer import file_search
from timeseriesviewer.utils import file_search
files = file_search(os.path.dirname(example.Images.__file__), '*.tif')
TS.addFiles(files)
ext = TS.getMaxSpatialExtent()
......
......@@ -30,7 +30,6 @@ from qgis.PyQt.QtCore import *
from timeseriesviewer import *
from timeseriesviewer.utils import *
from timeseriesviewer import file_search
from timeseriesviewer.timeseries import *
class SandboxObjects(object):
......
......@@ -26,14 +26,14 @@ import os, re, io
from unittest import TestCase
from timeseriesviewer import *
from timeseriesviewer.timeseries import *
from timeseriesviewer import DIR_EXAMPLES
from timeseriesviewer.utils import file_search
class TestObjects():
@staticmethod
def timeSeries():
TS = TimeSeries()
from timeseriesviewer import DIR_EXAMPLES, file_search
files = file_search(DIR_EXAMPLES, '*.bsq', recursive=True)
TS.addFiles(files)
return TS
......@@ -44,7 +44,7 @@ class TestObjects():
Returns n random spectral profiles from the test data
:return: lost of (N,3) array of floats specifying point locations.
"""
from timeseriesviewer import DIR_EXAMPLES, file_search
files = file_search(DIR_EXAMPLES, '*.tif', recursive=True)
results = []
import random
......
......@@ -24,8 +24,15 @@ import os, sys, math, re, io, fnmatch
from collections import defaultdict
from qgis.core import *
from qgis.gui import *
#from qgis.core import *
from qgis.core import QgsPointXY, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsApplication, QgsRectangle, \
QgsMapLayer, QgsRasterLayer, QgsVectorLayer, QgsRasterRenderer, QgsFeatureRenderer, QgsRasterDataProvider, QgsUnitTypes, QgsSingleBandPseudoColorRenderer
#from qgis.gui import *
from qgis.gui import QgsMapCanvas, QgisInterface
import qgis.utils
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
......
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