def run(self): placer = Placer(NEW, NR_OLD_ONES, from_nr) if SHOW: graphics = Graphics(placer) graphics.display() while True: pass else: placer.run()
def __init__(self, frame, treater, settings): self.is_active = False self.oposite = lambda: None self.treater = treater self.placer = Placer() self.settings = settings self.sort_by = "name" self.sort_rev = False self.pwd = StringVar() self._places = None self.has_pardir = False self.pwd.set(self.treater.get_cur_dir()) self.body(frame) ddir = join(dirname(__file__), "data") self.imgs = {} for t, f in (("directory", "folder.gif"), ("regular", "file.gif"), ("link", "link.gif"), ("fifo", "fifo.gif"), ("socket", "socket.gif"), ("block", "block.gif"), ("character", "character.gif"), ("Parent_dir", "pardir.gif")): self.imgs[t] = PhotoImage(master=frame, file=join(ddir, f)) self.str_ids = {} self.id_range = [] self.selected_ids = set() self.br_hist = [] self.fresh() self.renheadings()
import unittest import const from game_board import GameBoardADT from placer import Placer from helper import unplay_all, edge_setter placer = Placer() board = GameBoardADT() class TestGetSquareNoOfPlayed(unittest.TestCase): def test_with_zero(self): """ should return 0 when the given square has 0 played edges """ unplay_all(board) self.assertEqual(placer._get_square_no_of_played(5, board), 0) def test_with_one(self): """ should return 1 when the given square has 1 played edges """ unplay_all(board) board.setEdgeState(13, const.PLAYED) self.assertEqual(placer._get_square_no_of_played(5, board), 1) def test_with_two(self): """ should return 2 when the given square has 2 played edges """ unplay_all(board)
def place(filein, fileout): placer = Placer() placer.place(filein, fileout)
def __init__(self,config): Strategy.__init__(self,config) self.placer = Placer(config) self.picker = Picker(config)
class BasicStrategy(Strategy): def __init__(self,config): Strategy.__init__(self,config) self.placer = Placer(config) self.picker = Picker(config) def pickAndPlace(self,jobName,stepName,stream): boards={} jobs={} tapes={} tapeTemplates={} if self.config['boards']: for board in self.config['boards']: newBoard = Board(self.config) if 'name' in board: newBoard.setName(board['name']) if 'placements' in board: newBoard.setPlacements(PartPlacement.readFile(board['placements'])) else: print "No placement specified for board:",board['name'] if 'height' in board: newBoard.height=board['height']; boards[newBoard.name]=newBoard # Load up the tape templates for tapeTemplate in self.config['tapeTemplates']: template = TapeTemplate() if 'name' in tapeTemplate: template.name = tapeTemplate['name'] else: raise Exception("Missing tape template name") for name in tapeTemplate: if hasattr(template,name): setattr(template,name,tapeTemplate[name]) tapeTemplates[template.name]=template; # FIXME Should validate the tapetemplate here for tape in self.config['tapes']: if not 'name' in tape: raise Exception("No name specified for tape") if not 'template' in tape: raise Exception("No template specified for tape") if not tape['template'] in tapeTemplates: raise Exception("Invalid tape template specified:",tape['template']) partTape = Tape() for name in tape: if hasattr(partTape,name): setattr(partTape,name,tape[name]) partTape.tapeTemplate=tapeTemplates[tape['template']] tapes[partTape.name]=partTape for job in self.config['jobs']: j = Job() for name in job: if hasattr(j,name) and name!="steps" and name!="boards": setattr(j,name,job[name]) for board in job['boards']: if not 'name' in board: raise Exception("Invalid board specified") if not board['name'] in boards: raise Exception("Invalid board name:"+board['name']) j.addBoard(boards[board['name']], board['reference'],board['x'],board['y'], board['second_reference']) for step in job['steps']: js = JobStep() for name in step: if hasattr(js,name): setattr(js,name,step[name]) j.steps.append(js) jobs[j.name]=j ################################## #1 Get the current job and step if not jobName in jobs: raise Exception("Invalid job name:"+jobName) currentJob = jobs[jobName] currentStep = next((step for step in currentJob.steps if step.name == stepName), None) #2 Setup the placer with the boards stream.write("G28\n") stream.write("M302\n") stream.write("M92 E400\n") for board in currentJob.boards: self.placer.addBoard(board) self.placer.organizeBoards(stream) #3 Setup the picker with the tapes for tape in currentStep.tapes: self.picker.addTape(tapes[tape]) self.picker.organizeTapes(stream) self.picker.generatePlacements(self.placer.boards) while True: pn = self.picker.pickPart(stream); if None==pn: break self.placer.placePart(pn.board,pn.placement,pn.tape,stream);
def __init__(self, config): Strategy.__init__(self, config) self.placer = Placer(config) self.picker = Picker(config)
class BasicStrategy(Strategy): def __init__(self, config): Strategy.__init__(self, config) self.placer = Placer(config) self.picker = Picker(config) def pickAndPlace(self, jobName, stepName, stream): boards = {} jobs = {} tapes = {} tapeTemplates = {} if self.config['boards']: for board in self.config['boards']: newBoard = Board(self.config) if 'name' in board: newBoard.setName(board['name']) if 'placements' in board: newBoard.setPlacements( PartPlacement.readFile(board['placements'])) else: print "No placement specified for board:", board['name'] if 'height' in board: newBoard.height = board['height'] boards[newBoard.name] = newBoard # Load up the tape templates for tapeTemplate in self.config['tapeTemplates']: template = TapeTemplate() if 'name' in tapeTemplate: template.name = tapeTemplate['name'] else: raise Exception("Missing tape template name") for name in tapeTemplate: if hasattr(template, name): setattr(template, name, tapeTemplate[name]) tapeTemplates[template.name] = template # FIXME Should validate the tapetemplate here for tape in self.config['tapes']: if not 'name' in tape: raise Exception("No name specified for tape") if not 'template' in tape: raise Exception("No template specified for tape") if not tape['template'] in tapeTemplates: raise Exception("Invalid tape template specified:", tape['template']) partTape = Tape() for name in tape: if hasattr(partTape, name): setattr(partTape, name, tape[name]) partTape.tapeTemplate = tapeTemplates[tape['template']] tapes[partTape.name] = partTape for job in self.config['jobs']: j = Job() for name in job: if hasattr(j, name) and name != "steps" and name != "boards": setattr(j, name, job[name]) for board in job['boards']: if not 'name' in board: raise Exception("Invalid board specified") if not board['name'] in boards: raise Exception("Invalid board name:" + board['name']) j.addBoard(boards[board['name']], board['reference'], board['x'], board['y'], board['second_reference']) for step in job['steps']: js = JobStep() for name in step: if hasattr(js, name): setattr(js, name, step[name]) j.steps.append(js) jobs[j.name] = j ################################## #1 Get the current job and step if not jobName in jobs: raise Exception("Invalid job name:" + jobName) currentJob = jobs[jobName] currentStep = next( (step for step in currentJob.steps if step.name == stepName), None) #2 Setup the placer with the boards stream.write("G28\n") stream.write("M302\n") stream.write("M92 E400\n") for board in currentJob.boards: self.placer.addBoard(board) self.placer.organizeBoards(stream) #3 Setup the picker with the tapes for tape in currentStep.tapes: self.picker.addTape(tapes[tape]) self.picker.organizeTapes(stream) self.picker.generatePlacements(self.placer.boards) while True: pn = self.picker.pickPart(stream) if None == pn: break self.placer.placePart(pn.board, pn.placement, pn.tape, stream)
class Panel: def __init__(self, frame, treater, settings): self.is_active = False self.oposite = lambda: None self.treater = treater self.placer = Placer() self.settings = settings self.sort_by = "name" self.sort_rev = False self.pwd = StringVar() self._places = None self.has_pardir = False self.pwd.set(self.treater.get_cur_dir()) self.body(frame) ddir = join(dirname(__file__), "data") self.imgs = {} for t, f in (("directory", "folder.gif"), ("regular", "file.gif"), ("link", "link.gif"), ("fifo", "fifo.gif"), ("socket", "socket.gif"), ("block", "block.gif"), ("character", "character.gif"), ("Parent_dir", "pardir.gif")): self.imgs[t] = PhotoImage(master=frame, file=join(ddir, f)) self.str_ids = {} self.id_range = [] self.selected_ids = set() self.br_hist = [] self.fresh() self.renheadings() def body(self, frame): "Place panel widgets" frame.grid_columnconfigure(0, weight=1) frame.grid_rowconfigure(1, weight=1) top = Frame(frame) top.grid(column=0, row=0, sticky="ew") self.mounts = Combobox(top, takefocus=False, state="readonly") self.mounts["postcommand"] = self.get_places self.mounts.bind("<<ComboboxSelected>>", self.goto_place) self.mounts.pack(anchor="nw") pthl = Label(top, textvariable=self.pwd) pthl.pack(expand=True, anchor="w") self.tree = tree = Treeview(frame, columns=("size", "modified", "mode")) tree.grid(column=0, row=1, sticky="nwes") vsb = Scrollbar(frame, command=self.tree.yview, orient="vertical") vsb.grid(column=1, row=1, sticky="ns") tree["yscrollcommand"] = lambda f, l: autoscroll(vsb, f, l) hsb = Scrollbar(frame, command=self.tree.xview, orient="horizontal") hsb.grid(column=0, row=2, sticky="ew") tree["xscrollcommand"] = lambda f, l: autoscroll(hsb, f, l) tree.column("size", width=70, anchor="center") tree.column("modified", width=70, anchor="center") tree.column("mode", width=70, anchor="center") tree.heading("#0", command=lambda: self.on_heading("name")) tree.heading("size", command=lambda: self.on_heading("size")) tree.heading("modified", command=lambda: self.on_heading("date")) tree.tag_configure("selected", foreground="red") for i in (("<Return>", self.enter_file), ("<Double-Button-1>", self.enter_file), ("<Right>", self.enter_file), ("<Left>", self.go_back), ("<Tab>", self.switch), ("<Home>", self.go_top), ("<Button-1>", self.activate), ("<Insert>", self.turn_selection), ("<Control-r>", self.refresh)): tree.bind(*i) def renheadings(self): "Rename headings due to new sorting conditions" arrow_up = " \u2191" arrow_down = " \u2193" for col, name, sb in ( ("#0", _("Name"), "name"), ("size", _("Size"), "size"), ("modified", _("Date"), "date"), ("mode", _("Attr."), None)): if self.sort_by == sb: name += arrow_down if self.sort_rev else arrow_up self.tree.heading(col, text=name) def on_heading(self, name): "An heading was clicked" if self.sort_by == name: self.sort_rev = not self.sort_rev else: self.sort_by = name self.sort_rev = False self.sort_tree() self.renheadings() def fresh(self): "Update tree items. Returns True on error." files = self.treater.list_visible(self.settings) if files is None: return True self.clear() self.has_pardir = self.treater.has_pardir() if self.has_pardir: self.tree.insert("", "end", "Parent_dir", text=pardir, image=self.imgs["Parent_dir"]) ltp = localtime(time()) for name, ft, sz, mt, mode in files: mt = localtime(mt) if ltp.tm_year > mt.tm_year: smt = strftime("%b %Y", mt) elif ltp.tm_yday > mt.tm_yday: smt = strftime("%d %b", mt) else: smt = strftime("%H:%M") if sz < 1024: ssz = sz elif sz < 1048576: ssz = "%dK" % (sz // 1024) else: ssz = "%dM" % (sz // 1048576) mod_arr = [] # NOTE: Mentions of letters are: X - execute, W - write, R - read, # H - hidden, S - symbolic link for i, l in enumerate(_("XWRHS")): mod_arr.append(l if mode & (1 << i) else "-") mod_arr.reverse() mds = "".join(mod_arr) iid = self.tree.insert("", "end", text=name, values=(ssz, smt, mds), image=self.imgs[ft]) self.str_ids[iid] = (name, ft, sz, mt, mode) self.id_range.append(iid) self.sort_tree() def clear(self): for i in reversed(self.id_range): self.tree.delete(i) if self.has_pardir: self.tree.delete("Parent_dir") self.str_ids.clear() self.selected_ids.clear() del self.id_range[:] def sort_tree(self): si = {"name": 0, "size": 2, "date": 3}[self.sort_by] sd = self.str_ids key = lambda x: sd[x][si] self.id_range.sort(key=key, reverse=self.sort_rev) key = lambda x: 2 if sd[x][1] != "directory" else 1 self.id_range.sort(key=key) mv = self.tree.move start = 1 if self.has_pardir else 0 for pos, iid in enumerate(self.id_range, start): mv(iid, "", pos) def enter_file(self, evt=None): iid = self.tree.focus() if iid == "Parent_dir": self._change_dir(pardir) return if self.str_ids[iid][FT_TYPE] == "directory": mode = self.str_ids[iid][FT_MODE] mask = M_READABLE | M_EXECUTABLE if mode & mask != mask: return self._change_dir(self.str_ids[iid][FT_NAME]) def turn_selection(self, evt=None): iid = self.tree.focus() if iid != "Parent_dir": selected = self.selected_ids if iid in selected: self.tree.item(iid, tags=()) selected.discard(iid) else: selected.add(iid) self.tree.item(iid, tags=("selected")) if self.settings.get("insert_moves_down", True) and self.id_range: if iid == "Parent_dir": move_to = 0 else: move_to = self.id_range.index(iid) + 1 if move_to >= len(self.id_range): move_to = -1 tree = self.tree tree.focus(item=self.id_range[move_to]) tree.selection_remove(*(tree.selection())) tree.selection_add(tree.focus()) def go_back(self, evt=None): try: prdir = self.br_hist.pop(-1) except IndexError: return self._change_dir(prdir, False) def refresh(self, evt=None): iid = self.tree.focus() fid = self.str_ids fn = fid[iid][0] pos = self.id_range.index(iid) self.fresh() iid = "" for i in fid: if fid[i][0] == fn: iid = i break if iid == "": if pos < len(self.id_range): iid = self.id_range[pos] else: iid = self.id_range[-1] self.tree.focus(item=iid) self.activate() def activate(self, evt=None): if self.tree.focus() == "": self.tree.focus(item=self.id_range[0]) self.tree.selection_add(self.tree.focus()) oposite = self.oposite() if oposite is not None: opt = oposite.tree sel = opt.selection() if sel: opt.selection_remove(*sel) oposite.is_active = False self.tree.focus_set() self.is_active = True def switch(self, evt=None): if self.oposite() is None: return self.oposite().activate() def go_top(self, evt=None): self.tree.focus(item=self.id_range[0]) sel = self.tree.selection() if sel: self.tree.selection_remove(*sel) self.tree.selection_add(self.id_range[0]) def get_places(self): try: places = self.placer.placing_items() except AttributeError: self._places = None self.mounts["values"] = None return self.mounts["values"] = [i[0] for i in places] # self.mounts["values"] can be changet after assignment. Therefore # it is more safe to replace names of mount points by changed ones. self._places = dict((j, i[1]) for i, j in zip(places, self.mounts["values"])) def goto_place(self, evt=None): if self._places is None: return place = self._places[self.mounts.get()] self._change_dir(place) def _change_dir(self, path, remember=True): previous = self.treater.get_cur_dir() if self.treater.cd(path): return if self.fresh(): self.treater.cd(previous) elif remember: self.br_hist.append(previous) self.pwd.set(self.treater.get_cur_dir()) self.activate() def get_selected_filenames(self): result = [] if not self.selected_ids: iid = self.tree.focus() if iid != "Parent_dir": result.append(self.str_ids[iid][FT_NAME]) for iid in self.selected_ids: result.append(self.str_ids[iid][FT_NAME]) return result