def find(directory, pattern=None, exclude=None): class Pattern: def __init__(self, exp): self.__exp__ = exp def __eq__(self, b): ret = fnmatch.fnmatch(b, self.__exp__) # import pdb; ret and pdb.set_trace() return ret debg('Looking for paths in %r matching %r' % (directory, pattern)) matches = [] misses = [] if exclude is None: exclude = [] else: exclude = [Pattern(i) for i in exclude] directory = os.path.abspath(directory) for root, dirs, files in os.walk(directory): for basename in dirs + files: if basename in exclude: if basename in dirs: dirs.remove(basename) continue path = os.path.join(root, basename) if pattern is None or re.search(pattern, path): matches.append(path) else: misses.append(path) debg('Found %d matches and %d misses' % (len(matches), len(misses))) return matches, misses
def parse_markdown(cls, fname: Text) -> 'FMXml': """parse Nodes from markdown """ ret = FMXml() buf: List[Text] = [] for line in cls.get_lines(fname): line = line.rstrip("\r\n") n = cls.is_section_line(line, buf) if n == 0: buf.append(line) continue debg("found {}-{}".format(n, line.strip())) if len(buf) < 1: buf = [line] continue if n in (n_level_1st, n_level_2nd): # === or --- # exclude the last line. bf2 = buf[:-1] buf = [buf[-1], line] else: bf2 = buf buf = [line] ret.parse_markdown_build_hier(bf2) if len(ret.root.children) < 1: return ret ret.parse_markdown_build_hier(buf) # parse left data... return ret
def run(cmd, exit=True, cwd=None): debg(cmd) if subprocess.Popen(cmd.split(), cwd=cwd).wait() != 0: if exit: crit('Failed!') sys.exit(1) else: eror('Ignoring failure.')
def rm(path): debg('Deleting %r' % path) try: if os.path.isdir(path): shutil.rmtree(path) else: os.remove(path) except OSError: pass
def pickup(src, dst, later=False): _dst = os.path.join(dst, os.path.basename(src)) if os.path.islink(src): # keep soft-link if later: return src linkto = os.readlink(src) os.symlink(linkto, _dst) return debg("pickup: %s" % src) shutil.copy2(src, _dst)
def insert_node(self, nod: Node) -> None: # {{{1 cur = self.cur res = nod.level_diff(cur) debg("{}-{}-{}".format(res, cur, nod)) if res == 0: cur.append_to_parent(nod, self.root) elif res < 0: # new < cur -> drill up self.hier_insert_and_up(cur, nod) else: # new > cur -> drill down cur.append(nod) self.cur = nod
def install_py4a(pwd): # {{{1 class PathInfo: def __init__(self, file_or_dir, relSrc, relDst): self.file_or_dir = file_or_dir self.src = relSrc self.dst = relDst class PkgInfo: def __init__(self, seq, build, *items): self.path_seq = seq self.build = build self.items = items path_libs = cfg.dest + "/python-ext/python" run("mkdir -p %s" % path_libs) pkgs = { 'Installing xmppy.': PkgInfo( ('xmpppy', 'xmpp'), False, PathInfo(False, '', 'xmpp')), 'Installing BeautifulSoup.': PkgInfo( ('BeautifulSoup', ), False, PathInfo(True, 'BeautifulSoup.py', '')), 'Installing gdata.': PkgInfo( ('gdata', ), True, PathInfo(False, "build/lib/gdata", "gdata"), PathInfo(False, "build/lib/atom", "atom")), # disable twitter in 2015. # 'Installing python-twitter.': PkgInfo( # ('python-twitter', ), False, # PathInfo(True, 'twitter.pyc', '')), # 'Installing simplejson.': PkgInfo( # ('python-twitter', 'simplejson'), False, # PathInfo(False, "", 'simplejson')), 'Installing setuptools.': PkgInfo( ('setuptools', ), False, PathInfo(True, "*.pyc", 'site-packages'), PathInfo(False, "setuptools", 'setuptools')), } for title, v in pkgs.items(): info(title) path = os.path.join(*((pwd, 'python-libs') + v.path_seq)) if v.build: run('python setup.py build', cwd=path) debg("compile: %s" % (path, )) # compileall.compile_dir(path, quiet=True) # do it after. for item in v.items: src = os.path.join(path, item.src) dst = os.path.join(path_libs, item.dst) if not item.file_or_dir: # dir shutil.copytree(src, dst) continue # file to dir for fname in glob.glob(src): shutil.copy(fname, dst)
def install_py4a(pwd): # {{{1 class PathInfo: def __init__(self, file_or_dir, relSrc, relDst): self.file_or_dir = file_or_dir self.src = relSrc self.dst = relDst class PkgInfo: def __init__(self, seq, build, *items): self.path_seq = seq self.build = build self.items = items path_libs = cfg.dest + "/python-ext/python" run("mkdir -p %s" % path_libs) pkgs = { 'Installing xmppy.': PkgInfo( ('xmpppy', 'xmpp'), False, PathInfo(False, '', 'xmpp')), 'Installing BeautifulSoup.': PkgInfo( ('BeautifulSoup', ), False, PathInfo(True, 'BeautifulSoup.pyc', '')), 'Installing gdata.': PkgInfo( ('gdata', ), True, PathInfo(False, "build/lib/gdata", "gdata"), PathInfo(False, "build/lib/atom", "atom")), # disable twitter in 2015. # 'Installing python-twitter.': PkgInfo( # ('python-twitter', ), False, # PathInfo(True, 'twitter.pyc', '')), # 'Installing simplejson.': PkgInfo( # ('python-twitter', 'simplejson'), False, # PathInfo(False, "", 'simplejson')), 'Installing setuptools.': PkgInfo( ('setuptools', ), False, PathInfo(True, "*.pyc", 'site-packages'), PathInfo(False, "setuptools", 'setuptools')), } for title, v in pkgs.items(): info(title) path = os.path.join(*((pwd, 'python-libs') + v.path_seq)) if v.build: run('python setup.py build', cwd=path) debg("compile: %s" % (path, )) # compileall.compile_dir(path, quiet=True) # do it after. for item in v.items: src = os.path.join(path, item.src) dst = os.path.join(path_libs, item.dst) if not item.file_or_dir: # dir shutil.copytree(src, dst) continue # file to dir for fname in glob.glob(src): shutil.copy(fname, dst)
def restruct(self, mode: runmode) -> List[Nod1]: # {{{1 Node.key_attr_mode = mode debg("rest:mode={}-{}".format(mode, len(self.root.children))) n = 0 ret: List[Nod1] = [] for node in self.root.children: if not isinstance(node, FMNode): warn("rest:ignored-node={}".format(node.name)) # ret.append(node) continue n += 1 seq_flat = node.flattern("", exclude_self=False) debg("rest:flat:{}".format(len(seq_flat))) for i in seq_flat: debg("rest:sort:{}".format(i.name)) ret.extend(seq_flat) ret = self.restruct_dup_root(ret, mode) ret.sort(key=Node.key_attr) ret = HierBuilder().restruct(ret) # insert header and footer if len(ret) < 1 or Node.level(ret[0], mode) != cmn.lvl_root: ret.insert(0, Chars("\n")) ret.insert(0, Nod1("node", {"TEXT": mode.t()}).enter_only(True)) ret.append(LNode("node")) ret.insert(0, Chars("\n" + cmn.cmt_header + "\n")) ret.insert(0, Nod1("map", {"version": "1.1.0"}).enter_only(True)) # ret.append(Chars("\n")) # don't need, see Node.compose() ret.append(Chars("\n")) ret.append(LNode("map")) debg("rest:ret={}".format(len(ret))) return ret
def output(self, fname: Text) -> int: # {{{1 debg("out:open:" + fname) seq = self.root.children with open(fname, "wt") as fp: fp.write('<map version="1.1.0">\n') fp.write(cmn.cmt_header + '\n') fp.write('<node TEXT="document">\n') prv: Node = NodeDmy() for node in seq: text = node.compose(prv) # debg("out:" + text) fp.write(text) prv = node fp.write('</node>\n</map>\n') return 0
def append(self, nod: 'Node') -> None: # {{{1 if self.name == "root": # root will not have level_diff() dif = 0 else: dif = self.level_diff(nod) debg("append: {}".format(dif)) # nod_dummy, n = self, self.n_level nod_dummy = self for i in range(dif - 1): # n += n_level_unit nod_dummy_child = Node("### dummy paragraph ###", {}) nod_dummy.children.append(nod_dummy_child) nod_dummy_child.parent = nod_dummy nod_dummy = nod_dummy_child nod_dummy.children.append(nod) nod.parent = nod_dummy
def leave_tag(self, name: Text) -> None: # {{{1 if self.cur_rich is not None: if name == "richcontent": self.cur_rich = None else: self.cur_rich.leave_tag(name) return if name == "node": assert self.cur.parent is not None debg("cls node:" + self.cur.id_string) self.cur = self.cur.parent return nod = self.cur.children[-1] if nod.name == name: return nod = LNode(name) self.cur.children.append(nod)
def output(self, fname: Text, mode: runmode) -> int: # {{{1 debg("out:open:" + fname) if mode == runmode.through: seq = self.root.children HierBuilder().mark_backup(seq, "root") else: seq = self.restruct(mode) if self.n_output_markdown >= 0: with open(fname, "wt") as fp: fp.write("") return self.output_markdown(fname, seq, self.n_output_markdown) with open(fname, "wt") as fp: prv: Nod1 = NodeDmy() for node in seq: text = node.compose(prv) debg("out:" + text) fp.write(text) prv = node return 0
def enter_tag(self, name: Text, attrs: Dict[Text, Text]) -> None: # {{{1 if name == "node": self.f_header = False node = FMNode(attrs) node.parent = self.cur self.cur.children.append(node) self.cur = node debg("new node:" + node.id_string) return if self.cur_rich is not None: self.cur_rich.enter_tag(name, attrs) return elif name == "map": # TODO(shimoda): dirty, change parse procedures. nod: Nod1 = Node(name, attrs) nod.f_enter_only = True elif name != "richcontent": nod = Node(name, attrs) else: nod = self.cur_rich = NodeNote("") self.cur.children.append(nod)
def zipup(out_path, in_path, top, exclude=None, prefix=''): info("zipup: %s" % out_path) # Remove an existing zip file. rm(out_path) zip_file = zipfile.ZipFile(out_path, 'w', compression=zipfile.ZIP_DEFLATED) for path in find(in_path, exclude=exclude)[0]: if os.path.islink(path): dest = os.readlink(path) attr = zipfile.ZipInfo() attr.filename = prefix + path[len(top):].lstrip('/') attr.create_system = 3 # long type of hex say, symlink attr magic... attr.external_attr = 0xA1ED0000L zip_file.writestr(attr, dest) elif not os.path.isdir(path): arcname = prefix + path[len(top):].lstrip('/') debg('Adding %s to %s' % (arcname, out_path)) zip_file.write(path, arcname) zip_file.close()
def compose(self, prv: Nod1) -> Text: # {{{1 debg("compose:node:" + self.id_string) ret = '<node CREATED="{}" ID="{}" MODIFIED="{}"'.format( self.ts_create, self.id_string, self.ts_modify) if self.position: ret += ' POSITION="{}"'.format(self.position) ret += ' TEXT="{}"'.format(cmn.quote_attr(self.text)) if len(self.children) < 1: ret += "/>\n" else: ret += ">" prv_child: Nod1 = NodeDmy() for nod in self.children: if self.f_no_backup and (nod.name == "attribute" and nod.attr["NAME"] == "backup"): continue ret += nod.compose(prv_child) prv_child = nod ret += '</node>\n' return ret
def compose(self, prv: Node) -> Text: # {{{1 debg("compose:node:" + Text(self.n_level)) ret = '<node CREATED="{}" ID="{}" MODIFIED="{}"'.format( -1, int(time.time() * 1000), -1) ret += ' TEXT="{}"'.format(cmn.quote_attr(self.title)) if len(self.note) > 0: node = NodeNote(self.note) self.children.insert(0, node) self.children.insert(0, cmn.Chars("\n")) self.children.insert(0, self.attr_section_number(prv)) self.children.insert(0, cmn.Chars("\n")) self.children.insert(0, self.attr_level_score()) if len(self.children) < 1: ret += "/>\n" else: ret += ">\n" prv_child: Node = NodeDmy() for nod in self.children: ret += nod.compose(prv_child) prv_child = nod ret += '</node>\n' return ret
def parse_xconfline(cls, src): # cls {{{1 # type: (Text) -> Optional[Tuple[Text, NProp]] _src = src.strip() if _src.startswith("#"): return None # comment line if not _src.lower().startswith("option "): return None # not option line. _src = _src[8:].strip() # remove 'Option' with starting '"'. debg("NProp.xconf-parse: {}".format(_src)) for key, prop in cls.props(): idx = 0 for n, (opt, fmt) in enumerate(prop.fmts): o = opt.lower() + '" ' if not _src.lower().startswith(o): idx += prop.fmts.count_1fmt((opt, fmt)) continue debg("NProp.xconf-parse: match with {}".format(o)) _src = _src[len(o):] _src = cls.parse_quote(_src) ret = prop.copy(clear_vals=True) for n, v in cls.parse_xconfopt(idx, fmt, _src): ret.vals[n] = v return opt, ret return None
def main(): # {{{1 # type: () -> int logging.basicConfig(format="%(levelname)-8s:%(asctime)s:%(message)s") n_props = NPropDb.auto_id() global gui debg("fetch settings, options and arguments...") opts = options() if opts is None: eror("can't found Synaptics in xinput.") return 1 debg("create properties DB...") if n_props < 3: eror("can't found Synaptics properties in xinput.") return 2 debg("build GUI...") gui = buildgui(opts) gui.root.after_idle(gui.callback_idle) # type: ignore # for Tk debg("start gui...") gui.root.mainloop() # type: ignore # for Tk return 0
def zip_extract(fname: Text) -> Text: # {{{1 ret = tempfile.mktemp(".xml", "fmmulti", dir=".") try: debg("zip:" + fname) zf = ZipFile(fname) except Exception as ex: debg("zip:" + fname + "->" + Text(ex)) return "" try: for zi in zf.infolist(): debg("found entry {}".format(zi)) if zi.is_dir(): # type: ignore # no `is_dir` in python2 continue with open(ret, "w") as fp: fp.write(zf.extract(zi)) return ret finally: zf.close() return ""
def gui_canvas( inst, btns, # {{{2 vals, prms): # type: (tk.Canvas, List[str], List[int], List[List[int]]) -> None if gui is None: return _20 = 20 _35 = 35 _40 = 40 _45 = 45 _55 = 55 _60 = 60 _65 = 65 _80 = 80 draw.rectangle(inst, 0, 0, _100, _100, fill='white') # ,stipple='gray25') if len(prms) > 0: edges = prms[0] gui.ex1, gui.ey1 = gui_scale(edges[0], edges[2]) gui.ex2, gui.ey2 = gui_scale(edges[1], edges[3]) # print("gui_canvas: edge: ({},{})-({},{})".format(x1, y1, x2, y2)) areas = prms[1] gui.s1x1, gui.s1y1, gui.s1x2, gui.s1y2 = gui_softarea(areas[0:4]) gui.s2x1, gui.s2y1, gui.s2x2, gui.s2y2 = gui_softarea(areas[4:8]) debg("gui_canvas: RB: ({},{})-({},{})".format(gui.s1x1, gui.s1y1, gui.s1x2, gui.s1y2)) debg("gui_canvas: MB: ({},{})-({},{})".format(gui.s2x1, gui.s2y1, gui.s2x2, gui.s2y2)) if gui.s1x1 != gui.s1x2 and gui.s1y1 != gui.s1y2: draw.rectangle(inst, gui.s1x1, gui.s1y1, gui.s1x2, gui.s1y2, fill="green") # area for RB if gui.s2x1 != gui.s2x2 and gui.s2y1 != gui.s2y2: draw.rectangle(inst, gui.s2x1, gui.s2y1, gui.s2x2, gui.s2y2, fill="blue") # area for MB draw.rectangle(inst, gui.ex1, gui.ey1, gui.ex2, gui.ey2, width=2) # +-++++++-+ # | |||||| | (60 - 30) / 3 = 10 draw.rectangle(inst, _20, _20, _80, _80, fill='white') draw.rectangle(inst, _35, _20, _45, _45, fill=btns[0]) draw.rectangle(inst, _45, _20, _55, _45, fill=btns[1]) draw.rectangle(inst, _55, _20, _65, _45, fill=btns[2]) # inst.create_arc(_20, _20, _80, _40, style='arc', fill='white') # inst.create_line(_20, _40, _20, _80, _80, _80, _80, _40) draw.rectangle(inst, _40, _55, _60, _60, fill=btns[5]) draw.rectangle(inst, _40, _60, _60, _65, fill=btns[6]) x, y = gui_scale(vals[0], vals[1]) draw.oval(inst, x - 2, y - 2, x + 2, y + 2, fill="black") x, y = gui_scale(vals[2], vals[3]) x, y = x % _100, y % _100 draw.oval(inst, x - 2, y - 2, x + 2, y + 2, fill="red")
def run(self): logging.debug("Thread_initALARMSO: STARTING") try: # this would be the eeker.alarmso as opposed to the alarm dict with open("DATABASE-ALARMSo.pkl", "rb") as f: eeker.db_alarmso = pickle.load(f) logging.info( "Thread_initALARMSO" + "\t" + "eeker.db_alarmso PKL: Success. Size of eeker.db_alarmso = " + str(len(eeker.db_alarmso)) ) logging.debug( "Thread_initALARMSO" + "\t" + "eeker.db_alarmso PKL: Type of eeker.db_alarmso = " + str(type(eeker.db_alarmso)) ) logging.debug( "Thread_initALARMSO" + "\t" + "eeker.db_alarmso PKL: Timestamp of eeker.db_alarmso = " + eeker.db_alarmso.timeupdated_alarm ) except Exception as e: logging.critical("Thread_initALARMSO" + "\t" + "Exception loading eeker.db_alarmso pickle: " + str(e)) logging.debug("Thread_initALARMSO" + "\t" + "Starting the 'objectify_alarms' function....") try: """failing to load from disk means fresh pull of alarms from file configured in pmConfig. The eeker.db_alarmso is built from the existing alarm dictionary. Need to fix this later and get rid of the dictionary""" initALARMSO() except Exception as r: logging.debg("Thread_initALARMSO" + "\t" + "Exception running initALARMSO(): " + str(r)) # finally we check to see if we have data try: logging.debug( "Thread_initALARMSO" + "\t" + "eeker.db_alarmso FRESH: Length of eeker.db_alarmso : " + str(len(eeker.db_alarmso)) ) logging.debug( "Thread_initALARMSO" + "\t" + "eeker.db_alarmso FRESH: Type of eeker.db_alarmso = " + str(type(eeker.db_alarmso)) ) logging.debug( "Thread_initALARMSO" + "\t" + "eeker.db_alarmso FRESH: Timestamp of eeker.db_alarmso = " + eeker.db_alarmso.timeupdated_alarm ) except Exception as ee: logging.debug( "Thread_initALARMSO" + "\t" + "Tried to pull stats on eeker.db_alarmso but got exception: " + str(ee) ) # worst case scenario we just blank out the alarms eeker.dblock.acquire() eeker.db_alarmso = pmClasses.SuperList() eeker.dblock.release() logging.debug("Thread_initALARMSO: STOPPING")