Skip to content

Instantly share code, notes, and snippets.



Created Aug 6, 2015
What would you like to do?
# coding: utf-8
# If you click write to file, a directory called
# _temp_dir_name will be created in your root dir
# i think the normal temp/tmp dirs are invisible
# in pythonista. i refer to as a temp dir as in
# my case, i only download code from the forum
# to test it. i want it all to go to one dir.
# easier to clean up
# Something wrong with the clipboard code. I have
# hidden the button with a constant below until i
# can workout why i am corrupting the clipboard.
# at least least, i think thats what is happening
# if this is set to true, the file created will
# be opened in the editor. According to the docs
# the current file is automatically saved
import ui
import clipboard, console, editor
import os, time
import tempfile
import requests
import bs4
# name of the directory that will be used to save
# the code files if you write to disk. if the dir
# does not exist it will be created.
_temp_dir_name = 'forum_temp/'
# the full path of the temp dir, saving here!
__TEMP_DIR__ = os.getenv('HOME')+'/Documents/' + _temp_dir_name
class OmzFourmCopyUI(ui.View):
def __init__(self, data_list, post_title):
#the list of code blocks, scraped from the forum = data_list = post_title
# create acopy title btn
# using the var below to hide it. i think
# i could be corrupting the clipboard somehow
btn = ui.ButtonItem('Clipboard')
btn.action = self.copy_code
self.left_button_items = [btn]
# create copy to file btn
btn = ui.ButtonItem('Write to File')
btn.action = self.write_to_File
self.right_button_items = [ btn]
# create the tableview, set the data_source
# and delegate to self.
tv = ui.TableView(name = 'tv1') = 'tv1'
tv.data_source = self
tv.border_width = .5
tv.delegate = self
#create textview
txtv = ui.TextView(name = 'txtv')
txtv.font = ('Courier', 14)
# select the first code block
tv.selected_row = 0
txtv.text =[0]
# copy the selected code block to the clipboard
def copy_code(self, sender):
if self['txtv'].text:
code =[self['tv1'].selected_row[1]]
console.hud_alert('Copied', duration = .5)
# write the selected code block to a file
# tempfile is used, delete = False
# a var at the top of this file determines
# if the file is opened after its written
def write_to_File(self, sender):
if not self['txtv'].text:
console.hud_alert('No text to write to file', duration = 2)
# create a prefix for the file, using local
# time of the device
the_prefix = time.strftime('%X', time.localtime()).replace(':', '_') + '_'
# if no directory exits, create one
if not os.path.exists(__TEMP_DIR__):
# create and return a ref to the new file
fh = tempfile.NamedTemporaryFile(dir = __TEMP_DIR__, prefix = the_prefix, suffix = '.py', delete = False)
code =[self['tv1'].selected_row[1]]
fh.write(code) # write the code to the file
fspec =
fname ='/')[-1:][0]
#close the view
console.hud_alert('written to file {}'.format(fname, _temp_dir_name), duration = 2)
# handle the resizing more correctly.
def layout(self):
self.frame = self.bounds
tv = self['tv1']
tv.frame = (0,0,self.width * .20, self.height)
self['txtv'].frame = (tv.width, 0, self.width - tv.width, self.height)
# tableview data_source callback methods
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell()
cell.text_label.text = 'Code {}'.format(row + 1)
return cell
def tableview_number_of_rows(self, tableview, section):
# Return the number of rows in the section
return len(
# tableview delegate callback method
def tableview_did_select(self, tableview, section, row):
self['txtv'].text =[row]
def extract_code_blocks(url):
# code from omz as a quickie, i made some mods
lst = []
html = requests.get(url).text
soup = bs4.BeautifulSoup(html)
pre_tags = soup.find_all('pre')
if pre_tags:
for p in pre_tags:
title = ''
if len(lst):
# extract the title of the post
title = soup.title.get_text()
title = title.split('—')[-1:][0].lstrip(' ')
# if no code block found, lst will be an empty
# list and title will be ''
return (lst, title)
if __name__ == '__main__':
url_fail = True
# copy a url from the clipboard
# url should contain a link to a omz forum
# that you want to extract code from.
url = clipboard.get()
#check the url
if '' not in url:
console.alert('omz-forums, not in url')
elif len(url.splitlines()) > 1:
console.alert('url, has multiple lines')
# if the url looks ok, try and get
# a list of code blocks from the
# omz fourm
url_fail = False
result = extract_code_blocks(url)
lst , post_title = result
if not url_fail:
if len(lst) == 0:
# url appeared ok, but no code blocks found
console.alert('No code found on page')
x = OmzFourmCopyUI(lst, post_title).present()
# if the url does not pass the checks
# print the url to the console
print 'Clipboard Contents - {}'.format(url)

This comment has been minimized.

Copy link

@cclauss cclauss commented Aug 6, 2015

if len(lst):can be rewritten as if lst:.

if len(lst) == 0:can be rewritten if not lst:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment