Example #1
1
class DB:
    def __init__(self,db_path):
        self.db = TinyDB(db_path)

    def add(self, data):
        # Only add it if you can't find it
        Track = Query()
        if not self.db.get(Track.display_id == data['display_id']):
            return self.db.insert(data)

    def searchById(self, video_id):
        Track = Query()
        return self.db.get(Track.display_id == video_id)

    def search(self, text):
        pattern = re.compile(text,re.IGNORECASE)
        def test(txt):
            return pattern.search(txt)

        Track = Query()
        q = Track.title.test(test) | Track.description.test(test)
        return self.db.search(q)

    def all(self):
        return self.db.all()
Example #2
0
def dragon_greet():
    print("_______________________________________________________________\n")
    time = datetime.datetime.now().time()

    global user_full_name
    global user_prefix
    global config_file

    command = "getent passwd $LOGNAME | cut -d: -f5 | cut -d, -f1"
    user_full_name = os.popen(command).read()
    user_full_name = user_full_name[:-1]  # .decode("utf8")
    home = expanduser("~")
    config_file = TinyDB(home + '/.dragonfire_config.json')
    callme_config = config_file.search(Query().datatype == 'callme')
    if callme_config:
        user_prefix = callme_config[0]['title']
    else:
        gender_config = config_file.search(Query().datatype == 'gender')
        if gender_config:
            user_prefix = GENDER_PREFIX[gender_config[0]['gender']]
        else:
            gender = Classifier.gender(user_full_name.split(' ', 1)[0])
            config_file.insert({'datatype': 'gender', 'gender': gender})
            user_prefix = GENDER_PREFIX[gender]

    if time < datetime.time(12):
        time_of_day = "morning"
    elif datetime.time(12) < time < datetime.time(18):
        time_of_day = "afternoon"
    else:
        time_of_day = "evening"
    userin.execute(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'")
    userin.say(" ".join(["Good", time_of_day, user_prefix]))
Example #3
0
def download(name,force=False):
    db=TinyDB(path_db_)
    temp = Query()
    data=requests.get("https://raw.githubusercontent.com/PyThaiNLP/pythainlp-corpus/master/db.json")
    data_json=data.json()
    if name in list(data_json.keys()):
        temp_name=data_json[name]
        print("Download : "+name)
        if len(db.search(temp.name==name))==0:
            print(name+" "+temp_name['version'])
            download_(temp_name['download'],temp_name['file_name'])
            db.insert({'name': name, 'version': temp_name['version'],'file':temp_name['file_name']})
        else:
            if len(db.search(temp.name==name and temp.version==temp_name['version']))==0:
                print("have update")
                print("from "+name+" "+db.search(temp.name==name)[0]['version']+" update to "+name+" "+temp_name['version'])
                yes_no="y"
                if force==False:
                    yes_no=str(input("y or n : ")).lower()
                if "y"==yes_no:
                    download_(temp_name['download'],temp_name['file_name'])
                    db.update({'version':temp_name['version']},temp.name==name)
            else:
                print("re-download")
                print("from "+name+" "+db.search(temp.name==name)[0]['version']+" update to "+name+" "+temp_name['version'])
                yes_no="y"
                if force==False:
                    yes_no=str(input("y or n : ")).lower()
                if "y"==yes_no:
                    download_(temp_name['download'],temp_name['file_name'])
                    db.update({'version':temp_name['version']},temp.name==name)
    db.close()
Example #4
0
def get_file(name):
    db=TinyDB(path_db_)
    temp = Query()
    if len(db.search(temp.name==name))>0:
        path= get_path_data(db.search(temp.name==name)[0]['file'])
        db.close()
        if not os.path.exists(path):
            download(name)
        return path
Example #5
0
class TinyDBConvertor(object):

    def __init__(self, collection_name, db_name, db_path=LOCAL_DIR):

        self.local_db_fullpath = os.path.join(db_path, db_name)
        self.local_db = TinyDB(self.local_db_fullpath)
        self.collection_name = collection_name

    def reload(self):
        self.local_db.close()
        self.local_db = TinyDB(self.local_db_fullpath)

    def find(self):
        self.reload()

        query = Query()
        resp = self.local_db.search(query.type == self.collection_name)
        self.local_db.close()

        return resp

    def find_one(self, entry):
        self.reload()

        query = Query()
        resp = self.local_db.search((query.type == self.collection_name) &
                                    (query.fqu == entry['fqu']))

        self.local_db.close()

        if len(resp) == 0:
            return None
        else:
            return resp[0]

    def save(self, new_entry):
        self.reload()

        new_entry['type'] = self.collection_name
        resp = self.local_db.insert(new_entry)
        self.local_db.close()

        return resp

    def remove(self, entry):
        self.reload()

        query = Query()

        resp = self.local_db.remove((query.type == self.collection_name) &
                                    (query.fqu == entry['fqu']))

        self.local_db.close()

        return resp
Example #6
0
def test_delete(tmpdir):
    path = str(tmpdir.join('db.json'))

    db = TinyDB(path, ensure_ascii=False)
    q = Query()
    db.insert({'network': {'id': '114', 'name': 'ok', 'rpc': 'dac',
                           'ticker': 'mkay'}})
    assert db.search(q.network.id == '114') == [
        {'network': {'id': '114', 'name': 'ok', 'rpc': 'dac',
                     'ticker': 'mkay'}}
    ]
    db.remove(q.network.id == '114')
    assert db.search(q.network.id == '114') == []
Example #7
0
class TinyDBStore(object):
    def __init__(self):
        self.drafts_db = TinyDB('event_drafts.json')
        self.events_db = TinyDB('events.json')

    # Drafts
    def contains_draft(self, user_id):
        return self.drafts_db.contains(Query().user_id == user_id)

    def new_draft(self, user_id):
        if self.contains_draft(user_id):
            self.drafts_db.remove(Query().user_id == user_id)

        self.drafts_db.insert({
            'user_id': user_id,
            'current_field': 0,
            'event': {}
        })

    def update_draft(self, user_id, event, current_field):
        self.drafts_db.update({
            'user_id': user_id,
            'current_field': current_field,
            'event': event
        }, Query().user_id == user_id)

    def get_draft(self, user_id):
        return self.drafts_db.get(Query().user_id == user_id)

    def remove_draft(self, user_id):
        self.drafts_db.remove(Query().user_id == user_id)

    # Events
    def insert_event(self, event):
        event_id = self.events_db.insert(event)
        event['id'] = event_id
        return event

    def update_event(self, event):
        self.events_db.update(event, eids=[event.eid])

    def remove_event(self, event):
        self.events_db.remove(eids=[event['id']])

    def get_events(self, user_id, name=None):
        if name:
            return self.events_db.search((Query().user_id == user_id) & (Query().name.test(lambda v: name in v)))
        return self.events_db.search(Query().user_id == user_id)

    def get_event(self, event_id):
        return self.events_db.get(eid=int(event_id))
Example #8
0
File: clean.py Project: labase/eica
 def new_database_report_user_data(self):
     bank = TinyDB(JSONDBOUT)
     values = bank.search(self.query.user.exists())
     report = "nome:{user: >40}  idade: {idade: >4}   ano: {ano}   dia+hora: {hora} genero: {sexo} "
     for i, name in enumerate(values):
         jogadas = sum(len(copy["jogada"])
                       for copy in bank.search(self.query.user == name["user"]))
         tempos = [copy["jogada"]
                   for copy in bank.search(self.query.user == name["user"])]
         tempos = [lance["tempo"] for copy in tempos for lance in copy]
         # tempo = strptime(max(tempos), "%c")-strptime(min(tempos), "%c")
         tempo = (max(tempos)) - (min(tempos))
         print("{:3d}".format(i), "Lances: {:4d}".format(jogadas),
               "T:{:>9}".format(str(tempo)), report.format(**name))
Example #9
0
def check_prediction_cache(
        region_id,
        type_id,
        cache_path=CACHE_PATH,
        db_filename='prophet.json'
):
    """check tinyDB for cached predictions

    Args:
        region_id (int): EVE Online region ID
        type_id (int): EVE Online type ID
        cache_path (str): path to caches
        db_filename (str): name of tinydb

    Returns:
        pandas.DataFrame: cached prediction

    """
    utc_today = datetime.utcnow().strftime('%Y-%m-%d')

    prediction_db = TinyDB(path.join(cache_path, db_filename))

    raw_data = prediction_db.search(
        (Query().cache_date == utc_today) &
        (Query().region_id == region_id) &
        (Query().type_id == type_id)
    )

    prediction_db.close()

    if raw_data:
        panda_data = pd.read_json(raw_data[0]['prediction'])
        return panda_data
    else:
        return None
Example #10
0
def crawl(sr=0, er=3):
    archive = dict()
    url = "https://community.topcoder.com/tc?module=ProblemArchive&sr=%d&er=%d" % (sr, er)
    print "requesting seed page..."
    r = requests.get(url)
    html = h.unescape(r.content.decode('utf-8'))
    doc = pq(html)
    for i in doc('table.paddingTable2').eq(2).children()[3:]:
        round_name = pq(i).children().eq(2).find('a').text()
        sub_url = pq(i).children().eq(2).find('a').attr.href
        if sub_url is not None:
            rid = sub_url.split('rd=')[-1]
            archive[round_name] = {'rid': rid, 'round': round_name}
    db = TinyDB("data/db.json")
    tot = len(archive.values())
    cur = 0
    prob_cnt = 0
    for k in archive.values():
        problems = crawl_round(k['rid'], k['round'])
        print 'parse result:'
        for p in problems:
            for pk, pv in p.items():
                print "%-15s:   %s" % (pk, pv)
            prob_cnt += 1
            q = Query()
            if not db.search(q.name == p['name']):
                print '>>>>>>> insert problem: %s' % p['name']
                db.insert(p)
            print '-' * 10
        cur += 1
        print '*' * 10, 'finish', k['round'], ',tot rounds:', tot, 'cur round:', cur, 'round problems:', len(problems), '*' * 10
    print 'done, total round: %d, total problems: %d' % (cur, prob_cnt)
Example #11
0
def index():
    form = SearchForm()

    query = request.args.get('query', '').strip()

    db = TinyDB(recipyGui.config.get('tinydb'))

    if not query:
        runs = db.all()
    else:
        # Search run outputs using the query string
        runs = db.search(
            where('outputs').any(lambda x: listsearch(query, x)) |
            where('inputs').any(lambda x: listsearch(query, x)) |
            where('script').search(query) |
            where('notes').search(query) |
            where('unique_id').search(query))
    runs = sorted(runs, key = lambda x: parse(x['date'].replace('{TinyDate}:', '')) if x['date'] is not None else x['eid'], reverse=True)

    run_ids = []
    for run in runs:
        if 'notes' in run.keys():
            run['notes'] = str(escape(run['notes']))
        run_ids.append(run.eid)

    db.close()

    return render_template('list.html', runs=runs, query=query, form=form,
                           run_ids=str(run_ids),
                           dbfile=recipyGui.config.get('tinydb'))
Example #12
0
class MovedFileHandler(events.FileSystemEventHandler):
    def __init__(self, *args, **kwargs):
        self.dry_run = kwargs.pop('dry_run', True)
        self.verbose = kwargs.pop('verbose', True)
        super().__init__(*args, **kwargs)
        os.chdir(os.path.expanduser('~'))
        self.db = TinyDB('.sdpp-sync.json')

    def on_moved(self, event):
        path = event.src_path
        if not path.endswith('.pdf') or path.endswith('.PDF'):
            return
        home = os.path.expanduser('~/')
        dest = event.dest_path
        if path.startswith(home):
            path = path[len(home):]
        if dest.startswith(home):
            dest = dest[len(home):]
        box_old = to_sony_filename(path)
        box_new = to_sony_filename(dest)
        os.rename(box_old, box_new)
        q = Query()
        elem = self.db.search(q.box == box_old)
        elem['box'] = box_new
        elem['modtime'] = os.stat(box_new).st_mtime
        elem['paperpile'] = dest
Example #13
0
def process_and_add_one(pdf_path):
    pdf_name = pdf_path.split("/")
    pdf_name = pdf_name[-1]
    directory = pdf_path[0 : -len(pdf_name)]
    stripped_name = pdf_name[0:-4]
    title_path = title_dir + "/" + stripped_name + ".xml"
    extract_title(pdf_path, title_path)
    # check if title extraction worked, otherwise stop with this one
    tf = open(title_path, "r")
    txml = tf.read()
    if txml == "title extraction failed":
        return None

    # build dictionary with info we've got
    tf = open(title_path, "r")
    txml = tf.read()
    txml = txml.split(">")
    title = "title not found"
    for line in txml:
        if "</title" in line:
            title = line[0:-7]
            print title
            break

    # save nice text version of title
    txt_name_path = title_path[0:-4] + ".txt"
    ftxt = open(txt_name_path, "a")
    ftxt.write(title)
    if title == "title not found":
        return None

    # if title was found, get DOI from it
    currDOI = get_DOI_from_title(title)
    # open/create tiny db
    db = TinyDB(db_loc)
    # make sure the paper isnt in the db already
    paper = Query()
    gotit = db.search(paper.ownDOI == currDOI)
    if gotit:
        return currDOI

    text_path_xml = text_dir + "/" + stripped_name + ".xml"
    text_path_txt = text_dir + "/" + stripped_name + ".txt"
    if not extract_text(pdf_path, text_path_xml, text_path_txt):
        print ("text extraction failed")
        return None

    # only extract bibtex if you don't have it already, because this is the long part
    # TODO: Return before doing bib extraction
    bib_path = bib_dir + "/" + stripped_name + ".bib"
    if not extract_bibtex(pdf_path, bib_path):
        print ("caught in the new code")
        return None

    refDOIs = get_ref_list_DOIs(bib_path)

    new_dict = {"ownDOI": currDOI, "refDOIs": refDOIs, "filename": stripped_name}
    db.insert(new_dict)
    return currDOI
Example #14
0
def test_gc(tmpdir):
    # See https://github.com/msiemens/tinydb/issues/92
    path = str(tmpdir.join('db.json'))
    table = TinyDB(path).table('foo')
    table.insert({'something': 'else'})
    table.insert({'int': 13})
    assert len(table.search(where('int') == 13)) == 1
    assert table.all() == [{'something': 'else'}, {'int': 13}]
Example #15
0
def api():
    db = TinyDB(DB_FILENAME)
    results = db.search((Query().hidden == False) | (Query().starred == True))
    db.close()
    for result in results:
        result['eid'] = result.eid
        result['url'] = url_for('goto', eid=result.eid)
    results.sort(key=lambda r: r['created_at'])
    return json.dumps(results)
Example #16
0
class BotHelper:
	def __init__(self, bot):
		self.db 	= TinyDB('db.json')
		self.bot 	= bot

	def executeAssignment(self, chat_id, assignment):
		if assignment['command'] == 'add':
			entries = Query()
			if self.db.search(entries.trigger == assignment['trigger']):
				self.bot.sendMessage(chat_id=chat_id, text='No duplicates allowed for now')
				return False
			else:
				if assignment['response_type'] == 'image' or assignment['response_type'] == 'gif':
					filename 	= assignment['response'].split('/')[-1]
					extention 	= filename.split('.')[-1]
					urllib.request.urlretrieve(assignment['response'], 'media/images/' + assignment['trigger'] + '.' + extention)
					assignment['response'] = 'media/images/' + assignment['trigger'] + '.' + extention

				self.db.insert({
								'trigger':		assignment['trigger'],
								'response_type':assignment['response_type'],
								'response':		assignment['response'],
								})
		if assignment['command'] == 'del':
			self.db.remove(where('trigger') == assignment['trigger'])
		if assignment['command'] == 'list':
			entries = self.db.all()
			for entry in entries:
				self.bot.sendMessage(chat_id=chat_id, text=entry)

	def executeTrigger(self, chat_id, message):
		words 	= message.split(' ') 
		entries = Query()
		for word in words:
			entry = self.db.search(entries.trigger == word)
			if entry:
				if entry[0]['response_type'] == 'text':
					self.bot.sendMessage(chat_id=chat_id, text=entry[0]['response'])
				if entry[0]['response_type'] == 'image':
					img = open(entry[0]['response'], 'rb')
					self.bot.sendPhoto(chat_id=chat_id, photo=img)
				if entry[0]['response_type'] == 'gif':
					img = open(entry[0]['response'], 'rb')
					self.bot.sendDocument(chat_id=chat_id, document=img)
Example #17
0
def query_citations_by_DOI(DOI):
    db = TinyDB(db_loc)
    # make sure the paper isnt in the db already
    paper = Query()
    gotit = db.search(paper.ownDOI == DOI)  # returns of list of dicts
    result_dict = gotit[0]
    if gotit:
        return result_dict["refDOIs"]
    else:
        return None
Example #18
0
def get_corpus_path(name: str) -> [str, None]:
    """
    Get corpus path

    :param string name: corpus name
    """
    db = TinyDB(corpus_db_path())
    temp = Query()

    if len(db.search(temp.name == name)) > 0:
        path = get_full_data_path(db.search(temp.name == name)[0]["file"])
        db.close()

        if not os.path.exists(path):
            download(name)

        return path

    return None
Example #19
0
def remove(name):
    db=TinyDB(path_db_)
    temp = Query()
    data=db.search(temp.name==name)
    if len(data)>0:
        path=get_file(name)
        os.remove(path)
        db.remove(temp.name==name)
        return True
    return False
Example #20
0
def test_query_cache():
    db = TinyDB(storage=MemoryStorage)
    db.insert_multiple([
        {'name': 'foo', 'value': 42},
        {'name': 'bar', 'value': -1337}
    ])

    query = where('value') > 0

    results = db.search(query)
    assert len(results) == 1

    # Modify the db instance to not return any results when
    # bypassing the query cache
    db._table_cache[TinyDB.DEFAULT_TABLE]._read = lambda: {}

    # Make sure we got an independent copy of the result list
    results.extend([1])
    assert db.search(query) == [{'name': 'foo', 'value': 42}]
Example #21
0
def hide_all():
    db = TinyDB(DB_FILENAME)
    eids = [
        r.eid for r in db.search(
            (Query().hidden == False) & (Query().starred == False)
        )
    ]
    db.update({'hidden': True}, eids=eids)
    db.close()
    return 'OK'
Example #22
0
def greet(userin):
    """The top-level method to greet the user with message like "*Good morning, sir.*".

    Args:
        userin:  :class:`dragonfire.utilities.TextToAction` instance.

    Returns:
        str:  Response.
    """

    (columns, lines) = shutil.get_terminal_size()
    print(columns * "_" + "\n")
    time = datetime.datetime.now().time()

    global user_full_name
    global user_prefix
    global config_file

    command = "getent passwd $LOGNAME | cut -d: -f5 | cut -d, -f1"
    user_full_name = os.popen(command).read()
    user_full_name = user_full_name[:-1]  # .decode("utf8")
    home = expanduser("~")
    config_file = TinyDB(home + '/.dragonfire_config.json')
    callme_config = config_file.search(Query().datatype == 'callme')
    if callme_config:
        user_prefix = callme_config[0]['title']
    else:
        gender_config = config_file.search(Query().datatype == 'gender')
        if gender_config:
            user_prefix = GENDER_PREFIX[gender_config[0]['gender']]
        else:
            gender = Classifier.gender(user_full_name.split(' ', 1)[0])
            config_file.insert({'datatype': 'gender', 'gender': gender})
            user_prefix = GENDER_PREFIX[gender]

    if time < datetime.time(12):
        time_of_day = "morning"
    elif datetime.time(12) < time < datetime.time(18):
        time_of_day = "afternoon"
    else:
        time_of_day = "evening"
    userin.execute(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'")
    return userin.say(" ".join(["Good", time_of_day, user_prefix]))
Example #23
0
def fetch_split_cache_data(
        region_id,
        type_id,
        split_date=None,
        keep_columns=KEEP_COLUMNS
):
    """get data from cache

    Args:
        region_id (int): EVE Online region id
        type_id (int): EVE Online type id
        keep_colums (list): sort columns to keep in DataFrame
    Returns:
        pandas.DataFrame: pandas collection of data
            ['date', 'avgPrice', 'highPrice', 'lowPrice', 'volume', 'orders']

    """
    print(api_config.SPLIT_CACHE_FILE)
    db_handle = TinyDB(api_config.SPLIT_CACHE_FILE)

    if split_date:
        split_data = db_handle.search(
            (Query().region_id == int(region_id)) &
            (Query().type_id == int(type_id)) &
            (Query().date <= split_date)
        )
    else:
        split_data = db_handle.search(
            (Query().region_id == int(region_id)) &
            (Query().type_id == int(type_id))
        )
    if not split_data:
        raise exceptions.NoSplitDataFound()

    split_data = pd.DataFrame(split_data)
    split_data = split_data[keep_columns]
    split_data.sort_values(
        by='date',
        ascending=False,
        inplace=True
    )

    return split_data
Example #24
0
    def CheckUserExists(self, username, chatid):
        db = TinyDB('users.json')
        Username = Query()
        user = db.search((Username.username == username) &  (Username.chatid == chatid))
     
        if user == []:
            return 1

        else:
            return 0
Example #25
0
def search(args):
  if not os.path.exists(os.path.dirname(DBFILE)):
      os.mkdir(os.path.dirname(DBFILE))

  db = TinyDB(DBFILE) 

  filename = args['<outputfile>']

  if args['--fuzzy']:
    results = db.search(where('outputs').any(lambda x: re.match(".+%s.+" % filename, x)))
  elif args['--regex']:
    results = db.search(where('outputs').any(lambda x: re.match(filename, x)))
  else:
    results = db.search(where('outputs').any(os.path.abspath(filename)))

  def change_date(result):
    result['date'] = result['date'].replace('{TinyDate}:', '')
    return result

  results = [change_date(result) for result in results]

  # Sort the results
  results = sorted(results, key = lambda x: parse(x['date']))

  if len(results) == 0:
      print("No results found")
  else:
      if args['--all']:
          for r in results:
              print_result(r)
              print("-"*40)
      else:
          print_result(results[-1])
          if len(results) > 1:
              print("** Previous runs creating this output have been found. Run with --all to show. **")

          if args['--diff']:
            if 'diff' in results[-1]:
              print("\n\n")
              print(results[-1]['diff'])

  db.close()
Example #26
0
def compute_vector_for_DOI(DOI):
    db = TinyDB(db_loc)
    paper = Query()
    this_paper_dict = db.search(paper.ownDOI == DOI)[0] #returns entry as dictionary
    name = this_paper_dict['filename']
    ##here we call marcellos code
    vector = keyExtract.getRakeKeywords(text_dir+name+'.txt')
    vectore = normVectorGen(vector)
    out_path = vector_dir + name+'.pkl'
    output = open(out_path,'wb')
    pickle.dump(vector,output)
Example #27
0
def notify_subscribers(article):
    logger.info('notify subscribers for article %s' % article)
    db = TinyDB(pecosys.get_config('global', 'cwd') + '/db.json')
    for item in db.search(where('article') == article):
        logger.info(item)
        to_email = item['email']
        logger.info("notify reader %s for article %s" % (to_email, article))
        unsubscribe_url = pecosys.get_config('subscription', 'url') + '?email=' + to_email + '&article=' + article
        email_body = get_template('notify_subscriber').render(article_url=item['url'],
                                                              unsubscribe_url=unsubscribe_url)
        subject = get_template('notify_message').render()
        mail(pecosys.get_config('subscription', 'from_email'), to_email, subject, email_body)
Example #28
0
class Test_005_Search_Not_existing_data_by_valid_query_Function(unittest.TestCase):
	def setUp(self):
		self.db = TinyDB('db.json')

	def tearDown(self):
		self.db.purge()
		self.db.all()

	def test_simple_search_not_exist(self):
		print("case 5 search Non-existing data by valid query")
		result=self.db.search(where('Name') == 'Wendy')
		self.assertEqual(result,[])
Example #29
0
    def RemovePokemon(self, username, chatid, pokemon):
        db = TinyDB('users.json')
        Username = Query()
        user = db.search((Username.username == username) &  (Username.chatid == chatid))
        # print(pokemon)
        my_pokemon_cur = user[0]['pokemon'][pokemon]
        # print(my_pokemon_cur)
        my_pokemon_new = self.DecrementPokeArr(user[0]['pokemon'], pokemon)
        # print (my_pokemon_new)
        db.update({'pokemon': my_pokemon_new}, ((Username.username == username ) & (Username.chatid == chatid)))

        pass # RETURN: check bool
Example #30
0
def update_similarity_matrix(newDOI):
    if(os.path.isfile(sim_matrix)):    
        db = TinyDB(db_loc)
        paper = Query()
        this_paper_dict = db.search(paper.ownDOI == newDOI)[0] #returns entry as dictionary
        name = this_paper_dict['filename']
        vector_path = vector_dir + name+'.pkl'
        vec_file = open(vector_path, 'rb')
        newVector = pickle.load(vec_file)
        #now load the matrix
        mat_file = open(sim_matrix,'rb')
        mat_list = pickle.load(mat_file)
        index_dict = mat_list[0];
        mat = mat_list[1];
        if newDOI in index_dict:
            return False
        
        num_ent =   np.size(mat,axis = 0)
##        if sizemat == 1:
##            num_ent = 1
##        else:
##            num_ent = sizemat[0]
            
        index_dict[newDOI] = num_ent

        #extend the matrix to appropriate size
        mat = np.vstack((mat,np.zeros((1,num_ent))))
        mat = np.hstack((mat,np.zeros((num_ent+1,1))))
        print mat
        
        #get all the entries of the db
        all_DOIs = db.all()
        for line in all_DOIs:
            print vector_path
            name = line['filename']
            vector_path = vector_dir+name+'.pkl'
            print vector_path
            vec_file = open(vector_path,'rb')
            vec = pickle.load(vec_file)
            closeness = vector_mult_and_add(newVector,vec)
            newInd = index_dict[newDOI]
            oldInd = index_dict[line['ownDOI']]
            mat[newInd][oldInd] = closeness;
            mat[oldInd][newInd] = closeness;

        output_mat_list = [index_dict,mat]
        output = open(sim_matrix,'wb')
        pickle.dump(output_mat_list,output)   
        return None
    else:
        init_similarity_matrix(newDOI)
        return None
Example #31
0
async def reply(activity, bot, data):
    query = Query()
    db = TinyDB('./db.json')
    result = db.search(query.userid == activity.from_property.id)
    scraper = Scraper()
    if len(result) > 0:
        result = result[0]
        rollno = str(result['rollno'])
        wak = str(result['wak'])
        scraper.authenticate(rollno, wak)
        conducted, attended = scraper.getPeriods()
        percentageifpresent = round(((attended + 7) / (conducted + 7) * 100),
                                    2)
        percentageifabsent = round(((attended) / (conducted + 7) * 100), 2)
        response = "Your attendance will be %.2f if present, otherwise %.2f" % (
            percentageifpresent, percentageifabsent)
        await bot.send_text_activity(activity, response)
        if percentageifabsent > 95:
            responses = [
                "Go ahead and take a break, You deserve it. 🤗",
                "Seriously, take a day off. 😒",
                "You've been regular to the college. You've earned a day for yourselves. 👍"
            ]
        elif percentageifabsent > 90:
            responses = [
                "Well, your attendance % will still be quite good. But it's upto you. 😊",
                "You are very much eligible to skip college tomorrow. 😁",
                "You can skip college without any guilt. 😏"
            ]
        elif percentageifabsent > 85:
            responses = [
                "It is probably okay to skip college tomorrow. Exercise some caution though. 🙂",
                "You've been doing great. Try to keep it up... or just stay back for once. 😅",
                "Aaj Jaane Ki Zidd Naa Karo.. 😝"
            ]
        elif percentageifabsent > 80:
            responses = [
                "I'd tell you to go to the class par meri kaun sunta hai? 🙄",
                "If you think you can manage 75%, you are more than welcome to try. 😬",
                "\"Laziness may seem attractive, but work gives satisfaction.\" - Anne Frank 😇"
            ]
        elif percentageifabsent > 75:
            responses = [
                "I see you like to live dangerously ☠... But give a little thought to it.",
                "You can probably get away with this one. 😈",
                "You've been skipping college way too much but you just might get another day."
            ]
        elif percentageifabsent > 65:
            responses = [
                "Please just get off your butt and go to the class. 😒",
                "I know the bed looks very cozy right now but you should definitely go. 😐",
                "Your laziness is really not worth paying that extra money to the college. 😔"
            ]
        else:
            responses = [
                "Beta tumse naa ho paayega 😐",
                "I'm sorry but you probably can't afford to take even a single day off. 🤐",
                "Uhmm.. No way you can skip college right now. 😑"
            ]
        reply = random.choice(responses)
        await bot.send_text_activity(activity, reply)
def get_raw_entries(dataset, file_prefix):
	db = TinyDB("db/" + dataset + '-raw.json')
	query = Query()
	results = db.search(query.filename.matches(file_prefix))
	entries = list(map(lambda r: RawEntry(r), results))
	return entries
Example #33
0
from tinydb import Query, TinyDB

db = TinyDB('./db.json')

User = Query()

ing = db.insert({
    'original_url': 'https://password-generator.josiasdev.best',
    'short_url': 'bawzz'
})
print(ing)

data = db.search(
    User['original_url'] == 'https://password-generator.josiasdev.best')

print(data)
Example #34
0
def get_record(s_id):
    db = TinyDB(cfg.config['db_name'])
    Record = Query()
    record = db.search(Record.asset_id == s_id)
    db.close()
    return record
Example #35
0
class Proxy(object):
    def __init__(self, config):
        self.c = config
        self.ldb = None
        self.rdb = None
        self.tag = Query().noop()
        self.req = None
        if config.local:
            try:
                self.ldb = TinyDB(config.local,
                                  storage=CachingMiddleware(JSONStorage))
            except Exception:
                self.ldb = TinyDB(storage=MemoryStorage)
        else:
            self.ldb = TinyDB(storage=MemoryStorage)
        if config.url:
            auth = None
            if config.user:
                auth = (config.user, click.prompt("password", hide_input=True))
            if config.url.startswith("http"):
                dbclass = CouchDB
            elif config.url.startswith("mongodb"):
                dbclass = MongoDB
            try:
                self.rdb = dbclass(config.url, auth=auth, verify=config.verify)
            except Exception:
                self.rdb = None

    def set_tag(self, tag=None):
        self.tag = (where("tag") == tag) if (tag
                                             is not None) else Query().noop()

    def insert_multiple(self, docs):
        self.ldb.insert_multiple(docs)

    def contains(self, q=None, **kargs):
        if q is None:
            q = self.tag
        for k in kargs:
            q &= where(k) == kargs[k]
        if self.rdb:
            return self.rdb.contains(q._hash, **kargs)
        return self.ldb.contains(q)

    def search(self, q=None, **kargs):
        if q is None:
            q = self.tag
        else:
            q = self.tag & q
        for k in kargs:
            q &= where(k) == kargs[k]
        if self.rdb:
            return list(self.rdb.search(q._hash, **kargs))
        return self.ldb.search(q)

    def get(self, q=None, **kargs):
        if q is None:
            q = self.tag
        for k in kargs:
            q &= where(k) == kargs[k]
        if self.rdb:
            return self.rdb.get(q._hash, **kargs)
        return self.ldb.get(q)

    def cleanup_local(self):
        D = {}
        for e in self.ldb.search(self.tag):
            k = "%s := %s" % (e["id"], e["val"])
            if not k in D:
                D[k] = [e.doc_id]
            else:
                D[k].append(e.doc_id)
        for v in D.values():
            if len(v) > 1:
                self.ldb.remove(doc_ids=v[1:])

    def cleanup(self):
        self.rdb.cleanup(self)

    def find_matching_types(self, Locs, req=None, psize=0):
        if self.rdb is not None:
            self.rdb.find_matching_types(Locs, req, psize)
        return Locs

    def close(self):
        self.ldb.close()
Example #36
0
            # interpolate_coordination=True

    # assert not interpolate_coordination and interpolate_charge

    if variant:  # todo warning for missing variants
        for v in variant:
            fltrs *= table.variant.apply(lambda x: v in x)

    result = df.loc[fltrs.astype(bool), target]

    if result.index.size == 1:
        return result.values[0]  # return the specific number
    else:
        return result  # return the series


# update doi links for radii

get_ionic_radii.__doc__ = get_ionic_radii.__doc__.replace(
    "shannon1976", sphinx_doi_link("10.1107/S0567739476001551"))
get_ionic_radii.__doc__ = get_ionic_radii.__doc__.replace(
    "whittaker_muntus1970", sphinx_doi_link("10.1016/0016-7037(70)90077-3"))
# generate sets
__db__ = TinyDB(
    str(pyrolite_datafolder(subfolder="geochem") / "geochemdb.json"))
__common_elements__ = set(
    __db__.search(Query().name == "elements")[0]["collection"])
__common_oxides__ = set(
    __db__.search(Query().name == "oxides")[0]["collection"])
__db__.close()
Example #37
0
class Learn():
    def __init__(self):
        self.replacements = collections.OrderedDict(
        )  # Create an ordered dictionary
        self.replacements["I'M"] = "YOU-ARE"
        self.replacements["I-WAS"] = "YOU-WERE"
        self.replacements["I"] = "YOU"
        self.replacements["MY"] = "YOUR"
        self.replacements["MINE"] = "YOURS"
        self.replacements["MYSELF"] = "YOURSELF"
        self.replacements["OURSELVES"] = "YOURSELVES"
        home = expanduser("~")  # Get the home directory of the user
        self.db = TinyDB(
            home + '/.dragonfire_db.json'
        )  # This is where we store the database; /home/USERNAME/.dragonfire_db.json
        self.nlp = spacy.load(
            'en')  # Load en_core_web_sm, English, 50 MB, default model

    # Entry function for this class. Dragonfire calls only this function. It does not handle TTS.
    def respond(self, com):
        forget = "^(?:FORGET|UPDATE) (?:EVERYTHING YOU KNOW ABOUT |ABOUT )?(?P<subject>.*)"
        capture = re.search(forget, com)
        if capture:
            if self.db.remove(
                    Query().subject == self.pronoun_fixer(
                        capture.group('subject'))
            ):  # if there is a record about the subject in the database then remove that record and...
                return "OK, I FORGOT EVERYTHING I KNOW ABOUT " + self.mirror(
                    capture.group('subject'))
            else:
                return "I DON'T EVEN KNOW ANYTHING ABOUT " + self.mirror(
                    capture.group('subject'))

        define = "(?:PLEASE |COULD YOU )?(?:DEFINE|EXPLAIN|TELL ME ABOUT|DESCRIBE) (?P<subject>.*)"
        capture = re.search(define, com)
        if capture:
            return self.db_getter(capture.group('subject'))

        doc = self.nlp(
            com.decode('utf-8')
        )  # Command(user's speech) must be decoded from utf-8 to unicode because spaCy only supports unicode strings, self.nlp() handles all parsing
        subject = [
        ]  # subject list (subjects here usually are; I'M, YOU, HE, SHE, IT, etc.)
        prev_type = None  # type of the previous noun phrase
        for np in doc.noun_chunks:  # Iterate over the noun phrases(chunks) TODO: Cover 'dobj' also; doc = nlp(u'DESCRIBE THE SUN') >>> (u'THE SUN', u'SUN', u'dobj', u'DESCRIBE')
            # Purpose of this if statement is completing possessive form of nouns
            if np.root.dep_ == 'pobj':  # if it's an object of a preposition
                if prev_type == 'nsubj':  # and the previous noun phrase's type was nsubj(nominal subject) then (it's purpose is capturing subject like MY PLACE OF BIRTH)
                    subject.append(
                        np.root.head.text.encode('utf-8')
                    )  # append the parent text from syntactic relations tree (example: while nsubj is 'MY PLACE', np.root.head.text is 'OF')
                    subject.append(
                        np.text.encode('utf-8')
                    )  # append the text of this noun phrase (example: while nsubj is 'MY PLACE', np.text is 'BIRTH')
                prev_type = 'pobj'  # assign the previous type as pobj
            if np.root.dep_ == 'nsubj':  # if it's a nsubj(nominal subject)
                if np.root.tag_ != 'WP' and prev_type not in [
                        'pobj', 'nsubj'
                ]:  # "wh-" words are also considered as nsubj(nominal subject) but they are out of scope. This is why we are excluding them.
                    subject.append(np.text.encode(
                        'utf-8'))  # append the text of this noun phrase
                prev_type = 'nsubj'  # assign the previous type as nsubj(nominal subject)
                if np.root.tag_ == 'WP':
                    prev_type = 'WP'
            if np.root.dep_ == 'attr':  # if it's an attribute
                if prev_type not in [
                        'pobj', 'nsubj'
                ]:  # and the previous noun phrase's type was nsubj(nominal subject)
                    subject.append(np.text.encode(
                        'utf-8'))  # append the text of this noun phrase
                prev_type = 'attr'
        subject = ' '.join(
            subject).strip()  # concatenate all noun phrases found
        if subject:  # if the subject is not empty
            wh_found = False
            for word in doc:  # iterate over the each word in the given command(user's speech)
                if word.tag_ in [
                        'WDT', 'WP', 'WP$', 'WRB'
                ]:  # check if there is a "wh-" question (we are determining that if it's a question or not, so only accepting questions with "wh-" form)
                    wh_found = True
            if wh_found:  # if that's a question
                straight = self.db_getter(
                    subject)  # get the answer from the database
                if straight is None:
                    return self.db_getter(subject,
                                          True)  # if nothing found then invert
                return straight
            else:
                verb_found = False
                verbtense = None  # verbtense is the am/is/are of the main sentence
                clause = []  # is the information that we need to acknowledge
                for word in doc:
                    if verb_found:  # get the all words comes after the first verb which will be our verbtense
                        if word.pos_ != 'PUNCT':  # exclude punctuations
                            clause.append(word.text.encode('utf-8'))
                    if word.pos_ == 'VERB' and not verb_found:  # if that's a verb and verb does not found yet then
                        verb_found = True  # verb is found
                        verbtense = word.text.encode(
                            'utf-8')  # append it to verbtense
                clause = ' '.join(clause).strip()  # concatenate the clause
                return self.db_setter(subject, verbtense, clause,
                                      com)  # set the record to the database

    # Function to get a record from the database
    def db_getter(self, subject, invert=False):
        if invert:
            result = self.db.search(
                Query().clause == subject
            )  # make a database search by giving subject string (inverted)
        else:
            result = self.db.search(
                Query().subject ==
                subject)  # make a database search by giving subject string
        if result:  # if there is a result
            dictionary = {}
            for row in result:  # iterate over the rows of the result
                if row['verbtense'] not in dictionary:  # if the verbtense is not in the keys of the dictionary
                    dictionary[row['verbtense']] = []  # then add it
                if row['clause'] not in dictionary[row[
                        'verbtense']]:  # if the clause is not in the value like; dictionary['is']
                    dictionary[row['verbtense']].append(
                        row['clause'])  # then append the clause
            if invert:
                answer = row[
                    'subject']  # in WHO questions subject is actually the clause so we learn the subject from db
            else:
                answer = subject  # the answer we will return
            first_verbtense = False
            for key, value in dictionary.iteritems(
            ):  # iterate over the dictionary defined and assigned on above
                if not first_verbtense:  # if the first verbtense assignment does not made yet
                    answer += ' ' + str(key)  # concatenate with a whitespace
                    first_verbtense = True
                else:
                    answer += ', ' + str(
                        key)  # otherwise concatenate with a comma + whitespace
                first_clause = False
                for clause in value:  # iterate over the clauses of the key
                    if not first_clause:  # if the first verbtense assignment does not made yet
                        answer += ' ' + clause  # concatenate with a whitespace
                        first_clause = True
                    else:
                        answer += ' AND ' + clause  # otherwise concatenate with ' AND '
            return self.mirror(
                answer)  # mirror the answer (for example: I'M to YOU ARE)
        else:
            return None  # if there is no result return None

    # Function to set a record to the database
    def db_setter(self, subject, verbtense, clause, com):
        if not self.db.search(
            (Query().subject == subject) & (Query().verbtense == verbtense)
                & (Query().clause == clause)
        ):  # if there is no exacty record on the database then
            self.db.insert({
                'subject': subject,
                'verbtense': verbtense,
                'clause': clause
            })  # insert the given data
        return "OK, I GET IT. " + self.mirror(
            com)  # mirror the command(user's speech) and return it to say

    # Function to mirror the answer (for example: I'M to YOU ARE)
    def mirror(self, answer):
        answer = answer.upper()  # make the given string fully uppercase
        answer = self.forward_replace(answer)
        result = []
        for word in answer.split():  # for each word in the answer
            replaced = False
            for key, value in self.replacements.iteritems(
            ):  # iterate over the replacements
                if word == key:  # if the word is equal to key
                    result.append(value)  # append the value
                    replaced = True
            if word in ["WE", "US"]:
                result.append("YOU")
                replaced = True
            elif word == "OUR":
                result.append("YOUR")
                replaced = True
            elif word == "OURS":
                result.append("YOURS")
                replaced = True
            if not replaced:
                result.append(word)  # otherwise append the word
        if ' '.join(result) != answer:  # if there is a replacement
            return self.backward_replace(' '.join(result))  # return the result
        result = []
        for word in answer.split():  # for each word in the answer
            replaced = False
            for value, key in self.replacements.iteritems(
            ):  # invert the process above
                if word == key:  # if the word is equal to key
                    result.append(value)  # append the value
                    replaced = True
            if not replaced:
                result.append(word)  # otherwise append the word
        return self.backward_replace(
            ' '.join(result))  # return the result anyway

    def forward_replace(self, string):
        string = string.replace("I WAS", "I-WAS")
        string = string.replace("YOU ARE", "YOU-ARE")
        string = string.replace("YOU WERE", "YOU-WERE")
        return string

    def backward_replace(self, string):
        string = string.replace("I-WAS", "I WAS")
        string = string.replace("YOU-ARE", "YOU ARE")
        string = string.replace("YOU-WERE", "YOU WERE")
        return string

    # Pronoun fixer to handle situations like YOU and YOURSELF
    def pronoun_fixer(self,
                      subject):  # TODO: Extend the context of this function
        subject = subject.upper()
        if 'YOURSELF' in subject:
            subject = subject.replace('YOURSELF', 'YOU')
        return subject
Example #38
0
from tinydb import TinyDB, Query

# https://tinydb.readthedocs.io/en/latest/usage.html

db = TinyDB("score.json")
db.purge()

db.insert({"name": "math", "score": 100})
db.insert({"name": "english", "score": 90})
db.insert({"name": "japanese", "score": 80})

S = Query()
print(db.count(S.score >= 90))
print(db.search(S.name.matches(".*h$")))
print(db.search(S.name.search("h$")))

print(db.search(S.name.test(lambda v: v in ["math", "japanese"])))
print(db.search((S.name == "math") | (S.name == "japanese")))
print(db.search((S.name == "math") & (S.score >= 80)))

# for collection, using all,any
Example #39
0
class JobDB:
    """Keeps a database of jobs, with a MD5 hash that encodes the function
    name, version, and all arguments to the function.
    """
    def __init__(self, path):
        self.db = TinyDB(path)
        self.lock = Lock()

    def get_result_or_attach(self, key, prov, running):
        job = Query()
        with self.lock:
            rec = self.db.get(job.prov == prov)

            if 'result' in rec:
                return 'retrieved', rec['key'], rec['result']

            job_running = rec['key'] in running
            wf_running = rec['link'] in running.workflows

            if job_running or wf_running:
                self.db.update(attach_job(key), job.prov == prov)
                return 'attached', rec['key'], None

            print(
                "WARNING: unfinished job in database. Removing it and "
                " rerunning.",
                file=sys.stderr)
            self.db.remove(eids=[rec.eid])
            return 'broken', None, None

    def job_exists(self, prov):
        job = Query()
        with self.lock:
            return self.db.contains(job.prov == prov)

    def store_result(self, key, result):
        job = Query()
        with self.lock:
            if not self.db.contains(job.key == key):
                return

        self.add_time_stamp(key, 'done')
        with self.lock:
            self.db.update({'result': result, 'link': None}, job.key == key)
            rec = self.db.get(job.key == key)
            return rec['attached']

    def new_job(self, key, prov, job_msg):
        with self.lock:
            self.db.insert({
                'key': key,
                'attached': [],
                'prov': prov,
                'link': None,
                'time': {
                    'schedule': time_stamp()
                },
                'version': job_msg['data']['hints'].get('version'),
                'function': job_msg['data']['function'],
                'arguments': job_msg['data']['arguments']
            })

        return key, prov

    def add_link(self, key, ppn):
        job = Query()
        with self.lock:
            self.db.update({'link': ppn}, job.key == key)

    def get_linked_jobs(self, ppn):
        job = Query()
        with self.lock:
            rec = self.db.search(job.link == ppn)
            return [r['key'] for r in rec]

    def add_time_stamp(self, key, name):
        def update(r):
            r['time'][name] = time_stamp()

        job = Query()
        with self.lock:
            self.db.update(update, job.key == key)
Example #40
0
import random
import csv
from tinydb import TinyDB, Query

#file = './test.csv'
file = './docs/aidu_yaichi.csv'
db = TinyDB('yaichidb.json')
db.purge()

with open(file, 'r', encoding='utf-8') as f:
    reader = csv.reader(f)
    id = 0
    for row in reader:
        if row == []:
            continue
        id += 1
        db.insert({
            'id': id,
            'song': row[0],
            'source': row[1],
            'comment': row[2]
        })

f.close()

#print(db.all())
print(len(db.all()))
qry = Query()
print(db.search(qry['id'] == random.randint(1, len(db.all()))))
# print(db.all())
Example #41
0
    async def support(self, ctx, *reason):
        await ctx.message.delete(delay=0)
        db = TinyDB('db/tickets/requests.json')
        User = Query()
        count = db.count(User.guild_id == ctx.guild.id)
        db.insert({
            'request-owner': f'{ctx.message.author}',
            'guild_id': ctx.guild.id,
            'count': count
        })
        for category in ctx.guild.categories:
            if category.name == "Tickets":
                db = TinyDB('db/tickets/ticketroles.json')
                Role = Query()
                li = db.search(Role.guild_id == ctx.guild.id)
                for i in li:
                    role = i['role']
                    role1 = discord.utils.get(ctx.guild.roles, name=str(role))
                    role2 = discord.utils.get(ctx.guild.roles,
                                              name="Ticket on")
                    every = discord.utils.get(ctx.guild.roles,
                                              name="@everyone")
                    overwrites = {
                        role1:
                        discord.PermissionOverwrite(send_messages=True,
                                                    read_messages=True),
                        ctx.message.author:
                        discord.PermissionOverwrite(send_messages=False,
                                                    read_messages=True),
                        every:
                        discord.PermissionOverwrite(send_messages=False,
                                                    read_messages=False)
                    }
                    try:
                        rch = await ctx.message.guild.create_text_channel(
                            name=f'request-{count}',
                            overwrites=overwrites,
                            category=category)
                        await ctx.message.author.add_roles(role2)
                        embed = discord.Embed(
                            title="Ticket",
                            description=
                            f"**{ctx.message.author.mention}, your ticket has been accepted please wait until someone of the staff team opens it.**",
                            colour=0xf44e42,
                            timestamp=datetime.utcnow())
                        embed.add_field(
                            name="__Notice__",
                            value=
                            "**Any trolling will lead to unexpected consequences!**"
                        )
                        embed.add_field(
                            name="__Closing or Opening ticket__",
                            value=
                            "**If you wanna close the ticket just click on the chain lock 🔒 and to open it click on unlock 🔓.**"
                        )
                        embed.set_footer(icon_url=self.bot.user.avatar_url,
                                         text="?help for more help.")
                        msg = await rch.send(embed=embed)
                        emoji = "🔒"
                        emoji1 = "🔓"
                        await msg.add_reaction(emoji)
                        await msg.add_reaction(emoji1)
                        await asyncio.sleep(1)

                        def check(reaction, user):
                            for m in role1.members:
                                return user == m and str(
                                    reaction.emoji) == emoji or str(
                                        reaction.emoji) == emoji1

                        reaction, user = await self.bot.wait_for(
                            'reaction_add', check=check)
                        if str(reaction.emoji) == emoji:
                            embed2 = discord.Embed(
                                title="__Closing__",
                                description=
                                "**Channel will be deleted in 5 seconds 🛑.**",
                                timestamp=datetime.utcnow(),
                                colour=0xe88a84)
                            embed2.set_footer(
                                icon_url=self.bot.user.avatar_url,
                                text="?help for more help.")
                            await rch.send(embed=embed2)
                            await msg.delete(delay=0)
                            await asyncio.sleep(delay=5)
                            await ctx.message.author.remove_roles(role2)
                            await rch.delete()
                        if str(reaction.emoji) == emoji1:
                            await rch.set_permissions(ctx.message.author,
                                                      send_messages=True,
                                                      read_messages=True)
                            await msg.delete(delay=0)
                            embed3 = discord.Embed(
                                title="__Open__",
                                description=
                                f"**Channel is now open you can explain your issue to {user.mention}.**",
                                timestamp=datetime.utcnow(),
                                colour=0x9ee884)
                            embed3.add_field(
                                name=f"__{role}__",
                                value=
                                f"**{user.mention} You can close the ticket after talking with {ctx.message.author.mention} by reacting with {emoji}.**"
                            )
                            msg1 = await rch.send(embed=embed3)
                            await msg1.add_reaction(emoji)

                            def check2(reaction, user):
                                for m in role1.members:
                                    return user == m and str(
                                        reaction.emoji) == emoji

                            reaction, user = await self.bot.wait_for(
                                'reaction_add', check=check2)
                            embed4 = discord.Embed(
                                title="__Closing__",
                                description=
                                f"**Ticket has been closed, now please choose to delete the channel ⛔ or save this conversation 📰 **",
                                timestamp=datetime.utcnow(),
                                colour=0x9ee884)
                            msg2 = await rch.send(embed=embed4)
                            await msg2.add_reaction("â›”")
                            await msg2.add_reaction("📰")
                            await msg1.delete()

                            def check3(reaction, user):
                                for m in role1.members:
                                    return user == m and str(
                                        reaction.emoji) == "â›”" or str(
                                            reaction.emoji) == '📰'

                            reaction, user = await self.bot.wait_for(
                                'reaction_add', check=check3)
                            if str(reaction.emoji) == "â›”":
                                embed5 = discord.Embed(
                                    title="__Closing__",
                                    description=
                                    "**Channel will be deleted in 5 seconds 🛑.**",
                                    timestamp=datetime.utcnow(),
                                    colour=0xe88a84)
                                embed5.set_footer(
                                    icon_url=self.bot.user.avatar_url,
                                    text="?help for more help.")
                                await rch.send(embed=embed5)
                                await msg2.delete(delay=0)
                                await asyncio.sleep(delay=5)
                                await ctx.message.author.remove_roles(role2)
                                await rch.delete()
                            if str(reaction.emoji) == '📰':
                                messages = await rch.history(limit=None
                                                             ).flatten()
                                await asyncio.sleep(delay=5)
                                f = open(
                                    f'conversations/conversation-{count}-{ctx.guild.id}.txt',
                                    'w',
                                    encoding="utf-8")
                                writ = f.write(f"---Conversation {count}---\n")
                                messages.reverse()
                                for m in messages:
                                    if m.author != self.bot.user:
                                        db1 = TinyDB(
                                            'db/tickets/conversation.json')
                                        db1.insert({
                                            'request-owner':
                                            f'{ctx.message.author}',
                                            'guild_id':
                                            ctx.guild.id,
                                            'conversation':
                                            m.content,
                                            "conversation-headname":
                                            m.author.name,
                                            "conversation-headdisc":
                                            m.author.discriminator,
                                            'count':
                                            count
                                        })
                                        write = f.write(
                                            f"{m.author.name}#{m.author.discriminator} : {m.content}\n"
                                        )
                                msg3 = await rch.send("Saving...")
                                await msg3.delete()
                                embed5 = discord.Embed(
                                    title="__Saving__",
                                    description=
                                    "**This conversation has been saved and in order to see the conversation write ?conversation {conversation id number(you see it on the ticket channel)}, Example: ?conversation 10, this command will send you a dm with the full conservation. Now the channel is gonna be deleted after 15 seconds.**",
                                    timestamp=datetime.utcnow(),
                                    colour=0xe88a84)
                                embed5.add_field(
                                    name="__Notice__",
                                    value=
                                    "Only an admin can use this command and please wait at least 3 minutes before you use ?conversation command."
                                )
                                embed5.set_footer(
                                    icon_url=self.bot.user.avatar_url,
                                    text="?help for more help.")
                                await rch.send(embed=embed5)
                                await asyncio.sleep(30)
                                await ctx.message.author.remove_roles(role2)
                                await rch.delete()
                    except AttributeError as error:
                        print(error)
Example #42
0
class Plugin(object):
    def __init__(self, pm):
        self.pm = pm
        self.hugs = [
            '(づ ̄ ³ ̄)づ', '(つ≧▽≦)つ', '(つ✧ω✧)つ', '(づ ◕‿◕ )づ', '(⊃。•́‿•̀。)⊃',
            '(つ . •́ _ʖ •̀ .)つ', '(っಠ‿ಠ)っ', '(づ◡﹏◡)づ'
        ]
        self.configPath = 'pluginsconfig/data_config-utils_a.json'
        self.configDB = TinyDB(self.configPath)
        self.macroPath = 'pluginsconfig/data_macro_a.json'
        self.macroDB = TinyDB(self.macroPath)
        self.chaninfoPath = 'pluginsconfig/data_channel-info_a.json'
        self.chaninfDB = TinyDB(self.chaninfoPath)
        #self.converter = CurrencyConverter()
        print("[Noku-utils]Initalizing Imgur Stuff...")
        self.client = ImgurClient("43bdb8ab21d18b9",
                                  "fcba34a83a4650474ac57f6e3f8b0750dd26ecf5")
        print("[Noku-utils]Retrieving Images...")
        self.wmimages = self.client.subreddit_gallery("wholesomememes")
        self.catimages = self.client.subreddit_gallery("catsstandingup")
        self.dogimages = self.client.subreddit_gallery("rarepuppers")
        self.kanye = [
            "I miss the old Kanye, straight from the Go Kanye\nChop up the soul Kanye, set on his goals Kanye",
            "I hate the new Kanye, the bad mood Kanye\nThe always rude Kanye, spaz in the news Kanye",
            "I miss the sweet Kanye, chop up the beats Kanye",
            "I gotta say, at that time I'd like to meet Kanye",
            "See, I invented Kanye, it wasn't any Kanyes\nAnd now I look and look around and there's so many Kanyes",
            "I used to love Kanye, I used to love Kanye\nI even had the pink polo, I thought I was Kanye",
            "What if Kanye made a song about Kanye\nCalled 'I Miss The Old Kanye'? Man, that'd be so Kanye",
            "That's all it was Kanye, we still love Kanye\nAnd I love you like Kanye loves Kanye"
        ]
        self.kanyeOrder = 0
        self.pats = [
            '{1}(ノ_<。)ヾ(´▽`){0}', '{1}。・゚・(ノД`)ヽ( ̄ω ̄ ){0}',
            '{1}ρ(-ω-、)ヾ( ̄ω ̄; ){0}', '{0}ヽ( ̄ω ̄(。。 )ゝ{1}',
            '{0}(*´I`)ノ゚(ノД`゚)゚。{1}', '{0}ヽ(~_~(・_・ )ゝ{1}',
            '{1}(ノ_;)ヾ(´∀`){0}', '{1}(;ω; )ヾ(´∀`* ){0}', '{0}(*´ー)ノ(ノд`){1}',
            '{0}(´-ω-`( _ _ ){1}', '{0}(っ´ω`)ノ(╥ω╥){1}', '{0}(o・_・)ノ”(ノ_<、){1}'
        ]
        self.userqueue = []

    @staticmethod
    def register_events():
        return [
            Events.Command("ping", Ranks.Default,
                           "Pings the bot, nothing special"),
            Events.Command("hug", Ranks.Default,
                           "[@username] Sends a hug to a user."),
            Events.Command("system.purgeAllDM", Ranks.Admin,
                           "(!Admin use only)~~Cause people are paranoid"),
            Events.Command("pat", Ranks.Default,
                           "[@username] Sends a pat to a user."),
            Events.Command("info.set", Ranks.Admin),
            Events.Command("info.delete", Ranks.Admin),
            Events.Command("info", Ranks.Default, "Shows channel info"),
            Events.Command("meme", Ranks.Default, "posts a wholesome meme"),
            Events.Command("exch", Ranks.Default,
                           "[ammount] [from] [to] converts currency"),
            Events.Command("ud", Ranks.Default, "[query] Urban Dictionary"),
            Events.Command("lang", Ranks.Default,
                           "[query] Tries to determine the language"),
            Events.Command("cats", Ranks.Default, "Posts a cat"),
            Events.Command("emotext", Ranks.Default, "Emojifies a text"),
            Events.Command(
                "poll", Ranks.Default,
                "[question]/[item1]/[item2]/[item3]/[item..] posts a poll and its corresponding reactions."
            ),
            Events.Command("dogs", Ranks.Default, "Posts a dog"),
            Events.Command("old", Ranks.Default, "Kanye Kanye Kanye"),
            Events.Command("qjoin", Ranks.Default, "Join Queue"),
            Events.Command("qdone", Ranks.Default, "Finish Queue"),
            Events.Command("qview", Ranks.Default, "View Queue"),
            Events.Command("qkick", Ranks.Admin,
                           "[Admin] Kick user from Queue"),
            Events.Command("qreset", Ranks.Default, "[Admin] Reset Queue"),
            Events.Command("qhelp", Ranks.Default, "View Queue"),
            Events.Command("pins", Ranks.Default,
                           "[#channel] shows pins from a specified channel."),
            Events.Command("print_avatars_to_console", Ranks.Admin,
                           "[secret stuff]"),
            Events.Command("utils.allow", Ranks.Admin),
            Events.Command("restart", Ranks.Admin),
            Events.Command("utils.block", Ranks.Admin)
        ]

    async def handle_command(self, message_object, command, args):

        try:
            print("--{2}--\n[Noku-utils] {0} command from {1} by {3}".format(
                command, message_object.channel.name,
                arrow.now().format('MM-DD HH:mm:ss'),
                message_object.author.name))
        except:
            print("[Noku]Cannot display data, probably emojis.")
        #print(args)
        config = Query()

        if self.configDB.contains(config.chanallow == message_object.channel.id
                                  ) or message_object.channel.is_private:
            if command == "ping":
                await self.ping(message_object, "Pong")
            if command == "system.purgeAllDM":
                await self.purge(message_object)
            elif command == "pins":
                await self.showpins(message_object)
            elif command == "poll":
                await self.makepoll(message_object, args[1])
            elif command == "hug":
                await self.hug(message_object)
            elif command == "pat":
                await self.pat(message_object)
            elif command == "emotext":
                await self.emotext(message_object, args[1])
            elif command == "exch":
                await self.currency(message_object, args[1])
            elif command == "meme":
                await self.postmeme(message_object, self.wmimages)
            elif command == "ud":
                await self.urban(message_object, args[1])
            elif command == "lang":
                await self.lang(message_object, args[1])
            elif command == "cats":
                await self.postmeme(message_object, self.catimages)
            elif command == "dogs":
                await self.postmeme(message_object, self.dogimages)
            elif command == "old":
                await self.old(message_object, args[1])
            elif command == "info.set":
                await self.chaninfo(message_object, args[1], "set")
            elif command == "info.delete":
                await self.chaninfo(message_object, args[1], "delete")
            elif command == "info":
                await self.chaninfo(message_object, args[1], "info")
            elif command == "restart":
                await self.shutdown(message_object)
            elif command == "print_avatars_to_console":
                await self.getuser(message_object)

        if command == "qjoin":
            await self.qjoin(message_object)
        if command == "qdone":
            await self.qdone(message_object)
        if command == "qview":
            await self.qview(message_object)
        if command == "qkick":
            await self.qkick(message_object)
        if command == "qreset":
            await self.qreset(message_object)
        if command == "qhelp":
            await self.qhelp(message_object)

        if command == "utils.allow":
            await self.allowChan(message_object)
        if command == "utils.block":
            await self.blockChan(message_object)

    async def qhelp(self, message_object):
        em = discord.Embed(
            title="Queue Help",
            description=
            "**~qjoin** : _Join Queue_\n**~qdone** : _Finish your turn_\n**~qview** : _Display the users in Queue_\n**~qkick** <@user> : _Kicks user from Queue (Admin only)_\n**~qreset** : _Resets Queue (Admin only)_",
            colour=0x007AFF)
        await self.pm.client.send_message(message_object.channel, embed=em)

    async def qkick(self, message_object):
        try:
            self.userqueue.remove(message_object.mentions[0])
            em = discord.Embed(
                title="Queue",
                description="{0} has been kicked from the Queue!".format(
                    message_object.mentions[0].name),
                colour=0x007AFF)
        except:
            em = discord.Embed(
                title="Queue",
                description=
                "No user specified or the user is not in the queue!",
                colour=0x007AFF)

        await self.pm.client.send_message(message_object.channel, embed=em)

    async def qreset(self, message_object):
        self.userqueue = []
        em = discord.Embed(title="Queue",
                           description="The queue has been emptied!",
                           colour=0x007AFF)
        await self.pm.client.send_message(message_object.channel, embed=em)

    async def qview(self, message_object):
        if len(self.userqueue) > 0:
            display = "There's currently {0} users in the queue.\n---\n".format(
                len(self.userqueue))
            count = 1
            for user in self.userqueue:
                display += "{0}. **{1}**\n".format(count, user.name)
                count += 1

            em = discord.Embed(title="Queue",
                               description=display,
                               colour=0x007AFF)
        else:
            em = discord.Embed(title="Queue",
                               description="The queue is empty!",
                               colour=0x007AFF)

        await self.pm.client.send_message(message_object.channel, embed=em)

    async def qdone(self, message_object):
        try:
            self.userqueue.remove(message_object.author)
            em = discord.Embed(title="Queue",
                               description="You Successfuly left the Queue!",
                               colour=0x007AFF)
        except:
            em = discord.Embed(title="Queue",
                               description="You're not in the Queue!",
                               colour=0x007AFF)

        await self.pm.client.send_message(message_object.channel, embed=em)
        await self.qview(message_object)

    async def qjoin(self, message_object):
        if not message_object.author in self.userqueue:
            self.userqueue.append(message_object.author)
            em = discord.Embed(
                title="Queue",
                description="{0} has been added to the queue!".format(
                    message_object.author.name),
                colour=0x007AFF)
        else:
            em = discord.Embed(
                title="Queue",
                description="{0} is already in the queue!".format(
                    message_object.author.name),
                colour=0x007AFF)

        await self.pm.client.send_message(message_object.channel, embed=em)
        await self.qview(message_object)

    async def currency(self, message_object, args):
        try:
            ammount = int(args.split(" ")[0])
            fr = args.split(" ")[1]
            to = args.split(" ")[2]
            re = requests.get(
                "https://currency-api.appspot.com/api/{0}/{1}.json".format(
                    fr.lower(), to.lower()))
            if re.json()["success"] or re.json()["success"] == "true":
                converted = ammount * float(re.json()["rate"])
                description = ":currency_exchange: **{0:,.2f} {1}** Equals **{2:,.2f} {3}**".format(
                    ammount, fr.upper(), converted, to.upper())
                #em = discord.Embed(title=title, description=wjson["Weather"]["status"],)
                em = discord.Embed(title="Currency Exchange",
                                   description=description,
                                   colour=0x007AFF)
                em.set_footer(text="Current Rate: 1 {0} = {1} {2}".format(
                    fr.upper(),
                    re.json()["rate"], to.upper()))
            else:
                description = ":exclamation: _Invalid currency specified!_"
                #em = discord.Embed(title=title, description=wjson["Weather"]["status"], colour=0x007AFF)
                em = discord.Embed(title="Currency Exchange",
                                   description=description,
                                   colour=0x007AFF)

            await self.pm.client.send_message(message_object.channel, embed=em)
        except:
            description = ":information_source: Usage: [ammount] [from] [to]\nEx. `~exch 100 jpy usd`"
            em = discord.Embed(title="Currency Exchange",
                               description=description,
                               colour=0x007AFF)
            await self.pm.client.send_message(message_object.channel, embed=em)

    async def emotext(self, message_object, args):
        string = ""
        number = [
            "zero", "one", "two", "three", "four", "five", "six", "seven",
            "eight", "nine"
        ]
        for x in args.lower():
            try:
                if x in "qwertyuiopasdfghjklzxcvbnm":
                    string += ":regional_indicator_{0}: ".format(x)
                if x == " ":
                    string += "\n "
                if x in "1234567890":
                    string += ":{0}: ".format(number[int(x)])
            except:
                pass
        await self.pm.client.send_message(message_object.channel, string)

        pass

    async def lang(self, message_object, args):
        iso = langdetect.detect(args)
        x = "```{0}```Language result: {1}[{2}]".format(
            args, iso639.to_name(iso), iso639.to_native(iso))
        await self.pm.client.send_message(message_object.channel, x)

    async def urban(self, message_object, args):
        catalog = urbandict.define(args)
        em = discord.Embed(title='Urban Dictionary',
                           description="Query: " + args,
                           colour=0x007AFF)
        em.set_author(name='{0}\'s Result'.format(message_object.author.name))
        em.set_footer(text="Noku-utils version 0.3",
                      icon_url=self.pm.client.user.avatar_url)
        em.add_field(name="Definiton", value=catalog[0]['def'])
        em.add_field(name="Example", value=catalog[0]['example'])
        await self.pm.client.send_message(message_object.channel, embed=em)

    async def purge(self, message_object):
        print("Purge: A")
        for channels in self.pm.client.private_channels:
            if channels.is_private:
                print("Purge: B")
                #try:
                print("Purge: B.5")
                async for message in self.pm.client.logs_from(channels):
                    print("Purge: C")
                    if message.author == self.pm.client.user:
                        try:
                            print("Purge: D")
                            print("Delete:{0}".format(message.content))
                            await self.pm.client.delete_message(message)
                        except:
                            print("Purge: D.5")
                            pass
                #except:
                #    pass
        pass

    async def makepoll(self, message_object, args):
        reactions = ["🇦", "🇧", "🇨", "🇩", "🇪", "🇫", "🇬", "🇭", "🇮", "🇯"]
        letter = "ABCDEFGHIJ"
        items = args.split("/")
        content = ":pencil:|**{0}**\n".format(items[0])
        count = 0
        for x in items[1:]:
            content += ":black_small_square: **{0}.** `{1}`\n".format(
                letter[count], x)
            count += 1
        message = await self.pm.client.send_message(message_object.channel,
                                                    content)
        for x in range(0, count):
            await self.pm.client.add_reaction(message, reactions[x])
        await self.pm.client.delete_message(message_object)
        pass

    async def ping(self, message_object, reply):
        speed = datetime.datetime.now() - message_object.timestamp
        await self.pm.client.send_message(
            message_object.channel,
            reply + " " + str(round(speed.microseconds / 1000)) + "ms")

    async def old(self, message_object, args):
        if ".order" in args:
            args = args.replace(".order", args)
            await self.pm.client.send_message(
                message_object.channel,
                self.kanye[self.kanyeOrder].replace("Kanye", args))
            self.kanyeOrder = self.kanyeOrder + 1
            if self.kanyeOrder > len(self.kanye) - 1:
                self.kanyeOrder = 0
        else:
            await self.pm.client.send_message(
                message_object.channel,
                random.choice(self.kanye).replace("Kanye", args))

    async def getuser(self, message_object):
        for x in self.pm.client.get_all_members():
            print(x.avatar_url)

    async def hug(self, message_object):
        if len(message_object.mentions) != 0:
            await self.pm.client.send_message(
                message_object.channel,
                "{0} {1} {2}".format(message_object.author.mention,
                                     random.choice(self.hugs),
                                     message_object.mentions[0].mention))
        else:
            await self.pm.client.send_message(
                message_object.channel,
                ":exclamation:`Welp, that\'s not a valid user!`")

    async def pat(self, message_object):
        if len(message_object.mentions) != 0:
            await self.pm.client.send_message(
                message_object.channel,
                random.choice(self.pats).format(
                    message_object.author.mention,
                    message_object.mentions[0].mention))
        else:
            await self.pm.client.send_message(
                message_object.channel,
                ":exclamation:`Welp, that\'s not a valid user!`")

    async def macroadd(self, message_object, args):
        trigger = args.split(" ")[0]
        self.macroDB.insert({'trigger': trigger, 'data': args[len(trigger):]})
        await self.pm.client.send_message(
            message_object.channel,
            ":information_source:`{0} has been added as a macro!`".format(
                trigger))

    async def macrodel(self, message_object, args):
        self.macroDB.remove(Query().trigger == args)
        await self.pm.client.send_message(
            message_object.channel,
            ":information_source:`{0} has been deleted! Probably..`".format(
                args))

    async def helpUtil(self, message_object):
        await self.pm.client.send_message(
            message_object.channel,
            ":information_source: Help detail for Utilities")
        await self.pm.client.send_message(
            message_object.channel,
            "```~hug @user\n~ping\n~macro.add [trigger] [data]\n~macro.delete [trigger]\n~macro [trigger](alt. ~m)\n~macro.assigned```"
        )

    async def macroShow(self, message_object, args):
        try:
            await self.pm.client.send_message(
                message_object.channel,
                self.macroDB.search(Query().trigger == args)[0]["data"])
        except:
            await self.pm.client.send_message(
                message_object.channel,
                ":exclamation:`Welp, that\'s not a valid macro!`")

    async def macroShowAssigned(self, message_object):
        macros = self.macroDB.search(Query().trigger != "")
        x = "```"
        for m in macros:
            x = x + m['trigger'] + " "
        x = x + "```"
        await self.pm.client.send_message(message_object.channel, x)

    async def chaninfo(self, message_object, args, trigger):
        #trigger = args.split(" ")[0]
        if trigger == "set":
            self.chaninfDB.remove(Query().channel == message_object.channel.id)
            self.chaninfDB.insert({
                'channel': message_object.channel.id,
                'data': args
            })
            await self.pm.client.send_message(
                message_object.channel,
                ":information_source:`{0} info has been updated!`".format(
                    trigger))
        elif trigger == "delete":
            self.chaninfDB.remove(Query().channel == message_object.channel.id)
            await self.pm.client.send_message(
                message_object.channel,
                ":information_source:`{0} info has been removed!`".format(
                    trigger))
        else:
            try:
                await self.pm.client.send_message(
                    message_object.channel,
                    self.chaninfDB.search(Query().channel == message_object.
                                          channel.id)[0]["data"])
            except:
                await self.pm.client.send_message(
                    message_object.channel,
                    ":exclamation:No info! `~info set [message]` to set a channel info"
                )

    async def postmeme(self, message_object, imagelist):
        await self.pm.client.send_message(
            message_object.channel,
            "{0}".format(random.choice(imagelist).link))

    async def showpins(self, message_object):
        try:
            if len(message_object.channel_mentions) > 0:
                for x in await self.pm.client.pins_from(
                        message_object.channel_mentions[0]):
                    em = discord.Embed(title="\n",
                                       description=x.content,
                                       colour=0x007AFF)
                    em.set_author(name='{0} - {1}'.format(
                        x.author.name,
                        arrow.get(x.timestamp).format('MM-DD HH:mm')))
                    em.set_thumbnail(url=x.author.avatar_url)
                    await self.pm.client.send_message(message_object.channel,
                                                      embed=em)
                pass
            else:
                await self.pm.client.send_message(
                    message_object.channel,
                    ":exclamation:No channel specified! Usage: `~pins [#channel]`"
                )
        except:
            await self.pm.client.send_message(
                message_object.channel,
                ":exclamation:`Error retrieving pins!`")
            pass

    async def allowChan(self, message_object):
        self.configDB.insert({'chanallow': message_object.channel.id})
        await self.pm.client.send_message(
            message_object.channel,
            ':information_source:`Noku Bot-utils has been allowed access to {0}`'
            .format(message_object.channel.name))

    async def blockChan(self, message_object):
        self.configDB.remove(Query().chanallow == message_object.channel.id)
        await self.pm.client.send_message(
            message_object.channel,
            ':information_source:`Noku Bot-utils has been blocked access to {0}`'
            .format(message_object.channel.name))

    async def shutdown(self, message_object):
        game = discord.Game()
        game.name = "Restarting...".format()
        await self.pm.client.change_presence(game=game, afk=False)
        await self.pm.client.send_message(
            message_object.channel,
            ':information_source:`さようなら {0}! Noku bot is rebooting! <3`'.
            format(message_object.author.name))
        exit()
Example #43
0
class ATX_Server(object):
    """
    According to users requirements to select devices
    """
    def __init__(self, url):
        """
        Construct method
        """
        self._db = TinyDB(storage=MemoryStorage)
        if url and re.match(r"(http://)?(\d+\.\d+\.\d+\.\d+:\d+)", url):
            if '://' not in url:
                url = 'http://' + url + '/list'
            else:
                url = url + '/list'
            self._url = url
            self.load()
        else:
            logger.error('Atx server addr error')
        self.load()

    def load(self):
        """
        Use the data which got from stf platform to crate query db

        :return: the len of records in the db's table
        """
        res = requests.get(self._url).json()
        if res is not None:
            eids = self._db.insert_multiple(res)
            return len(eids)
        else:
            return 0

    def find(self, cond=None):
        """
        According condition to filter devices and return
        :param cond: condition to filter devices
        :type cond: where
        :return: stf_selector object and its db contains devices
        """
        if cond is not None:
            res = self._db.search(cond)
            self.purge()
            self._db.insert_multiple(res)
        return self

    def devices(self):
        """
        return all devices that meeting the requirement
        :return: list of devices
        """
        return self._db.all()

    def refresh(self):
        """
        reload the devices info from stf
        :return: the len of records in the db's table
        """
        self.purge()
        return self.load()

    def count(self):
        """
        count the records in the db's table
        :return: the len of records in the db's table
        """
        return len(self._db.all())

    def purge(self):
        """
        remove all the data from the db
        :return:
        """
        self._db.purge()

    def ready_devices(self):
        '''查找标记为ready的设备'''
        self.refresh()
        devices = self.find(where('ready') == True).devices()
        if len(devices) > 0:
            return devices
        else:
            return False

    def online_devices(self):
        '''查找online 的设备'''
        self.refresh()
        devices = self.find(where('present') == True).devices()
        if len(devices) > 0:
            return devices
        else:
            return False

    def model_devices(self, model):
        '''查找特定型号的设备'''
        self.refresh()
        devices = self.find(where('model') == model).devices()
        if len(devices) > 0:
            return devices
        else:
            return False

    def brand_devices(self, brand):
        '''查找特定品牌的设备'''
        self.refresh()

        devices = self.find(where('brand') == brand).devices()
        if len(devices) > 0:
            return devices
        else:
            return False

    def sdk_devices(self, sdk):
        '''查找特定SDK的设备'''
        self.refresh()
        devices = self.find(where('sdk') == sdk).devices()
        if len(devices) > 0:
            return devices
        else:
            return False

    def version_devices(self, version):
        '''查找特定SDK的设备'''
        self.refresh()
        devices = self.find(where('version') == version).devices()
        if len(devices) > 0:
            return devices
        else:
            return False

    def serial_devices(self, serial):
        '''查找特定serial的设备'''
        self.refresh()
        devices = self.find(where('serial') == serial).devices()
        if len(devices) > 0:
            return devices
        else:
            return False

    def all_devices(self):
        '''返回所有的设备'''
        self.refresh()
        devices = self.find().devices()
        if len(devices) > 0:
            return devices
        else:
            return False
Example #44
0
from tinydb import TinyDB, Query

db = TinyDB('db.json')
db.insert({'name': "John", 'City': 'Tulsa'})
db.insert({'name': "Luke", 'City': 'Tulsa'})

print(db.search(Query()['name'] == 'Luke'))
Example #45
0
class DBHandler:
    """Class that make the link between the DB and repator."""
    @staticmethod
    def auditors():
        """Default constructor for Auditors database."""
        return DBHandler(DB_LOCAL_FILES["auditors"], DB_AUDITORS_DEFAULT)

    @staticmethod
    def clients():
        """Default constructor for Clients database."""
        return DBHandler(DB_LOCAL_FILES["clients"], DB_CLIENTS_DEFAULT)

    @staticmethod
    def vulns():
        """Default constructor for Vulns database."""
        return DBHandler(DB_LOCAL_FILES["vulns"], DB_VULNS_DEFAULT)

    @staticmethod
    def vulns_git():
        """Default constructor for Vulns taken from git database."""
        return DBHandler(DB_GIT_LOCAL_FILES["vulns"], DB_VULNS_DEFAULT)

    @staticmethod
    def auditors_git():
        """Default constructor for Vulns taken from git database."""
        return DBHandler(DB_GIT_LOCAL_FILES["auditors"], DB_AUDITORS_DEFAULT)

    @staticmethod
    def clients_git():
        """Default constructor for Vulns taken from git database."""
        return DBHandler(DB_GIT_LOCAL_FILES["clients"], DB_CLIENTS_DEFAULT)

    def __init__(self, db_path, default_values=None):
        if not path.exists(path.dirname(db_path)):
            mkdir(path.dirname(db_path), 0o750)

        new_db = not path.isfile(db_path)

        self.path = db_path
        self.default_values = default_values if default_values else {}
        self.database = TinyDB(db_path,
                               indent=2,
                               object_pairs_hook=collections.OrderedDict)

        if new_db:
            self.insert_record(default_values)
        else:
            for name, value in default_values.items():
                self.insert_column(name, value)

    def insert_column(self, name, value):
        """Creates a new column in the database."""
        values = self.get_all()
        cols = {name: value}
        ids = []
        for record in values:
            if name in record:
                return False  # column already exists
            ids.append(record.doc_id)
        self.database.update(cols, doc_ids=ids)
        return True

    def insert_record(self, dictionary=None):
        """Adds a new record to the database."""
        if dictionary is None:
            dictionary = collections.OrderedDict(self.search_by_id(1))
            first_lang = True

            if self.path == DB_LOCAL_FILES["vulns"]:
                # Adds the keys which are different according to the languages
                for lang in LANGUAGES:
                    if first_lang:
                        first_lang = False
                    else:
                        for elem in DB_VULNS_DIFFERENT_LANG:
                            dictionary[elem + lang] = dictionary[elem]
        return self.database.insert(dictionary)

    def insert_multiple(self, dictionary):
        """Insertion of multiple entries."""
        return self.database.insert_multiple(dictionary)

    def get_all(self):
        """Gets all records but the first one which is a sample record."""
        return self.database.all()[1:]

    def search(self, name, value):
        """Implements the search method of TinyDB."""
        query = Query()
        return self.database.search(query[name] == value)

    def search_by_id(self, id_):
        """Searches for a document with the id id_ in the database."""
        return self.database.get(doc_id=id_)

    def update(self, id_, name, value):
        """Modifies the corresponding record in the database."""
        record = self.search_by_id(id_)
        if record is None:
            return False
        record[name] = value
        return self.database.update(record, doc_ids=[id_])

    def delete(self, id_):
        """Removes the corresponding record from the database."""
        return self.database.remove(doc_ids=[id_])

    def purge(self):
        """Purges the database and adds the default values to the newly created database."""
        self.database.purge()
        self.insert_record(self.default_values)

    def close(self):
        """Implements the close method of TinyDB."""
        self.database.close()
Example #46
0
class FunCog(commands.Cog, Server):
    def __init__(self, client):
        self.client = client
        self.events = TinyDB('database/events.json')
        self.users = TinyDB('database/users.json')

        Server.__init__(self)

        self.noon = datetime.datetime.now().replace(hour=12,
                                                    minute=0,
                                                    second=0,
                                                    microsecond=0)

        lastCoolGuy = self.events.get(where('name') == 'coolguy')['last']
        self.lastCoolGuy = datetime.datetime.strptime(
            lastCoolGuy.replace("-", ""), "%Y%m%d").date()

        self.activeUsers = self.events.get(
            where('name') == 'coolguy')['activeUsers']

        self.CheckBirthday.start()

    @tasks.loop(hours=24)
    async def CheckBirthday(self):

        userBirthdays = self.users.search(where('birthday').exists())

        for user in userBirthdays:
            if (user['birthday'] == str(datetime.datetime.now().date())):
                general = self.client.get_guild(self.server).get_channel(
                    self.generalChannel)
                await general.send("**Happy Birthday <@" + str(user['id']) +
                                   ">!** :birthday: :tada:")

    @staticmethod
    def is_cool_candidate(member):
        return member and (not member.bot) and (
            not member.guild_permissions.manage_messages)

    @staticmethod
    def pick_from_array(choices, member_resolver=None, skip_these=None):
        mutable = choices
        while (len(mutable)):
            pick = choice(mutable)
            resolved = ( pick and member_resolver.get_member(pick) ) \
                if member_resolver else pick

            if (not resolved) or \
                (skip_these and (resolved in skip_these) or \
                (not FunCog.is_cool_candidate(resolved)) ):

                if (not resolved):
                    print('failed to resolve a member with id:' + str(pick))

                # rather than a huge copy of the member list we'll make a clone
                # of it only when we need to (which hopefully is rare)
                if (mutable is choices):
                    mutable = choices[0:]

                # remove the choice so that it is not picked again.
                mutable.remove(pick)
                pick = None
            else:
                return resolved

        return None

    @commands.Cog.listener()
    async def on_message(self, message):

        #Temp remove messages with content in drawing arena
        if (message.channel.id == 750753280694550539
                and message.content != ""):
            await message.delete()

        member = message.author

        #Add non-staff to list of active users
        if (not member.bot
                and (str(member.id) not in self.activeUsers
                     and not member.guild_permissions.manage_messages)):
            self.activeUsers.append(str(member.id))
            self.events.update(set('activeUsers', self.activeUsers),
                               where('name') == 'coolguy')

        #Cool guy raffle once a day
        now = datetime.datetime.now()
        if (now > self.noon and (date.today() > self.lastCoolGuy)) or \
            (message.content == '!newguy'):

            #Set date
            self.lastCoolGuy = date.today()
            self.events.update(set('last', str(self.lastCoolGuy)),
                               where('name') == 'coolguy')

            coolGuyRole = message.guild.get_role(self.coolGuyRole)

            #Remove last cool guy(s)
            coolGuys = [] if coolGuyRole.members is None else coolGuyRole.members
            for coolGuy in coolGuys:
                await coolGuy.remove_roles(coolGuyRole)

            #New cool guys
            winners = list()

            print( 'active users are ('+str(len(self.activeUsers))+'):\n' + \
                ('\n'.join( self.activeUsers )) )

            found = self.pick_from_array(self.activeUsers,
                                         member_resolver=message.guild)

            if found:
                winners.append(found)

            while (2 != len(winners)):
                found = self.pick_from_array(message.guild.members,
                                             skip_these=winners)
                if found:
                    winners.append(found)
                else:
                    break

            for winner in winners:
                await winner.add_roles(coolGuyRole)

            general = message.guild.get_channel(self.generalChannel)
            await general.send(
                "No one won the cool guy raffle." if 0 == len(winners) else
                (winners[0].mention + " won the cool guy raffle!") if 1 == len(winners) else \
                (', '.join(winner.mention for winner in winners[:-1])) + \
                " and " +winners[-1].mention + \
                "won the cool guy raffle!")

            #Reset active users
            self.activeUsers = []
            self.events.update(set('activeUsers', self.activeUsers),
                               where('name') == 'coolguy')
Example #47
0
class JsonDb:
    def __init__(self):
        self.db = TinyDB('Database/db.json')
        self.querier = Query()
        # db.insert({'email': '*****@*****.**', 'telegram_id': '1234',
        # 	'mail_unique_id_count':{'mail123':1,'mail124':1},
        # 	'mail_last_read':{'mail123':'today','mail124':'yesterday'},
        #	'mail_comment': {'mail123': "testing", 'mail124': "testing2"}})
    def checkMail(self, email_id):
        result = self.db.search(self.querier.email == str(email_id))
        if len(result) == 1:
            return True
        else:
            return False

    def getReadCount(self, sender_email, unique_mail_id):
        result = self.db.search(self.querier.email == str(sender_email))
        count = result[0]['mail_unique_id_count'][unique_mail_id]
        return count

    def getConfigCount(self, sender_email, unique_mail_id):
        result = self.db.search(self.querier.email == str(sender_email))
        count = result[0]['config_count'][unique_mail_id]
        return count

    def updateConfigCount(self, sender_email, unique_mail_id):
        result = self.db.search(self.querier.email == str(sender_email))
        config_count = result[0]['config_count']
        config_count[unique_mail_id] = config_count[unique_mail_id] + 1
        self.db.update({'config_count': config_count},
                       self.querier.email == str(sender_email))

    def checkUniqueId(self, sender_email, unique_mail_id):
        result = self.db.search(self.querier.email == str(sender_email))
        if str(unique_mail_id) in result[0]['mail_unique_id_count'].keys():
            return False
        else:
            return True

    def insertMail(self, sender_email, unique_mail_id, comments):
        try:
            result = self.db.search(self.querier.email == str(sender_email))

            mail_id_dict = result[0]['mail_unique_id_count']
            mail_id_dict[unique_mail_id] = 0
            self.db.update({'mail_unique_id_count': mail_id_dict},
                           self.querier.email == str(sender_email))

            mail_last_dict = result[0]['mail_last_read']
            mail_last_dict[unique_mail_id] = {}
            self.db.update({'mail_last_read': mail_last_dict},
                           self.querier.email == str(sender_email))

            mail_comments = result[0]['mail_comment']
            mail_comments[unique_mail_id] = comments
            self.db.update({'mail_comment': mail_comments},
                           self.querier.email == str(sender_email))

            config_count = result[0]['config_count']
            config_count[unique_mail_id] = 0
            self.db.update({'config_count': config_count},
                           self.querier.email == str(sender_email))

            return True

        except Exception as e:
            print(e)
            return False

    def updateMailReadCount(self, sender_email, unique_mail_id):
        try:
            result = self.db.search(self.querier.email == str(sender_email))

            mail_id_dict = result[0]['mail_unique_id_count']
            mail_id_dict[unique_mail_id] = mail_id_dict[unique_mail_id] + 1
            self.db.update({'mail_unique_id_count': mail_id_dict},
                           self.querier.email == str(sender_email))

            mail_last_dict = result[0]['mail_last_read']
            mail_last_dict[unique_mail_id][len(
                mail_last_dict[unique_mail_id])] = datetime.datetime.now(
                ).strftime("%Y-%m-%d %H:%M:%S")
            self.db.update({'mail_last_read': mail_last_dict},
                           self.querier.email == str(sender_email))

            return True

        except Exception as e:
            print(e)
            return True

    def getFirstRead(self, sender_email, unique_mail_id):
        try:
            result = self.db.search(self.querier.email == str(sender_email))
            mail_last_read = result[0]['mail_last_read'][unique_mail_id]
            return mail_last_read
        except Exception as e:
            print(e)

    def getUniqueId(self, sender_email):
        result = self.db.search(self.querier.email == str(sender_email))
        user_id = result[0]['encrypted']
        return str(user_id)

    def getUserFromId(self, sender_email):
        result = self.db.search(self.querier.encrypted == str(sender_email))
        email = result[0]['email']
        return str(email)
Example #48
0
#!/usr/bin/python3
import os
import aiml
from tinydb import TinyDB, Query

user = ""
db = TinyDB('actual.json')
username = input("What is your username?\n")
name = input("What is your Name?\n")
ask = Query()
if(len(db.search(ask.username==username and ask.Name==name))==0):
	print("No user found. Login failed.")
	quit()
else:
	print("User found, logged in")
	user = username

BRAIN_FILE="brain.dump"

k = aiml.Kernel()
person = db.search(ask.username==user)
person = person[0]
k.setPredicate("name", person['Name'])
k.setPredicate("address", person['Address'])
# To increase the startup speed of the bot it is
# possible to save the parsed aiml files as a
# dump. This code checks if a dump exists and
# otherwise loads the aiml from the xml files
# and saves the brain dump.
if os.path.exists(BRAIN_FILE):
    print("Loading from brain file: " + BRAIN_FILE)
Example #49
0
class Database:
    def __init__(self):
        if os.path.exists('db.json'):
            print('DB exists')
            self.db = TinyDB('db.json')
        else:
            with open('propertiesData.json') as jd:
                data = json.load(jd)
            self.db = TinyDB('db.json')
            for item in data:
                self.db.insert(item)

    def incrementLanded(self, landed):
        Space = Query()
        prop = self.db.search(Space.Name == landed)
        for doc in prop:
            doc['Landed'] += 1
        self.db.write_back(prop)

    def purgeDocuments(self):
        self.db.purge()
        os.remove('db.json')

    def createGraph(self):
        name = []
        timesLanded = []
        costs = []
        rents = []
        self.removeDataPoints() # Removes All non-properties
        prop = self.db.all()
        for doc in prop:
            name.append(doc['Name'])
            timesLanded.append(doc['Landed'])
            costs.append(doc['Cost'])
            rents.append(doc['Rent'])
        timesLanded = self.convertToPercent(timesLanded)
        _graph = Graph(name, timesLanded, costs, rents)

        #_graph.barChart()
        _graph.lineChart()

    def convertToPercent(self, values):
        timesLanded = []
        total = 0
        for value in values:
            total += value
        for num in values:
            percent = (num / total) * 100
            timesLanded.append(round(percent, 2))
        return timesLanded

    def removeDataPoints(self):
        Place = Query()
        self.db.remove(Place.Name == 'Chance')
        self.db.remove(Place.Name == 'GO')
        self.db.remove(Place.Name == 'Community Chest')
        self.db.remove(Place.Name == 'Jail')
        self.db.remove(Place.Name == 'Free Parking')
        self.db.remove(Place.Name == 'GO TO Jail')
        self.db.remove(Place.Name == 'Income Tax')
        self.db.remove(Place.Name == 'Luxury Tax')
        
Example #50
0
                snipbase = float(
                    re.search('[0-9,.]+', snipstr).group(0).replace(
                        ',', ''))  # convert string to float
                if re.search('[p|n|u|m|k][O|V|F|C|A|W]',
                             snipstr.split('@')[0]):
                    snippre = prefix[re.search(
                        'p|n|u|m|k', snipstr).group(0)]  # grab prefix
                else:
                    snippre = 1.0
                snipnum = snipbase * snippre
            else:
                snipnum = 0
            # grab manufacturer part number  index = 4
            if idxtd == 4:
                fetEntry['partNumber'] = snipstr
                if db.search(fq.partNumber == fetEntry['partNumber']):
                    break  # if duplicate break out of for loop

            # check manufacturer part number against database to see if its already added
            # if already added, dont do anything
            # if not already added, create a new dictionary

            # add manufacturer string index = 5
            if idxtd == 5:
                fetEntry['manufacturer'] = snipstr

            # add unit price number index = 8
            if idxtd == 8:
                fetEntry['unitPrice'] = snipnum  # convert to float in USD

            # add part status string index = 12
Example #51
0
from tinydb import TinyDB, Query

db = TinyDB('db.json')
query = Query()

result = db.search(query.token == '6983fb75-8c35-4588-9490-67446e34a855')

result.append({'clientip': '127.0.0.1'})

print(result)

#db.insert({'token': 'test2', 'description': 'test token 2'})

Example #52
0
from tinydb import TinyDB, Query
import tinydb.operations as dbop

if __name__ == "__main__":
    # Which database would you like to update?
    databaseName = './databases/blaskatronicViewers.db'
    # Put the new properties you'd like to add to the database here as the keys in
    # the dictionary, and then assign some default values to those keys.
    newPropertiesToAdd = {'drinkExpiry': None, 'drinks': 0}

    # The following code will add those properties and their default values to
    # any elements in the database that don't already have they key.
    db = TinyDB(databaseName)
    allElements = db.search(Query().name.exists())
    for propertyToAdd in newPropertiesToAdd.keys():
        for element in allElements:
            if element in db.search(Query().propertyToAdd.exists()):
                continue
            if propertyToAdd == 'totalPoints':
                value = db.search(Query().name == element['name'])[0]['points']
                db.update(dbop.set(propertyToAdd, value),
                          Query().name == element['name'])
            else:
                db.update(
                    dbop.set(propertyToAdd, newPropertiesToAdd[propertyToAdd]),
                    Query().name == element['name'])

    # ---=== DEBUG LINES ===---
    # Create dummy db for testing
    #for viewer in ['test1', 'test2', 'test3']:
    #    db.insert({'name': viewer, 'points': 0, 'rank': 'None', 'multiplier': 1})
Example #53
0
def get_record_from_db(id, dbfile):
    db = TinyDB(dbfile)

    res = db.search(where('unique_id') == id)

    return res
Example #54
0
class Backend(core.Backend):
    def __init__(self, datadir="data/", **kwargs):
        super().__init__(**kwargs)

        # Post init_settings things
        if not os.path.isdir(datadir):
            os.makedirs(datadir)

        # TODO put this in start()
        serialization = SerializationMiddleware()
        serialization.register_serializer(DateTimeSerializer(), "TinyDate")
        serialization.register_serializer(AddressSerializer(), "TinyAddress")

        self.db = TinyDB(os.path.join(datadir, "db.json"), storage=serialization)
        self.maildb = TinyDB(os.path.join(datadir, "maildb.json"))

    async def get(self, filter):
        """ Helper to get an item by filter """
        results = self.db.search(filter)
        if len(results) < 1:
            raise DoesntExists()
        if len(results) > 1:
            raise MultipleResults()
        return results[0]

    async def get_or_create(self, filter, default):
        """ Helper to get or create an item by filter with default value """
        try:
            result = await self.get(filter)
            return result
        except DoesntExists:
            self.db.insert(default)
            return default

    async def store_bad_msg(self, bad_msg):
        """ To handle msg that failed to parse """
        bad_msg["type"] = "mail"

        self.db.insert(bad_msg)

    async def store_content(self, uid, content):
        """ Store raw message content """
        b64_content = (base64.b64encode(content).decode("utf-8"),)
        return self.maildb.insert({"uid": uid, "content": b64_content})

    async def get_content_msg(self, uid):
        """ Return EmailMessage instance for this message uid """
        Mail = Query()

        results = self.maildb.search(Mail.uid == uid)
        if len(results) < 1:
            raise DoesntExists()
        if len(results) > 1:
            raise MultipleResults()
        b64_content = results[0]["content"][0]

        msg = BytesParser(policy=policy.default).parsebytes(
            base64.b64decode(b64_content)
        )

        return msg

    async def get_or_create_mailbox(self, account, address, name):
        """ Create a mailbox """

        Mailbox = Query()

        mailbox = await self.get_or_create(
            (Mailbox.type == "mailbox")
            & (Mailbox["address"] == address)
            & (Mailbox["account"] == account.name),
            {
                "uid": uuid.uuid4().hex,
                "account": account.name,
                "type": "mailbox",
                "address": address,
                "name": name,
                "last_message": None,
                "messages": [],
            },
        )
        return mailbox

    async def store_msg(
        self, msg, account, from_addr, to_addrs, incoming=True, extra_data=None
    ):
        """ Store message in database """

        msg["type"] = "mail"

        msg["uid"] = uuid.uuid4().hex

        msg["incoming"] = incoming
        msg["unread"] = incoming
        msg["account"] = account.name

        if extra_data:
            msg.update(extra_data)

        eid = self.db.insert(msg)

        Mailbox = Query()

        # Mailbox to link mail
        mailboxes = []
        if incoming:
            mailboxes.append(
                (msg["from"].addr_spec, msg["from"].display_name)
            )  # store only in `from` box
        else:
            if len(to_addrs) == 1:
                to = to_addrs[0]
                mailboxes.append((to.addr_spec, to.display_name))
            else:  # TODO Create group mailboxes
                for to in to_addrs:  # Put in all recipients boxes
                    mailboxes.append((to.addr_spec, to.display_name))

        for mailbox_address, mailbox_name in mailboxes:
            # Get mailbox if exists or create it
            mailbox = await self.get_or_create_mailbox(
                account, mailbox_address, mailbox_name
            )

            # Update last_message date
            if mailbox["last_message"] is None or mailbox["last_message"] < msg["date"]:
                mailbox["last_message"] = msg["date"]

            mailbox_message_data = {"id": eid, "uid": msg["uid"], "date": msg["date"]}

            mailbox["messages"].append(mailbox_message_data)

            self.db.update(
                mailbox, (Mailbox.type == "mailbox") & (Mailbox.uid == mailbox["uid"])
            )

        return msg

    async def get_mailboxes(self, account):
        """ Return all mailboxes in db with unread message count and total """

        Mailbox = Query()
        Message = Query()

        mailboxes = list(
            self.db.search(
                (Mailbox.type == "mailbox") & (Mailbox["account"] == account.name)
            )
        )
        for mailbox in mailboxes:
            mailbox["total"] = len(mailbox["messages"])
            mailbox["unreads"] = 0
            for message in mailbox["messages"]:
                msg = await self.get(Message.uid == message["uid"])
                mailbox["unreads"] += 1 if msg.get("unread") else 0

        # TODO Hacky
        for m in mailboxes:
            if not isinstance(m["last_message"], datetime.datetime):
                m["last_message"] = arrow.get(m["last_message"]).datetime

        return mailboxes

    async def get_unreads(self, account, offset=0, limit=None):
        """ Return all unreads messages uids """

        Mailbox = Query()
        Message = Query()

        mailboxes = list(
            self.db.search(
                (Mailbox.type == "mailbox") & (Mailbox["account"] == account.name)
            )
        )
        unreads = []
        for mailbox in mailboxes:
            for message in mailbox["messages"]:
                msg = await self.get(Message.uid == message["uid"])
                if msg["unread"]:
                    unreads.append({"mailbox": mailbox["uid"], "message": msg["uid"]})

        return unreads

    async def get_mailbox(self, account, mailbox_id):
        """ Return the selected mailboxx """
        Mailbox = Query()
        Message = Query()

        mailbox = await self.get(
            (Mailbox.uid == mailbox_id) & (Mailbox["account"] == account.name)
        )

        messages = []
        mailbox["total"] = len(mailbox["messages"])
        mailbox["unreads"] = 0
        for message in mailbox["messages"]:
            msg = await self.get(Message.uid == message["uid"])
            msg = dict(msg)
            del msg["body"]
            mailbox["unreads"] += 1 if msg.get("unread") else 0
            messages.append(msg)

        mailbox["messages"] = messages

        # TODO reverse me
        for m in messages:
            if not isinstance(m["date"], datetime.datetime):
                m["date"] = arrow.get(m["date"]).datetime

        return mailbox

    async def get_mail(self, account, mail_uid):
        """ Get message by uid """

        Message = Query()

        mail = await self.get(
            (Message.uid == mail_uid) & (Message["account"] == account.name)
        )

        if not isinstance(mail["date"], datetime.datetime):
            mail["date"] = arrow.get(mail["date"]).datetime

        return mail

    async def get_mail_attachment(self, account, mail_uid, att_index):
        """ Return a specific mail attachment """

        Message = Query()

        mail = await self.get(
            (Message.uid == mail_uid) & (Message["account"] == account.name)
        )
        raw_mail = await self.get_content_msg(mail_uid)

        attachment = mail["attachments"][att_index]

        atts = list(raw_mail.iter_attachments())
        stream = atts[att_index]

        content = stream.get_content()
        return attachment, content

    async def update_mail(self, account, mail):
        """ Update any mail """

        Message = Query()

        self.db.update(
            mail, (Message.uid == mail["uid"]) & (Message["account"] == account.name)
        )

        return mail

    async def mark_mail_read(self, account, mail_uid):
        """ Mark a mail as read for an account """

        Message = Query()

        mail = await self.get(
            (Message.uid == mail_uid) & (Message["account"] == account.name)
        )

        mail["unread"] = False

        await self.update_mail(account, mail)

        return mail

    async def save_user_session(self, session_key, session):
        """ Save modified user session """
        Session = Query()

        session_from_storage = await self.get_user_session(session_key)
        session_from_storage.update(session)

        self.db.update(
            session_from_storage,
            (Session.type == "session") & (Session.key == session_key),
        )

    async def get_user_session(self, session_key):
        """ Load user session """

        Session = Query()
        return await self.get_or_create(
            (Session.type == "session") & (Session.key == session_key),
            {"type": "session", "key": session_key},
        )

    async def contacts_search(self, account, text):
        """ Search a contact from mailboxes """

        Mailbox = Query()

        results = self.db.search(
            (Mailbox.type == "mailbox")
            & (Mailbox["account"] == account.name)
            & (Mailbox["address"].search(text))
        )

        return [r["address"] for r in results[:10]]
Example #55
0
"""
Created on Thu Apr 26 11:20:42 2018

@author: bartlomiejparka
"""
from tinydb import TinyDB, Query

import pandas as pd
from pandas import DataFrame, read_csv
from datetime import datetime
import numpy as np

db = TinyDB('db.json')
User = Query()
db.insert({'name': 'John', 'age': 22})
db.search(User.name == 'John')

key_array = []

df_init = pd.read_csv(
    '/Users/bartlomiejparka/Documents/Resourcing App/grmt_resource_requests.csv'
)

col_list_keep = [
    'Job Title', 'Status', 'Openseat Number', 'Duplicate Seat',
    'Assignment Type', 'Work Location', 'Landed', 'Contract Specification',
    'PgW', 'Lot', 'Project Name', 'Confirmed Candidate', 'RO Number',
    'Commercial Status', 'ADAM Grade', 'No.Required', 'PRG Band High',
    'PRG Band Low', 'Open to Contractors', 'BP Requested Seat?',
    'IBM Requested Start Date', 'BP Requested Start Date', 'BP PM', 'PGW Lead',
    'IBM PM', 'Job Description', 'Mandatory Skills', 'Desirable skills'
Example #56
0
from tinydb import TinyDB, Query

# connect to database
db = TinyDB('sensor_reports.json')
doc = Query()

anom = db.search(doc.state == "anomaly")
print("anomalies", anom)
print("values", [a["value"] for a in anom])
Example #57
0
def main() -> None:
    st.title("Лабораторная работа №5")
    st.markdown(
        "<h3 style='margin-bottom: 50px'>"
        "{task}"
        "</h3>".format(task=task),
        unsafe_allow_html=True)

    database = TinyDB("database.json")

    answers = []
    for item in database:
        question = f"{item['number']}) {item['question']}"
        answers.append(st.radio(question, item["answer"]))

    if st.button("Рассчитать данные"):
        d, m, n = 0, 0, 0
        for idx, answer in enumerate(answers):
            id = idx + 1
            info = Query()
            result = database.search(info.number == id)[0]["answer"]
            case = result.index(answer)

            if id == 1:
                if case == 0:
                    d += 1
                if case == 1:
                    n += 1
                if case == 2:
                    m += 1
            if id == 2:
                if case == 0:
                    d += 1
                if case == 1:
                    n += 1
                if case == 2:
                    m += 1
            if id == 3:
                if case == 0:
                    m += 1
                if case == 1:
                    n += 1
                if case == 2:
                    d += 1
            if id == 4:
                if case == 0:
                    m += 1
                if case == 1:
                    d += 1
                if case == 2:
                    n += 1
            if id == 5:
                if case == 0:
                    n += 1
                if case == 1:
                    m += 1
                if case == 2:
                    d += 1
            if id == 6:
                if case == 0:
                    n += 1
                if case == 1:
                    d += 1
                if case == 2:
                    m += 1
            if id == 7:
                if case == 0:
                    d += 1
                if case == 1:
                    m += 1
                if case == 2:
                    n += 1
            if id == 8:
                if case == 0:
                    n += 1
                if case == 1:
                    d += 1
                if case == 2:
                    m += 1
            if id == 9:
                if case == 0:
                    n += 1
                if case == 1:
                    m += 1
                if case == 2:
                    d += 1
            if id == 10:
                if case == 0:
                    m += 1
                if case == 1:
                    n += 1
                if case == 2:
                    d += 1

        d = d / 10 * 100
        m = m / 10 * 100
        n = n / 10 * 100

        df = pd.DataFrame(
            data=[d, m, n],
            index=[
                "Делопроизводитель",
                "Менеджер",
                "Начальник"
            ],
            columns=["Вероятность"]
        )
        st.write(df)
Example #58
0
    tlist = []
    #pprint(handles)

    for h in handles:
        s = h.status()

        info = {
            'name': s.name,
            'progress': s.progress,
            'state': state_str[s.state]
        }

        print(s.name)
        tlist.append(info)

        #print ('%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
        #      (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
        #       s.num_peers, state_str[s.state]))

    return tlist


threading.Thread(target=consume_torrents).start()
threading.Thread(target=dispatcher).start()

#Search the DB for torrents to resume...
resume_list = db.search(User.url.exists())
for t in resume_list:
    if not t['completed']:
        queueTorrent(t['url'], t['uuid'])
Example #59
0
class WSDaemon():
    def __init__(self, options):
        self.kb_listener = keyboard.Listener(on_press=self.listener_on_press)
        self.kb_controller = keyboard.Controller()
        self.buffer_arr = []
        self.buffer_limit = int(options['buffer_limit'])
        self.expansion_file = options['expansion_file']
        self.trigger_prefix = options['trigger_prefix']
        self.cheatsheet_file = options['cheatsheet_file']
        self.expansion_arr = []
        self.load_keywords()

    def init_db(self):
        ##Try to close DB, then load a new copy.
        try:
            self.db_handle.close()
        except:
            pass

        self.db_handle = TinyDB(self.expansion_file, sort_keys=True, indent=1)

    def start(self):
        self.kb_listener.start()

    def listener_on_press(self, key_obj):
        self.update_buffer(key_obj)
        self.parse_buffer()

    def update_buffer(self, key_obj):
        if isinstance(key_obj, keyboard.KeyCode):
            ##Drop oldest character from buffer if we're over limit.
            char = key_obj.char
            if len(self.buffer_arr) + 1 > self.buffer_limit + len(
                    self.trigger_prefix):
                self.buffer_arr.pop(0)
            ##Add character to buffer
            self.buffer_arr.append(char)
        ##Delete the last character if backspace is pressed and array is not empty.
        elif key_obj == keyboard.Key.backspace and len(self.buffer_arr):
            self.buffer_arr.pop()

    def parse_buffer(self):
        buffer_string = "".join(self.buffer_arr)
        for expansion in self.expansion_arr:
            if buffer_string.endswith(self.trigger_prefix +
                                      expansion["trigger"]):
                self.execute_expansion(expansion["trigger"],
                                       str(expansion["text"]))
                ##Ideally I think the buffer should be cleared, but the typing
                ##appears to occur asyncronously. So even if I clear the buffer
                ##after calling the "press" functions, the keypresses don't
                ##appear to register until returning to the main loop.
                ##However, in practice it doesn't seem to cause an issue.

    def load_keywords(self):
        self.init_db()
        del self.expansion_arr[:]

        for row in iter(self.db_handle):

            self.expansion_arr.append(row)
        self.create_cheatsheet()

    def save_expansion(self, trigger, expansion):
        if not self.validate_unique_trigger(trigger):
            return False
        self.db_handle.insert({"trigger": trigger, "text": expansion})
        self.load_keywords()
        return True

    def delete_expansion(self, trigger):
        trigger_query = Query()
        ##Only try to delete if it already exists...
        if validate_unique_trigger(trigger):
            self.db_handle.remove(trigger_query == trigger)

    def validate_unique_trigger(self, trigger):
        trigger_query = Query()
        ##If there are no results we want to evaluate to true, false if
        ##anything is returned:
        return not self.db_handle.search(trigger_query.trigger == trigger)

    def execute_expansion(self, trigger, expansion):
        ##Erase the typed trigger, spit out the expansion.
        delete_length = len(self.trigger_prefix + trigger)
        for i in range(delete_length):
            self.kb_controller.press(keyboard.Key.backspace)

        for char in expansion:
            self.kb_controller.press(char)

    def create_cheatsheet(self):
        cs_generator = CheatSheetGenerator(self.cheatsheet_file)
        for expansion in self.expansion_arr:

            trigger = expansion["trigger"]
            text = expansion["text"]
            cs_generator.add_entry(trigger, text)

        cs_generator.write_output()
Example #60
0
def upload_user_file():
    """Upload a file."""
    user_name =  request.form['user_name']
    file_type = request.form['file_type']
    format = request.form['format']

    db = TinyDB(USERS_MANIFEST)
    qu = Query()
    qur = db.search(qu.username == user_name)

    if len(qur) == 0:
        return abort(400, 'user manifest not found')

    path = qur[0]["path"]

    file_name = request.form['file_name']
    if '/' in file_name:
        # Return 400 BAD REQUEST
        abort(400, 'no subdirectories directories allowed')

    if format == "b64":
        content = request.form['content']
        dat = base64.b64decode(content)
    else:
        dat = request.files['files'].read()

    print dat
    #im = Image.open(BytesIO(base64.b64decode(dat)))
    sender = request.form['sender']


    if sender != None:
        if sender == "mobileApp":
            work = request.form['work_name']
            batch = request.form['batch_name']
            construction_site = request.form['construction_site_name']
            zone = request.form['zone_name']
            path = os.path.join(path, zone)
            path = os.path.join(path, construction_site)
            path = os.path.join(path, batch)
            path = os.path.join(path, work)


    with open(os.path.join(path, file_name), 'wb') as fp:
        fp.write(dat)

    db = TinyDB(os.path.join(path, "manifest.json"))
    db.insert({"name": user_name,
               "logs": {
                   "log": {
                       "desc": "file uploaded : " + file_name,
                       "date": dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                       "details":{
                           "type": file_type,
                           "path": os.path.join(path, file_name)
                       }

                   }
               }
               })

    # Return 201 CREATED
    return '', 201