def update(_id, title, content, c_name, c_id, tags): summary = content[0:80] + '...' html = markdown.markdown(content) diary = { "title": title, "content": content, "category": c_name, "category_id": int(c_id), "tags": tags, "summary": markdown.markdown(summary), "html": html, "update_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") } publish_time = Diary.get_detail(_id).get('publish_time') last_cid = Diary.get_detail(_id).get('category_id') last_tags = Diary.get_detail(_id).get('tags') db.diaries.update({'_id': int(_id)}, {'$set': diary}) #Save for category Category.update_diary(c_id, _id, title, publish_time, last_cid) if last_tags is not None: # delete it from old tags Tag.del_diary(_id) if tags is not None: diary = Diary.get_detail(_id) # save tags for tag in tags: Tag.add(tag, diary) return
def add(title, content, c_name, c_id, tags): summary = content[0:80] + '...' html = markdown.markdown(content) diary = { "_id": Kid.kid(), "title": title, "category": c_name, "category_id": int(c_id), "tags": tags, "content": content, "html": html, "summary": markdown.markdown(summary), "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") } db.diaries.save(diary) # save category Category.update_diary(c_id, diary.get('_id'), title, diary.get('publish_time')) if tags is not None: # save tags for tag in tags: Tag.add(tag, diary) return
def start_ontext_manager(user_anwser): """ Запускает контекстный менеджер """ with HTML(output=f'{user_anwser}') as doc: with TopLevelTag("head") as head: with Tag("title") as title: title.text = "hello" head += title doc += head with TopLevelTag("body") as body: with Tag("h1", klass=("main-text", )) as h1: h1.text = "Test" body += h1 with Tag("div", klass=("container", "container-fluid"), id="lead") as div: with Tag("p") as paragraph: paragraph.text = "another test" div += paragraph with Tag("img", is_single=True, src="/icon.png", data_image="responsive") as img: div += img body += div doc += body
def productDir(productName=None, versionName=Tag("setup"), eupsenv=None): """ return the installation directory (PRODUCT_DIR) for the specified product. None is returned if no matching product can be found @param productName the name of the product of interest; if None return a dictionary of all productDirs @param version the desired version. This can in one of the following forms: * an explicit version * a version expression (e.g. ">=3.3") * a Tag instance * None, in which case, the (most) preferred version will be returned. The default is the global tag "setup". @param eupsenv The Eups instance to use to find the product. If not provided, a default will created. """ if productName and versionName == Tag("setup"): # we can take a shortcut return os.environ.get(utils.dirEnvNameFor(productName)) if not eupsenv: eupsenv = Eups() if not productName: tags = None if versionName == Tag("setup"): tags = versionName versionName = "" productList = eupsenv.findProducts(productName, versionName, tags) productDirs = {} for prod in productList: pdir = prod.dir if pdir == "none": pdir = None productDirs[prod.name] = pdir return productDirs prod = eupsenv.findProduct(productName, versionName) if not prod: return None pdir = prod.dir if pdir == "none": pdir = None return pdir
def __init__(self): gtk.GenericCellRenderer.__init__(self) self.height = 0 self.data = None font = gtk.Style().font_desc self.default_size = font.get_size() / pango.SCALE self.labels = graphics.Sprite() self.date_label = graphics.Label(size = self.default_size) self.interval_label = graphics.Label(size = self.default_size) self.labels.add_child(self.interval_label) self.activity_label = graphics.Label(size = self.default_size) self.labels.add_child(self.activity_label) self.category_label = graphics.Label(size = self.default_size) self.labels.add_child(self.category_label) self.description_label = graphics.Label(size = self.default_size) self.labels.add_child(self.description_label) self.duration_label = graphics.Label(size=self.default_size) self.labels.add_child(self.duration_label) default_font = gtk.Style().font_desc.to_string() self.tag = Tag("") self.selected_color = None self.normal_color = None self.col_padding = 10 self.row_padding = 4
def findProduct(productName, versionName=None, eupsenv=None): """ return the specified product. None is returned if no matching product can be found @param productName the name of the product of interest @param version the desired version (default: current). This can in one of the following forms: * an explicit version * a version expression (e.g. ">=3.3") * a Tag instance * None, in which case, the (most) preferred version will be returned. @param eupsenv The Eups instance to use to find the product. If not provided, a default will created. """ if not eupsenv: eupsenv = Eups() if versionName is None: versionName = Tag("current") return eupsenv.findProduct(productName, versionName)
def load_data(file): data = None with open(file) as resource: data = yaml.load(resource, Loader=yaml.FullLoader) tags = list( map(lambda t: Tag(t.get('name'), t.get('input_sentences')), data['tags'])) intents = list( map( lambda i: Intent( name=i.get('name'), answers=i.get('answers'), event=i.get('event'), input_sentences=i.get('input_sentences', []), required_footprints=i.get('required_footprints', []), required_tags=i.get('required_tags', []), footprints=list( map(lambda f: Footprint(f.get('name'), f.get('ttl')), i.get('footprints', '')))), data['intents'])) return (tags, intents)
class FactCellRenderer(gtk.GenericCellRenderer): """ We need all kinds of wrapping and spanning and the treeview just does not cut it""" __gproperties__ = { "data": (gobject.TYPE_PYOBJECT, "Data", "Data", gobject.PARAM_READWRITE), } def __init__(self): gtk.GenericCellRenderer.__init__(self) self.height = 0 self.data = None font = gtk.Style().font_desc self.default_size = font.get_size() / pango.SCALE self.labels = graphics.Sprite() self.date_label = graphics.Label(size = self.default_size) self.interval_label = graphics.Label(size = self.default_size) self.labels.add_child(self.interval_label) self.activity_label = graphics.Label(size = self.default_size) self.labels.add_child(self.activity_label) self.category_label = graphics.Label(size = self.default_size) self.labels.add_child(self.category_label) self.description_label = graphics.Label(size = self.default_size) self.labels.add_child(self.description_label) self.duration_label = graphics.Label(size=self.default_size) self.labels.add_child(self.duration_label) default_font = gtk.Style().font_desc.to_string() self.tag = Tag("") self.selected_color = None self.normal_color = None self.col_padding = 10 self.row_padding = 4 def do_set_property (self, pspec, value): setattr(self, pspec.name, value) def do_get_property(self, pspec): return getattr (self, pspec.name) def on_render (self, window, widget, background_area, cell_area, expose_area, flags): if not self.data: return """ ASCII Art --------------+--------------------------------------------+-------+---+ 13:12 - 17:18 | Some activity - category tag, tag, tag, | 14:44 | E | | tag, tag, some description | | | --------------+--------------------------------------------+-------+---+ """ # set the colors self.selected_color = widget.get_style().text[gtk.STATE_SELECTED] self.normal_color = widget.get_style().text[gtk.STATE_NORMAL] context = window.cairo_create() if isinstance(self.data, FactRow): fact, parent = self.data, None else: parent, fact = self.data, None x, y, width, height = cell_area context.translate(x, y) if flags & gtk.CELL_RENDERER_SELECTED: text_color = self.selected_color else: text_color = self.normal_color self.date_label.color = text_color self.duration_label.color = text_color if parent: self.date_label.text = "<b>%s</b>" % stuff.escape_pango(parent.label) self.date_label.x = 5 if self.data.first: y = 5 else: y = 20 self.date_label.y = y self.duration_label.text = "<b>%s</b>" % stuff.format_duration(parent.duration) self.duration_label.x = width - self.duration_label.width self.duration_label.y = y self.date_label._draw(context) self.duration_label._draw(context) else: self.render_cell(context, (x,y,width,height), widget, flags) def render_cell(self, context, bounds, widget, flags, really = True): if not bounds: return -1 x, y, cell_width, h = bounds self.selected_color = widget.get_style().text[gtk.STATE_SELECTED] self.normal_color = widget.get_style().text[gtk.STATE_NORMAL] g = graphics.Graphics(context) fact = self.data selected = flags and flags & gtk.CELL_RENDERER_SELECTED text_color = self.normal_color if selected: text_color = self.selected_color """ start time and end time at beginning of column """ interval = fact.start_time.strftime("%H:%M -") if fact.end_time: interval = "%s %s" % (interval, fact.end_time.strftime("%H:%M")) self.interval_label.text = interval self.interval_label.color = text_color self.interval_label.x = self.col_padding self.interval_label.y = 2 """ duration at the end """ self.duration_label.text = stuff.format_duration(fact.delta) self.duration_label.color = text_color self.duration_label.x = cell_width - self.duration_label.width self.duration_label.y = 2 """ activity, category, tags, description in middle """ # we want our columns look aligned, so we will do fixed offset from # both sides, in letter length cell_start = widget.longest_interval cell_width = cell_width - widget.longest_interval - widget.longest_duration # align activity and category (ellipsize activity if it does not fit) category_width = 0 self.category_label.text = "" if fact.category: self.category_label.text = " - <small>%s</small>" % stuff.escape_pango(fact.category) if not selected: category_color = graphics.Colors.contrast(text_color, 100) self.category_label.color = category_color else: self.category_label.color = text_color category_width = self.category_label.width self.activity_label.color = text_color self.activity_label.width = None if isinstance(fact, RedmineFactRow): text = fact.name text += " " text += fact.fact.redmine_tag() self.activity_label.text = stuff.escape_pango(text) else: self.activity_label.text = stuff.escape_pango(fact.name) # if activity label does not fit, we will shrink it if self.activity_label.width > cell_width - category_width: self.activity_label.width = (cell_width - category_width - self.col_padding) self.activity_label.ellipsize = pango.ELLIPSIZE_END else: self.activity_label.width = None #self.activity_label.ellipsize = None activity_width = self.activity_label.width y = 2 self.activity_label.x = cell_start self.activity_label.y = y self.category_label.x = cell_start + activity_width self.category_label.y = y x = cell_start + activity_width + category_width + 12 current_height = 0 if fact.tags: # try putting tags on same line if they fit # otherwise move to the next line tags_end = cell_start + cell_width if x + self.tag.width > tags_end: x = cell_start y = self.activity_label.height + 4 for i, tag in enumerate(fact.tags): self.tag.text = tag if x + self.tag.width >= tags_end: x = cell_start y += self.tag.height + 4 self.tag.x, self.tag.y = x, y if really: self.tag._draw(context) x += self.tag.width + 4 current_height = y + self.tag.height + 4 current_height = max(self.activity_label.height + 2, current_height) # see if we can fit in single line # if not, put description under activity self.description_label.text = "" if fact.description: self.description_label.text = "<small>%s</small>" % stuff.escape_pango(fact.description) self.description_label.color = text_color self.description_label.wrap = pango.WRAP_WORD description_width = self.description_label.width width = cell_width - x if description_width > width: x = cell_start y = current_height self.description_label.width = cell_width else: self.description_label.width = None self.description_label.x = x self.description_label.y = y current_height = max(current_height, self.description_label.y + self.description_label.height + 5) self.labels._draw(context) return current_height def on_get_size(self, widget, cell_area): if isinstance(self.data, GroupRow): if self.data.first: return (0, 0, 0, int((self.default_size + 10) * 1.5)) else: return (0, 0, 0, (self.default_size + 10) * 2) context = gtk.gdk.CairoContext(cairo.Context(cairo.ImageSurface(cairo.FORMAT_A1, 0, 0))) area = widget.get_allocation() area.width -= 40 # minus the edit column, scrollbar and padding (and the scrollbar part is quite lame) cell_height = self.render_cell(context, area, widget, None, False) return (0, 0, -1, cell_height)
def render_cell(self, context, bounds, widget, flags, really=True): if not bounds: return -1 x, y, cell_width, h = bounds self.selected_color = widget.get_style().text[gtk.STATE_SELECTED] self.normal_color = widget.get_style().text[gtk.STATE_NORMAL] g = graphics.Graphics(context) fact = self.data selected = flags and flags & gtk.CELL_RENDERER_SELECTED text_color = self.normal_color if selected: text_color = self.selected_color """ start time and end time at beginning of column """ interval = fact.start_time.strftime("%H:%M -") if fact.end_time: interval = "%s %s" % (interval, fact.end_time.strftime("%H:%M")) self.interval_label.text = interval self.interval_label.color = text_color self.interval_label.x = self.col_padding self.interval_label.y = 2 """ duration at the end """ self.duration_label.text = stuff.format_duration(fact.delta) self.duration_label.color = text_color self.duration_label.x = cell_width - self.duration_label.width self.duration_label.y = 2 """ activity, category, tags, description in middle """ # we want our columns look aligned, so we will do fixed offset from # both sides, in letter length cell_start = widget.longest_interval cell_width = cell_width - widget.longest_interval - widget.longest_duration # align activity and category (ellipsize activity if it does not fit) category_width = 0 self.category_label.text = "" if fact.category: self.category_label.text = " - <small>%s</small>" % stuff.escape_pango( fact.category) if not selected: category_color = graphics.Colors.contrast(text_color, 100) self.category_label.color = category_color else: self.category_label.color = text_color category_width = self.category_label.width self.activity_label.color = text_color self.activity_label.width = None self.activity_label.text = stuff.escape_pango(fact.name) # if activity label does not fit, we will shrink it if self.activity_label.width > cell_width - category_width: self.activity_label.width = (cell_width - category_width - self.col_padding) self.activity_label.ellipsize = pango.ELLIPSIZE_END else: self.activity_label.width = None #self.activity_label.ellipsize = None activity_width = self.activity_label.width y = 2 self.activity_label.x = cell_start self.activity_label.y = y self.category_label.x = cell_start + activity_width self.category_label.y = y x = cell_start + activity_width + category_width + 12 current_height = 0 if fact.tags: # try putting tags on same line if they fit # otherwise move to the next line tags_end = cell_start + cell_width tag = Tag(fact.tags[0]) if x + tag.width > tags_end: x = cell_start y = self.activity_label.height + 4 for i, tag in enumerate(fact.tags): tag = Tag(tag) if x + tag.width >= tags_end: x = cell_start y += tag.height + 4 tag.x, tag.y = x, y if really: tag._draw(context) x += tag.width + 4 current_height = y + tag.height + 4 current_height = max(self.activity_label.height + 2, current_height) # see if we can fit in single line # if not, put description under activity self.description_label.text = "" if fact.description: self.description_label.text = "<small>%s</small>" % stuff.escape_pango( fact.description) self.description_label.color = text_color self.description_label.wrap = pango.WRAP_WORD description_width = self.description_label.width width = cell_width - x if description_width > width: x = cell_start y = current_height self.description_label.width = cell_width else: self.description_label.width = None self.description_label.x = x self.description_label.y = y current_height = max( current_height, self.description_label.y + self.description_label.height + 5) self.labels._draw(context) return current_height
def render_cell(self, context, bounds, widget, flags, really = True): if not bounds: return -1 x, y, cell_width, h = bounds self.selected_color = widget.get_style().text[gtk.STATE_SELECTED] self.normal_color = widget.get_style().text[gtk.STATE_NORMAL] g = graphics.Graphics(context) fact = self.data selected = flags and flags & gtk.CELL_RENDERER_SELECTED text_color = self.normal_color if selected: text_color = self.selected_color """ start time and end time at beginning of column """ interval = fact.start_time.strftime("%H:%M -") if fact.end_time: interval = "%s %s" % (interval, fact.end_time.strftime("%H:%M")) self.interval_label.text = interval self.interval_label.color = text_color self.interval_label.x = self.col_padding self.interval_label.y = 2 """ duration at the end """ self.duration_label.text = stuff.format_duration(fact.delta) self.duration_label.color = text_color self.duration_label.x = cell_width - self.duration_label.width self.duration_label.y = 2 """ activity, category, tags, description in middle """ # we want our columns look aligned, so we will do fixed offset from # both sides, in letter length cell_start = widget.longest_interval cell_width = cell_width - widget.longest_interval - widget.longest_duration # align activity and category (ellipsize activity if it does not fit) category_width = 0 self.category_label.text = "" if fact.category: self.category_label.text = " - <small>%s</small>" % stuff.escape_pango(fact.category) if not selected: category_color = graphics.Colors.contrast(text_color, 100) self.category_label.color = category_color else: self.category_label.color = text_color category_width = self.category_label.width self.activity_label.color = text_color self.activity_label.width = None self.activity_label.text = stuff.escape_pango(fact.name) # if activity label does not fit, we will shrink it if self.activity_label.width > cell_width - category_width: self.activity_label.width = (cell_width - category_width - self.col_padding) self.activity_label.ellipsize = pango.ELLIPSIZE_END else: self.activity_label.width = None #self.activity_label.ellipsize = None activity_width = self.activity_label.width y = 2 self.activity_label.x = cell_start self.activity_label.y = y self.category_label.x = cell_start + activity_width self.category_label.y = y x = cell_start + activity_width + category_width + 12 current_height = 0 if fact.tags: # try putting tags on same line if they fit # otherwise move to the next line tags_end = cell_start + cell_width tag = Tag(fact.tags[0]) if x + tag.width > tags_end: x = cell_start y = self.activity_label.height + 4 for i, tag in enumerate(fact.tags): tag = Tag(tag) if x + tag.width >= tags_end: x = cell_start y += tag.height + 4 tag.x, tag.y = x, y if really: tag._draw(context) x += tag.width + 4 current_height = y + tag.height + 4 current_height = max(self.activity_label.height + 2, current_height) # see if we can fit in single line # if not, put description under activity self.description_label.text = "" if fact.description: self.description_label.text = "<small>%s</small>" % stuff.escape_pango(fact.description) self.description_label.color = text_color self.description_label.wrap = pango.WRAP_WORD description_width = self.description_label.width width = cell_width - x if description_width > width: x = cell_start y = current_height self.description_label.width = cell_width else: self.description_label.width = None self.description_label.x = x self.description_label.y = y current_height = max(current_height, self.description_label.y + self.description_label.height + 5) self.labels._draw(context) return current_height
def printProducts(ostrm, productName=None, versionName=None, eupsenv=None, tags=None, setup=False, tablefile=False, directory=False, dependencies=False, showVersion=False, showName=False, depth=None, productDir=None, topological=False, checkCycles=False, raw=False): """ print out a listing of products. Returned is the number of products listed. @param ostrm the output stream to send listing to @param productName restrict the listing to this product @param versionName restrict the listing to this version of the product. @param eupsenv the Eups instance to use; if None, a default will be created. @param tags restrict the listing to products with these tag names @param setup restrict the listing to products that are currently setup (or print actually setup versions with dependencies) @param tablefile include the path to each product's table file @param directory include each product's installation directory @param dependencies print the product's dependencies @param showVersion Only print the product{'s,s'} version[s] (e.g. eups list -V -s afw) @param showName Only print the product{'s,s'} name[s] (e.g. eups list --name -t rhl) @param depth a string giving an expression for determining whether a dependency of a certain depth should be included. This string can be a simple integer which will be taken to mean, print all depths <= that integer. An expression having just a a logical operator and an integer (e.g. "> 3") implies a comparison with the depth of each dependency (i.e. "depth > 3"). @param topological List dependencies in topological-sorted order @param checkCycles Raise RuntimeError if topological sort detects a cycle @param raw Generate "raw" output (suitable for further processing) """ if not eupsenv: eupsenv = Eups() if tags: if isinstance(tags, str): tags = tags.split() checkTagsList(eupsenv, tags) elif setup and not dependencies: tags = ["setup"] # If productDir is provided only list its dependencies; we do this by setting it up if productDir: if not dependencies: raise EupsException("-r only makes sense with --dependencies") if productDir: ups_dir = os.path.join(productDir, "ups") if not os.path.isdir(ups_dir): raise EupsException( "Unable to guess product name as product has no ups directory" ) p = utils.guessProduct(ups_dir) if productName: if p != productName: raise EupsException("Guessed product %s from ups directory, but %s from path" % \ (productName, p)) else: productName = p if tags: tag = tags[0] else: tag = None eupsenv.setup(productName, versionName, productRoot=os.path.abspath(productDir)) setup = True # only list this version productNameIsGlob = productName and re.search( r"[\[\]?*]", productName) # is productName actually a glob? productList = eupsenv.findProducts(productName, versionName, tags) if not productList: if productName: msg = productName if versionName: msg += " %s" % versionName if tags: msg += " tagged \"%s\"" % ", ".join( [Tag(t).name for t in tags]) raise ProductNotFound(productName, versionName, msg="Unable to find product %s" % msg) productList.sort(lambda a, b: cmp(a, b), lambda p: ":".join([p.name, p.version])) if dependencies: _msgs = {} # maintain list of printed dependencies recursionDepth, indent = 0, "" if len(productList) > 1: if setup: productList = eupsenv.getSetupProducts(productName) else: raise EupsException( "Please choose the version you want listed (%s)" % (", ".join([p.version for p in productList]))) else: if topological: raise EupsException( "--topological only makes sense with --dependencies") productTags = {} # list of tags indexed by product oinfo = None # previous value of "info"; used to suppress repetitions due to e.g. # listing directories when there's a NULL and Linux declaration if depth is None: depth = "True" else: try: depth = "<= %d" % int(depth) except ValueError: pass if not re.search(r"depth", depth): depth = "depth" + depth def includeProduct(recursionDepth): """Should we include a product at this recursionDepth in the listing?""" depthExpr = VersionParser(depth) depthExpr.define("depth", recursionDepth) return depthExpr.eval() if dependencies: recursionDepth = 0 product = productList[0] if includeProduct(recursionDepth) and not (checkCycles and not topological): if raw: fmt = "%s|%s" else: fmt = "%-40s %s" print fmt % (product.name, product.version) for product, optional, recursionDepth in eupsenv.getDependentProducts( product, setup, topological=topological, checkCycles=checkCycles): if not includeProduct(recursionDepth) or (checkCycles and not topological): continue if eupsenv.verbose or not _msgs.has_key(product.name): _msgs[product.name] = product.version if not re.search(r"==", depth): indent = "| " * (recursionDepth / 2) if recursionDepth % 2 == 1: indent += "|" if raw: print "%s|%s" % (product.name, product.version) else: print "%-40s %s" % ( ("%s%s" % (indent, product.name)), product.version) return 1 # # See if some tag appears more than once; if so, they are from different stacks # tagsSeen = {} for pi in productList: for t in pi.tags: if not tagsSeen.has_key(t): tagsSeen[t] = {} if not tagsSeen[t].has_key(pi.name): tagsSeen[t][pi.name] = 0 tagsSeen[t][pi.name] += 1 # # Actually list the products # nprod = len(productList) for pi in productList: name, version, root = pi.name, pi.version, pi.stackRoot( ) # for convenience if root == "none": root = " (none)" info = "" if setup: if not eupsenv.isSetup(pi.name, pi.version, pi.stackRoot()): continue else: if not pi._prodStack: # only found in the environment if False: continue # Exclude environment-only products if directory or tablefile: if eupsenv.verbose: if raw: if info: info += "|" info += version else: info += "%-10s" % (version) if directory: if pi.dir: if raw and info: info += "|" info += pi.dir else: info += "" if tablefile: if info: if raw: info += "|" else: info += "\t" if pi.tablefile: info += pi.tablefile else: info += "none" elif showName: if raw: if info: info += "|" info += name else: info += "%-10s" % (name) elif showVersion: info += "%-10s" % (version) else: if raw: if info: info += "|" info += name + "|" + version else: if productName and not productNameIsGlob: info += " " else: info += "%-21s " % (name) info += "%-10s " % (version) if eupsenv.verbose: if raw: if info: info += "|" if eupsenv.verbose > 1: if raw: info += pi.flavor + "|" else: info += "%-10s" % (pi.flavor) if raw: info += root + "|" + pi.dir else: info += "%-20s %-55s" % (root, pi.dir) extra = pi.tags else: extra = [] for t in pi.tags: if not eupsenv.verbose: t = Tag( t).name # get the bare tag name, not e.g. user:foo if tagsSeen.get(t) and tagsSeen[t].get(pi.name) > 1: t = "%s[%s]" % (t, root) extra.append(t) if eupsenv.isSetup(pi.name, pi.version, pi.stackRoot()): extra += ["setup"] if raw and info: info += "|" if extra: if raw: info += ":".join(extra) else: info += "\t" + " ".join(extra) if info: if info != oinfo: print info oinfo = info return nprod