Exemple #1
0
    def network_manager_ignore(self, interface, mac_address):
        interface_ignore_string = ""
        for iface in self.interfaces_to_ignore:
            interface_ignore_string += ",interface-name:{}".format(iface)

        try:
            ignore_config = dedent("""
                                    [main]
                                    plugins=ifupdown,keyfile

                                    [ifupdown]
                                    managed=false

                                    [keyfile]
                                    unmanaged-devices=mac:{mac_address},interface-name:{interface}{ignore_interfaces}
                                    """.format(
                mac_address=mac_address,
                interface=interface,
                ignore_interfaces=interface_ignore_string))

            self.cleanup_filehandler()
            self.file_handler = FileHandler(self.nm_config_file)
            self.file_handler.write(ignore_config)
        except Exception as e:
            print e
            return False

        return True
Exemple #2
0
    def network_manager_ignore(self, interface, mac_address, virtInterfaces):
        if virtInterfaces > 0:
            mac_address = mac_address[:-1] + "0"
        interface_ignore_string = interface

        for i in range(virtInterfaces - 1):
            interface_ignore_string += ",mac:{}".format(mac_address[:-1] +
                                                        str(i + 1))
            interface_ignore_string += ",interface-name:{}_{}".format(
                interface, i)

        try:
            ignore_config = dedent("""
									[main]
									plugins=ifupdown,keyfile

									[ifupdown]
									managed=false

									[keyfile]
									unmanaged-devices=mac:{mac_address},interface-name:{ignore_interfaces}
									""".format(mac_address=mac_address,
                    ignore_interfaces=interface_ignore_string))

            self.cleanup_filehandler()
            self.file_handler = FileHandler(self.nm_config_file)
            self.file_handler.write(ignore_config)
        except Exception as e:
            print e
            return False

        return True
Exemple #3
0
def main():
    log = get_logger('Tagger')

    app = QApplication(sys.argv)

    file_handler = FileHandler()
    start_settings = file_handler.load_settings()
    qProcess = GUI(start_settings)

    EXIT_CODE = app.exec_()

    log.info('Saving and closing...')
    # Note: Use modified settings the GUI changes after use.
    file_handler.save_settings(qProcess.settings)
Exemple #4
0
    def __init__(self, canvas, folder):
        self.image_index = 1
        self.canvas = canvas
        self.folder = folder
        self.fileCont = FileHandler(folder)
        self.picture_count = self.fileCont.count_pics(folder)
        self.activeRect = self.canvas.create_rectangle(0, 0, 0, 0, fill='red')
        self.canvas.bind('<Button-1>', self.click_callback)
        self.canvas.bind('<ButtonRelease-1>', self.clickUp_callback)
        self.canvas.bind('<B1-Motion>', self.motion_callback)
        self.canvas.bind('<Key>', self.key_callback)
        self.canvas.focus_set()

        self.pic = self.draw_picture()
    def network_manager_ignore(self, interface, mac_address, virtInterfaces = 0):
        if virtInterfaces > 0:
            mac_address = mac_address[:-1] + "0"
        interface_ignore_string = interface

        for i in range(virtInterfaces):
            interface_ignore_string += ",mac:{}".format(mac_address[:-1] + str(i + 1))
            interface_ignore_string += ",interface-name:{}_{}".format(interface, i)

        try:
            ignore_config = dedent( """
                                    [main]
                                    plugins=ifupdown,keyfile

                                    [ifupdown]
                                    managed=false

                                    [keyfile]
                                    unmanaged-devices=mac:{mac_address},interface-name:{ignore_interfaces}
                                    """.format( mac_address=mac_address,
                                                ignore_interfaces=interface_ignore_string
                                                ))

            self.cleanup_filehandler()
            self.file_handler = FileHandler(self.nm_config_file)
            self.file_handler.write(ignore_config)
            self.netcards[interface].set_managed(True)
        except Exception as e:
            print e
            return False

        return True
Exemple #6
0
 def __init__(self, filename):
     '''
     Initializes the instances of the class.
     
     :Parameter:
     ontology: filename of the ontology
     
     '''
     self.addedindividualsCount = 0
     self.addedclassesCount = 0
     self.addedaxiomsCount = 0
     self.addedassertionsCount = 0
     self._ontologyClosureTag = "</Ontology>\n"
     self._ontology = FileHandler(filename)
     self._append_string = ""
     if not self._ontology.exists():
         raise Exception(
             "[OWLOntology] ERROR: {} file does not exists.".format(
                 filename))
Exemple #7
0
 def createBackup(self, filename):
     '''
     This function create a backup of the current ontology, there new axioms
     will be written.
     
     :Parameters:
     filename: filename of the backup file.
     
     :Returns:
     self._backup: instance of FileHandler
     
     '''
     self._backup = FileHandler(filename)
     ontology = self._ontology.readAll()
     index = len(ontology) - 1
     while ontology[index] != self._ontologyClosureTag:
         index = index - 1
     del ontology[index]
     self._backup.writeAll(ontology)
     return self._backup
Exemple #8
0
            '```shell\n', '```bash\n')
        self.word_count()
        if os.path.isfile(self.target_file_path):
            self.get_old_content()
            if not self.compare_content():
                self.migration_log_handler.info(
                    f'文件 {self.file_rel_path} 有更新内容')
                self.file_write()
        elif os.path.isdir(target_file_dir):
            self.file_write()
        else:
            os.makedirs(target_file_dir)
            self.file_write()


if __name__ == '__main__':
    print(22222222222222222222222)
    from utils import PathHandler, FileHandler
    test_dir = settings.TEST_DIR
    f = FileHandler.FileHandler()
    p = PathHandler.PathHandler()
    p_list = []
    p.get_md_files(test_dir, p_list)
    # print(p_list)
    for p in p_list:
        content = f.single_md_operation(p, test_dir, p_list)
        m = MigrationHandler(content, p, settings.TEST_DIR,
                             settings.TARGET_TEST_DIR)
        m.migrate()
        break
class NetworkManager(object):
    def __init__(
            self,
            networkmanager_config_path='/etc/NetworkManager/NetworkManager.conf'
    ):
        self.interfaces = pyw.interfaces()
        self.netcards = {
            interface: NetworkCard(interface)
            for interface in pyw.winterfaces()
        }
        self.nm_config_file = networkmanager_config_path
        self.file_handler = None

    def iptables_redirect(self, from_if, to_if):
        card = self.get_netcard(from_if)  # Get NetCard object

        if card != None:
            NetUtils().flush_iptables()
            NetUtils().accept_forwarding(from_if)
            NetUtils().set_postrouting_interface(to_if)
            NetUtils().add_routing_rule(card.get_subnet(), card.get_mask(),
                                        card.get_ip())

    def configure_interface(self,
                            interface,
                            ip,
                            netmask=None,
                            broadcast=None,
                            mtu=1800):
        card = self.get_netcard(interface)  # Get NetCard object

        if card != None:
            card.ifconfig(ip, netmask, broadcast)
            card.set_mtu_size(mtu)

    def set_mac_and_unmanage(self, interface, mac, retry=False):
        card = self.get_netcard(interface)

        # Runs at least once, if retry is flagged
        # it will try to reset the interface and repeat the process
        while (1):
            if card != None:
                if not card.set_mac(mac):
                    return False

                if not self.network_manager_ignore(interface, mac):
                    return False

                os.system("service NetworkManager restart"
                          )  # Restarting NetworkManager service
                if pyw.macget(card.card) == mac:
                    return True

            if not retry:
                break

            print "[-] Unable to set mac and unmanage, resetting interface and retrying."
            retry = False
            card = NetworkCard(interface)
            if card.get_mode() != 'managed':
                card.set_mode('managed')

        return False

    # NetworkManager is usually a conflicting process,
    # but we can configure it to ignore the interface
    # we use as access point or to sniff packets
    def network_manager_ignore(self, interface, mac_address):
        try:
            ignore_config = dedent("""
                                    [main]
                                    plugins=ifupdown,keyfile

                                    [ifupdown]
                                    managed=false

                                    [keyfile]
                                    unmanaged-devices=mac:{mac_address},interface-name:{interface}
                                    """.format(mac_address=mac_address,
                                               interface=interface))

            self.cleanup_filehandler()
            self.file_handler = FileHandler(self.nm_config_file)
            self.file_handler.write(ignore_config)
        except Exception as e:
            print e
            return False

        return True

    def get_netcard(self, interface):
        netcard = None
        try:
            try:
                netcard = self.netcards[interface]
            except KeyError:
                # Check if it was plugged in at runtime
                self.netcards = {
                    interface: NetworkCard(interface)
                    for interface in pyw.winterfaces()
                }
                netcard = self.netcards[interface]
        except KeyError:
            print "[-] Interface: '{}' does not exist".format(interface)
            return None

        return netcard

    def cleanup_filehandler(self):
        if self.file_handler:
            self.file_handler.restore_file()
            self.file_handler = None

    def reset_interfaces(self):
        for card in self.netcards:
            self.netcards[card].set_mac(self.netcards[card].original_mac)
            self.netcards[card].set_mode('managed')

    def cleanup(self):
        NetUtils().flush_iptables()
        self.cleanup_filehandler()
        self.reset_interfaces()
Exemple #10
0
class OWLOntology(object):
    '''
    This class occupies to insert additional axiom in the OWL ontology using
    OWL/XML syntax. It requires an ontology written in OWL/XML syntax.
    Every modifies are added into a backup ontology in order to keep original
    version of the given ontology.
    
    '''
    def __init__(self, filename):
        '''
        Initializes the instances of the class.
        
        :Parameter:
        ontology: filename of the ontology
        
        '''
        self.addedindividualsCount = 0
        self.addedclassesCount = 0
        self.addedaxiomsCount = 0
        self.addedassertionsCount = 0
        self._ontologyClosureTag = "</Ontology>\n"
        self._ontology = FileHandler(filename)
        self._append_string = ""
        if not self._ontology.exists():
            raise Exception(
                "[OWLOntology] ERROR: {} file does not exists.".format(
                    filename))

    def close(self):
        '''
        This function finalizes the ontology backup.
        
        '''
        self.printcounts()
        if self._backup and self._append_string:
            print(sys.getsizeof(self._append_string))
            self._backup.append(self._append_string)
            self._backup.append(self._ontologyClosureTag)
            self._ontology = self._backup
            self._backup = None

    def append_string(self, string):
        if sys.getsizeof(self._append_string) < 1000000:
            self._append_string += string
        else:
            self._backup.append(self._append_string)
            self._backup.append(string)
            self._append_string = ""

    def printcounts(self):
        logging.warning("METRICS:")
        logging.warning("The process added " +
                        str(self.addedindividualsCount) +
                        " individuals to the ontology.")
        logging.warning("The process added " + str(self.addedclassesCount) +
                        " classes to the ontology.")
        logging.warning("The process added " + str(self.addedaxiomsCount) +
                        " axioms to the ontology.")
        logging.warning("The process added " + str(self.addedassertionsCount) +
                        " assertions to the ontology.")

    def createBackup(self, filename):
        '''
        This function create a backup of the current ontology, there new axioms
        will be written.
        
        :Parameters:
        filename: filename of the backup file.
        
        :Returns:
        self._backup: instance of FileHandler
        
        '''
        self._backup = FileHandler(filename)
        ontology = self._ontology.readAll()
        index = len(ontology) - 1
        while ontology[index] != self._ontologyClosureTag:
            index = index - 1
        del ontology[index]
        self._backup.writeAll(ontology)
        return self._backup

    def fuzzyLabelAnnotation(self, fuzzyowl):
        '''
        
        :Parameters:
        fuzzyowl: .
        
        :Returns:
        annotation: annotation property in string form.
        '''
        annotation = (
            "\n\t\t<Annotation>"
            "\n\t\t\t<AnnotationProperty IRI=\"#fuzzyLabel\"/>"
            "\n\t\t\t<Literal datatypeIRI=\"&rdf;PlainLiteral\">{}</Literal>"
            "\n\t\t</Annotation>").format(escape(fuzzyowl, quote=True))
        return annotation

    def addAxiom(self, axiom):
        '''
        This function adds a general axiom expressed in OWL/XML syntax.
        
        :Parameters:
        axiom: the axiom in string format.
        '''
        self.addedaxiomsCount += 1
        self.append_string(axiom)

    def addIndividual(self, individual, classId=None, annotation=""):
        '''
        This function adds a single individual into the ontology.
        
        :Parameters:
        individual: identifier of the individual.
        classId: identifier of the class where the individual is an instance.
        
        '''
        axiom = ""
        if annotation:
            annotation = self.fuzzyLabelAnnotation(annotation)
        if classId:
            axiom = ("\n\t<ClassAssertion>"
                     "{0}"
                     "\n\t\t<Class IRI=\"#{1}\"/>"
                     "\n\t\t<NamedIndividual IRI=\"#{2}\"/>"
                     "\n\t</ClassAssertion>\n").format(annotation, classId,
                                                       individual)
        else:
            axiom = ("\n\t<Declaration>"
                     "{0}"
                     "\n\t\t<NamedIndividual IRI=\"{1}\"/>"
                     "\n\t</Declaration>\t").format(annotation, individual)
        self.addedindividualsCount += 1
        self.addedassertionsCount += 1
        self.append_string(axiom)

    def addDifferentIndividuals(self, first, second):
        '''
        This function adds an axiom that states two individuals are different.
        
        :Parameters:
        first: identifier of first individual
        second: identifier of second individual
        
        '''
        different = ("\n\t<DifferentIndividuals>"
                     "\n\t\t<NamedIndividual IRI=\"#{0}\"/>"
                     "\n\t\t<NamedIndividual IRI=\"#{1}\"/>"
                     "\n\t</DifferentIndividuals>\n").format(first, second)
        self.addedassertionsCount += 1
        self.append_string(different)

    def addClass(self, className, individuals=None, annotation=""):
        '''
        This function adds a new class into the ontology. If individuals
        parameter is set to None, a declaration of the class without
        individuals will be added.
        
        :Parameters:
        className: identifier of the new class.
        individuals: list of identifiers of class individuals.
        annotation: annotation assertion to apply at this axiom.
        
        '''
        if annotation:
            annotation = self.fuzzyLabelAnnotation(annotation)
        declaration = ("\n\t<Declaration>"
                       "{0}"
                       "\n\t\t<Class IRI=\"#{1}\"/>"
                       "\n\t</Declaration>\n").format(annotation, className)
        self.addedaxiomsCount += 1
        self.addedclassesCount += 1
        self.append_string(declaration)
        if individuals:
            for individual in individuals:
                self.addIndividual(individual, className)

    def classAxiom(self, classId):
        '''
        This function defines a class axiom.
        
        :Parameters:
        classId: identifier of the class.
        
        :Return:
        the class axiom in string form.
        '''
        return "<Class IRI=\"#{}\"/>".format(classId)

    def addSubClass(self, subClass, superClass):
        '''
        This function creates a sub class of an existent class of the ontology.
        
        :Parameters:
        subClass: identifier of the sub class.
        superClass: identifier of the super class
        
        '''
        subclassaxiom = ("\n\t<SubClassOf>"
                         "\n\t\t<Class IRI=\"#{0}\"/>"
                         "\n\t\t<Class IRI=\"#{1}\"/>"
                         "\n\t</SubClassOf>\n").format(subClass, superClass)
        self.addedaxiomsCount += 1
        self.append_string(subclassaxiom)

    def addDisjointClasses(self, first, second):
        '''
        This function adds an axiom which define two disjoint classes.
        
        :Parameters:
        first: identifier of a disjoint class.
        second: identifier of a disjoint class.
        
        '''
        disjoint = ("\n\t<DisjointClasses>"
                    "\n\t\t<Class IRI=\"#{0}\"/>"
                    "\n\t\t<Class IRI\"#{1}\"/>"
                    "\n\t</DisjointClasses>\n").format(first, second)
        self.addedaxiomsCount += 1
        self.append_string(disjoint)

    def addObjectProperty(self, objProperty, first=None, second=None):
        '''
        This function adds an object property between two classes. If first and
        second parameters are set to None, a declaration axiom of property will
        be added. 
        
        :Parameters:
        first: identifier of the domain class.
        second: identifier of the range class.
        
        '''
        objectProperty = ""
        if first and second:
            objectProperty = ("\n\t<ObjectPropertyAssertion>"
                              "\n\t\t<ObjectProperty IRI=\"#{0}\"/>"
                              "\n\t\t<NamedIndividual IRI=\"#{1}\"/>"
                              "\n\t\t<NamedIndividual IRI=\"#{2}\"/>"
                              "\n\t</ObjectPropertyAssertion>\n").format(
                                  objProperty, first, second)
        else:
            objectProperty = ("\n\t<Declaration>"
                              "\n\t\t<ObjectProperty IRI=\"#{}\"/>"
                              "\n\t</Declaration>\n").format(objProperty)
        self.addedassertionsCount += 1
        self.append_string(objectProperty)

    def addFunctionalObjectProperty(self, objProperty):
        '''
        This function adds an axiom that states a object property is functional.
        
        :Parameters:
        objProperty: identifier of object property.
        '''
        functionalProperty = (
            "\n\t<FunctionalObjectProperty>"
            "\n\t\t<ObjectProperty IRI=\"#{}\"/>"
            "\n\t</FunctionalObjectProperty>\n").format(objProperty)
        self.append_string(functionalProperty)

    def addObjectPropertyDomain(self, objProperty, domainClass):
        propertyRestriction = ("\n\t<ObjectPropertyDomain>"
                               "\n\t\t<ObjectProperty IRI=\"#{0}\"/>"
                               "\n\t\t<Class IRI=\"#{1}\"/>"
                               "\n\t</ObjectPropertyDomain>").format(
                                   objProperty, domainClass)
        self.append_string(propertyRestriction)

    def addObjectPropertyRange(self, objProperty, rangeClass):
        '''
        This function adds a property domain and range restriction to an object
        property.
        
        :Parameters:
        objProperty: identifier of the object property.
        domainClass: identifier of the domain class.
        rangeClass: identifier of the range class.
        
        '''
        propertyRestriction = ("\n\t<ObjectPropertyDomain>"
                               "\n\t\t<ObjectProperty IRI=\"#{0}\"/>"
                               "\n\t\t<Class IRI=\"#{1}\"/>"
                               "\n\t</ObjectPropertyDomain>").format(
                                   objProperty, rangeClass)
        self.append_string(propertyRestriction)

    def addDatatypeProperty(self,
                            dataProperty,
                            individual=None,
                            datatype=None,
                            value=None):
        '''
        This class adds a datatype property between individuals and datatypes.
        If individual, datatype and value parameters are set to None,
        a declaration axiom of property will be added.
        
        :Parameters:
        dataProperty: identifier of the datatype property.
        individual: indentifier of the individual.
        datatype: identifier of the datatype.
        value: datatype value.
        
        '''
        datatypeProperty = ""
        if individual and datatype and value:
            datatypeProperty = (
                "\n\t<DataPropertyAssertion>"
                "\n\t\t<DataProperty IRI=\"#{0}\"/>"
                "\n\t\t<NamedIndividual IRI=\"#{1}\"/>"
                "\n\t\t<Literal datatypeIRI=\"&xsd;{2}\">{3}</Literal>"
                "\n\t</DataPropertyAssertion>\n").format(
                    dataProperty, individual, datatype, value)
        else:
            datatypeProperty = ("\n\t<Declaration>"
                                "\n\t\t<DataProperty IRI=\"#{}\"/>"
                                "\n\t</Declaration>\n").format(dataProperty)
        self.addedassertionsCount += 1
        self.append_string(datatypeProperty)

    def addFunctionalDatatypeProperty(self, dataProperty):
        '''
        This function adds an axiom that states a datatype property is functional.
        
        :Parameters:
        dataProperty: identifier of datatype property.
        '''
        functionalProperty = (
            "\n\t<FunctionalDataProperty>"
            "\n\t\t<DataProperty IRI=\"#{}\"/>"
            "\n\t</FunctionalDataProperty>\n").format(dataProperty)
        self.append_string(functionalProperty)

    def addDatatypePropertyDomain(self, dataProperty, domainClass):
        '''
        This function adds a property domain restriction to datatype property.
        
        :Parameter:
        dataProperty: identifier of the datatype property.
        domainClass: identifier of the domain class.
        
        '''
        propertyRestriction = ("\n\t<DataPropertyDomain>"
                               "\n\t\t<DataProperty IRI=\"#{0}\"/>"
                               "\n\t\t<Class IRI=\"#{1}\"/>"
                               "\n\t</DataPropertyDomain>").format(
                                   dataProperty, domainClass)
        self.append_string(propertyRestriction)

    def addDatatypePropertyRange(self,
                                 dataProperty,
                                 datatype="",
                                 facetRestriction=""):
        '''
        This function adds a property range restriction to datatype property.
        
        :Parameter:
        dataProperty: identifier of the datatype property.
        datatype: identifier of datatype.
        
        '''
        if datatype:
            axiom = "\n\t\t<Datatype IRI=\"&xsd;{}\"/>".format(datatype)
        elif facetRestriction:
            axiom = facetRestriction
        propertyRestriction = ("\n\t<DataPropertyRange>"
                               "\n\t\t<DataProperty IRI=\"#{0}\"/>"
                               "{1}"
                               "\n\t</DataPropertyRange>\n").format(
                                   dataProperty, axiom)
        self.append_string(propertyRestriction)

    def addDatatypeDefinition(self,
                              dataDefinition,
                              facetRestriction,
                              annotation=""):
        '''
        This functions add a datatype definition in order to create a new
        datatype to represent a range of data.
        
        :Parameters:
        dataDefinition: identifier of new datatype to define.
        datatype: identifier of datatype used to represent the dataset.
        facetRestriction: maximum value of dataset.
        annotation: annotation in string format.
        
        '''
        if annotation:
            annotation = self.fuzzyLabelAnnotation(annotation)
        definition = ("\n\t<DatatypeDefinition>"
                      "{0}"
                      "\n\t\t<Datatype IRI=\"#{1}\"/>"
                      "{2}"
                      "\n\t</DatatypeDefinition>\n").format(
                          annotation, dataDefinition, facetRestriction)
        self.append_string(definition)

    def datatypeFacetRestrictionAxiom(self, datatype, firstFacet, firstValue,
                                      secondFacet, secondValue):
        '''
        This function adds a facet restriction to a datatype in order to
        define a range of allowed values. 
        
        :Parameters:
        datatype: identifier of datatype.
        firstFacet: identifier of first facet restriction.
        firstValue: first literal datatype value.
        secondFacet: identifier of second facet restriction.
        secondValue: second literal datatype value.
        
        :Return:
        facet restriction in string format.
        
        '''
        facetRestriction = (
            "\n\t\t<DatatypeRestriction>"
            "\n\t\t\t<Datatype IRI=\"&xsd;{0}\"/>"
            "\n\t\t\t<FacetRestriction facet=\"&xsd;{1}\">"
            "\n\t\t\t\t<Literal datatypeIRI=\"&xsd;{0}\">{2}</Literal>"
            "\n\t\t\t</FacetRestriction>"
            "\n\t\t\t<FacetRestriction facet=\"&xsd;{3}\">"
            "\n\t\t\t\t<Literal datatypeIRI=\"&xsd;{0}\">{4}</Literal>"
            "\n\t\t\t</FacetRestriction>"
            "\n\t\t</DatatypeRestriction>").format(datatype, firstFacet,
                                                   firstValue, secondFacet,
                                                   secondValue)
        return facetRestriction

    def addEquivalentClasses(self, classId, axiom):
        '''
        This function adds an equivalent classes assertion into the ontology.
        
        :Parameters:
        classId: identifier of the class.
        axiom: sub axiom of an equivalent class.
        
        '''
        equivalent = ("\n\t<EquivalentClasses>"
                      "\n\t\t<Class IRI=\"#{0}\"/>"
                      "{1}"
                      "\n\t</EquivalentClasses>\n").format(classId, axiom)
        self.addedaxiomsCount += 1
        self.append_string(equivalent)

    def objectHasValue(self, objProperty, individual):
        '''
        This function defines an object property value.
        
        :Parameters:
        objProperty: identifier of the object property.
        individual: identifier of the individual.
        
        :Returns:
        hasValue: ObjectHasValue axiom in string form.
        '''
        hasValue = ("\n\t\t<ObjectHasValue>"
                    "\n\t\t\t<ObjectProperty IRI=\"#{0}\"/>"
                    "\n\t\t\t<NamedIndividual IRI=\"#{1}\"/>"
                    "\n\t\t</ObjectHasValue>\n").format(
                        objProperty, individual)
        return hasValue

    def objectSomeValuesFromAxiom(self, objProperty, className):
        '''
        This function define an existential quantification for an object property.
        
        :Parameters:
        objProperty: identifier of object property.
        className: identifier of the class.
        
        :Returns:
        someValues: existential quantification axiom in string form.
        
        '''
        someValues = ("\n\t\t\t<ObjectSomeValuesFrom>"
                      "\n\t\t\t\t<ObjectProperty IRI=\"#{0}\"/>"
                      "\n\t\t\t\t<Class IRI=\"#{1}\"/>"
                      "\n\t\t\t</ObjectSomeValuesFrom>\n").format(
                          objProperty, className)
        return someValues

    def addObjectSomeValueFrom(self, objProperty, domainClass, rangeClass):
        '''
        This function adds an existential quantification for an object property.
        
        :Parameters:
        objProperty: identifier of object property.
        domainClass: identifier of domain class.
        rangeClass: identifier of range class.
        
        '''
        someValuesAxiom = self.objectSomeValuesFromAxiom(
            objProperty, rangeClass)
        self.addedassertionsCount += 1
        self.addEquivalentClasses(domainClass, someValuesAxiom)

    def objectAllValuesFromAxiom(self, objProperty, className):
        '''
        This function define an universal quantification for an object property.
        
        :Parameters:
        objProperty: identifier of object property.
        className: identifier of the class.
        
        :Returns:
        allValues: universal quantification axiom in string form.
        '''
        allValues = ("\n\t\t<ObjectAllValuesFrom>"
                     "\n\t\t\t<ObjectProperty IRI=\"#{0}\"/>"
                     "\n\t\t\t<Class IRI=\"#{1}\"/>"
                     "\n\t\t</ObjectAllValuesFrom>\n").format(
                         objProperty, className)
        return allValues

    def addObjectAllValuesFrom(self, objProperty, domainClass, rangeClass):
        '''
        This function adds an universal quantification for an object property.
        
        :Parameters:
        objProperty: identifier of object property.
        domainClass: identifier of domain class.
        rangeClass: identifier of range class.
        
        '''
        allValuesAxiom = self.objectAllValuesFromAxiom(objProperty, rangeClass)
        self.addEquivalentClasses(domainClass, allValuesAxiom)

    def objectUnionOfAxiom(self, classId, axiom):
        '''
        This function defines an union axiom.
        
        :Parameters:
        classId: identifier of a class.
        axiom: sub axiom of union axiom.
        
        :Returns:
        union axiom in string form.
        
        '''
        union = ("\n\t\t<ObjectUnionOf>"
                 "\n\t\t\t<Class IRI=\"#{0}\"/>"
                 "\n\t\t\t{1}"
                 "\n\t\t</ObjectUnionOf>\n").format(classId, axiom)
        return union

    def addObjectUnionOf(self, mainClass, first, second):
        '''
        This function adds an axiom that specifies the equivalence of a class
        with the union of two several classes.
        
        :Parameters:
        mainClass: identifier of the result class.
        first: identifier of a class.
        second: identifier of a class.
        
        '''
        s = self.classAxiom(second)
        union = self.objectUnionOfAxiom(first, s)
        self.addEquivalentClasses(mainClass, union)

    def objectIntersectionOfAxiom(self, classId, axiom):
        '''
        This function defines an intersection axiom.
        
        :Parameters:
        classId: identifier of the class.
        axiom: sub axiom to insert.
        
        :Returns:
        intersection axiom in string form.
        
        '''
        intersection = ("\n\t\t<ObjectIntersectionOf>"
                        "\n\t\t\t<Class IRI=\"#{0}\"/>"
                        "\n\t\t\t{1}"
                        "\n\t\t</ObjectIntersectionOf>\n").format(
                            classId, axiom)
        return intersection

    def addObjectIntersectionOf(self, mainClass, first, second):
        '''
        This function adds an axiom that specifies the equivalence of a class
        with the intersection of two several classes.
        
        :Parameters:
        mainClass: identifier of the result class.
        first: identifier of a class.
        second: identifier of a class.
        
        '''
        f = self.classAxiom(first)
        s = self.classAxiom(second)
        intersection = self.objectIntersectionOfAxiom(f, s)
        self.addEquivalentClasses(mainClass, intersection)

    def objectComplementOfAxiom(self, axiom):
        '''
        This function defines an complement axiom.
        
        :Parameters:
        axiom: sub axiom to insert.
        
        :Returns:
        complement axiom in string form.
        
        '''
        complement = ("\n\t\t<ObjectComplementOf>"
                      "\n\t\t\t{}>"
                      "\n\t\t</ObjectUnionOf>\n").format(axiom)
        return complement

    def addObjectComplementOf(self, mainClass, classId):
        '''
        This function adds an axiom that specifies the equivalence of a class
        with the complement of a given class.
        
        :Parameters:
        mainClass: identifier of the result class.
        classId: identifier of a class.
        
        '''
        axiom = self.classAxiom(classId)
        complement = self.objectComplementOfAxiom(axiom)
        self.addEquivalentClass(mainClass, complement)
Exemple #11
0
class NetworkManager(object):
    def __init__(
            self,
            networkmanager_config_path='/etc/NetworkManager/NetworkManager.conf',
            unmanaged_interfaces=[]):
        self.interfaces = pyw.interfaces()
        self.netcards = {
            interface: NetworkCard(interface)
            for interface in pyw.winterfaces()
        }
        self.nm_config_file = networkmanager_config_path
        self.file_handler = None

        self.unmanaged_interfaces_setup(unmanaged_interfaces)

    def unmanaged_check(self, interface):
        ok_status = ["unmanaged", "unavailable"]
        for line in check_output(["nmcli", "dev"]).split("\n"):
            try:
                args = line.split()[:4]
                iface, type, status, connection = args
                if interface == iface:
                    return status in ok_status
                else:
                    continue
            except:
                pass

        return True

    def unmanaged_interfaces_setup(self, unmanaged_interfaces):
        for iface in unmanaged_interfaces:
            if iface in self.interfaces:
                self.set_mac_and_unmanage(iface,
                                          self.netcards[iface].get_mac(), True)

    def iptables_redirect(self, from_if, to_if):
        card = self.get_netcard(from_if)  # Get NetCard object
        if card is not None:
            NetUtils().accept_forwarding(from_if)
            if not card.is_virtual():
                NetUtils().set_postrouting_interface(to_if)
                NetUtils().add_routing_rule(card.get_subnet(), card.get_mask(),
                                            card.get_ip())

    def configure_interface(self,
                            interface,
                            ip,
                            netmask=None,
                            broadcast=None,
                            mtu=1800):
        NetUtils().interface_config(interface, ip, netmask, broadcast)
        NetUtils().set_interface_mtu(interface, mtu)

    def set_mac_and_unmanage(self,
                             interface,
                             mac,
                             retry=False,
                             virtInterfaces=0):
        card = self.get_netcard(interface)

        # Runs at least once, if retry is flagged
        # it will try to reset the interface and repeat the process
        while (True):
            if card is not None:
                if not card.set_mac(mac):
                    return False

                if not self.unmanaged_check(interface) or virtInterfaces > 0:
                    if not self.network_manager_ignore(interface, mac,
                                                       virtInterfaces):
                        return False

                    os.system("service network-manager restart"
                              )  # Restarting NetworkManager service

                if pyw.macget(card.card) == mac:
                    return True

            if not retry:
                break

            print "[-] Unable to set mac and unmanage, resetting interface and retrying."
            retry = False
            try:
                card = NetworkCard(interface)
                if card.get_mode() != 'managed':
                    card.set_mode('managed')
            except:
                return False

        return False

    # NetworkManager is usually a conflicting process,
    # but we can configure it to ignore the interface
    # we use as access point or to sniff packets
    def network_manager_ignore(self, interface, mac_address, virtInterfaces=0):
        if virtInterfaces > 0:
            mac_address = mac_address[:-1] + "0"
        interface_ignore_string = interface

        for i in range(virtInterfaces):
            interface_ignore_string += ",mac:{}".format(mac_address[:-1] +
                                                        str(i + 1))
            interface_ignore_string += ",interface-name:{}_{}".format(
                interface, i)

        try:
            ignore_config = dedent("""
                                    [main]
                                    plugins=ifupdown,keyfile

                                    [ifupdown]
                                    managed=false

                                    [keyfile]
                                    unmanaged-devices=mac:{mac_address},interface-name:{ignore_interfaces}
                                    """.format(
                mac_address=mac_address,
                ignore_interfaces=interface_ignore_string))

            self.cleanup_filehandler()
            self.file_handler = FileHandler(self.nm_config_file)
            self.file_handler.write(ignore_config)
            self.netcards[interface].set_managed(True)
        except Exception as e:
            print e
            return False

        return True

    def get_netcard(self, interface):
        netcard = None
        try:
            try:
                netcard = self.netcards[interface]
            except KeyError:
                # Check if it was plugged in at runtime
                self.netcards = {
                    interface: NetworkCard(interface)
                    for interface in pyw.winterfaces()
                }
                netcard = self.netcards[interface]
        except KeyError:
            print "[-] Interface: '{}' does not exist".format(interface)
            return None

        return netcard

    def cleanup_filehandler(self):
        if self.file_handler:
            self.file_handler.restore_file()
            self.file_handler = None
            os.system("service network-manager restart")

    def reset_interfaces(self):
        restart_services = False
        for card in [
                card for card in self.netcards
                if not self.netcards[card].is_virtual
        ]:
            self.netcards[card].set_mac(self.netcards[card].original_mac)
            self.netcards[card].set_mode('managed')
            if self.netcards[card].is_managed():
                restart_services = True

        if restart_services:
            os.system("service networking restart")
            os.system("service network-manager restart")

    def cleanup(self):
        NetUtils().flush_iptables()
        self.cleanup_filehandler()
        self.reset_interfaces()
class NetworkManager(object):

    def __init__(self, networkmanager_config_path='/etc/NetworkManager/NetworkManager.conf', unmanaged_interfaces = []):
        self.interfaces = pyw.interfaces()
        self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() }
        self.nm_config_file = networkmanager_config_path
        self.file_handler = None

        self.unmanaged_interfaces_setup(unmanaged_interfaces)

    def unmanaged_check(self, interface):
        ok_status = ["unmanaged", "unavailable"]
        for line in check_output(["nmcli", "dev"]).split("\n"):
            try:
                args = line.split()[:4]
                iface, type, status, connection = args
                if interface == iface:
                    return status in ok_status
                else: continue
            except: pass

        return True

    def unmanaged_interfaces_setup(self, unmanaged_interfaces):
        for iface in unmanaged_interfaces:
            if iface in self.interfaces:
                self.set_mac_and_unmanage(iface, self.netcards[iface].get_mac(), True)

    def iptables_redirect(self, from_if, to_if):
        card = self.get_netcard(from_if)  # Get NetCard object
        if card is not None:
            NetUtils().accept_forwarding(from_if)
            if not card.is_virtual():
                NetUtils().set_postrouting_interface(to_if)
                NetUtils().add_routing_rule(card.get_subnet(), card.get_mask(), card.get_ip())

    def configure_interface(self, interface, ip, netmask=None, broadcast=None, mtu=1800):
        NetUtils().interface_config(interface, ip, netmask, broadcast)
        NetUtils().set_interface_mtu(interface, mtu)

    def set_mac_and_unmanage(self, interface, mac, retry = False, virtInterfaces = 0):
        card = self.get_netcard(interface)

        # Runs at least once, if retry is flagged
        # it will try to reset the interface and repeat the process
        while(True):
            if card is not None:
                if not card.set_mac(mac):
                    return False

                if not self.unmanaged_check(interface) or virtInterfaces > 0:
                    if not self.network_manager_ignore(interface, mac, virtInterfaces):
                        return False

                    os.system("service network-manager restart")  # Restarting NetworkManager service

                if pyw.macget(card.card) == mac:
                    return True

            if not retry:
                break

            print "[-] Unable to set mac and unmanage, resetting interface and retrying."
            retry = False
            try:
                card = NetworkCard(interface)
                if card.get_mode() != 'managed':
                    card.set_mode('managed')
            except:
                return False

        return False

    # NetworkManager is usually a conflicting process,
    # but we can configure it to ignore the interface
    # we use as access point or to sniff packets
    def network_manager_ignore(self, interface, mac_address, virtInterfaces = 0):
        if virtInterfaces > 0:
            mac_address = mac_address[:-1] + "0"
        interface_ignore_string = interface

        for i in range(virtInterfaces):
            interface_ignore_string += ",mac:{}".format(mac_address[:-1] + str(i + 1))
            interface_ignore_string += ",interface-name:{}_{}".format(interface, i)

        try:
            ignore_config = dedent( """
                                    [main]
                                    plugins=ifupdown,keyfile

                                    [ifupdown]
                                    managed=false

                                    [keyfile]
                                    unmanaged-devices=mac:{mac_address},interface-name:{ignore_interfaces}
                                    """.format( mac_address=mac_address,
                                                ignore_interfaces=interface_ignore_string
                                                ))

            self.cleanup_filehandler()
            self.file_handler = FileHandler(self.nm_config_file)
            self.file_handler.write(ignore_config)
            self.netcards[interface].set_managed(True)
        except Exception as e:
            print e
            return False

        return True

    def get_netcard(self, interface):
        netcard = None
        try:
            try:
                netcard = self.netcards[interface]
            except KeyError:
                # Check if it was plugged in at runtime
                self.netcards = { interface: NetworkCard(interface) for interface in pyw.winterfaces() }
                netcard = self.netcards[interface]
        except KeyError:
            print "[-] Interface: '{}' does not exist".format(interface)
            return None

        return netcard

    def cleanup_filehandler(self):
        if self.file_handler:
            self.file_handler.restore_file()
            self.file_handler = None
            os.system("service network-manager restart")

    def reset_interfaces(self):
        restart_services = False
        for card in [card for card in self.netcards if not self.netcards[card].is_virtual]:
            self.netcards[card].set_mac(self.netcards[card].original_mac)
            self.netcards[card].set_mode('managed')
            if self.netcards[card].is_managed():
                restart_services = True

        if restart_services:
            os.system("service networking restart")
            os.system("service network-manager restart")

    def cleanup(self):
        NetUtils().flush_iptables()
        self.cleanup_filehandler()
        self.reset_interfaces()
Exemple #13
0
class PicController:
    rectList = []
    activeRect = 0
    bbList = []
    active_type = 1
    active_color = 'red'
    folder = ''
    fileCont = {}
    picture_count = 0
    x_size = 640
    y_size = 480
    image_index = 1

    def __init__(self, canvas, folder):
        self.image_index = 1
        self.canvas = canvas
        self.folder = folder
        self.fileCont = FileHandler(folder)
        self.picture_count = self.fileCont.count_pics(folder)
        self.activeRect = self.canvas.create_rectangle(0, 0, 0, 0, fill='red')
        self.canvas.bind('<Button-1>', self.click_callback)
        self.canvas.bind('<ButtonRelease-1>', self.clickUp_callback)
        self.canvas.bind('<B1-Motion>', self.motion_callback)
        self.canvas.bind('<Key>', self.key_callback)
        self.canvas.focus_set()

        self.pic = self.draw_picture()

    def draw_picture(self):

        self.photo = self.fileCont.read_pic(self.image_index)
        w = self.canvas.create_image((0, 0), image=self.photo, anchor='nw')
        return w

    def click_callback(self, event):
        cx = self.canvas.canvasx(event.x)
        cy = self.canvas.canvasx(event.y)
        print('clicked at ', cx, cy)
        self.activeRect = self.canvas.create_rectangle(cx,
                                                       cy,
                                                       cx,
                                                       cy,
                                                       fill=self.active_color,
                                                       stipple='gray25')
        self.rectList.append(self.activeRect)

    def clickUp_callback(self, event):
        coords = self.canvas.coords(self.activeRect)
        coords[2] = event.x
        coords[3] = event.y
        if self.check_box(coords):
            a = BoxEntry()
            a.coords = self.normalize_coords(coords)
            a.pic = self.get_current_filename()
            a.box_type = self.active_type
            self.bbList.append(a)
        self.canvas.coords(self.activeRect, coords)

    def check_box(self, coords):
        dx = coords[2] - coords[0]
        dy = coords[1] - coords[3]

        area = abs(dx * dy)
        if area > 10:
            return True
        else:
            return False

    def get_current_filename(self):
        numString = str(self.image_index)
        while len(numString) < 3:
            numString = '0' + numString
        return 'img' + numString + '.jpg'

    def normalize_coords(self, coords):
        r_coords = [0, 0, 0, 0]
        r_coords[0] = coords[0] / self.x_size
        r_coords[1] = coords[1] / self.y_size
        r_coords[2] = coords[2] / self.x_size
        r_coords[3] = coords[3] / self.y_size
        return r_coords

    def motion_callback(self, event):
        coords = self.canvas.coords(self.activeRect)
        coords[2] = event.x
        coords[3] = event.y

        self.canvas.coords(self.activeRect, coords)

    def key_callback(self, event):
        print('pressed', event.char)
        if event.char == " ":
            if self.image_index != self.picture_count:
                self.image_index = self.image_index + 1
            else:
                print('Done! No more pictures to annotate.')
                self.fileCont.write_list(self.bbList, self.folder)

            for ibox in self.rectList:
                self.canvas.delete(ibox)

            self.rectList = []
            self.photo = self.fileCont.read_pic(self.image_index)
            self.canvas.itemconfig(self.pic, image=self.photo)

        elif event.char == 'q':
            print(self.rectList)
            for i in self.bbList:
                print(i)

        elif event.char == 'w':
            self.fileCont.write_list(self.bbList, self.folder)

        elif event.char == 'r':
            self.rectList = []
            self.bbList, self.image_index = self.fileCont.read_list(
                self.folder)
            print('loaded list up to picture %i' % self.image_index)
            self.image_index = self.image_index + 1

            self.photo = self.fileCont.read_pic(self.image_index)
            self.canvas.itemconfig(self.pic, image=self.photo)

        elif event.char == '1':
            self.active_type = 1
            self.active_color = 'red'

        elif event.char == '2':
            self.active_type = 2
            self.active_color = 'blue'

        elif event.char == '3':
            self.active_type = 3
            self.active_color = 'green'

        elif event.char == '4':
            self.active_type = 4
            self.active_color = 'yellow'

        elif event.char == '5':
            self.active_type = 5
            self.active_color = 'pink'
Exemple #14
0
TODO: Should probably work out how to initialize the file handlers using a YAML config file
TODO: We don't really want other people to have to edit .py files unless necessary
"""

from utils import FileHandler
from pathlib import Path
from utils import logger

#ntuple_dir = Path("mnt/e/NTuples/TauClassifier")
#ntuple_dir = "C:\\Users\\benwi\\TauClassifier"
ntuple_dir = "E:\\NTuples\\TauClassifier"

#print(Path(f"{ntuple_dir}\\*Gammatautau*\\*.root"))

gammatautau_files = FileHandler(
    "Gammatautau",
    str(Path(f"{ntuple_dir}/*Gammatautau*/*.root")),
    class_label=1)
#gammatautau_files = FileHandler("Gammatautau", "/mnt/e/NTuples/TauClassifier/*Gammatautau*/*.root", class_label=1)
#jz1_files = FileHandler("JZ1",  "/mnt/e/NTuples/TauClassifier/*JZ1*/*.root", class_label=0)
jz1_files = FileHandler("JZ1",
                        str(Path(f"{ntuple_dir}/*JZ1*/*.root")),
                        class_label=0)
jz2_files = FileHandler("JZ2",
                        str(Path(f"{ntuple_dir}/*JZ2*/*.root")),
                        class_label=0)
jz3_files = FileHandler("JZ3",
                        str(Path(f"{ntuple_dir}/*JZ3*/*.root")),
                        class_label=0)
jz4_files = FileHandler("JZ4",
                        str(Path(f"{ntuple_dir}/*JZ4*/*.root")),
                        class_label=0)