def __init__(self, f_obj): f_obj = FileIO(f_obj.name) super(BufferedReaderWrapper, self).__init__(f_obj)
def addOutput(self, name, path, attrs=None): self.__outputs[name] = FileIO(path, attrs)
def __init__(self, f, **kwargs): buf = FileIO(f.fileno(), 'w') super(_Py3Utf8Output, self).__init__(buf, encoding='utf8', errors='strict')
) except UsageError as e: logging.error('Error: %s' %(str(e))) sys.exit(1) except Exception as e: logging.critical('Error when processing meta information about the codebase: %s' %(str(e))) logging.info(traceback.format_exc()) sys.exit(1) # Finally, do the conversion based on all the information above. for fileNum, fileInDir in enumerate(filesInDir): outputPath = os.path.join(os.path.normpath(options.outputDir), os.path.splitext(os.path.basename(fileInDir))[0] + ".P90.temp") printProgressIndicator(sys.stderr, fileInDir, fileNum + 1, len(filesInDir), "Converting to Standard Fortran") outputStream = FileIO(outputPath, mode="wb") try: converter = H90toF90Converter( ImmutableDOMDocument(cgDoc), #using our immutable version we can speed up ALL THE THINGS through caching implementationsByTemplateName, outputStream, moduleNodesByName, parallelRegionData, symbolAnalysisByRoutineNameAndSymbolName, symbolsByModuleNameAndSymbolName, symbolsByRoutineNameAndSymbolName, ) converter.processFile(fileInDir) except UsageError as e: logging.error('Error: %s' %(str(e))) sys.exit(1)
import os, sys, datetime, colored from time import sleep from io import FileIO report_fd = os.open("/dev/input/js1", os.O_RDWR | os.O_NONBLOCK) fd = FileIO(report_fd, "rb+", closefd=False) defBuf = bytearray(230) while True: buf = defBuf r = fd.readinto(buf) key = [] arr = [] sign = buf[2] i = 0 while i < len(buf): try: if (buf[i] == sign and buf[i + 1] == 0): key.append(i + 2) key.append(i + 3) except Exception as e: pass i += 1 i = 0 if (buf[5] != 0): arr.append(colored.bg("blue") + "!!" + colored.attr("reset"))
def __init__(self, shortpath, filepath, filemode=None, logger=None): TextIOWrapper.__init__(self, FileIO(filepath, 'a'), encoding='utf-8') self.shortpath = shortpath self.filepath = filepath self._log = logger self._filemode = filemode
def main(): """Adds pdf files to Google Drive folder and downloads their text representations. Pdf files will be uploaded and automatically converted to Google Docs. They will then be downloaded as txt files. Returns: Nothing, local folder will be populated with text files """ # authorize access and create Google Drive API service object credentials = get_credentials() http = credentials.authorize(httplib2.Http()) drive_service = discovery.build('drive', 'v3', http=http) # retrieve the file ID of a folder called Operating_Reports (manually created) # below code based off of https://developers.google.com/drive/v3/web/search-parameters response = drive_service.files().list(q="name='Operating_Reports'", spaces='drive', fields='files(id, name)').execute() file = response.get('files', [])[0] folder_id = file.get('id') print('Found file: {0} ({1})'.format(file.get('name'), folder_id)) reports_dir = "../data/operating_reports/pdf_files/" dest_dir = "../data/operating_reports/text_files/" # convert each pdf file in reports_dir to a txt file for item in os.listdir(reports_dir): file_path = reports_dir + item file_name = item[:-4] # upload pdf file to folder and convert to google doc # below code based off of https://developers.google.com/drive/v3/web/manage-uploads file_metadata = { 'name': file_name, 'mimeType': 'application/vnd.google-apps.document', 'parents': [folder_id] } media = MediaFileUpload(file_path, mimetype='application/pdf') file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() file_id = file.get('id') print('File ID: {0}'.format(file_id)) # download converted google doc as text file # below code based off of https://developers.google.com/drive/v3/web/manage-downloads # https://stackoverflow.com/questions/36173356/google-drive-api-download-files-python-no-files-downloaded file_dest = dest_dir + file_name + ".txt" request = drive_service.files().export_media(fileId=file_id, mimeType='text/plain') fh = FileIO(file_dest, 'wb') downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print("Download %d%%." % int(status.progress() * 100)) print('Files Successfully Converted')
# List available commands print("Available commands are:\n"+ "help\tinstall\tuninstall") elif(command == 'install'): print("Attempting to install...") # Check if the current user's bin directory exists exists = path.isdir(INSTALL_FOLDER) # Create directory if it doesn't exist if not exists: mkdir(INSTALL_FOLDER) # Create launcher file try: file = FileIO(launcher_path, "wb") except PermissionError: print("Error: Insufficient permissions.") exit() run_command = 'python3 ' + getcwd() + '/main.py $1 $2 $3\n' file.write('#!/bin/bash\n'.encode()) file.write(run_command.encode()) file.close() # Make launcher executable chmod(launcher_path, 0o777) # Try sourcing the .profile file if it exists source_profile()
def analyse_device_events(file_path, device): # this is where the pcaps are stored dir_path = Path(file_path) / device command_folders = Path(dir_path) country = str(file_path)[ -2:] # either the us or uk depends on the file path iot_devices = get_iot_devices(country) path = r"C:\Users\amith\Documents\Uni\Masters\Implementation\commands" def create_folders(): for obj in iot_devices: path = Path(path) / country / obj folder = Path(path) if folder.is_dir(): pass else: folder.mkdir() iot_objects = {} non_iot_objects = {} for command in command_folders.iterdir(): # if file.name in device_events[device]: # print(file.name) iot_objects[command.name] = [] non_iot_objects[command.name] = [] count = 0 for pcap in command.iterdir(): # print(pcap.name) # if pcap.name[0:-5] in device_events[device][file.name]: if count > 5: break print("analysing pcap", pcap.name) traffic_file = NetworkTrace(pcap, devices=iot_devices) analyse_pcap(traffic_file, FileIO(pcap), ttl="ttl") print("creating device objs from pcap") device_list = get_device_objects(traffic_file, [], []) for device_obj in device_list: if device_obj.mac_address == iot_devices[device]: iot_objects[command.name].append(device_obj) else: non_iot_objects[command.name].append(device_obj) # print("iot_objects:",iot_objects) event_traffic = get_reorganised_command_traffic_dict(iot_objects) # device_command_signature = PacketLevelSignature(event_traffic) # device_command_signature.cluster_event_traffic("on") def make_command_plot_folders(): for command_name in event_traffic: # Saving graphs in appropriate folders i.e. accroding to the command if len(event_traffic[command_name]['lan']) > 0 or len( event_traffic[command_name]['wan']) > 0: save_graph_path = Path(path) / country / device / command_name save_folder = Path(save_graph_path) if save_folder.is_dir(): pass else: save_folder.mkdir() # plot_command_traffic = model_device_behaviour(device_trafic_objects=iot_objects[command_name], dates=None, mal_flows={}, save_folder=save_folder, behaviour_type="benign") # if plot_command_traffic: # print(plot_command_traffic) # continue # make_command_plot_folders() model_command_traffic(iot_objects, country, device, path)
def save_image(user, url): if url: result = urlretrieve(url) user.image.save(os.path.basename(url), FileIO(result[0])) user.save()
#!~/anaconda3/bin/python from spot.Dome import Dome import datetime import os from io import BufferedWriter, FileIO import logging log = logging.getLogger('shutdown') logfile = '/Users/e298770/Developer/SpOT/DavisVP2/data/spot/weather/status_' + datetime.datetime.now().strftime('%m-%d-%y') + '.log' try: if os.path.exists(logfile): fd = BufferedWriter(FileIO(logfile, 'ab')) else: fd = BufferedWriter(FileIO(logfile, 'wb')) except IOError: log.error("Cannot create log file %s" % logfile) def main(): dome = Dome() state = dome.get_dome_state(byte=False) if state != 'Dome Closed': fd.write(bytes(state + ' - Continue Image Sequence\n', encoding='utf-8')) dome.disconnect() return 0 dome.refresh() state = dome.get_dome_state(byte=False) fd.write(bytes(datetime.datetime.now().strftime('%m-%d-%y %H:%M:%S '), encoding='utf-8')) if state == 'Dome Closed': fd.write(bytes(state+' - Skip Image Sequence\n', encoding='utf-8')) dome.disconnect()
def add_file_channel(self, name, file_name, level=LogLevel.INFO): """Add a channel with a given name to write to a file.""" self.add_channel(name, TextIOWrapper(FileIO(file_name, "w")), level) return self
def export_file(self, basedir): filename = os.path.join(basedir, self.filename) fileio = FileIO(filename, "w") self.export.draw(self, fileio) fileio.close() return filename
required=False, help="Use the dict parser") parser.add_argument('--transit-decode', dest='do_transit_decode', action='store_true', required=False, help="Transit decode") parser.add_argument('--debug', dest='do_debug', action='store_true', required=False, help="Debug") args = parser.parse_args() stream = FileIO(args.file_name, "rb") entities = 0 start_time = time.monotonic() if args.use_ijson: print("Using ijson parser") parser = IJSONParser(stream) elif args.use_string: print("Using string parser") parser = JSONStringParser(stream, debug=args.do_debug) elif args.use_dict: print("Using dict parser") parser = JSONDictParser(stream, debug=args.do_debug) else:
def __init__(self, name, mode='rb', bufsize=-1, streamsize=1 << 16, start=0, end=None): self._io = FileIO(name, mode=mode) self.streamsize = streamsize self.start = start self.end = end self._io.seek(start, 0)
def __init__(self, filepath, mode, *_args, **_kwargs): """Set some additional attributes to support staging.""" file_obj = FileIO(filepath, mode) super(ExtendedFile, self).__init__(file_obj) self._path = filepath self._staged = True
def write_results(results, results_file, impls): """ Writes test results from `results`, which complies with the following schema-by-example. { good: { 'test_file_1.ion': { 'ion-c_abcd123': { result: PASS }, 'ion-java_def4567': { result: PASS } }, 'test_file_2.ion': { 'ion-c_abcd123': { result: FAIL, read_error: ErrorReport::[{ error_type: READ, message: "ion_reader_text.c:999 Line 1 index 3: Repeated underscore in numeric value.", location: "test_file_2.ion" }] }, 'ion-java_def4567': { result: PASS } } }, bad: { 'test_file_3.ion': { 'ion-c_abcd123' : { result: FAIL, errors: [] }, 'ion-java_def4567': { result: PASS } } }, equivs: { 'test_file_4.ion': { 'ion-c_abcd123': { result: FAIL, read_compare: { errors: [], failures: ComparisonReport::[{ result: NOT_EQUAL, lhs: { location: "ion-c_abcd123.ion", event: { event_type: SCALAR, ion_type: INT, value_text: "1", value_binary: [0x21, 0x01], depth:1 }, event_index: 2 }, rhs: { location: "test_file_4.ion", event: { event_type: SCALAR, ion_type: INT, value_text: "2", value_binary: [0x21, 0x02], depth:1 }, event_index:2 }, message: "1 vs. 2" }] } }, 'ion-java_def4567': { result: FAIL, write_error: ErrorReport::[{ error_type: WRITE, message: "IonManagedBinaryWriter.java:999 UnsupportedOperationException", location: "test_file_4.ion" }] } } } } """ # NOTE: A lot of this is a hack necessitated by the fact that ion-python does not yet support pretty-printing Ion # text. Once it does, the only thing this method needs to do is 'dump' to results_file with pretty-printing enabled. if '.' in results_file: results_file_raw = results_file[0:results_file.rfind('.')] + '_raw.ion' else: results_file_raw = results_file + '_raw.ion' results_out = FileIO(results_file_raw, mode='wb') try: simpleion.dump(results, results_out, binary=False) finally: results_out.close() ionc = list(filter(lambda x: 'ion-c' in x.identifier, impls))[0] ionc.execute('process', '--output', results_file, results_file_raw)
def data(self, file_id, pip_ix): with FileIO(self.local_dir + file_id, 'r') as fin: fin.seek(pip_ix * self.pip_size) return fin.read(self.pip_size)
from io import BufferedReader, FileIO import os fi = FileIO('README.md') bb = BufferedReader(fi) result = bb.read() assert len(result) <= 8 * 1024 assert len(result) >= 0 assert isinstance(result, bytes) with FileIO('README.md') as fio: res = fio.read() assert len(result) <= 8 * 1024 assert len(result) >= 0 assert isinstance(result, bytes) fd = os.open('README.md', os.O_RDONLY) with FileIO(fd) as fio: res2 = fio.read() assert res == res2
def __init__(self, fd, encoding): self._fd = fd self._encoding = encoding self._source = BufferedReader(FileIO(self._fd))
def _get_sha1(path): # Using FileIO instead of open as fseeko with OFF_T=64 is broken in android NDK # See https://trac.kodi.tv/ticket/17827 with FileIO(path) as f: f.seek(-40, os.SEEK_END) return f.read()
def __init__(self, **kwargs): buf = FileIO(sys.stdout.fileno(), 'w') super(_Py3Utf8Stdout, self).__init__(buf, encoding='utf8', errors='strict')
def import_challenges(in_file, dst_attachments, move=False): from CTFd.models import db, Challenges, Flags, Tags, ChallengeFiles, Hints from CTFd.utils import uploads from CTFd.plugins.dynamic_challenges import DynamicChallenge, DynamicValueChallenge with open(in_file, "r") as in_stream: data = list(yaml.safe_load_all(in_stream)) if len(data) == 0 or data[0] is None or "challs" not in data[ 0] or data[0]["challs"] is None: raise ValueError("Invalid YAML format. Missing field 'challs'.") for chal in data[0]["challs"]: for req_field in REQ_FIELDS: if req_field not in chal: raise ValueError( "Invalid YAML format. Missing field '{0}'.".format( req_field)) if chal.get("type", "standard") == "dynamic": for req_field in ["minimum", "decay"]: if req_field not in chal: raise ValueError( "Invalid YAML format. Missing field '{0}'.".format( req_field)) if chal["flags"] is None: raise ValueError("Invalid YAML format. Missing field 'flag'.") for flag in chal["flags"]: if "flag" not in flag: raise ValueError( "Invalid YAML format. Missing field 'flag'.") flag["flag"] = flag["flag"].strip() if "type" not in flag: flag["type"] = "static" matching_chal = Challenges.query.filter_by( name=chal["name"].strip()).first() if matching_chal: print(("Updating {}: Duplicate challenge " "found in DB (id: {})").format( chal["name"].encode("utf8"), matching_chal.id)) Tags.query.filter_by(challenge_id=matching_chal.id).delete() ChallengeFiles.query.filter_by( challenge_id=matching_chal.id).delete() Flags.query.filter_by(challenge_id=matching_chal.id).delete() Hints.query.filter_by(challenge_id=matching_chal.id).delete() matching_chal.name = chal["name"].strip() matching_chal.description = chal["description"].strip() matching_chal.category = chal["category"].strip() if chal.get("type", "standard") == "standard": matching_chal.value = chal["value"] if chal.get("type", "standard") == "dynamic": matching_chal.minimum = chal["minimum"] matching_chal.decay = chal["decay"] matching_chal.initial = chal["value"] DynamicValueChallenge.calculate_value(matching_chal) db.session.commit() chal_dbobj = matching_chal else: print("Adding {}".format(chal["name"].encode("utf8"))) chal_type = chal.get("type", "standard") if chal_type == "standard": # We ignore traling and leading whitespace when # importing challenges chal_dbobj = Challenges( name=chal["name"].strip(), description=chal["description"].strip(), value=chal["value"], category=chal["category"].strip(), ) elif chal_type == "dynamic": # We ignore traling and leading whitespace when # importing challenges chal_dbobj = DynamicChallenge( name=chal["name"].strip(), description=chal["description"].strip(), category=chal["category"].strip(), value=int(chal["value"]), minimum=int(chal["minimum"]), decay=int(chal["decay"]), ) else: raise ValueError("Unknown type of challenge") db.session.add(chal_dbobj) db.session.commit() for tag in chal.get("tags", []): tag_dbobj = Tags(challenge_id=chal_dbobj.id, value=tag) db.session.add(tag_dbobj) for flag in chal["flags"]: flag_db = Flags(challenge_id=chal_dbobj.id, content=flag["flag"], type=flag["type"]) db.session.add(flag_db) for hint in chal.get("hints", []): hint_dbobj = Hints(challenge_id=chal_dbobj.id, content=hint["content"], cost=hint["cost"]) db.session.add(hint_dbobj) chal_dbobj.state = "hidden" if ( "hidden" in chal and chal["hidden"] == True) else "visible" chal_dbobj.max_attempts = chal[ "max_attempts"] if "max_attempts" in chal else 0 if "files" in chal: from io import FileIO for filename in chal["files"]: try: # upload_file takes a werkzeug.FileStorage object, but we # can get close enough with a file object with a # filename property added filepath = os.path.join(os.path.dirname(in_file), filename) with FileIO(filepath, mode="rb") as f: f.filename = os.path.basename(f.name) uploads.upload_file(file=f, challenge=chal_dbobj.id, type="challenge") except FileNotFoundError: raise ValueError( "Unable to import challenges. Missing file: " + filename) db.session.commit() # update challenge prerequisites after all the challenges were created with open(in_file, "r") as in_stream: data = list(yaml.safe_load_all(in_stream)) for chal in data[0]["challs"]: chal_dbobj = Challenges.query.filter_by( name=chal["name"].strip()).first() prerequisites = set() for prerequisite in chal.get("prerequisites", []): prerequisites.update([ c.id for c in Challenges.query.filter_by( name=prerequisite).all() ]) chal_dbobj.requirements = {"prerequisites": list(prerequisites)} db.session.commit() db.session.close()
import sys import io from io import FileIO inputFileName = sys.argv[1] outputFileName = sys.argv[2] nInLine = 10 if len(sys.argv) >= 4: nInLine = int(sys.argv[4]) inputStream = FileIO(inputFileName, 'r') outputFile = open(outputFileName, 'w') inputData = inputStream.readall() inputStream.close() outputStr = '' n = 0 for x in inputData: outputStr += str(ord(x)).rjust(3) + ", " n += 1 if n >= nInLine: outputStr += '\n' n = 0 outputFile.write(outputStr) outputFile.close()
def _complete_registration(self, msg, connect, maybe_tunnel): ctx = self.context loop = self.loop identity = self.bident idents, msg = self.session.feed_identities(msg) msg = self.session.deserialize(msg) content = msg['content'] info = self.connection_info def url(key): """get zmq url for given channel""" return str(info["interface"] + ":%i" % info[key]) def urls(key): return [f'{info["interface"]}:{port}' for port in info[key]] if content['status'] == 'ok': requested_id = self.id self.id = content['id'] if requested_id is not None and self.id != requested_id: self.log.warning( f"Did not get the requested id: {self.id} != {requested_id}" ) self.log.name = self.log.name.rsplit(".", 1)[0] + f".{self.id}" elif self.id is None: self.log.name += f".{self.id}" # create Shell Connections (MUX, Task, etc.): # select which broadcast endpoint to connect to # use rank % len(broadcast_leaves) broadcast_urls = urls('broadcast') broadcast_leaves = len(broadcast_urls) broadcast_index = self.id % len(broadcast_urls) broadcast_url = broadcast_urls[broadcast_index] shell_addrs = [url('mux'), url('task'), broadcast_url] self.log.info(f'Shell_addrs: {shell_addrs}') # Use only one shell stream for mux and tasks stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) stream.setsockopt(zmq.IDENTITY, identity) # TODO: enable PROBE_ROUTER when schedulers can handle the empty message # stream.setsockopt(zmq.PROBE_ROUTER, 1) self.log.debug("Setting shell identity %r", identity) shell_streams = [stream] for addr in shell_addrs: self.log.info("Connecting shell to %s", addr) connect(stream, addr) # control stream: control_url = url('control') curve_serverkey = self.curve_serverkey if self.enable_nanny: nanny_url, self.nanny_pipe = self.start_nanny( control_url=control_url, ) control_url = nanny_url # nanny uses our curve_publickey, not the controller's publickey curve_serverkey = self.curve_publickey control_stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) control_stream.setsockopt(zmq.IDENTITY, identity) connect(control_stream, control_url, curve_serverkey=curve_serverkey) # create iopub stream: iopub_addr = url('iopub') iopub_socket = ctx.socket(zmq.PUB) iopub_socket.SNDHWM = 0 iopub_socket.setsockopt(zmq.IDENTITY, identity) connect(iopub_socket, iopub_addr) try: from ipykernel.iostream import IOPubThread except ImportError: pass else: iopub_socket = IOPubThread(iopub_socket) iopub_socket.start() # disable history: self.config.HistoryManager.hist_file = ':memory:' # Redirect input streams and set a display hook. if self.out_stream_factory: sys.stdout = self.out_stream_factory( self.session, iopub_socket, 'stdout' ) sys.stdout.topic = f"engine.{self.id}.stdout".encode("ascii") sys.stderr = self.out_stream_factory( self.session, iopub_socket, 'stderr' ) sys.stderr.topic = f"engine.{self.id}.stderr".encode("ascii") # copied from ipykernel 6, which captures sys.__stderr__ at the FD-level if getattr(sys.stderr, "_original_stdstream_copy", None) is not None: for handler in self.log.handlers: if isinstance(handler, StreamHandler) and ( handler.stream.buffer.fileno() == 2 ): self.log.debug( "Seeing logger to stderr, rerouting to raw filedescriptor." ) handler.stream = TextIOWrapper( FileIO(sys.stderr._original_stdstream_copy, "w") ) if self.display_hook_factory: sys.displayhook = self.display_hook_factory(self.session, iopub_socket) sys.displayhook.topic = f"engine.{self.id}.execute_result".encode( "ascii" ) # patch Session to always send engine uuid metadata original_send = self.session.send def send_with_metadata( stream, msg_or_type, content=None, parent=None, ident=None, buffers=None, track=False, header=None, metadata=None, **kwargs, ): """Ensure all messages set engine uuid metadata""" metadata = metadata or {} metadata.setdefault("engine", self.ident) return original_send( stream, msg_or_type, content=content, parent=parent, ident=ident, buffers=buffers, track=track, header=header, metadata=metadata, **kwargs, ) self.session.send = send_with_metadata self.kernel = Kernel.instance( parent=self, engine_id=self.id, ident=self.ident, session=self.session, control_stream=control_stream, shell_streams=shell_streams, iopub_socket=iopub_socket, loop=loop, user_ns=self.user_ns, log=self.log, ) self.kernel.shell.display_pub.topic = f"engine.{self.id}.displaypub".encode( "ascii" ) # FIXME: This is a hack until IPKernelApp and IPEngineApp can be fully merged self.init_signal() app = IPKernelApp( parent=self, shell=self.kernel.shell, kernel=self.kernel, log=self.log ) if self.use_mpi and self.init_mpi: app.exec_lines.insert(0, self.init_mpi) app.init_profile_dir() app.init_code() self.kernel.start() else: self.log.fatal("Registration Failed: %s" % msg) raise Exception("Registration Failed: %s" % msg) self.start_heartbeat( maybe_tunnel(url('hb_ping')), maybe_tunnel(url('hb_pong')), content['hb_period'], identity, ) self.log.info("Completed registration with id %i" % self.id) self.loop.remove_timeout(self._abort_timeout)
if not os.path.exists(OUT_PATH): os.makedirs(OUT_PATH) storage = Storage(CREDS_FILE) credentials = storage.get() if credentials is None: # Run through the OAuth flow and retrieve credentials flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() print 'Go to the following link in your browser: ' + authorize_url code = raw_input('Enter verification code: ').strip() credentials = flow.step2_exchange(code) storage.put(credentials) # Create an httplib2.Http object and authorize it with our credentials httpq = credentials.authorize(Http()) drive_service = build('drive', 'v3', http=httpq) file_id = sys.argv[1] request = drive_service.files().export_media( fileId=file_id, mimeType= 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') fh = FileIO('test.docx', 'wb') downloader = http.MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print "Download %d%%." % int(status.progress() * 100)
from geometry_msgs.msg import PointStamped, PoseStamped import User import logging import sys from io import FileIO, BufferedWriter LoggerWarningLevel = logging.DEBUG # LoggerWarningLevel = logging.INFO # LoggerWarningLevel = logging.WARNING # LoggerWarningLevel = logging.ERROR logger = logging.getLogger('User_message') fileHandler_message = logging.StreamHandler(BufferedWriter(FileIO("User_message" + time.strftime("%Y%m%d-%H%M%S") + ".log", "w"))) logger.addHandler(fileHandler_message) formatter_message = logging.Formatter('%(asctime)s %(levelname)s %(message)s') fileHandler_message.setFormatter(formatter_message) logger.setLevel(LoggerWarningLevel) def sigint_handler(): logger.warning("shutdown!") def display_user_menu(): print("\n")
def save_upload(request, uploaded, filename, raw_data, msg_type): """ raw_data: if True, upfile is a HttpRequest object with raw post data as the file, rather than a Django UploadedFile from request.FILES """ try: user = get_user_from_token(request) if user is None: return False, -2 input_file_name = filename (fileBaseName, fileExtension) = os.path.splitext(filename) real_file_name = str(user.id) + "_" + str(uuid.uuid1()) + fileExtension filename = os.path.normpath( os.path.join(settings.MEDIA_ROOT + '/files/', real_file_name)) with BufferedWriter(FileIO(filename + '.temp', "w")) as dest: # if the "advanced" upload, read directly from the HTTP request # with the Django 1.3 functionality if raw_data: (dirName, fileName) = os.path.split(filename) (fileBaseName, fileExtension) = os.path.splitext(fileName) fileExtension = fileExtension[1:] # print fileName+"['"+fileBaseName+ "','"+fileExtension+"']" new_file = AttachedFile(file_type=fileExtension, file_name=input_file_name, uploader=user) new_file.file_contents.save(fileName, ContentFile(uploaded.read())) if msg_type == '1': make_thumbnail(new_file) # if not raw, it was a form upload so read in the normal Django chunks fashion else: (dirName, fileName) = os.path.split(filename) (fileBaseName, fileExtension) = os.path.splitext(fileName) fileExtension = fileExtension[1:] # print "2 " + fileName+"['"+fileBaseName+ "','"+fileExtension+"']" # TODO: figure out when this gets called, make it work to save into a Photo like above for c in uploaded.chunks(): dest.write(c) dest.close() # fileName2 = str(user.id) + "_" + str(uuid.uuid1()) +'.'+ fileExtension # new_file = AttachedFile(file_type=fileExtension,file_name=fileName2, uploader=user) # new_file.file_contents.save(fileName2,ContentFile(uploaded.read())) # new_file.save() new_file = AttachedFile(file_type=fileExtension, file_name=fileName, uploader=user) #fileName2 = user.username + "_" + str(uuid.uuid1()) +'.'+ fileExtension new_file.file_contents.save(fileName, File(open(filename + '.temp'))) new_file.save() os.remove(filename + '.temp') if msg_type == '1': make_thumbnail(new_file) except Exception as e: print str(e) # could not open the file most likely return False, -1 return True, new_file
def addInput(self, name, path, attrs=None): self.__inputs[name] = FileIO(path, attrs)