def run(argv): bauerGlobals = BauerGlobals() setupLogging(argv) generatorInfo = GeneratorInfo() argParser = BauerArgParser(bauerGlobals, generatorInfo) argParser.buildBauerArguments(argv) args = argParser.parse_args() if args == None: return if args.command == 'new': templateCreator = TemplateCreator() templateCreator.generate(args) return rootPath = os.path.abspath( os.path.join(os.path.realpath(__file__), "..", "..")) source_folder = os.getcwd() buildFolder = BuildFolder(bauerGlobals, generatorInfo, source_folder, args) commandProcessor = CommandProcessor(bauerGlobals, generatorInfo, args, rootPath, source_folder, buildFolder) commandProcessor.process()
def __init__(self): ''' Constructor ''' self.template = '%d-%d' self.cp = CommandProcessor() LocType.Load(self.cp.getLocTypes()) self.locs = {} self.locsByCoor = [] self.rangire = 0
def run(argv): bauerGlobals = BauerGlobals() setupLogging(argv) generatorInfo = GeneratorInfo() argParser = BauerArgParser(bauerGlobals, generatorInfo) argParser.buildBauerArguments(argv) args = argParser.parse_args() if args == None: return if args.command == 'new': templateCreator = TemplateCreator() templateCreator.generate(args) return if args.command == 'doc': doccer = Documentation() doccer.run(args) return rootPath = os.path.abspath( os.path.join(os.path.realpath(__file__), "..", "..")) source_folder = os.getcwd() # Python will switch to the real path when the working dir has a symlink in it. # We'll update the current dir and the environment variable to reflect this. os.chdir(source_folder) os.environ["PWD"] = source_folder buildFolder = BuildFolder(bauerGlobals, generatorInfo, source_folder, args) commandProcessor = CommandProcessor(bauerGlobals, generatorInfo, args, rootPath, source_folder, buildFolder) commandProcessor.process()
class Map(object): ''' classdocs ''' def __init__(self): ''' Constructor ''' self.template = '%d-%d' self.cp = CommandProcessor() LocType.Load(self.cp.getLocTypes()) self.locs = {} self.locsByCoor = [] self.rangire = 0 def load(self): for loc in self.cp.getLocations(): self.locs[self.template % (loc['x'], loc['y'])] = Location(loc) count = len(self.locs) self.rangire = int((math.sqrt(count) - 1) / 2) def wipe(self, fromRadius=0, maxRadius=50): for loc in self.locs.values(): if ((abs(loc.x) <= maxRadius and abs(loc.x) > fromRadius) or \ (abs(loc.y) <= maxRadius and abs(loc.y) > fromRadius)): loc.type = None def getLocByCoor(self, x, y): if self.template % (x, y) in self.locs.keys(): return self.locs[self.template % (x, y)] return None def getNeighs(self, x, y, useDiagonal=True): dxs = None if useDiagonal: dxs = ((x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y), (x + 1, y + 1), (x - 1, y - 1), (x + 1, y - 1), (x - 1, y + 1)) else: dxs = ((x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y)) neighs = [] for dxy in dxs: dx = dxy[0] dy = dxy[1] if dx < -self.rangire: dx += self.rangire * 2 + 1 elif dx > self.rangire: dx -= self.rangire * 2 + 1 if dy < -self.rangire: dy += self.rangire * 2 + 1 elif dy > self.rangire: dy -= self.rangire * 2 + 1 neighs.append(self.locs[self.template % (dx, dy)]) return neighs def getNewLocType(self, x, y): potencialLocTypes = {} for dxy in ((x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y)): dx = dxy[0] dy = dxy[1] if dx < -self.rangire: dx += self.rangire * 2 + 1 elif dx > self.rangire: dx -= self.rangire * 2 + 1 if dy < -self.rangire: dy += self.rangire * 2 + 1 elif dy > self.rangire: dy -= self.rangire * 2 + 1 neigh = self.getLocByCoor(dx, dy) if neigh and neigh.type: if neigh.type.type == neigh.type.baseType: potencialLocTypes[neigh.type] = random() * stability[neigh.type.baseType] else: locType = LocType.getRandomLocType(neigh.type.baseType) potencialLocTypes[locType] = random() * 0.9 * stability[locType.baseType] randLocType = LocType.getRandomLocType() potencialLocTypes[randLocType] = random() * randomStr * stability[randLocType.baseType] result = sorted(potencialLocTypes, key=lambda t: potencialLocTypes[t], reverse=True)[0] # print('Get new type', (datetime.now()-time).total_seconds()) return result def generateMap(self, fromRadius=0, maxRadius=50): i = -1 seed(datetime.now()) self.rangire = maxRadius data = {'id':self.locs[self.template % (-maxRadius, -maxRadius)].id, 'description':self.locs[self.template % (-maxRadius, -maxRadius)].description, 'x':-maxRadius, 'y':-maxRadius, 'type': LocType.getRandomLocType('field').id } loc = Location(data) self.locs[self.template % (loc.x, loc.y)] = loc for x in range(-maxRadius, maxRadius + 1): i *= -1 for y in range(-maxRadius, maxRadius + 1): y *= i if (x > fromRadius or y > fromRadius) or\ (x < -fromRadius or y < -fromRadius): data = {'id': self.locs[self.template % (loc.x, loc.y)].id, 'description': self.locs[self.template % (loc.x, loc.y)].description, 'x': x, 'y': y, 'type': self.getNewLocType(x, y).id } loc = Location(data) # print('Gen new loc', (datetime.now()-time).total_seconds()) self.locs[self.template % (loc.x, loc.y)] = loc def htmlMap(self): body = '' # count = len(self.locs) # predel = int((math.sqrt(count) - 1) / 2 ) # print(predel) predel = self.rangire for x in range(-predel, predel + 1): for y in range(-predel, predel + 1): loc = self.getLocByCoor(x, y) htmlTemplate = '''<div id='%s' style="float:left;margin:0px;width:10px; height:10px; background-color:%s;background-position: -4px -4px;"></div>''' body += htmlTemplate % ('%d-%d' % (x, y), colors[loc.type.baseType]) body += '<div style="width:10px;height:10px;"></div>' with open('tmp.html', 'w') as f: f.write(body) def saveMap(self): for loc in self.locs.values(): if loc.id: self.cp.updateDBLoc(loc) else: self.cp.createDBLoc(loc) self.cp.commit() def smoothMap(self, fromRadius=0, maxRadius=50, strengh=4): for loc in self.locs.values(): if ((abs(loc.x) <= maxRadius and abs(loc.x) > fromRadius) or \ (abs(loc.y) <= maxRadius and abs(loc.y) > fromRadius)): waters = 0 woods = 0 fields = 0 similar = 0 neighs = self.getNeighs(loc.x, loc.y) for neigh in neighs: if neigh.type.type == loc.type.type and \ neigh.type.baseType == loc.type.baseType: similar += 1 elif neigh.type.type == 'water': waters += 1 elif neigh.type.type == 'field': fields += 1 elif neigh.type.type == 'wood': woods += 1 if similar > 0 and waters < strengh and fields < strengh + 1 and woods < strengh + 1: continue if waters >= strengh: loc.type = LocType.getRandomLocType(baseType='water', ltype='water') elif fields >= strengh + 1: loc.type = LocType.getRandomLocType(baseType='field', ltype='field') elif woods >= strengh + 1: loc.type = LocType.getRandomLocType(baseType='wood', ltype='wood') elif waters >= fields and waters >= woods: loc.type = LocType.getRandomLocType(baseType='water', ltype='water') elif fields > woods: loc.type = LocType.getRandomLocType(baseType='field', ltype='field') else: loc.type = LocType.getRandomLocType(baseType='wood', ltype='wood')
def __init__(self, localConfig, logger): self.localConfig = localConfig self.logger = logger self.periphController = PeripheralController() self.commandProcessor = CommandProcessor(logger, self.periphController, self.localConfig)
class DiversityMain: port = None name = None logger = None main_thread = None listenerSocket = None senderSocket = None localConfig = None periphController = None commandProcessor = None def __init__(self, localConfig, logger): self.localConfig = localConfig self.logger = logger self.periphController = PeripheralController() self.commandProcessor = CommandProcessor(logger, self.periphController, self.localConfig) def start(self): self.event = threading.Event() self.main_thread = Thread(target=self.startMainThread, args=("test",)) self.main_thread.start() self.periphController.startThread() def startMainThread(self, teset): self.startReportingTimer() self.event.wait() self.logger.write("reporttimer", "out") def startReportingTimer(self): if self.event.isSet(): self.logger.write("reporttimer", "Not restarting") else: self.timer = Timer(1, self.doReport, args=["WOW"]) self.timer.start() freqcounter = 0 reportAtStartup = True def doReport(self, message): global localConfig freq = int(localConfig.connectfreq) # self.logger.write("main_thread","reporting") self.freqcounter += 1 if self.freqcounter >= freq or self.commandProcessor.wantPostNow() == True or self.reportAtStartup == True: self.reportAtStartup = False self.freqcounter = 0 self.postalive() self.startReportingTimer() def join(self): self.main_thread.join() def stop(self): self.periphController.stopThread() self.timer.cancel() self.event.set() def postalive(self): postdata = {"cid": localConfig.clientid, "psw": localConfig.password} self.commandProcessor.postAlive()
def InitCmdProcessor(): global command_processor command_processor = CommandProcessor()
#!/usr/bin/env python3 from commandprocessor import CommandProcessor cmd = CommandProcessor("cmdcli.conf") cmd.start() cmd.setCallback(print) i = "" while i != "!quit": i = input("CmdProc :: ") cmd.push(i)