Grabbing Text from Sublime Text Regions - sublime-text-plugin

I'm new to creating Sublime Text 3 Plugins and I'm trying to understand the Plugin API that's offered. I want to be able to "grab" text I highlight with my mouse and move it somewhere else. Is there a way I can do this using the Sublime Text Plugin API?
So far, all I've done is be able to create a whole region:
allcontent = sublime.Region(0, self.view.size())
I've tried to grab all of the text in the region and put it into a lot file:
logfile = open("logfile.txt", "w")
logfile.write(allcontent)
But unsuccessfully of course as the log file is blank after it runs.
I've looked over google and there is not a lot of documentation, except for the unofficial documentation, in which I can't find a way to grab the text. Nor are there many tutorials on this.
Any help is greatly appreciated!

A Region just represents a region of text (i.e. from position 0 to position 10), and isn't tied to any specific view.
To get the underlying text from the view's buffer, you need to call the view.substr method with the region as a parameter.
import os
logpath = os.path.join(sublime.cache_path(), 'logfile.txt')
allcontent = self.view.substr(sublime.Region(0, self.view.size()))
with open(logpath, 'w') as logfile:
logfile.write(allcontent)
print('written buffer contents to', logpath)
To get the region represented by the first selection, you can use self.view.sel()[0] in place of sublime.Region(0, self.view.size()).

Related

I am using aws textract StartDocumentTextDetectionCommand and GetDocumentTextDetectionCommand. I want only lines to be returned, not the single words

I am creating an OCR internal tool using aws textract and nodejs to detect text from a scanned pdf, specifically StartDocumentTextDetectionCommand and GetDocumentTextDetectionCommand. Currently returned in a list of block objects with the lines first and then starts detecting each word by word. Is there any way for me to add in a parameter or something where it will just return the lines for me and not the word by word in the pdf.
I would suggest to use the Amazon Textract Textractor library pip install amazon-textract-textractor
It makes parsing and using the Textract output much easier than the raw JSON.
from textractor import Textractor
extractor = Textractor(profile_name="default")
document = extractor.detect_document_text('test.png')
print(document.lines)
No, this is not possible. There are multiple block types, lines link to words via relationships.
Is there some reason why you cannot simply select only the block types you are interested in (lines)?
Response will always contain the lines and words. But you can iterate the response['Blocks'] and find only the blocks with BlockType == 'LINES'.
Eg. below:
for block in response["Blocks"]:
if block["BlockType"] == "LINE":
print(block)

Read-in df from csv before launching main app | Dash

I am trying to get my first dashboard with python dash running.
The whole thing is very similar to this https://github.com/dkrizman/dash-manufacture-spc-dashboard.
At the beginning a Dataframe is read in from a csv. My problem seems to be quite easy to solve but somehow I am not succeeding:
I want to create a initial window that allows the user to select (from e.g. dropdown) the csv file (or accordingly the path) that is read in. All the .csv files look the same but just have different values.
When using the modal components I get problems with the install of bootstrap and I thought there must be an easier way?
Thanks for your help!
Best,
Nik

Is there a way to copy Google Earth place names as text?

I'm new to python and doing a work project that involves juggling a lot (5-10k) of "places" (polygons representing regions) in google earth. As such, I wanted to run a list compare between the places I have in google earth against a txt file list of places I should have. The only problem is that I cannot seem to find a way to copy paste or otherwise capture the name text of the google earth places. Copying with control c or right click copy copies them as a KMZ file, or when pasted into a text editor gives the full source from their "properties" tab. I'm fairly confident in manipulating and comparing the lists once I have the data in that format, but could really use some help in attaining it as such.
First right-click on saved places in Google Earth and save as KML (text) file.
Next, you can use Python to extract the place names from the KML file.
Here's some sample code using pykml module to parse out the place names.
from pykml import parser
with open('places.kml') as f:
root = parser.parse(f).getroot()
# iterate over each placemark
for pm in root.Document.Placemark:
name = pm.name.text
print(name)

How do I create text file for Confluence and then import it as a Confluence wiki page?

Here is what I want to accomplish:
I am writing a script which will parse some source code, extract some comments that I want it to extract and I will store this text in a text file.
I want to write another script that uses the content of this text file to be programatically transformed into a Confluence wiki-page.
Please tell me the best way to do this. I already saw this
I felt that I could change the input in the above example to take input from text file and update contents of Confluence page. But, I am not sure how it will be formatted. If I have to format it, what do I need to do?
Thanks in advance!
As an alternative to XML-RPC you can use the integrated WebDAV plugin.
Write a script that creates a directory in the selected space.
The directory name will be the page name. After creating the directory a text-file with the same name (with .txt extension) will be created in the directory which holds the content of the page
let your script edit this file in insert the content of your text-file.
Information about the usage of the plugin:
Configuring a WebDAV client for Confluence
Confluence WebDAV Plugin
Troubleshooting WebDAV
I Also saw the code you are referring to. I tweaked it a bit a produced the following code that me help you for the second part of your question.
My code creates a new space and also a new page from a text file that is inserted in the previously created space.
import sys
import xmlrpc.client
import os
import re
# Connects to confluence server with username and password
site_URL = "YOUR_URL"
server = xmlrpc.client.ServerProxy(site_URL + "/rpc/xmlrpc")
username = "YOUR_USERNAME"
pwd = "YOUR_PASSWORD"
token = server.confluence2.login(username, pwd)
# The space you want to add a page to
spacekey = "YOUR_SPACENAME"
# Retrives text from a file
f = open('FileName.txt', 'r')
content = f.read()
f.close()
# Creates a new page to insert in the new space from text file content
newpage = {"title":"NEW_PAGENAME", "space":spacekey, "content":content}
server.confluence2.storePage(token, newpage)
server.confluence2.logout(token)
For your formatting issues, html is supported, but I have not quite figured out how to use CSS styles other than inline (everthing else does not seem to work).
These styles work when you write them with the HTML macro inside Confluence, but from the remote API, it does not seem to behave the same way.
UPDATE :
You can use the {html} macro to include your html by using :
content = server.confluence2.convertWikiToStorageFormat(token, content)
You can also specify your CSS in your global CSS stylesheet.
Another option is to develop a plugin to include a CSS resource :
https://developer.atlassian.com/display/CONFDEV/Creating+a+Stylesheet+Theme
I have tried this way and works petty well for me:
I used a Java program to created a programmatic client to create the dynamic content. This created a HTML document out of my plain text.
I used a RPC client connected to Confluence to uploaded it as a new page.
Your html will be preserved. But if you want to add CSS or JS/JQuery etc on top of your html you will need to create a Macro and enable it for the Particular page. This feature is not available in Confluence OnDemand.

Create or Change multipage TIFFs

I try to compose new and modify existing multipage TIFFs using Magick++.
Does someone know how I can do this?
I can read a specific page using this code:
Image * img = new Image("path/to/image.tif[0]"); //read page 0
But how can I save changes back to the TIFF? and how can I add new pages?
Google could only tell me things about splitting TIFFs in singe page ones.
Thanks for your help!
I never tried it but what about the writeImages function. Docs are here.
From the docs
Write images in container to file specified by string imageSpec_