Esempio n. 1
0
    def gen_package_info(self):
        """Writes a package-info.java file to the package directory with a high
        level description of the package functionality and requirements.

        """
        module = util.get_module(self.stmt).arg
        return ''.join([context.package_info.format(' ' + module, ''), self.pkg, ';'])
Esempio n. 2
0
 def cmd_help(self, what=None):
     if what is None or what == 'all':
         cmdlist = cmd.list + self.storage['extra_commands'].keys()
         if self.source_nick in config.ADMINS and what == 'all':
             yield ', '.join(cmd.list)
         else:
             yield ', '.join([x for x in cmd.list if not x.endswith("*")])
         return
     elif hasattr(self, 'cmd_%s' % what):
         # command
         f = self.parse_cmdline(what, True)[0]
         usage = getattr(f, 'usage', DUNNO)
         yield "%s: Usage: %s" % (what, usage)
         if hasattr(f, '__doc__') and f.__doc__ is not None:
             yield "%s: %s" % (what, f.__doc__)
     else: 
         # module
         try:
             mod = util.get_module(what)
             if hasattr(mod, '__doc__') and mod.__doc__ is not None:
                 yield '%s (module): %s' % (what, mod.__doc__.split("\n")[0])
             else:
                 yield '%s (module)' % what
         except ImportError:
             pass
Esempio n. 3
0
 def cmd_reload(self, what):
     if what == 'markov':
         start = time.time()
         reload(markov).rebuild()
         return "took %s seconds" % (time.time() - start)
     elif what == 'outputqueue':
         self.storage.output = util.get_module('outputqueue').Manager()
         return "now what?"
Esempio n. 4
0
 def create_alert(alert, alert_config):
     alert_class = self.alerts_mapping.get(alert) or get_module(alert)
     if not issubclass(alert_class, alerts.Alerter):
         raise EAException('Alert module %s is not a subclass of Alerter' % alert)
     missing_options = (rule['type'].required_options | alert_class.required_options) - frozenset(
         alert_config or [])
     if missing_options:
         raise EAException('Missing required option(s): %s' % (', '.join(missing_options)))
     return alert_class(alert_config)
Esempio n. 5
0
def get_versions():
    with open(CONFIG_PATH) as config_file:
        config = load(config_file)

    data = []
    for module in config['VERSIONING']:
        data.append({
            module['NAME']:
            get_module(module['PATH'], module['NAME']).__version__
        })

    return data
Esempio n. 6
0
 def find_cmd(cmdline, n):
     name = ('_'.join(cmdline.split(' ', n)[:n])).lower()
     if name in self.storage['extra_commands']:
         mod = util.get_module(self.storage['extra_commands'][name])
         method = lambda: getattr(mod, "cmd_" + name, None)(self)
     else:
         method = getattr(self, "cmd_" + name, None)
     
     
     if method:
         params = (cmdline + ' ').split(' ', n)[1].strip()
         results.append((method, params))
Esempio n. 7
0
    def __init__(self, stmt, path=None, package=None, src=None, ctx=None,
                 ns='', prefix_name='', yang_types=None, parent=None):
        """Constructor.

        stmt        -- A statement (sub)tree, parsed from a YANG model
        path        -- Full path to where the class should be written
        package     -- Name of Java package
        src         -- Filename of parsed yang module, or the module name and
                       revision if filename is unknown
        ctx         -- Context used to fetch option parameters
        ns          -- The XML namespace of the module
        prefix_name -- The module prefix
        yang_types  -- An instance of the YangType class
        parent      -- ClassGenerator to copy arguments that were not supplied
                       from (if applicable)

        """
        self.stmt = stmt
        self.path = path
        self.package = None if package is None else package.replace(os.sep, '.')
        self.src = src
        self.ctx = ctx
        self.ns = ns
        self.prefix_name = prefix_name
        self.yang_types = yang_types

        self.n = util.normalize(stmt.arg)
        self.n2 = util.camelize(stmt.arg)
        if stmt.keyword in context.module_stmts:
            self.filename = util.schema_class(util.normalize(util.search_one(stmt, 'prefix').arg)) + '.java'
            self.filename_visitor = util.visitor_class((util.normalize(util.search_one(stmt, 'prefix').arg))) + '.java'
        else:
            self.filename = self.n + '.java'
            self.filename_visitor = util.visitor_class(self.n) + '.java'

        if yang_types is None:
            self.yang_types = YangType()
        if parent is not None:
            for attr in ('package', 'src', 'ctx', 'path', 'ns',
                         'prefix_name', 'yang_types'):
                if getattr(self, attr) is None:
                    setattr(self, attr, getattr(parent, attr))

            module = util.get_module(stmt)
            if self.ctx.rootpkg:
                self.rootpkg = '.'.join([self.ctx.rootpkg.replace(os.sep, '.'),
                                         util.camelize(module.arg)])
            else:
                self.rootpkg = util.camelize(module.arg)
        else:
            self.rootpkg = package
Esempio n. 8
0
    def load_modules(self, rule, args=None):
        """ Loads things that could be modules. Enhancements, alerts and rule type. """
        # Set match enhancements
        match_enhancements = []
        for enhancement_name in rule.get('match_enhancements', []):
            if enhancement_name in dir(enhancements):
                enhancement = getattr(enhancements, enhancement_name)
            else:
                enhancement = get_module(enhancement_name)
            if not issubclass(enhancement, enhancements.BaseEnhancement):
                raise EAException("Enhancement module %s not a subclass of BaseEnhancement" % enhancement_name)
            match_enhancements.append(enhancement(rule))
        rule['match_enhancements'] = match_enhancements

        # Convert rule type into RuleType object
        if rule['type'] in self.rules_mapping:
            rule['type'] = self.rules_mapping[rule['type']]
        else:
            rule['type'] = get_module(rule['type'])
            if not issubclass(rule['type'], ruletypes.RuleType):
                raise EAException('Rule module %s is not a subclass of RuleType' % (rule['type']))

        # Make sure we have required alert and type options
        reqs = rule['type'].required_options

        if reqs - frozenset(rule.keys()):
            raise EAException('Missing required option(s): %s' % (', '.join(reqs - frozenset(rule.keys()))))
        # Instantiate rule
        try:
            rule['type'] = rule['type'](rule, args)
        except (KeyError, EAException) as e:
            raise EAException('Error initializing rule %s: %s' % (rule['name'], e)), None, sys.exc_info()[2]
        # Instantiate alerts only if we're not in debug mode
        # In debug mode alerts are not actually sent so don't bother instantiating them
        if not args or not args.debug:
            rule['alert'] = self.load_alerts(rule, alert_field=rule['alert'])
Esempio n. 9
0
    def cmd_module(self, action, modules):
        modules = modules and modules.split() or []
    
        for module in modules:
            try:
                if action == 'load' and module not in self.storage['modules']:
                    mod = util.get_module(module)
                    mod.load(self.handler)
                    
                    if getattr(mod, 'PROVIDES_COMMANDS', None):
                        for cmd in mod.PROVIDES_COMMANDS:
                            self.storage['extra_commands'][cmd] = module
                    
                    self.storage['modules'].append(module)
                    self.storage.touch()
                elif action == 'unload' and module in self.storage['modules']:
                    self.storage['modules'].remove(module)
                    self.storage.touch()
                    mod = util.get_module(module)
                    mod.unload(self.handler)
                    
                    if getattr(mod, 'PROVIDES_COMMANDS', None):
                        for cmd in mod.PROVIDES_COMMANDS:
                            if cmd in self.storage['extra_commands']:
                                del self.storage['extra_commands'][cmd]
                        self.storage.touch()
            except:
                yield "%s: %s" % (module, util.exc_lines()[-1])
            else:
                yield '%s: done' % module
 
        if action == 'list':
            modules = self.storage['modules']
            all = [x.replace(".py", "").replace("mods/", "") for x in glob.glob("mods/*.py")]
            all = [x + (x in modules and "*" or "") for x in all if not x.startswith("_")]
            yield ', '.join(sorted(all))
Esempio n. 10
0
    def generate_class(self):
        """Generates a Java class hierarchy providing an interface to a YANG
        module. Uses mutual recursion with generate_child.

        """
        stmt = self.stmt

        # If augment, add target module to context.augmented_modules dict
        if stmt.keyword == 'augment':
            if not hasattr(stmt, "i_target_node"):
                warn_msg = 'Target missing from augment statement'
                util.print_warning(warn_msg, warn_msg, self.ctx)
            else:
                target = stmt.i_target_node
                target_module = util.get_module(target)
                context.augmented_modules[target_module.arg] = target_module
            return  # XXX: Do not generate a class for the augment statement

        fields = OrderedSet()
        package_generated = False
        all_fully_qualified = True
        fully_qualified = False

        self.java_class = JavaClass(filename=self.filename,
                                    package=self.package,
                                    description=util.class_javadoc(self.ns, stmt),
                                    source=self.src,
                                    superclass=context.superclasses.get(stmt.keyword, 'YangElement'))

        # if (self.java_class.superclass == 'YangAnyXml'):
        #     print('Adding imports for ' + self.java_class.filename)
        #     self.java_class.imports.add('io.netconfessor.YangAnyXml')

        for ch in util.search(stmt, context.yangelement_stmts | {'leaf', 'leaf-list'}):
            field = self.generate_child(ch)
            ch_arg = util.normalize(ch.arg)
            if field is not None:
                package_generated = True
                if ch_arg == self.n and not fully_qualified:
                    fully_qualified = True
                    s = ('\n * <p>\n * Children with the same name as this ' +
                         'class are fully qualified.')
                    self.java_class.description += s
                else:
                    all_fully_qualified = False
                if field:
                    fields.add(field)  # Container child
                if (not self.ctx.opts.import_on_demand
                        or ch_arg in context.java_lang
                        or ch_arg in context.java_util
                        or ch_arg in context.io_netconfessor
                        or ch_arg in context.class_hierarchy[self.rootpkg]
                        or ch_arg in context.class_hierarchy[self.package]):
                    # Need to do explicit import
                    import_ = '.'.join([self.package, self.n2, ch_arg])
                    self.java_class.imports.add(import_)

        if self.ctx.opts.debug or self.ctx.opts.verbose:
            if package_generated:
                print('pkg ' + '.'.join([self.package, self.n2]) + ' generated')
            if self.ctx.opts.verbose:
                print('Generating "' + self.filename + '"...')

        gen = MethodGenerator(stmt, self.ctx)
        # print(stmt.keyword + ': ' + stmt.arg)
        for constructor in gen.constructors():
            self.java_class.add_constructor(constructor)

        for cloner in gen.cloners():
            self.java_class.add_cloner(cloner)

        if stmt.keyword in {'leaf', 'leaf-list'}:
            for value_setter in gen.leaf_value_access_methods():
                self.java_class.add_value_setter(value_setter)

        try:
            impl_methods = gen.gen.enum_holder_impl()
        except AttributeError:
            pass
        else:
            if impl_methods:
                self.java_class.add_interface_implementation('YangEnumerationHolder', 'io.netconfessor')
                for m in impl_methods:
                    self.java_class.add_support_method(m)

        try:
            lc_methods = gen.gen.list_container_impl()
        except AttributeError:
            pass
        else:
            if lc_methods:
                self.java_class.add_interface_implementation('YangContainer', 'io.netconfessor')
                for m in lc_methods:
                    self.java_class.add_support_method(m)

        support_method = gen.support_method(fields)
        if support_method is not None:
            self.java_class.add_support_method(support_method)

        self.java_class.add_name_getter(gen.key_names())
        self.java_class.add_name_getter(gen.children_names())

        if self.ctx.opts.import_on_demand:
            self.java_class.imports.add('io.netconfessor.*')
            self.java_class.imports.add('java.math.*')
            self.java_class.imports.add('java.util.*')
            if self.rootpkg != self.package:
                self.java_class.imports.add(self.rootpkg + '.*')
                top = util.get_module(self.stmt)
                if top is None:
                    top = self.stmt
                elif top.keyword == 'submodule':
                    top = util.search_one(top, 'belongs-to')
                top_classname = util.normalize(util.search_one(top, 'prefix').arg)
                if (top_classname in context.java_built_in
                        or top_classname in context.java_util):
                    top_import = self.rootpkg + '.' + top_classname
                    self.java_class.imports.add(top_import)
            if package_generated and not all_fully_qualified:
                import_ = '.'.join([self.package, self.n2, '*'])
                self.java_class.imports.add(import_)

        if stmt.keyword in {'container', 'list'}:
            self.java_class_visitor = JavaClass(
                abstract=True,
                filename=self.filename_visitor,
                package=self.package,
                description='Visitor of ' + stmt.keyword + ' ' + stmt.arg)

            self.generate_visitor(self.stmt)

        self.write_to_file()
Esempio n. 11
0
def pydocutize(output_dir, parent, nav_order):
    functions_mapping = {
        'Web': {
            'display_name': 'webweb.Web',
            'functions': [
                '__init__',
                'show',
                'save',
            ],
        },
        'Network': {
            'display_name': 'webweb.Network',
            'functions': [
                '__init__',
                'add_layer',
            ],
        }
    }

    util.clean_dir(output_dir)
    import pydoc
    import inspect
    webweb_module = util.get_module('webweb', WEBWEB_CODE)

    container = 'python'

    with open('python_documentation.md', 'r') as f:
        index_content = f.read()

    util.Index(
        title=container,
        writeable_title=container,
        nav_order=2,
        layout='main_page',
        has_children=True,
        parent=parent,
        content=index_content,
    ).write(output_dir)

    counter = 1
    for object_name in functions_mapping.keys():
        _object = getattr(webweb_module, object_name)

        object_display_name = functions_mapping[object_name]['display_name']

        for function_name in functions_mapping[object_name]['functions']:
            function_object = getattr(_object, function_name)
            function_signature = inspect.signature(function_object)

            # we want to exclude `self`
            parameters_list = []
            for parameter, parameter_string in function_signature.parameters.items():
                if parameter != 'self':
                    parameters_list.append(str(parameter_string))

            signature_string = "(" + ", ".join(parameters_list) + ")"

            function_doc = pydoc.getdoc(function_object)
            qualified_function_name = object_display_name
            writeable_function_name = object_display_name
            if function_name == '__init__':
                function_doc = pydoc.getdoc(_object) + "\n\n" + function_doc
            else:
                qualified_function_name += '.' + function_name
                writeable_function_name += '_' + function_name

            content = "```python\n{name}{signature}\n````\n\n{doc}".format(
                name=qualified_function_name,
                signature=signature_string,
                doc=function_doc,
            )

            util.Page(
                title=qualified_function_name,
                writeable_title=writeable_function_name,
                nav_order=counter,
                layout='main_page',
                parent=container,
                grand_parent=parent,
                content=content,
            ).write(output_dir)

            counter += 1
Esempio n. 12
0
                H,
                focal * args.scale,
                z_near,
                z_far,
                c=c * args.scale if c is not None else None).reshape(
                    -1, 8).to(device=device))  # ((NV-1)*H*W, 8)
            rays_spl = torch.split(all_rays, args.ray_batch_size,
                                   dim=0)  # Creates views
            poses = _poses_a = _poses_b = None
            focal = focal.to(device=device)

        n_gen_views = len(novel_view_idxs)

        util.get_module(renderer).net.encode(
            images[src_view_mask].to(device=device).unsqueeze(0),
            pri_poses.unsqueeze(0),
            focal, (z_near, z_far),
            c=c)

        all_rgb, all_depth = [], []
        for rays in tqdm.tqdm(rays_spl):
            rgb, depth = renderer(rays)
            rgb = rgb.cpu()
            depth = depth.cpu()
            all_rgb.append(rgb)
            all_depth.append(depth)

        all_rgb = torch.cat(all_rgb, dim=0)
        all_depth = torch.cat(all_depth, dim=0)
        all_depth = (all_depth - z_near) / (z_far - z_near)
        all_depth = all_depth.reshape(n_gen_views, H, W).numpy()