Пример #1
0
 def test_username_correct(self):
     """
     Tests that underscores are converted to spaces
     """
     self.assertEqual(get_username(self.object1, field='field'), 'BBT')
     self.assertEqual(get_username(self.object2, field='field'), 'Joe Silent')
     self.assertEqual(get_username(self.object2), 'Joe Silent')
Пример #2
0
    def __init__(
        self,
        request,
        form_text_name=None,
        form_img_name=None,
        require_text=True,
        validate_text=True,
        require_img=True,
    ):
        self.valid = True
        self.invalid_message = None

        self.request = request
        self.text_name = form_text_name
        self.img_name = form_img_name
        self.require_text = require_text
        self.require_img = require_img
        self.validate_text = validate_text

        self.ip = get_ip_from_request(self.request)
        self.text = request.form.get(form_text_name, None)
        self.img = make_img_from_request(request, form_img_name)
        self.user = get_username()

        self.event = get_event(self.ip)
        push_event(self.ip, event=self.event)

        self.set_is_valid()
Пример #3
0
def removeadmin(update, ctx):
    message = update.message
    reply = message.reply_to_message
    if not reply:
        message.reply_text(storage.get_string("REMOVEADMIN_NO_REPLY"))
    else:
        reply_author = reply.from_user.id
        username = utils.get_username(reply_author, bot)
        if storage.is_admin(reply_author):
            if storage.is_manager(reply_author):
                message.reply_text(
                    storage.get_string("REMOVEADMIN_IS_MANAGER"))
            else:
                storage.remove_admin(reply_author)
                string_template = Template(
                    storage.get_string("REMOVEADMIN_SUCCESS"))
                message.reply_text(
                    string_template.safe_substitute(username=username),
                    parse_mode=ParseMode.HTML)
        else:
            string_template = Template(
                storage.get_string("REMOVEADMIN_USER_NOT_ADMIN"))
            message.reply_text(
                string_template.safe_substitute(username=username),
                parse_mode=ParseMode.HTML)
Пример #4
0
def get_reason_and_warn(update, ctx):
    reason = update.message.text
    warn_initiator = update.effective_user.id
    user_id, timestamp = awaiting_warn_reason[warn_initiator]
    warns = storage.get_warns_for_user(user_id)
    if timestamp in warns:
        update.reply_text(storage.get_string("WARNED_WARNED_ALREADY_1"))
    else:
        warn_count = storage.add_warn_to_user(user_id, timestamp)
        template = Template(storage.get_string("WARN_WARNED"))
        username = utils.get_username(user_id, bot)
        update.message.reply_text(
            template.safe_substitute(username=username,
                                     warns=warn_count,
                                     max=storage.get_max_warns()))
        if warn_count == storage.get_max_warns():
            send_to_user_or_group(user_id, storage.get_string("WARN_BANNED"))
        else:
            template_user = Template(storage.get_string("WARN_WARNED_USER"))
            send_to_user_or_group(
                user_id,
                template_user.safe_substitute(reason=reason,
                                              warns=warn_count,
                                              max=storage.get_max_warns()))

        del awaiting_warn_reason[warn_initiator]
Пример #5
0
    def login(self, username=None, password=None):
        if not username:
            username = get_username()
        if not password:
            password = get_password()

        cursor = self.execute_query(get_login_query(username, password),
                                    get_dictionary=True)
        result = cursor.fetchone()
        return result
Пример #6
0
    async def hugs(self, ctx: commands.Context, user: discord.Member = None):
        """
        Get your lovely hug stats.
        """
        if user is None or user == ctx.author:
            user = ctx.author
            user_str = utils.get_username(user)
            title = "{0} Your Lovely Hug Stats {0}"
        else:
            user_str = utils.get_username(user)
            title = f"{{0}} {user_str}'s Lovely Hug Stats {{0}}"

        async with ctx.typing():
            stats = self.hugs_repo.get_members_stats(user.id)
            positions = self.hugs_repo.get_member_position(stats)
            avg_position = int((positions[0] + positions[1]) // 2)

            embed = discord.Embed(
                title=title.format(
                    self.get_default_emoji("peepoHugger") or ""
                ),
                description=" | ".join(
                    (
                        "**Ranks**",
                        f"Given: **{positions[0]}.**",
                        f"Received: **{positions[1]}.**",
                        f"Avg: **{avg_position}.**",
                    )
                ),
            )

            embed.set_author(name=user_str, icon_url=user.avatar_url)
            utils.add_author_footer(embed, ctx.author)

            given_emoji = self.get_default_emoji("peepohugs") or ""
            recv_emoji = self.get_default_emoji("huggers") or ""

            embed.add_field(name=f"{given_emoji} Given", value=str(stats.given))
            embed.add_field(name=f"{recv_emoji} Received", value=str(stats.received))

        await ctx.send(embed=embed)
        await self.check.botroom_check(ctx.message)
Пример #7
0
def update_creds(identifier):
    """Updates the credentials given the identifier and the new username and password."""
    # NOT DEAD CODE, prevents an UnboundLocalError from happening
    old_useraccount = 0
    new_useraccount = 0
    new_username = 0
    new_passwd = 0
    getting_identification = 0
    user_info = 0
    try:
        tempuser = UACC(identifier, '', '')
        if tempuser.identifier_is_valid():
            user_info = encryptedFileEditor.get_user_info(identifier)
            user_array = user_info.split(" ")
            if len(user_array) == 3:
                getting_identification = user_array[0]
                old_useraccount = UACC(getting_identification, user_array[1], user_array[2])
                print 'Please enter a new username'
                new_username = utils.get_username()
                print 'Please enter a new password'
                new_passwd = utils.get_passwd()
                new_useraccount = UACC(getting_identification, new_username, new_passwd)
                print "\n"
                if new_useraccount.identifier_is_valid():
                    if new_useraccount.username_is_valid():
                        if new_useraccount.password_is_valid():
                            print "Added New Credentials"
                            encryptedFileEditor.update_user(getattr(new_useraccount, 'identifier'),
                                                            new_useraccount.tostring())
                        else:
                            print password_error_message
                    else:
                        print username_error_message
                        if not new_useraccount.password_is_valid():
                            print password_error_message
                else:
                    print identifier_error_message
                    if not new_useraccount.username_is_valid():
                        print username_error_message
                        if not new_useraccount.password_is_valid():
                            print password_error_message
        else:
            print identifier_error_message
    except UserAccountNotFoundError:
        print '[WARNING] User account could not be found using identifier: \"' + identifier + \
              '\" skipping update.'
    finally:
        del old_useraccount
        del new_useraccount
        del new_username
        del new_passwd
        del getting_identification
        del user_info
        del tempuser
Пример #8
0
def remove_warns(update, ctx):
    message = update.message
    reply = message.reply_to_message
    if not reply:
        message.reply_text(storage.get_string("UNWARN_NO_REPLY"))
    else:
        reply_author = reply.from_user.id
        username = utils.get_username(reply_author, bot)
        template = Template(storage.get_string("UNWARN_OK"))
        storage.clear_warnings(reply_author)
        message.reply_text(template.safe_substitute(username=username))
Пример #9
0
def listadmins(update, ctx):
    message = update.message
    reply = storage.get_string("LIST_ADMINS_STRING")
    admins = ""
    for admin in storage.get_admin_set():
        admin_name = utils.get_username(admin, bot)
        print(admin_name)
        admins += "\t" + admin_name + "\n"
    template = Template(reply)
    message.reply_text(template.safe_substitute(admins=admins),
                       parse_mode=ParseMode.HTML)
Пример #10
0
def send_to_user_or_group(user_id, message):
    try:
        bot.send_message(user_id, message)
    except TelegramError as e:
        try:
            print(e)
            formatted_msg = utils.get_username(user_id, bot) + ": " + message
            bot.send_message(storage.get_target_chat(),
                             formatted_msg,
                             parse_mode=ParseMode.HTML)
        except TelegramError as e:
            print("Well i tried hard enough " + e.message)
Пример #11
0
def get_model():
    meta = request.json
    if not valid_credentials(meta["api_key"]):
        return {"message": "invalid access code"}, 401

    # todo: read and write access only to models dir
    model = f"{MODELS_DIR}/{meta['model_type']}/{meta['timestamp']}.model"
    if not os.path.isfile(model):
        return {"message": "invalid model - not found"}, 500
    username = get_username(meta["api_key"])
    log(f"get_model accessed by {username} - {meta['model_type']} - {meta['timestamp']}")
    return send_file(model, mimetype='application/octet-stream'), 200
Пример #12
0
def makeadmin(update, ctx):
    message = update.message
    reply = message.reply_to_message
    if not reply:
        message.reply_text(storage.get_string("MAKEADMIN_NO_REPLY"))
    else:
        reply_author = reply.from_user.id
        if reply_author != bot.id:
            storage.add_admin(reply_author)
            username = utils.get_username(reply_author, bot)
            template = Template(storage.get_string("ADMIN_GREETING"))
            message.reply_text(template.safe_substitute(admin=username),
                               parse_mode=ParseMode.HTML)
        else:
            message.reply_text(storage.get_string("REPLY_ID_SAME_AS_BOT"))
Пример #13
0
def evaluate_model():
    meta = request.json
    if not valid_credentials(meta["api_key"]):
        return {"message": "invalid access code"}, 401

    username = get_username(meta["api_key"])
    log(f"evaluate_model accessed by {username} - {meta['model_type']} - {meta['timestamp']}")

    model = f"{MODELS_DIR}/{meta['model_type']}/{meta['timestamp']}.model"
    if not os.path.isfile(model):
        return {"message": "invalid model - not found"}, 500

    eval_thread = Thread(target=evaluate_model_thread, args=(meta["model_type"], model), daemon=False)
    eval_thread.start()

    return {"message": "starting evaluation successful"}, 200
Пример #14
0
def log_message():
    """
    Log a message from a user

    available data fields:
        api_key
        message
    """
    meta = request.json
    if not valid_credentials(meta["api_key"]):
        return {"message": "invalid access code"}, 401

    username = get_username(meta["api_key"])
    message = meta["message"]
    log(f"Log message from {username} - {message}", True)
    return {'message': 'ok'}, 200
Пример #15
0
def main():
    signal(SIGINT, sig_handler)
    args = parse_args()
    default_branch = args.branch

    username = utils.get_username(args)
    if args.gerrit == "submit":
        gerrit.submit(default_branch, username, args.owner)
        sys.exit(0)
    elif args.gerrit == "vote":
        gerrit.vote(default_branch, username, args.owner)
        sys.exit(0)

    base_path = utils.get_base_path(default_branch)
    config_dict = utils.get_config_dict(args.config, default_branch)

    if args.path_to_crowdin == "crowdin" and not utils.check_dependencies():
        sys.exit(1)

    if args.upload_sources:
        upload.upload_sources_crowdin(default_branch, config_dict,
                                      args.path_to_crowdin)
    elif args.upload_translations:
        upload.upload_translations_crowdin(default_branch, config_dict,
                                           args.path_to_crowdin)
    elif args.download:
        xml_files = utils.get_xml_files(base_path, default_branch)
        download.download_crowdin(
            base_path,
            default_branch,
            xml_files,
            username,
            config_dict,
            args.path_to_crowdin,
        )
    elif args.unzip:
        xml_files = utils.get_xml_files(base_path, default_branch)
        from_zip.unzip(args.unzip, base_path, default_branch, xml_files,
                       username)

    if download.has_created_commits() or upload.has_uploaded():
        print("\nDone!")
        sys.exit(0)
    else:
        print("\nNothing to commit")
        sys.exit(2)
Пример #16
0
  def get_auth_creds(self):
    """
    return username, password to request auth token. If method is 'retrieve'
    and -p/--password_file arg is passed, user cred from .pantri_password.
    Else prompt user for creds
    """

    # Handling non-interactive actions.
    prompt_for_creds = self.prompt_for_creds()

    if prompt_for_creds:
      self.logger.info('Enter Credentials to Pull Auth Token')
      username = utils.get_username()
      password = getpass.getpass()
      return username, password
    else:
      creds = self.get_file_creds()
      return creds['username'], creds['password']
Пример #17
0
    def get_auth_creds(self):
        """
    return username, password to request auth token. If method is 'retrieve'
    and -p/--password_file arg is passed, user cred from .pantri_password.
    Else prompt user for creds
    """

        # Handling non-interactive actions.
        prompt_for_creds = self.prompt_for_creds()

        if prompt_for_creds:
            self.logger.info('Enter Credentials to Pull Auth Token')
            username = utils.get_username()
            password = getpass.getpass()
            return username, password
        else:
            creds = self.get_file_creds()
            return creds['username'], creds['password']
Пример #18
0
    async def hug(self, ctx: commands.Context, user: discord.Member = None, intensity: int = 0):
        """Because everyone likes hugs"""
        if user is None:
            user = ctx.author
        elif user.bot:
            await ctx.send(self.get_default_emoji("huggers") or ":people_hugging:")
            return

        async with ctx.typing():
            emojis = config.hug_emojis
            if user != ctx.author:
                self.hugs_repo.do_hug(giver_id=ctx.author.id, receiver_id=user.id)

            user_str = utils.get_username(user)

        if 0 <= intensity < len(emojis):
            await ctx.send(f"{emojis[intensity]} **{user_str}**")
        else:
            await ctx.send(f"{choice(emojis)} **{user_str}**")
Пример #19
0
def login(request):
    user_id = request.GET.get('user_id', '')
    token = request.GET.get('token', '')
    service = request.GET.get('service', '')
    response = {
        'status' : False,
        'user'   : None
    }
    if utils.authentication(user_id,token,service):
        users=AppUser.objects.filter(user_id=user_id)
        if users.count() > 0:
            response['status'] = True
        else:
            print token
            username = utils.get_username(user_id,token,service)
            user = AppUser(user_id=user_id,name=username)
            user.save()
            response['status'] = True
    return JsonResponse(response)
Пример #20
0
def upload_data():
    """
    Upload user data and save them to db/uploaded_data/<username>/<timestamp>.npz

    available data fields:
        api_key
    """
    meta = request.form.to_dict()
    if not valid_credentials(meta["api_key"]):
        log(f"Upload request of {meta['model_type']} rejected because api key {meta['api_key']} is invalid")
        return {"message": "invalid access code"}, 401

    username = get_username(meta["api_key"])

    data_dir = f"{DB_DIR}/uploaded_data/{username}"
    if not os.path.exists(data_dir): os.makedirs(data_dir)
    request.files['data_binary'].save(f"{data_dir}/{int(time.time())}.npz")

    log(f"upload_data accessed by {username} - upload successful")
    return {"message": "upload successful"}, 200
Пример #21
0
def save_github_search_repos(q='github.io', label='web', pages=35):
    load_config()
    url = "https://api.github.com/search/repositories?q=" + q + " + &page={page}&sort=stars"
    links = get_label_links(label=label)

    try:
        for i in range(pages):
            print str(i + 1)
            r = requests.get(url.format(page=i + 1), auth=(get_username(), get_token()))
            repos = json.loads(r.text)["items"]
            for repo in repos:
                name = repo["full_name"]
                links_length = len(links)
                links.add("https://github.com/" + name)
                if links_length != len(links):
                    print "Added: " + name
            time.sleep(2)
        write_label_links(links, label)
    except:
        write_label_links(links, label)
        print links
Пример #22
0
#!/usr/bin/python
# ADIC client program
#
# usage is the same as for "adiC" with additional
# required option "--username", e.g.:
# adic_client.py --username=nobody -vd gradient f.c
#
import sys, glob, socket
from ADIC import ADIC_Client
from utils import uniq, get_username, include_files, get_server, string

adic = ADIC_Client()
username, args = get_username(sys.argv[1:])
options,files = adic.check_options(args)
reduce(lambda x, y: x+y,map(glob.glob, files),[])      # expand unix wildcards 
files = uniq(include_files(files,adic.LanguageClass))  # add include files
try:
    host,port = get_server("r_adic")
except socket.error:
    sys.exit(1)
else:
    print adic.submit_request(host,port,username,string.join(options),files)

Пример #23
0
def detail(request, *args, **kwargs):
    if not request.session.get(
            'is_login', False) or request.session['user_type'] != 'admin':
        return redirect("/index")
    error_msg, page, page_list, projects_dict, project_id = initialize(
        args, request, True)
    with open("database/detail.pk", 'rb') as f:
        detail = pickle.load(f)
    if detail.get(project_id, None) is None:
        detail[project_id] = {}
        with open("database/detail.pk", 'wb') as f:
            f.write(pickle.dumps(detail))
    date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    if request.method == 'GET':
        return render(
            request, 'admin_financeoffdetail.html', {
                "projects_dict": projects_dict,
                "project_id": project_id,
                "page_list": page_list,
                "detail": detail[project_id].items(),
                "page": page,
                'error_msg': error_msg,
                "date": date,
            })
    if request.method == 'POST':
        data = {
            "name": request.POST.get("name"),
            "money": request.POST.get("money"),
            "account": request.POST.get("account"),
            "date": request.POST.get("date"),
            "grant": request.POST.get("grant"),
        }
        print data
        if not data.get("name", None):  # 检查项目名称是否合法
            error_msg = "请输入正确的款项名"
        # 负责人
        if not data.get("account", None):  # 检查负责人是否为空
            error_msg = "请输入正确负责人"
        elif utils.check_contain_chinese(data["account"]):  # 检查负责人账户是否合法
            username = utils.get_account(data["account"])
            if not username:
                error_msg = "用户 %s 不存在" % data['account']
            # data['account'] = username
        elif not utils.get_username(data["account"]):
            error_msg = "用户 %s 不存在" % data['account']
        # 检查预算
        if not data.get("money", None):
            error_msg = "请输入正确预算"
        else:
            data['money'] = utils.std_money(data['money'])
        if error_msg:
            return render(
                request, 'admin_financeoffdetail.html', {
                    "projects_dict": projects_dict,
                    "project_id": project_id,
                    "page_list": page_list,
                    "detail": detail[project_id].items(),
                    "page": page,
                    'error_msg': error_msg,
                    "date": date,
                })
        else:
            with open("database/detail.pk", 'rb') as f:
                detail = pickle.load(f)
            if detail.get(project_id, None) is None:
                detail[project_id] = {}
            print data.get('grant', None)
            try:
                detail[project_id][max(detail[project_id].keys()) + 1] = data
            except:
                detail[project_id][1] = data
            with open("database/detail.pk", 'wb') as f:
                f.write(pickle.dumps(detail))
            with open("database/projects.pk", 'rb') as f:
                projects = pickle.load(f)
            projects[project_id]["remitted"] += data['money']
            with open("database/projects.pk", 'wb') as f:
                f.write(pickle.dumps(projects))
            return render(
                request, 'admin_financeoffdetail.html', {
                    "projects_dict": projects_dict,
                    "project_id": project_id,
                    "page_list": page_list,
                    "detail": detail[project_id].items(),
                    "page": page,
                    'error_msg': error_msg,
                    "date": date,
                })
Пример #24
0
def get_reporter_and_user_handles(rep_id, author_id):
    username = utils.get_username(author_id, bot)
    reporter_name = utils.get_username(rep_id, bot)

    return (reporter_name, username)
Пример #25
0
 def assigned(self, username):
     raw = utils.get_username(username)
     self.ownerPHID = raw['phid']
Пример #26
0
def main():
    """User interface for the Password manager"""
    utils.print_splash()
    # Menu that the user will see
    menu = '\nWhat would you like to do?\n\n' \
           '1. Add Credentials\n' \
           '2. Get Credentials\n' \
           '3. Update Credentials\n' \
           '4. Remove Credentials\n' \
           '5. Quit\n\n' \
           'Please enter the integer of your choice: '
    # user options
    options = ['1', '2', '3', '4', '5']
    user_option = 0
    try:
        datastore = 'storage.enc'
        # if there is no existing file get a new password for authentication
        if os.path.isfile('.eta'):
            # Get the password from the user
            utils.authenticate()
        else:
            print 'Welcome to ETA Password Manager!'
            if os.path.isfile(datastore):
                answer = raw_input('A prior instance of the password manager has been found.\n' \
                'If you are creating a new account, the previous data from the password manager will be overwritten.\n' \
                'Are you sure you would like to overwrite the previous data? [y/n] ').strip().lower()
                if answer == 'y':
                    os.remove(datastore)
                else:
                    raise authenticationError("[ERROR] Failed to authenticate. " \
                    "Previous data detected, and .eta file missing.")
            print 'Please enter a password you want to use to access your account. '
            utils.new_passwd(utils.get_passwd())
        # add init here
        encryptedFileEditor.init()
        path = os.getcwd()

        # Run until the user quits the program
        while user_option != '5':
            # Get the Users input option, input must be a valid integer 1, 2, 3, or 4
            user_option = raw_input(menu)
            # User input must be valid to continue
            while user_option not in options:
                print '[ERROR] Improper input, please enter a valid integer (1 - 5).'
                user_option = raw_input(menu)

            # Perform the action based on the users choice
            if user_option == '1':
                print '********Add Credentials********'
                # add uacc object
                identifier = utils.get_identifier()
                username = utils.get_username()
                passwd = utils.get_passwd()
                user_account = UACC(identifier,username, passwd)
                print "\n"
                # Add the credentials using the given identifier, username, password combo
                useroptions.add_creds(user_account)
                # Remove sensitive information from memory
                del user_account
                del identifier
                del username
                del passwd
                # cause a pause after running.
                raw_input("Press enter to continue...")

            elif user_option == '2':
                print '********Get Credentials********'
                # Use the identifier to get the username and password for the authenticated user
                identifier = utils.get_identifier()
                # Call the get_creds function with the given identifier
                useroptions.get_creds(identifier)
                del identifier
                raw_input("Press enter to continue...")

            elif user_option == '3':
                print '********Update Credentials********'
                # Use the identifier and update the username and password for that identifier
                identifier = utils.get_identifier()
                # call update_creds function
                useroptions.update_creds(identifier)
                del identifier
                raw_input("Press enter to continue...")

            elif user_option == '4':
                print '********Remove Credentials********'
                # Use the identifier and remove the credentials from the datastore
                identifier = utils.get_identifier()
                # Call remove_creds function
                useroptions.remove_creds(identifier)
                del identifier
                raw_input("Press enter to continue...")
    except authenticationError, autherr:
        print autherr.message
Пример #27
0
#!/usr/bin/python
# ADIC client program
#
# usage is the same as for "adiC" with additional
# required option "--username", e.g.:
# adic_client.py --username=nobody -vd gradient f.c
#
import sys, glob, socket
from ADIC import ADIC_Client
from utils import uniq, get_username, include_files, get_server, string

adic = ADIC_Client()
username, args = get_username(sys.argv[1:])
options, files = adic.check_options(args)
reduce(lambda x, y: x + y, map(glob.glob, files), [])  # expand unix wildcards
files = uniq(include_files(files, adic.LanguageClass))  # add include files
try:
    host, port = get_server("r_adic")
except socket.error:
    sys.exit(1)
else:
    print adic.submit_request(host, port, username, string.join(options),
                              files)
Пример #28
0
def upload_model():
    """
    available data fields:
        api_key
        model_type
        dice (optional)
    """
    meta = request.form.to_dict()
    if not valid_credentials(meta["api_key"]):
        log(f"Upload request of {meta['model_type']} rejected because api key {meta['api_key']} is invalid")
        return {"message": "invalid access code"}, 401

    username = get_username(meta["api_key"])

    if meta["model_type"] not in get_model_types():
        log(f"Upload request of {meta['model_type']} from {username} rejected because model type is invalid")
        return {"message": "invalid model type"}, 500

    #model_binary = request.files['model_binary'].read()  # read() is needed to get bytes from FileStorage object
    #model_delta = DynamicDLModel.Loads(model_binary)
    #model_orig_path = f"{MODELS_DIR}/{meta['model_type']}/{model_delta.timestamp_id}.model"
    #model_orig = DynamicDLModel.Load(open(model_orig_path, "rb")) # unused here - see merge_models
    # calc_delta(): new_model - orig_model = delta
    # apply_delta(): model_orig + delta = model_new
    # Model delta: only activate if rest of federated learning working properly
    # model = model_orig.apply_delta(model_delta)
    #model = model_delta

    # directly save received model to disk
    model_path = f"{MODELS_DIR}/{meta['model_type']}/uploads/{str(int(time.time()))}_{username}.model"
    request.files['model_binary'].save(model_path)

    dice = meta["dice"] if "dice" in meta else -1.0

    log(f"upload_model accessed by {username} - {meta['model_type']} - {model_path} - client dice {dice}")

    try:
        original_hash = meta["hash"]
    except:
        original_hash = None

    local_hash = calculate_file_hash(model_path)

    if original_hash is not None and original_hash != local_hash:
        log("Error during model upload")
        return {"message": "Communication error during upload"}, 500

    #model.dump(open(model_path, "wb"))

    print("Starting merge...")
    # Thread needed. With multiprocessing.Process this will block in docker+nginx
    # (daemon=True/False works both)
    # merge_thread = Thread(target=merge_model, args=(meta["model_type"], model_path), daemon=False)
    merge_thread = Thread(target=merge_model_thread, args=(meta["model_type"], model_path), daemon=False)
    merge_thread.start()

    merged_model = 1

    if merged_model is not None:
        return {"message": "upload successful"}, 200
    else:
        print("Info: merging of models failed, because validation Dice too low.")
        return {"message": "merging of models failed, because validation Dice too low."}, 500
Пример #29
0
import utils

if __name__ == '__main__':
    username = '******'
    # print(username)
    print(utils.get_username(username))
    pass
Пример #30
0
def reply_check(update,context):
    bot_username = update.message.reply_to_message.from_user.username

    user = update.message.from_user
    username =utils.get_username(update,context)
    user_answer = update.message.text
    group_id = update.message.chat.id
    chat_type = update.message.chat.type
    admin = utils.Admin(userid=user.id, language=configs.LANGUAGE).get_data()
    #check if the user is replying in the group and to the bot's message
    if chat_type != "private" and bot_username == configs.BOTUSERNAME:
        # chat message id
        message_id = update.message.reply_to_message.message_id
        #question type(apollo,gaia,etc)
        bot_type = utils.bot_type(message_id=message_id)
        botify.logger.warning(f'bot type: {bot_type}')
        if bot_type=='Apollo':
            # if the user is not an admin
            if admin==False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.check_try(userId=user.id, messageId=message_id, bot=bot_type)
                if tries != True:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_apollo_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')

                    if int(que_level) >= int(student_level):
                        """Check user answer"""
                        answer = sql.get_apollo_answer_by_msgId(msgId=message_id)
                        if utils.stripper(text=user_answer) == utils.stripper(text=answer):
                            """add user's fortuna in this language and exercise"""
                            utils.AddFortunas(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                            """increase count of users who tried this question"""
                            sql.update_correct(messageId=message_id)
        elif bot_type=='Seshat':
            if admin==False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.check_try(userId=user.id, messageId=message_id, bot=bot_type)
                if tries != True:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_seshat_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')
                    if int(que_level) >= int(student_level):
                        """Check user answer"""
                        answer = sql.get_seshat_answer_by_msgId(msgId=message_id)
                        correct_answer=utils.stripper(text=answer).lower()
                        answer_submitted=utils.stripper(text=user_answer).lower()
                        if answer_submitted == correct_answer:
                            """add user's fortuna in this language and exercise"""
                            utils.AddFortunas(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                            """increase count of users who tried this question"""
                            sql.update_correct(messageId=message_id)

        elif bot_type == 'Tyche':

            if admin==False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.check_try(userId=user.id, messageId=message_id, bot=bot_type)
                if tries != True:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_tyche_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')

                    if int(que_level) >= int(student_level):
                        """Check user answer"""
                        answer = sql.get_tyche_answer_by_msgId(msgId=message_id)
                        correct_answer = utils.stripper(text=answer).lower()
                        answer_submitted = utils.stripper(text=user_answer).lower()
                        if answer_submitted == correct_answer:
                            """add user's fortuna in this language and exercise"""
                            utils.AddFortunas(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                            """increase count of users who tried this question"""
                            sql.update_correct(messageId=message_id)

        elif bot_type == 'Leizi':
            if admin==False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.check_try(userId=user.id, messageId=message_id, bot=bot_type)
                if tries != True:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_leizi_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')

                    if int(que_level) >= int(student_level):
                        """Check user answer"""
                        answer = sql.get_leizi_answer_by_msgId(msgId=message_id)
                        try:
                            answer1, answer2 = answer
                            correct_answer1 = utils.stripper(text=answer1).lower()
                            correct_answer2 = utils.stripper(text=answer2).lower()
                            answer_submitted = utils.stripper(text=user_answer).lower()
                            if answer_submitted == correct_answer1 or answer_submitted == correct_answer2:
                                """add user's fortuna in this language and exercise"""
                                utils.AddFortunas(userid=user.id, language=configs.LANGUAGE,
                                                  exercise=bot_type).get_data()
                                """increase count of users who tried this question"""
                                sql.update_correct(messageId=message_id)
                        except :
                            pass

        elif bot_type == 'Odin':
            if admin==False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.count_chances(userId=user.id, messageId=message_id, bot=bot_type)

                if tries ==1:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_odin_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')

                    if int(que_level) >= int(student_level):
                        """Check user answer"""
                        # answer = sql.get_odin_answer_by_msgId(msgId=message_id)
                        query = sql.get_odin_answer_by_msgId(msgId=message_id)
                        answer, meaning = query
                        try:

                            correct_answer = utils.stripper(text=answer).lower()

                            answer_submitted = utils.stripper(text=user_answer).lower()
                            if answer_submitted == correct_answer:
                                """add user's fortuna in this language and exercise"""
                                utils.AddFortunas(userid=user.id, language=configs.LANGUAGE,
                                                  exercise=bot_type).get_data()
                                """increase count of users who tried this question"""
                                sql.update_correct(messageId=message_id)
                        except :
                            pass

        elif bot_type == 'Zamo':
            if admin==False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.check_try(userId=user.id, messageId=message_id, bot=bot_type)
                if tries != True:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_zamo_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')
                    if int(que_level) >= int(student_level):
                        """Check user answer"""
                        answer = sql.get_zamo_answer_by_msgId(msgId=message_id)
                        correct_answer=utils.stripper(text=answer).lower()
                        answer_submitted=utils.stripper(text=user_answer).lower()
                        if answer_submitted == correct_answer:
                            """add user's fortuna in this language and exercise"""
                            utils.AddFortunas(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                            """increase count of users who tried this question"""
                            sql.update_correct(messageId=message_id)

        elif bot_type == 'nuwa':
            print(bot_type)
            if admin == False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.check_try(userId=user.id, messageId=message_id, bot=bot_type)
                if tries != True:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_nuwa_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')
                    if int(que_level) >= int(student_level):
                        """voice to text"""
                        answer = sql.get_nuwa_answer_by_msgId(msgId=message_id)
                        correct_answer = utils.stripper(text=answer).lower()

                        file_id = update.message.voice.file_id
                        newFile = context.bot.get_file(file_id)
                        newFile.download(f'nuwa_{user.id}.ogg')
                        length = update.message.voice.duration
                        if length < 10:
                            new = utils.convert_ogg_to_wav(f"nuwa_{user.id}.ogg",
                                                          f"nuwa_{user.id}.wav")
                            speech.file = new

                            langue = utils.language_select(language=configs.LANGUAGE)
                            text = speech.to_text(lang=langue)
                            utils.mr_logger(f"totext: {text},answer {answer}")
                            if text == 401:
                                update.message.reply_text(
                                    f"Hi {user.first_name}, I did not understand this, please try again")
                            elif text == 500:
                                update.message.reply_text(
                                    f"Sorry {user.first_name}, I got a little light headed, please try again")
                            elif text.lower() == correct_answer:
                                """add user's fortuna in this language and exercise"""
                                utils.AddFortunas(userid=user.id, language=configs.LANGUAGE,
                                                  exercise=bot_type).get_data()
                                """increase count of users who tried this question"""
                                sql.update_correct(messageId=message_id)
                                return utils.clear_nuwa(user_id=user.id)

        elif bot_type == 'gaia':
            print("BOT NAME",bot_type)
            if admin == False:
                """register/confirm the db under the question and language"""
                res = utils.CreateStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type,
                                          name=username).get_data()
                botify.logger.warning(f'creating student api called : {res}')
                """Check how many chances a user has got remaining,1 chance per user per question"""
                tries = sql.check_try(userId=user.id, messageId=message_id, bot=bot_type)
                if tries != True:
                    """mark the user as tried the question"""
                    sql.create_chance(userId=user.id, messageId=message_id, bot=bot_type)
                    """Check user level"""
                    student = utils.GetStudent(userid=user.id, language=configs.LANGUAGE, exercise=bot_type).get_data()
                    student_level = student['level']
                    qlevel = sql.get_gaia_level_by_msgId(msgId=message_id)
                    que_level = utils.levelq(qlevel)
                    botify.logger.warning(f'question level : {que_level}')
                    if int(que_level) >= int(student_level):
                        """voice to text"""
                        answer = sql.get_gaia_answer_by_msgId(msgId=message_id)
                        correct_answer = utils.stripper(text=answer)

                        file_id = update.message.voice.file_id
                        newFile = context.bot.get_file(file_id)
                        newFile.download('gaia_{}.ogg'.format(user.id))
                        length = update.message.voice.duration
                        if length < 10:
                            new = utils.convert_ogg_to_wav(f"gaia_{user.id}.ogg",
                                                          f"gaia_{user.id}.wav")
                            speech.file = new

                            langue = utils.language_select(language=configs.LANGUAGE)
                            text = speech.to_text(lang=langue)
                            utils.mr_logger(f"totext: {text},answer {answer}")
                            if text == 401:
                                update.message.reply_text(
                                    f"Hi {user.first_name}, I did not understand this, please try again")
                            elif text == 500:
                                update.message.reply_text(
                                    f"Sorry {user.first_name}, I got a little light headed, please try again")
                            elif text.lower() == correct_answer:
                                """add user's fortuna in this language and exercise"""
                                utils.AddFortunas(userid=user.id, language=configs.LANGUAGE,
                                                  exercise=bot_type).get_data()
                                """increase count of users who tried this question"""
                                sql.update_correct(messageId=message_id)
                                return utils.clear_gaia(user_id=user.id)
    elif chat_type=='private':
        message_id = update.message.reply_to_message.message_id
        session_type =solosql.get_session_type(userid=user.id,msgid=message_id)
        if session_type=='Apollo':
            canswer = solosql.get_answer_msgid(userid=user.id, msgid=message_id)
            if utils.stripper(text=user_answer) == utils.stripper(text=canswer):
                context.bot.delete_message(chat_id=user.id, message_id=message_id)
                pl = context.bot.send_message(chat_id=user.id, text=emoji.emojize(":fire:Your Answer is Correct:fire:",
                                                                                  use_aliases=True))
                correct = solosql.get_correct(user.id)
                print("correct", correct)
                correct += 1
                solosql.update_correct(user_id=user.id, correct=correct)
                print(pl.result().message_id)
                sleep(2)
                context.bot.delete_message(chat_id=user.id, message_id=pl.result().message_id)
                session, tries = solosql.get_tries(userid=user.id)
                tries -= 1
                solosql.update_tries(user.id, tries)
                session, tries = solosql.get_tries(userid=user.id)
                print(tries)
                if tries > 0:
                    apollo_ques = solosql.get_apollo_question()
                    if apollo_ques != False:
                        question, answer = apollo_ques
                        string = utils.remove_html_tags(utils.BotMessages(id=6).get_message())
                        message = f"<b>{string}</b>\n\n:orange_book:<i>{question}</i>"

                        payload = context.bot.send_message(chat_id=user.id,
                                                           text=emoji.emojize(message, use_aliases=True),
                                                           parse_mode='html')
                        messageId = payload.result().message_id
                        solosql.update_user_question(userid=user.id, answer=answer, session='Apollo',
                                                     message_id=messageId,
                                                     tries=tries, correct=correct)
                else:
                    key_main = [[InlineKeyboardButton('Africa', callback_data='solo_africa'),
                                 InlineKeyboardButton('Apollo', callback_data='solo_apollo')],
                                [InlineKeyboardButton('Gaia', callback_data='solo_gaia'),
                                 InlineKeyboardButton('Kadlu', callback_data='solo_kadlu')],
                                [InlineKeyboardButton('Leizi', callback_data='solo_leizi'),
                                 InlineKeyboardButton('Nuwa', callback_data='solo_nuwa')],
                                [InlineKeyboardButton('Odin', callback_data='solo_odin'),
                                 InlineKeyboardButton('Seshat', callback_data='solo_seshat')],
                                [InlineKeyboardButton('Tyche', callback_data='solo_tyche'),
                                 InlineKeyboardButton('Wala', callback_data='solo_wala')],
                                [InlineKeyboardButton('Zamo', callback_data='solo_zamo'),
                                 InlineKeyboardButton('Instructions', url='https://telegra.ph/Ra-v3-tutorials-05-08')],
                                [InlineKeyboardButton('Donate',
                                                      url='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WJYMNYLP9TQHL&source=url')]]
                    main_markup = InlineKeyboardMarkup(key_main)
                    correct = solosql.get_correct(user.id)
                    msg = f":fire: Congratulations you got {correct}/10,Keep on practicing to be the G.O.A.T"
                    context.bot.send_message(chat_id=user.id, text=emoji.emojize(msg, use_aliases=True),
                                             reply_markup=main_markup)
            else:
                context.bot.delete_message(chat_id=user.id, message_id=message_id)
                pl = context.bot.send_message(chat_id=user.id,
                                              text=emoji.emojize(
                                                  f":fire:Your Answer is Incorrect, the correct answer was: <b>{canswer}</b> :fire:",
                                                  use_aliases=True), parse_mode='html')

                print(pl.result().message_id)
                sleep(3)
                context.bot.delete_message(chat_id=user.id, message_id=pl.result().message_id)
                session, tries = solosql.get_tries(userid=user.id)
                tries -= 1
                solosql.update_tries(user.id, tries)
                session, tries = solosql.get_tries(userid=user.id)
                print(tries)
                if tries > 0:
                    apollo_ques = solosql.get_apollo_question()
                    if apollo_ques != False:
                        question, answer = apollo_ques
                        string = utils.remove_html_tags(utils.BotMessages(id=6).get_message())
                        message = f"<b>{string}</b>\n\n:orange_book:<i>{question}</i>"

                        payload = context.bot.send_message(chat_id=user.id,
                                                           text=emoji.emojize(message, use_aliases=True),
                                                           parse_mode='html')
                        messageId = payload.result().message_id
                        correct = solosql.get_correct(user.id)
                        solosql.update_user_question(userid=user.id, answer=answer, session='Apollo',
                                                     message_id=messageId,
                                                     tries=tries, correct=correct)
                else:
                    key_main = [[InlineKeyboardButton('Africa', callback_data='solo_africa'),
                                 InlineKeyboardButton('Apollo', callback_data='solo_apollo')],
                                [InlineKeyboardButton('Gaia', callback_data='solo_gaia'),
                                 InlineKeyboardButton('Kadlu', callback_data='solo_kadlu')],
                                [InlineKeyboardButton('Leizi', callback_data='solo_leizi'),
                                 InlineKeyboardButton('Nuwa', callback_data='solo_nuwa')],
                                [InlineKeyboardButton('Odin', callback_data='solo_odin'),
                                 InlineKeyboardButton('Seshat', callback_data='solo_seshat')],
                                [InlineKeyboardButton('Tyche', callback_data='solo_tyche'),
                                 InlineKeyboardButton('Wala', callback_data='solo_wala')],
                                [InlineKeyboardButton('Zamo', callback_data='solo_zamo'),
                                 InlineKeyboardButton('Instructions', url='https://telegra.ph/Ra-v3-tutorials-05-08')],
                                [InlineKeyboardButton('Donate',
                                                      url='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WJYMNYLP9TQHL&source=url')]]
                    main_markup = InlineKeyboardMarkup(key_main)
                    correct = solosql.get_correct(user.id)
                    msg = f":fire: Congratulations you got {correct}/10,Keep on practicing to be the G.O.A.T"
                    context.bot.send_message(chat_id=user.id, text=emoji.emojize(msg, use_aliases=True),
                                             reply_markup=main_markup)
        elif session_type=='Gaia':
            canswer = solosql.get_answer_msgid(userid=user.id, msgid=message_id)
            correct_answer = utils.stripper(text=canswer)
            try:
                file_id = update.message.voice.file_id
                newFile = context.bot.get_file(file_id)
                newFile.download('gaia_{}.ogg'.format(user.id))
                length = update.message.voice.duration
                if length < 10:
                    new = utils.convert_ogg_to_wav(f"gaia_{user.id}.ogg",
                                                   f"gaia_{user.id}.wav")
                    speech.file = new

                    langue = utils.language_select(language=configs.LANGUAGE)
                    text = speech.to_text(lang=langue)
                    utils.mr_logger(f"totext: {text},answer {canswer}")
                    if text == 401:
                        update.message.reply_text(
                            f"Hi {user.first_name}, I did not understand this, please try again")
                    elif text == 500:
                        update.message.reply_text(
                            f"Sorry {user.first_name}, I got a little light headed, please try again")
                    elif text.lower() == correct_answer:
                        context.bot.delete_message(chat_id=user.id, message_id=message_id)
                        pl = context.bot.send_message(chat_id=user.id,
                                                      text=emoji.emojize(":fire:Your Answer is Correct:fire:",
                                                                         use_aliases=True))
                        correct = solosql.get_correct(user.id)
                        print("correct", correct)
                        correct += 1
                        solosql.update_correct(user_id=user.id, correct=correct)
                        print(pl.result().message_id)
                        sleep(2)
                        context.bot.delete_message(chat_id=user.id, message_id=pl.result().message_id)
                        session, tries = solosql.get_tries(userid=user.id)
                        tries -= 1
                        solosql.update_tries(user.id, tries)
                        session, tries = solosql.get_tries(userid=user.id)
                        print(tries)
                        utils.clear_gaia(user_id=user.id)
                        if tries > 0:
                            gaia_ques = solosql.get_gaia_question()
                            if gaia_ques != False:
                                question, answer = gaia_ques
                                string = utils.remove_html_tags(utils.BotMessages(id=7).get_message())
                                message = f"<b>{string}</b>"
                                voice = utils.zamol_download(url=question)
                                payload = context.bot.send_audio(chat_id=user.id, audio=open(voice, 'rb'),
                                                                 caption=emoji.emojize(message, use_aliases=True),
                                                                 parse_mode='html')
                                # sleep(2)
                                # remove(voice)
                                messageId = payload.result().message_id
                                solosql.update_user_question(userid=user.id, answer=answer, session='Gaia',
                                                             message_id=messageId,
                                                             tries=tries, correct=correct)
                        else:
                            key_main = [[InlineKeyboardButton('Africa', callback_data='solo_africa'),
                                         InlineKeyboardButton('Apollo', callback_data='solo_apollo')],
                                        [InlineKeyboardButton('Gaia', callback_data='solo_gaia'),
                                         InlineKeyboardButton('Kadlu', callback_data='solo_kadlu')],
                                        [InlineKeyboardButton('Leizi', callback_data='solo_leizi'),
                                         InlineKeyboardButton('Nuwa', callback_data='solo_nuwa')],
                                        [InlineKeyboardButton('Odin', callback_data='solo_odin'),
                                         InlineKeyboardButton('Seshat', callback_data='solo_seshat')],
                                        [InlineKeyboardButton('Tyche', callback_data='solo_tyche'),
                                         InlineKeyboardButton('Wala', callback_data='solo_wala')],
                                        [InlineKeyboardButton('Zamo', callback_data='solo_zamo'),
                                         InlineKeyboardButton('Instructions',
                                                              url='https://telegra.ph/Ra-v3-tutorials-05-08')],
                                        [InlineKeyboardButton('Donate',
                                                              url='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WJYMNYLP9TQHL&source=url')]]
                            main_markup = InlineKeyboardMarkup(key_main)
                            correct = solosql.get_correct(user.id)
                            msg = f":fire: Congratulations you got {correct}/10,Keep on practicing to be the G.O.A.T"
                            context.bot.send_message(chat_id=user.id, text=emoji.emojize(msg, use_aliases=True),
                                                     reply_markup=main_markup)
                    elif text.lower() != correct_answer:
                        context.bot.delete_message(chat_id=user.id, message_id=message_id)
                        pl = context.bot.send_message(chat_id=user.id,
                                                      text=emoji.emojize(
                                                          f":fire:Your Answer is Incorrect, the correct answer was: <b>{canswer}</b> :fire:",
                                                          use_aliases=True), parse_mode='html')

                        print(pl.result().message_id)
                        sleep(3)
                        context.bot.delete_message(chat_id=user.id, message_id=pl.result().message_id)
                        session, tries = solosql.get_tries(userid=user.id)
                        tries -= 1
                        solosql.update_tries(user.id, tries)
                        session, tries = solosql.get_tries(userid=user.id)
                        print(tries)
                        if tries > 0:
                            gaia_ques = solosql.get_gaia_question()
                            if gaia_ques != False:
                                question, answer = gaia_ques
                                string = utils.remove_html_tags(utils.BotMessages(id=7).get_message())
                                message = f"<b>{string}</b>"
                                voice = utils.zamol_download(url=question)
                                payload = context.bot.send_audio(chat_id=user.id, audio=open(voice, 'rb'),
                                                                 caption=emoji.emojize(message, use_aliases=True),
                                                                 parse_mode='html')
                                # sleep(2)
                                # remove(voice)
                                messageId = payload.result().message_id
                                correct = solosql.get_correct(user.id)
                                solosql.update_user_question(userid=user.id, answer=answer, session='Gaia',
                                                             message_id=messageId,
                                                             tries=tries, correct=correct)
                        else:
                            key_main = [[InlineKeyboardButton('Africa', callback_data='solo_africa'),
                                         InlineKeyboardButton('Apollo', callback_data='solo_apollo')],
                                        [InlineKeyboardButton('Gaia', callback_data='solo_gaia'),
                                         InlineKeyboardButton('Kadlu', callback_data='solo_kadlu')],
                                        [InlineKeyboardButton('Leizi', callback_data='solo_leizi'),
                                         InlineKeyboardButton('Nuwa', callback_data='solo_nuwa')],
                                        [InlineKeyboardButton('Odin', callback_data='solo_odin'),
                                         InlineKeyboardButton('Seshat', callback_data='solo_seshat')],
                                        [InlineKeyboardButton('Tyche', callback_data='solo_tyche'),
                                         InlineKeyboardButton('Wala', callback_data='solo_wala')],
                                        [InlineKeyboardButton('Zamo', callback_data='solo_zamo'),
                                         InlineKeyboardButton('Instructions',
                                                              url='https://telegra.ph/Ra-v3-tutorials-05-08')],
                                        [InlineKeyboardButton('Donate',
                                                              url='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WJYMNYLP9TQHL&source=url')]]
                            main_markup = InlineKeyboardMarkup(key_main)
                            correct = solosql.get_correct(user.id)
                            msg = f":fire: Congratulations you got {correct}/10,Keep on practicing to be the G.O.A.T"
                            context.bot.send_message(chat_id=user.id, text=emoji.emojize(msg, use_aliases=True),
                                                     reply_markup=main_markup)
            except Exception as e:
                print(e)
                string = utils.remove_html_tags(utils.BotMessages(id=7).get_message())
                message = f"<b>{string}</b>"
                context.bot.send_message(chat_id=user.id, text=emoji.emojize(message, use_aliases=True),parse_mode="html")
Пример #31
0
class Builder:
	"""
	  A generic build class.
	"""

	project        = "vrayblender"
	version        = utils.VERSION
	revision       = utils.REVISION # Patches revision
	brev           = None           # Blender master revision
	commits        = '0'

	# Directories
	dir_build      = utils.path_join(os.getcwd(), "build")
	dir_install    = utils.path_join(os.getcwd(), "install")
	dir_release    = utils.path_join(os.getcwd(), "release")
	dir_source     = ""

	dir_blender     = ""
	dir_blender_svn = ""

	# Installation diractory name
	dir_install_name = "vrayblender"
	dir_install_path = utils.path_join(dir_install, dir_install_name)

	# Build archive for Mac and Linux
	# or NSIS installer for Windows
	generate_package = False
	generate_desktop = False
	generate_docs    = False
	with_installer   = 'NSIS'

	# Test mode - just print messages, does nothing
	mode_test      = True

	# Special mode used only by me =)
	mode_developer = False

	# Debug output of the script
	mode_debug     = False

	# Add V-Ray/Blender patches
	add_patches    = True

	# Add V-Ray/Blender datafiles
	add_datafiles  = True

	# Add patches from "extra" directory
	add_extra      = False

	# Add themes from "themes" directory
	add_themes     = False

	# Host info
	host_os        = utils.get_host_os()
	host_arch      = utils.get_host_architecture()
	host_name      = utils.get_hostname()
	host_username  = utils.get_username()
	host_linux     = utils.get_linux_distribution()

	# Install dependencies
	install_deps   = False
	build_deps     = False
	use_build_deps = False

	# Update sources
	update_blender = True
	update_patch   = True

	# Blender option
	use_debug      = False
	use_openmp     = True
	use_collada    = False
	use_sys_python = True
	use_sys_ffmpeg = True

	# Build settings
	build_arch          = host_arch
	build_threads       = 4
	build_optimize      = False
	build_optimize_type = "INTEL"
	build_clean         = False
	build_release       = False
	build_upload        = False
	checkout_revision   = None
	use_env_msvc        = False

	# user-config.py file path
	user_config         = ""

	# Use user defined user-config.py
	user_user_config    = ""

	# Max OS X specific
	osx_sdk             = "10.6"

	with_cycles         = False
	with_tracker        = False
	with_cuda           = False
	cuda_gpu            = "sm_21"
	with_osl            = False
	with_player         = False
	with_ge             = False

	use_proxy           = None

	use_github_branch   = None
	use_exp_branch      = None
	use_blender_hash    = None
	add_branch_name     = None

	vb30   = None
	vc2013 = None

	# Only prepare sources
	export_only = None

	def __init__(self, params):
		if not params:
			sys.stdout.write("Params are empty - using defaults...\n")

		for param in params:
			setattr(self, param, params[param])

		if self.mode_debug:
			for param in params:
				print("%s => %s" % (param, params[param]))
			print("")

		if not self.dir_source:
			sys.stderr.write("Fatal error!\n")
			sys.stderr.write("Source directory not specified!\n")
			sys.exit(2)

		if self.vb30:
			self.project += "3"
		elif self.use_github_branch == "dev/vray_for_blender/stable":
			self.project += "1"
		else:
			self.project += "2"


	def info(self):
		sys.stdout.write("\n")
		sys.stdout.write("Build information:\n")

		sys.stdout.write("OS: %s\n" % (self.host_os.title()))

		if self.host_os == utils.LNX:
			sys.stdout.write("Distribution: %s %s\n" % (self.host_linux["long_name"], self.host_linux["version"]))

		sys.stdout.write("Architecture: %s\n" % (self.host_arch))
		sys.stdout.write("Build architecture: %s\n" % (self.build_arch))
		sys.stdout.write("Target: %s %s (%s)\n" % (self.project, self.version, self.revision))
		sys.stdout.write("Source directory:  %s\n" % (self.dir_source))
		sys.stdout.write("Build directory:   %s\n" % (self.dir_build))
		sys.stdout.write("Install directory: %s\n" % (self.dir_install_path))
		sys.stdout.write("Release directory: %s\n" % (self.dir_release))
		sys.stdout.write("\n")


	def update_sources(self):
		"""
		  Getting/updating sources
		"""

		def exportSources():
			sys.stdout.write("Exporting sources...\n")
			if self.mode_test:
				return

			if os.path.exists(self.dir_blender):
				utils.remove_directory(self.dir_blender)

			# Copy full tree to have proper build info.
			shutil.copytree(self.dir_blender_svn, self.dir_blender)

			os.chdir(self.dir_blender)
			os.system("git remote update github")
			os.system("git checkout -b {branch} github/{branch}".format(branch=self.use_github_branch))

			if self.checkout_revision is not None:
				os.chdir(self.dir_blender)
				os.system("git checkout %s" % self.checkout_revision)

		# Update Blender sources
		if self.update_blender:
			if os.path.exists(self.dir_blender):
				sys.stdout.write("Removing exported sources...\n")
				if not self.mode_test:
					utils.remove_directory(self.dir_blender)

			if not os.path.exists(self.dir_blender_svn):
				sys.stdout.write("Obtaining Blender sources...\n")
				if not self.mode_test:
					os.chdir(self.dir_source)

					# Obtain sources
					os.system("git clone %s blender" % GITHUB_REPO)

					# Now set origin to Blender's git and additional github remote
					# This is needed for proper submodules init
					os.chdir(self.dir_blender)
					os.system("git remote set-url origin %s" % OFFICIAL_REPO)
					os.system("git remote add github %s" % GITHUB_REPO)
					os.system("git remote update")
					os.system("git pull --rebase")

					os.chdir(self.dir_blender)
					os.system("git submodule update --init --recursive")
					os.system("git submodule foreach git checkout master")
					os.system("git submodule foreach git pull --rebase origin master")

					os.chdir(self.dir_source)
					# Move "blender" to "blender-git"
					utils.move_directory(self.dir_blender, self.dir_blender_svn)

			else:
				sys.stdout.write("Updating Blender sources...\n")
				if not self.mode_test:
					os.chdir(self.dir_blender_svn)

					# Update sources
					os.system("git pull --rebase")
					os.system("git submodule foreach git pull --rebase origin master")

			exportSources()

			# Update Blender libs
			lib_dir = None
			svn_cmd = None
			if self.host_os != utils.LNX:
				if self.host_os == utils.WIN:
					lib_dir = utils.path_join(self.dir_source, "lib", "windows")
					svn_cmd = "svn checkout https://svn.blender.org/svnroot/bf-blender/trunk/lib/windows lib/windows"
					if self.host_arch == "x86_64":
						if self.vc2013:
							lib_dir = utils.path_join(self.dir_source, "lib", "win64_vc12")
							svn_cmd = "svn checkout https://svn.blender.org/svnroot/bf-blender/trunk/lib/win64_vc12 lib/win64_vc12"
						else:
							lib_dir = utils.path_join(self.dir_source, "lib", "win64")
							svn_cmd = "svn checkout https://svn.blender.org/svnroot/bf-blender/trunk/lib/win64 lib/win64"
				elif self.host_os == utils.MAC:
					lib_dir = utils.path_join(self.dir_source, "lib", "darwin-9.x.universal")
					svn_cmd = "svn checkout https://svn.blender.org/svnroot/bf-blender/trunk/lib/darwin-9.x.universal lib/darwin-9.x.universal"

				if not os.path.exists(lib_dir):
					sys.stdout.write("Getting \"lib\" data...\n")
					if not self.mode_test:
						os.chdir(self.dir_source)
						os.system(svn_cmd)
				else:
					sys.stdout.write("Updating \"lib\" data...\n")
					if not self.mode_test:
						os.chdir(lib_dir)
						os.system("svn update")

		# Update V-Ray/Blender patchset
		if self.update_patch and not self.mode_developer:
			vb25_patch = utils.path_join(self.dir_source, "vb25-patch")

			if os.path.exists(vb25_patch):
				sys.stdout.write("Updating V-Ray/Blender patches...\n")
				if not self.mode_test:
					os.chdir(vb25_patch)
					os.system("git pull")
			else:
				sys.stdout.write("Getting V-Ray/Blender patches...\n")
				if not self.mode_test:
					os.chdir(self.dir_source)
					os.system("git clone git://github.com/bdancer/vb25-patch.git")


	def update(self):
		self.revision, self.brev, self.commits = utils.get_svn_revision(self.dir_blender)
		self.version = utils.get_blender_version(self.dir_blender)[0]
		self.versionArr = utils.get_blender_version(self.dir_blender)

		if self.build_release:
			self.dir_install_name = utils.GetInstallDirName(self)
		else:
			self.dir_install_name = self.project

		self.dir_install_path = utils.path_join(self.dir_install, self.dir_install_name)


	def patch(self):
		patch_dir = utils.path_join(self.dir_source, "vb25-patch")

		if self.use_blender_hash:
			patchBin      = utils.find_patch()
			patchFilepath = os.path.join(tempfile.gettempdir(), "vray_for_blender.patch")

			os.chdir(self.dir_blender)

			os.system("git checkout %s" % self.use_github_branch) # Checkout exporter branch
			os.system("git diff master > %s" % patchFilepath)     # Generate diff with master
			os.system("git fetch --tags")                         # Hash could be tag also
			os.system("git checkout %s" % self.use_blender_hash)  # Checkout needed revision
			os.system("git checkout -b vray_for_blender")         # Create some branch for patching
			os.system("patch -Np1 -i %s" % patchFilepath)         # Apply patch

			os.remove(patchFilepath)

		# Add datafiles: splash, default scene etc
		if self.add_datafiles:
			sys.stdout.write("Adding datafiles...\n")

			datafiles_path = utils.path_join(self.dir_blender, "release", "datafiles")

			if not self.mode_test:
				# Change splash
				for splash_filename in ["splash.png", "splash_2x.png"]:
					splash_path_src = utils.path_join(patch_dir, "datafiles", splash_filename)
					splash_path_dst = utils.path_join(datafiles_path, splash_filename)

					shutil.copyfile(splash_path_src, splash_path_dst)

				# Change icons
				for subdir in ["blender_icons16", "blender_icons32"]:
					icons_path_src = utils.path_join(patch_dir, "datafiles", subdir)
					icons_path_dst = utils.path_join(datafiles_path, subdir)

					shutil.rmtree(icons_path_dst)
					shutil.copytree(icons_path_src, icons_path_dst)


	def docs(self):
		if self.generate_docs:
			api_dir = utils.path_join(self.dir_install_path, "api")

			sys.stdout.write("Generating API documentation: %s\n" % (api_dir))

			if self.host_os != utils.LNX:
				sys.stdout.write("API documentation generation is not supported on this platform.\n")

			else:
				if not self.mode_test:
					sphinx_doc_gen = "doc/python_api/sphinx_doc_gen.py"

					# Create API directory
					os.system("mkdir -p %s" % api_dir)

					# Generate API docs
					os.chdir(self.dir_blender)
					os.system("%s -b -P %s" % (utils.path_join(self.dir_install_path, "blender"), sphinx_doc_gen))
					os.system("sphinx-build doc/python_api/sphinx-in %s" % api_dir)


	def post_init(self):
		"""
		  Override this method in subclass.
		"""
		pass


	def init_paths(self):
		if self.generate_package:
			if not self.mode_test:
				utils.path_create(self.dir_release)

		self.dir_build        = utils.path_slashify(self.dir_build)
		self.dir_source       = utils.path_slashify(self.dir_source)
		self.dir_install_path = utils.path_slashify(self.dir_install_path)

		self.dir_blender      = utils.path_join(self.dir_source, "blender")
		self.dir_blender_svn  = utils.path_join(self.dir_source, "blender-git")
		self.user_config      = utils.path_join(self.dir_blender, "user-config.py")

		if self.user_user_config:
			self.user_user_config = utils.pathExpand(self.user_user_config)

		if self.build_clean:
			if os.path.exists(self.dir_build):
				shutil.rmtree(self.dir_build)


	def config(self):
		"""
		  Override this method in subclass.
		"""
		sys.stderr.write("Base class method called: config() This souldn't happen.\n")


	def compile(self):
		if self.host_os == utils.LNX and hasattr(self, 'compile_linux'):
			self.compile_linux()
		elif self.host_os == utils.MAC and hasattr(self, 'compile_osx'):
			self.compile_osx()
		else:
			compileCmd = [sys.executable]
			compileCmd.append("scons/scons.py")

			if not self.build_clean:
				compileCmd.append("--implicit-deps-unchanged")
				compileCmd.append("--max-drift=1")

			if self.host_os != utils.WIN:
				compileCmd.append('CXXFLAGS="-w"')
				compileCmd.append('CCFLAGS="-w"')

			if self.use_env_msvc:
				compileCmd.append(r'env="PATH:%PATH%,INCLUDE:%INCLUDE%,LIB:%LIB%"')

			if self.vc2013:
				compileCmd.append(r'MSVS_VERSION=12.0')

			cleanCmd = [sys.executable]
			cleanCmd.append("scons/scons.py")
			cleanCmd.append("clean")

			if not self.mode_test:
				os.chdir(self.dir_blender)

				if self.build_clean:
					sys.stdout.write("Calling: %s\n" % (" ".join(cleanCmd)))
					subprocess.call(cleanCmd)

				sys.stdout.write("Calling: %s\n" % (" ".join(compileCmd)))
				res = subprocess.call(compileCmd)
				if not res == 0:
					sys.stderr.write("There was an error during the compilation!\n")
					sys.exit(1)


	def compile_post(self):
		if self.host_os == utils.WIN:
			runtimeDir = utils.path_join(self.dir_source, "vb25-patch", "non-gpl", self.build_arch)
			files = []
			if self.vc2013:
				files.extend([
					"msvcp120.dll",
					"msvcr120.dll",
					"vcomp120.dll",
				])
			else:
				files.append("vcomp90.dll")
			for f in files:
				shutil.copy(utils.path_join(runtimeDir, f), self.dir_install_path)


	def exporter(self):
		"""
		  Add script and modules
		"""
		scriptsPath = utils.path_join(self.dir_install, self.dir_install_name, self.version, "scripts")
		if self.host_os == utils.MAC:
			scriptsPath = utils.path_join(self.dir_install, self.dir_install_name, "blender.app", "Contents", "Resources", self.version, "scripts")

		addonsPath  = utils.path_join(scriptsPath, "addons")
		startupPath = utils.path_join(scriptsPath, "startup")

		clonePath = addonsPath if self.vb30 else startupPath

		sys.stdout.write("Adding exporter...\n")
		sys.stdout.write("  in: %s\n" % clonePath)

		if not self.mode_test:
			if not os.path.exists(clonePath):
				sys.stderr.write("Something went wrong! Can't add Python modules and exporter!\n")
				sys.exit(3)

			if self.vb30:
				os.chdir(clonePath)
				exporterPath = utils.path_join(clonePath, "vb30")
				if os.path.exists(exporterPath):
					utils.remove_directory(exporterPath)
				os.system("git clone --recursive https://github.com/bdancer/vb30.git")

			else:
				os.chdir(clonePath)
				exporterPath = utils.path_join(clonePath, "vb25")
				if os.path.exists(exporterPath):
					utils.remove_directory(exporterPath)

				os.system("git clone --recursive https://github.com/bdancer/vb25.git")

			if self.use_exp_branch not in {'master'}:
				os.chdir(exporterPath)
				os.system("git remote update")
				os.system("git checkout -b {branch} origin/{branch}".format(branch=self.use_exp_branch))

			os.chdir(exporterPath)
			os.system("git submodule update --init --recursive")
			os.system("git submodule foreach git checkout master")
			os.system("git submodule foreach git pull --rebase origin master")


	def package(self):
		"""
		  Override this method in subclass.
		"""
		sys.stderr.write("Base class method called: package() This souldn't happen.\n")


	def build(self):
		self.init_paths()
		self.post_init()

		self.update_sources()
		self.update()

		self.info()

		self.patch()

		if not self.export_only:
			self.config()
			self.compile()
			self.compile_post()

			if not self.mode_developer:
				self.exporter()

			self.docs()

			if self.generate_package:
				if self.mode_developer:
					sys.stdout.write("Package generation is disabled in 'Developer' mode.\n")
				else:
					if self.build_release:
						releaeSubdir, releasePackage = self.package()
						if self.build_upload != 'off':
							self.upload(releaeSubdir, releasePackage)
					else:
						sys.stdout.write("Package generation is disabled in non-release mode.\n")


	def upload(self, subdir, filepath):
		if self.build_upload == 'http':
			import requests

			from ConfigParser import RawConfigParser

			config = RawConfigParser()
			config.read(os.path.expanduser("~/.passwd"))

			data = {
				"password" : config.get('cgdo.ru', 'upload_password'),
				"subdir"   : subdir,
			}

			files = {
				"file" : open(filepath, "rb"),
			}

			proxies = {}
			if self.use_proxy:
				proxies = {
					"http"  : self.use_proxy,
					"https" : self.use_proxy,
				}

			sys.stdout.write("Uploading package '%s' to '%s'...\n" % (filepath, subdir))
			requests.post("http://cgdo.ru/upload", files=files, data=data, proxies=proxies)

		elif self.build_upload == 'ftp':
			from ConfigParser import RawConfigParser

			config = RawConfigParser()
			config.read(os.path.expanduser("~/.passwd"))

			now = datetime.datetime.now()
			subdir = now.strftime("%Y%m%d")

			cmd = None

			if sys.platform == 'win32':
				ftpScriptFilepath = os.path.join(tempfile.gettempdir(), "blender_for_vray_upload.txt")

				with open(ftpScriptFilepath, 'w') as f:
					f.write('option batch abort\n')
					f.write('option confirm off\n')
					f.write('open ftp://%s:%s@%s -rawsettings ProxyMethod=%s ProxyHost=%s ProxyPort=%s\n' % (
						config.get('nightlies.ftp', 'user'),
						config.get('nightlies.ftp', 'pass'),
						config.get('nightlies.ftp', 'host'),
						config.get('nightlies.ftp', 'proxy_type'),
						config.get('nightlies.ftp', 'proxy_host'),
						config.get('nightlies.ftp', 'proxy_port'),
					))
					f.write('option transfer binary\n')
					f.write('put %s /%s/\n' % (filepath, subdir))
					f.write('exit\n')
					f.write('\n')

				cmd = ['winscp']
				cmd.append('/passive')
				cmd.append('/script="%s"' % ftpScriptFilepath)

				if not self.mode_test:
					os.system(' '.join(cmd))

			else:
				cmd = ['curl']
				cmd.append('--no-epsv')
				if self.use_proxy:
					cmd.append('--proxy')
					cmd.append(self.use_proxy)
				cmd.append('--user')
				cmd.append('%s:%s' % (
					config.get('nightlies.ftp', 'user'),
					config.get('nightlies.ftp', 'pass'),
				))
				cmd.append('--upload-file')
				cmd.append(filepath)
				cmd.append('ftp://%s/%s/' % (
					config.get('nightlies.ftp', 'host'),
					subdir,
				))

				if not self.mode_test:
					subprocess.call(cmd)

			if self.mode_test:
				print(' '.join(cmd))