class TwinodeServiceClass(DefaultServiceClass): def __init__(self, storeName): self.db = Store(storeName) self.auth = auth.DefaultAuthenticator(TwinodeSharedSecretSource(self.db)) hawkowl = self.db.findOrCreate(User, username="******", secret="password") self.db.findOrCreate(APIKey, username="******", password="******", user=hawkowl)
class CalendarDatabase: """ Database object for calendar""" def __init__(self): self.store = Store('/usr/local/tcs/tums/calendar.axiom') def createEntry(self, owner, date, stTime, enTime, descrip, email=False, repeat=0, vacation=False, private=False): day, month, year = date hourS, minuteS = stTime hourE, minuteE = enTime ehash = sha.sha('%s:%s:%s:%s%s' % (time.time(), day, month, year, owner)).hexdigest() calEnt = self.store.findOrCreate(CalendarEntry, owner=owner, day=day, month=month, year=year, hourS=hourS, minuteS=minuteS, hourE=hourE, minuteE=minuteE, descrip=descrip, emailAlert=email, repeats=repeat, vacation=vacation, private=private, ehash=ehash) return calEnt def getEntriesMonth(self, owner, month, year): viewers = [ i.owner for i in self.store.query( CalendarShareMapper, CalendarShareMapper.viewer == owner) ] owned = [owner] owned.extend(viewers) return self.store.query( CalendarEntry, AND(CalendarEntry.owner in owned, CalendarEntry.month == month, CalendarEntry.year == year)) def getEntriesDay(self, owner, day, month, year): return self.store.query( CalendarEntry, AND(CalendarEntry.owner == owner, CalendarEntry.day == day, CalendarEntry.month == month, CalendarEntry.year == year))
class OCRoot(Resource): def __init__(self, boards=[], *args, **kwargs): Resource.__init__(self) self.store = Store(*args, **kwargs) index = OCIndex(self.store) self.putChild("", index) self.putChild("index", index) for abbreviation, name in boards: self.add_board(abbreviation, name) def add_board(self, abbreviation, name): board = self.store.findOrCreate(Board, abbreviation=abbreviation, name=name) self.putChild(abbreviation, OCBoard(board))
class AggregatorDatabase: """ Database object for NetFlow aggregator""" def __init__(self): self.store = Store('/usr/local/tcs/tums/db.axiom') def addVolume(self, ip, vIn, vOut, port, index): # Volume records may generaly have *either* vIn or vOut. # This is because a flow record adds only per flow which is in # a single direction. # We aggregate more to the port, we don't care about connector source # ports because these are *always* random high ports. # We only add this per day. date = datetime.datetime.now() month = date.month year = date.year day = date.day volume = self.store.findOrCreate(Volume, localIp=ip, port=port, ifaceIndex=index, month=month, year=year, day=day) if volume.vIn: volume.vIn += vIn else: volume.vIn = vIn or 0 if volume.vOut: volume.vOut += vOut else: volume.vOut = vOut or 0 def getTotalIndex(self, month, year, day=0): """ Get totals by interface index """ if day > 0: volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year, Volume.day == day)) else: volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year)) volumeTotalsByIndex = {} for volume in volumeRecs: index = volume.ifaceIndex if index in volumeTotalsByIndex: if volume.vIn: volumeTotalsByIndex[index][0] += volume.vIn if volume.vOut: volumeTotalsByIndex[index][1] += volume.vOut else: volumeTotalsByIndex[index] = [ volume.vIn or 0, volume.vOut or 0 ] return volumeTotalsByIndex def getVolumeTotalByIp(self, month, year, day=0, index=0): """ Gets total volumes for each IP address on the set date and index """ a = [] if day > 0: a.append(Volume.day == day) if index: a.append(Volume.ifaceIndex == index) volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year, *a)) volumeTotalsByIp = {} for volume in volumeRecs: if volume.localIp in volumeTotalsByIp: if volume.vIn: volumeTotalsByIp[volume.localIp][0] += volume.vIn if volume.vOut: volumeTotalsByIp[volume.localIp][1] += volume.vOut else: volumeTotalsByIp[volume.localIp] = [ volume.vIn or 0, volume.vOut or 0 ] return volumeTotalsByIp def getVolumeRecByIp(self, month, year, ip): """ Get volume records for each IP on month """ return [(i.timestamp, i.vIn, i.vOut) for i in self.store.query( Volume, AND(Volume.month == month, Volume.year == year, Volume.localIp == ip))] def getPortBreakdownForIp(self, month, year, ip, day=0, index=0): """ Get port breakdown for each IP on date """ a = [] if day > 0: a.append(Volume.day == day) if index: a.append(Volume.ifaceIndex == index) volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year, Volume.localIp == ip, *a)) volumeByPort = {} for volume in volumeRecs: if volume.port in volumeByPort: if volume.vIn: volumeByPort[volume.port][0] += volume.vIn if volume.vOut: volumeByPort[volume.port][1] += volume.vOut else: volumeByPort[volume.port] = [volume.vIn or 0, volume.vOut or 0] return volumeByPort def getPortTotals(self, month, year, day=0, index=0): """ Get totals per port """ a = [] if day > 0: a.append(Volume.day == day) if index: a.append(Volume.ifaceIndex == index) volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year, *a)) volumeByPort = {} for volume in volumeRecs: if volume.port in volumeByPort: if volume.vIn: volumeByPort[volume.port][0] += volume.vIn if volume.vOut: volumeByPort[volume.port][1] += volume.vOut else: volumeByPort[volume.port] = [volume.vIn or 0, volume.vOut or 0] return volumeByPort
class Renamer(object): """ Renamer main logic. @type store: L{axiom.store.Store} @ivar store: Renamer database Store. @type history: L{renamer.history.History} @ivar history: Renamer history Item. @type options: L{renamer.application.Options} @ivar options: Parsed command-line options. @type command: L{renamer.irenamer.ICommand} @ivar command: Renamer command being executed. """ def __init__(self): self._obs = logging.RenamerObserver() log.startLoggingWithObserver(self._obs.emit, setStdout=False) self.options = self.parseOptions() self.store = Store(os.path.expanduser('~/.renamer/renamer.axiom')) # XXX: One day there might be more than one History item. self.history = self.store.findOrCreate(History) self.args = getattr(self.options, 'args', []) self.command = self.getCommand(self.options) def parseOptions(self): """ Parse configuration file and command-line options. """ _options = Options({}) _options.parseOptions() self._obs.verbosity = _options['verbosity'] self._configFile = config.ConfigFile( FilePath(os.path.expanduser(_options['config']))) command = self.getCommand(_options) options = Options(self._configFile) # Apply global defaults. options.update(self._configFile.get('renamer', options)) # Apply command-specific overrides for the global config. options.update( (k, v) for k, v in self._configFile.get(command.name, options).iteritems() if k in options) # Command-line options trump the config file. options.parseOptions() logging.msg( 'Global options: %r' % (options,), verbosity=5) return options def getCommand(self, options): """ Get the L{twisted.python.usage.Options} command that was invoked. """ command = getattr(options, 'subOptions', None) if command is None: raise usage.UsageError('At least one command must be specified') while getattr(command, 'subOptions', None) is not None: command = command.subOptions return command def performRename(self, dst, src): """ Perform a file rename. """ if self.options['no-act']: logging.msg('Simulating: %s => %s' % (src.path, dst.path)) return if src == dst: logging.msg('Skipping noop "%s"' % (src.path,), verbosity=2) return if self.options['link-dst']: self.changeset.do( self.changeset.newAction(u'symlink', src, dst), self.options) else: self.changeset.do( self.changeset.newAction(u'move', src, dst), self.options) if self.options['link-src']: self.changeset.do( self.changeset.newAction(u'symlink', dst, src), self.options) def runCommand(self, command): """ Run a generic command. """ logging.msg( 'Using command "%s"' % (command.name,), verbosity=4) logging.msg( 'Command options: %r' % (command,), verbosity=5) return defer.maybeDeferred(command.process, self, self.options) def runRenamingCommand(self, command): """ Run a renaming command. """ def _processOne(src): self.currentArgument = src d = self.runCommand(command) d.addCallback(self.performRename, src) return d self.changeset = self.history.newChangeset() logging.msg( 'Running, doing at most %d concurrent operations' % ( self.options['concurrency'],), verbosity=3) return util.parallel( self.args, self.options['concurrency'], _processOne) def run(self): """ Begin processing commands. """ if IRenamingCommand(type(self.command), None) is not None: d = self.runRenamingCommand(self.command) else: d = self.runCommand(self.command) d.addCallback(self.exit) return d def exit(self, ignored): """ Perform the exit routine. """ # We can safely do this even with "no-act", since nothing was actioned # and there is no point leaving orphaned Items around. self.history.pruneChangesets()
def initializeAPIs(Store: store.Store): PublicAPI = Store.findOrCreate(API, name="Public API") Store.powerUp(PublicAPI, IAPI) EmployeeAPI = Store.findOrCreate(API, name="Employee API") Store.powerUp(EmployeeAPI, IAPI) SupervisorAPI = Store.findOrCreate(API, name="Supervisor API") Store.powerUp(SupervisorAPI, IAPI) AdministratorAPI = Store.findOrCreate(API, name="Administrator API") Store.powerUp(AdministratorAPI, IAPI) findCommand = commandFinder(Store) PublicAPI.powerUp(findCommand("Login"), ICommand) PublicAPI.powerUp(findCommand("AsyncLogin"), ICommand) AdministratorAPI.powerUp(findCommand("MakeAdministrator"), ICommand) AdministratorAPI.powerUp(findCommand("MakeSupervisor"), ICommand) AdministratorAPI.powerUp(findCommand("AddToArea"), ICommand) AdministratorAPI.powerUp(findCommand("CheckForNewEmployees"), ICommand) AdministratorAPI.powerUp(findCommand("NewArea"), ICommand) AdministratorAPI.powerUp(findCommand("ApproveTime"), ICommand) AdministratorAPI.powerUp(findCommand("EditTime"), ICommand) AdministratorAPI.powerUp(findCommand("ClockOut"), ICommand) AdministratorAPI.powerUp(findCommand("ViewHours"), ICommand) AdministratorAPI.powerUp(findCommand("ViewAverageHours"), ICommand) AdministratorAPI.powerUp(findCommand("ViewEmployees"), ICommand) AdministratorAPI.powerUp(findCommand("AssumeRole"), ICommand) AdministratorAPI.powerUp(findCommand("ClockIn"), ICommand) AdministratorAPI.powerUp(findCommand("SetSupervisor"), ICommand) AdministratorAPI.powerUp(findCommand("SetWorkLocations"), ICommand) AdministratorAPI.powerUp(findCommand("SetSubAccounts"), ICommand) AdministratorAPI.powerUp(findCommand("ChangePassword"), ICommand) AdministratorAPI.powerUp(findCommand("ViewReports"), ICommand) AdministratorAPI.powerUp(findCommand("AssignTask"), ICommand) AdministratorAPI.powerUp(findCommand("CreateTask"), ICommand) AdministratorAPI.powerUp(findCommand("ManageSubAccounts"), ICommand) AdministratorAPI.powerUp(findCommand("ManageWorkLocations"), ICommand) AdministratorAPI.powerUp(findCommand("ScheduleTimeOff"), ICommand) AdministratorAPI.powerUp(findCommand("ApproveTimeOff"), ICommand) AdministratorAPI.powerUp(findCommand("ViewBenefits"), ICommand) AdministratorAPI.powerUp(findCommand("SetSupervisorSubAccounts"), ICommand) sv = findCommand("ScheduleVacation") if sv: for api in [AdministratorAPI, EmployeeAPI, SupervisorAPI]: if sv in api.powerupsFor(ICommand): api.powerDown(sv, ICommand) sv.deleteFromStore(Store) sv = findCommand("ApproveVacation") if sv: for api in [AdministratorAPI, EmployeeAPI, SupervisorAPI]: if sv in api.powerupsFor(ICommand): api.powerDown(sv, ICommand) sv.deleteFromStore(Store) EmployeeAPI.powerUp(findCommand("ClockIn"), ICommand) EmployeeAPI.powerUp(findCommand("ClockOut"), ICommand) EmployeeAPI.powerUp(findCommand("ViewHours"), ICommand) EmployeeAPI.powerUp(findCommand("ViewAverageHours"), ICommand) EmployeeAPI.powerUp(findCommand("ChangePassword"), ICommand) EmployeeAPI.powerUp(findCommand("ScheduleTimeOff"), ICommand) EmployeeAPI.powerUp(findCommand("ViewBenefits"), ICommand) SupervisorAPI.powerUp(findCommand("ClockOut"), ICommand) SupervisorAPI.powerUp(findCommand("ViewHours"), ICommand) SupervisorAPI.powerUp(findCommand("ViewAverageHours"), ICommand) # SupervisorAPI.powerUp(findCommand("ViewEmployees"), ICommand) SupervisorAPI.powerUp(findCommand("AssumeRole"), ICommand) SupervisorAPI.powerUp(findCommand("ClockIn"), ICommand) SupervisorAPI.powerUp(findCommand("EditTime"), ICommand) SupervisorAPI.powerUp(findCommand("ApproveTime"), ICommand) SupervisorAPI.powerUp(findCommand("ChangePassword"), ICommand) SupervisorAPI.powerUp(findCommand("AssignTask"), ICommand) SupervisorAPI.powerUp(findCommand("CreateTask"), ICommand) SupervisorAPI.powerUp(findCommand("ScheduleTimeOff"), ICommand) SupervisorAPI.powerUp(findCommand("ApproveTimeOff"), ICommand) SupervisorAPI.powerUp(findCommand("ViewBenefits"), ICommand)
def initializeCommands(Store: store.Store): Store.powerUp(Store.findOrCreate(MakeSupervisor, name="Make Supervisor"), ICommand) Store.powerUp(Store.findOrCreate(MakeAdministrator, name="Make Administrator"), ICommand) Store.powerUp(Store.findOrCreate(AddToArea, name="Add to SubAccount"), ICommand) Store.powerUp(Store.findOrCreate(CheckForNewEmployees, name="Check For New Employees"), ICommand) Store.powerUp(Store.findOrCreate(ClockIn, name="Clock In"), ICommand) Store.powerUp(Store.findOrCreate(NewArea, name="New SubAccount"), ICommand) Store.powerUp(Store.findOrCreate(ClockOut, name="Clock Out"), ICommand) Store.powerUp(Store.findOrCreate(ApproveTime, name="Approve Time"), ICommand) Store.powerUp(Store.findOrCreate(Login, name="Login"), ICommand) Store.powerUp(Store.findOrCreate(AsyncLogin), ICommand) Store.powerUp(Store.findOrCreate(EditTime, name="Edit Time"), ICommand) Store.powerUp(Store.findOrCreate(ViewHours, name="View Hours"), ICommand) Store.powerUp(Store.findOrCreate(ViewAverageHours, name="View Average Hours"), ICommand) Store.powerUp(Store.findOrCreate(ViewEmployees, name="View Employees"), ICommand) Store.powerUp(Store.findOrCreate(AssumeRole, name="Assume Role"), ICommand) Store.powerUp(Store.findOrCreate(SetSupervisor, name="Set Supervisor"), ICommand) Store.powerUp(Store.findOrCreate(SetWorkLocations, name="Set Work Locations"), ICommand) Store.powerUp(Store.findOrCreate(SetSubAccounts, name="Set Sub Accounts"), ICommand) Store.powerUp(Store.findOrCreate(ChangeAuthentication, name="Change Password"), ICommand) Store.powerUp(Store.findOrCreate(ViewReports, name="View Reports"), ICommand) Store.powerUp(Store.findOrCreate(ManageSubAccounts, name="Manage Sub Accounts"), ICommand) Store.powerUp(Store.findOrCreate(ManageWorkLocations, name="Manage Work Locations"), ICommand) Store.powerUp(Store.findOrCreate(CreateTask), ICommand) Store.powerUp(Store.findOrCreate(AssignTask), ICommand) Store.powerUp(Store.findOrCreate(ScheduleTimeOff), ICommand) Store.powerUp(Store.findOrCreate(ApproveTimeOff), ICommand) Store.powerUp(Store.findOrCreate(ViewBenefits), ICommand) Store.powerUp(Store.findOrCreate(SetSupervisorSubAccounts), ICommand)
class AggregatorDatabase: """ Database object for NetFlow aggregator""" def __init__(self): self.store = Store('/usr/local/tcs/tums/db.axiom') def addVolume(self, ip, vIn, vOut, port): # Volume records may generaly have *either* vIn or vOut. # This is because a flow record adds only per flow which is in # a single direction. # We aggregate more to the port, we don't care about connector source # ports because these are *always* random high ports. timenow = int(time.time()) date = datetime.datetime.now() month = date.month year = date.year day = date.day volume = self.store.findOrCreate(Volume, localIp = ip, port = port, vIn = vIn, vOut = vOut, timestamp = timenow, month = month, year = year, day = day ) def getVolumeTotalByIp(self, month, year, day=0): if day > 0: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year, Volume.day==day) ) else: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year) ) volumeTotalsByIp = {} for volume in volumeRecs: if volume.localIp in volumeTotalsByIp: volumeTotalsByIp[volume.localIp][0] += volume.vIn volumeTotalsByIp[volume.localIp][1] += volume.vOut else: volumeTotalsByIp[volume.localIp] = [volume.vIn, volume.vOut] return volumeTotalsByIp def getVolumeRecByIp(self, month, year, ip): return [(i.timestamp, i.vIn, i.vOut) for i in self.store.query(Volume, AND(Volume.month==month, Volume.year==year, Volume.localIp==ip) )] def getPortBreakdownForIp(self, month, year, ip, day=0): if day <1: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year, Volume.localIp==ip) ) else: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year, Volume.localIp==ip, Volume.day ==day) ) volumeByPort = {} for volume in volumeRecs: if volume.port in volumeByPort: volumeByPort[volume.port][0] += volume.vIn volumeByPort[volume.port][1] += volume.vOut else: volumeByPort[volume.port] = [volume.vIn, volume.vOut] return volumeByPort def getPortTotals(self, month, year, day=0): if day > 0: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year, Volume.day==day) ) else: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year) ) volumeByPort = {} for volume in volumeRecs: if volume.port in volumeByPort: volumeByPort[volume.port][0] += volume.vIn volumeByPort[volume.port][1] += volume.vOut else: volumeByPort[volume.port] = [volume.vIn, volume.vOut] return volumeByPort