コード例 #1
0
def index(request):

    entries = Entry.objects.all()

    entries = filter_checked(Language, request.GET, entries, "language")
    entries = filter_checked(LanguageVariant, request.GET, entries, "variant")

    bad_terms = []

    word = 'word' in request.GET and request.GET['word']
    definition = 'definition' in request.GET and request.GET['definition']

    if word:
        entries = search(entries, request.GET['word'].split(' '), 'word',
                         'word', bad_terms)
    if definition:
        entries = search(entries, request.GET['definition'].split(' '),
                         'definition', 'word', bad_terms)

    if bad_terms or (not word and not definition):
        entries = []

    context = {
        'entries': entries,
        'languages': Language.objects.all(),
        'variants': LanguageVariant.objects.all(),
    }

    context.update(base_context(request))

    context.update(request.GET)  # enable refilling the form

    return render(request, 'dictionary/index.html', context)
コード例 #2
0
ファイル: controllers.py プロジェクト: agrover/BandRadar
    def search(self, search, tg_errors=None):
        venues = util.search(Venue, search["text"], tg_errors)
        artists = util.search(Artist, search["text"], tg_errors)

        # if only one search result, redirect to it immediately
        if venues.count() == 1 and artists.count() == 0:
            redirect("/venues/%d" % venues[0].id)
        if venues.count() == 0 and artists.count() == 1:
            redirect("/artists/%d" % artists[0].id)

        return dict(venues=venues, artists=artists)
コード例 #3
0
ファイル: search.py プロジェクト: jmcb/jquizzyva
def main(args, cgi_args):
    if not (cgi_args.has_key("s") or cgi_args.has_key("w")
            or cgi_args.has_key("v") or cgi_args.has_key("d")):
        jsond()
        print json.dumps("No search terms provided.")
        return

    lexicon = cgi_args.getfirst("lexicon", None)

    if lexicon is None:
        lexicon = cgi_args.getfirst("l", None)

    if lexicon is None:
        jsond()
        print json.dumps("No lexicon provided.")
        return

    lex = util.db.lexicon(lexicon)

    if lex is None:
        jsond()
        print json.dumps("Invalid lexicon '%s'" % lexicon)

    if cgi_args.has_key("d"):
        return load(args, cgi_args, lex)
    elif cgi_args.has_key("v"):
        return save(args, cgi_args, lex)
    elif cgi_args.has_key("s"):
        return search(args, cgi_args, lex)
    elif cgi_args.has_key("w"):
        return challenge(args, cgi_args, lex)
    else:
        jsond()
        print json.dumps("Nothing.")
コード例 #4
0
def report(mail: imaplib.IMAP4) -> List[Any]:
    return [
        x
        for x in [
            get_tls_report(m) for m in search(mail, '(HEADER TLS-Report-Domain "")')
        ]
    ]
コード例 #5
0
def home():
    if request.method == "GET":
        return render_template("home.html")
    else:
        button = request.form["button"]
        if button == "Search":
            global gamelist
            global imagelist
            gamelist = util.search(request.form["textarea"], 0)["names"]
            images = util.search(request.form["textarea"], 0)["images"]
            imagelist = []
            for i in range(len(images)):
                try:
                    imagelist.append(images[i]["super_url"])
                except TypeError:
                    imagelist.append("http://www.worldofchemicals.com/Woclite/tmp/chem/no_image.gif")
            return redirect(url_for("results"))
コード例 #6
0
ファイル: app.py プロジェクト: dlevylambert/project2-pd6
def home():
	if request.method == 'GET':
		return render_template('home.html')
	else:
		button = request.form['button']
		if button == 'Search':
			global gamelist
			global imagelist
			gamelist= util.search(request.form['textarea'],0)['names']
			images = util.search(request.form['textarea'],0)['images']
			imagelist = []
			for i in range(len(images)):
				try:
					imagelist.append(images[i]['super_url'])
				except TypeError:
					imagelist.append("http://www.worldofchemicals.com/Woclite/tmp/chem/no_image.gif")
			return redirect(url_for('results'))
コード例 #7
0
ファイル: api.py プロジェクト: digital10101/SINEDRIVE
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('search_term', type=int, required=True)
        parser.add_argument('query_filter', type=str, required=True)
        request_params = parser.parse_args()

        data = util.search(request_params['search_term'], request_params['query_filter'])

        return data
コード例 #8
0
ファイル: baseline.py プロジェクト: iduck617/Jeeves2.0
def run_baseline(queries, docs):
	assert len(queries) == len(docs)
	results = {} # map ids to answers
	for i in range(len(queries)):
		for query,id_ in queries[i]:
			response = util.search(query,docs[i])
			answer = response[0][0] if response else []
			results[id_] = chose_word(query,answer)
	return results
コード例 #9
0
ファイル: app.py プロジェクト: dlevylambert/project2-pd6
def results():
	global description
	if request.method == 'GET':
		return render_template('results.html', gamelist=gamelist, imagelist=imagelist)
	else:
		button = request.form['button']
		print button
		description = util.search(button,0)['descriptions'][0]
		print description
		return redirect(url_for('description', description=description))
コード例 #10
0
def results():
    global description
    if request.method == "GET":
        return render_template("results.html", gamelist=gamelist, imagelist=imagelist)
    else:
        button = request.form["button"]
        if button == "Click2Back":
            return redirect(url_for("home"))
        else:
            description = util.search(button, 0)["descriptions"][0]
            return redirect(url_for("description", description=description))
コード例 #11
0
ファイル: main.py プロジェクト: haonanc/general-scraper
def multiScrapeMain(initUrls,
                    iterations,
                    regexpList,
                    outputFileName,
                    prefixs=None,
                    prefix_single=""):
    """
    Use regular expression to fetch desired urls from raw htmls text, used fetched urls to scrape raw htmls
    again. Repeat for certain number of times. Then use the last set of urls to scrape and save the result in a file
    :param initUrls: [list] initial list of Urls
    :param iterations: [int] number of iterations
    :param regexpList: [list] list of regular expressions that you plan on using
    :param outputFileName: [str] output file name
    :param prefixs: [list] optional list of prefixs for iteration searching
    :param prefix_single: [str] optional prefix for the final scraping
    :return: void
    """
    if len(regexpList) != iterations or (prefixs != []
                                         and len(prefixs) != iterations):
        print(
            "Make sure you have a regular expression and a prefix for each iteration"
        )
        return

    currIter = 0
    urls = initUrls
    log = []
    while currIter < iterations:
        print("Iteration: " + str(iterations))
        new_list = []
        regexp = regexpList[currIter]
        if prefixs != None:
            prefix = prefixs[currIter]
        else:
            prefix = ""
        for counter, url in enumerate(urls):
            url = prefix + url
            result, status = util.search(url, log)
            if status == 200:
                new_list += util.lookForPatterns(result, regexp)
            util.consoleLog(2,
                            url,
                            counter,
                            len(urls),
                            m2i=currIter,
                            m2t=iterations)
        urls = new_list
        currIter -= 1
    singleScrapeMain(urls, outputFileName, prefix_single)
コード例 #12
0
ファイル: main.py プロジェクト: hchalla/guardiansearch
 def get(self):
   tag = self.request.get('tag')
   page = self.request.get('page')
   next_page = int(page) + 1
   prev_page = int(page) - 1
   template_values = {'keyword': tag}
   num_results, search_results, pages = util.search(tag, page)
   template_values['search_results'] = search_results
   template_values['page'] = int(page)
   template_values['next_page'] = next_page
   template_values['prev_page'] = prev_page
   template_values['num_results'] = num_results
   template_values['pages'] = pages
   template = jinja_environment.get_template('main.html')
   self.response.out.write(template.render(template_values))
コード例 #13
0
ファイル: main.py プロジェクト: haonanc/general-scraper
def singleScrapeMain(urls, outputFileName, prefix=""):
    """
    Scrape the raw htmls code from a given list of urls, save the results in a file
    :param urls: [list] list of urls you plan on scraping
    :param outputFileName: [str]output file name
    :param prefix: [str] an optional prefix for each url
    :return: void
    """
    log = []
    output = {}
    for counter, url in enumerate(urls):
        url = prefix + url
        util.consoleLog(1, url, counter + 1, len(urls))
        data, status = util.search(url, log)
        if status == 200:
            output[url] = data
    util.save_text(outputFileName, output)
コード例 #14
0
ファイル: meetup.py プロジェクト: Web5design/meetup-graph
    def get(self):
        """figure out the type of search that was performed
        
        if it has the words like or dislike, capture the word after 
            and do a search
        if it contains <--> capture the words before and after and do
            a degree search
        """
        nodes = []
        results = ''
        search = str(self.get_argument('search', ''))
        bacon = False
        template = 'template/node.html'
        
        if '<-->' in search:
            r = re.search('(.*?)<\-\->(.*)\W*', search)
            bacon = True
            
            if r:
                left = r.group(1)
                right = r.group(2)
                nodes = util.degree(left, right, 'likes')
        elif 'likes' in search:
            r = re.search('(?:dis)?likes\s(\w+)', search)
            feels = 'likes'
            
            if 'dislikes' in search:
                feels = 'dislikes'

            if r:
                nodes = util.search(r.group(1), feels)

        for n in nodes:
            if bacon:
                if isinstance(n, Relationship):
                    template = 'template/relationship.html'
                    print '[[[[]', n.type
                else:
                    template = 'template/node.html'
                results += self.render_string(template, node=n, klass='relationship_node')
            else:
                results += self.render_string(template, node=n, klass="")
            
        self.write({
            'results': results
        })
コード例 #15
0
    def analyze(self, issue):
        print('Analyze', issue)
        actions = [AssignSubOwners(issue, self.members)]

        try:
            dumpstates = util.search(issue.directory, DumpRegex.DEFAULT, reverse=True)
            if dumpstates:
                runner = TestRunner(issue=issue, dumpstate=dumpstates[0])
                runner.run(default_suite(), actions)

                if issue.is_voc():
                    runner.run(storage_suite(), actions)
                    # runner.run(usb_suite(), actions)
            else:
                print('No dumpstate!')
        except Exception as e:
            print(e)
        finally:
            return actions
コード例 #16
0
ファイル: main.py プロジェクト: haonanc/general-scraper
def pagesScrapeMain(urls, pages, outputFileName, prefix=""):
    """
    Use to scrape websites that have multiple pages
    :param urls: [list] list of urls
    :param pages: [list] page information that will be used as a suffix during the scraping
    :param outputFileName: output file name
    :param prefix: an optional prefix
    :return: void
    """
    log = []
    output = {}
    for c_u, url in enumerate(urls):
        url = prefix + url
        for c_p, page in enumerate(pages):
            url_page = url + page
            result, status = util.search(url_page, log)
            util.consoleLog(3, url_page, c_u, len(urls), c_p, len(pages))
            if status == 200:
                output[url_page] = result
    util.save_text(outputFileName, output)
コード例 #17
0
ファイル: views.py プロジェクト: rohitnahata/NotesDjango
 def get_queryset(self):
     queryset = Note.objects.all()
     user = self.request.user
     label_param = self.request.GET.get('label')
     user_param = self.request.GET.get('user')
     search_param = self.request.GET.get('q')
     queryset = queryset.filter(Q(public=True)
                                | Q(author__username=user)).filter(
                                    archived=False)
     if isNotBlank(label_param):
         queryset = queryset.filter(labels__text=label_param)
     if isNotBlank(user_param):
         queryset = queryset.filter(author__username=user_param)
     if isNotBlank(search_param):
         MODEL_MAP = {
             Note: [
                 "author__username", "note_title", "note_text",
                 "labels__text"
             ],
         }
         queryset = search(search_param, queryset, MODEL_MAP)
     return queryset.filter(
         updated__lte=timezone.now(),
     ).order_by('-updated') \
コード例 #18
0
    def generate_package_info(self):
        """Main generator method: generates package-info files for self.stmt
        and all of its substatements.

        """
        util.write_file(self.d, 'package-info.java',
                        self.gen_package_info(), self.ctx)
        dirs = filter(lambda s: not s.endswith('.java'), os.listdir(self.d))
        stmts = util.search(self.stmt, context.node_stmts)
        for directory in dirs:
            for sub in stmts:
                if util.normalize(sub.arg) == util.normalize(directory):
                    old_d = self.d
                    self.d += os.sep + directory
                    old_pkg = self.pkg
                    self.pkg += '.' + directory
                    old_stmt = self.stmt
                    self.stmt = sub

                    self.generate_package_info()

                    self.d = old_d
                    self.pkg = old_pkg
                    self.stmt = old_stmt
コード例 #19
0
 def record(stmt, package):
     for ch in util.search(stmt, context.yangelement_stmts):
         if package not in context.class_hierarchy:
             context.class_hierarchy[package] = set([])
         context.class_hierarchy[package].add(util.normalize(ch.arg))
         record(ch, '.'.join([package, util.camelize(ch.arg)]))
コード例 #20
0
def report(mail: imaplib.IMAP4) -> List[Any]:
    return [
        x
        for x in [get_dmarc_report(m) for m in search(mail, '(TO "+dmarc")')]
    ]
コード例 #21
0
ファイル: models.py プロジェクト: bnlucas/python-metaspotify
 def track(self, track):
     return search(track, self.tracks)
コード例 #22
0
 def test_search(self):
     mail = unittest.mock.Mock()
     mail.uid.side_effect = [("OK", ["1"]), ("OK", [["1", b"2"]])]
     list(util.search(mail, "Foobar"))
     mail.uid.assert_any_call("SEARCH", "Foobar")
     mail.uid.assert_any_call("FETCH", "1", "(RFC822)")
コード例 #23
0
ファイル: models.py プロジェクト: bnlucas/python-metaspotify
 def album(self, album):
     return search(album, self.albums)
コード例 #24
0
        util.notify("No results found.")
    else:
        util.buildCollectionMenu(results['data'], extras['series_name'])
elif mode == 6:
    # get episode list
    extras = ast.literal_eval(parameters['extras'])
    episodes = util.crunchyroll_api(parameters['url'], extras)

    results = json.loads(episodes)
    if not results['data']:
        util.notify("No results found.")
    else:
        util.buildEpisodesMenu(results['data'], extras['collection_id'])
elif mode == 3:
    # run a search
    search = util.search(
        ast.literal_eval(parameters['extras'].replace("<or>", "|")))
    results = util.crunchyroll_api(parameters['url'], search)
    results = json.loads(results)
    if not results['data']:
        util.notify("No results found.")
    else:
        util.buildSeriesMenu(results['data'], '', 'anime|drama', 20, 0)
elif mode == 4:
    # get category list
    extras = ast.literal_eval(parameters['extras'])
    categories = util.crunchyroll_api(parameters['url'], extras)
    results = json.loads(categories)
    if not results['data']:
        util.notify("No results found.")
    else:
        util.buildCategoryMenu(
コード例 #25
0
def main():
    parser = argparse.ArgumentParser(description='Spoken Language Idenfication')
    parser.add_argument('--dropout', type=float, default=0.2, help='dropout rate in training (default: 0.2')
    parser.add_argument('--batch_size', type=int, default=64, help='batch size in training (default: 32')
    parser.add_argument('--workers', type=int, default=4, help='number of workers in dataset loader (default: 4)')
    parser.add_argument('--max_epochs', type=int, default=5, help='number of max epochs in training (default: 10)')
    parser.add_argument('--lr', type=float, default=1e-04, help='learning rate (default: 0.0001)')
    parser.add_argument('--n_class', type=int, default=2, help='number of class')
    parser.add_argument('--no_cuda', action='store_true', default=False, help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, help='random seed (default: 1)')
    parser.add_argument('--nn_type', type=str, default='crnn', help='type of neural networks')
    parser.add_argument('--save_name', type=str, default='model', help='the name of model')
    parser.add_argument('--mode', type=str, default='train')

    args = parser.parse_args()

    random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)

    args.cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device('cuda' if args.cuda else 'cpu')

    model = DNN.DNN()

    model = nn.DataParallel(model).to(device)

    optimizer = optim.Adam(model.module.parameters(), lr=args.lr)
    criterion = nn.CrossEntropyLoss(reduction='sum').to(device)

    if args.mode != 'train':
        return

    download_data()

    kor_db_list = []
    search('dataset/train/train_data', kor_db_list)

    train_wav_paths = np.loadtxt("dataset/TRAIN_list.csv", delimiter=',', dtype=np.unicode)
    valid_wav_paths = np.loadtxt("dataset/TEST_developmentset_list.csv", delimiter=',', dtype=np.unicode)
    test_wav_paths = np.loadtxt("dataset/TEST_coreset_list.csv", delimiter=',', dtype=np.unicode)

    train_wav_paths = list(map(lambda x: "dataset/TIMIT/{}.WAV".format(x), train_wav_paths))
    valid_wav_paths = list(map(lambda x: "dataset/TIMIT/{}.WAV".format(x), valid_wav_paths))
    test_wav_paths = list(map(lambda x: "dataset/TIMIT/{}.WAV".format(x), test_wav_paths))

    min_loss = 100000
    begin_epoch = 0

    loss_acc = [[], [], [], []]

    train_batch_num, train_dataset_list, valid_dataset, test_dataset = \
        split_dataset(args, train_wav_paths, valid_wav_paths, test_wav_paths, kor_db_list)

    logger.info('start')

    train_begin = time.time()

    for epoch in range(begin_epoch, args.max_epochs):

        train_queue = queue.Queue(args.workers * 2)

        train_loader = MultiLoader(train_dataset_list, train_queue, args.batch_size, args.workers, args.nn_type)
        train_loader.start()

        train_loss, train_acc = train(model, train_batch_num, train_queue, criterion, optimizer, device, train_begin, args.workers, 10)
        logger.info('Epoch %d (Training) Loss %0.4f Acc %0.4f' % (epoch, train_loss,  train_acc))

        train_loader.join()

        loss_acc[0].append(train_loss)
        loss_acc[1].append(train_acc)

        valid_queue = queue.Queue(args.workers * 2)

        valid_loader = BaseDataLoader(valid_dataset, valid_queue, args.batch_size, 0, args.nn_type)
        valid_loader.start()

        eval_loss, eval_acc = evaluate(model, valid_loader, valid_queue, criterion, device)
        logger.info('Epoch %d (Evaluate) Loss %0.4f Acc %0.4f' % (epoch, eval_loss, eval_acc))

        valid_loader.join()

        loss_acc[2].append(eval_loss)
        loss_acc[3].append(eval_acc)

        best_model = (eval_loss < min_loss)

        if best_model:
            min_loss = eval_loss
            torch.save(model.state_dict(), './save_model/best_model.pt')
            save_epoch = epoch

    model.load_state_dict(torch.load('./save_model/best_model.pt'))

    test_queue = queue.Queue(args.workers * 2)

    test_loader = BaseDataLoader(test_dataset, test_queue, args.batch_size, 0, args.nn_type)
    test_loader.start()

    confusion_matrix = torch.zeros((args.n_class, args.n_class))
    test_loss, test_acc = evaluate(model, test_loader, test_queue, criterion, device, confusion_matrix)
    logger.info('Epoch %d (Test) Loss %0.4f Acc %0.4f' % (save_epoch, test_loss, test_acc))

    test_loader.join()

    save_data(loss_acc, test_loss, test_acc, confusion_matrix.to('cpu').numpy())
    plot_data(loss_acc, test_loss, test_acc)

    return 0
コード例 #26
0
ファイル: plotter.py プロジェクト: frokaikan/astro-plot
 def plotAtTime(self, curTime):
     # 同plotAtIndex() 该函数不应该频繁调用
     self.curTime = curTime
     idx = search(self.data["timestamp"], curTime)
     self.plotAtIndex(idx)
コード例 #27
0
    def generate_classes(self):
        """Generates a Java class hierarchy from a module statement, allowing
        for netconf communication using the jnc library.

        """
        assert (self.stmt.keyword == 'module')
        # Namespace and prefix
        ns_arg = util.search_one(self.stmt, 'namespace').arg
        prefix = util.search_one(self.stmt, 'prefix')

        # Add root to context.class_hierarchy dict
        if self.rootpkg not in context.class_hierarchy:
            context.class_hierarchy[self.rootpkg] = set([])
        context.class_hierarchy[self.rootpkg].add(self.n)

        # Add all classes that will be generated to context.class_hierarchy dict
        def record(stmt, package):
            for ch in util.search(stmt, context.yangelement_stmts):
                if package not in context.class_hierarchy:
                    context.class_hierarchy[package] = set([])
                context.class_hierarchy[package].add(util.normalize(ch.arg))
                record(ch, '.'.join([package, util.camelize(ch.arg)]))

        record(self.stmt, self.rootpkg)

        # Gather typedefs to generate and add to context.class_hierarchy dict
        typedef_stmts = set([])
        context.module_stmts = set([self.stmt])
        included = map(lambda x: x.arg, util.search(self.stmt, 'include'))
        for (module, rev) in self.ctx.modules:
            if module in included:
                context.module_stmts.add(self.ctx.modules[(module, rev)])
        for module_stmt in context.module_stmts:
            for stmt in util.search(module_stmt, 'typedef'):
                typedef_stmts.add(stmt)
                context.class_hierarchy[self.rootpkg].add(util.normalize(stmt.arg))
                try:
                    while True:
                        type_stmt = util.search_one(stmt, 'type')
                        if type_stmt.i_typedef is None:
                            break
                        typedef_stmts.add(type_stmt.i_typedef)
                        stmt = type_stmt.i_typedef
                        context.class_hierarchy[self.rootpkg].add(util.normalize(stmt.arg))
                except AttributeError:
                    pass

        # Generate the typedef classes
        for stmt in typedef_stmts:
            name = util.normalize(stmt.arg)

            description = util.class_javadoc(ns=self.ns, stmt=stmt)

            java_class = JavaClass(filename=name + '.java',
                                   package=self.package,
                                   description=description,
                                   source=self.src,
                                   superclass='YangElement')
            if self.ctx.opts.verbose:
                print('Generating Java class "' + name + '.java' + '"...')

            gen = MethodGenerator(stmt, self.ctx)

            for const_field in gen.enum_consts():
                java_class.add_field(const_field)

            for constructor in gen.constructors():
                java_class.add_constructor(constructor)

            for i, method in enumerate(gen.setters()):
                java_class.append_access_method(str(i), method)

            java_class.add_support_method(gen.enums())
            java_class.append_access_method('check', gen.checker())

            type_stmt = util.search_one(stmt, 'type')
            super_type = util.get_types(type_stmt, self.ctx)[0]
            java_class.superclass = super_type.rpartition('.')[2]
            java_class.imports.add(super_type)
            if super_type == 'io.netconfessor.YangDecimal64':
                java_class.imports.add('java.math.BigDecimal')
            elif super_type in ('io.netconfessor.YangBits',
                                'io.netconfessor.YangUInt64'):
                java_class.imports.add('java.math.BigInteger')
            elif super_type in ('io.netconfessor.YangLeafref',
                                'io.netconfessor.YangIdentityref'):
                java_class.imports.add('io.netconfessor.Element')

            util.write_file(self.path, java_class.filename,
                            java_class.as_list(), self.ctx)

        # Generate classes for children and keep track of augmented modules
        for stmt in util.search(self.stmt, list(context.yangelement_stmts | {'augment', 'leaf'})):
            child_generator = ClassGenerator(stmt, package=self.package,
                                             ns=ns_arg, prefix_name=self.n, parent=self)
            child_generator.generate()

        # Generate root class
        if self.ctx.opts.verbose:
            print('Generating Java class "' + self.filename + '"...')
        self.java_class = JavaClass(filename=self.filename,
                                    package=self.package, description=('The root class for namespace ' +
                                                                       ns_arg + ' (accessible from \n * ' + self.n +
                                                                       '.NAMESPACE) with prefix "' + prefix.arg + '" (' + self.n +
                                                                       '.PREFIX).'),
                                    source=self.src)

        self.java_class_visitor = JavaClass(
            abstract=True,
            filename=self.filename_visitor,
            package=self.package,
            description='Visitor of module ' + prefix.arg + '(' + ns_arg + ')')

        self.generate_visitor(self.stmt, module=True)

        self.class_fields(ns_arg, prefix)
        self.method_enable(prefix)
        self.method_register_schema(prefix)
        self.write_to_file()
コード例 #28
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()
コード例 #29
0
    def generate_visitor(self, stmt, module=False):

        def pkg(class_name):
            return self.java_class_visitor.package \
                   + (('.' + util.camelize(self.n2)) if not module else '') \
                   + '.' + class_name

        def if_none_then_null(val):
            return ('"' + val + '"') if val is not None else 'null'

        method_visit = JavaMethod(return_type='void', name='visit')
        method_visit.add_javadoc('Auto-generated module traverse algorithm')
        method_visit.add_parameter('io.netconfessor.NodeSet', 'nodes')
        method_visit.add_modifier('public')
        method_visit.add_dependency('io.netconfessor.Element')

        this_config = util.is_config(stmt)
        stmt_id = util.camelize(stmt.arg)
        stmt_class = util.normalize(stmt.arg)

        method_collect = None

        # module visitor collects into NodeSet, children into corresponding java object
        if module:
            method_collect = JavaMethod(return_type='io.netconfessor.NodeSet', name='collectConfig')
            method_collect.add_modifier('public')
            method_collect.add_line('NodeSet nodes = new NodeSet();')
        elif this_config:
            method_collect = JavaMethod(return_type=self.package + '.' + self.n, name='collectConfig')

            if stmt.keyword in {'list', 'leaf-list'}:
                method_collect.add_parameter(param_type=pkg(stmt_class), param_name=stmt_id)
                # method_collect.add_line('%s %s = getNext%s();' % (stmt_class, stmt_id, stmt_class))
                method_collect.add_line('if (%s == null) {' % stmt_id)
                method_collect.add_line('    return null;')
                method_collect.add_line('}')

            else:
                method_collect.add_line('%s %s = new %s();' % (stmt_class, stmt_id, stmt_class))
            method_collect.add_modifier('public')

        method_setup_all = JavaMethod(return_type='void', name='setup')
        method_setup_all.add_modifier('public')

        for s in util.search(stmt, context.yangelement_stmts):

            config = util.is_config(s)
            xpath = statements.mk_path_str(s, True)
            keyword = s.keyword
            id = util.camelize(s.arg)
            visitee_name = util.normalize(s.arg)
            visitee = util.escape_conflicts(pkg(visitee_name))
            visitor_id = util.visitor_class(id)
            visitee_full = pkg(visitee_name)

            if keyword in {'container'}:
                method_name = 'on' + visitee_name
                next_visitor = util.visitor_class(visitee_name)
                visit_method = JavaMethod(
                    return_type='void',  # pkg(next_visitor),
                    name=method_name)
                visit_method.add_parameter(visitee_full, keyword)
                visit_method.add_modifier('public')
                visit_method.add_modifier('abstract')
                self.java_class_visitor.add_method(visit_method)

                field_visitor = JavaValue()
                field_visitor.add_modifier('private')
                field_visitor.add_modifier(next_visitor)
                field_visitor.set_name(util.visitor_class(id))
                self.java_class_visitor.add_field(field_visitor)

                if config:
                    method_collect.add_line('if (%s != null) {' % visitor_id)
                    method_collect.add_line('    %s %s = %s.collectConfig();' % (visitee, id, visitor_id))
                    method_collect.add_line('    if (%s != null) {' % id)
                    if module:
                        method_collect.add_line('        nodes.add(%s);' % id)
                    else:
                        method_collect.add_line('        %s.add%s(%s);' % (stmt_id, visitee_name, id))
                    method_collect.add_line('    }')
                    method_collect.add_line('}')

                method_visit.add_line('')
                method_visit.add_line('final Element %s = nodes.getFirstChild("%s");' % (id, s.arg))
                method_visit.add_line('if (%s != null) {' % id)
                method_visit.add_line('    %s((%s)%s);'
                                      % (method_name, visitee, id))
                method_visit.add_line('    if (%s != null) {' % visitor_id)
                method_visit.add_line('        if (%s.hasChildren()) {' % id)
                method_visit.add_line('            %s.visit(%s.getChildren());' % (visitor_id, id))
                method_visit.add_line('        }')
                method_visit.add_line('    }')
                method_visit.add_line('}')

                method_visit.add_dependency(pkg(next_visitor))

                method_setup = JavaMethod(return_type=pkg(next_visitor), name='setup' + visitee_name)
                method_setup.add_modifier('public')
                method_setup.add_modifier('abstract')
                method_setup.add_parameter(param_type='io.netconfessor.YangData', param_name='data')
                desc_stmt = util.search_one(s, 'description')
                desc = if_none_then_null(desc_stmt.arg if desc_stmt is not None else None)
                method_setup_all.add_line('%s = setup%s(new YangData("%s", "%s", %s, %s, YangDataType.%s));'
                                          % (visitor_id, visitee_name, s.arg, xpath, util.yang_string_to_jstring(desc),
                                             'true' if config else 'false', util.camelize(s.keyword)))
                method_setup_all.add_line('if (%s != null) {' % visitor_id)
                method_setup_all.add_line('    %s.setup();' % visitor_id)
                method_setup_all.add_line('}')
                method_setup_all.add_dependency('io.netconfessor.YangData')
                method_setup_all.add_dependency('io.netconfessor.YangDataType')
                self.java_class_visitor.add_field(method_setup)

            elif keyword in {'list'}:
                next_method_name = 'onNext' + visitee_name
                entry_visitor = util.visitor_class(visitee_name)
                visit_method = JavaMethod(return_type='void',  # pkg(entry_visitor),
                                          name=next_method_name)
                visit_method.add_modifier('public')
                visit_method.add_modifier('abstract')
                visit_method.add_parameter(visitee_full, 'item')
                self.java_class_visitor.add_method(visit_method)

                start_method_name = 'onStart' + visitee + 'List'
                visit_method = JavaMethod(return_type='void', name=start_method_name)
                visit_method.add_modifier('protected')
                self.java_class_visitor.add_method(visit_method)

                stop_method_name = 'onStop' + visitee + 'List'
                visit_method = JavaMethod(return_type='void', name=stop_method_name)
                visit_method.add_modifier('protected')
                self.java_class_visitor.add_method(visit_method)

                if config:
                    collect_get_next_name = 'getNext' + visitee_name

                    method_collect.add_line('%s %s;' % (util.escape_conflicts(visitee, visitee_name), id))
                    method_collect.add_line('while((%s = %s()) != null) {' % (id, collect_get_next_name))
                    if module:
                        method_collect.add_line('    nodes.add(%s);' % id)
                    else:
                        method_collect.add_line('    %s.removeNonKeysIfMarkedToDelete();' % id)
                        method_collect.add_line('    %s.add%s(%s);' % (stmt_id, visitee_name, id))
                    method_collect.add_line('}')

                    collect_get_next = JavaMethod(return_type=visitee_full, name=collect_get_next_name)
                    collect_get_next.add_modifier('abstract')
                    collect_get_next.add_modifier('protected')
                    self.java_class_visitor.add_method(collect_get_next)

                method_visit.add_line('')
                method_visit.add_line('%s();' % start_method_name)
                method_visit.add_line('for (Element node : nodes.getChildren("%s")) {' % (s.arg))
                method_visit.add_line('    %s((%s)node);' % (next_method_name, visitee))
                method_visit.add_line('    if (%s != null) {' % visitor_id)
                method_visit.add_line('        if (node.hasChildren()) {')
                method_visit.add_line('            %s.visit(node.getChildren());' % visitor_id)
                method_visit.add_line('        }')
                method_visit.add_line('    }')
                method_visit.add_line('}')
                method_visit.add_line('%s();' % stop_method_name)

                method_setup = JavaMethod(return_type=pkg(entry_visitor), name='setup' + visitee_name)
                method_setup.add_modifier('public')
                method_setup.add_modifier('abstract')
                method_setup.add_parameter(param_type='io.netconfessor.YangData', param_name='data')
                self.java_class_visitor.add_field(method_setup)
                desc_stmt = util.search_one(s, 'description')
                desc = if_none_then_null(desc_stmt.arg if desc_stmt is not None else None)
                method_setup_all.add_line('%s = setup%s(new YangData("%s", "%s", %s, %s, YangDataType.%s));'
                                          % (visitor_id, visitee_name, s.arg, xpath, util.yang_string_to_jstring(desc),
                                             'true' if config else 'false', util.camelize(s.keyword)))
                method_setup_all.add_line('if (%s != null) {' % visitor_id)
                method_setup_all.add_line('    %s.setup();' % visitor_id)
                method_setup_all.add_line('}')
                method_setup_all.add_dependency('io.netconfessor.YangData')
                method_setup_all.add_dependency('io.netconfessor.YangDataType')

                field_visitor = JavaValue()
                field_visitor.add_modifier('private')
                field_visitor.add_modifier(entry_visitor)
                field_visitor.set_name(util.visitor_class(id))
                self.java_class_visitor.add_field(field_visitor)

            elif keyword in {'leaf'}:
                method_name = 'on' + visitee
                visit_method = JavaMethod(return_type='void', name=method_name)
                visit_method.add_modifier('public')
                visit_method.add_modifier('abstract')
                visit_method.add_parameter(visitee_full, keyword)
                self.java_class_visitor.add_method(visit_method)

                jnc, primitive = util.get_types(s, self.ctx)
                type_reference = util.escape_conflicts(visitee, visitee_name)
                base_type = util.get_base_type(s)
                is_enum = base_type.arg == 'enumeration'
                type_class = util.escape_conflicts(jnc, visitee_name)

                if config:
                    get_method = JavaMethod(name='get' + visitee)
                    get_method.set_return_type(visitee, visitee_name)
                    get_method.add_modifier('public')
                    get_method.add_modifier('abstract')
                    self.java_class_visitor.add_method(get_method)

                    method_collect.add_line('%s %s = get%s();' % (visitee_name, id, visitee_name))
                    method_collect.add_line('if (%s != null) {' % id)
                    method_collect.add_line('    %s.add%s(%s);' % (stmt_id, visitee_name, id))
                    method_collect.add_line('}')

                method_visit.add_line('')
                method_visit.add_line('final Element %s = nodes.getFirstChild("%s");' % (id, s.arg))
                method_visit.add_line('if (%s != null) {' % id)
                method_visit.add_line('    %s((%s)%s);' % (method_name, type_reference, id))
                method_visit.add_line('}')

                method_setup = JavaMethod(return_type='void', name='setup' + visitee_name)
                method_setup.add_modifier('public')
                method_setup.add_modifier('abstract')

                yang_data_type = 'io.netconfessor.EnumYangData' if is_enum else 'io.netconfessor.LeafYangData'

                method_setup.add_parameter_generic(param_type=yang_data_type,
                                                   generic_type=jnc,
                                                   param_name='data',
                                                   this_class_name=visitee_full)
                desc_stmt = util.search_one(s, 'description')
                desc = if_none_then_null(desc_stmt.arg if desc_stmt is not None else None)
                if is_enum:
                    method_setup_all.add_line('setup%s(new EnumYangData<>('
                                              '"%s", "%s", %s, %s, YangDataType.%s, "%s", s -> new %s(s), %s.enums()));'
                                              % (visitee_name, s.arg, xpath, util.yang_string_to_jstring(desc),
                                                 'true' if config else 'false',
                                                 util.camelize(s.keyword), jnc, type_class, type_class))
                else:

                    method_setup_all.add_line('setup%s(new LeafYangData<>('
                                              '"%s", "%s", %s, %s, YangDataType.%s, "%s", s -> new %s(s)));'
                                              % (visitee_name, s.arg, xpath, util.yang_string_to_jstring(desc),
                                                 'true' if config else 'false',
                                                 util.camelize(s.keyword), jnc, type_class))

                method_setup_all.add_dependency(jnc)
                method_setup_all.add_dependency(yang_data_type)
                method_setup_all.add_dependency('io.netconfessor.YangDataType')
                self.java_class_visitor.add_field(method_setup)

        if module:
            method_collect.add_javadoc('Retrieve all config values in registered visitors')
            method_collect.add_javadoc('Before send to device you need sync result with older nodeset (empty allowed)')
            method_collect.add_line('return nodes;')
        elif this_config:
            method_collect.add_javadoc('Retrieve all config values in registered visitors')
            method_collect.add_line('if (%s.hasChildren()) {' % stmt_id)
            method_collect.add_line('   return %s;' % stmt_id)
            method_collect.add_line('} else {')
            method_collect.add_line('   return null;')
            method_collect.add_line('}')

        self.java_class_visitor.add_method(method_setup_all)
        self.java_class_visitor.add_method(method_collect)
        self.java_class_visitor.add_method(method_visit)