def open(self): mode = 'a+' if self.__append else 'w+' if self.__filename is None: Common.handle_wrong_cmdargs("Error -- no filename given.", self.__action) try: self.__filehandle = codecs.open(self.__filename, mode, encoding='utf-8') if self.__use_the_force: Common.print_if_verbose("Creating " + self.__filename + " ...", self.__verbose) except: Common.handle_wrong_cmdargs("Error on creating/writing to the file.", self.__action) if self.__filehandle is not None and not self.__print_as_plaintext: self.__add_html_header()
def create_file(self): if self.__filename is None: self.__filename = self.__construct_filename() if os.path.isfile(self.__filename) and not self.__append: if not self.__use_the_force: Common.handle_wrong_cmdargs("File " + self.__filename + " already exists. " + \ "Use -f to overwrite or -a to append.", self.__action) else: try: os.remove(self.__filename) Common.print_if_verbose ("Deleted file " + self.__filename + " ...", \ self.__verbose) except: #TODO improve Common.handle_wrong_cmdargs("Couldn't remove file " + self.__filename + \ ". Abort.", self.__action) elif not os.path.isfile(self.__filename) and self.__append: Common.print_if_verbose("Warning: can't append to " + self.__filename + \ " as it doesn't exist", self.__verbose)
# consistency check 101 verbose = args.v use_the_force = args.f append = args.a disable_timestamps = args.d disable_links = args.D print_as_plaintext = args.p enable_embed_video = args.E print_info_only = args.n limit = args.limit filename = args.output yt_id = args.youtube_id yt_url = args.url if limit < 1: Common.handle_wrong_cmdargs("Number of requested comments must be >=1.", \ parser.print_usage) elif yt_id is None and yt_url is None: Common.handle_wrong_cmdargs("You must give either URL or YouTube-ID.", \ parser.print_usage) elif yt_id is not None and yt_url is not None: Common.handle_wrong_cmdargs("You gave both URL and YouTube-ID. Make up your mind.", \ parser.print_usage) elif yt_url is not None and not CF.CommentFetcher.is_valid_url(yt_url): Common.handle_wrong_cmdargs("Invalid URL.", \ parser.print_usage) elif append and use_the_force: Common.handle_wrong_cmdargs("Conflicting flags: append (-a) and overwrite (-f).", \ parser.print_usage) elif append and not print_as_plaintext: Common.handle_wrong_cmdargs("Append mode for HTML files not supported.", \ parser.print_usage)