コード例 #1
0
ファイル: core.py プロジェクト: flashover/jaide
    def compare_config(self, commands="", req_format="text"):
        """ Execute a 'show | compare' against the specified commands.

        Purpose: This method will take in string of multiple commands,
               | and perform and 'show | compare' on the device to show the
               | differences between the active running configuration and
               | the changes proposed by the passed commands parameter.

        @param commands: A string, filepath, or list of multiple commands
                       | that the device will compare with.
        @type commands: str or list
        @param req_format: The desired format of the response, defaults to
                         | 'text', but also accepts 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not commands:
            raise InvalidCommandError("No commands specified")
        clean_cmds = [cmd for cmd in clean_lines(commands)]
        self.lock()
        self._session.load_configuration(action="set", config=clean_cmds)
        out = self._session.compare_configuration()
        self.unlock()
        if req_format.lower() == "xml":
            return out
        return out.xpath("configuration-information/configuration-output")[0].text
コード例 #2
0
ファイル: core.py プロジェクト: kimcharli/jaide
    def compare_config(self, commands="", req_format="text"):
        """ Execute a 'show | compare' against the specified commands.

        Purpose: This method will take in string of multiple commands,
               | and perform and 'show | compare' on the device to show the
               | differences between the active running configuration and
               | the changes proposed by the passed commands parameter.

        @param commands: A string, filepath, or list of multiple commands
                       | that the device will compare with.
        @type commands: str or list
        @param req_format: The desired format of the response, defaults to
                         | 'text', but also accepts 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not commands:
            raise InvalidCommandError('No commands specified')
        clean_cmds = [cmd for cmd in clean_lines(commands)]
        self.lock()
        self._session.load_configuration(action='set', config=clean_cmds)
        out = self._session.compare_configuration()
        self.unlock()
        if req_format.lower() == "xml":
            return out
        return out.xpath(
            'configuration-information/configuration-output')[0].text
コード例 #3
0
ファイル: core.py プロジェクト: kimcharli/jaide
    def commit_check(self, commands="", req_format="text"):
        """ Execute a commit check operation.

        Purpose: This method will take in string of multiple commands,
               | and perform and 'commit check' on the device to ensure
               | the commands are syntactically correct. The response can
               | be formatted as text or as xml.

        @param commands: A string, filepath, or list of multiple commands
                       | that the device will compare with.
        @type commands: str or list
        @param req_format: The desired format of the response, defaults to
                         | 'text', but also accepts 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not commands:
            raise InvalidCommandError('No commands specified')
        clean_cmds = []
        for cmd in clean_lines(commands):
            clean_cmds.append(cmd)
        self.lock()
        self._session.load_configuration(action='set', config=clean_cmds)
        # conn.validate() DOES NOT return a parse-able xml tree, so we
        # convert it to an ElementTree xml tree.
        results = ET.fromstring(self._session.validate(
            source='candidate').tostring)
        # release the candidate configuration
        self.unlock()
        if req_format == "xml":
            return ET.tostring(results)
        out = ""
        # we have to parse the elementTree object, and get the text
        # from the xml.
        for i in results.iter():
            # the success message is just a tag, so we need to get it
            # specifically.
            if i.tag == 'commit-check-success':
                out += 'configuration check succeeds\n'
            # this is for normal output with a tag and inner text, it will
            # strip the inner text and add it to the output.
            elif i.text is not None:
                if i.text.strip() + '\n' != '\n':
                    out += i.text.strip() + '\n'
            # this is for elements that don't have inner text, it will add the
            # tag to the output.
            elif i.text is None:
                if i.tag + '\n' != '\n':
                    out += i.tag + '\n'
        return out
コード例 #4
0
ファイル: core.py プロジェクト: NetworkAutomation/jaide
    def commit_check(self, commands="", req_format="text"):
        """ Execute a commit check operation.

        Purpose: This method will take in string of multiple commands,
               | and perform and 'commit check' on the device to ensure
               | the commands are syntactically correct. The response can
               | be formatted as text or as xml.

        @param commands: A string, filepath, or list of multiple commands
                       | that the device will compare with.
        @type commands: str or list
        @param req_format: The desired format of the response, defaults to
                         | 'text', but also accepts 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not commands:
            raise InvalidCommandError('No commands specified')
        clean_cmds = []
        for cmd in clean_lines(commands):
            clean_cmds.append(cmd)
        self.lock()
        self._session.load_configuration(action='set', config=clean_cmds)
        # conn.validate() DOES NOT return a parse-able xml tree, so we
        # convert it to an ElementTree xml tree.
        results = ET.fromstring(self._session.validate(
            source='candidate').tostring)
        # release the candidate configuration
        self.unlock()
        if req_format == "xml":
            return ET.tostring(results)
        out = ""
        # we have to parse the elementTree object, and get the text
        # from the xml.
        for i in results.iter():
            # the success message is just a tag, so we need to get it
            # specifically.
            if i.tag == 'commit-check-success':
                out += 'configuration check succeeds\n'
            # this is for normal output with a tag and inner text, it will
            # strip the inner text and add it to the output.
            elif i.text is not None:
                if i.text.strip() + '\n' != '\n':
                    out += i.text.strip() + '\n'
            # this is for elements that don't have inner text, it will add the
            # tag to the output.
            elif i.text is None:
                if i.tag + '\n' != '\n':
                    out += i.tag + '\n'
        return out
コード例 #5
0
def main(ctx, host, password, port, quiet, session_timeout, connect_timeout,
         username):
    """ Manipulate one or more Junos devices.

    Purpose: The main function is the entry point for the jaide tool. Click
           | handles arguments, commands and options. The parameters passed to
           | this function are all potential options (required or not) that
           | must come *before* the command from the group in the command line.

    @param ctx: The click context paramter, for receiving the object dictionary
              | being manipulated by other previous functions. Needed by any
              | function with the @click.pass_context decorator.
    @type ctx: click.Context
    @param host: The IP(s) or hostname(s) of the devices to connect to.
    @type host: str
    @param password: The string password used to connect to the device.
    @type password: str
    @param port: The numerical port to establish the connection to. Defauls
               | to 22.
    @type port: int
    @param quiet: An option that the user can set to suppress all output
                | from jaide.
    @type quiet: bool
    @param session_timeout: Sets the session timeout value. A higher value may
                          | be desired for long running commands, such as
                          | 'request system snapshot slice alternate'
    @type session_timeout: int
    @param connect_timeout: Sets the connection timeout value. This is how
                          | we'll wait when connecting before classifying
                          | the device unreachable.
    @type connect_timeout: int
    @param username: The string username used to connect to the device.
    @type useranme: str

    @returns: None. Functions part of click relating to the command group
            | 'main' do not return anything. Click handles passing context
            | between the functions and maintaing command order and chaining.
    """
    # build the list of hosts
    ctx.obj['hosts'] = [ip for ip in clean_lines(host)]
    # set the connection parameters
    ctx.obj['conn'] = {
        "username": username,
        "password": password,
        "port": port,
        "session_timeout": session_timeout,
        "connect_timeout": connect_timeout
    }
    if quiet:
        ctx.obj['out'] = "quiet"
コード例 #6
0
ファイル: wrap.py プロジェクト: NetworkAutomation/jaide
def shell(jaide, commands):
    """ Send shell commands to a device.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: The shell commands to send to the device.
    @type commands: str or list.

    @returns: The output of the commands.
    @rtype str
    """
    out = ""
    for cmd in clean_lines(commands):
        out += color('> %s\n' % cmd, 'yel')
        out += jaide.shell_cmd(cmd) + '\n'
    return out
コード例 #7
0
ファイル: wrap.py プロジェクト: suakow/jaide
def shell(jaide, commands):
    """ Send shell commands to a device.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: The shell commands to send to the device.
    @type commands: str or list.

    @returns: The output of the commands.
    @rtype str
    """
    out = ""
    for cmd in clean_lines(commands):
        out += color('> %s\n' % cmd, 'yel')
        out += jaide.shell_cmd(cmd) + '\n'
    return out
コード例 #8
0
ファイル: wrap.py プロジェクト: suakow/jaide
def command(jaide, commands, format="text", xpath=False):
    """ Run an operational command.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: the operational commands to send to the device.
    @type commands: str or list
    @param format: The desired output format from the device, either 'text'
                 | or 'xml' is supported.
    @type format: str
    @param xpath: The xpath expression to filter the results from the device.
                | If set, this forces the output to be requested in xml format.
    @type xpath: str

    @returns: The output from the device, and xpath filtered if desired.
    @rtype: str
    """
    output = ""
    for cmd in clean_lines(commands):
        expression = ""
        output += color('> ' + cmd + '\n', 'yel')
        # Get xpath expression from the command, if it is there.
        # If there is an xpath expr, the output will be xml,
        # overriding the req_format parameter
        #
        # Example command forcing xpath: show route % //rt-entry
        if len(cmd.split('%')) == 2:
            expression = cmd.split('%')[1].strip()
            cmd = cmd.split('%')[0] + '\n'
        elif xpath is not False:
            expression = xpath
        if expression:
            try:
                output += jaide.op_cmd(command=cmd,
                                       req_format='xml',
                                       xpath_expr=expression) + '\n'
            except lxml.etree.XMLSyntaxError:
                output += color('Xpath expression resulted in no response.\n',
                                'red')
        else:
            output += jaide.op_cmd(cmd, req_format=format) + '\n'
    return output
コード例 #9
0
ファイル: wrap.py プロジェクト: NetworkAutomation/jaide
def command(jaide, commands, format="text", xpath=False):
    """ Run an operational command.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: the operational commands to send to the device.
    @type commands: str or list
    @param format: The desired output format from the device, either 'text'
                 | or 'xml' is supported.
    @type format: str
    @param xpath: The xpath expression to filter the results from the device.
                | If set, this forces the output to be requested in xml format.
    @type xpath: str

    @returns: The output from the device, and xpath filtered if desired.
    @rtype: str
    """
    output = ""
    for cmd in clean_lines(commands):
        expression = ""
        output += color('> ' + cmd + '\n', 'yel')
        # Get xpath expression from the command, if it is there.
        # If there is an xpath expr, the output will be xml,
        # overriding the req_format parameter
        #
        # Example command forcing xpath: show route % //rt-entry
        if len(cmd.split('%')) == 2:
            expression = cmd.split('%')[1].strip()
            cmd = cmd.split('%')[0] + '\n'
        elif xpath is not False:
            expression = xpath
        if expression:
            try:
                output += jaide.op_cmd(command=cmd, req_format='xml',
                                       xpath_expr=expression) + '\n'
            except lxml.etree.XMLSyntaxError:
                output += color('Xpath expression resulted in no response.\n',
                                'red')
        else:
            output += jaide.op_cmd(cmd, req_format=format) + '\n'
    return output
コード例 #10
0
    def from_string(string):
        """
        Reads an PWInput object from a string.
        Convert PWInput to XSF, then parse XSF by ase.io to get coord, cell.
        Args:
            string (str): PWInput string

        Returns:
            PWInput object
        """
        tmpfile = '/tmp/tmp.in'
        with zopen(tmpfile, "w") as ftmp:
            ftmp.write(string)
        cmd = os.path.abspath(os.path.dirname(__file__))
        cmd += '/../script/pwi2xsf.sh '
        cmd += tmpfile
        p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
        xsf_str = p.stdout.read()
        ## Note: current pwi2xsf output can't be recognized by ase.io.read,
        ## so we have to replace DIM_GROUP at first 2 lines by CRYSTAL.
        ## pwi2xsf.x in qe can generate ase compatible xsf format,
        ## however it is not portable.
        xsf_lines = list(clean_lines(xsf_str.splitlines()))
        xsf_lines = ['CRYSTAL'] + xsf_lines[2:]
        xsf_str = '\n'.join(xsf_lines)
        xsf_io = StringIO.StringIO(xsf_str)
        
        # only for test
        #with open('/tmp/test.xsf','w') as f:
        #    f.write(xsf_str)
        
        atoms = ase.io.read(xsf_io,format='xsf')
        os.remove(tmpfile)
        
        lines = list(clean_lines(string.splitlines()))

        def input_mode(line):
            if line[0] == "&":
                return ("sections", line[1:].lower())
            elif "ATOMIC_SPECIES" in line:
                return ("pseudo", )
            elif "K_POINTS" in line:
                line = line.replace('{',' ')
                line = line.replace('}',' ')
                return ("kpoints", line.split()[1])
            elif "CELL_PARAMETERS" in line or "ATOMIC_POSITIONS" in line:
                line = line.replace('{',' ')
                line = line.replace('}',' ')
                return ("structure", line.split()[1])
            elif line == "/":
                return None
            else:
                return mode  # inherit from last line

        sections = {"control": {}, "system": {}, "electrons": {}, 
                    "ions": {}, "cell":{}}
        pseudo = {}
        lattice = []
        species = []
        coords = []
        structure = None
        mode = None
        for line in lines:
            mode = input_mode(line)
            if mode == None:
                pass
            elif mode[0] == "sections":
                section = mode[1]
                m = re.match(r'(\w+)\(?(\d*?)\)?\s*=\s*(.*)', line)
                if m:
                    key = m.group(1).strip()
                    key_ = m.group(2).strip()
                    val = m.group(3).strip()
                    if key_ != "":
                        if sections[section].get(key, None) == None:
                            val_ = [0.0]*20 # MAX NTYP DEFINITION
                            val_[int(key_)-1] = PWInput.proc_val(key, val)
                            sections[section][key] = val_
                        else:
                            sections[section][key][int(key_)-1] = PWInput.proc_val(key, val) 
                    else:
                        sections[section][key] = PWInput.proc_val(key, val)
                        
            elif mode[0] == "pseudo":
                m = re.match(r'(\w+)\s+(\d*.\d*)\s+(.*)', line)
                if m:
                    pseudo[m.group(1).strip()] = m.group(3).strip()
            elif mode[0] == "kpoints":
                m = re.match(r'(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)', line)
                if m:
                    kpoints_grid = (int(m.group(1)), int(m.group(2)), int(m.group(3)))
                    kpoints_shift = (int(m.group(4)), int(m.group(5)), int(m.group(6)))
                else:
                    kpoints_mode = mode[1]
            elif mode[0] == "structure":
                m_l = re.match(r'(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)', line)
                m_p = re.match(r'(\w+)\s+(-?\d+\.\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)', line)
                if m_l:
                    lattice += [ float(m_l.group(1)), float(m_l.group(2)), float(m_l.group(3)) ]
                elif m_p:
                    species += m_p.group(1)
                    coords += [[float(m_p.group(2)), float(m_p.group(3)), float(m_p.group(4))]]

                if mode[1] == "angstrom":
                    coords_are_cartesian = True
                elif mode[1] == "crystal":
                    coords_are_cartesian = False
            
        #return (species,coords,pseudo,lattice)
        #if coords_are_cartesian:
        #    atoms = ase.Atoms(symbols=species,positions=coords,pbc=True,cell=lattice)
        #else:
        #    atoms = 
        #        ase.Atoms(symbols=species,scaled_positions=coords,pbc=True,cell=lattice)
        #return sections
        return PWInput(
                    atoms=atoms,
                    pseudo = pseudo,
                    control=sections["control"],
                    system=sections["system"], 
                    electrons=sections["electrons"], 
                    ions=sections["ions"], 
                    cell=sections["cell"], 
                    kpoints_mode=kpoints_mode,
                    kpoints_grid=kpoints_grid, 
                    kpoints_shift=kpoints_shift)
コード例 #11
0
ファイル: core.py プロジェクト: kimcharli/jaide
    def commit(self, commands="", confirmed=None, comment=None,
               at_time=None, synchronize=False, req_format='text'):
        """ Perform a commit operation.

        Purpose: Executes a commit operation. All parameters are optional.
               | commit confirm and commit at are mutually exclusive. All
               | the others can be used with each other and commit confirm/at.

        @param commands: A string or list of multiple commands
                       | that the device will compare with.
                       | If a string, it can be a single command,
                       | multiple commands separated by commas, or
                       | a filepath location of a file with multiple
                       | commands, each on its own line.
        @type commands: str or list
        @param confirmed: integer value of the number of **seconds** to
                             | confirm the commit for, if requested.
        @type confirmed: int
        @param comment: string that the user wants to comment the commit
                      | with. Will show up in the 'show system commit' log.
        @type comment: str
        @param at_time: string designating the time at which the commit
                      | should happen. Can be in one of two Junos approved
                      | formats.
        @type comment: str
        @param synchronize: boolean set to true if desiring a commit
                          | synchronize operation.
        @type synchronize: bool
        @param req_format: string to specify the response format. Accepts
                         | either 'text' or 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        # ncclient doesn't support a truly blank commit, so if nothing is
        # passed, use 'annotate system' to make a blank commit
        if not commands:
            commands = 'annotate system ""'
        clean_cmds = []
        for cmd in clean_lines(commands):
            clean_cmds.append(cmd)
        # try to lock the candidate config so we can make changes.
        self.lock()
        self._session.load_configuration(action='set', config=commands)
        results = ""
        # confirmed and commit at are mutually exclusive. commit confirm
        # takes precedence.
        if confirmed:
            results = self._session.commit(confirmed=True,
                                           timeout=str(confirmed),
                                           comment=comment,
                                           synchronize=synchronize)
        else:
            results = self._session.commit(comment=comment, at_time=at_time,
                                           synchronize=synchronize)
        self.unlock()
        if results:
            if req_format == 'xml':
                return results
            # commit() DOES NOT return a parse-able xml tree, so we
            # convert it to an ElementTree xml tree.
            results = ET.fromstring(results.tostring)
            out = ''
            for i in results.iter():
                # the success message is just a tag, so we need to get it
                # specifically.
                if i.tag == 'commit-check-success':
                    out += 'configuration check succeeds\n'
                elif i.tag == 'commit-success':
                    out += 'commit complete\n'
                elif i.tag == 'ok':
                    out += 'commit complete\n'
                # this is for normal output with a tag and inner text, it will
                # strip the inner text and add it to the output.
                elif i.text is not None:
                    if i.text.strip() + '\n' != '\n':
                        out += i.text.strip() + '\n'
                # this is for elements that don't have inner text,
                # it will add the tag to the output.
                elif i.text is None:
                    if i.tag + '\n' != '\n':
                        out += i.tag + '\n'
            return out
        return False
コード例 #12
0
ファイル: core.py プロジェクト: flashover/jaide
    def commit(self, commands="", confirmed=None, comment=None, at_time=None, synchronize=False, req_format="text"):
        """ Perform a commit operation.

        Purpose: Executes a commit operation. All parameters are optional.
               | commit confirm and commit at are mutually exclusive. All
               | the others can be used with each other and commit confirm/at.

        @param commands: A string or list of multiple commands
                       | that the device will compare with.
                       | If a string, it can be a single command,
                       | multiple commands separated by commas, or
                       | a filepath location of a file with multiple
                       | commands, each on its own line.
        @type commands: str or list
        @param confirmed: integer value of the number of **seconds** to
                             | confirm the commit for, if requested.
        @type confirmed: int
        @param comment: string that the user wants to comment the commit
                      | with. Will show up in the 'show system commit' log.
        @type comment: str
        @param at_time: string designating the time at which the commit
                      | should happen. Can be in one of two Junos approved
                      | formats.
        @type comment: str
        @param synchronize: boolean set to true if desiring a commit
                          | synchronize operation.
        @type synchronize: bool
        @param req_format: string to specify the response format. Accepts
                         | either 'text' or 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        # ncclient doesn't support a truly blank commit, so if nothing is
        # passed, use 'annotate system' to make a blank commit
        if not commands:
            commands = 'annotate system ""'
        clean_cmds = []
        for cmd in clean_lines(commands):
            clean_cmds.append(cmd)
        # try to lock the candidate config so we can make changes.
        self.lock()
        self._session.load_configuration(action="set", config=commands)
        results = ""
        # confirmed and commit at are mutually exclusive. commit confirm
        # takes precedence.
        if confirmed:
            results = self._session.commit(
                confirmed=True, timeout=str(confirmed), comment=comment, synchronize=synchronize
            )
        else:
            results = self._session.commit(comment=comment, at_time=at_time, synchronize=synchronize)
        self.unlock()
        if results:
            if req_format == "xml":
                return results
            # commit() DOES NOT return a parse-able xml tree, so we
            # convert it to an ElementTree xml tree.
            results = ET.fromstring(results.tostring)
            out = ""
            for i in results.iter():
                # the success message is just a tag, so we need to get it
                # specifically.
                if i.tag == "commit-check-success":
                    out += "configuration check succeeds\n"
                elif i.tag == "commit-success":
                    out += "commit complete\n"
                elif i.tag == "ok":
                    out += "commit complete\n"
                # this is for normal output with a tag and inner text, it will
                # strip the inner text and add it to the output.
                elif i.text is not None:
                    if i.text.strip() + "\n" != "\n":
                        out += i.text.strip() + "\n"
                # this is for elements that don't have inner text,
                # it will add the tag to the output.
                elif i.text is None:
                    if i.tag + "\n" != "\n":
                        out += i.tag + "\n"
            return out
        return False
コード例 #13
0
with open('final_model.json', 'rt') as f:
    arch = f.read()

model = model_from_json(arch,
                        custom_objects={'AttentionLayer': AttentionLayer})
model.load_weights(args['model'])
# model.summary()

encoder_model, decoder_model = build_inference_models(eng_length,
                                                      ger_vocab_size, model)
# encoder_model.summary()
# decoder_model.summary()

# Preprocess input data same as in data-deu.py
source = [args['source']]
source = clean_lines(source)
source = encode_sequences(eng_tokenizer,
                          eng_length,
                          source,
                          padding_type='pre')

seq = encode_sequences(ger_tokenizer, None, ['sos'])
onehot_seq = np.expand_dims(to_categorical(seq, num_classes=ger_vocab_size), 1)

enc_outs, enc_fwd_state, enc_back_state = encoder_model.predict(source)
dec_fwd_state, dec_back_state = enc_fwd_state, enc_back_state

translation = predict_attention_sequence(decoder_model, enc_outs,
                                         dec_fwd_state, dec_back_state,
                                         ger_tokenizer, ger_vocab_size,
                                         ger_length, onehot_seq)