Commit feb4a796 authored by Lars Kruse's avatar Lars Kruse

code deduplication

Use a separate function (get_non_conflicting_name) for determining the
name of new objects (models, processes, bounds, tasks and toolpaths).
parent 488f60c9
......@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Plugins
# TODO: move Toolpath.Bounds here?
import pycam.Toolpath
from pycam.Utils import get_non_conflicting_name
_RELATIVE_UNIT = ("%", "mm")
......@@ -362,11 +363,9 @@ class Bounds(pycam.Plugins.ListPluginBase):
self.core.emit_event("bounds-changed")
def _bounds_new(self, *args):
bounds_names = [bounds["name"] for bounds in self]
bounds_id = 1
while ("Bounds #%d" % bounds_id) in bounds_names:
bounds_id += 1
new_bounds = BoundsDict(self.core, "Bounds #%d" % bounds_id)
name = get_non_conflicting_name("Bounds #%d",
[bounds["name"] for bounds in self])
new_bounds = BoundsDict(self.core, name)
self.append(new_bounds)
self.select(new_bounds)
......
......@@ -24,6 +24,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
#import gtk
import pycam.Plugins
from pycam.Utils import get_non_conflicting_name
_GTK_COLOR_MAX = 65535.0
......@@ -173,11 +174,8 @@ class Models(pycam.Plugins.ListPluginBase):
color=None):
model_dict = ModelEntity(model)
if not name:
model_id = 1
model_names = [model["name"] for model in self]
while (name_template % model_id) in model_names:
model_id += 1
name = name_template % model_id
name = get_non_conflicting_name(name_template,
[model["name"] for model in self])
model_dict["name"] = name
if not color:
color = self.core.get("color_model")
......
......@@ -21,6 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Plugins
from pycam.Utils import get_non_conflicting_name
class Processes(pycam.Plugins.ListPluginBase):
......@@ -243,14 +244,11 @@ class Processes(pycam.Plugins.ListPluginBase):
strategies = self.core.get("get_parameter_sets")("process").values()
strategies.sort(key=lambda item: item["weight"])
strategy = strategies[0]
strategy_names = [process["name"] for process in self]
process_id = 1
name_template = "Process #%d"
while (name_template % process_id) in strategy_names:
process_id += 1
name = get_non_conflicting_name("Process #%d",
[process["name"] for process in self])
new_process = ProcessEntity({"strategy": strategy["name"],
"parameters": strategy["parameters"].copy(),
"name": name_template % process_id})
"name": name})
self.append(new_process)
self.select(new_process)
......
......@@ -25,6 +25,7 @@ import time
import pycam.Plugins
import pycam.Utils
from pycam.Exporters.GCodeExporter import GCodeGenerator
from pycam.Utils import get_non_conflicting_name
class Tasks(pycam.Plugins.ListPluginBase):
......@@ -248,14 +249,11 @@ class Tasks(pycam.Plugins.ListPluginBase):
types = self.core.get("get_parameter_sets")("task").values()
types.sort(key=lambda item: item["weight"])
one_type = types[0]
task_id = 1
name_template = "Task #%d"
task_names = [task["name"] for task in self]
while (name_template % task_id) in task_names:
task_id += 1
name = get_non_conflicting_name("Task #%d",
[task["name"] for task in self])
new_task = TaskEntity({"type": one_type["name"],
"parameters": one_type["parameters"].copy(),
"name": name_template % task_id})
"name": name})
self.append(new_task)
self.select(new_task)
......
......@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Plugins
import pycam.Toolpath
from pycam.Utils import get_non_conflicting_name
class Toolpaths(pycam.Plugins.ListPluginBase):
......@@ -156,11 +157,9 @@ class Toolpaths(pycam.Plugins.ListPluginBase):
parameters = new_tp.get_params()
else:
moves, parameters = new_tp
toolpath_id = len(self) + 1
name_template = "Toolpath #%d"
while (name_template % toolpath_id) in [tp["name"] for tp in self]:
toolpath_id += 1
attributes= {"visible": True, "name": name_template % toolpath_id}
name = get_non_conflicting_name("Toolpath #%d",
[tp["name"] for tp in self])
attributes= {"visible": True, "name": name}
new_tp = ToolpathEntity(toolpath_path=moves,
toolpath_parameters=parameters, attributes=attributes)
self.append(new_tp)
......
......@@ -59,6 +59,7 @@ def get_platform():
else:
return PLATFORM_UNKNOWN
def get_case_insensitive_file_pattern(pattern):
""" Convert something like "*.svg" into "*.[sS][vV][gG]" - as it is
required for GTK's FileFilter.
......@@ -73,6 +74,36 @@ def get_case_insensitive_file_pattern(pattern):
return "".join(result)
def get_non_conflicting_name(template, conflicts, start=None,
get_next_func=None):
""" Find a string containing a number that is not in conflict with any of
the given strings. A name template (containing "%d") is required.
You may use non-numbers (strings, floats, ...) as well. In this case
you need to override "start" and "get_next_func".
@value template: a string template containing "%d" (e.g. "Object %d")
@type template: basestr
@value conflicts: a list of strings that need may not be used
@type conflicts: list(basestr)
@value start: optional initial value (default: len(conflicts) + 1)
@type start: undefined
@value get_next_func: function used for determining the next value to
be tested. This function defaults to "lambda value: value + 1".
@returns: a usable name that was not found in "conflicts"
@rtype: basestr
"""
if start is None:
index = len(conflicts) + 1
else:
index = start
if get_next_func is None:
get_next_func = lambda current: current + 1
while (template % index) in conflicts:
index = get_next_func(index)
return template % index
class URIHandler(object):
DEFAULT_PREFIX = "file://"
......
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