Example #1
0
def try_import_on_name_error():
    try:
        method()
    except NameError as e:
        importable = ["os"]
        name = name_error(e)
        if name and name in importable:
            from importlib import __import__

            __import__(name)
            method()
Example #2
0
def import_from_dir(dirpath, module_name):
    """
    Import a module from a specified_directory.

    """
    if dirpath in sys.path:
        module = importlib.__import__(module_name)
    else:
        sys.path.insert(0, dirpath)
        try:
            module = importlib.__import__(module_name)
        finally:
            del sys.path[0]
    return module
Example #3
0
File: trans.py Project: Mause/pyalp
 def get_app(self, context):
     app_name = context['app_name']
     assert app_name, (
         "Did you make sure to set the context_instance "
         "to and instance of RequestContext"
     )
     return importlib.__import__(app_name)
Example #4
0
def worker(*args):
    while True:
        libraryOrFn = q.get()
        start = time.time()
        if libraryOrFn == False:
            q.task_done()
            break  # Die
        elif type(libraryOrFn) is str:
            try:
                part = importlib.__import__(libraryOrFn)
                log = part.main(*args)
                with lock:
                    print("> %7.5f for %s: %s" % (time.time() - start, libraryOrFn, log))
            except Exception as e:
                with lock:
                    print("Error for "+str(libraryOrFn)+": "+str(e))
                    traceback.print_exc()
            finally:
                q.task_done()
        else:
            try:
                log = libraryOrFn()
                with lock:
                    print("~ %7.5f for %s" % (time.time() - start, log))
            except Exception as e:
                with lock:
                    print("Error for "+str(libraryOrFn)+": "+str(e))
                    traceback.print_exc()
            finally:
                q.task_done()
Example #5
0
 def __add__(self, other):
     """Addition operation."""
     module = importlib.__import__(self.__class__.__module__)
     termsum = getattr(module, self._termsum_type)()
     termsum._add(self)
     termsum._add(other)
     return termsum
Example #6
0
 def load_outputs(self):
     # print("Loading slicers...")
     for output in self.outputs_list:
         if not output.startswith('_'):
             modulepath = "on.plugins.output." + output
             module = importlib.__import__(modulepath, fromlist=['OutputPlugin'])
             self.outputs[output] = module
Example #7
0
def _connect_db(config):
    ''' Return a DB connection. '''

    if config.type == 'postgres':
        try:
            psycopg2 = importlib.__import__('psycopg2')
        except ImportError as e:
            msg = 'psycopg2 module is required for Postgres.'
            raise click.ClickException(msg) from e

        try:
            return psycopg2.connect(
                host=config.host,
                port=config.port,
                user=config.user,
                password=config.password,
                database=config.schema
            )
        except Exception as e:
            if config.debug:
                raise
            else:
                err = 'Unable to connect to database: %s'
                raise click.ClickException(err % str(e)) from e
    else:
        raise ValueError('Database type "%s" not supported.' % config.type)
Example #8
0
 def load_slicers(self):
     # print("Loading slicers...")
     for slicer in self.slicers_list:
         if not slicer.startswith("_"):
             modulepath = "on.plugins.slicers." + slicer
             # print("\t" + modulepath)
             self.slicers[slicer] = importlib.__import__(modulepath, fromlist=["SlicerPlugin"])
Example #9
0
 def load_jauges(self):
     self.jauges = {}
     for jauge_name in self.jauge_names:
         mod, cls = jauge_name.split('.')
         _mod = importlib.__import__('jauges.' + mod,
                                     fromlist=(jauge_name,))
         self.jauges[jauge_name] = getattr(_mod, cls)(
             self.config[cls.upper()])
Example #10
0
    def search(root):
        nonlocal touched_paths, found_subclasses, here_path

        root = os.path.realpath(root)
        if root in touched_paths: return
        touched_paths.add(root)

        relpath = os.path.relpath(root, here_path)
        #print('  relpath: %s' % relpath)

        for path in glob.glob(os.path.join(root, '*')):
            if os.path.isdir(path):
                if not path.endswith('__pycache__'):
                    search(path)
                continue
            pyfile,ext = os.path.splitext(os.path.basename(path))
            if ext != '.py': continue
            if pyfile == '__init__': continue

            try:
                # a hack to turn a file path into a python import
                # TODO: find a better way!
                pyfile = os.path.join(relpath, pyfile)
                pyfile = re.sub(r'\\', '/', pyfile)
                if pyfile.startswith('./'): pyfile = pyfile[2:]
                level = pyfile.count('..')
                pyfile = re.sub(r'^(\.\./)*', '', pyfile)
                pyfile = re.sub('/', '.', pyfile)
                #print('    Searching: %s (%d, %s)' % (pyfile, level, path))
                try:
                    tmp = importlib.__import__(pyfile, globals(), locals(), [], level=level+1)
                except Exception as e:
                    se = str(e)
                    # pass over ignorable exceptions
                    if se == 'attempted relative import beyond top-level package':
                        # if symlink to another folder, this exception can get thrown
                        continue
                    print('Caught exception while attempting to search for classes')
                    print('  cls: %s' % str(cls))
                    print('  pyfile: %s' % pyfile)
                    print('  %s' % str(e))
                    continue
                for tk in dir(tmp):
                    m = getattr(tmp, tk)
                    if not inspect.ismodule(m): continue
                    for k in dir(m):
                        v = getattr(m, k)
                        if not inspect.isclass(v): continue
                        if v is cls: continue
                        if not issubclass(v, cls): continue
                        # v is a subclass of cls, so add it to the global namespace
                        #print('      Found %s in %s' % (str(v), pyfile))
                        globals()[k] = v
                        found_subclasses.add(v)
            except Exception as e:
                print('Exception occurred while searching %s' % path)
                debugger.print_exception()
Example #11
0
def email_tool(self,args):
    addon = False
    if os.path.exists('tools_functions\\address_book.py'):
        address_book = importlib.__import__('address_book')
        addon = True
    message = input('Enter message body: ')
    msg = MIMEText(message)
    msg['Subject'] = 'Sent from %s' % sys.argv[0]
    if args[0] == 'contact' and addon:
        if len(args) > 2:
            while len(args) > 2:
                args[1] = args[1] + ' ' + args.pop(2)
        name = args[1]
        info = address_book.export(name,'email')
        if info == -1:
            print('Error: contact not found')
            return;
        else:
            msg['To'] = info
    elif args[0] == 'contact' and not addon:
        print('Error: address_book module not found')
        return;
    else:
        msg['To'] = args[0]
    msg['From'] = input('\'From\' address: ')
    mailHost = msg['From']
    mailHost = mailHost[mailHost.index('@')+1:]
    try:
        s = smtplib.SMTP(serverDict[mailHost][0],serverDict[mailHost][1])
    except KeyError:
        print('Error: could not find server for ' + mailHost)
    s.ehlo()
    s.starttls()
    if self.simpleDecrypt(self.settingsList['password'][0]) is '':
     try:
        s.login(msg['From'], getpass.getpass('password:'******'Invalid password: '******'quit':
          try:
            s.login(msg['From'], password)
            password = '******'
          except smtplib.SMTPAuthenticationError:
            password = getpass.getpass('Invalid password: '******'From'], self.simpleDecrypt(self.settingsList['password'][0]))
     except smtplib.SMTPAuthenticationError:
        password = getpass.getpass('Error: unrecognized password in "settings.tool". Password: '******'quit':
          try:
            s.login(msg['From'], password)
            s.send_message(msg)
            password = '******'
          except smtplib.SMTPAuthenticationError:
            password = getpass.getpass('Invalid password: ')
    s.quit()
    def _load_plugins(self, config, script_path, reload=False):

        # all_script = os.listdir(script_path)
        if reload or not os.path.isdir(cfg.EXEC_TMP):
            shutil.rmtree(cfg.EXEC_TMP, ignore_errors=True)
            def ignore(src, names):
                    return [name for name in names if "pyc" in name]
            shutil.copytree(script_path, cfg.EXEC_TMP, ignore=ignore)
            #subprocess.call(["/usr/bin/env", "2to3-3.4",  "-w", cfg.EXEC_TMP])
            subprocess.call(["/usr/bin/env", "2to3",  "-w", cfg.EXEC_TMP]) #DEBUG

        modules = pkgutil.iter_modules([cfg.EXEC_TMP])
        sys.path.insert(0, "{}/../".format(cfg.EXEC_TMP))
        sys.path.insert(0, "{}/".format(cfg.EXEC_TMP))
        importlib.__import__(cfg.SCT_TMP_PKG, globals(), locals(), [], 0)
        sct_tools = []
        for loader, mod_name, ispkg in modules:
            module = importlib.import_module('.'+mod_name, package=cfg.SCT_TMP_PKG)
            get_parser = getattr(module, cfg.GET_PARSER, None)
            if get_parser:
                parser = get_parser()
                options = {}
                for i, o in enumerate(parser.options.values()):
                    if not getattr(o, cfg.OPTION_DEPRECATED, None):
                        options.update({o.name: {k: v for k, v in o.__dict__.items()
                                                 if k in cfg.OPTION_TRANSMIT}})
                        #add a value key for user parameters
                        options[o.name]['value']=None
                        #add section handling
                        parser.usage.section[1] = "Main Config"
                        x = parser.usage.section
                        sorted_x = sorted(x.items(), key=operator.itemgetter(0))
                        for i in sorted_x:
                            if options[o.name]['order'] >= i[0]:
                                options[o.name]['section'] = i[1]

                # options.sort(key=lambda e: e[cfg.OPTION_ORDER])
                sct_tools.append(models.RegisteredTool(name=mod_name,
                                                       help_str=parser.usage.description,
                                                       options=options))

        return sct_tools
Example #13
0
def set_repo(mode='repo_mysql'):
	global importlib
	if importlib is None:
		import importlib
	global default_storage
	try:
		default_storage=getattr(importlib.__import__(mode),mode)
		print("default repo changed to {}".format(mode))
	except AttributeError:
		print("class name in the module should be: {}".format(mode))
	except ImportError:
		print("module name '{}' not found".format(mode))
Example #14
0
def destroydb(_ENGINE_NAME):
    global ENGINE_NAME
    global Session

    assert ENGINE_NAME is None
    assert Session is None

    assert 'test' in _ENGINE_NAME

    engine = create_engine(_ENGINE_NAME, echo=False, pool_recycle=300)
    tables = walk_mods( importlib.__import__('n6wells').__path__ )
    Base.metadata.drop_all(bind=engine)
Example #15
0
def set_configuration_file(config_file):
    """ Extract a config based on the contents of a python file. The file should contain a dict named
    parsimony_configuration with keys from this module.
    
    :param config_file:  path to the config file
    """
    
    module_name, extension = os.path.splitext(config_file)
    module_path,module_name = os.path.split(module_name)
    sys.path.append(module_path)
    config_module = importlib.__import__(module_name)
    
    update_configuration(**config_module.parsimony_configuration)
Example #16
0
 def isDynamicBlock(path):
     """ Check if the file refers to a dynamic block
         It returns the module if it is a dynamic block
     """
     if os.path.splitext(path)[1] == ".py":
         name = os.path.splitext(os.path.split(path)[1])[0]
         mod = importlib.__import__(name)
         try:
             if mod.__isBlock__:
                 return mod
         except:
             return False
     return False
Example #17
0
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            if key in System._attributes:
                setattr(self, key, value)
            else:
                raise AttributeError("Unexpected kwarg {}.".format(key))

        self._module = importlib.__import__(self.__class__.__module__)
        if "hamiltonian" not in self.__dict__:
            self.hamiltonian = 0
        if "dynamics" not in self.__dict__:
            self.dynamics = 0

        self.drive_number = 0
def load(name):
	try:
		obj = __import__(name)
		reload(obj)
		return obj
	except:
		pass

	try:
		import importlib
		obj = importlib.__import__(name)
		importlib.reload(obj)
		return obj
	except:
		pass
Example #19
0
    def import_name_errors(*args, **kwargs):
        try:
            method(*args, **kwargs)
        except NameError as e:
            name = name_error(e)
            if not name:
                return False
            if name in sys.modules:
                return False
            from importlib import __import__

            module = __import__(name)
            globals()[name] = module
            method(*args, **kwargs)
            if name not in importable:
                importable[name] = module
Example #20
0
    def reload(cls):
        cls.verboseLog(cls._tr("Reloading plugins"),
                       "pyTSon.PluginHost.reload")
        # stop all running modules
        for key, p in cls.active.items():
            try:
                p.stop()
            except:
                logprint(cls._tr("Error stopping python plugin {name}: "
                                 "{trace}").format(name=key,
                                                   trace=traceback.
                                                   format_exc()),
                         ts3defines.LogLevel.LogLevel_ERROR,
                         "pyTSon.PluginHost.reload")

        cls.active = {}
        cls.plugins = {}

        # import all modules
        spath = pytson.getPluginPath("scripts")
        for d in glob.glob(os.path.join(spath, "*/")):
            if not os.path.isdir(d):
                continue

            base = os.path.relpath(d, spath)
            try:
                if base in cls.modules:
                    cls.modules[base] = importlib.reload(cls.modules[base])
                else:
                    cls.modules[base] = importlib.__import__(base)
            except:
                logprint(cls._tr(
                         "Error loading python plugin from {path}: {trace}").
                         format(path=d, trace=traceback.format_exc()),
                         ts3defines.LogLevel.LogLevel_ERROR,
                         "pyTSon.PluginHost.reload")

        # save local menu ids
        for globid, (p, locid) in cls.menus.items():
            # previously reloaded?
            if not type(p) is str:
                cls.menus[globid] = (p.name, locid)

        # save local hotkeys
        for keyword, (p, lockey) in cls.hotkeys.items():
            if not type(p) is str:
                cls.hotkeys[keyword] = (p.name, lockey)
Example #21
0
def import_or_die(module_name, entrypoint_names):
    '''
    Import user code; return reference to usercode function.

    (str) -> function reference
    '''
    log_debug("Importing {}".format(module_name))
    module_name = os.path.abspath(module_name)
    if module_name.endswith('.py'):
        module_name,ext = os.path.splitext(module_name)
    modname = os.path.basename(module_name)
    dirname = os.path.dirname(module_name)
    if dirname and dirname not in sys.path:
        sys.path.append(dirname)

    # first, try to reload code
    if modname in sys.modules:
        user_module = sys.modules.get(modname)
        user_module = importlib.reload(user_module)
    # if it isn't in sys.modules, load it for the first time, or
    # try to.
    else:
        try:
            mypaths = [ x for x in sys.path if ("Cellar" not in x and "packages" not in x)]
            # print("Loading {} from {} ({})".format(modname, dirname, mypaths))
            # user_module = importlib.import_module(modname)
            user_module = importlib.__import__(modname)
        except ImportError as e:
            log_failure("Fatal error: couldn't import module (error: {}) while executing {}".format(str(e), modname))
            raise ImportError(e)

    # if there aren't any functions to call into, then the caller
    # just wanted the module/code to be imported, and that's it.
    if not entrypoint_names:
        return

    existing_names = dir(user_module)
    for method in entrypoint_names:
        if method in existing_names:
            return getattr(user_module, method)

    if len(entrypoint_names) > 1:
        entrypoints = "one of {}".format(', '.join(entrypoint_names))
    else:
        entrypoints = entrypoint_names[0]
    raise ImportError("Required entrypoint function or symbol ({}) not found in your code".format(entrypoints))
Example #22
0
def emailToSMS(self, args):
    addon = False
    if os.path.exists('tools_functions\\address_book.py'):
        address_book = importlib.__import__('address_book')
        addon = True
    carrierCodes = {
    'sprint': '@messaging.sprintpcs.com',
    'verizon': '@vtext.com',
    't-mobile': '@tmomail.net',
    'at&t': '@txt.att.net',
    'virgin mobile': '@vmobl.com',
    'us cellular': '@email.uscc.net',
    'nextel': '@messaging.nextel.com',
    'boost': '@myboostmobile.com',
    'alltel': '@message.alltel.com'}
    if args[0] == 'contact' and addon:
        if len(args) > 2:
            while len(args) > 2:
                args[1] = args[1] + ' ' + args.pop(2)
        name = args[1]
        info = address_book.export(name,'phone')
        if info == -1:
            print('Error: contact not found')
            return;
        else:
            number, carrier = info
    elif args[0] == 'contact' and not addon:
        print('Error: address_book module not found')
        return;
    else:
        number = args[0]
        carrier = args[1]
    couldFindCarrier = True
    if carrier not in carrierCodes:
        couldFindCarrier = False
        print('Error: unrecognized carrier.')
    else:
        toAddress = number + carrierCodes[carrier]
        fromAddress = input('\'From\' address: ')
        password = getpass.getpass()
        messages = parseTXT(input('Message: '))
        for segment in messages:
            send(self, segment, fromAddress, password, toAddress)
Example #23
0
def register_tables( engine  ):
    """
    simply walk the package hierarchy, thus ensuring that declarative
    classes are registered

    currently, recursion is disabled, as it winds up walking everything
    thats ever imported, so make sure your models are exposed in the root
    package namespace (ie, n6wells.foo)
    """
    tables = walk_mods( importlib.__import__('n6wells').__path__ )
    for tname, t in Base.metadata.tables.items():
        for fk in t.foreign_keys:
            fk.use_alter=True
            fk.constraint.use_alter=True

    Base.metadata.create_all(bind=engine)

    for tablename, cls in tables.items():
        print('@@ tablename=%s, cls=%s' % (tablename, cls))
        globals()[tablename] = cls
        __all__.append( tablename )
 def init_functions(self):
     if not os.path.isdir(self.toolsRoute):
         print('tools_functions directory not found. Empty directory will be created.\n')
         os.mkdir(self.toolsRoute)
         return;
     sys.path.insert(0,self.toolsRoute)
     importList = os.listdir(self.toolsRoute)
     for imp in importList:
         if (imp[-3:]) == '.py':
             module = imp[:-3]
             obj = importlib.__import__(module) #this line causes mac specific problems
             #possibilities include a difference in filenames (I'm pulling out '.py'), directory structure (I don't think so)
             #or the fact that I'm explicitly doing an importlib.__import__ but thsi also shouldn't matter
             #also, possibly a redundant import of the address book, although I'm not sure
             try:
                 self.functions[getattr(obj, 'func_alias')] = getattr(obj, 'func_info')
                 settings = getattr(obj, 'settings', False)
                 if settings:
                     for key in settings:
                         self.settingsList[key] = settings[key]
             except AttributeError:
                 print('Failed to load ' + imp + ' because it is not a properly formatted tools\nmodule. Please ensure it is written to template (check the readme).')
Example #25
0
File: parser.py Project: sopnic/ybk
def parse(site):
    rabbrs = {v: k for k, v in ABBRS.items()}
    abbr = rabbrs[site]
    parser = importlib.__import__('ybk.parsers.{}'.format(site),
                                  fromlist=['Parser']).Parser()
    log.info('解析交易所 {}'.format(abbr))
    num_parsed = 0
    num_failed = 0
    for a in Announcement.query({'exchange': abbr,
                                 'parsed': {'$ne': True}}):
        log.info('parsing {}'.format(a.url))
        try:
            for c in parser.parse(a.type_, a.html):
                c['from_url'] = a.url
                Collection(c).upsert()
            a.update({'$set': {'parsed': True}})
            num_parsed += 1
        except Exception as e:
            num_failed += 1
            if not isinstance(e, NotImplementedError):
                log.exception('解析错误')
            continue

    log.info('解析完毕, {}个成功, {}个失败'.format(num_parsed, num_failed))
Example #26
0
def check_qudi_modules(filelist):
    from core.base import Base
    from core.util.interfaces import InterfaceMetaclass
    from gui.guibase import GUIBase
    from logic.generic_logic import GenericLogic
    from logic.generic_task import InterruptableTask, PrePostTask

    othererror = []
    importerror = []
    importsuccess = []
    modules = {
        'hardware': {},
        'logic': {},
        'gui': {},
        'interface': {},
        'itask': {},
        'pptask':{}
    }
    for f in filelist:
        try:
            mod = importlib.__import__(f, fromlist=['*'])
            importsuccess.append(f)
            thinglist = dir(mod)
            for thingname in thinglist:
                path = '{}.{}'.format(f, thingname)
                thing = getattr(mod, thingname)
                try:
                    if not inspect.isclass(thing):
                        continue
                    if issubclass(thing, GenericLogic) and thingname != 'GenericLogic':
                        modules['logic'][path] = Module(
                            path,
                            thingname,
                            thing,
                            [(i,v) for i, v in thing._connectors.items()],
                            [thingname]
                            )
                    elif issubclass(thing, GUIBase) and thingname != 'GUIBase':
                        modules['gui'][path] = Module(
                            path,
                            thingname,
                            thing,
                            [(i,v) for i, v in thing._connectors.items()],
                            [thingname]
                            )
                    elif issubclass(thing, InterruptableTask) and thingname != 'InterruptableTask' :
                        modules['itask'][path] = {'pause': [i for i in thing.pauseTasks]}
                    elif issubclass(thing, PrePostTask) and thingname != 'PerPostTask':
                        modules['pptask'][path] = {}
                    elif (issubclass(thing, Base)
                        and thingname != 'Base'
                        and thingname != 'GenericLogic'
                        and thingname != 'GUIBase'
                        ):
                        modules['hardware'][path] = Module(
                            path,
                            thingname,
                            thing,
                            [(i,v) for i, v in thing._connectors.items()],
                            [thingname]
                            )
                    elif (f.startswith('interface')
                        and not issubclass(thing, Base)
                        and issubclass(thing.__class__, InterfaceMetaclass)
                        ):
                        modules['interface'][path] = thing
                    else:
                        pass
                except Exception as e:
                    pass
        except ImportError as e:
            importerror.append([f, e])
        except Exception as e:
            othererror.append([f, e])

    for base in ['hardware', 'logic', 'gui']:
        for modpath, module in modules[base].items():
            for ifname, interface in modules['interface'].items():
                n = ifname.split('.')[-1]
                if issubclass(module.reference, interface):
                    module.interfaces.append(n)

    return modules, importsuccess, importerror, othererror
Example #27
0
 def mock__import__(name, globals_=None, locals_=None, fromlist=(), level=0):
     if level == 0 and name.partition('.')[0] == 'kubernetes':
         raise ImportError("No module named 'kubernetes'")
     return importlib.__import__(name, globals=globals_, locals=locals_, fromlist=fromlist, level=level)
import sys
import importlib
import boto3
from boto3.session import Session
try:    
    a=sys.argv[1]
    props=importlib.__import__(a)
    ofile=open(props.OUTPUT_FILE_PATH, "w+")
    aws_access=props.ACCESS_KEY
    aws_secret=props.SECRET_KEY
    reg_name=props.REGION
    node_count = props.NODES_COUNT
    ins_type=props.NODES_TYPE
    key_name=props.KEY_PAIR_NAME
    #Starting session
    session = Session(aws_access_key_id=aws_access,
                          aws_secret_access_key=aws_secret,
                          region_name=reg_name)
    print("Session Started")

    ec2 = session.resource('ec2')
    ec2client=session.client('ec2')

    if(ins_type=='ubuntu'):
        image_id='ami-9abea4fb'
    elif(ins_type=='redhat'):
        image_id='ami-775e4f16'
    elif(ins_type=='windows'):
        image_id='ami-83a5bce2'
    elif(ins_type=='suse'):
        image_id='ami-d2627db3'
 def __call__(self, *args, **kwargs):
     if args[0] == 'threading':
         raise ImportError
     else:
         return importlib.__import__(*args, **kwargs)
Example #30
0
def runfsm(module_name, running_modules=dict()):
    """runfsm('modname') reloads that module and expects it to contain
    a class of the same name. It calls that class's constructor and then
    calls the instance's start() method."""

    global running_fsm
    robot = NewFSM.robot
    if running_fsm:
        stopAllMotors()
        running_fsm.stop()

    r_py = re.compile('.*\.py$')
    if r_py.match(module_name):
        print("\n'%s' is not a module name. Trying '%s' instead.\n" %
              (module_name, module_name[0:-3]))
        module_name = module_name[0:-3]

    found = False
    try:
        reload(running_modules[module_name])
        found = True
    except KeyError:
        pass
    except:
        raise
    if not found:
        try:
            running_modules[module_name] = __import__(module_name)
        except ImportError as e:
            print("Error loading %s: %s.  Check your search path.\n" %
                  (module_name, e))
            return
        except Exception as e:
            print('\n===> Error loading %s:' % module_name)
            raise

    py_filepath = running_modules[module_name].__file__
    fsm_filepath = py_filepath[0:-2] + 'fsm'
    try:
        py_time = datetime.datetime.fromtimestamp(
            os.path.getmtime(py_filepath))
        fsm_time = datetime.datetime.fromtimestamp(
            os.path.getmtime(fsm_filepath))
        if py_time < fsm_time:
            cprint(
                'Warning: %s.py is older than %s.fsm. Should you run genfsm?' %
                (module_name, module_name),
                color="yellow")
    except:
        pass

    # The parent node class's constructor must match the module name.
    the_module = running_modules[module_name]
    the_class = the_module.__getattribute__(module_name) \
                if module_name in dir(the_module) else None
    # if not isinstance(the_class,type) or not issubclass(the_class,StateNode):
    #     cprint("Module %s does not contain a StateNode class named %s.\n" %
    #           (module_name, module_name), color="red")
    #     return
    # print("StateMachineProgram robot in runfsm: {}".format(StateMachineProgram.robot))
    running_fsm = the_class()
    the_module.robot = robot
    the_module.world = the_module.robot.world
    the_module.charger = the_module.robot.world.charger
    the_module.cube = the_module.robot.world.connected_light_cube
    # StateMachineProgram.robot = robot
    # Class's __init__ method will call setup, which can reference the above variables.
    running_fsm.robot = robot
    cli_globals = globals()
    cli_globals['running_fsm'] = running_fsm
    robot.conn.loop.call_soon(running_fsm.start)
    return running_fsm
Example #31
0
import sys
import logging
import importlib

import discord
from discord.ext import commands

from Framework import general_check

var_config = importlib.__import__("Config.var_config_" + sys.argv[1],
                                  fromlist=("var_config_" + sys.argv[1]))


class Excuse(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(aliases=['excuse', 'ex'])
    async def _excuse(self, ctx, *args: commands.Greedy[discord.Member]):
        await excuse(ctx, args)

    @commands.command(aliases=['unexcuse', 'unex', 'ux'])
    async def _unexcuse(self, ctx, *args: commands.Greedy[discord.Member]):
        await unexcuse(ctx, args)


async def excuse(ctx: discord.ext.commands.Context, args):
    ismod = ctx.author.guild_permissions.administrator or ctx.author.guild_permissions.manage_roles or ctx.author.guild_permissions.manage_guild
    if not await general_check.voice_channel_command_check(ctx):
        return
    # check if a user is practicing in the voice channel and if the user that executed the command is currently the one practicing in this voice channel, or a moderator
Example #32
0
import importlib
a = importlib.import_module('os')
b = importlib.import_module('pickle')
c = importlib.__import__('sys')
d = importlib.__import__('subprocess')
Example #33
0
    predict_parser.add_argument('--show-plot',
                                action='store_true',
                                dest='show_plot')
    predict_parser.add_argument('--save-plot',
                                action='store_true',
                                dest='save_plot')
    args = parser.parse_args()
    print(args)
    if args.log_filter:
        log.addFilter(CMDFilter(args.log_filter))

    ROOT_DIR = initialize_task(args.hpconfig)

    sys.path.append('.')
    print(sys.path)
    HPCONFIG = importlib.__import__(args.hpconfig.replace('.py', ''))
    config.HPCONFIG = HPCONFIG.CONFIG
    config.ROOT_DIR = ROOT_DIR
    config.NAME = SELF_NAME
    print('====================================')
    print(ROOT_DIR)
    print('====================================')

    if config.CONFIG.flush:
        log.info('flushing...')
        dataset = load_data(config, filename=config.HPCONFIG.dataset_path)
        pickle.dump(dataset,
                    open('{}/{}__cache.pkl'.format(ROOT_DIR, SELF_NAME), 'wb'))
    else:
        dataset = pickle.load(
            open('{}/{}__cache.pkl'.format(ROOT_DIR, SELF_NAME), 'rb'))
Example #34
0
    # ToDo not yet ready for prime time
    acq_e_h = None
    #if args.delete:
    #  wm=pyinotify.WatchManager()
    #  wm.add_watch(args.base_path,pyinotify.ALL_EVENTS, rec=True)
    #  acq_e_h=EventHandler(lg=logger,fn=args.acquired_positions)
    #  nt=pyinotify.ThreadedNotifier(wm,acq_e_h)
    #  nt.start()

    # ToD revisit dynamical loding
    # now load meteo class ...
    mt = importlib.import_module('meteo.' + args.meteo_class)
    meteo = mt.Meteo(lg=logger)
    # ... and device, ToDo revisit
    mod = importlib.__import__('u_point.devices', fromlist=[args.device_class])
    Device = getattr(mod, args.device_class)
    quick_analysis = None
    if args.do_quick_analysis:
        quick_analysis = QuickAnalysis(
            lg=logger,
            ds9_display=args.ds9_display,
            background_max=args.background_max,
            peak_max=args.peak_max,
            objects_min=args.objects_min,
            exposure_interval=args.exposure_interval,
            ratio_interval=args.ratio_interval)

    px_scale = args.pixel_scale / 3600. / 180. * np.pi  # arcsec, radian

    obs = EarthLocation(lon=float(args.obs_lng) * u.degree,
    if 'aug_mixup_alpha' not in conf:
        conf.aug_mixup_alpha = 1.0
    if 'samples_per_file' not in conf:
        conf.samples_per_file = 1
    if 'eval_ensemble' not in conf:
        conf.eval_ensemble = True  # Set False if samples_per_file > 1 but ensemble is not available
    if 'what_is_sample' not in conf:
        conf.what_is_sample = 'log mel-spectrogram'
    if 'use_audio_training_model' not in conf:
        conf.use_audio_training_model = True


#module dynamic import from variable 'moduleName' from config.py
import importlib
from config import *
module = importlib.__import__(moduleName, fromlist=['conf'])
conf = getattr(module, 'conf')
auto_complete_conf(conf)
print(conf)


# # Data utilities
def load_labels(conf):
    conf.labels = load_npy(conf, 'labels.npy')
    auto_complete_conf(conf)
    print('Labels are', conf.labels)


def datapath(conf, filename):
    return conf.folder / filename
Example #36
0
import unittest
import sys
import os

# Stampa un'intestazione che descrive l'esercizio
lista_delle_funzioni = "\n".join("  - " + f for f in ex_functions)
print(__doc__.format(ex_name, lista_delle_funzioni))

errori_preliminari = False  # il file ha la giusta struttura?

# Importa le soluzioni degli esercizi
print("CONTROLLO PRELIMINARE DEL FILE {0}.py:".format(ex_name))

lab = None
try:
    lab = importlib.__import__(ex_name)
except Exception:
    msg = """
    Problema a importare il file {0}.py:
    -- il file potrebbe non essere presente in questa cartella,
    -- oppure potrebbe contenere errori che lo rendono non eseguibile.
    
    Provate ad eseguire il comando 'python3 {0}.py' da terminale per
    avere più informazioni. """.format(ex_name)
    print(msg)
    errori_preliminari = True
else:
    #Carica nel namespace le funzioni definite
    f_objects = {}
    for f_name in ex_functions:
        try:
Example #37
0
def import_(*args, **kwargs):
    """Delegate to allow for injecting different implementations of import."""
    if using___import__:
        return __import__(*args, **kwargs)
    else:
        return importlib.__import__(*args, **kwargs)
Example #38
0
from __future__ import print_function  # makes this work for python2 and 3

import gvar as gv
import corrfitter as cf


def main():
    data, basis = make_data('etab.h5')
    s = gv.dataset.svd_diagnosis((data, 113), models=make_models())
    print('svdcut =', s.svdcut)
    s.plot_ratio(show=True)


import importlib
import sys
if sys.version_info > (2, ):
    etab_alt = importlib.import_module('etab-alt')
else:
    etab_alt = importlib.__import__('etab-alt')
make_models = etab_alt.make_models
make_data = etab_alt.make_data

if __name__ == '__main__':
    main()