Module sertit.arcpy
Expand source code
import logging
import logging.handlers
# flake8: noqa
def init_conda_arcpy_env():
"""
Initialize conda environment with Arcgis Pro
"""
# Try importing lxml
try:
from lxml import etree
except ImportError:
import os
import sys
if "python" in sys.executable:
root_dir = os.path.dirname(sys.executable)
else:
import subprocess
try:
conda_env_list = subprocess.run(
"conda env list", capture_output=True, shell=True, encoding="UTF-8"
).stdout
conda_env_list = conda_env_list.split("\n")
curr_env = [env for env in conda_env_list if "*" in env][0]
root_dir = [elem for elem in curr_env.split(" ") if elem][-1]
except IndexError:
os_file = os.__file__
root_dir = os.path.dirname(os.path.dirname(os_file))
except Exception:
raise ImportError(
"Cannot import lxml. Please try 'pip uninstall lxml -y' then 'pip install lxml'."
)
os.environ["PATH"] = root_dir + r"\Library\bin;" + os.environ["PATH"]
print(f"Missing lxml DLLs. Completing PATH: {os.environ['PATH']}")
from lxml import etree # Try again
class ArcPyLogHandler(logging.handlers.RotatingFileHandler):
"""
Custom logging class that bounces messages to the arcpy tool window as well
as reflecting back to the file.
"""
def emit(self, record):
"""
Write the log message
"""
import arcpy
try:
msg = record.msg % record.args
except:
try:
msg = record.msg.format(record.args)
except:
msg = record.msg
if record.levelno >= logging.ERROR:
arcpy.AddError(msg)
elif record.levelno >= logging.WARNING:
arcpy.AddWarning(msg)
elif record.levelno >= logging.INFO:
arcpy.AddMessage(msg)
super(ArcPyLogHandler, self).emit(record)
Functions
def init_conda_arcpy_env(
)
-
Initialize conda environment with Arcgis Pro
Expand source code
def init_conda_arcpy_env(): """ Initialize conda environment with Arcgis Pro """ # Try importing lxml try: from lxml import etree except ImportError: import os import sys if "python" in sys.executable: root_dir = os.path.dirname(sys.executable) else: import subprocess try: conda_env_list = subprocess.run( "conda env list", capture_output=True, shell=True, encoding="UTF-8" ).stdout conda_env_list = conda_env_list.split("\n") curr_env = [env for env in conda_env_list if "*" in env][0] root_dir = [elem for elem in curr_env.split(" ") if elem][-1] except IndexError: os_file = os.__file__ root_dir = os.path.dirname(os.path.dirname(os_file)) except Exception: raise ImportError( "Cannot import lxml. Please try 'pip uninstall lxml -y' then 'pip install lxml'." ) os.environ["PATH"] = root_dir + r"\Library\bin;" + os.environ["PATH"] print(f"Missing lxml DLLs. Completing PATH: {os.environ['PATH']}") from lxml import etree # Try again
Classes
class ArcPyLogHandler (filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)
-
Custom logging class that bounces messages to the arcpy tool window as well as reflecting back to the file.
Open the specified file and use it as the stream for logging.
By default, the file grows indefinitely. You can specify particular values of maxBytes and backupCount to allow the file to rollover at a predetermined size.
Rollover occurs whenever the current log file is nearly maxBytes in length. If backupCount is >= 1, the system will successively create new files with the same pathname as the base file, but with extensions ".1", ".2" etc. appended to it. For example, with a backupCount of 5 and a base file name of "app.log", you would get "app.log", "app.log.1", "app.log.2", … through to "app.log.5". The file being written to is always "app.log" - when it gets filled up, it is closed and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc. exist, then they are renamed to "app.log.2", "app.log.3" etc. respectively.
If maxBytes is zero, rollover never occurs.
Expand source code
class ArcPyLogHandler(logging.handlers.RotatingFileHandler): """ Custom logging class that bounces messages to the arcpy tool window as well as reflecting back to the file. """ def emit(self, record): """ Write the log message """ import arcpy try: msg = record.msg % record.args except: try: msg = record.msg.format(record.args) except: msg = record.msg if record.levelno >= logging.ERROR: arcpy.AddError(msg) elif record.levelno >= logging.WARNING: arcpy.AddWarning(msg) elif record.levelno >= logging.INFO: arcpy.AddMessage(msg) super(ArcPyLogHandler, self).emit(record)
Ancestors
- logging.handlers.RotatingFileHandler
- logging.handlers.BaseRotatingHandler
- logging.FileHandler
- logging.StreamHandler
- logging.Handler
- logging.Filterer
Methods
def emit(
self,
record)-
Write the log message
Expand source code
def emit(self, record): """ Write the log message """ import arcpy try: msg = record.msg % record.args except: try: msg = record.msg.format(record.args) except: msg = record.msg if record.levelno >= logging.ERROR: arcpy.AddError(msg) elif record.levelno >= logging.WARNING: arcpy.AddWarning(msg) elif record.levelno >= logging.INFO: arcpy.AddMessage(msg) super(ArcPyLogHandler, self).emit(record)