"""Mixin*****"""importjsonimportloggingimportosfromdjango.contrib.staticfilesimportfindersfrom..modelimportManifest# Set the logger related to styleguide applogger=logging.getLogger("py-css-styleguide")
[docs]classStyleguideMixin:""" A mixin to return a manifest object. """
[docs]defresolve_css_filepath(self,path):""" Validate path or resolve static filepath if needed. Arguments: path (string): Either an absolute path or a relative path to an enabled static directory. Returns: string: Resolved path if success, else return None. """# Absolute path, just check it existsifos.path.isabs(path):ifos.path.exists(path):returnpathreturnNoneelse:resolved=finders.find(path)ifresolved:returnresolvedreturnNone
[docs]defget_css_manifest(self,manifest,path,json_filepath=None,save_dump=False):""" From given path, load CSS with manifest model and possibly save it if required. Arguments: manifest (py_css_styleguide.model.Manifest): Manifest model object to use to parse CSS manifest. path (string): Path to the CSS manifest. If this is a relative path, it is assumed it is a file inside static directory to resolve by Django static finder. Give a full absolute path if your file is out of the enabled static directories. Keyword Arguments: json_filepath (string): Absolute filepath for JSON dump destination. If empty, no dump will be created. save_dump (boolean): To enable manifest JSON dump write. This can only works if CSS manifest has been correctly loaded. Returns: py_css_styleguide.model.Manifest: The manifest object with loaded references. """# Update status flagmanifest.status="live"# Ensure the path exists or try to resolve path if neededresolved_path=self.resolve_css_filepath(path)ifresolved_path:# Open and parse CSSwithopen(resolved_path,"r")asfp:manifest.load(fp)# Save JSON manifest dump if requiredifsave_dumpandjson_filepath:withopen(json_filepath,"w")asfp:fp.write(manifest.to_json())else:# Log CSS load fail detailsmanifest.status="failed"msg="Unable to find CSS manifest from: {}"logger.warning(msg.format(path))manifest.loading_error=msg.format(path)returnmanifest
[docs]defget_json_manifest(self,manifest,path):""" From given path, load JSON manifest dump. Arguments: manifest (py_css_styleguide.model.Manifest): Manifest model object to use to parse CSS manifest. path (string): Path to the CSS manifest. Returns: py_css_styleguide.model.Manifest: The manifest object with loaded references. """# Update status flagmanifest.status="dump"try:withopen(path,"r")asfp:content=json.load(fp)exceptFileNotFoundError:# Log detailsmsg="Unable to find JSON manifest from: {}"logger.warning(msg.format(path))manifest.loading_error=msg.format(path)manifest.status="failed"exceptjson.decoder.JSONDecodeErrorase:# Log detailsmsg="Invalid JSON manifest: {}"logger.warning(msg.format(str(e)))manifest.loading_error=msg.format(str(e))manifest.status="failed"else:# Load dump from manifest model object "from dict"manifest.from_dict(content)returnmanifest
[docs]defget_manifest(self,css_filepath,json_filepath=None,save_dump=True,development_mode=True):""" Get and load manifest, either from CSS or JSON file depending options. Arguments: css_filepath (string): Path to CSS manifest file. Either an absolute path or a relative path to an enabled static directory. Keyword Arguments: json_filepath (string): Path to JSON manifest (to read or write). save_dump (boolean): To enable manifest JSON dump write. This can only works if CSS manifest has been correctly loaded and ``json_filepath`` has been given. development_mode (boolean): In development mode, CSS manifest is readed if it exists then a JSON dump may be written depending ``save_dump``. Returns: py_css_styleguide.model.Manifest: Manifest object. """# Init manifest with additional flags for loading status and possible errormanifest=Manifest()# Default mode flag assume CSS is parsedmanifest.status="empty"# No error by defaultmanifest.loading_error=Noneifdevelopment_mode:manifest=self.get_css_manifest(manifest,css_filepath,json_filepath=json_filepath,save_dump=save_dump)ifmanifest.statusin["empty","failed"]andjson_filepath:manifest=self.get_json_manifest(manifest,json_filepath)returnmanifest