Sanitizes given python module.

    :param file: Python module file.
    :type file: unicode
    :return: Definition success.
    :rtype: bool
356358
def bleach(file):
    

    LOGGER.info("{0} | Sanitizing '{1}' python module!".format(__name__, file))

    source_file = File(file)
    content = source_file.read()
    for pattern in STATEMENTS_SUBSTITUTE:
        matches = [match for match in re.finditer(pattern, content, re.DOTALL)]

        offset = 0
        for match in matches:
            if any(map(lambda x: x in match.group("bleach"), STATEMENT_IGNORE)):
                continue

            start, end = match.start("bleach"), match.end("bleach")
            substitution = "{0}{1}".format(STATEMENT_UPDATE_MESSAGE,
                                           re.sub("\n", "\n{0}".format(STATEMENT_UPDATE_MESSAGE),
                                                  match.group("bleach")))
            content = "".join((content[0: start + offset],
                               substitution,
                               content[end + offset:]))
            offset += len(substitution) - len(match.group("bleach"))

    source_file.content = [content]
    source_file.write()

    return True
760886
def __get_file(self, file):
        
        file_object = None
        if file['name'] in request.files:
            file_object = request.files[file['name']]
            clean_filename = secure_filename(file_object.filename)
            if clean_filename == '':
                return file_object
            if file_object and self.__allowed_extension(
                    clean_filename, file['extensions']):
                return file_object
        elif file['name'] not in request.files and file['required']:
            return file_object
        return file_object
35761
def safe_py_code(code):
    '''
    
    '''
    bads = (
            'import',
            ';',
            'subprocess',
            'eval',
            'open',
            'file',
            'exec',
            'input')
    for bad in bads:
        if code.count(bad):
            return False
    return True
703843
def sanitize_gff_file(gff_fname,
                      in_memory=True,
                      in_place=False):
    
    db = None
    if is_gff_db(gff_fname):
        # It's a database filename, so load it
        db = gffutils.FeatureDB(gff_fname)
    else:
        # Need to create a database for file
        if in_memory:
            db = gffutils.create_db(gff_fname, ":memory:",
                                    verbose=False)
        else:
            db = get_gff_db(gff_fname)
    if in_place:
        gff_out = gffwriter.GFFWriter(gff_fname,
                                      in_place=in_place)
    else:
        gff_out = gffwriter.GFFWriter(sys.stdout)
    sanitized_db = sanitize_gff_db(db)
    for gene_rec in sanitized_db.all_features(featuretype="gene"):
        gff_out.write_gene_recs(sanitized_db, gene_rec.id)
    gff_out.close()
823672
def convert(string, sanitize=False):
    
    return r.convert(string, (preprocess if sanitize else False))
705989
public boolean filterFile(File file)
    {
        String strName = file.getName();
        if (!strName.endsWith(".java"))
            return false;
        return super.filterFile(file);
    }
794508
def clean_file(self):
        
        data = self.cleaned_data['file']

        available_parsers = self.get_parsers()

        for parser in available_parsers:
            try:
                return parser.parse_file(data)
            except parsers.ParserError:
                pass

        raise forms.ValidationError(
            "No parser could read the file. Tried with parsers %s." %
            (", " % (force_text(p) for p in available_parsers)))
88098
def check_file_encoding(self, input_file_path):
        
        self.log([u"Checking encoding of file '%s'", input_file_path])
        self.result = ValidatorResult()
        if self._are_safety_checks_disabled(u"check_file_encoding"):
            return self.result
        if not gf.file_can_be_read(input_file_path):
            self._failed(u"File '%s' cannot be read." % (input_file_path))
            return self.result
        with io.open(input_file_path, "rb") as file_object:
            bstring = file_object.read()
            self._check_utf8_encoding(bstring)
        return self.result
1327893
def normalize(file)
      if defined?(CarrierWave::Uploader::Base) && file.is_a?(CarrierWave::Uploader::Base)
        file = file.file
      end

      if defined?(Paperclip) && file.is_a?(Paperclip::Attachment)
        if file.options[:storage] == :filesystem
          {file: File.open(file.path)}
        else
          {url: file.expiring_url}
        end
      elsif defined?(CarrierWave::Storage::Fog::File) && file.is_a?(CarrierWave::Storage::Fog::File)
        {url: file.url}
      elsif defined?(CarrierWave::SanitizedFile) && file.is_a?(CarrierWave::SanitizedFile)
        {file: File.open(file.path)}
      elsif is_io?(file)
        {file: file}
      elsif defined?(ActiveStorage::Blob) && file.is_a?(ActiveStorage::Blob)
        {url: file.service_url}
      elsif is_active_storage_attachment?(file) || is_active_storage_one?(file)
        {url: file.blob.service_url}
      elsif file.respond_to? :url
        {url: file.url}
      elsif file.respond_to? :path
        {file: File.open(file.path)}
      elsif file.respond_to? :file
        {file: File.open(file.file)}
      else
        raise(ArgumentError, 'Bad File/Stream')
      end
    end
389994
def _SanitizeField(self, field):
    
    if self._field_delimiter and isinstance(field, py2to3.STRING_TYPES):
      return field.replace(self._field_delimiter, ' ')
    return field
1113722
def _ensure_unicode(text):
    
    if isinstance(text, six.binary_type):
        return text.decode(sys.getfilesystemencoding(), 'replace')
    else:
        return text
112082
private static boolean isValidJavaClassFile(String file) {
        if (!file.endsWith(".class")) return false;
        String clazzName = file.substring(0, file.length() - ".class".length());
        return isValidClassName(clazzName);
    }
776801
def withFile(file, func, mode='r', expand=False):
    
    file = _normalizeToFile(file, mode=mode, expand=expand)
    try:      return func(file)
    finally:  file.close()
983435
def sanitize(meta, diagnostics=False):
    
    bad_encodings, bad_fields = set(), set()

    def sane_encoding(field, text):
        "Transcoding helper."
        for encoding in ('utf-8', meta.get('encoding', None), 'cp1252'):
            if encoding:
                try:
                    u8_text = text.decode(encoding).encode("utf-8")
                    if encoding != 'utf-8':
                        bad_encodings.add(encoding)
                        bad_fields.add(field)
                    return u8_text
                except UnicodeError:
                    continue
        else:
            # Broken beyond anything reasonable
            bad_encodings.add('UNKNOWN/EXOTIC')
            bad_fields.add(field)
            return str(text, 'utf-8', 'replace').replace('\ufffd', '_').encode("utf-8")

    # Go through all string fields and check them
    for field in ("comment", "created by"):
        if field in meta:
            meta[field] = sane_encoding(field, meta[field])

    meta["info"]["name"] = sane_encoding('info name', meta["info"]["name"])

    for entry in meta["info"].get("files", []):
        entry["path"] = [sane_encoding('file path', i) for i in entry["path"]]

    return (meta, bad_encodings, bad_fields) if diagnostics else meta
425400
function sanitize(lang, filter) {
  if(!(typeof lang == 'string')) return lang;
  lang = lang.replace(/\..*$/, '').toLowerCase();
  if(typeof filter == 'function') {
    lang = filter(lang);
  }
  return lang;
}
538695
def use_file(
    filename: typing.Union[Path, str, None], trust: bool = False
) -> typing.Union[types.ModuleType, None]:
    
    config_file = filename or resolve_path(CONFIG_FILE)

    def preview_unauthorized() -> None:
        if not config_file:
            return None
        print(SEPARATOR, file=sys.stderr)
        with Path(config_file).open("r", encoding="utf-8") as fp:
            for line in fp:
                print(line, end="", file=sys.stderr)
        print(SEPARATOR, file=sys.stderr)

    if config_file and not Path(config_file).exists():
        print_error(f'"{filename}" not found.')
        sys.exit(1)
    if config_file and Path(config_file).exists():
        if not trust:
            with AuthFile.load() as authfile:
                try:
                    authfile.check(Path(config_file))
                except KonchrcChangedError:
                    print_error(f'"{config_file}" has changed since you last used it.')
                    preview_unauthorized()
                    if confirm("Would you like to authorize it?"):
                        authfile.allow(Path(config_file))
                        print()
                    else:
                        sys.exit(1)
                except KonchrcNotAuthorizedError:
                    print_error(f'"{config_file}" is blocked.')
                    preview_unauthorized()
                    if confirm("Would you like to authorize it?"):
                        authfile.allow(Path(config_file))
                        print()
                    else:
                        sys.exit(1)

        logger.info(f"Using {config_file}")
        # Ensure that relative imports are possible
        __ensure_directory_in_path(Path(config_file))
        mod = None
        try:
            mod = imp.load_source("konchrc", str(config_file))
        except UnboundLocalError:  # File not found
            pass
        else:
            return mod
    if not config_file:
        print_warning("No konch config file found.")
    else:
        print_warning(f'"{config_file}" not found.')
    return None
636213
def to_file(file_):
        
        from sevenbridges.models.file import File
        if not file_:
            raise SbgError('File is required!')
        elif isinstance(file_, File):
            return file_.id
        elif isinstance(file_, six.string_types):
            return file_
        else:
            raise SbgError('Invalid file parameter!')