Skip to content

Commit

Permalink
Use regular expression to normalize names
Browse files Browse the repository at this point in the history
This version adds an underscore before a file starting with a digit.
  • Loading branch information
rayosborn committed Apr 12, 2024
1 parent c1c4a70 commit 4e624f0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
20 changes: 2 additions & 18 deletions src/nexpy/gui/treeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
NXlink, NXroot, nxload)

from .pyqt import QtCore, QtGui, QtWidgets
from .utils import display_message, modification_time, report_error
from .utils import display_message, get_name, modification_time, report_error
from .widgets import NXSortModel


Expand Down Expand Up @@ -116,23 +116,7 @@ def reload(self, name):
raise NeXusError(f"{name} not in the tree")

def get_name(self, filename):
name = os.path.splitext(
os.path.basename(filename))[0].replace(' ', '_')
name = "".join([c for c in name.replace('-', '_')
if c.isalpha() or c.isdigit() or c == '_'])
if name in self._shell:
ind = []
for key in self._shell:
try:
if key.startswith(name+'_'):
ind.append(int(key[len(name)+1:]))
except ValueError:
pass
if ind == []:
ind = [0]
name = name+'_'+str(sorted(ind)[-1]+1)
return name

return get_name(filename, self._shell)
def get_new_name(self):
ind = []
for key in self._shell:
Expand Down
6 changes: 2 additions & 4 deletions src/nexpy/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import os
import re
import sys
import time
import traceback as tb
from configparser import ConfigParser
from datetime import datetime
from pathlib import Path
from threading import Thread

import numpy as np
Expand Down Expand Up @@ -427,9 +427,7 @@ def convertHTML(text):

def get_name(filename, entries=[]):
"""Return a valid object name from a filename."""
name = os.path.splitext(os.path.basename(filename))[0].replace(' ', '_')
name = "".join([c for c in name.replace('-', '_')
if c.isalpha() or c.isdigit() or c == '_'])
name = re.sub('\W|^(?=\d)','_', Path(filename).stem)
if name in entries:
ind = []
for key in entries:
Expand Down

0 comments on commit 4e624f0

Please sign in to comment.