Exemplo n.º 1
0
def createBlogData(blogFile=None):
    if blogFile == None:
        # edit temp blog data file 
        blogFile = NamedTemporaryFile(suffix='.txt', prefix='blogger_py')
        editor = os.environ.get('EDITOR', Editor)

        stat = os.spawnlp(os.P_WAIT, editor, editor, blogFile.name)

        if stat != 0:
            raise EditError, 'edit tempfile failed: %s %s' % \
                             (editor, blogFile.name)

    # read blog data from temp file, publish a public post.
    title = blogFile.readline().rstrip('\r\n')
    label = blogFile.readline().strip()
    if label == '':
        _title = title.split(':') # title == 'LABEL: string'
        if len(_title) >= 2:
            label = _title[0]

    content = blogFile.read()
    _parts = docutils.core.publish_parts(source=content, writer_name='html')
    body = _parts['body']
    title = title.decode('utf-8')

    saveBlogFile(blogFile.name)

    blogFile.close()  # blogFile will delete automatically

    return title, body, label
Exemplo n.º 2
0
    def _regread(self,path):
        """Read contents of given key from the registry.

        The result is a generator of (key,name,data,type) tuples.  For
        the keys themselves each of name, data and type will be None.
        """
        tf = NamedTemporaryFile()
        self._run_regedit("/E",tf.name,path)
        tf.seek(0)
        tf.readline()
        (key,name,data,type) = (None,None,None,None)
        for ln in tf:
            if not ln:
                continue
            if ln.startswith("["):
                 key = ln.strip().strip("[]").strip("\"")
                 (name,data,type) = (None,None,None)
            else:
                val_re = r'^"?(\@|[^"]+)"?="?(([a-zA-Z0-9\(\)]+:)?)([^"]+)"?$'
                m = re.match(val_re,ln.strip())
                if not m:
                    continue
                (name,type,_,data) = m.groups()
                if name == "@":
                    name = ""
                type = _unmap_type(type)
                data = _unmap_data(data,type)
            yield (key,name,data,type)
Exemplo n.º 3
0
def Gtk2_GetSaveFilename(master, name, title, icon, **kw):
	''' Calls Gnome open file dialog.   
	Parameteres:
	master - parent window
	name - application name
	title - dialog title
	**kw:
	initialdir - absolute path to initial dir
	initialfile - file name
	
	Returns: tuple of utf8 and system encoded file names
	'''
	initialdir = check_initialdir(kw['initialdir'])
	initialfile = ''
	if kw['initialfile']: initialfile = kw['initialfile']

	filetypes = convert_for_gtk2(kw['filetypes'])
	name += ' - ' + title

	tmpfile = NamedTemporaryFile()
	execline = ''
	if sk1.LANG: execline += 'export LANG=' + sk1.LANG + ';'
	path = os.path.join(initialdir, initialfile)
	execline += 'python %s/gtk/save_file_dialog.py ' % (PATH,)
	execline += ' caption="' + name + '" path="' + path + '" '
	execline += ' filetypes="' + filetypes + '" window-icon="' + icon + '"'
	os.system(execline + '>' + tmpfile.name)
	result = tmpfile.readline().strip()
	return (result, result)
Exemplo n.º 4
0
def get_system_fonts():
	"""
	Returns list of four fonts, used in sK1 UI:
	[small_font,normal_font,large_font,fixed_font]
	Each font is a list like:
	[font_family,font_style,font_size]
	where:
	font_family - string representation of font family
	font_style - list of font styles like 'bold' and 'italic'
	font_size - font size integer value
	
	Currently Gtk binding is implemented. Win32 and MacOS X 
	implementation will be later.
	"""

	tmpfile = NamedTemporaryFile()
	command = "import gtk;w = gtk.Window();w.realize();style=w.get_style(); print style.font_desc.to_string();"
	os.system('python -c "%s" >%s 2>/dev/null' % (command, tmpfile.name))

	font = tmpfile.readline().strip()

	normal_font = process_gtk_font_string(font)
	small_font = copy.deepcopy(normal_font)
	small_font[2] -= 1

	large_font = copy.deepcopy(normal_font)
	large_font[2] += 2
	if not 'bold' in large_font[1]:
		large_font[1].append('bold')

	fixed_font = copy.deepcopy(normal_font)
	fixed_font[0] = 'monospace'

	return [small_font, normal_font, large_font, fixed_font]
Exemplo n.º 5
0
def get_system_fonts():
    """
	Returns list of four fonts, used in sK1 UI:
	[small_font,normal_font,large_font,fixed_font]
	Each font is a list like:
	[font_family,font_style,font_size]
	where:
	font_family - string representation of font family
	font_style - list of font styles like 'bold' and 'italic'
	font_size - font size integer value
	
	Currently Gtk binding is implemented. Win32 and MacOS X 
	implementation will be later.
	"""

    tmpfile = NamedTemporaryFile()
    command = "import gtk;w = gtk.Window();w.realize();style=w.get_style(); print style.font_desc.to_string();"
    os.system('python -c "%s" >%s 2>/dev/null' % (command, tmpfile.name))

    font = tmpfile.readline().strip()

    normal_font = process_gtk_font_string(font)
    small_font = copy.deepcopy(normal_font)
    small_font[2] -= 1

    large_font = copy.deepcopy(normal_font)
    large_font[2] += 2
    if not 'bold' in large_font[1]:
        large_font[1].append('bold')

    fixed_font = copy.deepcopy(normal_font)
    fixed_font[0] = 'monospace'

    return [small_font, normal_font, large_font, fixed_font]
Exemplo n.º 6
0
def get_gtk_fonts():
    """
    Returns list of four fonts, used in UI:
    [small_font,normal_font,large_font,fixed_font]
    Each font is a list like:
    [font_family,font_style,font_size]
    where:
    font_family - string representation of font family
    font_style - list of font styles like 'bold' and 'italic'
    font_size - font size integer value
    """
    
    tmpfile = NamedTemporaryFile()
    command = "import gi;from gi.repository import Gtk;print(Gtk.Settings.get_default().get_property('gtk-font-name'))"
    os.system(sys.executable +' -c "%s" >%s 2>/dev/null' % (command, tmpfile.name))
    
    font = tmpfile.readline().strip()
    if not font:
            font = 'Sans 10'
    
    normal_font = process_gtk_font_string(font)
    small_font = copy.deepcopy(normal_font)
    small_font[2] -= 1
    
    large_font = copy.deepcopy(normal_font)
    large_font[2] += 2
    if not 'bold' in large_font[1]:
        large_font[1].append('bold')
        
    fixed_font = copy.deepcopy(normal_font)
    fixed_font[0] = 'monospace'
    
    return [small_font, normal_font, large_font, fixed_font]
Exemplo n.º 7
0
class TombOutputThread(QtCore.QThread):
    line_received = QtCore.pyqtSignal(QtCore.QString)
    error_received = QtCore.pyqtSignal(QtCore.QString)
    progressed = QtCore.pyqtSignal(int) #value in percent

    def __init__(self):
        QtCore.QThread.__init__(self)
        self.buffer = NamedTemporaryFile()

    def run(self):
        while True:
            where = self.buffer.tell()
            line = self.buffer.readline()
            if not line:
                time.sleep(1)
                self.buffer.seek(where)
            else:
                #ansi color escapes messes this up, but it'ok anyway
                self.line_received.emit(line)
                self.parse_line(line)

    def parse_line(self, line):
        #This could be simplified, and s/search/match, if --no-color supported
        #see #59
        #TODO: this should be moved to tomblib.parse
        parsed = parse_line(line)
        if parsed and parsed['type'] == 'error':
            self.error_received.emit(parsed.content)
Exemplo n.º 8
0
class SDKServer(object):
    def __init__(
        self,
        port=None,  # type: Optional[int]
        singleton=True,  # type: bool
        lazy=False,  # type: bool
        idle_timeout=None,  # type: Optional[int]
        log_file_name=None,  # type: Optional[str]
        executable=executable_path,  # type: str
    ):
        if log_file_name:
            self._log_file = open(log_file_name, "w+b")
            self.log_file_name = log_file_name
        else:
            self._log_file = NamedTemporaryFile("w+b")
            self.log_file_name = self._log_file.name
        self.executable = executable
        command = [executable]
        if port is not None:
            command.extend(["--port", port])
        if not singleton:
            command.append("--no-singleton")
        if lazy:
            command.append("--lazy")
        if idle_timeout is not None:
            command.extend(["--idle-timeout", idle_timeout])
        if sys.version_info < (3,) and sys.platform != "win32":
            # python2 tends to hang if there are unclosed fds owned by child process
            # close_fds is not supported by windows python2 so not using it there
            sdk = Popen(command, stdout=self._log_file, stderr=STDOUT, close_fds=True)
        else:
            sdk = Popen(command, stdout=self._log_file, stderr=STDOUT)
        self._sdk_process = None if singleton else sdk  # leak the singleton process
        self.port = self._read_port()
        self.is_closed = False  # explicit flag because python2 calls __del__ many times
        logger.info("Started Universal SDK server at %s", self.port)

    def _read_port(self):
        while True:
            self._log_file.seek(0)
            first_line = self._log_file.readline()
            if first_line:
                return int(first_line)
            else:
                sleep(0.5)

    def close(self):
        if not self.is_closed:
            self.is_closed = True
            if self._sdk_process:
                logger.info("Terminating Universal SDK server at %s", self.port)
                self._sdk_process.terminate()
            self._log_file.close()

    def __repr__(self):
        return "SDKServer(port={})".format(self.port)

    def __del__(self):
        self.close()
Exemplo n.º 9
0
def wpteval(align, train_filenames, test_filename, gold_wa):
    test_numbers = []

    mosesf = NamedTemporaryFile('w+', encoding='utf-8')

    with NamedTemporaryFile('w', encoding='utf-8') as outf1, \
         NamedTemporaryFile('w', encoding='utf-8') as outf2:
        with open(test_filename[0], 'r', encoding='utf-8') as f:
            for i,line in enumerate(f):
                m = RE_NUMBERED.match(line)
                assert m, 'Test data file %s not numbered properly!' % \
                          test_filename[0]
                test_numbers.append(m.group(1))
                print(m.group(2).strip(), file=outf1)
        with open(test_filename[1], 'r', encoding='utf-8') as f:
            for i,line in enumerate(f):
                m = RE_NUMBERED.match(line)
                assert m, 'Test data file %s not numbered properly!' % \
                          test_filename[1]
                assert test_numbers[i] == m.group(1)
                print(m.group(2).strip(), file=outf2)

        for filename1,filename2 in train_filenames:
            with open(filename1, 'r', encoding='utf-8') as f1, \
                 open(filename2, 'r', encoding='utf-8') as f2:
                while True:
                    line1 = f1.readline()
                    line2 = f2.readline()
                    assert (not line1) == (not line2), \
                           'Number of lines differs between %s and %s!' % (
                           filename1, filename2)
                    if (not line1) or (not line2): break
                    line1 = line1.strip()
                    line2 = line2.strip()
                    if line1 and line2:
                        print(line1, file=outf1)
                        print(line2, file=outf2)

        outf1.flush()
        outf2.flush()
        align(outf1.name, outf2.name, mosesf.name)

    with NamedTemporaryFile('w', encoding='utf-8') as outf:
        for lineno in test_numbers:
            for i,j in map(lambda s: s.split('-'), mosesf.readline().split()):
                print('%s %d %d' % (lineno, int(i)+1, int(j)+1), file=outf)

        outf.flush()
        subprocess.call(
                ['perl', '3rdparty/wa_check_align.pl', outf.name])
        subprocess.call(
                ['perl', '3rdparty/wa_eval_align.pl', gold_wa, outf.name])

    mosesf.close()
Exemplo n.º 10
0
def test_utils_redirect_output():
    """Test redirect output context manager."""
    temp_file = NamedTemporaryFile(mode='w+', delete=False)

    with ipa_utils.redirect_output(temp_file):
        print('Test message to file!')

    with open(temp_file.name) as temp_file:
        assert temp_file.readline() == 'Test message to file!\n'

    with ipa_utils.ignored(OSError):
        os.remove(temp_file.name)
Exemplo n.º 11
0
	def import_gtk_colors(self):
		"""
		Imports system gtk color scheme using pygtk binding. 
		"""
		colors={}
		tmpfile=NamedTemporaryFile()
		command="import gtk;w = gtk.Window();w.realize();style=w.get_style();"
		command+="print style.base[gtk.STATE_NORMAL].to_string(),"+ \
			" style.base[gtk.STATE_ACTIVE].to_string(),"+ \
			" style.base[gtk.STATE_PRELIGHT].to_string(),"+ \
			" style.base[gtk.STATE_SELECTED].to_string(),"+ \
			" style.base[gtk.STATE_INSENSITIVE].to_string();"
		command+="print style.text[gtk.STATE_NORMAL].to_string(),"+ \
			" style.text[gtk.STATE_ACTIVE].to_string(),"+ \
			" style.text[gtk.STATE_PRELIGHT].to_string(),"+ \
			" style.text[gtk.STATE_SELECTED].to_string(),"+ \
			" style.text[gtk.STATE_INSENSITIVE].to_string();"
		command+="print style.fg[gtk.STATE_NORMAL].to_string(),"+ \
			" style.fg[gtk.STATE_ACTIVE].to_string(),"+ \
			" style.fg[gtk.STATE_PRELIGHT].to_string(),"+ \
			" style.fg[gtk.STATE_SELECTED].to_string(),"+ \
			" style.fg[gtk.STATE_INSENSITIVE].to_string();"
		command+="print style.bg[gtk.STATE_NORMAL].to_string(),"+ \
			" style.bg[gtk.STATE_ACTIVE].to_string(),"+ \
			" style.bg[gtk.STATE_PRELIGHT].to_string(),"+ \
			" style.bg[gtk.STATE_SELECTED].to_string(),"+ \
			" style.bg[gtk.STATE_INSENSITIVE].to_string();"
	
		os.system('python -c "%s" >%s 2>/dev/null'%(command, tmpfile.name))	

		for type in ["base","text","fg","bg"]:
			line=tmpfile.readline().strip().split()
			colors[type+' normal']=gtk_to_tk_color(line[0])
			colors[type+' active']=gtk_to_tk_color(line[1])
			colors[type+' prelight']=gtk_to_tk_color(line[2])
			colors[type+' selected']=gtk_to_tk_color(line[3])
			colors[type+' insensitive']=gtk_to_tk_color(line[4])
		tmpfile.close()
		
		self.map_gtk_colors(colors)
Exemplo n.º 12
0
    def import_gtk_colors(self):
        """
        Imports system gtk color scheme using pygtk binding. 
        """
        colors = {}
        tmpfile = NamedTemporaryFile()
        command = "import gtk;w = gtk.Window();w.realize();style=w.get_style();"
        command += "print style.base[gtk.STATE_NORMAL].to_string()," + \
            " style.base[gtk.STATE_ACTIVE].to_string()," + \
            " style.base[gtk.STATE_PRELIGHT].to_string()," + \
            " style.base[gtk.STATE_SELECTED].to_string()," + \
            " style.base[gtk.STATE_INSENSITIVE].to_string();"
        command += "print style.text[gtk.STATE_NORMAL].to_string()," + \
            " style.text[gtk.STATE_ACTIVE].to_string()," + \
            " style.text[gtk.STATE_PRELIGHT].to_string()," + \
            " style.text[gtk.STATE_SELECTED].to_string()," + \
            " style.text[gtk.STATE_INSENSITIVE].to_string();"
        command += "print style.fg[gtk.STATE_NORMAL].to_string()," + \
            " style.fg[gtk.STATE_ACTIVE].to_string()," + \
            " style.fg[gtk.STATE_PRELIGHT].to_string()," + \
            " style.fg[gtk.STATE_SELECTED].to_string()," + \
            " style.fg[gtk.STATE_INSENSITIVE].to_string();"
        command += "print style.bg[gtk.STATE_NORMAL].to_string()," + \
            " style.bg[gtk.STATE_ACTIVE].to_string()," + \
            " style.bg[gtk.STATE_PRELIGHT].to_string()," + \
            " style.bg[gtk.STATE_SELECTED].to_string()," + \
            " style.bg[gtk.STATE_INSENSITIVE].to_string();"
    
        os.system('python -c "%s" >%s 2>/dev/null' % (command, tmpfile.name))    

        for type in ["base", "text", "fg", "bg"]:
            line = tmpfile.readline().strip().split()
            colors[type + ' normal'] = gtk_to_tk_color(line[0])
            colors[type + ' active'] = gtk_to_tk_color(line[1])
            colors[type + ' prelight'] = gtk_to_tk_color(line[2])
            colors[type + ' selected'] = gtk_to_tk_color(line[3])
            colors[type + ' insensitive'] = gtk_to_tk_color(line[4])
        tmpfile.close()

        self.map_gtk_colors(colors)
Exemplo n.º 13
0
def align_to_genome(basecalls, genome_filename, graphmap_path):
    ## align to genome
    read_fp = NamedTemporaryFile(delete=False, suffix='.fasta')
    read_fp.write(">" + ''.join(basecalls[:5]) + '\n' + ''.join(basecalls) +
                  '\n')
    read_fp.close()
    out_fp = NamedTemporaryFile(delete=False)
    try:
        # suppress output from graphmap with devnull sink
        with open(os.devnull, 'w') as FNULL:
            exitStatus = call([
                graphmap_path, 'align', '-r', genome_filename, '-d',
                read_fp.name, '-o', out_fp.name, '-L', 'm5'
            ],
                              stdout=FNULL,
                              stderr=STDOUT)

        alignment = dict(zip(GRAPHMAP_FIELDS, out_fp.readline().split()))
        out_fp.close()
    except:
        raise OSError, ('Problem running/parsing graphmap. Ensure ' +
                        'you have a compatible version installed.')

    ## flip read and genome to match raw signal if neg strand mapped
    if len(alignment) != len(GRAPHMAP_FIELDS):
        raise NotImplementedError, ('Graphmap did not produce alignment.')

    if alignment['tStrand'] != '+':
        raise NotImplementedError, (
            'Graphmap indicates negative strand reference mapping.')

    if alignment['qStrand'] == "+":
        alignVals = zip(alignment['qAlignedSeq'], alignment['tAlignedSeq'])
    else:
        alignVals = zip(rev_comp(alignment['qAlignedSeq']),
                        rev_comp(alignment['tAlignedSeq']))

    return alignVals, genomeLoc(int(alignment['tStart']), alignment['qStrand'],
                                alignment['tName'])
Exemplo n.º 14
0
class ControlBase(metaclass=ABCMeta):
    def __init__(self):
        super().__init__()
        self.rows = None
        self.can_generate = True
        self._temp_file = NamedTemporaryFile('r+', delete=False)
        self.list_is_unique = True

    def __del__(self):
        import os
        if not self._temp_file.closed:
            self._temp_file.close()
        os.unlink(self._temp_file.name)

    def init_list(self):
        self.rows = ASList(unique=self.list_is_unique)

    @property
    @abstractmethod
    def type_name(self):
        """ Must return the type name as is in SQL (e.g: VARCHAR(30) NOT NOLL...) """
        pass

    @abstractmethod
    def gen(self):
        """ Must generate the random data. If can_generate is set true then gen must do nothing. """
        pass

    def dump_line(self):
        """ Get one item from the temporary file. If serialize() has not called before then the file will be empty """
        return self._temp_file.readline().rstrip()

    def serialize(self):
        """ Save the data in a temporary file, one per line.
        The file will be removed when the instance of this class get collected by the garbage collector
        """
        self._temp_file.writelines([f"{str(item)}\n" for item in self.rows])
        self._temp_file.flush()
        self._temp_file.seek(0)  # reset position for when dump_line be called
Exemplo n.º 15
0
class FileStore(object):

   def __init__(self, proxy, processId, mode= 'r'):
      '''
      Gives r+w access to file on static instance via local tempfile.NamedTemproraryFile
      '''
      self.proxy= proxy
      self.processId= processId
      self.mode= mode

      self.tmpFile= NamedTemporaryFile(mode= 'r+', prefix= self.processId, delete= True)
      if 'r' in mode:
         self.__get__()

   def __get__(self):
      '''
      Get contents of static instance file and save to local temp file
      '''
      data= self.proxy.getFileContents(self.processId)
      self.tmpFile.write(data.tostring())
      self.tmpFile.seek(0)

   def __post__(self):
      '''
      Posts contents of local temp file to static instance file
      '''
      self.tmpFile.seek(0)
      data= self.tmpFile.read()
      self.proxy.setFileContents(self.processId, data)
      self.tmpFile.seek(0)

   def getName(self):
      return self.processId

   def getLocalName(self):
      return self.tmpFile.name

   def write(self, data):
      '''
      Writes data to local tempfile
      '''
      if 'w' not in self.mode:
         raise Exception('file open for read only')

      self.tmpFile.write(dumps(data))

   def read(self, size= -1):
      '''
      Reads data from local tempfile.  See file read() for more details.
      '''
      if 'r' not in self.mode:
         raise Exception('file open for write only')
  
      return loads(self.tmpFile.read(size))

   def readlines(self):
      '''
      Reads lines from local tempfile.  See file readlines() for more detals.
      '''
      if 'r' not in self.mode:
         raise Exception('file open for write only')

      return loads(self.tmpFile.readlines())

   def readline(self):
      '''
      Reads line from local tempfile. See file readline() for more details.
      '''
      if 'r' not in self.mode:
         raise Exception('file open for write only')

      return loads(self.tmpFile.readline())
 
   def close(self, delete= False):
      '''
      Saves the contents of the local tempfile and then closes/destroys the local tempfile.  See self.__post__() and python tempfile for more details.
      '''

      if 'w' in self.mode:
         self.__post__()

      elif 'r' in self.mode:

         # if delete requested -- remove file form static instance
         if delete:
            self.proxy.deleteFile(self.processId)

      self.tmpFile.close()