def __init__(self, configuration): Configurable.__init__(self, configuration) try: ## calibre plugin will set externally to match PI version. self.metadata = {"version": os.environ["CURRENT_VERSION_ID"]} except: self.metadata = {"version": "4.4"} self.replacements = [] self.in_ex_cludes = {} self.chapters = [] # chapters will be namedtuple of Chapter(url,title,html,etc) self.chapter_first = None self.chapter_last = None self.imgurls = [] self.imgtuples = [] # save processed metadata, dicts keyed by 'key', then (removeentities,dorepl) # {'key':{(removeentities,dorepl):"value",(...):"value"},'key':... } self.processed_metadata_cache = {} self.cover = None # *href* of new cover image--need to create html. self.oldcover = ( None ) # (oldcoverhtmlhref,oldcoverhtmltype,oldcoverhtmldata,oldcoverimghref,oldcoverimgtype,oldcoverimgdata) self.calibrebookmark = None # cheesy way to carry calibre bookmark file forward across update. self.logfile = None # cheesy way to carry log file forward across update. self.replacements_prepped = False
def __init__(self, configuration): Configurable.__init__(self, configuration) try: ## calibre plugin will set externally to match PI version. self.metadata = {'version': os.environ['CURRENT_VERSION_ID']} except: self.metadata = {'version': '4.4'} self.replacements = [] self.in_ex_cludes = {} self.chapters = [ ] # chapters will be namedtuple of Chapter(url,title,html,etc) self.chapter_first = None self.chapter_last = None self.imgurls = [] self.imgtuples = [] # save processed metadata, dicts keyed by 'key', then (removeentities,dorepl) # {'key':{(removeentities,dorepl):"value",(...):"value"},'key':... } self.processed_metadata_cache = {} self.cover = None # *href* of new cover image--need to create html. self.oldcover = None # (oldcoverhtmlhref,oldcoverhtmltype,oldcoverhtmldata,oldcoverimghref,oldcoverimgtype,oldcoverimgdata) self.calibrebookmark = None # cheesy way to carry calibre bookmark file forward across update. self.logfile = None # cheesy way to carry log file forward across update. self.replacements_prepped = False
def test_save_to_file(self): """ 1. create temp file with desired config (use default format) 2. create a ConfigurableAPI using that file as a config 3. delete the temp file 4. save the config 5. load the tempfile and check it's equal to the config """ temp_file = tempfile.NamedTemporaryFile(delete=False) formatter = FormatFactory.get_formatter() formatter.dump(self.test_config, temp_file) temp_file.close() conf_api = Configurable(local_file=temp_file.name) os.unlink(temp_file.name) conf_api.save() with open(temp_file.name, 'rb') as infile: conf_from_file = formatter.load(infile) self.assertEquals(self.test_config, conf_from_file) os.unlink(temp_file.name)
def __init__(self): Configurable.__init__(self, ROOT.mithep.Analysis, 'mithep.Analysis') self._sequence = None self._outputMods = [] self.isRealData = False self.book = '' self.dataset = '' self.custom = {}
def __init__(self): Configurable.__init__(self, ROOT.mithep.Analysis, 'mithep.Analysis') self._sequence = None self._outputMods = [] self.isRealData = False self.book = '' self.dataset = '' self.custom = {}
def test_get_with_flask_config(self): test_config = {'a': 1, 'b': {'c': 3, 'd': [True]}} conf_api = Configurable(defaults=test_config) # let's inject a flask config without having to make a whole app conf_api._config = {'DEBUG': True} # test flask _config existing get self.assertTrue(conf_api['DEBUG'])
def reset(self, *args, **kwargs): if self._sequence: # reset isBuilt status of all modules self._sequence.unbuild() Configurable.reset(self, *args, **kwargs) self._sequence = None self._outputMods = [] self.isRealData = False self.custom = {}
def reset(self, *args, **kwargs): if self._sequence: # reset isBuilt status of all modules self._sequence.unbuild() Configurable.reset(self, *args, **kwargs) self._sequence = None self._outputMods = [] self.isRealData = False self.custom = {}
def __init__(self, conf_path): # invoke the parent constructor Configurable.__init__(self, conf_path) # set variable values self.x0 = self.readCoord('width_start') self.y0 = self.readCoord('height_start') self.x1 = self.readCoord('width_end') self.y1 = self.readCoord('height_end')
def test_no_merge(self): base_conf = { 'a': 1, } merge_in = {} conf_api = Configurable(defaults=base_conf) changes = conf_api.merge_in_dict(merge_in) # Check the changes: there shouldn't be any self.assertEquals(0, len(changes)) self.assertEquals(base_conf, conf_api.config)
def test_nested_merge(self): base_conf = {'a': 1, 'b': {'c': [True]}} merge_in = {'b': {'d': {'f': {1, 2, 3}}}} conf_api = Configurable(defaults=base_conf) changes = conf_api.merge_in_dict(merge_in) expected = {'a': 1, 'b': {'c': [True], 'd': {'f': {1, 2, 3}}}} # Check the changes: 1 change where 'b.d' was set to {'f': {1, 2, 3}} self.assertEquals(1, len(changes)) self.assertEquals({'b.d': ({'f': {1, 2, 3}}, None)}, changes) self.assertEquals(expected, conf_api.config)
def test_top_level_merge(self): base_conf = {'a': 1} merge_in = {'b': 2} conf_api = Configurable(defaults=base_conf) changes = conf_api.merge_in_dict(merge_in) expected = {'a': 1, 'b': 2} # Check the changes: 1 change where 'b' went from None -> 2 self.assertEquals(1, len(changes)) self.assertEquals({'b': (2, None)}, changes) self.assertEquals(expected, conf_api.config)
def dumpPython(self, varName='analysis', withCtor=True, objects={}): if not self._sequence.isBuilt: self.buildSequence() code = '' modules = {} for mod in self._sequence: modules[mod] = mod._name objects.update(modules) # module names are guaranteed to be unique from buildSequence() for mod in self._sequence: code += mod.dumpPython(varName=mod._name, objects=objects) code += '\n' for mod in self._sequence: for nextNode in mod._nextNodes: code += mod._name + '.Add(' + nextNode._name + ')\n' code += '\n' code += Configurable.dumpPython(self, varName='analysis', withCtor=withCtor, objects=modules) return code
def test_set(self): test_conf = { 'a': 1, 'b': { 'c': '3', } } conf_api = Configurable(defaults=test_conf) conf_api['b']['e'] = 10 conf_api['d'] = {'r': {'a': [True]}} expected_conf = { 'a': 1, 'b': { 'c': '3', 'e': 10 }, 'd': { 'r': { 'a': [True] } } } self.assertEquals(expected_conf, conf_api.config)
def dumpPython(self, varName = 'analysis', withCtor = True, objects = {}): if not self._sequence.isBuilt: self.buildSequence() code = '' modules = {} for mod in self._sequence: modules[mod] = mod._name objects.update(modules) # module names are guaranteed to be unique from buildSequence() for mod in self._sequence: code += mod.dumpPython(varName = mod._name, objects = objects) code += '\n' for mod in self._sequence: for nextNode in mod._nextNodes: code += mod._name + '.Add(' + nextNode._name + ')\n' code += '\n' code += Configurable.dumpPython(self, varName = 'analysis', withCtor = withCtor, objects = modules) return code
def __init__(self, configuration): Configurable.__init__(self, configuration) try: ## calibre plugin will set externally to match PI version. self.metadata = {'version': os.environ['CURRENT_VERSION_ID']} except: self.metadata = {'version': '4.4'} self.in_ex_cludes = {} self.chapters = [ ] # chapters will be namedtuple of Chapter(url,title,html,etc) self.chapter_first = None self.chapter_last = None self.imgurls = [] self.imgtuples = [] self.cover = None # *href* of new cover image--need to create html. self.oldcover = None # (oldcoverhtmlhref,oldcoverhtmltype,oldcoverhtmldata,oldcoverimghref,oldcoverimgtype,oldcoverimgdata) self.calibrebookmark = None # cheesy way to carry calibre bookmark file forward across update. self.logfile = None # cheesy way to carry log file forward across update. ## Look for config parameter, split and add each to metadata field. for (config, metadata) in [("extracategories", "category"), ("extragenres", "genre"), ("extracharacters", "characters"), ("extraships", "ships"), ("extrawarnings", "warnings")]: for val in self.getConfigList(config): self.addToList(metadata, val) self.replacements = make_replacements( self.getConfig('replace_metadata')) in_ex_clude_list = [ 'include_metadata_pre', 'exclude_metadata_pre', 'include_metadata_post', 'exclude_metadata_post' ] for ie in in_ex_clude_list: ies = self.getConfig(ie) # print("%s %s"%(ie,ies)) if ies: iel = [] self.in_ex_cludes[ie] = set_in_ex_clude(ies)
def __init__(self, init_line): # invoke the parent initialisation method Configurable.__init__(self, self.OVR_PATH) # read whether the overlay is to be enabled self.enabled = self._setEnabled() if self.enabled: # read parametres from the config file self.opacity = self._setOpacity() self.position = self._setPosition() self.max_lines = self._setMaxLines() self.font = self._setFont() self.colors = self._setColors() self.root = self._setRoot(init_line) self.display = [] self.writeLine(init_line, 'info')
def idxs2str(indices): """""" shape = Configurable.tupleshape(indices) if len(shape) == 2: return ' '.join(':'.join(str(subidx) for subidx in index) if index[0] == Vocab.UNK else str(index[0]) for index in indices) elif len(shape) == 1: return ' '.join(str(index) for index in indices) elif len(shape) == 0: return '' else: raise ValueError('Indices should have len(shape) 1 or 2, not %d' % len(shape)) return
def __init__(self, configuration): Configurable.__init__(self, configuration) try: ## calibre plugin will set externally to match PI version. self.metadata = {'version':os.environ['CURRENT_VERSION_ID']} except: self.metadata = {'version':'4.4'} self.in_ex_cludes = {} self.chapters = [] # chapters will be namedtuple of Chapter(url,title,html,etc) self.chapter_first = None self.chapter_last = None self.imgurls = [] self.imgtuples = [] self.cover=None # *href* of new cover image--need to create html. self.oldcover=None # (oldcoverhtmlhref,oldcoverhtmltype,oldcoverhtmldata,oldcoverimghref,oldcoverimgtype,oldcoverimgdata) self.calibrebookmark=None # cheesy way to carry calibre bookmark file forward across update. self.logfile=None # cheesy way to carry log file forward across update. ## Look for config parameter, split and add each to metadata field. for (config,metadata) in [("extracategories","category"), ("extragenres","genre"), ("extracharacters","characters"), ("extraships","ships"), ("extrawarnings","warnings")]: for val in self.getConfigList(config): self.addToList(metadata,val) self.replacements = make_replacements(self.getConfig('replace_metadata')) in_ex_clude_list = ['include_metadata_pre','exclude_metadata_pre', 'include_metadata_post','exclude_metadata_post'] for ie in in_ex_clude_list: ies = self.getConfig(ie) # print("%s %s"%(ie,ies)) if ies: iel = [] self.in_ex_cludes[ie] = set_in_ex_clude(ies)
def test_get(self): test_config = {'a': 1, 'b': {'c': 3, 'd': [True]}} conf_api = Configurable(defaults=test_config) # test existing get self.assertEquals(test_config['a'], conf_api['a']) # test non-existing get with self.assertRaises(Exception): doesnt_exist = conf_api['e'] # test nested value get self.assertEquals([True], conf_api['b']['d'])
def test_load_from_file(self): """ 1. create temp file with desired config (use default format) 2. create a ConfigurableAPI using that file as a config TEST desired config is equal to the APIs config """ temp_file = tempfile.NamedTemporaryFile(delete=False) formatter = FormatFactory.get_formatter() formatter.dump(self.test_config, temp_file) temp_file.close() conf_api = Configurable(local_file=temp_file.name) os.unlink(temp_file.name) self.assertEquals(self.test_config, conf_api.config)
def test_app_wrap(self): test_config = {'a': 1, 'b': {'c': [True], 'd': 'wowowow'}} Configurable(flask_app=self.test_app, defaults=test_config) # 1. can we still get what the API delivers ? client = self.test_app.test_client() response = client.get('/hello') self.assertEquals("hello", response.data) # 2. can we access the /config/ route ? response = client.get('/config/') response_json = json.loads(response.data) self.assertEquals(response_json, test_config) # 3. can the underlying application use the .configuration ? response = client.get('/test_config') response_json = json.loads(response.data) self.assertEquals(response_json, test_config)
def test_get_default(self): test_config = {'a': 1, 'b': {'c': 3, 'd': [True]}} conf_api = Configurable(defaults=test_config) # test existing get self.assertEquals(test_config['a'], conf_api.get('a', 10)) # test non-existing get self.assertEquals(10, conf_api.get('e', 10)) # test nested value get self.assertEquals([True], conf_api.get('b').get('d')) # test nested value get defaulted self.assertEquals([False], conf_api.get('b').get('r', [False]))
def dumpPython(self, varName = 'analysis', objects = {}): if not self._sequence.isBuilt: self.buildSequence() code = '' auxObjects = {} modules = {} # module names are guaranteed to be unique from buildSequence() for mod in self._sequence: for attrName, args, isMethod in mod.config: for arg in args: if not isinstance(arg, Configurable): continue if arg in auxObjects: continue auxName = 'aux' + str(len(auxObjects)) code += arg.dumpPython(auxName, auxObjects) code += '\n' auxObjects[arg] = auxName code += mod.dumpPython(mod._name, auxObjects) code += '\n' modules[mod] = mod._name for mod in self._sequence: for nextNode in mod.nextNodes: code += mod._name + '.Add(' + nextNode._name + ')\n' code += '\n' code += Configurable.dumpPython(self, 'analysis', modules) return code
def __init__(self): # PREPARE LOGGING FIRST TO ENSURE # THE ABILITY TO OUTPUT ERROR LOGS # # create logging folders self._makeLogDirs() # initialise the logger self.logger = self._setLogger() # catch exceptions to log them try: # call parent class constructor Configurable.__init__(self, self.SLF_PATH) # initialise overlay self.overlay = Overlay(f'{self.PRG_NAME.upper()} {self.PRG_VERS}') # point pytesseract at tesseract installation self._setPyTesseract() # initialise a lobby reader self.lobby_reader = LobbyReader() # initialise a loot reader self.loot_reader = LootReader() # initialise a bounty reader self.bounty_reader = BountyReader() # initialise a data sender self.data_sender = DataSender() # read user from config file self.user = self._setUser() # read game patch self.patch = self._setGamePatch() # initialise screen capture as empty self.screen_capture = None # data holders self.bounty_data = {} self.lobby_data = {} self.loot_data = [] # exception counter for repeated issue handling self.exc_counter = 0 # welcome the user self.welcomeMessage() except Exception as e: # log the exception self.logger.error(str(e)) # shut down the app quit()
def __init__(self): Configurable.__init__(self, ROOT.mithep.Analysis, 'mithep.Analysis') self._sequence = None self.isRealData = False
def __init__(self, cppcls, clsName, *args, **kwargs): Configurable.__init__(self, cppcls, clsName, *args, **kwargs) Node.__init__(self, self._cppobj, self._cppobj.GetName())
from configurable import Configurable DHCPD_CONF = """ subnet %NETWORK% netmask %MASK% { range %STARTADDR% %ENDADDR%; option broadcast-address %BROADCAST%; option routers %GATEWAY%; default-lease-time 600; max-lease-time 7200; option domain-name "local"; option domain-name-servers %DNSSERVER%; } """ DHCPD = Configurable(DHCPD_CONF, [ "NETWORK", "MASK", "STARTADDR", "ENDADDR", "BROADCAST", "GATEWAY", "DNSSERVER" ], "/etc/dhcp/dhcpd.conf", "a") DHCP_ENABLE = Configurable('INTERFACES="%INT%"', ["INT"], "/etc/default/isc-dhcp-server", "a") INT_CONF = """ iface %INTERFACE% inet static address %IPADDR% netmask %MASK% """ INTERFACES = Configurable(INT_CONF, ["INTERFACE", "IPADDR", "MASK"], "/etc/network/interfaces", "a") HOSTAPD_CONF = """
def __init__(self, cppcls, clsName, *args, **kwargs): Configurable.__init__(self, cppcls, clsName, *args, **kwargs) Node.__init__(self, self._cppobj, self._cppobj.GetName())
def __getattr__(self, name): try: cls = getattr(self._core, name) except: # search for the class name in the compiled libraries try: cppname = '' for sup in self._superspaces: cppname += sup + '::' cppname += self._name + '::' + name mangled = '_ZN' for sup in self._superspaces: mangled += str(len(sup)) + sup mangled += str(len(self._name)) + self._name if '<' in name: # templated class - can only deal with simple templates op = name.find('<') if name.rfind('<') != op: raise Exception() cl = name.find('>') mangled += str(op) + 'INS_' + str(cl - op - 1) + name[op + 1:cl] + 'EE' else: mangled += str(len(name)) + name # mangled name now looks like '_ZN6mithep9OutputMod' libdirs = os.environ['LD_LIBRARY_PATH'].split(':') for libdir in libdirs: for libname in os.listdir(libdir): # only look at libraries that start with libMit or those linked manually if not libname.startswith( 'libMit' ) or libname in CppNamespace.loadedLibs: continue with open(libdir + '/' + libname, 'rb') as lib: cont = lib.read() if cont.find(mangled) < 0 and cont.find( cppname) < 0: # typedefs are written directly with cppname continue print '(mithep): Auto-loading library', libname ROOT.gSystem.Load(libname) CppNamespace.loadedLibs.append(libname) try: cls = getattr(self._core, name) break except: continue else: continue break else: raise Exception() except: print 'No class "' + name + '" found in namespace mithep. Perhaps a missing library?' sys.exit(1) # class full name with all namespaces clsName = '' for sup in self._superspaces: clsName += sup + '.' clsName += self._name + '.' + name try: tclass = cls.Class() except AttributeError: tclass = None if tclass: # this is a TObject configurable = Configurable if tclass.InheritsFrom(ROOT.mithep.BaseMod.Class()): configurable = Module ret = Configurable.Generator(cls, clsName, configurable) elif issubclass(type(cls), ROOT.PyRootType): # for now, all PyRootType that have MethodProxy attributes are treated as Configurable # would be nice in future to check for non-const methods (otherwise e.g. TMath is a Configurable) for attr in cls.__dict__.values(): if type(attr) is ROOT.MethodProxy: ret = Configurable.Generator(cls, clsName, Configurable) break else: # no MethodProxy -> this is a namespace ret = CppNamespace(cls, name, self._superspaces + [self._name]) else: # this is a simple variable ret = cls setattr(self, name, ret) return ret