Skip to content

Commit

Permalink
Better endpoint management, avoiding using global settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed May 26, 2013
1 parent f7d5a2b commit 46177a9
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 22 deletions.
6 changes: 5 additions & 1 deletion Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[
{
"caption": "Run SPARQL Query",
"caption": "SPARQL: Run query",
"command": "run_sparql"
},
{
"caption": "SPARQL: Select endpoint",
"command": "select_sparql_endpoint"
}
]
34 changes: 34 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "SPARQLRunner",
"children":
[
{
"command": "open_file",
"args": {"file": "${packages}/SPARQLRunner/SPARQLRunner.sublime-settings"},
"caption": "Settings – Default"
},
{
"command": "open_file",
"args": {"file": "${packages}/User/SPARQLRunner.sublime-settings"},
"caption": "Settings – User"
}
]
}
]
}
]
}
]
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@ A Sublime Text 2/3 plugin to run SPARQL queries inside Sublime.
Installing
----------

* ~~Use Sublime Package Control to install it;~~
* Clone this repository inside your packages directory (`~/Library/Application Support/Sublime Text 3/Packages/` on OSX);
* Add the following config to your Preferences.sublime-settings file:

```
{
...
"sparql_endpoint": "http://dbpedia.org/sparql"
...
}
```
* Use Sublime Package Control to install it (or clone it inside your Packages dir);


Using
-----

Simply run it from the command palette. SPARQL Runner will consider either the **selected text** or the **entire file** as the SPARQL query.
* To add a new endpoint or select the current one open the command palette and choose `SPARQL: Select endpoint`
* To run a query choose `SPARQL: Run query`. SPARQL Runner will run the query against the current endpoint. It will consider either the **selected text** or the **entire file** as the SPARQL query.

If you want to add a key binding, open your "Default.sublime-keymap" and add:
If you want to add a key binding to run queries, open your "Default.sublime-keymap" and add:

[
{ "keys": ["super+shift+k"], "command": "run_sparql" }
]


* Further config options can be found in Preferences -> Package Settings -> SPARQL Runner -> Settings
86 changes: 78 additions & 8 deletions SPARQLRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
('fn:', 'http://www.w3.org/2005/xpath-functions#')
]
PREFIX_REGEX = re.compile(r'^\s*prefix\s+(.*?)\s+<(.*?)>\s*$', re.MULTILINE | re.IGNORECASE)
SETTINGS_FILE = 'SPARQLRunner.sublime-settings'


class QueryRunner(threading.Thread):
def __init__(self, server, query):
self.server = server
self.query = query
self.result = None
super(QueryRunner, self).__init__()

def parse_prefixes(self):
Expand Down Expand Up @@ -95,7 +97,6 @@ def run(self):
except Exception as e:
err = '%s: Error %s running query' % (__name__, str(e))
sublime.error_message(err)
self.result = None


class RunSparqlCommand(sublime_plugin.TextCommand):
Expand All @@ -118,28 +119,97 @@ def handle_thread(self, thread, i=0):
sublime.set_timeout(lambda: self.handle_thread(thread, (i + 1) % len(PROGRESS)), 100)
return

self.view.erase_status('sparql_query')

if not thread.result:
return

sublime.status_message('Query successfully run on %s' % thread.server)
self.view.erase_status('sparql_query')
new_view = self.view.window().new_file()
new_view.settings().set('word_wrap', False)
new_view.set_name("SPARQL Query Results")
new_view.run_command('insert', {
'characters': thread.result
})
new_view.set_scratch(True)
new_view.set_read_only(True)
new_view.set_name("SPARQL Query Results")
new_view.settings().set('word_wrap', False)

def run(self, edit):
settings = self.view.settings()
server = settings.get('sparql_endpoint')
if (not server) or len(server) == 0:
sublime.error_message("You should add 'sparql_endpoint' setting to your preferences file.")
self.settings = sublime.load_settings(SETTINGS_FILE)
server = self.settings.get('current_endpoint', None)
if not server:
sublime.error_message("You should add/select an endpoint using 'SPARQL: Select endpoint' command.")
return

query = self.get_selection() or self.get_full_text()
query_thread = QueryRunner(server, query)
query_thread.start()
self.handle_thread(query_thread)


class SelectSparqlEndpointThread(threading.Thread):

def __init__(self, window):
super(SelectSparqlEndpointThread, self).__init__()
self.window = window

def gather_endpoints(self):
self.settings = sublime.load_settings(SETTINGS_FILE)
self.current_endpoint = self.settings.get('current_endpoint', None)
self.sparql_endpoints = self.settings.get('sparql_endpoints', [])

self.endpoints = [
['Add new endpoint...', ''],
]

for endpoint in self.sparql_endpoints:
url = endpoint['url']
name = endpoint['name']
if url == self.current_endpoint:
name = "*%s" % name
self.endpoints.append([name, url])

def add_endpoint(self, name, url):
self.settings.set('sparql_endpoints', self.sparql_endpoints + [
{
'name': name,
'url': url
}
])
self.set_as_current(url)

def set_as_current(self, url):
self.settings.set('current_endpoint', url)
sublime.save_settings(SETTINGS_FILE)

def run(self):
self.gather_endpoints()
self.window.show_quick_panel(self.endpoints, self.on_panel_select_done)

def on_panel_select_done(self, selected):
if selected < 0:
return

if selected == 0:
self.window.show_input_panel('Endpoint name', '', self.on_name_done, self.on_change, self.on_cancel)
return
self.set_as_current(self.endpoints[selected][1])

def on_name_done(self, name):
self.name = name
self.window.show_input_panel('Endpoint url', '', self.on_url_done, self.on_change, self.on_cancel)

def on_url_done(self, url):
self.add_endpoint(self.name, url)

def on_change(self, name):
pass

def on_cancel(self):
pass


class SelectSparqlEndpointCommand(sublime_plugin.WindowCommand):

def run(self):
SelectSparqlEndpointThread(self.window).start()
9 changes: 9 additions & 0 deletions SPARQLRunner.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"sparql_endpoints": [
{
"name": "dbpedia",
"url": "http://dbpedia.org/sparql"
}
],
"current_endpoint": ""
}

0 comments on commit 46177a9

Please sign in to comment.