Commit 68c546d5 authored by Renato Bellotti's avatar Renato Bellotti
Browse files

Code reformatting for better compliance with PEP8.

parent 69761a5e
import hashlib import hashlib
"""
Simple path name generator that ensures that actual path lengths do not exceed
the UNIX 255 chars.
Directory names that are longer than 255 chars will be shortened to a sha
hash. The mapping can later be writte to stdout or file to have access to the
original filename.
"""
class PathNameGenerator: class PathNameGenerator:
"""
Ensures that actual path lengths do not exceed the UNIX 255 chars.
def __init__(self): Directory names that are longer than 255 chars will be shortened to a sha
hash. The mapping can later be written to stdout or file to have access to
the original filename.
"""
def __init__(self):
self.mapping = {} self.mapping = {}
self.max_path_length = 160 self.max_path_length = 160
def __str__(self): def __str__(self):
mapping = "" mapping = ""
for hash_value, dir_name in self.mapping.items(): for hash_value, dir_name in self.mapping.items():
mapping += hash_value + " => " + dir_name + "\n" mapping += hash_value + " => " + dir_name + "\n"
return mapping return mapping
def compress(self, path_name): def compress(self, path_name):
if len(path_name) < self.max_path_length: if len(path_name) < self.max_path_length:
return path_name return path_name
...@@ -37,5 +32,3 @@ class PathNameGenerator: ...@@ -37,5 +32,3 @@ class PathNameGenerator:
self.mapping[path_name_hex] = path_name self.mapping[path_name_hex] = path_name
return path_name_hex return path_name_hex
...@@ -9,6 +9,8 @@ OpalDictionary class ...@@ -9,6 +9,8 @@ OpalDictionary class
@author: Yves Ineichen @author: Yves Ineichen
@version: 0.1 @version: 0.1
""" """
class OpalDict: class OpalDict:
def __init__(self, template): def __init__(self, template):
...@@ -25,7 +27,7 @@ class OpalDict: ...@@ -25,7 +27,7 @@ class OpalDict:
def __setitem__(self, key, value): def __setitem__(self, key, value):
scalevars = {} scalevars = {}
scalevars['GUNSOLB'] = 1.0 scalevars['GUNSOLB'] = 1.0
try: try:
self.dict[key] = value * scalevars[key] self.dict[key] = value * scalevars[key]
...@@ -39,13 +41,13 @@ class OpalDict: ...@@ -39,13 +41,13 @@ class OpalDict:
return self.dict.items() return self.dict.items()
def fillDictionary(self, fileName): def fillDictionary(self, fileName):
fp = open(fileName,"r") fp = open(fileName, "r")
for line in fp: for line in fp:
if not line == "\n": if not line == "\n":
li = line.strip() li = line.strip()
if not li.startswith("#"): if not li.startswith("#"):
aline = line.split("#")[0] aline = line.split("#")[0]
name,val = aline.split() name, val = aline.split()
self.dict[name.rstrip()] = val.lstrip().rstrip() self.dict[name.rstrip()] = val.lstrip().rstrip()
fp.close() fp.close()
...@@ -69,7 +71,7 @@ class OpalDict: ...@@ -69,7 +71,7 @@ class OpalDict:
#if self.dict.has_key(var): #if self.dict.has_key(var):
self.dict[var] = float(self.dict[var])*scaleWith self.dict[var] = float(self.dict[var])*scaleWith
def getType(self,s): def getType(self, s):
try: try:
return int(s) return int(s)
except ValueError: except ValueError:
...@@ -87,15 +89,15 @@ class OpalDict: ...@@ -87,15 +89,15 @@ class OpalDict:
def addUserValues(self, argv): def addUserValues(self, argv):
for arg in argv: for arg in argv:
if arg.find("=") > 0: if arg.find("=") > 0:
data = str(arg.split(" ")) # arguments are separated by spaces data = str(arg.split(" ")) # arguments are separated by spaces
eqsidx = data.find("=") # idx of = eqsidx = data.find("=") # idx of =
var = data[2:eqsidx] var = data[2:eqsidx]
rhs = data[eqsidx+1:len(data)-2] rhs = data[eqsidx+1:len(data)-2]
if var in self.dict: if var in self.dict:
#if self.dict.has_key(var): #if self.dict.has_key(var):
#check if we have a range # check if we have a range
if rhs.find(':') > 0: if rhs.find(':') > 0:
range = rhs.split(":") range = rhs.split(":")
if len(range) == 3: if len(range) == 3:
...@@ -105,18 +107,18 @@ class OpalDict: ...@@ -105,18 +107,18 @@ class OpalDict:
self.rangevars[var] = rvar self.rangevars[var] = rvar
self.numRanges = self.numRanges + 1 self.numRanges = self.numRanges + 1
else: else:
print( "OpalDict: Range has to be of the form from:to:step!") print("OpalDict: Range has to be of the form from:to:step!")
sys.exit(1) sys.exit(1)
else: else:
try: try:
val = literal_eval(rhs) val = literal_eval(rhs)
if (isinstance(val, int) or isinstance(val, float)): if (isinstance(val, int) or isinstance(val, float)):
self.uservars.append( (var, Decimal(rhs)) ) self.uservars.append((var, Decimal(rhs)))
self.dict[var] = Decimal(rhs) #self.getType(rhs) self.dict[var] = Decimal(rhs) #self.getType(rhs)
except: # add string except: # add string
self.uservars.append( (var, rhs) ) self.uservars.append( (var, rhs) )
self.dict[var] = rhs self.dict[var] = rhs
else: else:
if var.find("--") < 0: # not a regular option if var.find("--") < 0: # not a regular option
print( 'OpalDict: Key (' + var + ')not found can not add to dictionary, check the OPAL template file') print(f'OpalDict: Key ({key}) not found cannot add to dictionary, check the OPAL template file')
sys.exit(1) sys.exit(1)
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