# Path: sark/exceptions.py
# class SarkException(Exception):
# class SarkError(SarkException):
# class SarkNoSelection(SarkError):
# class SarkNoFunction(SarkError):
# class SarkAddFunctionFailed(SarkError):
# class SarkFunctionExists(SarkError):
# class SarkStructError(SarkError):
# class SarkInvalidRegisterName(SarkError):
# class SarkStructAlreadyExists(SarkStructError):
# class SarkStructCreationFailed(SarkStructError):
# class SarkStructNotFound(SarkStructError):
# class SarkErrorAddStructMemeberFailed(SarkStructError):
# class SarkErrorStructMemberName(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberOffset(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberSize(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberTinfo(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberStruct(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberUnivar(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberVarlast(SarkErrorAddStructMemeberFailed):
# class SarkEnumError(SarkError):
# class SarkErrorAddEnumMemeberFailed(SarkEnumError):
# class SarkErrorEnumMemberName(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberValue(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberEnum(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberMask(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberIllv(SarkErrorAddEnumMemeberFailed):
# class EnumNotFound(SarkEnumError):
# class EnumCreationFailed(SarkEnumError):
# class EnumAlreadyExists(SarkEnumError):
# class CantRenameEnumMember(SarkEnumError):
# class CantSetEnumMemberComment(SarkEnumError):
# class SarkErrorNameAlreadyExists(SarkError):
# class SarkSetNameFailed(SarkError):
# class SarkSwitchError(SarkError):
# class SarkNotASwitch(SarkSwitchError):
# class SarkNoInstruction(SarkError):
# class SarkOperandError(SarkError):
# class SarkOperandWithoutReg(SarkOperandError):
# class CantSetEnumComment(SarkEnumError):
# class CantDeleteEnumMember(SarkEnumError):
# class CantSetEnumBitfield(SarkEnumError):
# class CantRenameEnum(SarkEnumError):
# class SarkGuiError(SarkError):
# class SarkMenuError(SarkGuiError):
# class MenuAlreadyExists(SarkMenuError):
# class MenuNotFound(SarkMenuError):
# class FormNotFound(SarkGuiError):
# class InvalidStructOffset(SarkStructError):
# class SegmentError(SarkError):
# class NoMoreSegments(SegmentError):
# class InvalidBitness(SegmentError):
# class NoFileOffset(SarkError):
# class SarkNoString(SarkError):
# class SarkExpectedPatchedByte(SarkError):
# class PhraseError(SarkOperandError):
# class OperandNotPhrase(PhraseError):
# class PhraseNotSupported(PhraseError):
# class PhraseProcessorNotSupported(PhraseNotSupported):
# class SetTypeFailed(SarkError):
#     def __init__(self, ea, c_signature):


from itertools import repeat
from contextlib import suppress
from collections import deque
from sark import exceptions
import sark
import idaapi
import networkx as nx

def _try_get_function_start(ea):
    with suppress(exceptions.SarkNoFunction):
        return sark.Function(ea).start_ea

    return ea


def _get_best_name(ea):
    try:
        return sark.Function(ea).demangled
    except exceptions.SarkNoFunction:
        name = idaapi.get_ea_name(ea)
        if name:
            return name
        return '0x{:X}'.format(ea)


def _xrefs_to(function_ea):
    try:
        return sark.Function(function_ea).xrefs_to

    except exceptions.SarkNoFunction:
        return sark.Line(function_ea).xrefs_to


def _xrefs_from(function_ea):
    try:
        return sark.Function(function_ea).xrefs_from


# Path: sark/exceptions.py
# class SarkException(Exception):
# class SarkError(SarkException):
# class SarkNoSelection(SarkError):
# class SarkNoFunction(SarkError):
# class SarkAddFunctionFailed(SarkError):
# class SarkFunctionExists(SarkError):
# class SarkStructError(SarkError):
# class SarkInvalidRegisterName(SarkError):
# class SarkStructAlreadyExists(SarkStructError):
# class SarkStructCreationFailed(SarkStructError):
# class SarkStructNotFound(SarkStructError):
# class SarkErrorAddStructMemeberFailed(SarkStructError):
# class SarkErrorStructMemberName(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberOffset(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberSize(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberTinfo(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberStruct(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberUnivar(SarkErrorAddStructMemeberFailed):
# class SarkErrorStructMemberVarlast(SarkErrorAddStructMemeberFailed):
# class SarkEnumError(SarkError):
# class SarkErrorAddEnumMemeberFailed(SarkEnumError):
# class SarkErrorEnumMemberName(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberValue(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberEnum(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberMask(SarkErrorAddEnumMemeberFailed):
# class SarkErrorEnumMemberIllv(SarkErrorAddEnumMemeberFailed):
# class EnumNotFound(SarkEnumError):
# class EnumCreationFailed(SarkEnumError):
# class EnumAlreadyExists(SarkEnumError):
# class CantRenameEnumMember(SarkEnumError):
# class CantSetEnumMemberComment(SarkEnumError):
# class SarkErrorNameAlreadyExists(SarkError):
# class SarkSetNameFailed(SarkError):
# class SarkSwitchError(SarkError):
# class SarkNotASwitch(SarkSwitchError):
# class SarkNoInstruction(SarkError):
# class SarkOperandError(SarkError):
# class SarkOperandWithoutReg(SarkOperandError):
# class CantSetEnumComment(SarkEnumError):
# class CantDeleteEnumMember(SarkEnumError):
# class CantSetEnumBitfield(SarkEnumError):
# class CantRenameEnum(SarkEnumError):
# class SarkGuiError(SarkError):
# class SarkMenuError(SarkGuiError):
# class MenuAlreadyExists(SarkMenuError):
# class MenuNotFound(SarkMenuError):
# class FormNotFound(SarkGuiError):
# class InvalidStructOffset(SarkStructError):
# class SegmentError(SarkError):
# class NoMoreSegments(SegmentError):
# class InvalidBitness(SegmentError):
# class NoFileOffset(SarkError):
# class SarkNoString(SarkError):
# class SarkExpectedPatchedByte(SarkError):
# class PhraseError(SarkOperandError):
# class OperandNotPhrase(PhraseError):
# class PhraseNotSupported(PhraseError):
# class PhraseProcessorNotSupported(PhraseNotSupported):
# class SetTypeFailed(SarkError):
#     def __init__(self, ea, c_signature):


# Path: plugins/xrefsgraph.py
from itertools import repeat
from contextlib import suppress
from collections import deque
from sark import exceptions
import sark
import idaapi
import networkx as nx


def _try_get_function_start(ea):
    with suppress(exceptions.SarkNoFunction):
        return sark.Function(ea).start_ea

    return ea


def _get_best_name(ea):
    try:
        return sark.Function(ea).demangled
    except exceptions.SarkNoFunction:
        name = idaapi.get_ea_name(ea)
        if name:
            return name
        return '0x{:X}'.format(ea)


def _xrefs_to(function_ea):
    try:
        return sark.Function(function_ea).xrefs_to

    except exceptions.SarkNoFunction:
        return sark.Line(function_ea).xrefs_to


def _xrefs_from(function_ea):
    try:
        return sark.Function(function_ea).xrefs_from


    except exceptions.SarkNoFunction: