def update_image(self): image_type, width, height = getimageinfo.getImageInfo(self.file) self.mimetype = image_type self.image_size = '%d,%d' % (width, height) width, height = self.getRoot().thumbnail_size thumbnail = images.resize(self.file, width, height, images.JPEG) self.image_thumbnail = db.Blob(thumbnail) image_type, width, height = getimageinfo.getImageInfo(thumbnail) self.thumbnail_size = '%d,%d' % (width, height)
def get_sizes(url): try: opened_file = urllib2.urlopen(url) except: print "the image url could not be opened" return 0, 0, 0 try: image_type, width, height = getimageinfo.getImageInfo(opened_file) except: print "the image info could not be found" return 0, 0, 0 try: #print opened_file.headers.get("content-length") size = int(opened_file.headers.get("content-length")) except: try: read_file = opened_file.read() size = int(len(read_file)) except: size = 0 print "the file size could not be found" opened_file.close() return size, width, height
def post(self): ## Identify desired template template_name = "edit.html" # Construct the path to the template directory = os.path.dirname(__file__) path = os.path.join(directory, 'templates', template_name) user = users.get_current_user() # if not self.user: # self.redirect(users.create_login_url(self.request.uri)) product = db.get(self.request.get("id")) msg = None if users.get_current_user(): author = users.get_current_user() product.author = author if self.request.get("delete"): delete = self.request.get("delete") logging.info('DEBUG: Permanently deleting item: %s' % product.title) msg = "Product deleted: %s" % product.title product.delete() else: if self.request.get("available"): description = self.request.get("available") logging.info('DEBUG: avilable == %s' % str(description)) product.available = True else: product.available = False if self.request.get("title"): title = self.request.get("title") product.title = title if self.request.get("description"): description = self.request.get("description") product.description = description if self.request.get("price"): price = float(self.request.get("price")) product.price = price if self.request.get("image"): image = self.request.get("image") imageblob = db.Blob(image) imagetype = getImageInfo(image) product.image = image product.put() msg = "New product uploaded!" self.redirect('/admin/edit')
def addImage(mime,description,bf,name): 'Add Image' image=Images(mime=mime,description=description,bf=bf,name=name) image.size=len(image.bf) image.filetype,image.width,image.height=getImageInfo(bf) image.put() return image
def get(self, key): import getimageinfo theme = db.get(key) # image = images.Image(theme.BackgroundImage) info = getimageinfo.getImageInfo(theme.BackgroundImage) self.response.out.write("info : %s, %s, %s" % info)
def post(self): #check login if not users.get_current_user(): self.redirect(users.create_login_url('/')) img = self.request.get('img') content_type, width, height = getImageInfo(img) # input validation if not img or not content_type: self.redirect('/') return # delete previous image, if exists user = users.get_current_user() query = db.GqlQuery("SELECT * FROM Image WHERE user = :1", user) for image in query: db.delete(image) image = Image() image.user = user image.data = db.Blob(img) if content_type == "image/jpeg": image.content_type = "image/jpeg" image.output_encoding = images.JPEG else: # GIFs, etc. get converted to PNG since only JPEG and PNG are currently supported image.content_type = "image/png" image.output_encoding = images.PNG image.width = width image.height = height image.put() self.redirect('/')
def DetermineImgSize(self, url): try: notValid = False file = urllib2.urlopen(url) codeStr = str(file.getcode()) if codeStr == "200": infoDict = file.info().items() size = None for key,value in infoDict: if key == "content-length": size = int(value) break #self.safePrint(size) if size and size > 999999: #more than 1mb notValid = True else: #headers={"Range": "bytes=0-100"} r = requests.get(url) content_type, width, height = getimageinfo.getImageInfo(r.content) if width >= 1000 or height >= 1000: notValid = True return notValid except urllib2.URLError, e: self.safePrint(e.reason) return False
def addImage(mime, description, bf, name): 'Add Image' image = Images(mime=mime, description=description, bf=bf, name=name) image.size = len(image.bf) image.filetype, image.width, image.height = getImageInfo(bf) image.put() return image
def run(self, edit): view = self.view view.run_command("commit_completion") sel = view.sel()[0].a if not 'html' in view.scope_name(sel): return scope = view.extract_scope(sel - 1) tag_scope = view.extract_scope(scope.a - 1) path = view.substr(scope) if path.startswith(("'", "\"", "(")): path = path[1:-1] path = path[path.rfind('/'):] if '/' in path else '' full_path = self.this_dir + path if '<img' in view.substr(tag_scope) and path.endswith(('.png', '.jpg', '.jpeg', '.gif')): with open(full_path, 'rb') as r: read_data = r.read( ) if path.endswith(('.jpg', '.jpeg')) else r.read(24) con_type, w, h = getImageInfo(read_data) if self.get_setting('afn_insert_width_first', view): self.insert_dimension(edit, h, 'height', tag_scope) self.insert_dimension(edit, w, 'width', tag_scope) else: self.insert_dimension(edit, w, 'width', tag_scope) self.insert_dimension(edit, h, 'height', tag_scope)
def run(self, edit): view = self.view view.run_command("commit_completion") sel = view.sel()[0].a if not 'html' in view.scope_name(sel): return scope = view.extract_scope(sel-1) tag_scope = view.extract_scope(scope.a-1) path = view.substr(scope) if path.startswith(("'","\"","(")): path = path[1:-1] path = path[path.rfind('/'):] if '/' in path else '' full_path = self.this_dir + path if '<img' in view.substr(tag_scope) and path.endswith(('.png','.jpg','.jpeg','.gif')): with open(full_path,'rb') as r: read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24) con_type, w, h = getImageInfo(read_data) if self.get_setting('afn_insert_width_first',view): self.insert_dimension(edit,h,'height',tag_scope) self.insert_dimension(edit,w,'width',tag_scope) else: self.insert_dimension(edit,w,'width',tag_scope) self.insert_dimension(edit,h,'height',tag_scope)
def addImage(name, mime,description,tag,bf): 'Add Image' image=Images(name=name, mime=mime,description=description,tag=tag.split(','), bf=bf) image.size=len(image.bf) image.filetype,image.width,image.height=getImageInfo(bf) image.put() AddTags(image.tag) return image
def fix_dir(self,sdir,fn): if fn.endswith(('.png','.jpg','.jpeg','.gif')): path = os.path.join(sdir, fn) with open(path,'rb') as r: read_data = r.read() if path.endswith(('.jpg','.jpeg')) else r.read(24) con_type, w, h = getImageInfo(read_data) return fn+'\t'+'w:'+ str(w) +" h:" + str(h) return fn
def addImage2(bf): image = Images(bf=bf) image.size = len(bf) image.filetype, image.width, image.height = getImageInfo(bf) if not image.filetype: return None image.mime = image.filetype image.put() return image
def addImage2(bf): image=Images(bf=bf) image.size=len(bf) image.filetype,image.width,image.height=getImageInfo(bf) if not image.filetype:return None image.mime=image.filetype image.put() return image
def process_item(self, item, orig_item = None): if isinstance(item, dict) and "subpage" in item: item["subpage"] = self.process_item(item["subpage"]) if type(item) in (str,unicode) and os.path.exists(item) and not os.path.isdir(item): if not orig_item: orig_item = {} newitem = {"type": get_file_type(item), "mime": get_mimetype(item)} if "width" in orig_item: newitem["width"] = orig_item["width"] if "height" in orig_item: newitem["height"] = orig_item["height"] if newitem["type"] == "image" and ("width" not in newitem or "height" not in newitem): content_type, width, height = getImageInfo(open(item).read()) if content_type and width and height: try: if "width" not in newitem and "height" not in newitem: newitem["width"] = width newitem["height"] = height elif "width" not in newitem: newitem["width"] = width * newitem["height"] / height else: newitem["height"] = height * newitem["width"] / width except TypeError: pass newitem["rawpath"] = item if self.params["WEB_ROOT"] in item: newitem["url"] = item.replace(self.params["WEB_ROOT"], self.params["WEB_URL"]) elif self.params["copy_images"]: new_name = item.replace(os.sep, "_") new_path = os.path.join(self.params["target_dir"], "imgs", new_name) if not DEBUG and (not os.path.exists(new_path) or (os.path.getmtime(item) > os.path.getmtime(new_path))): shutil.copy(item, new_path) newitem["rawpath"] = new_path newitem["url"] = os.path.join("imgs", new_name) else: newitem["url"] = item.replace(self.params["target_dir"], "") return newitem elif isinstance(item, dict) and "type" in item: if item["type"] in ("image", "video"): processed = self.process_item(item["url"], item) if isinstance(processed, dict): item.update(processed) else: item["url"] = processed if "popup" in item and "rawpath" in item: item["fullurl"] = item["url"] item["url"] = make_thumbnail(item) item["type"] = "imagegallery" if item["popup"] == "gallery" else "imagepopup" return item elif item["type"] == "stack": item["stack"] = self.process_items(item["stack"]) return item elif item["type"] in item_processors: return item_processors[item["type"]](item, self.params) else: return item elif isinstance(item, list): return self.process_items(item) else: return item
def getNonImgurImageInfo(self, url): try: hdr = {'User-Agent': 'Mozilla/5.0'} req = urllib2.Request(url,headers=hdr) imgdata = urllib2.urlopen(req) image_type,width,height = getimageinfo.getImageInfo(imgdata) return (image_type,width,height) except Exception,e: print e return (None, -1, -1)
def addImage(title,bf,referer): 'Add Image' image=Images(description=title,bf=bf,referer=referer) image.size=len(image.bf) image.filetype,image.width,image.height=getImageInfo(bf) if not image.filetype:return None image.mime=image.filetype image.put() return image
def addImage(title, bf, referer): 'Add Image' image = Images(description=title, bf=bf, referer=referer) image.size = len(image.bf) image.filetype, image.width, image.height = getImageInfo(bf) if not image.filetype: return None image.mime = image.filetype image.put() return image
def test_should_return_thumbnail_image_if_it_exists(self): photo_path = os.path.join(config.photo_dir, 'IMG_6868.jpg') thumbnail_path = os.path.join(config.thumbnail_dir, 'IMG_6868.jpg') shutil.copy2(photo_path, thumbnail_path) thumbnail = Thumbnail('/IMG_6868.jpg') thumbnail_path = thumbnail.get_path() with open(thumbnail_path, 'rb') as photo_file: data = photo_file.read(81000) self.assertEquals(getImageInfo(data), ('image/jpeg', 800, 600))
def addImage(mime,description,bf): 'Add Image' image=Images(mime=mime,description=description) image.size=len(image.bf) image.filetype,image.width,image.height=getImageInfo(bf) image.put() imageblob = ImageBlob(image=image, bf=bf) imageblob.put() prependToRSS([image]) return image
def test_should_recreate_thumbnail_if_older_than_photo(self): photo_path = os.path.join(config.photo_dir, 'wilderness-01.jpg') thumbnail_path = os.path.join(config.thumbnail_dir, 'wilderness-01.jpg') shutil.copy2(photo_path, thumbnail_path) os.utime(thumbnail_path, (947374366, 947374366)) thumbnail = Thumbnail('/wilderness-01.jpg') thumbnail_path = thumbnail.get_path() with open(thumbnail_path, 'rb') as photo_file: data = photo_file.read(81000) self.assertEquals(getImageInfo(data), ('image/jpeg', 300, 168))
def post(self): #check login if not users.get_current_user(): self.redirect(users.create_login_url('/')) user = users.get_current_user() query = db.GqlQuery("SELECT * FROM Image WHERE user = :1", user) saved_image = query.get() action = self.request.get("action") if saved_image and action: if action == "resize": width = self.request.get("width") height = self.request.get("height") if width and height: img = images.resize(saved_image.data, int(width), int(height), saved_image.output_encoding) elif action == "rotate": degrees = self.request.get("degrees") if degrees: img = images.rotate(saved_image.data, int(degrees), saved_image.output_encoding) elif action == "im_feeling_lucky": img = images.im_feeling_lucky(saved_image.data, saved_image.output_encoding) elif action == "crop": left_x = self.request.get("left_x") top_y = self.request.get("top_y") right_x = self.request.get("right_x") bottom_y = self.request.get("bottom_y") if left_x and top_y and right_x and bottom_y: img = images.crop(saved_image.data, float(left_x), float(top_y), float(right_x), float(bottom_y), saved_image.output_encoding) if img: # save new settings output_encoding = saved_image.output_encoding content_type, width, height = getImageInfo(img) # delete previous entity for image in query: db.delete(image) image = Image() image.user = user image.data = db.Blob(img) image.output_encoding = output_encoding image.content_type = content_type image.width = width image.height = height image.put()
def addImage2(bf): image=Images() image.size=len(bf) image.filetype,image.width,image.height=getImageInfo(bf) if not image.filetype:return None image.mime=image.filetype image.put() imageblob = ImageBlob(image=image, bf=bf) imageblob.put() prependToRSS([image]) return image
def test_should_create_thumbnail_image_if_it_doesnt_exist(self): thumbnail = Thumbnail('subdir with spaces/lunch_at_dave-1.jpg') thumbnail_path = thumbnail.get_path() path = os.path.join(config.thumbnail_dir, 'subdir with spaces/lunch_at_dave-1.jpg') norm_path = os.path.abspath(os.path.normpath(path)) self.assertEqual(thumbnail_path, norm_path) self.assertTrue(os.path.exists(thumbnail_path)) with open(thumbnail_path, 'rb') as photo_file: data = photo_file.read(81000) self.assertEquals(getImageInfo(data), ('image/jpeg', 300, 199))
def AddPhoto(name, description, mime, album, user, stream, imgurl=''): 'Add Photo' photo = model.Photo() photo.Album = album photo.Author = user photo.Description = description photo.Mime = mime photo.Name = name photo.PhotoStream = None photo.Size = len(stream) photo.FileType, photo.Width, photo.Height = getImageInfo(stream) photo.imgurl = imgurl photo.Save() memcache.delete('ALLALBUMS') return photo
def post(self): ## Identify desired template template_name = "upload.html" # Construct the path to the template directory = os.path.dirname(__file__) path = os.path.join(directory, 'templates', template_name) user = users.get_current_user() if not self.user: self.redirect(users.create_login_url(self.request.uri)) msg = None author = None if users.get_current_user(): author = users.get_current_user() description = self.request.get("description") title = self.request.get("title") price = float(self.request.get("price")) image = self.request.get("image") imageblob = db.Blob(image) imagetype = getImageInfo(image) product = Product(author=author, title=title, description=description, price=price, image=imageblob, imagetype=imagetype[0]) product.put() msg = "New product uploaded!" body = None values = { 'page_title': "ADMIN -- Welcome to Nothing Tees!", 'author': "THIS IS __author__", 'version': 1, 'body': body, 'msg': msg, } self.response.out.write(template.render(path, values))
def AddImageByUrlBak(url,fileName,tag): result = urlfetch.fetch(url) if result.status_code == 200: image=Images(description=url,bf=result.content) image.mime=result.headers.get('Content-Type', '') if image.mime.find('image')==-1: return None image.size=len(image.bf) image.name=fileName image.filetype,image.width,image.height=getImageInfo(image.bf) image.tag=tag.split(',') image.put() AddTags(image.tag) return image else: return None
def storeRes(self, src, filename, image=None): src = re.sub("-rw$", "", src) src = re.sub("\d*$", "9999", src) data = urllib.urlopen(src).read() #check image size/type if image != None: type, width, height = getImageInfo(data) type = re.sub("image/", "", type) filename += type f = open(filename, "wb") f.write(data) f.close() self.filelist.append(filename) if image != None: return (width, height, filename)
def post(self): ## Identify desired template template_name = "upload.html" # Construct the path to the template directory = os.path.dirname(__file__) path = os.path.join(directory, 'templates', template_name) user = users.get_current_user() if not self.user: self.redirect(users.create_login_url(self.request.uri)) msg = None author = None if users.get_current_user(): author = users.get_current_user() description = self.request.get("description") title = self.request.get("title") price = float(self.request.get("price")) image = self.request.get("image") imageblob = db.Blob(image) imagetype = getImageInfo(image) product = Product(author = author, title = title, description = description, price = price, image = imageblob, imagetype = imagetype[0]) product.put() msg = "New product uploaded!" body = None values = { 'page_title': "ADMIN -- Welcome to Nothing Tees!", 'author': "THIS IS __author__", 'version': 1, 'body': body, 'msg': msg, } self.response.out.write(template.render(path, values))
def search(self, search): g = GoogleSearch(search) g.results_per_page = 50 g.page = 1 searchNot = self.mapKeeper.searchNot(search.replace(' ', '_')) results = g.get_results() print "number of results: ", len(results) for res in results: url = res.url.encode("utf8") base_url = url req = urllib2.Request(url,headers=self.hdr) try: response = urllib2.urlopen(req) print "Processing: ", url except (UnicodeEncodeError, urllib2.HTTPError, urllib2.URLError, socket.error, httplib.BadStatusLine), e: print url+": ", e continue page = BeautifulSoup(response, "lxml") images = page.select('img[alt]') for image in images: if search in image.get('alt').lower(): imageURL = image.get('src') imageURL = urlparse.urljoin(base_url, imageURL) if imageURL in searchNot: print "Image is in searchNot: ", imageURL continue try: imgdata = urllib2.urlopen(imageURL) except urllib2.HTTPError, e: print "Error: "+imageURL+":", e.code self.mapKeeper.addNot(search.replace(' ', '_')+" "+imageURL) continue except urllib2.URLError, e: print "Error: "+imageURL+":", e.args self.mapKeeper.addNot(search.replace(' ', '_')+" "+imageURL) continue image_type,width,height = getimageinfo.getImageInfo(imgdata) if image_type == ' ' or (width < 200 and height < 200): print "Image Invalid: ", imageURL self.mapKeeper.addNot(search.replace(' ', '_')+" "+imageURL) continue print "image type:", image_type, "width:", width, "height:", height return imageURL
def get(self, key): ''' Return the corresponding image. NOT a webpage, actual binary image-ful data. @param key: ''' im = db.get(db.Key(key)) if not im: self.error(404) return cache = memcache.Client() if cache.get(key) is None: cache.set(key, gzip.zlib.decompress(im.data)) if cache.get(key+"type") is None: content_type, width, height = getImageInfo(cache.get(key)) cache.set(key+"type",content_type) self.response.headers.add_header("Expires", "Thu, 01 Dec 2014 16:00:00 GMT") self.response.headers["Content-Type"] = cache.get(key+"type") self.response.out.write(cache.get(key))
def search(self, search): g = pygoogle(search) g.pages = 5 searchNot = self.mapKeeper.searchNot(search.replace(" ", "_")) results = g.get_urls() print "number of results: ", len(results) for url in results: base_url = url req = urllib2.Request(url, headers=self.hdr) try: response = urllib2.urlopen(req) print "Processing: ", url except (UnicodeEncodeError, urllib2.HTTPError, urllib2.URLError, socket.error, httplib.BadStatusLine), e: print "Error when opening url -> " + url + ": ", e continue page = BeautifulSoup(response, "lxml") images = page.select("img[alt]") for image in images: if search in image.get("alt").lower(): imageURL = image.get("src") imageURL = urlparse.urljoin(base_url, imageURL) if imageURL in searchNot: print "Image is in searchNot: ", imageURL continue try: imgdata = urllib2.urlopen(imageURL) except urllib2.HTTPError, e: print "Error: " + imageURL + ":", e.code self.mapKeeper.addNot(search.replace(" ", "_") + " " + imageURL) continue except urllib2.URLError, e: print "Error: " + imageURL + ":", e.args self.mapKeeper.addNot(search.replace(" ", "_") + " " + imageURL) continue image_type, width, height = getimageinfo.getImageInfo(imgdata) if image_type == " " or (width < 200 and height < 200): print "Image Invalid: ", imageURL self.mapKeeper.addNot(search.replace(" ", "_") + " " + imageURL) continue print "image type:", image_type, "width:", width, "height:", height return imageURL
def parse_guest_appearances(): records = 0 picpath = location.docroot + "albums/appearances/pix/" guestAlbums = ET.parse(location.dbroot + 'guest.xml').getroot() index_rows = [] for album in guestAlbums: if album.tag == 'description': guest_description = ET.tostring(album) continue if album.get('private'): continue records += 1 if album.tag == "single": title = SPAN(dclass="title", content="[single]") if album.tag == "album": title = SPAN(dclass="title", content=album.get('title')) artist = SPAN(dclass="gartist", content="(" + album.get('artist') + ")") if not artist: print "missing artist for album", album.get('title') if not title: print "missing title for album by", artist year = "(" + album.get('year')[:4] + ")" if not year: year = "(unknown)" pic = album.get('pic') if not pic: picTD = TD(dclass="missing_sleeve", content="[photo not available]") print title, "missing guest pic", artist pic = "NA" picfile = picpath + pic if os.path.isfile(picfile): (type, width, height) = getImageInfo(picfile) picTD = TD(dclass="single_sleeve", content=IMG(dclass="sleeve", src="pix/" + pic, width=width, height=height)) else: picTD = TD(dclass="missing_sleeve", content="[photo not available]") print "ERROR: can't find guest picture:", title, album.get( 'artist') amazon = album.get('amazon') if amazon: amazon = templates.getAmazonButton(amazon) else: amazon = "" trackrows = [] notes = [] for track in album: if track.tag == 'issue': continue if track.tag == 'note': notes.append(track.text) continue length = track.get('length') or "#:##" containsTD = TD(dclass="top", content="contains:") textTD = TD(dclass="track", content=linkTrack(track)) trackrows.append(TR(containsTD + textTD)) tracktable = TABLE("\n".join(trackrows)) detailsText = " ".join([title, artist]) detailsText += BR() + year + BR() + amazon + BR() + BR().join(notes) detailsTD = TD(dclass="guest_text", content=detailsText + BR() + tracktable) guest_entry = TABLE(TR(picTD + detailsTD)) index_rows.append(DIV(dclass="bubble", content=guest_entry)) # figure out how many index pages we need to create max_rows_per_index = 20 total_index_pages = len(index_rows) / max_rows_per_index if len(index_rows) % max_rows_per_index > 0: total_index_pages += 1 # chop away at the index_rows list, moving them into the current index page guestroot = location.docroot + "albums/appearances/" for page in range(total_index_pages): my_rows = index_rows[: max_rows_per_index] # grab the rows that go into this index page del index_rows[:max_rows_per_index] # clean up as we go guest_index_text = templates.getGuestIndex().substitute( TITLE=DIV(dclass="heading padded10", content="Elton John Guest Appearances"), DESCR=DIV(dclass="description", content=guest_description), ALBUMS=DIV(dclass="full_width", content="\n\n".join(my_rows)), #NAVBAR = DIV(content=DIV(content=" ".join(navDots))), NAVBAR=templates.getDotsDiv(page, total_index_pages), ) suffix = str(page + 1) if page == 0: suffix = "" with open(guestroot + "index" + suffix + ".html", "w") as guestindex: guestindex.write(guest_index_text) return records
def __init__(self, kwargs): super = DocumentRoot() doc = super.impl.createDocument("package", "package", None) package = doc.documentElement package.setAttribute("xmlns", "http://www.idpf.org/2007/opf") package.setAttribute("unique-identifier", "BookID") package.setAttribute("version", "2.0") metadata = doc.createElement("metadata") metadata.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/") metadata.setAttribute("xmlns:opf", "http://www.idpf.org/2007/opf") package.appendChild(metadata) dc = doc.createElement("dc:title") dc.appendChild(doc.createTextNode(kwargs.get("filename"))) metadata.appendChild(dc) if kwargs.get("creator"): dc = doc.createElement("dc:creator") dc.setAttribute("opf:role", "aut") dc.appendChild(doc.createTextNode(kwargs.get("creator"))) metadata.appendChild(dc) if kwargs.get("language"): dc = doc.createElement("dc:language") dc.appendChild(doc.createTextNode(kwargs.get("language"))) metadata.appendChild(dc) if kwargs.get("identifier"): dc = doc.createElement("dc:identifier") dc.setAttribute("id", "BookID") dc.setAttribute("opf:scheme", "UUID") dc.appendChild(doc.createTextNode(kwargs.get("identifier"))) metadata.appendChild(dc) meta = doc.createElement("meta") meta.setAttribute("name", "Sigil Version") meta.setAttribute("content", "0.1.8") metadata.appendChild(meta) manifest = doc.createElement("manifest") item = doc.createElement("item") item.setAttribute("id", "ncx") item.setAttribute("href", "toc.ncx") item.setAttribute("media-type", "application/x-dtbncx+xml") manifest.appendChild(item) for image in kwargs.get("images"): info = getImageInfo(open(image, "r").read()) filename = image.split("/").pop() item = doc.createElement("item") item.setAttribute("id", filename) item.setAttribute("href", "images/" + filename) item.setAttribute("media-type", info[0]) manifest.appendChild(item) for content in kwargs.get("contents"): item = doc.createElement("item") item.setAttribute("id", content) item.setAttribute("href", "text/" + content) item.setAttribute("media-type", "application/xhtml+xml") manifest.appendChild(item) package.appendChild(manifest) spine = doc.createElement("spine") spine.setAttribute("toc", "ncx") for content in kwargs.get("contents"): itemref = doc.createElement("itemref") itemref.setAttribute("idref", content) spine.appendChild(itemref) package.appendChild(spine) guide = doc.createElement("guide") reference = doc.createElement("reference") reference.setAttribute("type", "cover") reference.setAttribute("title", kwargs.get("filename")) reference.setAttribute("href", "text/" + kwargs.get("contents").pop(0)) guide.appendChild(reference) package.appendChild(guide) self.doc = doc
def generate_books(): totalBooks = 0 totalPages = 0 bookspath = location.docroot + "books/" picpath = bookspath + "pix/" bubbles = [] root = ET.parse(location.dbroot + 'books.xml').getroot() for book in root: if book.tag == "introduction": introduction = book.text continue pages = int(book.get('pages')) totalPages += pages totalBooks += 1 if book.get('private'): continue rows = [] pic = book.get('pic') picfile = picpath + pic try: (type, width, height) = getImageInfo(picfile) imageTD = TD(dclass="single_sleeve", content=IMG(dclass="sleeve", src="pix/" + pic, width=width, height=height)) if width != 200: print "bad width for", pic except IOError: print "cannot find picture for", book.get('title'), ":", pic titletext = book.get('title') rows.append( DIV(dclass="important left", content="Title: " + U(B(titletext)))) if book.get('author'): authortext = book.get('author') rows.append(DIV(dclass="left", content="Author: " + B(authortext))) rows.append(DIV(dclass="left", content="Year: " + B(book.get('year')))) if book.get('publisher'): pubtext = book.get('publisher') rows.append(DIV(dclass="left", content="publisher: " + B(pubtext))) if book.get('ISBN'): rows.append( DIV(dclass="left", content="ISBN: " + B(book.get('ISBN')))) if book.get('country'): rows.append( DIV(dclass="left", content="Country: " + B(book.get('country')))) rows.append(DIV(dclass="left", content="pages: " + B(str(pages)))) rows.append( DIV(dclass="left", content="binding: " + B(book.get('format')))) if book.get('amazon'): rows.append( DIV(dclass="left", content=templates.getAmazonButton(book.get('amazon')))) d = book.find('description') if d is not None: if d.get('source'): rows.append( DIV(dclass="left small padded3 bold", content="[description text from " + d.get('source') + ":]")) rows.append(DIV(dclass="left", content=ET.tostring(element=d))) textTD = TD(dclass="top", content="".join(rows)) bubbles.append( DIV(dclass="bubble", content=TABLE(TR(imageTD + textTD)))) #index_text = templates.getBooksIndex().substitute( # DESCR = DIV(dclass="description", content=introduction), # BOOKS = DIV(dclass="full_width", content="\n\n".join(bubbles)), #) # figure out how many index pages we need to create max_rows_per_index = 14 total_index_pages = len(bubbles) / max_rows_per_index if len(bubbles) % max_rows_per_index > 0: total_index_pages += 1 # chop away at the bubbles list, moving them into the current index page bookroot = location.docroot + "books/" for page in range(total_index_pages): my_rows = bubbles[: max_rows_per_index] # grab the books that go into this index page del bubbles[:max_rows_per_index] # clean up as we go book_index_text = templates.getBooksIndex().substitute( EXTRA_HEADER="", DESCR=DIV(dclass="description", content=introduction), BOOKS=DIV(dclass="full_width", content="\n\n".join(my_rows)), #NAVBAR = DIV(content=DIV(content=" ".join(navDots))), NAVBAR=templates.getDotsDiv(page, total_index_pages), ) suffix = str(page + 1) if page == 0: suffix = "" with open(bookroot + "index" + suffix + ".html", "w") as bookindex: bookindex.write(book_index_text.encode('UTF-8')) return totalBooks
def generate_magazines(): totalMags = 0 magspath = location.docroot + "magazines/" picpath = magspath + "pix/" bubbles = defaultdict(list) root = ET.parse(location.dbroot + 'magazines.xml').getroot() for mag in root: if mag.tag == "introduction": #introduction = mag.text introduction = ET.tostring(mag) continue if mag.get('private'): continue # skip stuff not ready for publication if mag.get('own') == "no": continue # skip stuff that I don't actually have if mag.get('nocover'): continue # skip stuff if elton not on the cover page if mag.tag == "fanmagazine": continue # fan magazines will be done some other way pic = mag.get('pic') if pic == None: continue # TBD: we should complain about missing pic totalMags += 1 rows = [] picfile = picpath + pic try: (type, width, height) = getImageInfo(picfile) allPicFiles.remove(pic) imageTD = TD(dclass="single_sleeve", content=IMG(dclass="sleeve", src="pix/" + pic, width=width, height=height)) if width != 200: print "bad width for", pic except: try: print "cannot find picture for", mag.get('title'), mag.get( 'date'), ":", pic except: print "cannot find picture for", mag.get('date') titletext = mag.get('title') rows.append(DIV(dclass="important left", content=U(B(titletext)))) issuetext = mag.get('date') year = addYear(issuetext) date = issuetext.split('-') if len(date) > 1: if date[1] in monthAbbreviation: date[1] = monthAbbreviation[date[1]] if len(date) == 2: issuetext = date[1] + " " + date[0] if len(date) == 3: issuetext = date[1] + " " + date[2] + ", " + date[0] if (mag.get('issue') != None): if is_number(mag.get('issue')): issuetext = issuetext + ' (#' + mag.get('issue') + ')' else: issuetext = issuetext + ' (' + mag.get('issue') + ')' rows.append(DIV(dclass="left", content="Issue Date: " + B(issuetext))) rows.append( DIV(dclass="left", content="Country: " + B(mag.get('country')))) color = "blue" #if mag.get('own') != None : color = "yellow" textTD = TD(dclass="top", content="".join(rows)) bubbles[year].append( DIV(dclass="bubble " + color, content=TABLE(TR(imageTD + textTD)))) # this javascript gets added to each mags html file # it is the response to load a new page when a selection is made yearScript = ''' <script type="text/javascript"> function showYear(selector,firstyear) { year = selector.options[selector.selectedIndex].value if (year == firstyear) year = "" newurl = "index" + year + ".html"; document.location=newurl; } </script> ''' # divide all the magazines into sepatate index files sorted by year magroot = location.docroot + "magazines/" prevlink = None for y in allYears: mag_index_text = templates.getMagazineIndex().substitute( EXTRA_HEADER=yearScript, DESCR=DIV(dclass="description", content=introduction), BOOKS=DIV(dclass="full_width", content="\n\n".join(bubbles[y])), NAVBAR=yearSelector(y), ) suffix = str(y) if y == allYears[0]: suffix = "" with open(magroot + "index" + suffix + ".html", "w") as magindex: magindex.write(mag_index_text.encode('UTF-8')) return totalMags
def parse_other_albums(): totalAlbums = 0 loadTracks(location.dbroot + "album_tracks.xml") albumroot = location.docroot + "albums/international/" picroot = albumroot + "pix/" root = ET.parse(location.dbroot + 'other_albums.xml').getroot() # establish next/prev links prevcountry = None for country in root: if country.tag == "description": continue if country.get('private'): continue country.set('previcon', "") country.set('nexticon', "") country.set('id', re.sub(' ', '_', country.get('name')).lower()) if prevcountry != None: prevcountry.set('nexticon', linkto("right", country)) # forward link country.set('previcon', linkto("left", prevcountry)) # backward link prevcountry = country indexrows = [] for country in root: if country.tag == "description": description = country.text continue if country.get('private'): continue count = 0 name = country.get('name') bubbles = [] for album in country: print album.get('title') if album.get('private'): continue count += 1 totalAlbums += 1 # TBD id = album.get('id') picfile = country.get('id') + '/' + album.get('pic') picpath = picroot + picfile if not os.path.isfile(picpath): print "error no pic: " + picpath (type, width, height) = getImageInfo(picpath) image = IMG(dclass="sleeve", src="pix/" + picfile, width=width, height=height) picTD = TD(dclass="single_sleeve", content=image) discs = album.get('discs') if discs: prefix = discs else: prefix = "" iconfile = "/pix/" + prefix + album.tag + ".gif" iconpath = location.docroot + iconfile (type, width, height) = getImageInfo(iconpath) iconTD = TD( IMG(dclass="padded10", src=iconfile, width=width, height=height)) details = [] details.append( DIV(dclass="left important", content=U(album.get('title')))) details.append( DIV(dclass="left", content="Year: " + B(album.get('year')))) details.append( DIV(dclass="left", content="Catalog: " + B(album.get('catalog')))) textTD = TD("\n".join(details)) headerTable = TABLE(TR(iconTD + textTD)) notes = [] tracks = [] for element in album: if element.tag == 'tracks': tracks = tracks + alltracks[element.get('id')] elif element.tag == 'note': notes.append(element.text) elif element.tag == 'see': notes.append(element.text) else: print "bad tag", element.tag notesDiv = DIV(dclass="left padded3", content="<br/>".join(notes)) trackrows = [] for track in tracks: tracktime = TD(dclass="time", content=track.get('length')) tracktext = TD(dclass="track", content=linkTrack(track)) trackrows.append(TR(tracktime + tracktext)) tracksTable = TABLE("\n".join(trackrows)) detailsTD = TD(headerTable + notesDiv + tracksTable) bubbles.append( DIV(dclass="bubble", content=TABLE(dclass="single_issue", content=TR(picTD + detailsTD)))) page_template = templates.getInternationalAlbumTemplate(name) page_text = page_template.substitute(NEXTICON=country.get('nexticon'), PREVICON=country.get('previcon'), COUNTRY=DIV(dclass="heading", content="select " + name + " pressings"), ALBUMS="\n".join(bubbles)) target = country.get('id') + ".html" with open(albumroot + target, "w") as countryfile: countryfile.write(page_text.encode('UTF-8')) flagimg = A(href=target, content=IMG(dclass="hardleft20", src="/pix/flags/" + country.get('id') + ".gif")) namespan = SPAN(dclass="important hardleft200", content=A(href=target, content=name)) s = "" if count > 1: s = "s" countspan = SPAN(dclass="important hardleft400", content=A(href=target, content=str(count) + " pressing" + s)) indexrows.append( DIV(dclass="left bubble", content=flagimg + namespan + countspan)) index_template = templates.getInternationalIndexTemplate() index_text = index_template.substitute( EXTRA_STYLE="", PAGE_TITLE="Elton John International Albums", PAGE_CONTENT=DIV(dclass="description", content=description) + "".join(indexrows), ) with open(albumroot + "index.html", "w") as indexfile: indexfile.write(index_text) return totalAlbums
def read_image_data(self, image_name): with open(os.path.join(self.album_dir, image_name), 'rb') as photo_file: data = photo_file.read(81000) return getImageInfo(data)
def edit(request, repo_name, branch=REPO_BRANCH, path=None): file_source = "" msgs = [] json_convert = None if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view(request, result_msg) if path[-1:] == "/": path = path[:-1] file_path = path #!!! FIX security repo = get_repo(repo_name) tree = repo.tree() # edit only if exist in tree try: tree = tree[path] except KeyError: msg.append(MSG_NO_FILE_IN_TREE) return error_view(request, msg) # edit only if it is file if not tree.type is "blob": msgs.append(MSG_CANT_VIEW) return error_view(request, msg) mime = tree.mime_type.split("/") file_meta = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, abspath=tree.abspath, repo=repo, mime=tree.mime_type, size=tree.size, tree=tree, mime_type=mime[0], type=file_type_from_mime(tree.mime_type), ) if file_meta["mime_type"] in EDITABLE_MIME_TYPES: form_class = TextFileEditForm else: form_class = FileEditForm signals.file_edit_start.send(sender=repo, file_path=file_path, url="") if request.method == "POST": form = form_class(request.POST, request.FILES) if form.is_valid(): file_abs_path = os.path.join(repo.working_dir, file_path) if file_meta["mime_type"] == "text" or mime[1] in ["xml"]: file_source = form.cleaned_data["file_source"] file_writen = write_file(file_abs_path, file_source) else: file_writen = handle_uploaded_file(file_abs_path, request.FILES["file_source"]) if file_writen: msgs.append("File has been saved") message = form.cleaned_data["message"] ammend = form.cleaned_data.get("ammend", None) msg = mk_commit(repo, message, file_path, ammend) msgs.append(msg) else: msgs.append(MSG_CANT_SAVE_FILE) else: msgs.append(form.errors) if request.is_ajax(): json_convert = message_convert else: if file_meta["mime_type"] in EDITABLE_MIME_TYPES: file_source = tree.data_stream[3].read else: file_source = file_meta["abspath"] form = form_class(initial={"file_source": file_source, "message": "modified %s" % path}) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, form=form, file_source=file_source, breadcrumbs=make_crumbs(path), file_meta=file_meta, msg=msgs, repo_name=repo_name, branch_name=branch, repo=repo, delete_form=FileDeleteForm(initial={"message": MSG_DELETE_SUCCESS % path}), path=path, name=path.split("/")[-1:][0], ) if mime[0] == "image": import base64 context["img_base"] = base64.b64encode(file_source) from getimageinfo import getImageInfo context["img_meta"] = getImageInfo(file_source) return mix_response(request, "stratus/edit.html", context, json_convert)
def view(request, repo_name, branch, path, commit_sha=None): """ view file in the commit """ file_source = diff = "" if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view(request, msg) file_path = path #!!! FIX security if path[-1:] == "/": path = path[:-1] repo = get_repo(repo_name) commit, tree = get_commit_tree(repo, commit_sha) if commit.parents: diff = get_diff(repo, path, commit.parents[0].hexsha, commit.hexsha) try: tree = tree[path] except KeyError: msg = MSG_NO_FILE_IN_TREE return error_view(request, msg) if not tree.type is "blob": msg = MSG_NO_FILE_IN_TREE return error_view(request, msg) mime = tree.mime_type.split("/") file_source = tree.data_stream[3].read() # import ipdb; ipdb.set_trace() file_meta = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, abspath=tree.abspath, mime=tree.mime_type, size=tree.size, tree=tree, path=tree.abspath, mime_type=mime[0], type=file_type_from_mime(tree.mime_type), ) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, file_source=file_source, breadcrumbs=make_crumbs(path), commit=commit, diff=diff, file_meta=file_meta, repo_name=repo_name, repo=repo, branch_name=branch, path=path, name=path.split("/")[-1:][0], ) if mime[0] == "image": import base64 context["img_base"] = base64.b64encode(file_source) from getimageinfo import getImageInfo context["img_meta"] = getImageInfo(file_source) return mix_response(request, "stratus/view_file.html", context)
def generate_session_albums(): totalAlbums = 0 loadTracks(location.dbroot + "album_tracks.xml") sessionroot = location.docroot + "albums/sessions/" picroot = sessionroot + "pix/" root = XML.parse(location.dbroot + 'sessionwork.xml').getroot() totalrecords = 0 description = root.find('description') #description = description.text description = XML.tostring(description) index_rows = [] songrows = [] songrows.append( TR( TD(B(U("released"))) + TD(B(U("title"))) + TD(B(U("original artist"))))) for song in root.findall('listing/song'): year = song.get('date')[:4] month = song.get('date')[5:7] month = monthAbbreviation[month] eltonvocals = song.get('elton', default="lead vocals") eltonvocals = " [" + eltonvocals + "]" songrow = TR( TD(month + " " + year) + TD(B(U(song.get('title'))) + eltonvocals) + TD(song.get('artist'))) songrows.append(songrow) songtable = DIV(dclass="bubble", content=TABLE(content="\n".join(songrows), dclass="left small")) description += songtable for category in root.findall('category'): if category.get('private'): continue format = category.get('format') categoryname = category.get('format') for issue in category: if issue.get('private'): continue #if issue.get('pic') == None: continue totalrecords += 1 title = issue.get('title') if title == None: title = "" if format == "7": title = "7-inch single" if format == "CD5": title = "CD single" catalog = issue.get('catalog') if catalog == None: print "no catalog for", title continue pic = issue.get('pic') if pic == None: picTD = TD(dclass="missing_sleeve", content="[no sleeve]") else: picfile = picroot + pic if os.path.isfile(picfile): (type, width, height) = getImageInfo(picfile) picTD = TD(dclass="single_sleeve", content=IMG(dclass="sleeve", src="pix/" + pic, width=width, height=height)) else: picTD = TD(dclass="missing_sleeve", content="[photo not available]") print "ERROR: can't find session picture:", title, catalog year = issue.get('year', default="") if len(year): year = " (" + year[:4] + ")" trackrows = [] for track in issue.findall('track'): if track.get('artist') == "anonymous": trackrows.append( LI(dclass="anonymous", content=track.get('title'))) else: trackrows.append( LI(dclass="track", content=linkTrack(track))) reftracks = [] for element in issue: if element.tag == 'tracks': reftracks = reftracks + alltracks[element.get('id')] for track in reftracks: trackrows.append(LI(dclass="track", content=linkTrack(track))) prefix = issue.get('discs', "") iconfile = "/pix/" + prefix + format + ".gif" (type, width, height) = getImageInfo(location.docroot + iconfile) icon = IMG(dclass="padded10", src=iconfile, width=width, height=height) notes = "" for note in issue.findall('note'): notes += BR() + note.text tracktable = OL("\n".join(trackrows)) title = DIV(dclass="large bold", content=title) detailsText = icon + title + catalog + year + notes detailsTD = TD(dclass="guest_text", content=detailsText + BR() + tracktable) session_issue = TABLE(TR(picTD + detailsTD)) index_rows.append(DIV(dclass="bubble", content=session_issue)) session_index_text = templates.getSessionIndex().substitute( TITLE=DIV(dclass="heading padded10", content="Elton John as a Session Musician"), DESCR=DIV(dclass="description", content=description), ALBUMS=DIV(dclass="full_width", content="\n\n".join(index_rows)), ) with open(sessionroot + "index.html", "w") as sessionindex: sessionindex.write(session_index_text.encode('latin-1')) return totalrecords
def parse_import_cds(per_page): count = 0 imports = ET.parse(location.dbroot + 'import_cds.xml').getroot() importpath = location.docroot + "albums/unauthorized/" picpath = importpath + "pix/" introduction = "" bubbles = [] for cd in imports: if cd.tag == 'introduction': introduction = cd.text continue if cd.get('private'): continue count += 1 # establish the left TD with a cover scan pic = cd.get('pic') if not pic: raise Exception("missing import pic for " + title) picfile = picpath + pic (type, width, height) = getImageInfo(picfile) img = IMG(dclass="sleeve", src="pix/" + pic, width=width, height=height, alt="scan of sleeve", title="scan of sleeve") picTD = TD(dclass="single_sleeve", content=img) # establish the right TD with all the text (as separate table rows) textRows = [] textRows.append( TR(TD(dclass="left important", content=U(cd.get('title'))))) textRows.append( TR( TD(dclass="left", content='label/catalog: ' + B(cd.get('catalog'))))) if cd.get('year'): textRows.append( TR(TD(dclass="left", content='issued: ' + B(cd.get('year'))))) if cd.get('venue'): textRows.append( TR(TD(dclass="left", content='venue: ' + B(cd.get('venue'))))) if cd.get('date'): textRows.append( TR( TD(dclass="left", content='recorded live: ' + B(textDate(cd.get('date')))))) if cd.get('country'): textRows.append( TR( TD(dclass="left", content='country: ' + B(cd.get('country'))))) tracks = [] for element in cd: if element.tag == 'track': tracks.append(LI(dclass="left", content=linkTrack(element))) elif element.tag == 'note': textRows.append(TR(TD(dclass="left", content=element.text))) textRows.append(TR(TD(OL("\n".join(tracks))))) textTD = TD(TABLE("\n".join(textRows))) bubbles.append(DIV(dclass="bubble", content=TABLE(TR(picTD + textTD)))) # figure out how many index pages we need to create total_index_pages = len(bubbles) / per_page # first pages are maxed out if len(bubbles) % per_page > 0: total_index_pages += 1 # last page is partial set # chop away at the index_rows list, moving them into the current index page for ipage in range(total_index_pages): my_rows = bubbles[: per_page] # grab the rows that go into this index page del bubbles[:per_page] # clean up as we go index_text = templates.getImportIndex().substitute( TITLE=DIV(dclass="heading padded10", content="Elton John Unauthorized CDs"), DESCR=DIV(dclass="description", content=introduction), ALBUMS=DIV(dclass="full_width", content="\n\n".join(my_rows)), NAVBAR=templates.getDotsDiv(ipage, total_index_pages), ) suffix = str(ipage + 1) if ipage == 0: suffix = "" with open(importpath + "index" + suffix + ".html", "w") as indexfile: indexfile.write(index_text.encode('latin-1')) return count
def __init__(self,kwargs): super = DocumentRoot() doc = super.impl.createDocument("package","package",None) package = doc.documentElement package.setAttribute("xmlns" , "http://www.idpf.org/2007/opf") package.setAttribute("unique-identifier" , "BookID") package.setAttribute("version" , "2.0") metadata = doc.createElement("metadata") metadata.setAttribute("xmlns:dc" , "http://purl.org/dc/elements/1.1/") metadata.setAttribute("xmlns:opf" , "http://www.idpf.org/2007/opf") package.appendChild( metadata ) dc = doc.createElement("dc:title") dc.appendChild( doc.createTextNode( kwargs.get("filename") ) ) metadata.appendChild(dc) if kwargs.get("creator"): dc = doc.createElement("dc:creator") dc.setAttribute("opf:role" , "aut") dc.appendChild( doc.createTextNode( kwargs.get("creator") ) ) metadata.appendChild(dc) if kwargs.get("language"): dc = doc.createElement("dc:language") dc.appendChild( doc.createTextNode( kwargs.get("language") ) ) metadata.appendChild(dc) if kwargs.get("identifier"): dc = doc.createElement("dc:identifier") dc.setAttribute("id" , "BookID") dc.setAttribute("opf:scheme" , "UUID" ) dc.appendChild( doc.createTextNode( kwargs.get("identifier") ) ) metadata.appendChild(dc) meta = doc.createElement("meta") meta.setAttribute("name" , "Sigil Version") meta.setAttribute("content","0.1.8") metadata.appendChild( meta ) manifest = doc.createElement("manifest") item = doc.createElement("item") item.setAttribute("id" , "ncx") item.setAttribute("href" , "toc.ncx") item.setAttribute("media-type" , "application/x-dtbncx+xml") manifest.appendChild( item ) for image in kwargs.get("images") : info = getImageInfo( open(image,"r").read() ) filename = image.split("/").pop() item = doc.createElement("item") item.setAttribute("id" , filename) item.setAttribute("href" , "images/" + filename ) item.setAttribute("media-type" , info[0] ) manifest.appendChild( item ) for content in kwargs.get("contents") : item = doc.createElement("item") item.setAttribute("id" , content ) item.setAttribute("href" , "text/" + content ) item.setAttribute("media-type" , "application/xhtml+xml" ) manifest.appendChild( item ) package.appendChild( manifest ) spine = doc.createElement("spine") spine.setAttribute("toc" , "ncx") for content in kwargs.get("contents") : itemref = doc.createElement("itemref") itemref.setAttribute("idref" , content ) spine.appendChild( itemref ) package.appendChild( spine ) guide = doc.createElement("guide") reference = doc.createElement("reference") reference.setAttribute("type" , "cover") reference.setAttribute("title" , kwargs.get("filename") ) reference.setAttribute("href" , "text/" + kwargs.get("contents").pop(0) ) guide.appendChild( reference ) package.appendChild( guide ) self.doc = doc
def parse_standard_albums(): totalAlbums = 0 loadTracks(location.dbroot + "album_tracks.xml") albumroot = location.docroot + "albums/standard/" picroot = albumroot + "pix/" root = ET.parse(location.dbroot + 'category_albums.xml').getroot() ##decodedroot = ET.tostring(root) ##root2 = ET.fromstring(decodedroot) ### establish prev/next icons for category in root: if category.tag == 'description': pageDescription = DIV(dclass="description", content=category.text) continue prevalbum = None for album in category: album.set('previcon', "") album.set('nexticon', "") if prevalbum != None: prevpic = IMG(src="/pix/left_arrow.gif", border="0", title=prevalbum.get('title')) nextpic = IMG(src="/pix/right_arrow.gif", border="0", title=album.get('title')) prevalbum.set('nexticon', A(href=album.get('id') + ".html", content=nextpic)) # forward link album.set('previcon', A(href=prevalbum.get('id') + ".html", content=prevpic)) # backward link prevalbum = album styles = [] catThumbs = {} categories = [] for category in root: if category.tag == 'description': continue ### create the CSS style & index matrix for all the sprites in this category thumbs = [] for album in category: id = album.get('id') XY = '-' + album.get('spriteX') + 'px ' + '-' + album.get( 'spriteY') + 'px' imagecss = "img#" + id + "{ width:50px; height:50px; background:url(sprite.jpg) " + XY + ";}" styles.append( imagecss) # each album gets an icon in their category thumbs.append(minilink(album)) categories.append(category.get('name')) catThumbs[category.get('name')] = DIV(dclass="thumbnails", content=" ".join(thumbs)) ### create an HMTL page for each album for album in category: totalAlbums += 1 albumnotes = [] versions = [] title = album.get('title') for element in album: if element.tag == 'see': continue if element.tag == 'note': albumnotes.append(element.text) continue # otherwise it's a version tracks = [] versionNotes = [] version = element name = version.get('name') if name == None: name = "" year = '(' + version.get('year')[:4] + ')' amazonID = version.get('amazon') if amazonID: amazon = templates.getAmazonButton(amazonID) else: amazon = "" picname = version.get('pic') if not os.path.isfile(picroot + picname): print "error no pic", picname (type, width, height) = getImageInfo(picroot + picname) image = IMG(dclass="sleeve", src="pix/" + picname, width=width, height=height) picTD = TD(dclass="single_sleeve", content=image) for element in version: if element.tag == 'tracks': tracks = tracks + alltracks[element.get('id')] elif element.tag == 'section': tracks = tracks + [element] elif element.tag == 'note': versionNotes.append(element.text) else: print "bad tag", element.tag trackrows = [] trackrows.append( TR( TD(dclass="single_details", colspan=2, content=" ".join([name, year])))) for note in versionNotes: trackrows.append( TR(TD(dclass="note", colspan=2, content=note))) trackrows.append( TR(TD(dclass="amazon", colspan=2, content=amazon))) for track in tracks: if track.tag == 'section': trackrows.append( TR( TD(colspan=2, dclass="tracksection", content=track.get('title')))) continue tracktime = TD(dclass="time", content=track.get('length')) tracktext = TD(dclass="track", content=linkTrack(track)) trackrows.append(TR(tracktime + tracktext)) tracktable = TABLE("\n".join(trackrows)) detailsTD = TD(tracktable) versiontable = TABLE(dclass="single_issue", content=TR(picTD + detailsTD)) versions.append(DIV(dclass="bubble", content=versiontable)) page_template = templates.getStandardAlbumTemplate(title) album_text = page_template.substitute( NEXTICON=album.get('nexticon'), PREVICON=album.get('previcon'), ALBUM_TITLE=DIV(dclass="heading", content=U(title)), ALBUM_NOTES=DIV(dclass="subheading", content="<br/>".join(albumnotes)), VERSIONS="\n".join(versions)) target = album.get('id') + ".html" with open(albumroot + target, "w") as albumfile: albumfile.write(album_text.encode('UTF-8')) # end album #end category catDivs = [] for category in categories: catDivs.append( DIV(dclass="bubble", content=DIV(dclass="heading", content=category + ':') + catThumbs[category])) albumCSS = STYLE(content="\n".join(styles)) index_template = templates.getStandardIndexTemplate() index_text = index_template.substitute( EXTRA_STYLE=albumCSS, CONTENT=pageDescription + "".join(catDivs), ) with open(albumroot + "index.html", "w") as indexfile: indexfile.write(index_text.encode('UTF-8')) return totalAlbums
## LOAD GUEST APPERANCES root = ET.parse('guest.xml').getroot() for release in root: if release.get('private') != None: continue if release.tag == 'album': if release.get('title') == None: print "error: appearances album missing title tag" title = release.get('title') if release.get('pic') == None: print "error: no pic for", title if release.get('year') == None: print "error: no year for", title if release.get('artist') == None: print "error: no artist for", title elif release.tag == 'single': if release.get('artist') == None: print "error: no artist for single" if release.get('pic') == None: continue pic = release.get('pic') if not os.path.isfile(picpath + pic): print "error: can't find picture", pic else: print "getting dimensions for", pic imgdata = open(picpath + pic, 'rb').read() (type, width, height) = getimageinfo.getImageInfo(imgdata) for element in release: if element.tag != 'track': continue # skip notes and issues if element.get('artist'): continue # skip other artist songs if element.get('elton'): continue # elton not on vocals if element.get('title') == None: print "error no title for", title for subtitle in element.get('title').split('/'): if subtitle in unlinkablesongs: continue if subtitle not in songs: print "bad song reference:", subtitle
def generate_singles(): singles_root = location.docroot + "singles/" # ---------------------------------------------------------------------------- # Collect href information for albums, to verify album linking. Dictionary is # fileID -> title_string # ---------------------------------------------------------------------------- #album_ids = {} #albums = ET.parse(location.dbroot+'category_albums.xml').getroot() #for album in albums: # album_ids[album.get('id')] = album.get('title') #albums = ET.parse(location.dbroot+'other_albums.xml').getroot() #for album in albums: # album_ids[album.get('id')] = album.get('title') xmldoc = ET.parse(location.dbroot+'singles.xml').getroot() # load singles from file # ---------------------------------------------------------------------------- # Set the id/prev/next links as XML attributes. # ---------------------------------------------------------------------------- prevsingle = None for single in xmldoc: if single.get('artist') : continue # some other artist single if single.get('private') : continue # not for public website if single.tag == 'description': description = single.text continue if single.tag == 'break' : continue if single.tag == 'single' : # get the id from the title id = re.sub(' ', '_', single.get('title')).lower() id = re.sub('\(', '', id) id = re.sub('\)', '', id) id = re.sub('\/',' ', id) id = re.sub('\'', "", id) single.set('id', re.sub(' ', '_', id)) if single.tag == 'miscellaneous' : single.set('id', re.sub(' ','_',single.get('country').lower())) if prevsingle != None: prevsingle.set('nextid',single.get('id')) # forward link single.set('previd',prevsingle.get('id')) # backward link else : firstsingle = single # save first for linnking with last prevsingle = single single.set('nextid', firstsingle.get('id')) # fully cycled double linked-list firstsingle.set('previd', single.get('id')) # fully cycled double linked-list # ---------------------------------------------------------------------------- # Store each single as its own HTML file with listing of issues. Also # generate index pages. # ---------------------------------------------------------------------------- picpath = singles_root + "pix/" flagpath = location.docroot + "/pix/flags/" index_rows = [] # prepare list for index page total_issues = 0 # prepare info for index page total_pages = 0 for single in xmldoc: if single.tag == 'description': continue # already got this if single.get('artist'): continue # ignore singles by non-Elton artists if single.get('private'): continue # ignore singles not intended for public total_pages += 1 icon=None # should be populated later if single.tag == 'single': title = single.get('title') subtitle = '('+single.get('release')[0:4] + ')' (type,width,height) = getImageInfo(picpath+'icons/'+single.get('icon')) single_icon=IMG(src="pix/icons/"+single.get('icon'), width=width, height=height, alt=title, dclass="single_icon") elif single.tag == 'break': index_rows.append(DIV(dclass="bubble description", content=single.text)) continue elif single.tag == 'miscellaneous' : title = single.get('country')+" unique singles" subtitle = "(miscellaneous releases)" flag = re.sub(' ','_',single.get('country').lower()) + ".gif" if not os.path.isfile(flagpath+flag): print "ERROR missing flag for",country (type,width,height) = getImageInfo(flagpath+flag) single_icon = IMG(src="/pix/flags/"+flag, width=width, height=height, alt=title, dclass="single_icon") #print "parsing",title # TBD... #if single.get('album') : # album_href = "/albums/"+single.get('album')+".html" # album_title = album_ids.get(single.get('album')) # albumlink = "From the album " + A(href=album_href, content=album_title) # #subtitle += '<br/>('+albumlink+')' issues = [] for issue in single: if issue.get('private'): continue # not for website if issue.get('format') == "CS": continue # not for website yet total_issues += 1 # info for index page country = issue.get('country') if country == None: print "ERROR missing country" count = single.get('count') if count: count = int(count)+1 else: count = 1 single.set('count',str(count)) # detect flag GIF based on country flag = re.sub(' ','_',country.lower()) + ".gif" if not os.path.isfile(flagpath+flag): print "ERROR missing flag for",country (type,width,height) = getImageInfo(flagpath+flag) flagpic = IMG(src="/pix/flags/"+flag, width=width, height=height, alt=country, title=country) if single.tag == 'miscellaneous': flagpic="" # flag for misc issues is redundant format = issue.get('format') if format == None: print "ERROR missing format" if format == 'postcard': continue # ignore Polish cards for now year = issue.get('year') if year == 'unknown': year = "" if year == None: print "ERROR missing year" catalog = issue.get('catalog') if catalog == None: print "ERROR missing catalog" promo = "" if issue.get('promo'): promo = '[promo]' discs = "" if issue.get('discs'): discs = '[' + issue.get('discs') + ' discs]' note = "" if issue.get('note') : note = '[' + issue.get('note') + ']' if not issue.get('ps'): sleeve = "" picTD = TD(dclass="missing_sleeve", content="(no sleeve)") else: if issue.get('ps') == 'true' : sleeve = "[picture sleeve]" else : sleeve = '[' + issue.get('ps') + ']' pic = re.sub('/','',catalog).upper() pic = '/'.join([format,country,pic]) if issue.get('pic'): pic = "/".join([format,issue.get('pic')]) pic = re.sub(' ','_',pic) pic = re.sub('-','',pic) pic = re.sub('\.','',pic) if issue.get('pic') : pic = '/'.join([format,issue.get('pic')]) picfile = None if os.path.isfile(picpath+pic+".jpg"): picfile = "pix/"+pic+".jpg" elif os.path.isfile(picpath+pic+".gif"): picfile = "pix/"+pic+".gif" if picfile: (type,width,height) = getImageInfo(singles_root + picfile) picTD = TD(dclass="single_sleeve", content=IMG(dclass="sleeve", src=picfile, width=width, height=height) + BR() + sleeve) else: picTD = TD(dclass="missing_sleeve", content="[sleeve photo not available]") if issue.get('pic') != "NA": print "ERROR: can't find picture:",issue.get('pic'), title, format, country, year, catalog if os.path.isfile(singles_root + "pix/"+format+".gif"): icon = IMG(src="pix/"+format+".gif") # use graphical icon else: icon = format # use text name country = "("+country+")" if single.tag == 'miscellaneous': country = "" issueheader = TR(TD(dclass="single_details", colspan=2, content=" ".join([icon,year,catalog,country,discs,promo,note]))) trackrows = [] trackrows.append(issueheader) for track in issue: tracktime = TD(dclass="time", content=track.get('length')) tracktext = TD(dclass="track", content=linkTrack(track)) trackrows.append(TR(tracktime + tracktext)) tracktable = TABLE("\n".join(trackrows)) detailsTD = TD(tracktable) issues.append(DIV(dclass="bubble", content=TABLE(dclass="single_issue", content=TR(picTD + detailsTD)))) page_template = templates.getSingleTemplate() single_text = page_template.substitute( NEXTID = single.get('nextid'), PREVID = single.get('previd'), SINGLE_TITLE = '"'+title+'"', SINGLE_SUBTITLE = subtitle, ISSUES = "\n".join(issues) ) target = single.get('id')+".html" with open(singles_root+target, "w") as singlefile: singlefile.write(single_text.encode('latin1')) count = single.get('count') suffix = "" if count > 1: suffix = "s" subtitle = SPAN(dclass="single_count",content=subtitle + BR()+'(' + count + ' pressing'+suffix+')') iconTD = TD(dclass="single_icon", content=A(href=target, content=single_icon)) textTD = TD(dclass="single_index", content=SPAN(dclass="single_index", content=A(href=target, content=title)) + subtitle) index_rows.append(DIV(dclass="bubble", content=TABLE(TR(iconTD + textTD)))) #TBD index_rows.append(TR(TD("total worldwide pressings: " + str(total_issues)))) # ---------------------------------------------------------------------------- # Now write set of index pages that links to all the other single pages. # ---------------------------------------------------------------------------- max_rows_per_index = 30; total_index_pages = len(index_rows)/max_rows_per_index if len(index_rows) % max_rows_per_index > 0: total_index_pages += 1 for page in range(total_index_pages) : navDots=[] for link in range(total_index_pages) : suffix = str(link+1) if link == 0: suffix = "" if link == page: navDots.append(IMG(dclass="navdots", src="/pix/white_dot.gif")) else: navDots.append(A(href="index"+suffix+".html", alt="page "+suffix, content=IMG(dclass="noborder navdots", border="0", src="/pix/green_dot.gif"))) my_rows = index_rows[:max_rows_per_index] del index_rows[:max_rows_per_index] single_index_text = templates.getSingleIndex().substitute( TITLE = DIV(dclass='heading padded10', content="Elton John Singles"), DESCR = DIV(dclass='description', content=description), ISSUES = "\n".join(my_rows), NAVBAR = DIV(content=DIV(content=" ".join(navDots))), ) suffix = str(page+1) if page == 0: suffix = "" with open(singles_root+"index"+suffix+".html", "w") as singleindex: singleindex.write(single_index_text) return total_issues