Skip to content

Commit

Permalink
Merge pull request #21 from plone/python3
Browse files Browse the repository at this point in the history
Fix tests and functionality in python 3
  • Loading branch information
pbauer authored Sep 15, 2018
2 parents ade7b1a + d667792 commit 7217815
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 108 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ New features:

Bug fixes:

- *add item here*
- Fix functionality and tests in py3
[pbauer]


2.1.1 (2018-02-02)
Expand Down
114 changes: 65 additions & 49 deletions plone/resourceeditor/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from plone.resource.file import FilesystemFile
from plone.resource.interfaces import IResourceDirectory
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import safe_unicode
from Products.Five.browser.decode import processInputs
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from six.moves import urllib
Expand Down Expand Up @@ -85,8 +86,8 @@ def getFolder(self, path):
to getFolder(). This can be used for example to only show image files
in a file system tree.
"""

path = path.encode('utf-8')
if six.PY2 and isinstance(path, six.text_type):
path = path.encode('utf-8')

folders = []
files = []
Expand All @@ -108,7 +109,9 @@ def getFolder(self, path):
return folders + files

def getFile(self, path):
path = self.normalizePath(path.encode('utf-8'))
if six.PY2:
path = path.encode('utf-8')
path = self.normalizePath(path)
ext = self.getExtension(path=path)
result = {'ext': ext}
self.request.response.setHeader('Content-Type', 'application/json')
Expand All @@ -124,7 +127,10 @@ def getFile(self, path):
try:
data = data.read()

result['contents'] = str(data)
if six.PY2 and isinstance(data, six.text_type):
result['contents'] = data.encode('utf8')
else:
result['contents'] = safe_unicode(data)
try:
return json.dumps(result)
except UnicodeDecodeError:
Expand Down Expand Up @@ -212,8 +218,13 @@ def getInfo(self, obj, path='/'):
}

def saveFile(self, path, value):
path = path.lstrip('/').encode('utf-8')
path = path.lstrip('/')
if six.PY2:
path = path.encode('utf-8')
value = six.text_type(value.strip(), 'utf-8')
if six.PY2:
value = value.encode('utf-8')

value = value.replace('\r\n', '\n')

if path in self.context:
Expand Down Expand Up @@ -251,9 +262,9 @@ def saveFile(self, path, value):
def addFolder(self, path, name):
"""Create a new directory on the server within the given path.
"""

path = path.encode('utf-8')
name = name.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
name = name.encode('utf-8')

code = 0
error = ''
Expand Down Expand Up @@ -303,8 +314,9 @@ def addFolder(self, path, name):
def addFile(self, path, name):
"""Add a new empty file in the given directory
"""
path = path.encode('utf-8')
name = name.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
name = name.encode('utf-8')

error = ''
code = 0
Expand All @@ -331,7 +343,7 @@ def addFile(self, path, name):
context=self.request)
code = 1
else:
self.resourceDirectory.writeFile(newPath, '')
self.resourceDirectory.writeFile(newPath, b'')

self.request.response.setHeader('Content-Type', 'application/json')
return json.dumps({
Expand All @@ -345,8 +357,8 @@ def addFile(self, path, name):
def delete(self, path):
"""Delete the item at the given path.
"""

path = path.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')

npath = self.normalizePath(path)
parentPath = '/'.join(npath.split('/')[:-1])
Expand Down Expand Up @@ -381,8 +393,9 @@ def renameFile(self, path, newName):
"""Rename the item at the given path to the new name
"""

path = path.encode('utf-8')
newName = newName.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
newName = newName.encode('utf-8')

npath = self.normalizePath(path)
oldPath = newPath = '/'.join(npath.split('/')[:-1])
Expand Down Expand Up @@ -424,9 +437,9 @@ def renameFile(self, path, newName):
def move(self, path, directory):
"""Move the item at the given path to a new directory
"""

path = path.encode('utf-8')
directory = directory.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
directory = directory.encode('utf-8')

npath = self.normalizePath(path)
newParentPath = self.normalizePath(directory)
Expand Down Expand Up @@ -546,8 +559,8 @@ def getDirectory(folder, relpath=''):
def download(self, path):
"""Serve the requested file to the user
"""

path = path.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')

npath = self.normalizePath(path)
parentPath = '/'.join(npath.split('/')[:-1])
Expand Down Expand Up @@ -759,8 +772,8 @@ def getFolder(self, path, getSizes=False):
to getFolder(). This can be used for example to only show image files
in a file system tree.
"""

path = path.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')

folders = []
files = []
Expand All @@ -784,8 +797,8 @@ def getInfo(self, path, getSize=False):
indicates whether the dimensions of the file (if an image) should be
returned.
"""

path = path.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')

path = self.normalizePath(path)
obj = self.getObject(path)
Expand Down Expand Up @@ -862,9 +875,9 @@ def getInfo(self, path, getSize=False):
def addFolder(self, path, name):
"""Create a new directory on the server within the given path.
"""

path = path.encode('utf-8')
name = name.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
name = name.encode('utf-8')

code = 0
error = ''
Expand Down Expand Up @@ -921,8 +934,9 @@ def add(self, path, newfile, replacepath=None):
URL, so URL-encoded at a minimum.
"""

path = path.encode('utf-8')
if replacepath is not None:
if six.PY2:
path = path.encode('utf-8')
if six.PY2 and replacepath is not None:
replacepath = replacepath.encode('utf-8')

parentPath = self.normalizePath(path)
Expand All @@ -931,7 +945,7 @@ def add(self, path, newfile, replacepath=None):
code = 0

name = newfile.filename
if isinstance(name, six.text_type):
if six.PY2 and isinstance(name, six.text_type):
name = name.encode('utf-8')

if replacepath:
Expand Down Expand Up @@ -973,9 +987,9 @@ def add(self, path, newfile, replacepath=None):
def addNew(self, path, name):
"""Add a new empty file in the given directory
"""

path = path.encode('utf-8')
name = name.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
name = name.encode('utf-8')

error = ''
code = 0
Expand All @@ -1002,7 +1016,7 @@ def addNew(self, path, name):
context=self.request)
code = 1
else:
self.resourceDirectory.writeFile(newPath, '')
self.resourceDirectory.writeFile(newPath, b'')

return {
'parent': self.normalizeReturnPath(parentPath),
Expand All @@ -1014,9 +1028,9 @@ def addNew(self, path, name):
def rename(self, path, newName):
"""Rename the item at the given path to the new name
"""

path = path.encode('utf-8')
newName = newName.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
newName = newName.encode('utf-8')

npath = self.normalizePath(path)
oldPath = newPath = '/'.join(npath.split('/')[:-1])
Expand Down Expand Up @@ -1055,8 +1069,8 @@ def rename(self, path, newName):
def delete(self, path):
"""Delete the item at the given path.
"""

path = path.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')

npath = self.normalizePath(path)
parentPath = '/'.join(npath.split('/')[:-1])
Expand Down Expand Up @@ -1089,9 +1103,9 @@ def delete(self, path):
def move(self, path, directory):
"""Move the item at the given path to a new directory
"""

path = path.encode('utf-8')
directory = directory.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
directory = directory.encode('utf-8')

npath = self.normalizePath(path)
newParentPath = self.normalizePath(directory)
Expand Down Expand Up @@ -1153,8 +1167,8 @@ def move(self, path, directory):
def download(self, path):
"""Serve the requested file to the user
"""

path = path.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')

npath = self.normalizePath(path)
parentPath = '/'.join(npath.split('/')[:-1])
Expand Down Expand Up @@ -1198,8 +1212,8 @@ def getExtension(self, path, obj):
# Methods that are their own views
def getFile(self, path):
self.setup()

path = path.encode('utf-8')
if six.PY2:
path = path.encode('utf-8')

path = self.normalizePath(path)
file = self.context.context.unrestrictedTraverse(path)
Expand All @@ -1219,9 +1233,11 @@ def saveFile(self, path, value):

path = self.request.form.get('path', path)
value = self.request.form.get('value', value)

path = path.lstrip('/').encode('utf-8')
value = value.replace('\r\n', '\n').encode('utf-8')
if six.PY2:
path = path.encode('utf-8')
value = value.encode('utf-8')
path = path.lstrip('/')
value = value.replace('\r\n', '\n')
self.context.writeFile(path, value)
return ' ' # Zope no likey empty responses

Expand Down
Loading

1 comment on commit 7217815

@jenkins-plone-org
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pbauer Jenkins CI reporting about code analysis
See the full report here: https://jenkins.plone.org/job/package-plone.resourceeditor/21/violations

plone/resourceeditor/testing.py:17:41: C812 missing trailing comma
plone/resourceeditor/browser.py:102:56: C812 missing trailing comma
plone/resourceeditor/browser.py:107:55: C812 missing trailing comma
plone/resourceeditor/browser.py:203:71: C812 missing trailing comma
plone/resourceeditor/browser.py:217:32: C812 missing trailing comma
plone/resourceeditor/browser.py:300:60: C812 missing trailing comma
plone/resourceeditor/browser.py:302:45: C812 missing trailing comma
plone/resourceeditor/browser.py:354:25: C812 missing trailing comma
plone/resourceeditor/browser.py:420:60: C812 missing trailing comma
plone/resourceeditor/browser.py:501:5: C901 'FileManagerActions.do_action' is too complex (14)
plone/resourceeditor/browser.py:518:49: C812 missing trailing comma
plone/resourceeditor/browser.py:575:54: C812 missing trailing comma
plone/resourceeditor/browser.py:578:11: T000 Todo note found.
plone/resourceeditor/browser.py:606:27: C812 missing trailing comma
plone/resourceeditor/browser.py:617:25: C812 missing trailing comma
plone/resourceeditor/browser.py:618:14: C812 missing trailing comma
plone/resourceeditor/browser.py:621:5: C901 'FileManager.mode_selector' is too complex (12)
plone/resourceeditor/browser.py:634:65: C812 missing trailing comma
plone/resourceeditor/browser.py:639:63: C812 missing trailing comma
plone/resourceeditor/browser.py:644:56: C812 missing trailing comma
plone/resourceeditor/browser.py:651:58: C812 missing trailing comma
plone/resourceeditor/browser.py:656:56: C812 missing trailing comma
plone/resourceeditor/browser.py:661:58: C812 missing trailing comma
plone/resourceeditor/browser.py:665:56: C812 missing trailing comma
plone/resourceeditor/browser.py:670:66: C812 missing trailing comma
plone/resourceeditor/browser.py:674:56: C812 missing trailing comma
plone/resourceeditor/browser.py:719:44: C812 missing trailing comma
plone/resourceeditor/browser.py:825:75: C812 missing trailing comma
plone/resourceeditor/browser.py:835:29: C812 missing trailing comma
plone/resourceeditor/browser.py:841:33: C812 missing trailing comma
plone/resourceeditor/browser.py:852:25: C812 missing trailing comma
plone/resourceeditor/browser.py:858:29: C812 missing trailing comma
plone/resourceeditor/browser.py:913:60: C812 missing trailing comma
plone/resourceeditor/browser.py:915:45: C812 missing trailing comma
plone/resourceeditor/browser.py:1183:54: C812 missing trailing comma
plone/resourceeditor/browser.py:1186:11: T000 Todo note found.
plone/resourceeditor/browser.py:1256:41: C812 missing trailing comma
plone/resourceeditor/browser.py:1269:48: C812 missing trailing comma
plone/resourceeditor/tests/test_file_manager.py:4:1: F401 'six' imported but unused
plone/resourceeditor/tests/test_file_manager_action.py:3:1: I003 isort expected 1 blank line in imports, found 0

Follow these instructions to reproduce it locally.

Please sign in to comment.