Skip to content

Commit

Permalink
add support for py2
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotan committed Nov 23, 2023
1 parent 3798147 commit 7ff1d35
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions nxstools/nxsfileinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
from . import filewriter
from .ontology import id_techniques, nexus_panet


if sys.version_info > (3,):
basestring = str


WRITERS = {}
try:
from . import h5pywriter
Expand Down Expand Up @@ -655,7 +660,7 @@ def append_copymap_field(self, metadata, cmap, clist, cmapfield=None):
if dct and isinstance(dct, dict):
cmap.update(dct)
elif dct:
if isinstance(dct, str):
if isinstance(dct, basestring):
dct = getlist(str(md).strip())
if isinstance(dct, list):
for line in dct:
Expand Down Expand Up @@ -685,7 +690,8 @@ def overwrite(self, metadata, cmap=None, clist=None, cmapfield=None):
self.append_copymap_field(metadata, cpmap, cplist, cmapfield)
if metadata:
for ts, vs in cpmap.items():
if ts and vs and isinstance(ts, str) and isinstance(vs, str) \
if ts and vs and isinstance(ts, basestring) \
and isinstance(vs, basestring) \
and not ts.startswith(vs + "."):
vls = vs.split(".")
md = metadata
Expand All @@ -708,7 +714,8 @@ def overwrite(self, metadata, cmap=None, clist=None, cmapfield=None):
parent[tg] = md
for line in cplist:
if line and len(line) > 1 and line[0] and line[1] and \
isinstance(line[0], str) and isinstance(line[1], str) and \
isinstance(line[0], basestring) and \
isinstance(line[1], basestring) and \
not line[0].startswith(line[1] + "."):
ts = line[0]
vs = line[1]
Expand Down Expand Up @@ -758,7 +765,7 @@ def remove_metadata(self, metadata, cmap=None, clist=None, cmapfield=None):
vv = vs
if not vs:
vv = ts
if vv and isinstance(vv, str):
if vv and isinstance(vv, basestring):
vls = vv.split(".")
md = metadata
parent = None
Expand All @@ -780,7 +787,7 @@ def remove_metadata(self, metadata, cmap=None, clist=None, cmapfield=None):
vv = line[0]
if line[1] and not line[0]:
vv = line[1]
if vv and isinstance(vv, str):
if vv and isinstance(vv, basestring):
vls = vv.split(".")
md = metadata
parent = None
Expand Down Expand Up @@ -1377,7 +1384,7 @@ def metadata(cls, root, options):
if dct and isinstance(dct, dict):
usercopymap.update(dct)
elif dct:
if isinstance(dct, str):
if isinstance(dct, basestring):
dct = getlist(options.copymap.strip())
if isinstance(dct, list):
for line in dct:
Expand All @@ -1398,7 +1405,7 @@ def metadata(cls, root, options):
if dct and isinstance(dct, dict):
usercopymap.update(dct)
elif dct:
if isinstance(dct, str):
if isinstance(dct, basestring):
dct = getlist(jstr.strip())
if isinstance(dct, list):
for line in dct:
Expand Down Expand Up @@ -1843,7 +1850,8 @@ def _update_metadata(cls, gr, ds, cplist):

for line in cplist:
if line and len(line) > 1 and line[0] and line[1] and \
isinstance(line[0], str) and isinstance(line[1], str) and \
isinstance(line[0], basestring) and \
isinstance(line[1], basestring) and \
not line[0].startswith(line[1] + "."):
ts = line[0]
vs = line[1]
Expand Down Expand Up @@ -1880,7 +1888,7 @@ def _merge_meta(cls, parent, key, md):
:type md: :obj:`str` or :obj:`dict`
"""
tg = None
if isinstance(md, str):
if isinstance(md, basestring):
if key in parent.keys():
tg = parent[key]
if tg and isinstance(tg, dict):
Expand Down Expand Up @@ -1909,7 +1917,7 @@ def _merge_meta(cls, parent, key, md):
if key in parent.keys():
tg = parent[key]
if md:
if isinstance(md[0], str):
if isinstance(md[0], basestring):
if isinstance(tg, list):
parent[key].extend(
[mi for mi in md if mi not in tg])
Expand Down Expand Up @@ -2042,7 +2050,6 @@ def _create_metadata(cls, scfile, clist, options):
grouped attachments]
:rtype: [:obj:`str`,:obj:`str`, :obj:`str`]
"""
# print("CREATE from ", scfile)

cpmap = {
"accessGroups": "accessGroups",
Expand Down Expand Up @@ -2115,16 +2122,18 @@ def _create_metadata(cls, scfile, clist, options):
if len(spid) > 1:
spid[-1] = beamtimeid
spid[-2] = group
metadata["pid"] = "/".join(spid)
metadata["pid"] = str("/".join(spid))
else:
metadata["pid"] = "%s/%s" % (group, beamtimeid)
metadata["pid"] = str(
"%s/%s" % (group, beamtimeid))
if group:
metadata["keywords"].append(group)

for ts, vs in cpmap.items():
if options.raw:
ts = vs
if ts and vs and isinstance(ts, str) and isinstance(vs, str) \
if ts and vs and isinstance(ts, basestring) \
and isinstance(vs, basestring) \
and not ts.startswith(vs + "."):
vls = vs.split(".")
md = ds
Expand Down Expand Up @@ -2224,7 +2233,7 @@ def groupmetadata(cls, options):
if dct and isinstance(dct, dict):
usergroupmap.update(dct)
elif dct:
if isinstance(dct, str):
if isinstance(dct, basestring):
dct = getlist(options.groupmap.strip())
if isinstance(dct, list):
for line in dct:
Expand All @@ -2245,7 +2254,7 @@ def groupmetadata(cls, options):
if dct and isinstance(dct, dict):
usergroupmap.update(dct)
elif dct:
if isinstance(dct, str):
if isinstance(dct, basestring):
dct = getlist(jstr.strip())
if isinstance(dct, list):
for line in dct:
Expand All @@ -2256,7 +2265,6 @@ def groupmetadata(cls, options):
for ky, vl in usergroupmap.items():
if vl:
grouplist.append([ky, vl])

if omfile:
if os.path.isfile(omfile):
result = cls._group_metadata(
Expand Down

0 comments on commit 7ff1d35

Please sign in to comment.