def replacesubtext(oldfile, newfile, replacements): f = codopen(oldfile, "r", "utf-8") fn = codopen(newfile,"w", "utf-8") for line in f: for rep in replacements: if len(rep) < 2: orig, new = rep[0], "" else: orig, new = rep line = line.replace(orig, new) fn.write(line) f.close() fn.close()
def combine_options_fromfile(defaults, options, fname, scriptpath): fileoptions = {} scriptpath = dirname(abspath(scriptpath)) if not options.noconf: fname = join(scriptpath, fname) if options.conf: fname = options.conf if exists(fname): for x in codopen(fname, "r", "utf-8"): x = x.strip() if x == "": continue elif x.startswith("#"): continue try: setting, value = x.split("=", 1) except ValueError: continue setting = setting.rstrip().lower() value = value.strip() if setting in defaults: if isinstance(defaults[setting], bool): fileoptions[setting] = value.lower() in ("true","1","t","yes") else: fileoptions[setting] = value else: if options.debug: log.warning("Config file (%s) not found." % fname) for option in defaults: if options.__dict__.get(option) == None: options.__dict__[option] = fileoptions.get(option, defaults[option])
def checksums(fname, method): f = codopen(fname,"rb","utf-8") mode = None if splitext(path)[1] == ".crc" or splitext(path)[1] == ".sfv": mode = "crc" else: mode = "md5" lineno = 0 for line in f: if line.startswith("#") or line.startswith(";"): lineno += 1 continue line = line.strip() hash = None if mode == "crc": try: fname, hash = line.rsplit(None, 1) except: log.warn("Invalid line (%s): %s" % (lineno, line)) Globals.invalid += 1 else: try: hash, fname = line.split(None, 1) except: log.warn("Invalid line (%s): %s" % (lineno, line)) Globals.invalid += 1 lineno += 1 if isabs(fname): if exists(fname): compsum(fname,hash,mode) else: LogUtil.printlog("File not found: %s" % fname) Globals.notfound += 1 else: if Globals.options.aster: if fname[0] == "*": fname = fname[1:] if exists(join(Globals.cwd, fname)): compsum(join(Globals.cwd, fname),hash,mode) else: LogUtil.printlog("File not found: %s" % fname)
def extractsub(path, options): dir, file = split(path) log.info("** Processing: %s **" % file) exe = join(options.mkvtoolnix,"mkvinfo.exe") chdir(dir) output = runcommand('"%s" "%s"' % (exe,file), True) subs, fileinfo = _getsublist(output) outnames = [] for track in subs: log.info("** Extracting track: %s Default: %s **" % (track.num, track.default)) outnames.append('%s:"%s.%s%s%s"' % (track.num, splitext(file)[0], track.num, ".default" if track.default else "", track.ext)) exe = join(options.mkvtoolnix,"mkvextract.exe") runcommand('"%s" tracks "%s" %s' % (exe,file, " ".join(outnames))) tempfiles = [] if options.file: r = codopen(options.file,"r","utf-8") reps = [] for x in r: reps.append(x.strip().split('\t')) r.close() muxoptions = [] for track in subs: if track.ext != "." and track.ext != "": oldfile = "%s.%s%s%s" % (splitext(file)[0], track.num, ".default" if track.default else "", track.ext) newfile = "%s.%s%s%s" % (splitext(file)[0], track.num, ".default" if track.default else "", "_replaced" + track.ext) move(oldfile, oldfile + "_old") oldfile = oldfile+"_old" tempfiles.append(oldfile) tempfiles.append(newfile) replacesubtext(oldfile, newfile, reps) #TrackThings.subtemplate # {lang}{trackname}{default}{fname} if options.default is None: muxoptions.append(TrackThings.subtemplate.format(lang=track.lang, trackname=track.name, default="yes" if track.default else "no", fname=newfile)) else: if track.num == options.default: muxoptions.append(TrackThings.subtemplate.format(lang=track.lang, trackname=track.name, default="yes", fname=newfile)) else: muxoptions.append(TrackThings.subtemplate.format(lang=track.lang, trackname=track.name, default="no", fname=newfile)) # if remux do things here if options.remux: #--segment-uid exe = join(options.mkvtoolnix,"mkvmerge.exe") #make backup folder if exists("backups"): if not isdir("backups"): log.critical('"backups" is not a dir, bailing') exit(1) else: mkdir("backups") fn, ext = splitext(file) newfname = "%s%s%s" % (fn, options.suffix, ext) if options.removecrc: newfname = TrackThings.regexCRC.sub("", newfname) oldfname = "%s-old%s" % (fn, ext) oldfname = join(dir, "backups", oldfname) move(path, oldfname) command = u'{cmd} -o "{fname}" {oldsubs} "{oldfname}" {subs}' log.info("** REMUXING FILE **") runcommand(command.format(cmd=exe, fname=file, oldsubs="-S" if not options.keeporig else "", oldfname=oldfname, subs=" ".join(muxoptions))) #also put subfiles into backup for z in tempfiles: move(join(dir,z), join(dir,"backups",z)) if not options.noclean: try: rmtree("backups") except WindowsError: try: rmtree("backups") except WindowsError as e: log.error("%s" % str(e))
exit(1) fname = argv[1] enc = "sjis" bom = False if len(argv) == 3: if argv[2] == "BOM": bom = True else: enc = argv[2] if len(argv) == 4: if argv[2] == "BOM": bom = True enc = argv[3] sfname = splitext(fname) if bom: #put BOM mark 'cause foobar is lame fw = open(sfname[0]+"-utf8"+sfname[1],"w") fw.write(BOM_UTF8) fw.close() fr = codopen(fname,"r",enc) fw = None if not bom: fw = codopen(sfname[0]+"-utf8"+sfname[1],"w","utf-8") else: fw = codopen(sfname[0]+"-utf8"+sfname[1],"a","utf-8") fw.write(fr.read()) fw.close() fr.close()
options.method = options.method.lower() if options.method != "crc" and options.method != "md5": options.method = "crc" if options.create: mode = "create" log.info("Creating...") elif options.contcreate: mode = "Continue" if not options.output: log.error("Continue create doesn't really make sense without an output file. Bailing.") exit(1) log.warn("WARNING: ATTEMPING TO APPEND TO (%s). You should probably backup this file. \nPress button to continue." % options.output) raw_input() copyfile(options.output, options.output+".tmp") Globals.tmpfile = codopen(options.output+".tmp", "r", "utf-8") LogUtil.f = codopen(options.output, "a", "utf-8") else: log.info("Checking...") if not options.contcreate: if options.output: if exists(options.output): log.warn("WARNING: OVERWRITE (%s)? (y/n)" % options.output) yesno = raw_input() if yesno != "y": log.info("Bailing...") exit() LogUtil.f = codopen(options.output, "w", "utf-8") if options.verbose:
def processBuildings(fn): with codopen(fn, 'rb', 'utf-8') as csvfile: for row in unicode_csv_reader(csvfile): BUILDING_MAP[row[0].strip()] = (row[2].strip(), row[3].strip(), buildfacs(row[4:15])) # addr, type, facs
def process(fn): names = {} # { (first, last) : TempPerson} sessions = {} # { code : TempSession } count = 1 with codopen(fn, 'rb', 'utf-8') as csvfile: for row in unicode_csv_reader(csvfile): if row[0].strip() == 'CODE': continue name = ("%s" % row[6].strip(), "%s" % row[7].strip()) type = "PRESENTER" code = row[0] sessname = row[4].strip() location = processRoom(row[38:40]) loctype = str(row[34].strip()) day = "T" if code[0] in ("A", "B", "C") else "F" #phone = processPhone(str(row[24].strip()), str(row[25].strip())) org = row[8].strip() email = str(row[9].strip().lower()) facilities_req = "\n".join(processFacs(row[35])) facilities_got = "\n".join(processFacs(row[36])) equipment = processEquip(row[36]) handouts_said, handouts_did = processHandouts(row[42:44]) p = None if name != ("", ""): if name in names: p = names[name] else: p = TempPerson(*name) names[name] = p p.email.add(email) #~ for ph in phone: #~ p.phone.add(ph) p.sessions.append((code, type)) s = None if code in sessions: s = sessions[code] else: s = TempSession(code, day) sessions[code] = s s.name = sessname s.locations.add(location) s.loctypes.add(loctype) s.handouts_said.add(handouts_said) s.handouts_did.add(handouts_did) if facilities_req: s.facilities_req.add(facilities_req) if facilities_got: s.facilities_got.add(facilities_got) count += 1 copres = [] # process co-presenters part1 if row[11].strip(): copres += processCoPresent(row[11:15]) #first, last, org, email # process co-presenters part2 if row[15].strip(): copres += processCoPresent(row[16:19]) for ip in copres: name = (ip.firstname, ip.lastname) if name not in names: names[name] = ip else: ip = names[name] type = "COPRESENTER" ip.sessions.append((code, type)) return (names, sessions)
def process(fn): names = {} # { (first, last) : TempPerson} sessions = {} # { code : TempSession } count = 1 with codopen(fn, 'rb', 'utf-8') as csvfile: for row in unicode_csv_reader(csvfile): if row[0].strip() == 'SubmissionID': continue # CHECK ROW[10] OVER name = (u"%s" % row[7].strip(), u"%s" % row[8].strip()) type = PRESTYPEMAP.get("Presenter", PersonType.other) #code = processCode(row[0].strip(), row[1].strip()) code = row[1].strip() if "CANCELLED" in code: continue if not code: continue for code in code.split("&"): code = code.strip() title = row[4].strip() building = row[3].strip() if not building: building = "1" room = row[2].strip() #loctype = str(row[35].strip()) sessname = row[4].strip() day = "T" if code[0] in ("A", "B", "C") else "F" phone = str(row[10].strip()) org = row[8].strip() email = str(row[9].strip().lower()) facilities_req = "\n".join(processFacsCells(row[47:54])) try: if "&" in room: troom = room.split("&",1)[0].strip() facilities_got = BUILDING_MAP[troom][2] address = BUILDING_MAP[troom][0] loctype = BUILDING_MAP[troom][1] else: facilities_got = BUILDING_MAP[room][2] address = BUILDING_MAP[room][0] loctype = BUILDING_MAP[room][1] except: print "AAA: %r" % row print "KEYS: %r" % BUILDING_MAP.keys() raise #facilities_got = ["?"] #equipment = processEquip(row[36]) #handouts_said, handouts_did = processHandouts(row[43:45]) handouts_said = HANDOUTS[row[56].strip()] #handouts = handouts_did p = None if name != ("", ""): if name in names: p = names[name] else: p = TempPerson(*name) names[name] = p p.email.add(email) #~ for ph in phone: p.phone.add(phone) p.sessions.append((code, type)) s = None if code in sessions: s = sessions[code] else: s = TempSession(code, day) sessions[code] = s s.name = sessname #s.location = location s.loctype = loctype s.address = address s.building = building s.room = room s.handouts_said = handouts_said s.handouts = None s.facilities_req = facilities_req if facilities_req else None s.facilities_got = facilities_got if facilities_got else None copres = [] # process co-presenters part1 if row[64].strip(): if p and row[64].strip() != p.firstname: copres.append(processCoPresent(row[64:69], ["FIRSTNAME", "LASTNAME", "EMAIL", "ORG", "EMAIL"], type=PRESTYPEMAP.get(row[70].strip(), PersonType.other))) #first, last, email, org, email again # process co-presenters part2 if row[13].strip(): copres.append(processCoPresent(row[13:17], ["NAME", "ORG", "EMAIL", "PHONE"], type=PersonType.copresenter)) # process co-presenters part2 cont... if row[18].strip(): copres.append(processCoPresent(row[18:22], ["NAME", "ORG", "EMAIL", "PHONE"], type=PersonType.copresenter)) # process co-presenters part2 cont again... if row[23].strip(): copres.append(processCoPresent(row[23:27], ["NAME", "ORG", "EMAIL", "PHONE"], type=PersonType.copresenter)) # process co-presenters part2 cont again again... if row[28].strip(): copres.append(processCoPresent(row[28:32], ["NAME", "ORG", "EMAIL", "PHONE"], type=PersonType.copresenter)) for ip in copres: name = (ip.firstname, ip.lastname) oip = ip if name not in names: names[name] = ip else: ip = names[name] exists = False for session in ip.sessions: if code in session: exists = True break if exists: continue ip.sessions.append((code, oip.type)) count += 1 return (names, sessions)
def process(fn): names = {} # { (first, last) : TempPerson} sessions = {} # { code : TempSession } count = 1 with codopen(fn, 'rb', 'utf-8') as csvfile: for row in unicode_csv_reader(csvfile): if row[0].strip() == 'CODE': continue # CHECK ROW[10] OVER name = ("%s" % row[6].strip(), "%s" % row[7].strip()) type = "PRESENTER" #code = processCode(row[0].strip(), row[1].strip()) code = row[0].strip() sessname = row[4].strip() location = processRoom(row[39:41]) loctype = str(row[35].strip()) day = "T" if code[0] in ("A", "B", "C") else "F" phone = str(row[10].strip()) org = row[8].strip() email = str(row[9].strip().lower()) facilities_req = "\n".join(processFacs(row[36].strip())) facilities_got = "\n".join(processFacs(row[37].strip())) #equipment = processEquip(row[36]) handouts_said, handouts_did = processHandouts(row[43:45]) handouts = handouts_did p = None if name != ("", ""): if name in names: p = names[name] else: p = TempPerson(*name) names[name] = p p.email.add(email) #~ for ph in phone: p.phone.add(phone) p.sessions.append((code, type)) s = None if code in sessions: s = sessions[code] else: s = TempSession(code, day) sessions[code] = s s.name = sessname s.location = location s.loctype = loctype s.handouts_said = handouts_said s.handouts = handouts s.facilities_req = facilities_req if facilities_req else None s.facilities_got = facilities_got if facilities_got else None copres = [] # process co-presenters part1 if row[12].strip(): copres += processCoPresent(row[12:16]) #first, last, org, email # process co-presenters part2 if row[17].strip(): copres += processCoPresent(row[17:20]) for ip in copres: name = (ip.firstname, ip.lastname) if name not in names: names[name] = ip else: ip = names[name] type = "COPRESENTER" ip.sessions.append((code, type)) count += 1 return (names, sessions)
parser.add_option("--min-size", dest="minsize", metavar="SIZE", default=None, help="Files/dirs will need to be at least this big before being printed. Size is in bytes. Default: no limit. If you need to overrite your config, you can use 0 to mean the same.") parser.add_option("--max-size", dest="maxsize", metavar="SIZE", default=None, help="Files/dirs will need to be at most this big before being printed. Size is in bytes. Default: no limit. If you need to overrite your config, you can use 0 to mean the same.") parser.add_option("--sort-by", dest="sortby", default=None, help="Sort by one of the following: name size. Default: name") parser.add_option("--debug", dest="debug", default=None, help="Print some debug infos. 0=nothing 1=print 2=file 3=both") (options, args) = parser.parse_args() #grab defaults from file, and then override with commandlines. combine_options_fromfile(defaults, options, "treeprinter.conf", __file__) if options.output: LogUtil.f = codopen(options.output, "w", "utf-8") if options.debug: log.level=DEBUG try: options.rounding = int(options.rounding) except: log.warn("Rounding not int, using default: 5") options.rounding = int(defaults["rounding"]) if options.depth: try: options.depth = int(options.depth) except: log.warn("Max depth not int, using default: none (-1)") options.depth = -1 else: options.depth = -1 if options.minsize:
help="Read STDIN from INFILE") parser.add_option("-n", "--no-stdin", dest="nostdin", action="store_true", help="Do not read from STDIN") parser.add_option("-s", "--wait", type="float", dest="wait", metavar="FLOAT", help="Wait for FLOAT seconds before passing STDIN to program") parser.add_option("--no-filefirst", dest="nofilefirst", action="store_true", help="Read STDIN from normal STDIN before file. (Default is read STDIN from file before normal STDIN)") (options, args) = parser.parse_args() if not args: print "MISSING PROGRAM TO LAUNCH (and it's params.) Exiting." exit(1) buff = [] if options.infile: try: f = codopen(options.infile, "r", "utf-8") except: print "Unable to open file (%s) for reading. Exiting." % options.infile exit(1) buff.append(f) if not options.nostdin: if options.nofilefirst: buff.insert(0, stdin) else: buff.append(stdin) if not options.wait: options.wait = 0 print "Starting input thread" console = Queue()
See: https://packaging.python.org/en/latest/distributing.html https://github.com/pypa/sampleproject """ # Always prefer setuptools over distutils from setuptools import setup, find_packages # To use a consistent encoding from codecs import open as codopen from os import path here = path.abspath(path.dirname(__file__)) # Get the long description from the README file with codopen(path.join(here, 'README.md'), encoding='utf-8') as f: long_description = f.read() # Arguments marked as "Required" below must be included for upload to PyPI. # Fields marked as "Optional" may be commented out. setup( # This is the name of your project. The first time you publish this # package, this name will be registered for you. It will determine how # users can install this project, e.g.: # # $ pip install sampleproject # # And where it will live on PyPI: https://pypi.org/project/sampleproject/ # # There are some restrictions on what makes a valid project name