From ee289dc39e78277fcc5e0c52925eeb6f1ff54c49 Mon Sep 17 00:00:00 2001 From: Gabor Guzmics Date: Wed, 25 Jun 2014 17:10:49 +0200 Subject: [PATCH] upgrading code to use dejaqt upgraded dejaqt to use folders implemented correct folder matching rearranged pagereply to be a resourcereply (and falling back on it) --- archive/__init__.py | 0 {gui => archive}/localbrowser.py | 0 dejaqt/folders.py | 27 +++--- dejaqt/qweb.py | 91 ++++++++++--------- dj/scon/templates/scon/base.html | 91 +++++++++++++++---- dj/scon/templates/scon/crafting/overview.html | 66 +++++++------- gui/viewer.py | 7 +- 7 files changed, 172 insertions(+), 110 deletions(-) create mode 100644 archive/__init__.py rename {gui => archive}/localbrowser.py (100%) diff --git a/archive/__init__.py b/archive/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gui/localbrowser.py b/archive/localbrowser.py similarity index 100% rename from gui/localbrowser.py rename to archive/localbrowser.py diff --git a/dejaqt/folders.py b/dejaqt/folders.py index f5f89cf..bbc5d7f 100644 --- a/dejaqt/folders.py +++ b/dejaqt/folders.py @@ -13,7 +13,9 @@ class FolderLibrary(object): if settings: self.folders.update( getattr(settings, 'DEJAQT_DIRS', {}) ) except: - logging.error('DEJAQT_DIRS in django settings is corrupt.') + logging.error('DEJAQT_DIRS in django settings threw error.') + import traceback + traceback.print_exc() if folders: # no try here: if this fails, you got yourself a programming error. self.folders.update(folders) @@ -47,24 +49,27 @@ class FolderLibrary(object): def matched_folder(self, url): m = self.match(url) if m is not None: - real_folder = self._folders[m] - print m - print url - print url[len(m):] - print os.path.split(real_folder) - print os.path.split(url) - return real_folder + folder = self._folders[m] + #heading, rest = url[:len(m)], url[len(m):] + rest = url[len(m):] + real_folder = os.path.abspath( os.path.join(folder, rest) ) + if real_folder.startswith(os.path.abspath(folder)): + return real_folder + else: + logging.error('%s does not seem to be a subpath of %s' % (real_folder, folder)) def print_folders(self): print '{' for k in self._keys: - print "'%s': '%s'" % (k, self._folders[k]) + print "'%s': '%s'," % (k, self._folders[k]) print '}' if __name__ == "__main__": # test this: - f = FolderLibrary({'abc/dab/': 'c:/dab', + import os + os.environ['DJANGO_SETTINGS_MODULE'] = 'scon.dj.settings' + f = FolderLibrary({'abc/dab/': 'c:/media', 'abc': 'd:/abc', 'abc/dab/tmp': '/tmp', 'uiuiui': 'x:/', @@ -75,5 +80,5 @@ if __name__ == "__main__": f.add_folder('abc/dub/', 'c:/dubdub') f.print_folders() - print f.matched_folder('abc/dab/okokok/hurnkint.pdf') + print f.matched_folder('abc/dab/okokok/some.png') \ No newline at end of file diff --git a/dejaqt/qweb.py b/dejaqt/qweb.py index f7eda9d..df00913 100644 --- a/dejaqt/qweb.py +++ b/dejaqt/qweb.py @@ -4,6 +4,7 @@ import os, logging from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork from django.test import Client +from folders import FolderLibrary class DebugPage(QtWebKit.QWebPage): def sayMyName(self): @@ -11,17 +12,17 @@ class DebugPage(QtWebKit.QWebPage): class DejaWebView(QtWebKit.QWebView): ''' - BaseDir. Get rid of it? + Optional: + * folders: FolderLibrary() Instance. + * page: Initialized QWebPage instance for initial page (default DebugPage()) ''' def __init__(self, *args, **kwargs): - basedir = kwargs.pop('basedir', None) + self.folders = kwargs.pop('folders', FolderLibrary()) + page = kwargs.pop('page', DebugPage()) QtWebKit.QWebView.__init__(self, *args, **kwargs) - oldManager = self.page().networkAccessManager() - self.setPage(DebugPage()) - self.page().setNetworkAccessManager(DejaNetworkAccessManager(self, basedir)) - - def set_basedir(self, basedir): - self.page().setNetworkAccessManager(DejaNetworkAccessManager(self, basedir)) + #self.oldManager = self.page().networkAccessManager() + self.setPage(page) + self.page().setNetworkAccessManager(DejaNetworkAccessManager(self)) class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager): ''' @@ -39,14 +40,11 @@ class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager): Note2: not sure if cookies and sessions will work this way! ''' USE_NETWORK = False - def __init__(self, parent=None, basedir=None): + def __init__(self, parent=None): QtNetwork.QNetworkAccessManager.__init__(self, parent=None) - if not basedir: - # take current dir as basedir. - self.basedir = os.path.dirname(os.path.abspath(__file__)) - else: - self.basedir = basedir - + if parent: + self.folders = getattr(parent, 'folders', FolderLibrary()) + def createRequest(self, operation, request, data): scheme = request.url().scheme() if scheme != 'page' and scheme != 'res': @@ -61,13 +59,13 @@ class DejaNetworkAccessManager(QtNetwork.QNetworkAccessManager): #print reply return reply elif operation == self.PostOperation: - #print data.readAll() + print data.readAll() #print request reply = PageReply(self, request.url(), self.PostOperation) return reply elif scheme == 'res': if operation == self.GetOperation: - return ImageReply(self, request.url(), self.GetOperation, self.basedir) + return ResourceReply(self, request.url(), self.GetOperation) else: if self.USE_NETWORK: return QtNetwork.QNetworkAccessManager.createRequest(self, operation, request, data) @@ -113,7 +111,36 @@ class BasePageReply(QtNetwork.QNetworkReply): self.offset = end return data -class PageReply(BasePageReply): +class ResourceReply(BasePageReply): + content_type = 'image/png' + + def determine_content_type(self, path): + return self.content_type + + def initialize_content(self, url, operation): + # determine folder: + path = unicode(url.path()).lstrip('/') + folders = getattr(self.parent(), 'folders') + if folders: + path = folders.matched_folder(path) + if path: + if os.path.exists(path): + try: + f = open(path, 'rb') + return f.read() + finally: + f.close() + else: + logging.warning('Path does not exist: %s' % path) + else: + logging.error('Containing Folder not found for %s' % path) + else: + logging.error('Configuration Error: No Folders found.') + return '' + +class PageReply(ResourceReply): + content_type = 'text/html' + def initialize_content(self, url, operation): c = Client() print "Response for %s, method %s" % (url.path(), operation) @@ -121,9 +148,12 @@ class PageReply(BasePageReply): response = c.get(unicode(url.path()), ) elif operation == DejaNetworkAccessManager.PostOperation: response = c.post(unicode(url.path())) + self.content_type = response.get('Content-Type', self.content_type) # response code - print "Response Status: %s" % response.status_code + #print "Response Status: %s" % response.status_code # note: on a 404, we might need to trigger file response. + if response.status_code == 404: + return ResourceReply.initialize_content(self, url, DejaNetworkAccessManager.GetOperation) return response.content class NoNetworkReply(BasePageReply): @@ -137,26 +167,3 @@ class NoNetworkReply(BasePageReply): ''' -class ResourceReply(BasePageReply): - content_type = 'image/png' - - def __init__(self, parent, url, operation, basedir): - self.basedir = basedir - BasePageReply.__init__(self, parent, url, operation) - - def determine_content_type(self, path): - return self.content_type - - def initialize_content(self, url, operation): - path = os.path.join(self.basedir, unicode(url.path()).lstrip('/')) - if not os.path.exists(path): - logging.error('Image does not exist: %s' % path) - return '' - #h = url.host() - # @todo: host checks? - try: - f = open(path, 'rb') - return f.read() - finally: - f.close() - \ No newline at end of file diff --git a/dj/scon/templates/scon/base.html b/dj/scon/templates/scon/base.html index aa58ab0..114a8fc 100644 --- a/dj/scon/templates/scon/base.html +++ b/dj/scon/templates/scon/base.html @@ -2,7 +2,31 @@ {% block css %} diff --git a/dj/scon/templates/scon/crafting/overview.html b/dj/scon/templates/scon/crafting/overview.html index 63c9065..37f8a0b 100644 --- a/dj/scon/templates/scon/crafting/overview.html +++ b/dj/scon/templates/scon/crafting/overview.html @@ -3,43 +3,40 @@ {% block context %}

Crafting Overview

- - - - - - - - - - + {% for item in items %} {% if item.primary_recipee %} - - - {% with item.primary_recipee as recipee %} - - - + {% endwith %} - + +   {% endif %} {% endfor %} - -
Source Crafts intoAlso used in
+
+ +
{% if item.icon %}{% endif %} {{ item.name }} {% if item.sell_price %}
Sell: {{item.sell_price}} cr{% endif %}
-
-
{% if recipee.amount > 1 %}{{ recipee.amount }}{% endif %}
-
-
- {% if recipee.output.icon %}{% endif %} {{ recipee.output.html }} - {% if recipee.output.sell_price %}
Sell: {{recipee.output.sell_price}} cr{% endif %} -
-
-
    - {% for ingredient in recipee.ingredients %} -
  • {{ ingredient.amount }} x {{ ingredient.item.html }}
  • - {% endfor %} -
-
-
+ + + {% with item.primary_recipee as recipee %} +
  +
{% if recipee.amount > 1 %}{{ recipee.amount }}{% endif %}
+
+ +
+
+
+ {% if recipee.output.icon %}{% endif %} {{ recipee.output.html }} + {% if recipee.output.sell_price %}
Sell: {{recipee.output.sell_price}} cr{% endif %} +
+
+
    + {% for ingredient in recipee.ingredients %} +
  • {{ ingredient.amount }} x {{ ingredient.item.html }}
  • + {% endfor %} +
+
+
+
+ +
    {% for i1 in item.crafting_used_in %} {% with i1.crafting.output as ci %} @@ -52,11 +49,10 @@ {% endwith %} {% endfor %}
-
{% endblock context %} \ No newline at end of file diff --git a/gui/viewer.py b/gui/viewer.py index 7b7e302..5aaa9d9 100644 --- a/gui/viewer.py +++ b/gui/viewer.py @@ -11,8 +11,9 @@ os.environ['DJANGO_SETTINGS_MODULE'] = 'scon.dj.settings' #setup_environ(settings) import sys from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork +from scon.dejaqt.folders import FolderLibrary +from scon.dejaqt.qweb import DejaWebView from treeview import TreeViewModel, Node -from localbrowser import LocalWebView class MenuTree(QtGui.QTreeView): def __init__(self, *args, **kwargs): @@ -64,7 +65,9 @@ class Browser(QtGui.QMainWindow): self.gridLayout.addLayout(self.horizontalMainLayout) # #self.menu = MenuTree() - self.html = LocalWebView(basedir='D:/work/workspace/scon/src/scon/dj/scon/media/') + self.html = DejaWebView(folders=FolderLibrary({'': + 'D:/work/workspace/scon/src/scon/dj/scon/media/'}) + ) #self.horizontalMainLayout.addWidget(self.menu) self.horizontalMainLayout.addWidget(self.html) self.mainLayout.addWidget(self.frame)