Пример #1
2
def cmd_call_when_spam(bot, update, args):
    """Command /call_when_spam message handler.
    Allows enable/disable as commands. If no valid arguments are
    provided it returns to the default value"""

    chat_id = update.message.chat_id
    chat_config = storage.get_chat_config(chat_id)
    user_id = update.message.from_user.id
    chat_type = update.message.chat.type
    lang = chat_config.language
    if chat_config.user_is_admin(bot, user_id):
        new_value = (args[0].upper() if (args is not None) and len(args) > 0
                     else conf.INIT_CALL_ADMINS_WHEN_SPAM)
        if new_value in ["ENABLE", "DISABLE"]:
            chat_config.call_admins_when_spam_detected = new_value == "ENABLE"
            if chat_config.call_admins_when_spam_detected:
                bot_msg = msg(lang, "CALL_WHEN_SPAM_ENABLE")
            else:
                bot_msg = msg(lang, "CALL_WHEN_SPAM_DISABLE")
        else:
            new_value = conf.INIT_CALL_ADMINS_WHEN_SPAM
            bot_msg = msg(lang, "CALL_WHEN_SPAM_NOT_ARG")
        chat_config.save()
    else:
        bot_msg = msg(lang, "CMD_NOT_ALLOW")
    send_bot_msg(chat_type, bot, chat_id, bot_msg, update)
Пример #2
1
def run_con(lower_bounds, upper_bounds, in_grid, true_val, true_alt=None):
    "Conditionally evaluate raster range within bounds."""

    if config.debug:
        utils.msg("run_con: lb: {} ub: {} grid: {}  val: {}, alt: {}".format(
            lower_bounds, upper_bounds, in_grid, true_val, true_alt))

    out_grid = None

    # if our initial desired output value isn't set, use the backup
    if true_val is None:
        true_val = true_alt
    # calculate our output grid
    if lower_bounds is not None:
        if upper_bounds is not None:
            out_grid_a = Con(
                in_grid, true_val, 0, "VALUE < {}".format(upper_bounds))
            out_grid = Con(
                in_grid, out_grid_a, 0, "VALUE > {}".format(lower_bounds))
        else:
            out_grid = Con(
                in_grid, true_val, 0, "VALUE >= {}".format(lower_bounds))
    elif upper_bounds is not None:
        out_grid = Con(
            in_grid, true_val, 0, "VALUE <= {}".format(upper_bounds))

    if type(out_grid).__name__ == 'NoneType' and \
       type(true_val) == arcpy.sa.Raster:
        out_grid = true_val

    return out_grid
Пример #3
1
def try_to_add_a_bot_event(bot, message, join_user, chat_id):
    """Check if the join user is a bot and has been added by
    someone with authorization. Delete the bot if not.

    Returns true if the bot can be registered."""
    chat_config = storage.get_chat_config(chat_id)
    if not chat_config.enabled:
        return

    to_register_user = True

    # who is trying to register
    from_user = message.from_user
    msg_from_user_id = from_user.id

    # to who is registering
    join_user_id = join_user.id
    join_user_alias = join_user.name

    lang = chat_config.language

    try:
        user = storage.get_user(user_id=msg_from_user_id, chat_id=chat_id)
    except UserDoesNotExists:
        storage.register_new_user(
            chat_id=chat_id,
            user_id=msg_from_user_id,
            user_name=from_user.name,
            first_name=from_user.first_name,
            last_name=from_user.last_name,
            join_date=datetime.datetime(1971, 1, 1),
            allow_user=False,
        )

    # If the user that has been added the Bot is not an Admin
    if not user.is_admin(bot):
        # If not allow users to add Bots
        if not chat_config.allow_users_to_add_bots:
            # Kick the Added Bot and notify
            log.info("An user has added a Bot.\n  (Chat) - ({}).".format(chat_id))
            try:
                kick_user_from_chat(
                    bot, chat_id, join_user_id, from_user.name, "Can not add a bot"
                )
            except Exception as e:
                log.error("Exception when kicking a Bot - {}".format(str(e)))
                if str(e) == "Not enough rights to restrict/unrestrict chat member":
                    bot_message = msg(lang, "USER_CANT_ADD_BOT_CANT_KICK").format(
                        message.from_user.name, join_user.name
                    )

            if chat_config.call_admins_when_spam_detected:
                admins = chat_config.get_admins_usernames_in_string(bot)
                if admins:
                    bot_message = "{}{}".format(
                        bot_message, msg(lang, "CALLING_ADMINS").format(admins)
                    )
            bot.send_message(chat_id, bot_message)
            to_register_user = False
    return to_register_user
Пример #4
0
    def display(self, shellcodeId):
        if shellcodeId is None:
            return None

        try:
            msg("Connecting to shell-storm.org...")
            s = six.moves.http_client.HTTPConnection("shell-storm.org")
        except:
            error_msg("Cannot connect to shell-storm.org")
            return None

        try:
            s.request(
                "GET",
                "/shellcode/files/shellcode-" + str(shellcodeId) + ".php")
            res = s.getresponse()
            data = res.read().decode('utf-8').split("<pre>")[1].split(
                "<body>")[0]
        except:
            error_msg("Failed to download shellcode from shell-storm.org")
            return None

        data = data.replace("&quot;", "\"")
        data = data.replace("&amp;", "&")
        data = data.replace("&lt;", "<")
        data = data.replace("&gt;", ">")
        return data
Пример #5
0
def console(results):
    """
    Just prints results into your neat terminal window.
    """
    results = sorted(
        results,
        key=lambda res: (res.exception is None, res.location)
    )

    for idx, result in enumerate(results):
        if result.exception:
            title = msg(result.location, TITLE_COLOR, attrs=["bold"])
            title += msg(
                u" ({}) ".format(text_type(result.exception)),
                EXCEPTION_COLOR, attrs=["bold"]
            )
            title += LOCATION_DELIMETER * (STDOUT_WIDTH - len(title))
            print_(title)
            continue

        if len(results) > 1:
            title = msg(result.location + u" ", TITLE_COLOR)
            title += LOCATION_DELIMETER * (STDOUT_WIDTH - len(title))
            print_(title)

        for track_idx, (title, length) in enumerate(result.data, start=1):
            message = str(track_idx)
            message += CSV_DELIMIETER
            message += msg(title.replace("|", r"\|"), attrs=["bold"])
            message += CSV_DELIMIETER
            message += length
            print_(message)

        if idx < len(results) - 1:
            print_("")
Пример #6
0
    def parse_repos(self):
        """Parse repos information from file "repos.txt"
        """
        msg("Parsing repos.txt")
        lines = file('/'.join((self.datadir, "repos.txt"))).read().split('\n')
        pairs = [line.replace(':',',').split(',') for line in lines if line]
        pairs = [tuple([int(pair[0]),
                        int(pair[3]) if pair[3:4] else 0,
                        pair[1],
                        pair[2]
                    ])
                 for pair in pairs]
        for repos, parent, name, creation in pairs:
            if parent > 0:
                self.forks_of_r[parent].append(repos)
                self.parent_of_r[repos] = parent
            author, name = name.split('/')
            words = [int(x) for x in creation.split('-')]
            creation = date(words[0],words[1],words[2]).toordinal()
            self.r_info[repos] = (author, name, creation)
            self.u_authoring[author].append(repos)
            self.r_name[name].append(repos)

        for repos_gen1, repos_gen2 in self.parent_of_r.items():
            if repos_gen2 in self.parent_of_r:
                repos_gen3 = self.parent_of_r[repos_gen2]
                self.gparent_of_r[repos_gen1] = repos_gen3
Пример #7
0
    def search(self, keyword):
        if keyword is None:
            return None
        try:
            msg("Connecting to shell-storm.org...")
            s = httplib.HTTPConnection("shell-storm.org")
            s.request("GET", "/api/?s=" + str(keyword))
            res = s.getresponse()
            data_l = decode(res.read()).split('\n')
        except:
            error_msg("Cannot connect to shell-storm.org")
            return None

        data_dl = []
        for data in data_l:
            try:
                desc = data.split("::::")
                dico = {
                    'ScAuthor': desc[0],
                    'ScArch': desc[1],
                    'ScTitle': desc[2],
                    'ScId': desc[3],
                    'ScUrl': desc[4]
                }
                data_dl.append(dico)
            except:
                pass

        return data_dl
Пример #8
0
def select_by(layer, selection, expression):
    try:
        utils.msg("Performing selection {} with query {}".format(selection, expression))
        arcpy.SelectLayerByAttribute_management(layer, selection, expression)
    except Exception as e:
        utils.msg("Unable to select by attributes", mtype='error', exception=e)
        sys.exit()
Пример #9
0
    def parse_lang(self):
        """Parse the repos langs information from file "lang.txt"
        """
        msg("parsing lang.txt")
        lines = file('/'.join((self.datadir,"lang.txt"))).read().split('\n')

        pairs = [line.split(":") for line in lines if line]
        pairs = [(int(pair[0]),
                  [tuple(x.split(";")) for x in pair[1].split(",")])
                 for pair in pairs]
        pairs = [(x, tuple([(int(z[1]), z[0].lower()) for z in y]))
                 for (x, y) in pairs]


        all_langs = defaultdict(bool)
        for repos, langs in pairs:
            for kloc, lang in langs:
                all_langs[lang]=True
        all_langs = sorted(all_langs.keys())
        
        msg("building lang_by_r and r_langs")
        for repos, langs in pairs:
            for kloc, lang in langs:
                lnloc = int(log(kloc + 1, 10))
                self.lang_by_r[lang].append((lnloc, repos))
                self.r_langs[repos].append((lang,lnloc))
        for lang in self.lang_by_r.keys():
            self.lang_by_r[lang].sort(key=lambda x:x[1])
Пример #10
0
    def summary(self, unabridged=False):
        props= ("watching_r "
                "u_watching "
                "r_info "
                "r_name "
                "r_langs "
                "forks_of_r "
                "parent_of_r "
                "gparent_of_r "
                "lang_by_r "
                "u_authoring "
                ).split()
        for prop in props:
            print(">> %s" % prop)
            if unabridged:
                pprint(dict(getattr(self, prop)).items())
            else:
                pprint(dict(getattr(self, prop)).items()[:5])
            print("")

        msg(">> test_u")
        if unabridged:
            pprint(self.test_u)
        else:
            pprint(self.test_u[:5])
Пример #11
0
    def search(self, keyword):
        if keyword is None:
            return None
        try:
            msg("Connecting to shell-storm.org...")
            s = http.client.HTTPConnection("shell-storm.org")
            s.request("GET", "/api/?s="+str(keyword))
            res = s.getresponse()
            data_l = res.read().split('\n')
        except:
            error_msg("Cannot connect to shell-storm.org")
            return None

        data_dl = []
        for data in data_l:
            try:
                desc = data.split("::::")
                dico = {
                         'ScAuthor': desc[0],
                         'ScArch': desc[1],
                         'ScTitle': desc[2],
                         'ScId': desc[3],
                         'ScUrl': desc[4]
                       }
                data_dl.append(dico)
            except:
                pass

        return data_dl
Пример #12
0
    def shellcode(self, sctype, port=None, host=None):
        if not self.shellcodes or sctype not in self.shellcodes:
            return None

        if port is None:
            port = 16706
        if host is None:
            host = '127.127.127.127'

        shellcode = self.shellcodes[sctype]
        try:
            port = struct.pack(">H", port)
            addr = socket.inet_aton(host)
            shellcode = shellcode.replace(b"\x66\x68\x41\x42",
                                          b"\x66\x68" + port)
            shellcode = shellcode.replace(b"\x68\xff\x02\x41\x42",
                                          b"\x68\xff\x02" + port)
            shellcode = shellcode.replace(b"\x68\x7f\x7f\x7f\x7f",
                                          b"\x68" + addr)
            return shellcode
        except Exception as e:
            if config.Option.get("debug") == "on":
                msg("Exception: %s" % e)
                traceback.print_exc()
            return None
Пример #13
0
 def item_model(self):
     """construct item similarities
     """
     msg("building r_matrix")
     repos=set(self.data.watching_r.keys())
     for repo in repos:
         r_similar[repo] = self.related_items(repo)
     r_similar.sort(reverse=True)
Пример #14
0
 def user_model(self):
     """construct user similarities
     """
     msg("building u_matrix")
     users = set(self.data.u_watching.keys())
     for user in users:
         u_similar[user] = self.related_users(user)
     u_similar.sort(reverse=True)
Пример #15
0
def select_by(layer, selection, expression):
    try:
        utils.msg("Performing selection {} with query {}".format(
            selection, expression))
        arcpy.SelectLayerByAttribute_management(layer, selection, expression)
    except Exception as e:
        utils.msg("Unable to select by attributes", mtype='error', exception=e)
        sys.exit()
Пример #16
0
    def getResource(self, resource, msgBar=None):
        self.forgetResponse()
        self.host = re.sub("gopher://", "", resource.getHost(), 1)
        self.port = resource.getPort()
        self.lastType = resource.getType()

        try:
            if resource.getDataBlock():
                request = resource.getLocator() + "\t+\t1\r\n"
                request = request + resource.getDataBlock()

                self.response = self.requestToData(resource, request, msgBar,
                                                   1)
            elif resource.isGopherPlusResource() and resource.isAskType():
                info = resource.getInfo(shouldFetch=1)
                af = AskForm.AskForm(info.getBlock("ASK"))

                # Copy host/port/locator information into af
                af.dup(resource)
                return af
            elif resource.isGopherPlusResource():
                request = resource.getLocator() + "\t+\r\n"
                self.response = self.requestToData(resource, request, msgBar,
                                                   1)
            else:
                request = resource.getLocator() + "\r\n"
                self.response = self.requestToData(resource, request, msgBar,
                                                   None)
        except Connection.ConnectionException as estr:
            error_resp = GopherResponse.GopherResponse()
            errstr = "Cannot fetch\n%s:\n%s" % (resource.toURL(), estr)

            error_resp.setError(errstr)
            return error_resp

        utils.msg(msgBar, "Examining response...")

        resp = GopherResponse.GopherResponse()
        resp.setType(resource.getTypeCode())

        if resource.getLen() != -2:
            self.response = self.stripTail(self.response)

        try:
            # The parser picks up directory entries and sets the internal
            # data of the object as needed.
            resp.parseResponse(self.response)

            # if we get this far, then it's a directory entry, so set the
            # data to nothing.
            resp.setData(None)
        except Exception as erstr:
            # print "Caught exception while parsing response: \"%s\"" % erstr
            if self.verbose:
                print("OK, it's data.")
            resp.setData(self.response)

        return resp
Пример #17
0
def tag(force='no', push='no'):
    """
    Tag a new release.

    Normally, if a Git tag exists matching the current version, and no Git
    commits appear after that tag, we abort assuming the user is making a
    mistake or forgot to commit their work.

    To override this -- i.e. to re-tag and re-upload -- specify ``force=yes``.
    We assume you know what you're doing if you use this.

    By default we do not push the tag remotely; specify ``push=yes`` to force a
    ``git push origin <tag>``.
    """
    force = force.lower() in ['y', 'yes']
    with settings(warn_only=True):
        changed = []
        # Does the current in-code version exist as a Git tag already?
        # If so, this means we haven't updated the in-code version specifier
        # yet, and need to do so.
        if current_version_is_tagged():
            # That is, if any work has been done since. Sanity check!
            if not commits_since_last_tag() and not force:
                abort("No work done since last tag!")
            # Open editor, update version
            version_file = "fabric/version.py"
            changed.append(update_code(version_file, force))
        # If the tag doesn't exist, the user has already updated version info
        # and we can just move on.
        else:
            print("Version has already been updated, no need to edit...")
        # Similar process but for the changelog.
        changelog = "docs/changelog.rst"
        if not current_version_is_changelogged(changelog):
            changed.append(update_code(changelog, force))
        else:
            print("Changelog already updated, no need to edit...")
        # Commit any changes
        if changed:
            with msg("Committing updated version and/or changelog"):
                reload(fabric.version)
                local("git add %s" % " ".join(changed))
                local("git commit -m \"Cut %s\"" % _version('verbose'))
                local("git push")

        # At this point, we've incremented the in-code version and just need to
        # tag it in Git.
        f = 'f' if force else ''
        with msg("Tagging"):
            local("git tag -%sam \"Fabric %s\" %s" % (
                f,
                _version('normal'),
                _version('short')
            ))
        # And push to the central server, if we were told to
        if push.lower() in ['y', 'yes']:
            with msg("Pushing"):
                local("git push origin %s" % _version('short'))
Пример #18
0
def tag(force='no', push='no'):
    """
    Tag a new release.

    Normally, if a Git tag exists matching the current version, and no Git
    commits appear after that tag, we abort assuming the user is making a
    mistake or forgot to commit their work.

    To override this -- i.e. to re-tag and re-upload -- specify ``force=yes``.
    We assume you know what you're doing if you use this.

    By default we do not push the tag remotely; specify ``push=yes`` to force a
    ``git push origin <tag>``.
    """
    force = force.lower() in ['y', 'yes']
    with settings(warn_only=True):
        changed = []
        # Does the current in-code version exist as a Git tag already?
        # If so, this means we haven't updated the in-code version specifier
        # yet, and need to do so.
        if current_version_is_tagged():
            # That is, if any work has been done since. Sanity check!
            if not commits_since_last_tag() and not force:
                abort("No work done since last tag!")
            # Open editor, update version
            version_file = "fabric/version.py"
            changed.append(update_code(version_file, force))
        # If the tag doesn't exist, the user has already updated version info
        # and we can just move on.
        else:
            print("Version has already been updated, no need to edit...")
        # Similar process but for the changelog.
        changelog = "docs/changelog.rst"
        if not current_version_is_changelogged(changelog):
            changed.append(update_code(changelog, force))
        else:
            print("Changelog already updated, no need to edit...")
        # Commit any changes
        if changed:
            with msg("Committing updated version and/or changelog"):
                reload(fabric.version)
                local("git add %s" % " ".join(changed))
                local("git commit -m \"Cut %s\"" % _version('verbose'))
                local("git push")

        # At this point, we've incremented the in-code version and just need to
        # tag it in Git.
        f = 'f' if force else ''
        with msg("Tagging"):
            local("git tag -%sam \"Fabric %s\" %s" % (
                f,
                _version('normal'),
                _version('short')
            ))
        # And push to the central server, if we were told to
        if push.lower() in ['y', 'yes']:
            with msg("Pushing"):
                local("git push origin %s" % _version('short'))
Пример #19
0
 def parse_watching(self):
     """Parse watching data from file "data.txt"
     """
     msg("Parsing data.txt")
     lines = file('/'.join((self.datadir,"data.txt"))).read().split('\n')
     pairs = [[int(x) for x in line.split(':')] for line in lines if line]
     for user, repos in pairs:
         self.watching_r[repos].append(user)
         self.u_watching[user].append(repos)
Пример #20
0
 def display_box(self, channel, user=None, ignore_whisper=False):
     if self.content is None or self.box_open:
         return
     if user is not None:
         utils.msg(user, self.content)
         self.clear_display()
         return
     utils.echo(channel, self.content, ignore_whisper=ignore_whisper)
     self.clear_display()
Пример #21
0
def main(bathy=None, out_raster=None):
    try:
        arcpy.env.rasterStatistics = "STATISTICS"
        # Calculate the slope of the bathymetric raster
        utils.msg("Calculating the slope...")
        out_slope = Slope(bathy, "DEGREE", 1)
        out_raster = utils.validate_path(out_raster)
        out_slope.save(out_raster)
    except Exception as e:
        utils.msg(e, mtype='error')
Пример #22
0
async def on_ready():
    msg('Login Success!')
    msg('Username: '******'Connected to Servers:')
    for server in bot.servers:
        msg(' - ' + server.name)
    msg('Starting async loops')
    loop = asyncio.get_event_loop()
    loop.run_until_complete(await tasks())
    loop.close()
Пример #23
0
def cmd_call_admins(bot, update):
    """Command /call_admins message handler"""
    chat_id = update.message.chat_id
    chat_config = storage.get_chat_config(chat_id)
    admins = chat_config.get_admins_usernames_in_string(bot)
    if admins:
        bot_msg = msg(chat_config.language, "CALLING_ADMINS").format(admins)
    else:
        bot_msg = msg(chat_config.language, "CALLING_ADMINS_NO_ADMINS")
    bot.send_message(chat_id, bot_msg)
Пример #24
0
def main(out_workspace, input_bathymetry, broad_bpi_inner_radius, 
        broad_bpi_outer_radius, fine_bpi_inner_radius, fine_bpi_outer_radius, 
        classification_dict, output_zones):

    # Load required toolboxes
    local_path = os.path.dirname(__file__)
    btm_toolbox = os.path.abspath(os.path.join(local_path, '..', 'btm.pyt'))
    arcpy.ImportToolbox(btm_toolbox)

    # local variables:
    broad_bpi = os.path.join(out_workspace, "broad_bpi")
    fine_bpi = os.path.join(out_workspace, "fine_bpi")
    slope_rast = os.path.join(out_workspace, "slope")
    broad_std = os.path.join(out_workspace, "broad_std")
    fine_std = os.path.join(out_workspace, "fine_std")

    utils.workspace_exists(out_workspace)
    # set geoprocessing environments
    arcpy.env.scratchWorkspace = out_workspace
    arcpy.env.workspace = out_workspace

    # TODO: currently set to automatically overwrite, expose this as option
    arcpy.env.overwriteOutput = True

    try:
        # Process: Build Broad Scale BPI
        utils.msg("Calculating broad-scale BPI...")
        bpi.main(input_bathymetry, broad_bpi_inner_radius, \
                broad_bpi_outer_radius, broad_bpi, bpi_type='broad')

        # Process: Build Fine Scale BPI
        utils.msg("Calculating fine-scale BPI...")
        bpi.main(input_bathymetry, fine_bpi_inner_radius, \
                fine_bpi_outer_radius, fine_bpi, bpi_type='fine')

        # Process: Standardize BPIs
        utils.msg("Standardizing BPI rasters...")
        arcpy.standardizebpi_btm(broad_bpi, "0", "0", broad_std, fine_bpi, \
                "0", "0", fine_std)

        # Process: Calculate Slope
        slope.main(input_bathymetry, slope_rast)

        # Process: Zone Classification Builder
        outputs_base = arcpy.env.addOutputsToMap
        arcpy.env.addOutputsToMap = True
        utils.msg("Classifying Zones...")
        classify.main(classification_dict, broad_std, fine_std, slope_rast, \
                input_bathymetry, output_zones)
        arcpy.env.addOutputsToMap = outputs_base

    except Exception as e:
        # Print error message if an error occurs
        utils.msg(e, mtype='error')
Пример #25
0
    def infox(self, *arg):
        """
        Customized xinfo command from https://github.com/longld/peda
        Usage:
            MYNAME address
            MYNAME register [reg1 reg2]
        """

        (address, regname) = utils.normalize_argv(arg, 2)
        if address is None:
            self._missing_argument()

        text = ""
        #if not self._is_running():
        if False:
            return

        def get_reg_text(r, v):
            text = green("%s" % r.upper().ljust(3), "bold") + ": "
            chain = e.examine_mem_reference(v)
            text += utils.format_reference_chain(chain)
            text += "\n"
            return text

        (arch, bits) = e.getarch()
        if str(address).startswith("r"):
            # Register
            regs = e.getregs(" ".join(arg[1:]))
            if regname is None:
                for r in REGISTERS[bits]:
                    if r in regs:
                        text += get_reg_text(r, regs[r])
            else:
                for (r, v) in sorted(regs.items()):
                    text += get_reg_text(r, v)
            if text:
                utils.msg(text.strip())
            if regname is None or "eflags" in regname:
                self.eflags()
            return

        elif utils.to_int(address) is None:
            warning_utils.msg("not a register nor an address")
        else:
            # Address
            chain = e.examine_mem_reference(address)
            #text += '\n'
            #text += 'info: '
            text += utils.format_reference_chain(chain) # + "\n"
            vmrange = e.get_vmrange(address)
            if vmrange:
                (start, end, perm, name) = vmrange
        utils.msg(text)
        return
Пример #26
0
 def is_nonsense(string, show=trace):
     # Lower-case the string & remove non-letters before checking length.
     string = string.lower().translate(_delchars)
     if len(string) < min_length:
         raise ValueError('Too short to test')
     is_nonsense, real_score, nonsense_score = string_score(string)
     if show:
         msg('"{}": {} (real score {:.4f}, nonsense score {:.4f})'
             .format(string, 'y' if is_nonsense else 'n',
                     real_score, nonsense_score))
     return is_nonsense
Пример #27
0
def main(input_raster=None, selected_layer=None, interpolate=None, 
         mode=settings.mode):

        utils.msg("Executing ExtractRasterValuesToPoints.")   
        arcpy.CheckOutExtension("Spatial")

        # was bilinear interpolation asked for? maps to
        # 'bilinear_intepolate_values'.
        if interpolate in ('BILINEAR', True):
            bilinear = 'BILINEAR'
        else:
            bilinear = 'NONE'

        # create a value table, prefix all output rasters with 'R_'
        rasters = input_raster.split(";")
        value_table = []
        for raster in rasters:
            # default name is just the label, prepend 'R_'
            (label, input_ext) = os.path.splitext(os.path.basename(raster))
            label = "R_{0}".format(label)
            value_table.append([raster, label])
        utils.msg("generated value table: %s" % value_table)
        utils.msg("Running ExtractMultiValuesToPoints...")
        ExtractMultiValuesToPoints(selected_layer, value_table, bilinear)
        utils.msg("Values successfully extracted")  
Пример #28
0
def main(input_raster=None,
         selected_layer=None,
         interpolate=None,
         mode=settings.mode):

    utils.msg("Executing ExtractRasterValuesToPoints.")
    arcpy.CheckOutExtension("Spatial")

    # was bilinear interpolation asked for? maps to
    # 'bilinear_intepolate_values'.
    if interpolate in ('BILINEAR', True):
        bilinear = 'BILINEAR'
    else:
        bilinear = 'NONE'

    # create a value table, prefix all output rasters with 'R_'
    rasters = input_raster.split(";")
    value_table = []
    for raster in rasters:
        # default name is just the label, prepend 'R_'
        (label, input_ext) = os.path.splitext(os.path.basename(raster))
        label = "R_{0}".format(label)
        value_table.append([raster, label])
    utils.msg("generated value table: %s" % value_table)
    utils.msg("Running ExtractMultiValuesToPoints...")
    ExtractMultiValuesToPoints(selected_layer, value_table, bilinear)
    utils.msg("Values successfully extracted")
Пример #29
0
 def zsc(self,os,job,encode):
     try:
         msg('Connection to OWASP ZSC API api.z3r0d4y.com')
         params = urlencode({
                 'api_name': 'zsc', 
                 'os': os,
                 'job': job,
                 'encode': encode})
         shellcode = urlopen("http://api.z3r0d4y.com/index.py?%s\n"%(str(params))).read()
         if pyversion is 3:
             shellcode = str(shellcode,encoding='ascii')
         return '\n"'+shellcode.replace('\n','')+'"\n'
     except:
         error_msg("Error while connecting to api.z3r0d4y.com ...")
         return None
Пример #30
0
    def fill_pickle_jar(self):
        """Fill the data to pickle
        """
        jar = '/'.join((self.datadir,"pickle.jar"))
        d = {}
        
        msg("Filling pickle jar '%s'" % jar)

        for field in self.fields:
            d[field] = getattr(self, field)
        d['fields'] = self.fields
        
        jarf = open(jar, 'w')
        pickle.dump(d, jarf)
        jarf.close()
Пример #31
0
 def zsc(self,os,job,encode):
     try:
         msg('Connection to OWASP ZSC API api.z3r0d4y.com')
         params = urlencode({
                 'api_name': 'zsc',
                 'os': os,
                 'job': job,
                 'encode': encode})
         shellcode = urlopen("http://api.z3r0d4y.com/index.py?%s\n"%(str(params))).read()
         if pyversion == 3:
             shellcode = str(shellcode,encoding='ascii')
         return '\n"'+shellcode.replace('\n','')+'"\n'
     except:
         error_msg("Error while connecting to api.z3r0d4y.com ...")
         return None
Пример #32
0
def cmd_disable_user(bot, update, args):
    """Command disable_user message handler
        Removes the rights to post link messages to
        the chat to the user alias passed as args and
        considers him a possible spammer.

        Checks that the user who runs the command is admin
        and changes the destination user configuration."""

    chat_id = update.message.chat_id
    chat_type = update.message.chat.type
    user_id = update.message.from_user.id
    chat_config = storage.get_chat_config(chat_id)
    # command user
    command_user = storage.get_user(user_id=user_id, chat_id=chat_id)
    # destination_user
    destination_user_alias = _get_user_alias(args)
    lang = chat_config.language

    if command_user.is_admin(bot):
        try:
            destination_user = storage.get_user_from_alias(
                chat_id, destination_user_alias)
            destination_user.penalize()
            bot_msg = _("User {} has ben considered a potential spammer"
                        ).format(destination_user_alias)
        except UserDoesNotExists:
            bot_msg = _("User not found")
    else:
        bot_msg = msg(lang, "CMD_NOT_ALLOW")

    send_bot_msg(chat_type, bot, chat_id, bot_msg, update)
Пример #33
0
def load_geodesic_dll():
    fn = None
    # load the DLL path from the config settings.
    dll_path = settings.geodesic_dll_path
    if os.path.exists(dll_path):
        try:
            loaded_dll = ctypes.cdll.LoadLibrary(dll_path)
        except Exception as e:
            msg = "Failed to load high-speed geodesic library."
            utils.msg(msg, mtype='error', exception=e)
            return None
        fn = loaded_dll.CalculatePairwiseGeodesicDistances
        fn.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_double, ctypes.c_bool]
        fn.restype = ctypes.c_int
    else:
        msg = "Unable to locate high-speed geodesic DLL at: {}".format(dll_path)
        utils.msg(msg, mtype='error')
    return fn                
Пример #34
0
def Add_client(request):
    if request.method == 'POST':
        form = ClientForm(request.POST)
        if form.is_valid():
            form.save()
            return msg(request,"Cliente adicionado com Sucesso")
    else:
        form = ClientForm
    return render(request, 'add_client.html', {'user':request.user,'form': form})
Пример #35
0
 def grp(self, *arg):
     """
     Grep command-output
     Usage:
         MYNAME command regexp
     """
     try:
         (cmd, regex) = utils.normalize_argv(arg, 2)
         cmd = str(cmd)
         regex = str(regex)
         output = gdb.execute(cmd, to_string=True)
         regexed = re.findall(regex, output)
         for line in regexed:
             print(line)
     except Exception as e:
         utils.msg("Exception in grp(%s, %s): %s" % (repr(cmd), repr(regex), e), "red")
         traceback.print_exc()
         return False
Пример #36
0
def load_geodesic_dll():
    fn = None
    # load the DEBUG build of the DLL.
    dll_path = os.path.abspath( \
        os.path.join(os.path.abspath(os.path.dirname(__file__)), \
        '..', 'arcobjects', 'geodesic', 'Debug', 'geodesic.dll'))

    if os.path.exists(dll_path):
        try:
            loaded_dll = ctypes.cdll.LoadLibrary(dll_path)
        except Exception as e:
            msg = "Failed to load high-speed geodesic library."
            utils.msg(msg, mtype='error', exception=e)
            return None
        fn = loaded_dll.CalculatePairwiseGeodesicDistances
        fn.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_double]
        fn.restype = ctypes.c_int
    return fn
Пример #37
0
def load_geodesic_dll():
    fn = None
    # load the DEBUG build of the DLL.
    dll_path = os.path.abspath( \
        os.path.join(os.path.abspath(os.path.dirname(__file__)), \
        '..', 'arcobjects', 'geodesic', 'Debug', 'geodesic.dll'))

    if os.path.exists(dll_path):
        try:
            loaded_dll = ctypes.cdll.LoadLibrary(dll_path)
        except Exception as e:
            msg = "Failed to load high-speed geodesic library."
            utils.msg(msg, mtype='error', exception=e)
            return None
        fn = loaded_dll.CalculatePairwiseGeodesicDistances
        fn.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_double]
        fn.restype = ctypes.c_int
    return fn
Пример #38
0
 async def fetch_jars(self, version):
     """
     get release url
     """
     url = JAVA_TRON_RELEASES_URL
     if (version == 'lastest'):
         url += 'Odyssey-v' + JAVA_TRON_LASTEST_VERSION
     elif ('3.1.3' <= version <= '3.1.3'):
         url += 'Odyssey-v' + version
     """
     download
     """
     msg('download fullnode jar might take a while')
     await download(self.source_full_jar, url)
     success_msg('.jar file of Fullnode is successfully downloaded')
     msg('download solidity jar might take a while')
     await download(self.source_sol_jar, url)
     success_msg('.jar file of Soliditynode is successfully downloaded')
Пример #39
0
def Add_employee(request):
    if request.method == 'POST':
        form = EmployeeForm(request.POST)
        if form.is_valid():
            form.save()
            return msg(request,"Funcionario adicionado com Sucesso")
    else:
        form = EmployeeForm
    return render(request, 'add_employee.html', {'user':request.user,'form': form})
Пример #40
0
def Add_service(request):
    if request.method == 'POST':
        form = ServiceForm(request.POST)
        if form.is_valid():
            form.save()
            return msg(request,"Serviço adicionado com Sucesso")
    else:
        form = ServiceForm
    return render(request, 'add_service.html', {'user':request.user,'form': form})
Пример #41
0
def cmd_start(bot, update):
    """Command /start message handler"""
    chat = update.message.chat
    chat_id = update.message.chat_id
    chat_type = chat.type
    chat_config = storage.get_chat_config(chat_id)
    chat_config.enabled = True
    lang = chat_config.language

    if chat_type == "private":
        log.info("The bot started in {} private chat".format(chat_id))
        bot.send_message(chat_id, msg(lang, "START"))
    else:
        log.info("The bot started in {} group chat {}".format(
            chat_id, chat.title))
        chat_config.title = chat.title
        tlg_msg_to_selfdestruct(bot, update.message)
        tlg_send_selfdestruct_msg(bot, chat_id, msg(lang, "START"))
    chat_config.save()
Пример #42
0
def cmd_enable(bot, update):
    """Command /enable message handler.
    Enables bot spam filter actions"""

    chat_id = update.message.chat_id
    chat_config = storage.get_chat_config(chat_id)
    user_id = update.message.from_user.id
    chat_type = update.message.chat.type
    lang = chat_config.language
    if chat_config.user_is_admin(bot, user_id):
        if chat_config.enabled:
            bot_msg = msg(lang, "ALREADY_ENABLE")
        else:
            bot_msg = msg(lang, "ENABLE")
            chat_config.enabled = True
            chat_config.save()
    else:
        bot_msg = msg(lang, "CMD_NOT_ALLOW")
    send_bot_msg(chat_type, bot, chat_id, bot_msg, update)
Пример #43
0
def cmd_disable(bot, update):
    """Command /disable message handler"""

    chat_id = update.message.chat_id
    chat_config = storage.get_chat_config(chat_id)
    user_id = update.message.from_user.id
    chat_type = update.message.chat.type
    lang = chat_config.language

    if chat_config.user_is_admin(bot, user_id):
        if not chat_config.enabled:
            bot_msg = msg(lang, "ALREADY_DISABLE")
        else:
            bot_msg = msg(lang, "DISABLE")
            chat_config.enabled = False
            chat_config.save()
    else:
        bot_msg = msg(lang, "CMD_NOT_ALLOW")
    send_bot_msg(chat_type, bot, chat_id, bot_msg, update)
Пример #44
0
def get_sync_file(token, repo):
    headers = {'Authorization': 'token ' + token}
    repository = f"https://raw.githubusercontent.com/openstax/{repo}/main"

    try:
        endpoint = f"{repository}/META-INF/books.xml"
        resp = requests.get(endpoint, headers=headers)
        resp.raise_for_status()
    except requests.exceptions.HTTPError:
        try:
            endpoint = f"{repository}/archive-syncfile"
            resp = requests.get(endpoint, headers=headers)
            resp.raise_for_status()
        except requests.exceptions.HTTPError:
            msg("Error: missing '/META-INF/books.xml' or 'archive-syncfile' from {}", repo)
            sys.exit(1)

    sync_file = resp.content.decode()
    return sync_file
Пример #45
0
def main(input_fc=None,
         selection_1=None,
         expression_1=None,
         selection_2=None,
         expression_2=None,
         selection_3=None,
         expression_3=None,
         output_fc=None,
         mode='toolbox'):

    # set mode based on how script is called.
    settings.mode = mode
    add_output = arcpy.env.addOutputsToMap
    arcpy.env.addOutputsToMap = True

    arcpy.env.overwriteOutput = settings.overwrite

    # get the spatial reference of input
    desc = arcpy.Describe(input_fc)
    sr = desc.spatialReference

    # Perform manipulations on an in-memory copy of the data
    utils.msg("Copying features into memory")
    temp_fc = 'in_memory/select_by_attributes'
    temp_layer = "select_by_attributes_lyr"
    arcpy.CopyFeatures_management(input_fc, temp_fc)
    arcpy.MakeFeatureLayer_management(temp_fc, temp_layer)

    # initial selection
    select_by(temp_layer, selection_1, expression_1)

    if selection_2 and expression_2:
        select_by(temp_layer, selection_2, expression_2)

    if selection_3 and expression_3:
        select_by(temp_layer, selection_3, expression_3)

    utils.msg("Writing results to {}".format(output_fc))
    arcpy.CopyFeatures_management(temp_layer, output_fc)

    # restore add outputs state
    arcpy.env.addOutputsToMap = add_output
Пример #46
0
def main(bathy=None, inner_radius=None, outer_radius=None,
    out_raster=None, bpi_type='broad', mode='toolbox'):

    arcpy.env.rasterStatistics = "STATISTICS"
    try:
        # Create the broad-scale Bathymetric Position Index (BPI) raster
        msg = "Generating the {bpi_type}-scale ".format(bpi_type=bpi_type) + \
                "Bathymetric Position Index (BPI) raster..."
        utils.msg(msg)
        utils.msg("Calculating neighborhood...")
        neighborhood = NbrAnnulus(inner_radius, outer_radius, "CELL")
        utils.msg("Calculating FocalStatistics for {}...".format(bathy))
        out_focal_statistics = FocalStatistics(bathy, neighborhood, "MEAN")
        outRaster = Int(Plus(Minus(bathy, out_focal_statistics), 0.5))

        out_raster = utils.validate_path(out_raster)
        outRaster.save(out_raster)
        utils.msg("Saved output as {}".format(out_raster))
    except Exception as e:
        utils.msg(e, mtype='error')
Пример #47
0
    def saveOptions(self, *args):
        """Saves the user options to a file in their home directory.  Who knows
        what happens on windows boxen."""

        filename = self.getPrefsDirectory() + os.sep + "forg-associations"

        try:
            Options.program_options.associations.writeToFile(filename)
        except IOError as str:
            print("***Error saving associations to disk: %s" % str)

        ofilename = self.getPrefsDirectory() + os.sep + "forgrc"

        try:
            Options.program_options.save()
        except IOError as str:
            self.genericError("Couldn't write options to file:\n%s" % str)

        utils.msg(self.messageBar, "Finished saving options.")
        return None
Пример #48
0
def cmd_allow_user(bot, update, args):
    """Command /allow_user message handler
        Givies the rights to post messages for
        the chat to the user alias passed as args.

        Checks that the user who runs the command is admin
        and changes the destination user configuration."""

    chat_id = update.message.chat_id
    chat_type = update.message.chat.type
    user_id = update.message.from_user.id
    chat_config = storage.get_chat_config(chat_id)
    lang = chat_config.language
    # command user
    try:
        command_user = storage.get_user(user_id=user_id, chat_id=chat_id)
    except UserDoesNotExists:
        bot_msg = msg(lang, "CMD_ALLOW_USR_NOT_FOUND")
        send_bot_msg(chat_type, bot, chat_id, bot_msg, update)
        return
    # destination_user
    destination_user_alias = _get_user_alias(args)

    if command_user.is_admin(bot):
        try:
            destination_user = storage.get_user_from_alias(
                chat_id, destination_user_alias)
            if not destination_user.verified:
                destination_user.verified = True
                destination_user.save()
                bot_msg = msg(
                    lang, "CMD_ALLOW_USR_OK").format(destination_user_alias)
            else:
                bot_msg = msg(lang, "CMD_ALLOW_USR_ALREADY_ALLOWED").format(
                    destination_user_alias)
        except UserDoesNotExists:
            bot_msg = msg(lang, "CMD_ALLOW_USR_NOT_FOUND")
    else:
        bot_msg = msg(lang, "CMD_NOT_ALLOW")

    send_bot_msg(chat_type, bot, chat_id, bot_msg, update)
Пример #49
0
    def shellcode(self, sctype, port=None, host=None):
        if not self.shellcodes or sctype not in self.shellcodes:
            return None

        if port is None:
            port=16706
        if host is None:
            host='127.127.127.127'

        shellcode = self.shellcodes[sctype]
        try:
            port = struct.pack(">H", port)
            addr = socket.inet_aton(host)
            shellcode = shellcode.replace(b"\x66\x68\x41\x42", b"\x66\x68" + port)
            shellcode = shellcode.replace(b"\x68\xff\x02\x41\x42", b"\x68\xff\x02" + port)
            shellcode = shellcode.replace(b"\x68\x7f\x7f\x7f\x7f", b"\x68" + addr)
            return shellcode
        except Exception as e:
            if config.Option.get("debug") == "on":
                msg("Exception: %s" %e)
                traceback.print_exc()
            return None
Пример #50
0
def main(bathy=None, out_sin_raster=None, out_cos_raster=None):
    try:
        arcpy.env.rasterStatistics = "STATISTICS"
        # Calculate the aspect of the bathymetric raster. "Aspect is expressed in 
        # positive degrees from 0 to 359.9, measured clockwise from north."
        utils.msg("Calculating aspect...")
        aspect = Aspect(bathy)

        # both the sin and cos functions here expect radians, not degrees.
        # convert our Aspect raster into radians, check that the values are in range.
        aspect_rad = aspect * (math.pi / 180)

        # because this statistic is circular (0 and 359.9 are immediately adjacent),
        # we need to transform this into a form which preserves distances between items.
        # trig is the simplest mechanism.
        aspect_sin = Sin(aspect_rad)
        aspect_cos = Cos(aspect_rad)

        out_sin_raster = utils.validate_path(out_sin_raster)
        out_cos_raster = utils.validate_path(out_cos_raster)
        aspect_sin.save(out_sin_raster)
        aspect_cos.save(out_cos_raster)
    except Exception as e:
        utils.msg(e, mtype='error')
Пример #51
0
def main(input_fc=None, selection_1=None, expression_1=None, selection_2=None,
        expression_2=None, selection_3=None, expression_3=None, output_fc=None,
        mode='toolbox'):

    # set mode based on how script is called.
    settings.mode = mode
    add_output = arcpy.env.addOutputsToMap
    arcpy.env.addOutputsToMap = True

    arcpy.env.overwriteOutput = settings.overwrite

    # get the spatial reference of input
    desc = arcpy.Describe(input_fc)
    sr = desc.spatialReference

    # Perform manipulations on an in-memory copy of the data
    utils.msg("Copying features into memory")
    temp_fc = 'in_memory/select_by_attributes'
    temp_layer = "select_by_attributes_lyr"
    arcpy.CopyFeatures_management(input_fc, temp_fc)
    arcpy.MakeFeatureLayer_management(temp_fc, temp_layer)

    # initial selection
    select_by(temp_layer, selection_1, expression_1)

    if selection_2 and expression_2:
        select_by(temp_layer, selection_2, expression_2)

    if selection_3 and expression_3:
        select_by(temp_layer, selection_3, expression_3)

    utils.msg("Writing results to {}".format(output_fc))
    arcpy.CopyFeatures_management(temp_layer, output_fc)

    # restore add outputs state
    arcpy.env.addOutputsToMap = add_output
Пример #52
0
    def display(self, shellcodeId):
        if shellcodeId is None:
            return None

        try:
            msg("Connecting to shell-storm.org...")
            s = six.moves.http_client.HTTPConnection("shell-storm.org")
        except:
            error_msg("Cannot connect to shell-storm.org")
            return None

        try:
            s.request("GET", "/shellcode/files/shellcode-"+str(shellcodeId)+".php")
            res = s.getresponse()
            data = res.read().split("<pre>")[1].split("<body>")[0]
        except:
            error_msg("Failed to download shellcode from shell-storm.org")
            return None

        data = data.replace("&quot;", "\"")
        data = data.replace("&amp;", "&")
        data = data.replace("&lt;", "<")
        data = data.replace("&gt;", ">")
        return data
def update_vendors():
    """Update vendor modules."""
    for module in get_vendor_modules():
        submodules = getattr(module, "submodules")
        if submodules is not None:
            vendor_root = os.path.dirname(module.__file__)
            for name, params in submodules.iteritems():
                vendor_path = os.path.join(vendor_root, name)
                if os.path.isdir(vendor_path):
                    tag = params.get("tag", "")
                    with lcd(vendor_path), msg("Updating vendor submodule %s" % name):
                        local("git checkout %s" % tag)
                        sources = params.get("sources", [])
                        destination = params.get("destination")
                        if destination is not None:
                            destination = os.path.normpath(os.path.join(vendor_root, destination))
                            with settings(warn_only=True):
                                local("mkdir -p %s" % destination)
                            for source in sources:
                                local("cp -R %s %s" % (source, destination))
Пример #54
0
def prompt(text, validator=always_true, save_as=None, options=(), parse=False):
    """
    Prompt the user with a question, possibly multiple choice. 
    Read the answer, validate it and store it in the session store.
    """
    save_as = save_as or text
    while True: # allows coroutines to be re-run
        # get menu system & session store
        ms, session = yield
        question = msg(text, options)
        if parse:
            question = question % session
        # return question & boolean indicating end of session
        # FIXME: boolean is ugly
        yield question, False
        # get answer back
        answer = yield
        # validate
        validated_answer = validator(answer, options)
        # store question & validated answer
        session[save_as] = validated_answer
        # return validated answer
        yield validated_answer
Пример #55
0
def main(input_fc=None, output_csv=None, mode=settings.mode):

    if not arcpy.Exists(input_fc):
        utils.msg("Input, {} doesn't exist.".format(input_fc))
        sys.exit()

    try:
        with open(output_csv, 'wb') as output_file:
            writer = csv.writer(output_file, dialect='excel', \
                quotechar='"', quoting=csv.QUOTE_ALL)

            # fields generated by ArcGIS & lacking a direct use in Shepherd
            ignored_fields = ['OID', 'OBJECTID', 'Shape']
            # header row should contain our fields declaration
            fields = [field.name 
                    for field in 
                    arcpy.ListFields(input_fc)
                    if field.name not in ignored_fields]
            writer.writerow(fields)

            our_date = "Date_formatted"
            date_exists = False
            # If we don't have a formatted date field, don't try to manipulate it.
            if our_date in fields:
                date_pos = fields.index(our_date)
                date_exists = True
            with arcpy.da.SearchCursor(input_fc, fields) as cursor:
                for row in cursor:
                    formatted_row = list(row[:])
                    if date_exists:
                        date_value = row[date_pos]
                        try:
                            formatted_row[date_pos] = formatDate(date_value)
                        except Exception as e:
                            arcpy.AddError(e)
                        writer.writerow([utils.xstr(r) for r in formatted_row])

    except Exception as e:
        utils.msg("Error creating output file.", mtype='error', exception=e)
        sys.exit()

    utils.msg("Exported results saved to %s." % output_csv)
    if mode == 'toolbox':
        time.sleep(4)
Пример #56
0
    def search(self, keyword):
        if keyword is None:
            return None
        try:
            msg("Connecting to shell-storm.org...")
            s = six.moves.http_client.HTTPConnection("shell-storm.org")

            s.request("GET", "/api/?s="+str(keyword))
            res = s.getresponse()
            read_result = res.read().decode('utf-8')
            data_l = [x for x in read_result.split('\n') if x]  # remove empty results
        except Exception as e:
            if config.Option.get("debug") == "on":
                msg("Exception: %s" %e)
                traceback.print_exc()
            error_msg("Cannot connect to shell-storm.org")
            return None

        data_dl = []
        for data in data_l:
            try:
                desc = data.split("::::")
                dico = {
                         'ScAuthor': desc[0],
                         'ScArch': desc[1],
                         'ScTitle': desc[2],
                         'ScId': desc[3],
                         'ScUrl': desc[4]
                       }
                data_dl.append(dico)
            except Exception as e:
                if config.Option.get("debug") == "on":
                    msg("Exception: %s" %e)
                    traceback.print_exc()

        return data_dl
Пример #57
0
Файл: tag.py Проект: 4383/fabric
def _seek_version(cmd, txt):
    with nested(hide('running'), msg(txt)):
        cmd = cmd % _version('short')
        return local(cmd, capture=True)