-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kunihiko Kido
committed
May 19, 2015
1 parent
47043c6
commit 91c5e2a
Showing
5 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sublime | ||
import sublime_plugin | ||
import csv | ||
import io | ||
import json | ||
|
||
|
||
class CsvConvertBulkFormatCommand(sublime_plugin.TextCommand): | ||
|
||
def new_file(self, title=''): | ||
view = sublime.active_window().new_file() | ||
view.set_name('**Bulk {} Data**'.format(title)) | ||
view.set_scratch(True) | ||
view.set_syntax_file('Packages/JavaScript/JSON.tmLanguage') | ||
return view | ||
|
||
def run(self, edit, action='index'): | ||
|
||
text = self.view.substr(sublime.Region(0, self.view.size())) | ||
|
||
output_view = self.new_file(action) | ||
|
||
for doc in csv.DictReader(io.StringIO(text.strip())): | ||
bulk_action = {action: {}} | ||
|
||
if 'id' in doc: | ||
bulk_action[action]['_id'] = doc['id'] | ||
|
||
data = [] | ||
data.append(json.dumps(bulk_action)) | ||
|
||
if action in ('index', 'create'): | ||
data.append(json.dumps(doc, ensure_ascii=False)) | ||
elif action == 'update': | ||
data.append(json.dumps({'doc': doc}, ensure_ascii=False)) | ||
|
||
output_text = '\n'.join(data) + '\n' | ||
|
||
output_view.insert( | ||
edit, output_view.size(), output_text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters