Commit 9cd33ca4 authored by Julien Muchembled's avatar Julien Muchembled

Simplify code by using some new Python syntax

- PEP 308: Conditional Expressions
- PEP 341: Unified try/except/finally
- PEP 343: The ‘with’ statement
parent 48212534
......@@ -78,7 +78,6 @@ class ActivityBuffer(TM):
def _finish(self):
# LOG('ActivityBuffer', 0, '_finish %r' % (self,))
try:
try:
# Try to push / delete all messages
for activity, message in self.flushed_activity:
......
......@@ -884,9 +884,7 @@ class ActivityTool (Folder, UniqueObject):
in zope.conf. The Default is every 5 seconds.
"""
# Prevent TimerService from starting multiple threads in parallel
acquired = timerservice_lock.acquire(0)
if not acquired:
return
if timerservice_lock.acquire(0):
try:
# make sure our skin is set-up. On CMF 1.5 it's setup by acquisition,
# but on 2.2 it's by traversal, and our site probably wasn't traversed
......@@ -895,7 +893,6 @@ class ActivityTool (Folder, UniqueObject):
# both setupCurrentSkin and REQUEST are acquired from containers.
self.setupCurrentSkin(self.REQUEST)
old_sm = getSecurityManager()
try:
try:
# get owner of portal_catalog, so normally we should be able to
# have the permission to invoke all activities
......@@ -907,7 +904,7 @@ class ActivityTool (Folder, UniqueObject):
processing_node_list = self.getNodeList(role=ROLE_PROCESSING)
# only distribute when we are the distributingNode
if (self.getDistributingNode() == currentNode):
if self.getDistributingNode() == currentNode:
self.distribute(len(processing_node_list))
# SkinsTool uses a REQUEST cache to store skin objects, as
......@@ -920,13 +917,14 @@ class ActivityTool (Folder, UniqueObject):
pass
# call tic for the current processing_node
# the processing_node numbers are the indices of the elements in the node tuple +1
# because processing_node starts form 1
# the processing_node numbers are the indices of the elements
# in the node tuple +1 because processing_node starts form 1
if currentNode in processing_node_list:
self.tic(processing_node_list.index(currentNode)+1)
self.tic(processing_node_list.index(currentNode) + 1)
except:
# Catch ALL exception to avoid killing timerserver.
LOG('ActivityTool', ERROR, 'process_timer received an exception', error=sys.exc_info())
LOG('ActivityTool', ERROR, 'process_timer received an exception',
error=sys.exc_info())
finally:
setSecurityManager(old_sm)
finally:
......@@ -984,8 +982,7 @@ class ActivityTool (Folder, UniqueObject):
while has_awake_activity:
has_awake_activity = 0
for activity in activity_list:
acquired = is_running_lock.acquire(0)
if acquired:
if is_running_lock.acquire(0):
try:
activity.tic(inner_self, processing_node) # Transaction processing is the responsability of the activity
has_awake_activity = has_awake_activity or activity.isAwake(inner_self, processing_node)
......
......@@ -192,7 +192,7 @@ class TradeModelPath(Path):
# * remove categories which base name is not category
# * respect base parameter
prefix = category + '/'
start_index = not base and len(prefix) or 0
start_index = 0 if base else len(prefix)
return [category[start_index:]
for category in category_list
if category.startswith(prefix)]
......
......@@ -179,11 +179,9 @@ def fixSkinNames(self, REQUEST=None, file=None, dry_run=0):
msg += 'This does not modify anything by default. If you really want to fix skin names, specify %s/ERP5Site_fixSkinNames?file=NAME&dry_run=0 \n\n' % self.absolute_url()
return msg
file = os.path.join(data_dir, file)
file = open(file, 'r')
with open(os.path.join(data_dir, file)) as file:
class NamingInformation: pass
info_list = []
try:
reader = csv.reader(file)
for row in reader:
folder, name, new_name, meta_type = row[:4]
......@@ -205,8 +203,6 @@ def fixSkinNames(self, REQUEST=None, file=None, dry_run=0):
info.regexp = re.compile('\\b' + re.escape(name) + '\\b') # This is used to search the name
info.removed = removed
info_list.append(info)
finally:
file.close()
# Now we have information enough. Check the skins.
msg = ''
......@@ -232,11 +228,8 @@ def fixSkinNames(self, REQUEST=None, file=None, dry_run=0):
msg += '%s\n' % line
if not dry_run:
if skin.meta_type in fs_skin_spec:
f = open(expandpath(skin.getObjectFSPath()), 'w')
try:
with open(expandpath(skin.getObjectFSPath()), 'w') as f:
f.write(text)
finally:
f.close()
else:
REQUEST['BODY'] = text
skin.manage_FTPput(REQUEST, REQUEST.RESPONSE)
......
......@@ -206,8 +206,7 @@ class AlarmTool(BaseTool):
This method is called by TimerService in the interval given
in zope.conf. The Default is every 5 seconds.
"""
acquired = last_tic_lock.acquire(0)
if not acquired:
if not last_tic_lock.acquire(0):
return
try:
# make sure our skin is set-up. On CMF 1.5 it's setup by acquisition,
......
......@@ -141,7 +141,9 @@ class IntrospectionTool(LogMixin, BaseTool):
if compressed:
tmp_file_path = tempfile.mktemp(dir=tmp_file_path)
tmp_file = tarfile.open(tmp_file_path,"w:gz")
try:
tmp_file.add(file_path)
finally:
tmp_file.close()
RESPONSE.setHeader('Content-type', 'application/x-tar')
RESPONSE.setHeader('Content-Disposition', \
......@@ -154,13 +156,10 @@ class IntrospectionTool(LogMixin, BaseTool):
tmp_file_path = file_path
f = open(tmp_file_path)
try:
with open(tmp_file_path) as f:
RESPONSE.setHeader('Content-Length', os.stat(tmp_file_path).st_size)
for data in f:
RESPONSE.write(data)
finally:
f.close()
if compressed:
os.remove(tmp_file_path)
......@@ -190,30 +189,21 @@ class IntrospectionTool(LogMixin, BaseTool):
char_per_line = 75
tailed_file = open(log_file,'r')
with open(log_file,'r') as tailed_file:
while 1:
try:
tailed_file.seek(-1 * char_per_line * line_number, 2)
except IOError:
tailed_file.seek(0)
if tailed_file.tell() == 0:
at_start = 1
else:
at_start = 0
pos = tailed_file.tell()
lines = tailed_file.read().split("\n")
if (len(lines) > (line_number + 1)) or at_start:
if len(lines) > (line_number + 1) or not pos:
break
# The lines are bigger than we thought
char_per_line = char_per_line * 1.3 # Inc for retry
tailed_file.close()
if len(lines) > line_number:
start = len(lines) - line_number - 1
else:
start = 0
char_per_line *= 1.3 # Inc for retry
start = max(len(lines) - line_number - 1, 0)
return "\n".join(lines[start:len(lines)])
security.declareProtected(Permissions.ManagePortal, 'tailEventLog')
......@@ -330,19 +320,16 @@ class IntrospectionTool(LogMixin, BaseTool):
"""
def cached_getSystemVersionDict():
import pkg_resources
def tuple_to_format_str(t):
return '.'.join([str(i) for i in t])
version_dict = {}
for dist in pkg_resources.working_set:
version_dict[dist.key] = dist.version
from Products import ERP5 as erp5_product
erp5_product_path = erp5_product.__file__.split("/")[:-1]
erp5_product_path = os.path.dirname(erp5_product.__file__)
try:
erp5_v = open("/".join((erp5_product_path) + ["VERSION.txt"])).read().strip()
erp5_version = erp5_v.replace("ERP5 ", "")
except:
with open(os.path.join(erp5_product_path, "VERSION.txt")) as f:
erp5_version = f.read().strip().replace("ERP5 ", "")
except Exception:
erp5_version = None
version_dict["ProductS.ERP5"] = erp5_version
......
......@@ -298,13 +298,10 @@ class TemplateTool (BaseTool):
"""
Import template from a temp file (as uploaded by the user)
"""
file = open(path, 'rb')
try:
with open(path, 'rb') as file:
# read magic key to determine wich kind of bt we use
file.seek(0)
magic = file.read(5)
finally:
file.close()
if magic == '<?xml': # old version
self._importObjectFromFile(path, id=id)
......@@ -342,11 +339,8 @@ class TemplateTool (BaseTool):
prop_dict.pop('id', '')
bt.edit(**prop_dict)
# import all other files from bt
fobj = open(path, 'rb')
try:
with open(path, 'rb') as fobj:
bt.importFile(file=fobj)
finally:
fobj.close()
finally:
tar.close()
return bt
......@@ -398,7 +392,8 @@ class TemplateTool (BaseTool):
if not os.path.exists(prop_path):
value = None
else:
value = open(prop_path, 'rb').read()
with open(prop_path, 'rb') as f:
value = f.read()
if value is 'None':
# At export time, we used to export non-existent properties:
# str(obj.getProperty('non-existing')) == 'None'
......@@ -523,11 +518,8 @@ class TemplateTool (BaseTool):
tempid, temppath = mkstemp()
try:
os.close(tempid) # Close the opened fd as soon as possible
tempfile = open(temppath, 'wb')
try:
with open(temppath, 'wb') as tempfile:
tempfile.write(import_file.read())
finally:
tempfile.close()
bt = self._importBT(temppath, id)
finally:
os.remove(temppath)
......
......@@ -427,13 +427,8 @@ class TestERP5Catalog(ERP5TypeTestCase, LogInterceptor):
#if uid_buffer_key in uid_buffer_dict:
# del uid_buffer_dict[uid_buffer_key]
def getUIDBuffer(*args, **kw):
uid_lock = catalog.__class__._reserved_uid_lock
uid_lock.acquire()
try:
result = catalog.getUIDBuffer(*args, **kw)
finally:
uid_lock.release()
return result
with catalog.__class__._reserved_uid_lock:
return catalog.getUIDBuffer(*args, **kw)
getUIDBuffer(force_new_buffer=True)
......
......@@ -2343,8 +2343,8 @@ class ListBoxHTMLRendererLine(ListBoxRendererLine):
url_column_dict = dict(renderer.getUrlColumnList())
selection = renderer.getSelection()
selection_name = renderer.getSelectionName()
ignore_layout = int(request.get('ignore_layout', \
not request.get('is_web_mode', False) and 1 or 0))
ignore_layout = int(request.get('ignore_layout',
0 if request.get('is_web_mode') else 1))
editable_mode = int(request.get('editable_mode', 0))
ui_domain = 'erp5_ui'
# We need a way to pass the current line object (ie. brain) to the
......
......@@ -976,33 +976,25 @@ class %s(Constraint):
# Create an empty __init__.py.
init = os.path.join(path, '__init__.py')
if not os.path.exists(init):
f = open(init, 'w')
f.close()
open(init, 'w').close()
# For convenience, make .cvsignore.
if generate_cvsignore:
cvsignore = os.path.join(path, '.cvsignore')
if not os.path.exists(cvsignore):
f = open(cvsignore, 'w')
try:
with open(cvsignore, 'w') as f:
f.write('*.pyc' + os.linesep)
finally:
f.close()
# Create a Permissions module for this Product.
permissions = os.path.join(base_path, 'Permissions.py')
if not os.path.exists(permissions):
f = open(permissions, 'w')
f.close()
open(permissions, 'w').close()
# Make .cvsignore for convenience.
if generate_cvsignore:
cvsignore = os.path.join(base_path, '.cvsignore')
if not os.path.exists(cvsignore):
f = open(cvsignore, 'w')
try:
with open(cvsignore, 'w') as f:
f.write('*.pyc' + os.linesep)
finally:
f.close()
# Create an init file for this Product.
init = os.path.join(base_path, '__init__.py')
......@@ -1055,11 +1047,8 @@ def initialize( context ):
content_constructors = (),
content_classes = ())
''' % COPYRIGHT
f = open(init, 'w')
try:
with open(init, 'w') as f:
f.write(text)
finally:
f.close()
# Create a skeleton README.txt.
readme = os.path.join(base_path, 'README.txt')
......@@ -1069,11 +1058,8 @@ def initialize( context ):
%s was automatically generated by ERP5 Class Tool.
''' % (product_id, product_id)
f = open(readme, 'w')
try:
with open(readme, 'w') as f:
f.write(text)
finally:
f.close()
# Now, copy selected code.
for d, m, id_list in (('Document', readLocalDocument, document_id_list),
......@@ -1084,11 +1070,8 @@ def initialize( context ):
for class_id in id_list:
path = os.path.join(base_path, d, class_id) + '.py'
text = m(class_id)
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
if REQUEST is not None:
REQUEST.RESPONSE.redirect('%s/manage_viewProductGeneration?manage_tabs_message=New+Product+Saved+In+%s' % (self.absolute_url(), base_path))
......
......@@ -536,12 +536,8 @@ def readLocalPropertySheet(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "PropertySheet")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def writeLocalPropertySheet(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -554,11 +550,8 @@ def writeLocalPropertySheet(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
# load the file, so that an error is raised if file is invalid
module = imp.load_source(class_id, path)
getattr(module, class_id)
......@@ -570,8 +563,7 @@ def importLocalPropertySheet(class_id, path = None):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "PropertySheet")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
klass = None
try:
......@@ -582,8 +574,6 @@ def importLocalPropertySheet(class_id, path = None):
setattr(PropertySheet, class_id, klass)
# Register base categories
registerBaseCategories(klass)
finally:
f.close()
base_category_dict = {}
def registerBaseCategories(property_sheet):
......@@ -608,11 +598,8 @@ def importLocalInterface(module_id, path = None, is_erp5_type=False):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "interfaces")
path = os.path.join(path, "%s.py" % module_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
finally:
f.close()
from zope.interface import Interface
from Products.ERP5Type import interfaces
InterfaceClass = type(Interface)
......@@ -627,12 +614,9 @@ def importLocalConstraint(class_id, path = None):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Constraint")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
setattr(Products.ERP5Type.Constraint, class_id, getattr(module, class_id))
finally:
f.close()
def importLocalInteractor(class_id, path=None):
import Products.ERP5Type.Interactor
......@@ -640,13 +624,10 @@ def importLocalInteractor(class_id, path=None):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Interactor")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
with open(path) as f:
module = imp.load_source(class_id, path, f)
setattr(Products.ERP5Type.Interactor, class_id, getattr(module, class_id))
registerInteractorClass(class_id, getattr(Products.ERP5Type.Interactor, class_id))
finally:
f.close()
def getLocalExtensionList():
if not getConfiguration:
......@@ -699,12 +680,8 @@ def readLocalExtension(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Extensions")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def removeLocalTest(class_id):
instance_home = getConfiguration().instancehome
......@@ -718,23 +695,15 @@ def readLocalTest(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "tests")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def readLocalConstraint(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Constraint")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def writeLocalExtension(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -747,11 +716,8 @@ def writeLocalExtension(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
def writeLocalTest(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -764,11 +730,8 @@ def writeLocalTest(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
def writeLocalConstraint(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -781,11 +744,8 @@ def writeLocalConstraint(class_id, text, create=1, instance_home=None):
if create:
if os.path.exists(path):
raise IOError, 'the file %s is already present' % path
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
# load the file, so that an error is raised if file is invalid
module = imp.load_source(class_id, path)
getattr(module, class_id)
......@@ -829,12 +789,8 @@ def readLocalDocument(class_id):
instance_home = getConfiguration().instancehome
path = os.path.join(instance_home, "Document")
path = os.path.join(path, "%s.py" % class_id)
f = open(path)
try:
text = f.read()
finally:
f.close()
return text
with open(path) as f:
return f.read()
def writeLocalDocument(class_id, text, create=1, instance_home=None):
if instance_home is None:
......@@ -849,11 +805,8 @@ def writeLocalDocument(class_id, text, create=1, instance_home=None):
raise IOError, 'the file %s is already present' % path
# check there is no syntax error (that's the most we can do at this time)
compile(text, path, 'exec')
f = open(path, 'w')
try:
with open(path, 'w') as f:
f.write(text)
finally:
f.close()
def setDefaultClassProperties(property_holder):
"""Initialize default properties for ERP5Type Documents.
......
......@@ -285,7 +285,6 @@ class PortalTypeMetaClass(GhostBaseMetaClass, PropertyHolder):
from Products.ERP5.ERP5Site import getSite
site = getSite()
ERP5Base.aq_method_lock.acquire()
try:
try:
try:
class_definition = generatePortalTypeClass(site, portal_type)
......
......@@ -27,8 +27,7 @@ if '__getstate__' not in Uninstalled.BrokenClass.__dict__:
cache = Uninstalled.broken_klasses
def Broken(self, oid, pair):
lock.acquire()
try:
with lock:
cached = pair in cache
result = Uninstalled_Broken(self, oid, pair)
if not cached:
......@@ -36,8 +35,6 @@ if '__getstate__' not in Uninstalled.BrokenClass.__dict__:
assert not issubclass(klass, PersistentBroken), \
"This monkey-patch is not useful anymore"
cache[pair] = persistentBroken(klass)
finally:
lock.release()
return result
Uninstalled.Broken = Broken
......@@ -15,14 +15,11 @@ class DummyTaskDistributionTool(object):
return None, revision
def startUnitTest(self, test_result_path, exclude_list=()):
self.lock.acquire()
try:
with self.lock:
for i, test in enumerate(self.test_name_list):
if test not in exclude_list:
del self.test_name_list[i]
return None, test
finally:
self.lock.release()
def stopUnitTest(self, test_path, status_dict):
pass
\ No newline at end of file
......@@ -143,12 +143,8 @@ class Browser:
def _createFile(self, filename, content):
file_path = os.path.join(self.profile_dir, filename)
f = open(file_path, 'w')
try:
with open(file_path, 'w') as f:
f.write(content)
finally:
f.close()
return file_path
def _setDisplay(self, display):
......
......@@ -289,9 +289,7 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
raise WriteError
portal.person_module.__class__._setLastId = _setLastId
try:
try:
o = portal.person_module.newContent(portal_type="Person",
temp_object=1)
o = portal.person_module.newContent(portal_type="Person", temp_object=1)
except WriteError:
self.fail("Container last ID modified")
finally:
......
......@@ -323,7 +323,7 @@ class Git(WorkingCopy):
if push:
# if we can't push because we are not up-to-date, we'll either 'merge' or
# 'rebase' depending on we already have local commits or not
merge = self.getAheadCount() and 'merge' or 'rebase'
merge = 'merge' if self.getAheadCount() else 'rebase'
selected_set = set(added)
selected_set.update(modified)
......
......@@ -48,16 +48,13 @@ _chdir_lock = threading.RLock()
@simple_decorator
def chdir_working_copy(func):
def decorator(self, *args, **kw):
_chdir_lock.acquire()
try:
with _chdir_lock:
cwd = os.getcwd()
try:
os.chdir(self.working_copy)
return func(self, *args, **kw)
finally:
os.chdir(cwd)
finally:
_chdir_lock.release()
return decorator
class Subversion(WorkingCopy):
......
......@@ -209,11 +209,8 @@ class WorkingCopy(Implicit):
revision = int(self.showOld(path)) + 1
except NotVersionedError:
return 1
file = open(os.path.join(self.working_copy, path), 'w')
try:
with open(os.path.join(self.working_copy, path), 'w') as file:
file.write(str(revision))
finally:
file.close()
return revision
def hasDiff(self, path):
......@@ -279,11 +276,8 @@ class WorkingCopy(Implicit):
head = '<span style="font-weight: bold; color: black;">%s</span>' \
% real_path
try:
f = open(os.path.join(self.working_copy, path), 'rU')
try:
with open(os.path.join(self.working_copy, path), 'rU') as f:
text = f.read()
finally:
f.close()
except IOError, e:
if e.errno == errno.EISDIR:
return '%s<hr/>%r is a folder!' % (head, path)
......
......@@ -214,16 +214,14 @@ class EGOVUserManager(ERP5UserManager):
sm = getSecurityManager()
if sm.getUser().getId() != SUPER_USER:
newSecurityManager(self, self.getUser(SUPER_USER))
try:
try:
result = portal.portal_catalog.unrestrictedSearchResults(
select_expression='reference',
portal_type=self.portal_type_list, reference=login)
if len(result) != 1: # we won't proceed with groups
if len(result) > 1: # configuration is screwed
raise ConsistencyError, 'There is more than one Person whose \
login is %s : %s' % (user_name,
raise ConsistencyError('There is more than one Person whose'
' login is %s : %s' % (user_name,
repr([r.getObject() for r in catalog_result]))
else: # no person is linked to this user login
# this permit to get the module of the application
......
......@@ -697,8 +697,7 @@ class Catalog(Folder,
"""
Import properties from an XML file.
"""
f = open(file)
try:
with open(file) as f:
doc = parse(f)
root = doc.documentElement
try:
......@@ -747,8 +746,6 @@ class Catalog(Folder,
self.filter_dict[id]['expression_instance'] = None
finally:
doc.unlink()
finally:
f.close()
def manage_historyCompare(self, rev1, rev2, REQUEST,
historyComparisonResults=''):
......
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