Exemple #1
0
def get(ident,tags,limit=8+1):
	category = hash(tags) % 0x7FFFFFFF
	arg = argbuilder()
	category = arg(category)
	stmt = Select(withtags.tagsWhat,InnerJoin(
					'randomSeen',InnerJoin('media','things',EQ('things.id','media.id')),
		EQ('randomSeen.media','media.id')),
								AND(
									"seen",
									AND(EQ('randomSeen.category',category),
											"randomSeen.id <= "+arg(ident))))
	stmt = Order(stmt,'randomSeen.id DESC')
	stmt = Limit(stmt,limit=limit)
	rows = db.execute(stmt.sql(),arg.args)
	#print('\n'.join(r[0] for r in rows))
	#raise SystemExit
	return rows
Exemple #2
0
 def get(self):
     """Get all items, sorted by type"""
     full_data = {}
     for table in ITEMS_TABLES:
         data = Select('*', table).all()
         data_dict = associative_data_dict(data, table)
         full_data[table.capitalize()] = data_dict
     return full_data
Exemple #3
0
def search(table, name, where=False, model=False):
    data = Select('*', table, search=True, name=name, where=where).all()
    if data == []:
        return {"Error": f"'{name}' not found when searching"}
    if "Error" in str(data):
        return data, 500
    if model:
        return data_list_to_model_dict(data, model)
    return data
Exemple #4
0
 def get(self, table):
     """Retrieve raw data from a specified table"""
     """Get all data entries from a specified table """
     data = Select('*', table).all()
     if data == []:
         return {"Error": f"No entries found in {table}"}, 404
     if "Error" in str(data):
         return data, 500
     return data
Exemple #5
0
 def limit(*args, **kwargs):
     # Check for class_name in the database first
     class_names = [
         entry['name'] for entry in Select('name', 'classes').all()
     ]
     name = kwargs["class_name"]
     if name.capitalize() not in class_names:
         return {"Error": f"Class '{name}' not found in classes'"}, 404
     return function(*args, **kwargs)
Exemple #6
0
def get_category_ids(category):
    """List of sub-category ids within category """
    if category:
        where = f"type='{category}'"
    data = Select('id', 'weapon_categories', where=where).all()
    return [
        item['id']
        for item in data
    ]
Exemple #7
0
def get_categories(item_type):
    """Get all categories within type """
    symbol = '>' if item_type.lower() == 'ranged' else '<='
    data = Select('type', 'weapon_categories', where=f'id {symbol} 11').all()
    response = set(  # reject duplicates
        item['type']
        for item in data
    )
    return list(response)
Exemple #8
0
 def get(self, table, entry_name):
     """Retrieve data entry by name from a specified table"""
     data = Select('*', table, name=entry_name).one()
     if data is None:
         return {
             "Error":
             f"{entry_name} not found in {table}. "
             f"Try this instead: {NS.search._path}/{table}/{entry_name}"
         }, 404
     elif "Error" in str(data):
         return data, 500
     return data
Exemple #9
0
def get_as_object(table, model, where=False, category=False):
    """Get entry as data objects """
    items = Select('*', table, where=where).all()
    if not items:
        return {"Error": "No entries found fitting this description"}

    data = {}
    for item in items:
        name = item.pop('name')
        if category:
            weapon = model(data=item, category=category)
        else:
            weapon = model(data=item)
        data[name] = weapon.data()
    return data
Exemple #10
0
 def get(self):
     """All feats"""
     data = Select('*', 'feats').all()
     return data_list_to_model_dict(data, FeatData)
Exemple #11
0
 def get(self):
     """Get all abilities data """
     return Select('*', "abilities").all()
Exemple #12
0
 def get(self, ability):
     """Get ability by name or shorthand """
     where = f"name = '{ability}' OR shorthand = '{ability}'"
     return Select('*', "abilities", where=where).all()
Exemple #13
0
 def get(self):
     """Data for all races"""
     data = Select('*', 'races').all()
     return data_list_to_model_dict(data, BasicData)
Exemple #14
0
 def get(self, level):
     """Show all mystic spells under a specific level"""
     where = f"mystic_level <= {level}"
     data = Select('*', 'spells', where=where).all()
     return data_list_to_model_dict(data, SpellData)
Exemple #15
0
 def get(self):
     """Non-combat feats"""
     data = Select('*', 'feats', where="combat_feat=0").all()
     return data_list_to_model_dict(data, FeatData)
Exemple #16
0
 def get(self):
     """Data for all themes"""
     data = Select('*', 'themes').all()
     return data_list_to_model_dict(data, ThemeData)
Exemple #17
0
 def get(self, item_type):
     """Get all items for type"""
     data = Select('*', item_type).all()
     return associative_data_dict(data, item_type)
Exemple #18
0
def tagStatement(tags,offset=0,limit=0x30,taglimit=0x10,wantRelated=False):
	From = InnerJoin('media','things',EQ('things.id','media.id'))
	negaWanted = Select('id','unwanted')
	negaClause = NOT(Intersects('neighbors',array(negaWanted)))
	if not (tags.posi or tags.nega):
		where = None
	elif tags.posi:
		where = Group(Select(EVERY(Intersects('neighbors','tags')),'wanted'))

		if tags.nega:
			negaWanted.where = NOT(IN('id',Select('unnest(tags)','wanted')))
			where = AND(where,negaClause)
	elif tags.nega:
		# only negative tags
		negaWanted.where = None
		where = negaClause

	if User.noComics:
		first_page = NOT(IN('things.id',Select('medium','comicPage','which != 0')))
		if where is None:
			where = first_page
		else:
			where = AND(where,first_page)
	arg = argbuilder()

	mainCriteria = Select('things.id',From,where)
	mainOrdered = Limit(Order(mainCriteria,
						  'media.added DESC'),
					(arg(offset) if offset else False),arg(limit))

	if tags.posi:
		posi = Type(arg([getTag(tag) if isinstance(tag,str) else tag for tag in tags.posi]),'INTEGER[]')


	if wantRelated:
		mainOrdered = EQ('things.id',ANY(mainOrdered))
		if tags.posi:
			mainOrdered = AND(
							NOT(EQ('tags.id',ANY(posi))),
				mainOrdered)

		tagStuff = Select(
			['tags.id','first(tags.name) as name'],
			InnerJoin('tags','things',
					  EQ('tags.id','ANY(things.neighbors)')),
			mainOrdered)
		stmt = Select(['derp.id','derp.name'],
					  AS(
						  Limit(GroupBy(tagStuff,'tags.id'),
								limit=arg(taglimit)),
						  'derp'))

		stmt = Order(stmt,'derp.name')
	else:
		mainCriteria.what = tagsWhat
		if User.noComics:

			mainCriteria.what += (
				EXISTS(Select(
					'medium','comicPage',EQ("things.id","comicPage.medium"))),)
		stmt = mainOrdered

	# we MIGHT need a with statement...
	clauses = {}

	if tags.nega:
		nega = Type(arg([getTag(tag) if isinstance(tag,str) else tag for tag in tags.nega]),'INTEGER[]')
		notWanted = EQ('things.id',ANY(nega))
		if tags.posi:
			notWanted = AND(notWanted,
							NOT(EQ('things.id',ANY(posi))))
		herp = AS(Func('unnest',nega),'id')

		clauses['unwanted'] = (
			'id',
			Union(Select('tags.id',
						 InnerJoin('tags','things',
								   EQ('tags.id','things.id')),
									 notWanted),
				  Select('id',herp)))
	else:
		notWanted = None

	if tags.posi:
		# make sure positive tags don't override negative ones
		noOverride = NOT(EQ('things.id',ANY(posi)))
		notWanted = AND(notWanted,noOverride) if notWanted else noOverride
		# MUST separate implications to separate arrays
		# for the AND requirement a & b & c = (a|a2|a3)&(b|b2|b3)&...
		clauses['wanted'] = ('tags',Select(array(Select('implications(unnest)')),
										   Func('unnest',posi)))
												 


	#sel = stmt.clause.clause
	#sel.what = 'media.id, neighbors'
	#sel.where = Group(Select(EVERY(Intersects('neighbors','wanted.tags')),'wanted'))
	#stmt = 'SELECT tags FROM wanted WHERE $1::int > -1000'

	if clauses:
		stmt = With(stmt,**clauses)

	return stmt,arg
Exemple #19
0
 def get(self):
     """Show all mystic spells"""
     data = Select('*', 'spells', where="mystic_level IS NOT NULL").all()
     return data_list_to_model_dict(data, SpellData)
Exemple #20
0
 def get(self, item_type, item_name):
     """Get item by type and name"""
     data = Select('*', item_type, name=item_name).one()
     return BasicData(item_type, data=data).associative_data()
Exemple #21
0
 def get(self):
     """Show all technomancer spells"""
     where = "technomancer_level IS NOT NULL"
     data = Select('*', 'spells', where=where).all()
     return data_list_to_model_dict(data, SpellData)
Exemple #22
0
def main():
    recalculate()
    setup()
    note.magenta('got em yay')

    from orm import Select,With,AS,Limit,OuterJoin

    stmt = Select('id,hasTags,uniquelyIdentifies,(select path from filesources where id = thingy.id),(select uri from urisources where id=thingy.id) from thingy')
    #stmt = Limit(stmt,limit=10)

    stmt = With(
        stmt,
        thingy=((),
                Select('sources.id,hasTags,uniquelyIdentifies',
                       OuterJoin(
                           OuterJoin('sources',
                                     'goodsources',
                                     'sources.id = goodsources.id'),
                           'sourceProblems',
                           'sources.id = sourceProblems.id'),
                       'goodsources.id IS NULL AND sourceProblems.id IS NULL')))

    stmt = stmt.sql()
    note.blue(stmt)

    def problem(ident,s):
        note.alarm('+++',ident,s)
        if ident is None: return
        db.execute('INSERT INTO sourceProblems (id,problem) VALUES ($1,$2)',
                   (ident,s))

    for id,hasTags,uniquelyIdentifies,path,uri in db.execute(stmt):
        print('---------------')
        note.blue(id)
        note.blue(path,uri)
        time.sleep(1)#input()
        if path and os.path.exists(path):
            note.green('found path',path)
            impmort(path,{'laptop','special:recovery'})
        elif uri:
            if uri.startswith('file://'):
                path = uri[len('file://'):]
                note.green('uri path',path)
                if os.path.exists(path):
                    impmort(path,{'laptop','special:recovery'})
                else:
                    id = problem(id,'bad file: uri')
            else:
                note.green('uri',uri)
                try:
                    norm = normalize(uri)
                    if uri != norm:
                        id = problem(id,'denormalized uri: {}'.format(norm))
                        newid = db.execute('SELECT id FROM urisources where uri = $1',(norm,))
                        if newid:
                            id = newid[0][0]
                        else:
                            id = None
                    if hasTags and uniquelyIdentifies:
                        parse(norm)
                    elif not uniquelyIdentifies:
                        id = problem(id,'not a unique source')
                    else:
                        id = problem(id,'no tags at this one')
                    #recalculate()
                except ParseError as e:
                    id = problem(id,str(e))
                except urllib.error.HTTPError as e:
                    id = problem(id,str(e))
                except http.client.RemoteDisconnected as e:
                    id = problem(id,str(e))
                except NoGood as e:
                    id = problem(id,str(e))
        elif path:
            id = problem(id,'bad path')
        else:
            id = problem(id,'no path, and a bad uri')
Exemple #23
0
 def get(self, item_type):
     """All entries for specified type, unformatted"""
     data = Select('*', f'{item_type}_weapons').all()
     if "Error" in str(data):
         return data, 500
     return data